Inventory.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. int dimensionId;
  68. Framework::Vec3<float> location;
  69. virtual bool allowPullStack(ItemSlot* zSlot, Direction dir) const;
  70. virtual bool allowPushStack(
  71. ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const;
  72. virtual void afterPullStack(
  73. ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  74. virtual void afterPushStack(
  75. ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  76. virtual void loadInventory(Framework::StreamReader* zReader);
  77. virtual void saveInventory(Framework::StreamWriter* zWriter);
  78. void removeObserver(Entity* zSource, Framework::Text id);
  79. void addObserver(Entity* zSource, Framework::Text id);
  80. virtual void addItems(
  81. ItemStack* zItems, Direction dir, ItemFilter* zFilter);
  82. void lock();
  83. void unlock();
  84. public:
  85. Inventory(const Framework::Vec3<float> location,
  86. int dimensionId,
  87. bool hasInventory);
  88. virtual ~Inventory();
  89. void notifyObservers(NetworkMessage* msg);
  90. const ItemSlot* zSlot(int id) const;
  91. void addSlot(ItemSlot* slot);
  92. void localTransaction(Framework::Array<ItemSlot*>* zSourceSlots,
  93. Framework::Array<ItemSlot*>* zTargetSlots,
  94. ItemFilter* zFilter,
  95. int count,
  96. Direction outDir,
  97. Direction inDir);
  98. ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
  99. virtual void addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir);
  100. InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
  101. void unsaveAddItem(ItemStack* zStack, Direction dir, ItemFilter *zFilter);
  102. int numberOfAddableItems(const ItemStack* zStack, Direction dir) const;
  103. Framework::Iterator<ItemSlot*> begin();
  104. Framework::Iterator<ItemSlot*> end();
  105. void inventoryApi(Framework::StreamReader* zRequest,
  106. NetworkMessage* zResponse,
  107. Entity* zSource);
  108. void registerAfterPullStackCall(std::function<void(
  109. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  110. call);
  111. void registerAfterPushStackCall(std::function<void(
  112. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  113. call);
  114. void registerObserverAddedCall(
  115. std::function<void(Entity* zSource, Framework::Text id)> call);
  116. int getDimensionId() const;
  117. Framework::Vec3<float> getLocation() const;
  118. friend InventoryInteraction;
  119. friend MultipleInventoryLock;
  120. private:
  121. static bool unsafeMove(Inventory* zSource,
  122. Inventory* zTarget,
  123. Framework::Iterator<ItemSlot*>& sourceSlot,
  124. Framework::Iterator<ItemSlot*>& targetSlot,
  125. Direction outDir,
  126. Direction inDir,
  127. int& count);
  128. };