Inventory.h 4.4 KB

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