Inventory.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. public:
  82. Inventory(const Framework::Vec3<float> location, bool hasInventory);
  83. virtual ~Inventory();
  84. void notifyObservers(NetworkMessage* msg);
  85. const ItemSlot* zSlot(int id) const;
  86. void addSlot(ItemSlot* slot);
  87. void localTransaction(Framework::Array<ItemSlot*>* zSourceSlots,
  88. Framework::Array<ItemSlot*>* zTargetSlots,
  89. ItemFilter* zFilter,
  90. int count,
  91. Direction outDir,
  92. Direction inDir);
  93. ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
  94. virtual void addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir);
  95. InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
  96. void unsaveAddItem(ItemStack* zStack, Direction dir, ItemFilter *zFilter);
  97. int numberOfAddableItems(const ItemStack* zStack, Direction dir) const;
  98. Framework::Iterator<ItemSlot*> begin();
  99. Framework::Iterator<ItemSlot*> end();
  100. void inventoryApi(Framework::StreamReader* zRequest,
  101. NetworkMessage* zResponse,
  102. Entity* zSource);
  103. void registerAfterPullStackCall(std::function<void(
  104. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  105. call);
  106. void registerAfterPushStackCall(std::function<void(
  107. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  108. call);
  109. void registerObserverAddedCall(
  110. std::function<void(Entity* zSource, Framework::Text id)> call);
  111. friend InventoryInteraction;
  112. friend MultipleInventoryLock;
  113. private:
  114. static bool unsafeMove(Inventory* zSource,
  115. Inventory* zTarget,
  116. Framework::Iterator<ItemSlot*>& sourceSlot,
  117. Framework::Iterator<ItemSlot*>& targetSlot,
  118. Direction outDir,
  119. Direction inDir,
  120. int& count);
  121. };