Inventory.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. #include <Vec3.h>
  3. #include <ReferenceCounter.h>
  4. #include <HashMap.h>
  5. #include <Critical.h>
  6. #include "ItemSlot.h"
  7. #include "Area.h"
  8. class ItemFilter;
  9. class Inventory;
  10. class InventoryInteraction
  11. {
  12. private:
  13. Inventory* current;
  14. Inventory* other;
  15. Direction dir;
  16. void lock();
  17. void unlock();
  18. void transaction(Inventory* zSource, Inventory* zTarget, ItemFilter* zFilter, Direction sourceView, Direction targetView, int count);
  19. public:
  20. InventoryInteraction(Inventory* zCurrent, Inventory* zOther, Direction dir);
  21. InventoryInteraction(const InventoryInteraction& interaction);
  22. ~InventoryInteraction();
  23. InventoryInteraction& operator=(const InventoryInteraction& data);
  24. void endInteraction();
  25. void pullItems(int count, ItemFilter* zFilter);
  26. void pushItems(int count, ItemFilter* zFilter);
  27. };
  28. class Inventory : public virtual Framework::ReferenceCounter
  29. {
  30. private:
  31. Framework::RCArray<ItemSlot>* pullSlotsOrder;
  32. Framework::RCArray<ItemSlot>* pushSlotsOrder;
  33. Framework::HashMap<int, Framework::Array<ItemSlot*>*>* itemCache;
  34. Framework::Critical cs;
  35. int nextSlotId;
  36. void updateCache(ItemSlot* zSlot, int beforeKey);
  37. protected:
  38. Framework::Vec3<float> location;
  39. virtual bool allowPullStack(ItemSlot* zSlot, Direction dir) const;
  40. virtual bool allowPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const;
  41. virtual void afterPullStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  42. virtual void afterPushStack(ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
  43. virtual void loadInventory(Framework::StreamReader* zReader);
  44. virtual void saveInventory(Framework::StreamWriter* zWriter);
  45. public:
  46. Inventory(const Framework::Vec3<float> location, bool hasInventory);
  47. virtual ~Inventory();
  48. void addSlot(ItemSlot* slot);
  49. void localTransaction(Framework::Array< ItemSlot* >* zSourceSlots, Framework::Array< ItemSlot* >* zTargetSlots, ItemFilter* zFilter, int count, Direction outDir, Direction inDir);
  50. ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
  51. virtual void addItems(ItemStack* items, Direction dir);
  52. InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
  53. void unsaveAddItem(ItemStack* zStack, Direction dir);
  54. int numberOfAddableItems(const ItemStack* zStack, Direction dir) const;
  55. Framework::Iterator<ItemSlot*> begin();
  56. Framework::Iterator<ItemSlot*> end();
  57. friend InventoryInteraction;
  58. };