Inventory.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 loadInventory(Framework::StreamReader* zReader);
  80. virtual void saveInventory(Framework::StreamWriter* zWriter);
  81. void removeObserver(Entity* zSource, Framework::Text id);
  82. void addObserver(Entity* zSource, Framework::Text id);
  83. virtual void addItems(
  84. ItemStack* zItems, Direction dir, ItemFilter* zFilter);
  85. void lock();
  86. void unlock();
  87. public:
  88. Inventory(const Framework::Vec3<float> location,
  89. int dimensionId,
  90. bool hasInventory);
  91. virtual ~Inventory();
  92. void notifyObservers(NetworkMessage* msg);
  93. const ItemSlot* zSlot(int id) const;
  94. void addSlot(ItemSlot* slot);
  95. void localTransaction(Framework::Array<ItemSlot*>* zSourceSlots,
  96. Framework::Array<ItemSlot*>* zTargetSlots,
  97. ItemFilter* zFilter,
  98. int count,
  99. Direction outDir,
  100. Direction inDir);
  101. ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
  102. virtual void addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir);
  103. InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
  104. void unsaveAddItem(ItemStack* zStack, Direction dir, ItemFilter *zFilter);
  105. int numberOfAddableItems(const ItemStack* zStack, Direction dir) const;
  106. Framework::Iterator<ItemSlot*> begin();
  107. Framework::Iterator<ItemSlot*> end();
  108. void inventoryApi(Framework::StreamReader* zRequest,
  109. NetworkMessage* zResponse,
  110. Entity* zSource);
  111. void registerAfterPullStackCall(std::function<void(
  112. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  113. call);
  114. void registerAfterPushStackCall(std::function<void(
  115. ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
  116. call);
  117. void registerObserverAddedCall(
  118. std::function<void(Entity* zSource, Framework::Text id)> call);
  119. int getDimensionId() const;
  120. Framework::Vec3<float> getLocation() const;
  121. friend InventoryInteraction;
  122. friend MultipleInventoryLock;
  123. private:
  124. static bool unsafeMove(Inventory* zSource,
  125. Inventory* zTarget,
  126. Framework::Iterator<ItemSlot*>& sourceSlot,
  127. Framework::Iterator<ItemSlot*>& targetSlot,
  128. Direction outDir,
  129. Direction inDir,
  130. int& count);
  131. };