123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #pragma once
- #include <Critical.h>
- #include <HashMap.h>
- #include <ImmutablePair.h>
- #include <ReferenceCounter.h>
- #include <Vec3.h>
- #include "Area.h"
- #include "ItemSlot.h"
- class ItemFilter;
- class Inventory;
- class NetworkMessage;
- class Entity;
- class InventoryInteraction
- {
- private:
- Inventory* current;
- Inventory* other;
- Direction dir;
- void lock();
- void unlock();
- void transaction(Inventory* zSource,
- Inventory* zTarget,
- ItemFilter* zFilter,
- Direction sourceView,
- Direction targetView,
- int count);
- public:
- InventoryInteraction(Inventory* zCurrent, Inventory* zOther, Direction dir);
- InventoryInteraction(const InventoryInteraction& interaction);
- ~InventoryInteraction();
- InventoryInteraction& operator=(const InventoryInteraction& data);
- void endInteraction();
- void pullItems(int count, ItemFilter* zFilter);
- void pushItems(int count, ItemFilter* zFilter);
- };
- class MultipleInventoryLock
- {
- private:
- Inventory** inventories;
- int count;
- bool locked;
- public:
- MultipleInventoryLock(Inventory** inventories, int count);
- ~MultipleInventoryLock();
- void unlock();
- void lock();
- };
- class Inventory : public virtual Framework::ReferenceCounter
- {
- private:
- Framework::Array<Framework::ImmutablePair<int, Framework::Text>> observers;
- Framework::RCArray<ItemSlot>* pullSlotsOrder;
- Framework::RCArray<ItemSlot>* pushSlotsOrder;
- Framework::HashMap<int, Framework::Array<ItemSlot*>*>* itemCache;
- Framework::Critical cs;
- Framework::Array<std::function<void(
- ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>>
- afterPullStackCalls;
- Framework::Array<std::function<void(
- ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>>
- afterPushStackCalls;
- Framework::Array<std::function<void(Entity* zSource, Framework::Text id)>>
- observerAddedCalls;
- int nextSlotId;
- void updateCache(ItemSlot* zSlot, int beforeKey);
- protected:
- Framework::Vec3<float> location;
- virtual bool allowPullStack(ItemSlot* zSlot, Direction dir) const;
- virtual bool allowPushStack(
- ItemSlot* zSlot, Direction dir, const Item* zItem, int& count) const;
- virtual void afterPullStack(
- ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
- virtual void afterPushStack(
- ItemSlot* zSlot, Direction dir, const Item* zItem, int count);
- virtual void loadInventory(Framework::StreamReader* zReader);
- virtual void saveInventory(Framework::StreamWriter* zWriter);
- void removeObserver(Entity* zSource, Framework::Text id);
- void addObserver(Entity* zSource, Framework::Text id);
- virtual void addItems(
- ItemStack* zItems, Direction dir, ItemFilter* zFilter);
- public:
- Inventory(const Framework::Vec3<float> location, bool hasInventory);
- virtual ~Inventory();
- void notifyObservers(NetworkMessage* msg);
- const ItemSlot* zSlot(int id) const;
- void addSlot(ItemSlot* slot);
- void localTransaction(Framework::Array<ItemSlot*>* zSourceSlots,
- Framework::Array<ItemSlot*>* zTargetSlots,
- ItemFilter* zFilter,
- int count,
- Direction outDir,
- Direction inDir);
- ItemStack* takeItemsOut(ItemSlot* zSlot, int count, Direction dir);
- virtual void addItems(ItemSlot* zSlot, ItemStack* zItems, Direction dir);
- InventoryInteraction interactWith(Inventory* zInventory, Direction dir);
- void unsaveAddItem(ItemStack* zStack, Direction dir, ItemFilter *zFilter);
- int numberOfAddableItems(const ItemStack* zStack, Direction dir) const;
- Framework::Iterator<ItemSlot*> begin();
- Framework::Iterator<ItemSlot*> end();
- void inventoryApi(Framework::StreamReader* zRequest,
- NetworkMessage* zResponse,
- Entity* zSource);
- void registerAfterPullStackCall(std::function<void(
- ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
- call);
- void registerAfterPushStackCall(std::function<void(
- ItemSlot* zSlot, Direction dir, const Item* zItem, int count)>
- call);
- void registerObserverAddedCall(
- std::function<void(Entity* zSource, Framework::Text id)> call);
- friend InventoryInteraction;
- friend MultipleInventoryLock;
- private:
- static bool unsafeMove(Inventory* zSource,
- Inventory* zTarget,
- Framework::Iterator<ItemSlot*>& sourceSlot,
- Framework::Iterator<ItemSlot*>& targetSlot,
- Direction outDir,
- Direction inDir,
- int& count);
- };
|