diff options
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | src/gamehandler.cpp | 6 | ||||
-rw-r--r-- | src/inventory.cpp | 137 | ||||
-rw-r--r-- | src/inventory.h | 107 | ||||
-rw-r--r-- | src/itemmanager.cpp | 29 | ||||
-rw-r--r-- | src/itemmanager.h | 8 | ||||
-rw-r--r-- | src/player.cpp | 42 | ||||
-rw-r--r-- | src/player.h | 18 |
8 files changed, 236 insertions, 118 deletions
@@ -1,8 +1,13 @@ -2006-10-19 Yohann Ferreira <bertram@cegetel.net> +2006-10-20 Yohann Ferreira <bertram@cegetel.net> * data/items.xsd, data/items.xml, src/item.h, src/itemmanager.cpp, src/main.cpp: Simplified item reference a bit and filled the reference items.xml file. + * data/items.xml, data/items.xsd, src/item.h, src/itemmanager.cpp: + Added the missing Weapon Type and Max Per Slot item properties. + * src/inventory.h, src/inventory.cpp, src/itemmanager.h, + src/itemmanager.cpp, src/player.h, src/player.cpp, + src/gamehandler.cpp: Adding inventory handling (basics) Part 1. 2006-10-19 Yohann Ferreira <bertram@cegetel.net> diff --git a/src/gamehandler.cpp b/src/gamehandler.cpp index 42cb0c70..968325a7 100644 --- a/src/gamehandler.cpp +++ b/src/gamehandler.cpp @@ -178,7 +178,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) // remove the item from world map // send feedback - computer.getCharacter()->addInventory(itemId); + computer.getCharacter()->addItem(itemId); result.writeShort(GPMSG_PICKUP_RESPONSE); result.writeByte(ERRMSG_OK); } break; @@ -211,11 +211,11 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) case PGMSG_EQUIP: { - int itemId = message.readLong(); + int itemId = message.readLong(); // Not useful, the inventory knows it char slot = message.readByte(); result.writeShort(GPMSG_EQUIP_RESPONSE); - result.writeByte(computer.getCharacter()->equip(itemId, slot) ? + result.writeByte(computer.getCharacter()->equip(slot) ? ERRMSG_OK : ERRMSG_FAILURE); } break; diff --git a/src/inventory.cpp b/src/inventory.cpp index 187c641f..442b5952 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -23,75 +23,154 @@ #include "inventory.h" -unsigned short -getSlotIndex(const unsigned int itemId) +Inventory::Inventory() { - return 0; + itemList.reserve(MAX_ITEMS_IN_INVENTORY); + itemList.reserve(TOTAL_EQUIPMENT_SLOTS); } -bool -addItem(unsigned int itemId, unsigned short amount) +unsigned char +Inventory::getInventoryFreeSlot() { - return false; + for (int a = 0; a < MAX_ITEMS_IN_INVENTORY; a++) + { + if (itemList.at(a).amount == 0) + return a; + } + return INVENTORY_FULL; } -bool -removeItem(unsigned int itemId, unsigned short amount = 0) +unsigned char +Inventory::getSlotFromId(const unsigned int itemId) { - return false; + for (int a = 0; a < MAX_ITEMS_IN_INVENTORY; a++) + { + if (itemList.at(a).itemId == itemId) + return a; + } + return INVENTORY_FULL; } bool -removeItem(unsigned short index, unsigned short amount = 0) +Inventory::hasItem(unsigned int itemId) { + for (int a = 0; a < MAX_ITEMS_IN_INVENTORY; a++) + { + if (itemList.at(a).itemId == itemId) + return true; + } return false; } -bool -equipItem(unsigned int itemId) +short +Inventory::addItem(unsigned int itemId, unsigned char amount) { - return false; + // We get the max number of item we can have in the same slot + // for the given item. + unsigned char maxPerSlot = itemManager->getMaxPerSlot(itemId); + // We'll add items in slots with the item type and in free slots + // until it's all done or until the inventory will be all parsed. + // Searching for items with the same Id in the inventory, before + // seeking a free slot. + unsigned char amountToAdd = amount; + + // Parsing inventory + unsigned char currentAmountInSlot = 0; + for (int a = 0; a < MAX_ITEMS_IN_INVENTORY; a++) + { + currentAmountInSlot = itemList.at(a).amount; + // If a slot a got place for some more of such an item. + if (itemList.at(a).itemId == itemId && currentAmountInSlot < maxPerSlot) + { + // If there isn't enough space to put every item in the slot. + // We add the difference. + if ((maxPerSlot - currentAmountInSlot) < amountToAdd) + { + itemList.at(a).amount += (maxPerSlot - currentAmountInSlot); + amountToAdd -= (maxPerSlot - currentAmountInSlot); + } + else // there is enough to add everything. + { + itemList.at(a).amount += amountToAdd; + amountToAdd = 0; // Ok! + } + } + // Or if there is an empty slot. + else if (itemList.at(a).amount == 0) + { + // We add the item in the new slot (setting also the Id.) + if (maxPerSlot < amountToAdd) + { + itemList.at(a).amount = maxPerSlot; + amountToAdd -= maxPerSlot; + } + else + { + itemList.at(a).amount += amountToAdd; + amountToAdd = 0; // Ok! + } + itemList.at(a).itemId = itemId; + } + } + + return (short)amount - amountToAdd; } -bool -unequipItem(unsigned int itemId) +short +Inventory::removeItem(unsigned int itemId, unsigned char amount) { - return false; + return false; // TODO } -bool -equipItem(unsigned short index) +short +Inventory::removeItem(unsigned char slot, unsigned char amount) { - return false; + if (itemList.at(slot).amount < amount) + { + unsigned char value = itemList.at(slot).amount; + itemList.at(slot).amount = 0; + return (short)value; + } + else + { + itemList.at(slot).amount -= amount; + return amount; + } } bool -unequipItem(unsigned short index) +Inventory::equipItem(unsigned int itemId) { - return false; + return false; // TODO } +bool +Inventory::unequipItem(unsigned int itemId) +{ + return false; // TODO +} bool -use(unsigned short index, BeingPtr itemUser) +Inventory::equipItem(unsigned char slot) { - return false; + return false; // TODO } bool -use(unsigned int itemId, BeingPtr itemUser) +Inventory::unequipItem(unsigned char slot) { - return false; + return false; // TODO } + bool -useWithScript(unsigned short index, const std::string scriptFile) +Inventory::use(unsigned char slot, BeingPtr itemUser) { - return false; + return false; // TODO } bool -useWithScript(unsigned int itemId, const std::string scriptFile) +Inventory::use(unsigned int itemId, BeingPtr itemUser) { - return false; + return false; // TODO } diff --git a/src/inventory.h b/src/inventory.h index 79d54b26..198c181b 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -24,91 +24,124 @@ #ifndef INVENTORY_H #define INVENTORY_H -#include "item.h" +#include "itemmanager.h" + +// items in inventory : +const unsigned char MAX_ITEMS_IN_INVENTORY = 50, // Max 254. +// Equipment rules: +// 1 Brest equipment +// 1 arms equipment +// 1 head equipment +// 1 legs equipment +// 1 feet equipment +// 2 rings +// 1 necklace +// Fight: +// 2 one-handed weapons +// or 1 two-handed weapon +// or 1 one-handed weapon + 1 shield. +// = 10 total slots for equipment. + TOTAL_EQUIPMENT_SLOTS = 10, + INVENTORY_FULL = 255; /** * Stored Item only contains id reference to items * in the order not to carry every item info for each carried items * in the inventory. - * Also contains amount, and if equipped. + * Also contains amount. */ struct StoredItem { unsigned int itemId; - unsigned short amount; - bool equipped; + unsigned char amount; +}; + +/** + * Equipped items that keeps which kind of item is in equipment. + */ +struct EquippedItem +{ + unsigned int itemId; + short itemType; }; /** * Class used to store minimal info on player's inventories * to keep it fast. - * See Item and ItemReference to get more info on an item. + * See Item and ItemManager to get more info on an item. */ class Inventory { public: /** - * ctor. + * ctor. Add slot spaces to MAX_ITEMS_IN_INVENTORY. */ - Inventory() {} + Inventory(); /** - * Convenience function to get index from ItemId. + * Convenience function to get slot from ItemId. * If more than one occurence is found, the first is given. */ - unsigned short - getSlotIndex(const unsigned int itemId); + unsigned char + getSlotFromId(const unsigned int itemId); /** * Return StoredItem */ StoredItem - getStoredItemAt(unsigned short index) const { return itemList.at(index); }; + getStoredItemAt(unsigned char slot) const { return itemList.at(slot); }; /** * Return Item reference from ItemReference */ //ItemPtr getItem(unsigned short index) const - //{ return itemReferencegetItem(itemList.at(index).itemId); }; + //{ return itemReference.getItem(itemList.at(index).itemId); }; /** - * Tells if an item is equipped + * Search in inventory only if an item is present. + * Don't tell if an item is equipped. */ bool - isEquipped(unsigned short index) const { return itemList.at(index).equipped; }; + hasItem(unsigned int itemId); /** * Tells an item's amount */ unsigned short - getItemAmount(unsigned short index) const { return itemList.at(index).amount; }; + getItemAmount(unsigned char slot) const { return itemList.at(slot).amount; }; /** * Return Item reference Id */ unsigned int - getItemId(unsigned short index) const { return itemList.at(index).itemId; }; + getItemId(unsigned char slot) const { return itemList.at(slot).itemId; }; /** * add an item with amount - * (creates it non-equipped if amount was 0) + * (don't create it if amount was 0) + * @return short value: Indicates the number of items added. */ - bool - addItem(unsigned int itemId, unsigned short amount); + short + addItem(unsigned int itemId, unsigned char amount = 1); /** * Remove an item searched by ItemId. * Delete if amount = 0. + * @return short value: Indicates the number of items removed. + * This function removes the given amount using every slots + * if necessary. */ - bool - removeItem(unsigned int itemId, unsigned short amount = 0); + short + removeItem(unsigned int itemId, unsigned char amount = 0); /** * Remove an item searched by slot index. * Delete if amount = 0. + * @return short value: Indicates the number of items removed. + * Removes only in the given slot. */ - bool - removeItem(unsigned short index, unsigned short amount = 0); + short + removeItem(unsigned char slot, unsigned char amount = 0); /** * Equip an item searched by its id. @@ -126,20 +159,20 @@ class Inventory * Equip an item searched by its slot index. */ bool - equipItem(unsigned short index); + equipItem(unsigned char slot); /** * Unequip an item searched by its slot index. */ bool - unequipItem(unsigned short index); + unequipItem(unsigned char slot); /** * The function called to use an item applying * only the modifiers (for simple items...) */ bool - use(unsigned short index, BeingPtr itemUser); + use(unsigned char slot, BeingPtr itemUser); /** * The function called to use an item applying @@ -148,23 +181,21 @@ class Inventory bool use(unsigned int itemId, BeingPtr itemUser); + private: + /** - * The function called to use an item - * using a script (for complex actions) + * Give the first free slot number in itemList. */ - bool - useWithScript(unsigned short index, const std::string scriptFile); + unsigned char getInventoryFreeSlot(); + // Stored items in inventory and equipment + std::vector<StoredItem> itemList; /**< Items in inventory */ + std::vector<EquippedItem> equippedItemList; /**< Equipped Items */ /** - * The function called to use an item - * using a script (for complex actions) + * Used to know which type of arrow is used with a bow, + * for instance */ - bool - useWithScript(unsigned int itemId, const std::string scriptFile); - - private: - //Item type - std::vector<StoredItem> itemList; + StoredItem equippedProjectiles; }; /** diff --git a/src/itemmanager.cpp b/src/itemmanager.cpp index ae3351ca..79ce24e0 100644 --- a/src/itemmanager.cpp +++ b/src/itemmanager.cpp @@ -136,7 +136,7 @@ ItemManager::ItemManager(const std::string &itemReferenceFile) if (id != 0) { ItemPtr item(new Item(modifiers, itemType, weight, - value, scriptName)); + value, scriptName, maxPerSlot)); mItemReference[id] = item; nbItems++; } @@ -160,7 +160,32 @@ ItemManager::ItemManager(const std::string &itemReferenceFile) LOG_INFO("Item: ID: " << id << ", itemType: " << itemType << ", weight: " << weight << ", value: " << value << ", scriptName: " << scriptName << ", maxPerSlot: " << maxPerSlot << ".", 3); - //TODO: Log level 5 with everything + // Log level 5 + LOG_INFO("Modifiers:: element: " << modifiers.element << + ", lifetime: " << modifiers.lifetime + << std::endl << + ", strength: " << modifiers.rawStats[STAT_STRENGTH] << + ", agility: " << modifiers.rawStats[STAT_AGILITY] << + ", vitality: " << modifiers.rawStats[STAT_VITALITY] + << std::endl << + ", intelligence: " << modifiers.rawStats[STAT_INTELLIGENCE] << + ", dexterity: " << modifiers.rawStats[STAT_DEXTERITY] << + ", luck: " << modifiers.rawStats[STAT_LUCK] + << std::endl << + ", heat: " << modifiers.computedStats[STAT_HEAT] << + ",attack: " << modifiers.computedStats[STAT_ATTACK] << + ", defence: " << modifiers.computedStats[STAT_DEFENCE] + << std::endl << + ", magic: " << modifiers.computedStats[STAT_MAGIC] << + ", accuracy: " << modifiers.computedStats[STAT_ACCURACY] << + ", speed: " << modifiers.computedStats[STAT_SPEED] << + std::endl << + ", hp: " << modifiers.hp << + ", mp: " << modifiers.mp << + std::endl << + ", range: " << modifiers.range << + ", weapon_type: " << modifiers.weaponType << + ", status_effect: " << modifiers.beingStateEffect, 5); } LOG_INFO("Loaded " << nbItems << " items from " diff --git a/src/itemmanager.h b/src/itemmanager.h index 3a80db40..b3889a9c 100644 --- a/src/itemmanager.h +++ b/src/itemmanager.h @@ -39,7 +39,7 @@ class ItemManager /** * Constructor (loads item reference file) */ - ItemManager(std::string itemReferenceFile); + ItemManager(const std::string &itemReferenceFile); /** * Destructor @@ -74,6 +74,12 @@ class ItemManager { return mItemReference[itemId].get()->getGoldValue(); }; /** + * Return max item per slot + */ + unsigned short getMaxPerSlot(const unsigned int itemId) + { return mItemReference[itemId].get()->getMaxPerSlot(); }; + + /** * Return item's modifiers */ Modifiers diff --git a/src/player.cpp b/src/player.cpp index 0be018d6..e4c49b12 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -44,56 +44,32 @@ void Player::update() setStat(STAT_SPEED, mRawStats.stats[STAT_DEXTERITY]); } -void Player::setInventory(const std::vector<unsigned int> &inven) +void Player::setInventory(const Inventory &inven) { inventory = inven; } -bool Player::addInventory(unsigned int itemId) +bool Player::addItem(unsigned int itemId, unsigned char amount) { - // If required weight could be tallied to see if player can pick up more. - inventory.push_back(itemId); - return true; + return inventory.addItem(itemId, amount); } -bool Player::delInventory(unsigned int itemId) +bool Player::removeItem(unsigned int itemId, unsigned char amount) { - for (std::vector<unsigned int>::iterator i = inventory.begin(); - i != inventory.end(); i++) { - if (*i == itemId) { - inventory.erase(i); - return true; - } - } - return false; + return inventory.removeItem(itemId, amount); } bool Player::hasItem(unsigned int itemId) { - for (std::vector<unsigned int>::iterator i = inventory.begin(); - i != inventory.end(); i++) - { - if (*i == itemId) - return true; - } - return false; + return inventory.hasItem(itemId); } -bool Player::equip(unsigned int itemId, unsigned char slot) +bool Player::equip(unsigned char slot) { - // currently this is too simplistic and doesn't check enough - // but until further functionality is implemented in the - // server it will suffice - if (slot < MAX_EQUIP_SLOTS) { - equipment[slot] = itemId; - return true; - } else - return false; + return true; // TODO } bool Player::unequip(unsigned char slot) { - // NOTE: 0 will be invalid item id (or we could use key/value pairs) - equipment[slot] = 0; - return true; + return true; // TODO } diff --git a/src/player.h b/src/player.h index 03917703..83f5fee1 100644 --- a/src/player.h +++ b/src/player.h @@ -27,12 +27,11 @@ #include <vector> #include "being.h" +#include "inventory.h" + #include "defines.h" #include "utils/countedptr.h" -/** Maximum number of equipped slots */ -const unsigned int MAX_EQUIP_SLOTS = 5; - class GameClient; class Player : public Being @@ -161,7 +160,7 @@ class Player : public Being * Sets inventory. */ void - setInventory(const std::vector<unsigned int> &inven); + setInventory(const Inventory &inven); /** * Adds item with ID to inventory. @@ -169,7 +168,7 @@ class Player : public Being * @return Item add success/failure */ bool - addInventory(unsigned int itemId); + addItem(unsigned int itemId, unsigned char amount = 1); /** * Removes item with ID from inventory. @@ -177,7 +176,7 @@ class Player : public Being * @return Item delete success/failure */ bool - delInventory(unsigned int itemId); + removeItem(unsigned int itemId, unsigned char amount = 0); /** * Checks if character has an item. @@ -193,7 +192,7 @@ class Player : public Being * @return Equip success/failure */ bool - equip(unsigned int itemId, unsigned char slot); + equip(unsigned char slot); /** * Un-equips item. @@ -237,10 +236,7 @@ class Player : public Being unsigned int mMoney; /**< wealth of the being */ RawStatistics mRawStats; /**< raw stats of the being */ - std::vector<unsigned int> inventory; /**< Player inventory */ - - /** Equipped item ID's (from inventory) */ - unsigned int equipment[MAX_EQUIP_SLOTS]; + Inventory inventory; /**< Player inventory and Equipment */ friend class GameClient; }; |