Inventory.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #include <Critical.h>
  3. #include <HashMap.h>
  4. #include <ImmutablePair.h>
  5. #include <ReferenceCounter.h>
  6. #include <Vec3.h>
  7. #include "Area.h"
  8. #include "ItemSlot.h"
  9. class ItemFilter;
  10. class Inventory;
  11. class NetworkMessage;
  12. class Entity;
  13. class InventoryInteraction
  14. {
  15. private:
  16. Inventory* current;
  17. Inventory* other;
  18. Direction dir;
  19. void lock();
  20. void unlock();
  21. void transaction(Inventory* zSource,
  22. Inventory* zTarget,
  23. ItemFilter* zFilter,
  24. Direction sourceView,
  25. Direction targetView,
  26. int count);
  27. public:
  28. InventoryInteraction(Inventory* zCurrent, Inventory* zOther, Direction dir);
  29. InventoryInteraction(const InventoryInteraction& interaction);
  30. ~InventoryInteraction();
  31. InventoryInteraction& operator=(const InventoryInteraction& data);
  32. void endInteraction();
  33. void pullItems(int count, ItemFilter* zFilter);
  34. void pushItems(int count, ItemFilter* zFilter);
  35. };
  36. class Inventory : public virtual Framework::ReferenceCounter
  37. {
  38. private:
  39. Framework::Array<Framework::ImmutablePair<int, Framework::Text>> observers;
  40. Framework::RCArray<ItemSlot>* pullSlotsOrder;
  41. Framework::RCArray<ItemSlot>* pushSlotsOrder;
  42. Framework::HashMap<int, Framework::Array<ItemSlot*>*>* itemCache;
  43. Framework::Critical cs;
  44. Framework::Array<std::function<void(
  45. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>>
  46. afterPullStackCalls;
  47. Framework::Array<std::function<void(
  48. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>>
  49. afterPushStackCalls;
  50. Framework::Array<std::function<void(Entity* zSource, Framework::Text id)>>
  51. observerAddedCalls;
  52. int nextSlotId;
  53. void updateCache(ItemSlot* zSlot, int beforeKey);
  54. protected:
  55. Framework::Vec3<float> location;
  56. virtual bool allowPullStack(ItemSlot* zSlot, Direction dir) const;
  57. virtual bool allowPushStack(
  58. ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const;
  59. virtual void afterPullStack(
  60. ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  61. virtual void afterPushStack(
  62. ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  63. virtual void loadInventory(Framework::StreamReader* zReader);
  64. virtual void saveInventory(Framework::StreamWriter* zWriter);
  65. void removeObserver(Entity* zSource, Framework::Text id);
  66. void addObserver(Entity* zSource, Framework::Text id);
  67. virtual void addItems(ItemStack* zItems, Direction dir);
  68. public:
  69. Inventory(const Framework::Vec3<float> location, bool hasInventory);
  70. virtual ~Inventory();
  71. void notyObservers(NetworkMessage* msg);
  72. const ItemSlot* zSlot(int id) const;
  73. void addSlot(ItemSlot* slot);
  74. void localTransaction(Framework::Array<ItemSlot*>* zSourceSlots,
  75. Framework::Array<ItemSlot*>* zTargetSlots,
  76. ItemFilter* zFilter,
  77. int count,
  78. Direction outDir,
  79. Direction inDir);
  80. ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
  81. virtual void addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir);
  82. InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
  83. void unsaveAddItem(ItemStack* zStack, Direction dir);
  84. int numberOfAddableItems(const ItemStack* zStack, Direction dir) const;
  85. Framework::Iterator<ItemSlot*> begin();
  86. Framework::Iterator<ItemSlot*> end();
  87. void inventoryApi(Framework::StreamReader* zRequest,
  88. NetworkMessage* zResponse,
  89. Entity* zSource);
  90. void registerAfterPullStackCall(std::function<void(
  91. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  92. call);
  93. void registerAfterPushStackCall(std::function<void(
  94. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  95. call);
  96. void registerObserverAddedCall(
  97. std::function<void(Entity* zSource, Framework::Text id)> call);
  98. friend InventoryInteraction;
  99. private:
  100. static bool unsafeMove(Inventory* zSource,
  101. Inventory* zTarget,
  102. Framework::Iterator<ItemSlot*>& sourceSlot,
  103. Framework::Iterator<ItemSlot*>& targetSlot,
  104. Direction outDir,
  105. Direction inDir,
  106. int& count);
  107. };