Inventory.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. int nextSlotId;
  40. void updateCache(ItemSlot* zSlot, int beforeKey);
  41. protected:
  42. Framework::Vec3<float> location;
  43. virtual bool allowPullStack(ItemSlot* zSlot, Direction dir) const;
  44. virtual bool allowPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const;
  45. virtual void afterPullStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  46. virtual void afterPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  47. virtual void loadInventory(Framework::StreamReader* zReader);
  48. virtual void saveInventory(Framework::StreamWriter* zWriter);
  49. void notyObservers(NetworkMessage& msg);
  50. void removeObserver(Entity* zSource, Framework::Text id);
  51. public:
  52. Inventory(const Framework::Vec3<float> location, bool hasInventory);
  53. virtual ~Inventory();
  54. const ItemSlot* zSlot(int id) const;
  55. void addSlot(ItemSlot* slot);
  56. void localTransaction(Framework::Array< ItemSlot* >* zSourceSlots, Framework::Array< ItemSlot* >* zTargetSlots, ItemFilter* zFilter, int count, Direction outDir, Direction inDir);
  57. ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
  58. virtual void addItems(ItemStack* items, Direction dir);
  59. InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
  60. void unsaveAddItem(ItemStack* zStack, Direction dir);
  61. int numberOfAddableItems(const ItemStack* zStack, Direction dir) const;
  62. Framework::Iterator<ItemSlot*> begin();
  63. Framework::Iterator<ItemSlot*> end();
  64. void inventoryApi(Framework::StreamReader* zRequest, NetworkMessage* zResponse, Entity* zSource);
  65. friend InventoryInteraction;
  66. };