Some inventory const-ification

This commit is contained in:
Giuseppe Bilotta 2011-08-10 11:38:49 +02:00
parent c007d8219e
commit 467b3cf4c1
3 changed files with 52 additions and 35 deletions

@ -178,9 +178,9 @@ std::string FurnaceNodeMetadata::infoText()
//return "Furnace"; //return "Furnace";
if(m_fuel_time >= m_fuel_totaltime) if(m_fuel_time >= m_fuel_totaltime)
{ {
InventoryList *src_list = m_inventory->getList("src"); const InventoryList *src_list = m_inventory->getList("src");
assert(src_list); assert(src_list);
InventoryItem *src_item = src_list->getItem(0); const InventoryItem *src_item = src_list->getItem(0);
if(src_item) if(src_item)
return "Furnace is out of fuel"; return "Furnace is out of fuel";
@ -219,7 +219,7 @@ bool FurnaceNodeMetadata::step(float dtime)
InventoryList *src_list = m_inventory->getList("src"); InventoryList *src_list = m_inventory->getList("src");
assert(src_list); assert(src_list);
InventoryItem *src_item = src_list->getItem(0); const InventoryItem *src_item = src_list->getItem(0);
// Start only if there are free slots in dst, so that it can // Start only if there are free slots in dst, so that it can
// accomodate any result item // accomodate any result item
@ -268,7 +268,7 @@ bool FurnaceNodeMetadata::step(float dtime)
InventoryList *fuel_list = m_inventory->getList("fuel"); InventoryList *fuel_list = m_inventory->getList("fuel");
assert(fuel_list); assert(fuel_list);
InventoryItem *fuel_item = fuel_list->getItem(0); const InventoryItem *fuel_item = fuel_list->getItem(0);
if(ItemSpec(ITEM_MATERIAL, CONTENT_TREE).checkItem(fuel_item)) if(ItemSpec(ITEM_MATERIAL, CONTENT_TREE).checkItem(fuel_item))
{ {

@ -139,12 +139,12 @@ ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f
MaterialItem MaterialItem
*/ */
bool MaterialItem::isCookable() bool MaterialItem::isCookable() const
{ {
return item_material_is_cookable(m_content); return item_material_is_cookable(m_content);
} }
InventoryItem *MaterialItem::createCookResult() InventoryItem *MaterialItem::createCookResult() const
{ {
return item_material_create_cook_result(m_content); return item_material_create_cook_result(m_content);
} }
@ -176,7 +176,7 @@ ServerActiveObject* CraftItem::createSAO(ServerEnvironment *env, u16 id, v3f pos
return InventoryItem::createSAO(env, id, pos); return InventoryItem::createSAO(env, id, pos);
} }
u16 CraftItem::getDropCount() u16 CraftItem::getDropCount() const
{ {
// Special cases // Special cases
s16 dc = item_craft_get_drop_count(m_subname); s16 dc = item_craft_get_drop_count(m_subname);
@ -186,12 +186,12 @@ u16 CraftItem::getDropCount()
return InventoryItem::getDropCount(); return InventoryItem::getDropCount();
} }
bool CraftItem::isCookable() bool CraftItem::isCookable() const
{ {
return item_craft_is_cookable(m_subname); return item_craft_is_cookable(m_subname);
} }
InventoryItem *CraftItem::createCookResult() InventoryItem *CraftItem::createCookResult() const
{ {
return item_craft_create_cook_result(m_subname); return item_craft_create_cook_result(m_subname);
} }
@ -416,7 +416,7 @@ InventoryList & InventoryList::operator = (const InventoryList &other)
return *this; return *this;
} }
std::string InventoryList::getName() const std::string &InventoryList::getName() const
{ {
return m_name; return m_name;
} }
@ -443,6 +443,13 @@ u32 InventoryList::getFreeSlots()
return getSize() - getUsedSlots(); return getSize() - getUsedSlots();
} }
const InventoryItem * InventoryList::getItem(u32 i) const
{
if(i > m_items.size() - 1)
return NULL;
return m_items[i];
}
InventoryItem * InventoryList::getItem(u32 i) InventoryItem * InventoryList::getItem(u32 i)
{ {
if(i > m_items.size() - 1) if(i > m_items.size() - 1)
@ -545,7 +552,7 @@ InventoryItem * InventoryList::addItem(u32 i, InventoryItem *newitem)
bool InventoryList::itemFits(u32 i, InventoryItem *newitem) bool InventoryList::itemFits(u32 i, InventoryItem *newitem)
{ {
// If it is an empty position, it's an easy job. // If it is an empty position, it's an easy job.
InventoryItem *to_item = getItem(i); const InventoryItem *to_item = getItem(i);
if(to_item == NULL) if(to_item == NULL)
{ {
return true; return true;
@ -736,7 +743,15 @@ InventoryList * Inventory::getList(const std::string &name)
return m_lists[i]; return m_lists[i];
} }
s32 Inventory::getListIndex(const std::string &name) const InventoryList * Inventory::getList(const std::string &name) const
{
s32 i = getListIndex(name);
if(i == -1)
return NULL;
return m_lists[i];
}
const s32 Inventory::getListIndex(const std::string &name) const
{ {
for(u32 i=0; i<m_lists.size(); i++) for(u32 i=0; i<m_lists.size(); i++)
{ {
@ -866,7 +881,7 @@ void IMoveAction::apply(InventoryContext *c, InventoryManager *mgr)
Craft checking system Craft checking system
*/ */
bool ItemSpec::checkItem(InventoryItem *item) bool ItemSpec::checkItem(const InventoryItem *item) const
{ {
if(type == ITEM_NONE) if(type == ITEM_NONE)
{ {
@ -916,7 +931,7 @@ bool ItemSpec::checkItem(InventoryItem *item)
return true; return true;
} }
bool checkItemCombination(InventoryItem **items, ItemSpec *specs) bool checkItemCombination(InventoryItem const * const *items, const ItemSpec *specs)
{ {
u16 items_min_x = 100; u16 items_min_x = 100;
u16 items_max_x = 100; u16 items_max_x = 100;
@ -979,8 +994,8 @@ bool checkItemCombination(InventoryItem **items, ItemSpec *specs)
u16 items_y = items_min_y + y; u16 items_y = items_min_y + y;
u16 specs_x = specs_min_x + x; u16 specs_x = specs_min_x + x;
u16 specs_y = specs_min_y + y; u16 specs_y = specs_min_y + y;
InventoryItem *item = items[items_y * 3 + items_x]; const InventoryItem *item = items[items_y * 3 + items_x];
ItemSpec &spec = specs[specs_y * 3 + specs_x]; const ItemSpec &spec = specs[specs_y * 3 + specs_x];
if(spec.checkItem(item) == false) if(spec.checkItem(item) == false)
return false; return false;

@ -61,19 +61,19 @@ public:
// Creates an object from the item, to be placed in the world. // Creates an object from the item, to be placed in the world.
virtual ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos); virtual ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
// Gets amount of items that dropping one SAO will decrement // Gets amount of items that dropping one SAO will decrement
virtual u16 getDropCount(){ return getCount(); } virtual u16 getDropCount() const { return getCount(); }
/* /*
Quantity methods Quantity methods
*/ */
// Shall return true if the item can be add()ed to the other // Shall return true if the item can be add()ed to the other
virtual bool addableTo(InventoryItem *other) virtual bool addableTo(const InventoryItem *other) const
{ {
return false; return false;
} }
u16 getCount() u16 getCount() const
{ {
return m_count; return m_count;
} }
@ -82,7 +82,7 @@ public:
m_count = count; m_count = count;
} }
// This should return something else for stackable items // This should return something else for stackable items
virtual u16 freeSpace() virtual u16 freeSpace() const
{ {
return 0; return 0;
} }
@ -102,11 +102,11 @@ public:
*/ */
// Whether it can be cooked // Whether it can be cooked
virtual bool isCookable(){return false;} virtual bool isCookable() const {return false;}
// Time of cooking // Time of cooking
virtual float getCookTime(){return 3.0;} virtual float getCookTime(){return 3.0;}
// Result of cooking (can randomize) // Result of cooking (can randomize)
virtual InventoryItem *createCookResult(){return NULL;} virtual InventoryItem *createCookResult() const {return NULL;}
// Eat, press, activate, whatever. // Eat, press, activate, whatever.
// Called when item is right-clicked when lying on ground. // Called when item is right-clicked when lying on ground.
@ -160,7 +160,7 @@ public:
return os.str(); return os.str();
} }
virtual bool addableTo(InventoryItem *other) virtual bool addableTo(const InventoryItem *other) const
{ {
if(std::string(other->getName()) != "MaterialItem") if(std::string(other->getName()) != "MaterialItem")
return false; return false;
@ -169,7 +169,7 @@ public:
return false; return false;
return true; return true;
} }
u16 freeSpace() u16 freeSpace() const
{ {
if(m_count > QUANTITY_ITEM_MAX_COUNT) if(m_count > QUANTITY_ITEM_MAX_COUNT)
return 0; return 0;
@ -178,8 +178,8 @@ public:
/* /*
Other properties Other properties
*/ */
bool isCookable(); bool isCookable() const;
InventoryItem *createCookResult(); InventoryItem *createCookResult() const;
/* /*
Special methods Special methods
*/ */
@ -289,9 +289,9 @@ public:
} }
ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos); ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
u16 getDropCount(); u16 getDropCount() const;
virtual bool addableTo(InventoryItem *other) virtual bool addableTo(const InventoryItem *other) const
{ {
if(std::string(other->getName()) != "CraftItem") if(std::string(other->getName()) != "CraftItem")
return false; return false;
@ -300,7 +300,7 @@ public:
return false; return false;
return true; return true;
} }
u16 freeSpace() u16 freeSpace() const
{ {
if(m_count > QUANTITY_ITEM_MAX_COUNT) if(m_count > QUANTITY_ITEM_MAX_COUNT)
return 0; return 0;
@ -311,8 +311,8 @@ public:
Other properties Other properties
*/ */
bool isCookable(); bool isCookable() const;
InventoryItem *createCookResult(); InventoryItem *createCookResult() const;
bool use(ServerEnvironment *env, Player *player); bool use(ServerEnvironment *env, Player *player);
@ -467,7 +467,7 @@ public:
InventoryList(const InventoryList &other); InventoryList(const InventoryList &other);
InventoryList & operator = (const InventoryList &other); InventoryList & operator = (const InventoryList &other);
std::string getName(); const std::string &getName() const;
u32 getSize(); u32 getSize();
// Count used slots // Count used slots
u32 getUsedSlots(); u32 getUsedSlots();
@ -477,6 +477,7 @@ public:
void setDirty(bool dirty=true){ m_dirty = dirty; }*/ void setDirty(bool dirty=true){ m_dirty = dirty; }*/
// Get pointer to item // Get pointer to item
const InventoryItem * getItem(u32 i) const;
InventoryItem * getItem(u32 i); InventoryItem * getItem(u32 i);
// Returns old item (or NULL). Parameter can be NULL. // Returns old item (or NULL). Parameter can be NULL.
InventoryItem * changeItem(u32 i, InventoryItem *newitem); InventoryItem * changeItem(u32 i, InventoryItem *newitem);
@ -529,6 +530,7 @@ public:
InventoryList * addList(const std::string &name, u32 size); InventoryList * addList(const std::string &name, u32 size);
InventoryList * getList(const std::string &name); InventoryList * getList(const std::string &name);
const InventoryList * getList(const std::string &name) const;
bool deleteList(const std::string &name); bool deleteList(const std::string &name);
// A shorthand for adding items. // A shorthand for adding items.
// Returns NULL if the item was fully added, leftover otherwise. // Returns NULL if the item was fully added, leftover otherwise.
@ -542,7 +544,7 @@ public:
private: private:
// -1 if not found // -1 if not found
s32 getListIndex(const std::string &name); const s32 getListIndex(const std::string &name) const;
core::array<InventoryList*> m_lists; core::array<InventoryList*> m_lists;
}; };
@ -689,14 +691,14 @@ struct ItemSpec
{ {
} }
bool checkItem(InventoryItem *item); bool checkItem(const InventoryItem *item) const;
}; };
/* /*
items: a pointer to an array of 9 pointers to items items: a pointer to an array of 9 pointers to items
specs: a pointer to an array of 9 ItemSpecs specs: a pointer to an array of 9 ItemSpecs
*/ */
bool checkItemCombination(InventoryItem **items, ItemSpec *specs); bool checkItemCombination(const InventoryItem * const*items, const ItemSpec *specs);
#endif #endif