Inventory.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <Writer.h>
  8. #include "Area.h"
  9. #include "ItemSlot.h"
  10. #include "Recipie.h"
  11. class ItemFilter;
  12. class Inventory;
  13. class NetworkMessage;
  14. class Entity;
  15. class Item;
  16. class ItemStack;
  17. class InventoryInteraction
  18. {
  19. private:
  20. Inventory* current;
  21. Inventory* other;
  22. Direction dir;
  23. void lock();
  24. void unlock();
  25. void transaction(Inventory* zSource,
  26. Inventory* zTarget,
  27. ItemFilter* zFilter,
  28. Direction sourceView,
  29. Direction targetView,
  30. int count);
  31. public:
  32. InventoryInteraction(Inventory* zCurrent, Inventory* zOther, Direction dir);
  33. InventoryInteraction(const InventoryInteraction& interaction);
  34. ~InventoryInteraction();
  35. InventoryInteraction& operator=(const InventoryInteraction& data);
  36. void endInteraction();
  37. void pullItems(int count, ItemFilter* zFilter);
  38. void pushItems(int count, ItemFilter* zFilter);
  39. };
  40. class MultipleInventoryLock
  41. {
  42. private:
  43. Inventory** inventories;
  44. int count;
  45. bool locked;
  46. public:
  47. MultipleInventoryLock(Inventory** inventories, int count);
  48. ~MultipleInventoryLock();
  49. void unlock();
  50. void lock();
  51. };
  52. class Inventory : public virtual Framework::ReferenceCounter
  53. {
  54. private:
  55. Framework::Array<Framework::ImmutablePair<int, Framework::Text>> observers;
  56. Framework::RCArray<ItemSlot>* pullSlotsOrder;
  57. Framework::RCArray<ItemSlot>* pushSlotsOrder;
  58. Framework::HashMap<int, Framework::Array<ItemSlot*>*>* itemCache;
  59. Framework::Critical cs;
  60. Framework::Array<std::function<void(
  61. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>>
  62. afterPullStackCalls;
  63. Framework::Array<std::function<void(
  64. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>>
  65. afterPushStackCalls;
  66. Framework::Array<std::function<void(Entity* zSource, Framework::Text id)>>
  67. observerAddedCalls;
  68. int nextSlotId;
  69. public:
  70. Inventory(const Framework::Vec3<float> location,
  71. int dimensionId,
  72. bool hasInventory);
  73. virtual ~Inventory();
  74. private:
  75. void updateCache(ItemSlot* zSlot, int beforeKey);
  76. protected:
  77. int dimensionId;
  78. Framework::Vec3<float> location;
  79. virtual bool allowPullStack(ItemSlot* zSlot, Direction dir) const;
  80. virtual bool allowPushStack(
  81. ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const;
  82. virtual void afterPullStack(
  83. ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  84. virtual void afterPushStack(
  85. ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  86. virtual void updateSlot(ItemSlot* zSlot);
  87. virtual void loadInventory(Framework::StreamReader* zReader);
  88. virtual void saveInventory(Framework::StreamWriter* zWriter);
  89. void removeObserver(Entity* zSource, Framework::Text id);
  90. void addObserver(Entity* zSource, Framework::Text id);
  91. virtual void addItems(
  92. ItemStack* zItems, Direction dir, ItemFilter* zFilter);
  93. public:
  94. void lock();
  95. void unlock();
  96. void notifyObservers(NetworkMessage* msg);
  97. const ItemSlot* zSlot(int id) const;
  98. void addSlot(ItemSlot* slot);
  99. void localTransaction(Framework::Array<ItemSlot*>* zSourceSlots,
  100. Framework::Array<ItemSlot*>* zTargetSlots,
  101. ItemFilter* zFilter,
  102. int count,
  103. Direction outDir,
  104. Direction inDir);
  105. ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
  106. virtual void addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir);
  107. InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
  108. void unsaveAddItem(ItemStack* zStack, Direction dir, ItemFilter* zFilter);
  109. int numberOfAddableItems(const Item* zItem, Direction dir) const;
  110. int numberOfAddableItems(const Item* zItem,
  111. Direction dir,
  112. const Framework::Text& slotName) const;
  113. bool isAllAvailable(Framework::RCArray<RecipieInput>& inputs,
  114. const Framework::Text& slotName) const;
  115. void consume(Framework::RCArray<RecipieInput>& inputs,
  116. const Framework::Text& slotName) const;
  117. Framework::ArrayIterator<ItemSlot*> begin();
  118. Framework::ArrayIterator<ItemSlot*> end();
  119. void inventoryApi(Framework::StreamReader* zRequest,
  120. NetworkMessage* zResponse,
  121. Entity* zSource);
  122. void registerAfterPullStackCall(std::function<void(
  123. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  124. call);
  125. void registerAfterPushStackCall(std::function<void(
  126. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  127. call);
  128. void registerObserverAddedCall(
  129. std::function<void(Entity* zSource, Framework::Text id)> call);
  130. int getDimensionId() const;
  131. Framework::Vec3<float> getLocation() const;
  132. friend InventoryInteraction;
  133. friend MultipleInventoryLock;
  134. private:
  135. static bool unsafeMove(Inventory* zSource,
  136. Inventory* zTarget,
  137. Framework::ArrayIterator<ItemSlot*>& sourceSlot,
  138. Framework::ArrayIterator<ItemSlot*>& targetSlot,
  139. Direction outDir,
  140. Direction inDir,
  141. int& count);
  142. };