Inventory.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #pragma once
  2. #include <Critical.h>
  3. #include <HashMap.h>
  4. #include <ReferenceCounter.h>
  5. #include <Vec3.h>
  6. #include <Writer.h>
  7. #include "Area.h"
  8. #include "ItemSlot.h"
  9. #include "Recipie.h"
  10. #include "UIObservable.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. struct InventoryObserver
  53. {
  54. int entityId;
  55. Framework::Text uimlId;
  56. int processorId;
  57. };
  58. class Inventory : public virtual Framework::ReferenceCounter
  59. {
  60. private:
  61. UIObservable observable;
  62. Framework::RCArray<ItemSlot>* pullSlotsOrder;
  63. Framework::RCArray<ItemSlot>* pushSlotsOrder;
  64. Framework::HashMap<int, Framework::Array<ItemSlot*>*>* itemCache;
  65. Framework::Critical cs;
  66. Framework::Array<std::function<void(
  67. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>>
  68. afterPullStackCalls;
  69. Framework::Array<std::function<void(
  70. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>>
  71. afterPushStackCalls;
  72. int nextSlotId;
  73. public:
  74. Inventory(const Framework::Vec3<float> location,
  75. int dimensionId,
  76. bool hasInventory);
  77. virtual ~Inventory();
  78. private:
  79. void updateCache(ItemSlot* zSlot, int beforeKey);
  80. protected:
  81. int dimensionId;
  82. Framework::Vec3<float> location;
  83. virtual bool allowPullStack(ItemSlot* zSlot, Direction dir) const;
  84. virtual bool allowPushStack(
  85. ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const;
  86. virtual void afterPullStack(
  87. ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  88. virtual void afterPushStack(
  89. ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  90. virtual void updateSlot(ItemSlot* zSlot);
  91. virtual void loadInventory(Framework::StreamReader* zReader);
  92. virtual void saveInventory(Framework::StreamWriter* zWriter);
  93. virtual void addItems(
  94. ItemStack* zItems, Direction dir, ItemFilter* zFilter);
  95. public:
  96. void lock();
  97. void unlock();
  98. void notifyObservers(NetworkMessage* msg);
  99. const ItemSlot* zSlot(int id) const;
  100. void addSlot(ItemSlot* slot);
  101. void localTransaction(Framework::Array<ItemSlot*>* zSourceSlots,
  102. Framework::Array<ItemSlot*>* zTargetSlots,
  103. ItemFilter* zFilter,
  104. int count,
  105. Direction outDir,
  106. Direction inDir);
  107. ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
  108. virtual void addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir);
  109. InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
  110. void unsaveAddItem(ItemStack* zStack, Direction dir, ItemFilter* zFilter);
  111. int numberOfAddableItems(const Item* zItem, Direction dir) const;
  112. int numberOfAddableItems(const Item* zItem,
  113. Direction dir,
  114. const Framework::Text& slotName) const;
  115. bool isAllAvailable(Framework::RCArray<RecipieInput>& inputs,
  116. const Framework::Text& slotName) const;
  117. void consume(Framework::RCArray<RecipieInput>& inputs,
  118. const Framework::Text& slotName);
  119. Framework::ArrayIterator<ItemSlot*> begin();
  120. Framework::ArrayIterator<ItemSlot*> end();
  121. void inventoryApi(Framework::StreamReader* zRequest,
  122. NetworkMessage* zResponse,
  123. Entity* zSource);
  124. void registerAfterPullStackCall(std::function<void(
  125. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  126. call);
  127. void registerAfterPushStackCall(std::function<void(
  128. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  129. call);
  130. void registerObserverAddedCall(
  131. std::function<void(Entity* zSource, Framework::Text id, int processor)>
  132. call);
  133. int getDimensionId() const;
  134. Framework::Vec3<float> getLocation() const;
  135. friend InventoryInteraction;
  136. friend MultipleInventoryLock;
  137. private:
  138. static bool unsafeMove(Inventory* zSource,
  139. Inventory* zTarget,
  140. Framework::ArrayIterator<ItemSlot*>& sourceSlot,
  141. Framework::ArrayIterator<ItemSlot*>& targetSlot,
  142. Direction outDir,
  143. Direction inDir,
  144. int& count);
  145. };