Inventory.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 MultipleInventoryLock
  37. {
  38. private:
  39. Inventory** inventories;
  40. int count;
  41. bool locked;
  42. public:
  43. MultipleInventoryLock(Inventory** inventories, int count);
  44. ~MultipleInventoryLock();
  45. void unlock();
  46. void lock();
  47. };
  48. class Inventory : public virtual Framework::ReferenceCounter
  49. {
  50. private:
  51. Framework::Array<Framework::ImmutablePair<int, Framework::Text>> observers;
  52. Framework::RCArray<ItemSlot>* pullSlotsOrder;
  53. Framework::RCArray<ItemSlot>* pushSlotsOrder;
  54. Framework::HashMap<int, Framework::Array<ItemSlot*>*>* itemCache;
  55. Framework::Critical cs;
  56. Framework::Array<std::function<void(
  57. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>>
  58. afterPullStackCalls;
  59. Framework::Array<std::function<void(
  60. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>>
  61. afterPushStackCalls;
  62. Framework::Array<std::function<void(Entity* zSource, Framework::Text id)>>
  63. observerAddedCalls;
  64. int nextSlotId;
  65. void updateCache(ItemSlot* zSlot, int beforeKey);
  66. protected:
  67. Framework::Vec3<float> location;
  68. virtual bool allowPullStack(ItemSlot* zSlot, Direction dir) const;
  69. virtual bool allowPushStack(
  70. ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const;
  71. virtual void afterPullStack(
  72. ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  73. virtual void afterPushStack(
  74. ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  75. virtual void loadInventory(Framework::StreamReader* zReader);
  76. virtual void saveInventory(Framework::StreamWriter* zWriter);
  77. void removeObserver(Entity* zSource, Framework::Text id);
  78. void addObserver(Entity* zSource, Framework::Text id);
  79. virtual void addItems(
  80. ItemStack* zItems, Direction dir, ItemFilter* zFilter);
  81. void lock();
  82. void unlock();
  83. public:
  84. Inventory(const Framework::Vec3<float> location, bool hasInventory);
  85. virtual ~Inventory();
  86. void notifyObservers(NetworkMessage* msg);
  87. const ItemSlot* zSlot(int id) const;
  88. void addSlot(ItemSlot* slot);
  89. void localTransaction(Framework::Array<ItemSlot*>* zSourceSlots,
  90. Framework::Array<ItemSlot*>* zTargetSlots,
  91. ItemFilter* zFilter,
  92. int count,
  93. Direction outDir,
  94. Direction inDir);
  95. ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
  96. virtual void addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir);
  97. InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
  98. void unsaveAddItem(ItemStack* zStack, Direction dir, ItemFilter *zFilter);
  99. int numberOfAddableItems(const ItemStack* zStack, Direction dir) const;
  100. Framework::Iterator<ItemSlot*> begin();
  101. Framework::Iterator<ItemSlot*> end();
  102. void inventoryApi(Framework::StreamReader* zRequest,
  103. NetworkMessage* zResponse,
  104. Entity* zSource);
  105. void registerAfterPullStackCall(std::function<void(
  106. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  107. call);
  108. void registerAfterPushStackCall(std::function<void(
  109. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  110. call);
  111. void registerObserverAddedCall(
  112. std::function<void(Entity* zSource, Framework::Text id)> call);
  113. friend InventoryInteraction;
  114. friend MultipleInventoryLock;
  115. private:
  116. static bool unsafeMove(Inventory* zSource,
  117. Inventory* zTarget,
  118. Framework::Iterator<ItemSlot*>& sourceSlot,
  119. Framework::Iterator<ItemSlot*>& targetSlot,
  120. Direction outDir,
  121. Direction inDir,
  122. int& count);
  123. };