From 4a4acd479ee8de82df0935b3511257ade13258b6 Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Wed, 27 Jul 2011 20:05:50 +0200 Subject: Reworked the way equip slot info are loaded in a more logical way. --- example/clientdata/equip.xml | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) (limited to 'example') diff --git a/example/clientdata/equip.xml b/example/clientdata/equip.xml index 0d1c1d3b..097229cd 100644 --- a/example/clientdata/equip.xml +++ b/example/clientdata/equip.xml @@ -1,12 +1,19 @@ + - - - - - - - - - + + + + + + + + + -- cgit v1.2.3-60-g2f50 From 67af89b5fe14d2d0431cefb9b714f72873a74e16 Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Fri, 19 Aug 2011 00:56:22 +0200 Subject: Used the client base to make a final bugfix on the server code. Made the server handle only one slot type requirement since it's irrelevant to have more anyway. Plus, it simplifies the code for both equipping/unequipping. I also added a dagger to show how equipping/unequipping is working. --- example/clientdata/equip.xml | 2 +- .../equipment/weapons/weapon-dagger-dagger.png | Bin 0 -> 582 bytes .../graphics/sprites/weapons/weapon-dagger.xml | 27 +++ example/clientdata/items.xml | 17 ++ example/serverdata/scripts/maps/desert.lua | 2 +- src/common/inventorydata.h | 2 +- src/game-server/inventory.cpp | 225 ++++++++++----------- src/game-server/inventory.h | 8 +- src/game-server/item.h | 25 ++- src/game-server/itemmanager.cpp | 21 +- 10 files changed, 194 insertions(+), 135 deletions(-) create mode 100644 example/clientdata/graphics/items/equipment/weapons/weapon-dagger-dagger.png create mode 100644 example/clientdata/graphics/sprites/weapons/weapon-dagger.xml (limited to 'example') diff --git a/example/clientdata/equip.xml b/example/clientdata/equip.xml index 097229cd..1a17fd22 100644 --- a/example/clientdata/equip.xml +++ b/example/clientdata/equip.xml @@ -14,6 +14,6 @@ - + diff --git a/example/clientdata/graphics/items/equipment/weapons/weapon-dagger-dagger.png b/example/clientdata/graphics/items/equipment/weapons/weapon-dagger-dagger.png new file mode 100644 index 00000000..cdfc3708 Binary files /dev/null and b/example/clientdata/graphics/items/equipment/weapons/weapon-dagger-dagger.png differ diff --git a/example/clientdata/graphics/sprites/weapons/weapon-dagger.xml b/example/clientdata/graphics/sprites/weapons/weapon-dagger.xml new file mode 100644 index 00000000..1909aff6 --- /dev/null +++ b/example/clientdata/graphics/sprites/weapons/weapon-dagger.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/example/clientdata/items.xml b/example/clientdata/items.xml index 6f72185b..3e204ef4 100644 --- a/example/clientdata/items.xml +++ b/example/clientdata/items.xml @@ -142,6 +142,23 @@ weapons/weapon-bow.xml + + + + + + + + + + + + + weapons/weapon-dagger.xml + { item } typedef std::map< unsigned int, InventoryItem > InventoryData; -// equip slot id -> { item id } +// equip slot id -> { item id, item instance } // Equipment taking up multiple equip slot ids will be referenced multiple times typedef std::multimap< unsigned int, EquipmentItem > EquipData; diff --git a/src/game-server/inventory.cpp b/src/game-server/inventory.cpp index c3d2bfcb..55986106 100644 --- a/src/game-server/inventory.cpp +++ b/src/game-server/inventory.cpp @@ -600,58 +600,66 @@ bool Inventory::equip(int inventorySlot) return false; // Test the equip requirements. If none, it's not an equipable item. - const ItemEquipsInfo &equipInfoList = - itemManager->getItem(it->second.itemId)->getItemEquipData(); - if (equipInfoList.empty()) + const ItemEquipRequirement &equipReq = + itemManager->getItem(it->second.itemId)->getItemEquipRequirement(); + if (!equipReq.equipSlotId) { LOG_DEBUG("No equip requirements for item id: " << it->second.itemId << " at slot: " << inventorySlot); return false; } - // Iterate through all slots requirements. - std::list equipSlotsToUnequipFirst; - for (ItemEquipsInfo::const_iterator it2 = equipInfoList.begin(), - it2_end = equipInfoList.end(); it2 != it2_end; ++it2) - { - // We first check the equipment slots for: - // - 1. whether enough total equip slot space is available. - // - 2. whether some other equipment is to be unequipped first. + // List of potential unique itemInstances to unequip first. + std::set equipInstancesToUnequipFirst; - // If not enough total space in the equipment slot is available, - // we cannot equip. - if (itemManager->getEquipSlotCapacity(it2->first) < it2->second) - { - LOG_DEBUG("Not enough equip capacity at slot: " << it2->first - << ", total available: " - << itemManager->getEquipSlotCapacity(it2->first) - << ", required: " << it2->second); - return false; - } + // We first check the equipment slots for: + // - 1. whether enough total equip slot space is available. + // - 2. whether some other equipment is to be unequipped first. + + // If not enough total space in the equipment slot is available, + // we cannot equip. + if (itemManager->getEquipSlotCapacity(equipReq.equipSlotId) + < equipReq.capacityRequired) + { + LOG_DEBUG("Not enough equip capacity at slot: " << equipReq.equipSlotId + << ", total available: " + << itemManager->getEquipSlotCapacity(equipReq.equipSlotId) + << ", required: " << equipReq.capacityRequired); + return false; + } - // Test whether some item(s) is(are) to be unequipped first. - if (!checkEquipmentCapacity(it2->first, it2->second)) + // Test whether some item(s) is(are) to be unequipped first. + if (!checkEquipmentCapacity(equipReq.equipSlotId, + equipReq.capacityRequired)) + { + // And test whether the unequip action would succeed first. + if (testUnequipScriptRequirements(equipReq.equipSlotId) + && hasInventoryEnoughSpace(equipReq.equipSlotId)) { - // And test whether the unequip action would succeed first. - if (testUnequipScriptRequirements(it2->first) - && hasInventoryEnoughSpace(it2->first)) + // Then, we unequip each iteminstance of the equip slot + for (EquipData::iterator iter = + mPoss->equipSlots.begin(); + iter != mPoss->equipSlots.end(); ++iter) { - equipSlotsToUnequipFirst.push_back(it2->first); - } - else - { - // Some non-unequippable equipment is to be unequipped first. - // Can be the case of cursed items, - // or when the inventory is full, for instance. - return false; + if (iter->first == equipReq.equipSlotId + && iter->second.itemInstance) + equipInstancesToUnequipFirst.insert( + iter->second.itemInstance); } } + else + { + // Some non-unequippable equipment is to be unequipped first. + // Can be the case of cursed items, + // or when the inventory is full, for instance. + return false; + } } // Potential Pre-unequipment process - for (std::list::const_iterator it3 = - equipSlotsToUnequipFirst.begin(); - it3 != equipSlotsToUnequipFirst.end(); ++it3) + for (std::set::const_iterator it3 = + equipInstancesToUnequipFirst.begin(); + it3 != equipInstancesToUnequipFirst.end(); ++it3) { if (!unequip(*it3)) { @@ -667,69 +675,65 @@ bool Inventory::equip(int inventorySlot) //W equip slot type count, W item id, { W equip slot, W capacity used}* MessageOut equipMsg(GPMSG_EQUIP); equipMsg.writeInt16(it->second.itemId); // Item Id - equipMsg.writeInt16(equipInfoList.size()); // Number of equip slot changed. + equipMsg.writeInt16(1); // Number of equip slot changed. // Compute an unique equip item Instance id (unicity is per character only.) int itemInstance = getNewEquipItemInstance(); - for (ItemEquipsInfo::const_iterator it2 = equipInfoList.begin(), - it2_end = equipInfoList.end(); it2 != it2_end; ++it2) + unsigned int capacityLeft = equipReq.capacityRequired; + unsigned int capacityUsed = 0; + // Apply equipment changes + for (EquipData::iterator it4 = mPoss->equipSlots.begin(), + it4_end = mPoss->equipSlots.end(); it4 != it4_end; ++it4) { - unsigned int capacityLeft = it2->second; - unsigned int capacityUsed = 0; - // Apply equipment changes - for (EquipData::iterator it4 = mPoss->equipSlots.begin(), - it4_end = mPoss->equipSlots.end(); it4 != it4_end; ++it4) - { - if (!capacityLeft) - break; + if (!capacityLeft) + break; - // We've found an existing equip slot - if (it4->first == it2->first) + // We've found an existing equip slot + if (it4->first == equipReq.equipSlotId) + { + // We've found an empty slot + if (it4->second.itemInstance == 0) { - // We've found an empty slot - if (it4->second.itemInstance == 0) - { - it4->second.itemId = it->second.itemId; - it4->second.itemInstance = itemInstance; - --capacityLeft; - } - else // The slot is already in use. - { - ++capacityUsed; - } + it4->second.itemId = it->second.itemId; + it4->second.itemInstance = itemInstance; + --capacityLeft; + } + else // The slot is already in use. + { + ++capacityUsed; } } + } - // When there is still something to apply even when out of that loop, - // It means that the equip multimapis missing empty slots. - // Hence, we add them back - if(capacityLeft) - { - unsigned int maxCapacity = - itemManager->getEquipSlotCapacity(it2->first); + // When there is still something to apply even when out of that loop, + // It means that the equip multimapis missing empty slots. + // Hence, we add them back + if(capacityLeft) + { + unsigned int maxCapacity = + itemManager->getEquipSlotCapacity(equipReq.equipSlotId); - // A should never happen case - assert(maxCapacity >= capacityUsed + capacityLeft); + // A should never happen case + assert(maxCapacity >= capacityUsed + capacityLeft); - while (capacityLeft) - { - EquipmentItem equipItem(it->second.itemId, itemInstance); - mPoss->equipSlots.insert( - std::make_pair - (it2->first, equipItem)); - --capacityLeft; - } + while (capacityLeft) + { + EquipmentItem equipItem(it->second.itemId, itemInstance); + mPoss->equipSlots.insert( + std::make_pair + (equipReq.equipSlotId, equipItem)); + --capacityLeft; } - - // Equip slot - equipMsg.writeInt16(it2->first); - // Capacity used - equipMsg.writeInt16(it2->second); - // Item instance - equipMsg.writeInt16(itemInstance); } + // Equip slot + equipMsg.writeInt16(equipReq.equipSlotId); + // Capacity used + equipMsg.writeInt16(equipReq.capacityRequired); + // Item instance + equipMsg.writeInt16(itemInstance); + // New item trigger updateEquipmentTrigger(0, it->second.itemId); @@ -744,54 +748,47 @@ bool Inventory::equip(int inventorySlot) bool Inventory::unequip(unsigned int itemInstance) { - // map of { itemInstance, itemId } - std::map itemIdListToInventory; + if (!itemInstance) + return false; MessageOut equipMsg(GPMSG_EQUIP); equipMsg.writeInt16(0); // Item Id, useless in case of unequip. + // The itemId to unequip + unsigned int itemId = 0; + // Empties all equip entries that point to the given equipment slot // The equipment slots should NEVER be erased after initialization! for (EquipData::iterator it = mPoss->equipSlots.begin(), it_end = mPoss->equipSlots.end(); it != it_end; ++it) { - if (it->second.itemInstance == itemInstance) + if (it->second.itemInstance == itemInstance && it->second.itemId) { // Add the item to the inventory list if not already present there - std::map::const_iterator it2 = - itemIdListToInventory.find(it->second.itemInstance); - if (it2 == itemIdListToInventory.end()) - { - itemIdListToInventory.insert( - std::make_pair - (it->second.itemInstance, it->second.itemId)); - } - + itemId = it->second.itemId; it->second.itemId = 0; it->second.itemInstance = 0; } } + // When there were no corresponding item id, it means no item was to + // be unequipped. + if (!itemId) + return false; + // Number of slot types touched, - equipMsg.writeInt16(itemIdListToInventory.size()); + equipMsg.writeInt16(1); - // Apply unequip trigger(s), and move the item(s) back to inventory. - for (std::map::const_iterator it2 = - itemIdListToInventory.begin(), it2_end = itemIdListToInventory.end(); - it2 != it2_end; ++it2) - { - updateEquipmentTrigger(it2->second, 0); - insert(it2->second, 1); + // Move the item back to inventory. + insert(itemId, 1); - equipMsg.writeInt16(it2->first); - equipMsg.writeInt16(0); // Capacity used, set to 0 to unequip. - } + equipMsg.writeInt16(itemInstance); + equipMsg.writeInt16(0); // Capacity used, set to 0 to unequip. - if (equipMsg.getLength() > 2) - { - gameHandler->sendTo(mCharacter, equipMsg); - return true; - } + gameHandler->sendTo(mCharacter, equipMsg); + + // Apply unequip trigger + updateEquipmentTrigger(itemId, 0); - return false; + return true; } diff --git a/src/game-server/inventory.h b/src/game-server/inventory.h index 39f83216..1eaf0527 100644 --- a/src/game-server/inventory.h +++ b/src/game-server/inventory.h @@ -120,7 +120,7 @@ class Inventory * @todo */ bool hasInventoryEnoughSpace(unsigned int equipmentSlot) - { return false; } + { return true; } /** * Test the items unequipment requirements. @@ -152,6 +152,12 @@ class Inventory */ void checkInventorySize(); + /** + * Check whether the equipment change has visible consequence + * on the character sprite. + */ + void checkLookUpdate(unsigned slotTypeId); + /** * Apply equipment triggers. */ diff --git a/src/game-server/item.h b/src/game-server/item.h index caef386a..f7c380f1 100644 --- a/src/game-server/item.h +++ b/src/game-server/item.h @@ -28,10 +28,15 @@ class Being; class Script; -// A pair indicating: Equipment slot id -> how much slots required. -typedef std::pair< unsigned int, unsigned int> ItemEquipInfo; -// The list of required slots to equip. -typedef std::list< ItemEquipInfo > ItemEquipsInfo; +// Indicates the equip slot "cost" to equip an item. +struct ItemEquipRequirement { + ItemEquipRequirement(): + equipSlotId(0), + capacityRequired(0) + {} + + unsigned int equipSlotId, capacityRequired; +}; /** * State effects to beings, and actors. @@ -226,9 +231,10 @@ class ItemClass { return mSpriteID; } /** - * Returns equip requirements. + * Returns equip requirement. */ - const ItemEquipsInfo &getItemEquipData() const { return mEquip; } + const ItemEquipRequirement &getItemEquipRequirement() const + { return mEquipReq; } private: /** @@ -274,12 +280,9 @@ class ItemClass std::multimap< ItemTriggerType, ItemEffectInfo * > mDispells; /** - * List of list of requirements for equipping. Only one inner list - * need be satisfied to sucessfully equip. Checks occur in order - * from outer front to back. - * All conditions in an inner list must be met for success. + * Requirement for equipping. */ - ItemEquipsInfo mEquip; + ItemEquipRequirement mEquipReq; friend class ItemManager; }; diff --git a/src/game-server/itemmanager.cpp b/src/game-server/itemmanager.cpp index 074b8343..d41212bb 100644 --- a/src/game-server/itemmanager.cpp +++ b/src/game-server/itemmanager.cpp @@ -264,6 +264,13 @@ void ItemManager::readEquipNode(xmlNodePtr equipNode, ItemClass *item) { if (xmlStrEqual(subNode->name, BAD_CAST "slot")) { + if (item->mEquipReq.equipSlotId) + { + LOG_WARN("Item Manager: duplicate equip slot definitions!" + " Only the first will apply."); + break; + } + std::string slot = XML::getProperty(subNode, "type", std::string()); if (slot.empty()) { @@ -273,21 +280,23 @@ void ItemManager::readEquipNode(xmlNodePtr equipNode, ItemClass *item) if (utils::isNumeric(slot)) { // When the slot id is given - item->mEquip.push_back(std::make_pair(utils::stringToInt(slot), - XML::getProperty(subNode, "required", 1))); + item->mEquipReq.equipSlotId = utils::stringToInt(slot); } else { // When its name is given - item->mEquip.push_back(std::make_pair(getEquipSlotIdFromName(slot), - XML::getProperty(subNode, "required", 1))); + item->mEquipReq.equipSlotId = getEquipSlotIdFromName(slot); } + item->mEquipReq.capacityRequired = + XML::getProperty(subNode, "required", 1); } } - if (item->mEquip.empty()) + + if (!item->mEquipReq.equipSlotId) { LOG_WARN("Item Manager: empty equip requirement " - "definition for item " << item->getDatabaseID() << "!"); + "definition for item " << item->getDatabaseID() << "!" + " The item will be unequippable."); return; } } -- cgit v1.2.3-60-g2f50 From 2a581a0f65f4a381a76590be0f8a6e1048347177 Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Fri, 19 Aug 2011 02:38:49 +0200 Subject: Added boots and pants to play with on the test map. --- example/clientdata/equip.xml | 18 ++-- .../graphics/items/equipment/feet/boots.png | Bin 0 -> 333 bytes .../graphics/items/equipment/legs/pants.png | Bin 0 -> 855 bytes .../sprites/equipment/feet/boots-female.png | Bin 0 -> 8734 bytes .../sprites/equipment/feet/boots-female.xml | 8 ++ .../graphics/sprites/equipment/feet/boots-male.png | Bin 0 -> 8662 bytes .../graphics/sprites/equipment/feet/boots-male.xml | 104 +++++++++++++++++++++ .../sprites/equipment/legs/pants-female.png | Bin 0 -> 19817 bytes .../sprites/equipment/legs/pants-female.xml | 104 +++++++++++++++++++++ .../graphics/sprites/equipment/legs/pants-male.png | Bin 0 -> 55222 bytes .../graphics/sprites/equipment/legs/pants-male.xml | 104 +++++++++++++++++++++ example/clientdata/items.xml | 34 ++++++- example/serverdata/scripts/maps/desert.lua | 2 +- 13 files changed, 363 insertions(+), 11 deletions(-) create mode 100644 example/clientdata/graphics/items/equipment/feet/boots.png create mode 100644 example/clientdata/graphics/items/equipment/legs/pants.png create mode 100644 example/clientdata/graphics/sprites/equipment/feet/boots-female.png create mode 100644 example/clientdata/graphics/sprites/equipment/feet/boots-female.xml create mode 100644 example/clientdata/graphics/sprites/equipment/feet/boots-male.png create mode 100644 example/clientdata/graphics/sprites/equipment/feet/boots-male.xml create mode 100644 example/clientdata/graphics/sprites/equipment/legs/pants-female.png create mode 100644 example/clientdata/graphics/sprites/equipment/legs/pants-female.xml create mode 100644 example/clientdata/graphics/sprites/equipment/legs/pants-male.png create mode 100644 example/clientdata/graphics/sprites/equipment/legs/pants-male.xml (limited to 'example') diff --git a/example/clientdata/equip.xml b/example/clientdata/equip.xml index 1a17fd22..be180a07 100644 --- a/example/clientdata/equip.xml +++ b/example/clientdata/equip.xml @@ -7,13 +7,13 @@ updates when making changes on it. --> - - - - - - - - - + + + + + + + + + diff --git a/example/clientdata/graphics/items/equipment/feet/boots.png b/example/clientdata/graphics/items/equipment/feet/boots.png new file mode 100644 index 00000000..9f9913ff Binary files /dev/null and b/example/clientdata/graphics/items/equipment/feet/boots.png differ diff --git a/example/clientdata/graphics/items/equipment/legs/pants.png b/example/clientdata/graphics/items/equipment/legs/pants.png new file mode 100644 index 00000000..c50daf22 Binary files /dev/null and b/example/clientdata/graphics/items/equipment/legs/pants.png differ diff --git a/example/clientdata/graphics/sprites/equipment/feet/boots-female.png b/example/clientdata/graphics/sprites/equipment/feet/boots-female.png new file mode 100644 index 00000000..ba984324 Binary files /dev/null and b/example/clientdata/graphics/sprites/equipment/feet/boots-female.png differ diff --git a/example/clientdata/graphics/sprites/equipment/feet/boots-female.xml b/example/clientdata/graphics/sprites/equipment/feet/boots-female.xml new file mode 100644 index 00000000..4bb2fffe --- /dev/null +++ b/example/clientdata/graphics/sprites/equipment/feet/boots-female.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/example/clientdata/graphics/sprites/equipment/feet/boots-male.png b/example/clientdata/graphics/sprites/equipment/feet/boots-male.png new file mode 100644 index 00000000..f441c5b0 Binary files /dev/null and b/example/clientdata/graphics/sprites/equipment/feet/boots-male.png differ diff --git a/example/clientdata/graphics/sprites/equipment/feet/boots-male.xml b/example/clientdata/graphics/sprites/equipment/feet/boots-male.xml new file mode 100644 index 00000000..7edbcef6 --- /dev/null +++ b/example/clientdata/graphics/sprites/equipment/feet/boots-male.xml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/clientdata/graphics/sprites/equipment/legs/pants-female.png b/example/clientdata/graphics/sprites/equipment/legs/pants-female.png new file mode 100644 index 00000000..ad8c2e0e Binary files /dev/null and b/example/clientdata/graphics/sprites/equipment/legs/pants-female.png differ diff --git a/example/clientdata/graphics/sprites/equipment/legs/pants-female.xml b/example/clientdata/graphics/sprites/equipment/legs/pants-female.xml new file mode 100644 index 00000000..4f5dbcdf --- /dev/null +++ b/example/clientdata/graphics/sprites/equipment/legs/pants-female.xml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/clientdata/graphics/sprites/equipment/legs/pants-male.png b/example/clientdata/graphics/sprites/equipment/legs/pants-male.png new file mode 100644 index 00000000..f8415753 Binary files /dev/null and b/example/clientdata/graphics/sprites/equipment/legs/pants-male.png differ diff --git a/example/clientdata/graphics/sprites/equipment/legs/pants-male.xml b/example/clientdata/graphics/sprites/equipment/legs/pants-male.xml new file mode 100644 index 00000000..3a90c6aa --- /dev/null +++ b/example/clientdata/graphics/sprites/equipment/legs/pants-male.xml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/example/clientdata/items.xml b/example/clientdata/items.xml index 3e204ef4..3078d9de 100644 --- a/example/clientdata/items.xml +++ b/example/clientdata/items.xml @@ -177,7 +177,39 @@ equipment/chest/chest-leather-male.xml equipment/chest/chest-leather-female.xml - + + + + + + + + + + + equipment/legs/pants-male.xml + equipment/legs/pants-female.xml + + + + + + + + + + + + equipment/feet/boots-male.xml|#4f2d29,c89078 + equipment/feet/boots-female.xml|#4f2d29,c89078 + +