Inventory.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include <Vec3.h>
  3. #include <ReferenceCounter.h>
  4. #include <HashMap.h>
  5. #include <Critical.h>
  6. #include <ImmutablePair.h>
  7. #include "ItemSlot.h"
  8. #include "Area.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, Inventory* zTarget, ItemFilter* zFilter, Direction sourceView, Direction targetView, int count);
  22. public:
  23. InventoryInteraction(Inventory* zCurrent, Inventory* zOther, Direction dir);
  24. InventoryInteraction(const InventoryInteraction& interaction);
  25. ~InventoryInteraction();
  26. InventoryInteraction& operator=(const InventoryInteraction& data);
  27. void endInteraction();
  28. void pullItems(int count, ItemFilter* zFilter);
  29. void pushItems(int count, ItemFilter* zFilter);
  30. };
  31. class Inventory : public virtual Framework::ReferenceCounter
  32. {
  33. private:
  34. Framework::Array<Framework::ImmutablePair<int, Framework::Text>> observers;
  35. Framework::RCArray<ItemSlot>* pullSlotsOrder;
  36. Framework::RCArray<ItemSlot>* pushSlotsOrder;
  37. Framework::HashMap<int, Framework::Array<ItemSlot*>*>* itemCache;
  38. Framework::Critical cs;
  39. Framework::Array<std::function<void(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>> afterPullStackCalls;
  40. Framework::Array<std::function<void(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>> afterPushStackCalls;
  41. Framework::Array<std::function<void(Entity* zSource, Framework::Text id)>> observerAddedCalls;
  42. int nextSlotId;
  43. void updateCache(ItemSlot* zSlot, int beforeKey);
  44. protected:
  45. Framework::Vec3<float> location;
  46. virtual bool allowPullStack(ItemSlot* zSlot, Direction dir) const;
  47. virtual bool allowPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const;
  48. virtual void afterPullStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  49. virtual void afterPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  50. virtual void loadInventory(Framework::StreamReader* zReader);
  51. virtual void saveInventory(Framework::StreamWriter* zWriter);
  52. void removeObserver(Entity* zSource, Framework::Text id);
  53. void addObserver(Entity* zSource, Framework::Text id);
  54. virtual void addItems(ItemStack* zItems, Direction dir);
  55. public:
  56. Inventory(const Framework::Vec3<float> location, bool hasInventory);
  57. virtual ~Inventory();
  58. void notyObservers(NetworkMessage* msg);
  59. const ItemSlot* zSlot(int id) const;
  60. void addSlot(ItemSlot* slot);
  61. void localTransaction(Framework::Array< ItemSlot* >* zSourceSlots, Framework::Array<ItemSlot*>* zTargetSlots, ItemFilter* zFilter, int count, Direction outDir, Direction inDir);
  62. ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
  63. virtual void addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir);
  64. InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
  65. void unsaveAddItem(ItemStack* zStack, Direction dir);
  66. int numberOfAddableItems(const ItemStack* zStack, Direction dir) const;
  67. Framework::Iterator<ItemSlot*> begin();
  68. Framework::Iterator<ItemSlot*> end();
  69. void inventoryApi(Framework::StreamReader* zRequest, NetworkMessage* zResponse, Entity* zSource);
  70. void registerAfterPullStackCall(std::function<void(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call);
  71. void registerAfterPushStackCall(std::function<void(ItemSlot* zSlot, Direction dir, const Item* zItem, int count)> call);
  72. void registerObserverAddedCall(std::function<void(Entity* zSource, Framework::Text id)> call);
  73. friend InventoryInteraction;
  74. private:
  75. static bool unsafeMove(Inventory* zSource, Inventory* zTarget, Framework::Iterator<ItemSlot*>& sourceSlot, Framework::Iterator<ItemSlot*>& targetSlot, Direction outDir, Direction inDir, int& count);
  76. };