Inventory.h 5.0 KB

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