Inventory.h 4.7 KB

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