diff options
author | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2011-08-11 02:54:10 +0200 |
---|---|---|
committer | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2011-08-11 02:54:10 +0200 |
commit | 9527d079307454178fea3b2958e301875cecc15a (patch) | |
tree | 333ed71036813c9cdec4c8e1de1e058b86275bca /src/game-server | |
parent | 0820d6632b0ce5887c49d16929ac7903aa185fe4 (diff) | |
parent | 8766149dc12d197205b1632ec6e9fc663de05990 (diff) | |
download | manaserv-9527d079307454178fea3b2958e301875cecc15a.tar.gz manaserv-9527d079307454178fea3b2958e301875cecc15a.tar.bz2 manaserv-9527d079307454178fea3b2958e301875cecc15a.tar.xz manaserv-9527d079307454178fea3b2958e301875cecc15a.zip |
Merge branch 'master' of git://gitorious.org/~bertram/mana/manaserv-equipment-fix into equipment-fix
Conflicts:
src/common/manaserv_protocol.h
Diffstat (limited to 'src/game-server')
-rw-r--r-- | src/game-server/buysell.cpp | 6 | ||||
-rw-r--r-- | src/game-server/character.cpp | 2 | ||||
-rw-r--r-- | src/game-server/gamehandler.cpp | 16 | ||||
-rw-r--r-- | src/game-server/inventory.cpp | 924 | ||||
-rw-r--r-- | src/game-server/inventory.h | 140 | ||||
-rw-r--r-- | src/game-server/item.h | 4 | ||||
-rw-r--r-- | src/game-server/itemmanager.cpp | 123 | ||||
-rw-r--r-- | src/game-server/itemmanager.h | 44 | ||||
-rw-r--r-- | src/game-server/state.cpp | 10 | ||||
-rw-r--r-- | src/game-server/trade.cpp | 4 |
10 files changed, 631 insertions, 642 deletions
diff --git a/src/game-server/buysell.cpp b/src/game-server/buysell.cpp index a9658546..78c2bfe0 100644 --- a/src/game-server/buysell.cpp +++ b/src/game-server/buysell.cpp @@ -72,9 +72,9 @@ int BuySell::registerPlayerItems() // We parse the player inventory and add all item // in a sell list. - const Possessions &charPoss = mChar->getPossessions(); - for (InventoryData::const_iterator it = charPoss.inventory.begin(), - it_end = charPoss.inventory.end(); it != it_end; ++it) + const InventoryData &inventoryData = mChar->getPossessions().getInventory(); + for (InventoryData::const_iterator it = inventoryData.begin(), + it_end = inventoryData.end(); it != it_end; ++it) { unsigned int nb = it->second.amount; if (!nb) diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp index bbe26bd6..ef001638 100644 --- a/src/game-server/character.cpp +++ b/src/game-server/character.cpp @@ -81,7 +81,7 @@ Character::Character(MessageIn &msg): setName(msg.readString()); deserializeCharacterData(*this, msg); mOld = getPosition(); - Inventory(this).initialise(); + Inventory(this).initialize(); modifiedAllAttribute(); setSize(16); diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp index 58d289f0..30a620cd 100644 --- a/src/game-server/gamehandler.cpp +++ b/src/game-server/gamehandler.cpp @@ -489,7 +489,7 @@ void GameHandler::handlePickup(GameClient &client, MessageIn &message) void GameHandler::handleUseItem(GameClient &client, MessageIn &message) { - const int slot = message.readInt8(); + const int slot = message.readInt16(); Inventory inv(client.character); if (ItemClass *ic = itemManager->getItem(inv.getItem(slot))) @@ -509,8 +509,8 @@ void GameHandler::handleUseItem(GameClient &client, MessageIn &message) void GameHandler::handleDrop(GameClient &client, MessageIn &message) { - const int slot = message.readInt8(); - const int amount = message.readInt8(); + const int slot = message.readInt16(); + const int amount = message.readInt16(); Inventory inv(client.character); if (ItemClass *ic = itemManager->getItem(inv.getItem(slot))) @@ -547,22 +547,22 @@ void GameHandler::handleWalk(GameClient &client, MessageIn &message) void GameHandler::handleEquip(GameClient &client, MessageIn &message) { - const int slot = message.readInt8(); + const int slot = message.readInt16(); Inventory(client.character).equip(slot); } void GameHandler::handleUnequip(GameClient &client, MessageIn &message) { - const int slot = message.readInt8(); + const int slot = message.readInt16(); if (slot >= 0 && slot < INVENTORY_SLOTS) Inventory(client.character).unequip(slot); } void GameHandler::handleMoveItem(GameClient &client, MessageIn &message) { - const int slot1 = message.readInt8(); - const int slot2 = message.readInt8(); - const int amount = message.readInt8(); + const int slot1 = message.readInt16(); + const int slot2 = message.readInt16(); + const int amount = message.readInt16(); Inventory(client.character).move(slot1, slot2, amount); // log transaction diff --git a/src/game-server/inventory.cpp b/src/game-server/inventory.cpp index 99390651..1eaf8b91 100644 --- a/src/game-server/inventory.cpp +++ b/src/game-server/inventory.cpp @@ -25,196 +25,13 @@ #include "game-server/inventory.h" #include "game-server/item.h" #include "game-server/itemmanager.h" +#include "game-server/state.h" #include "net/messageout.h" #include "utils/logger.h" -// TODO: -// - Inventory::initialise() Usable but could use a few more things -// - Inventory::equip() Usable but last part would be nice - -typedef std::set<unsigned int> ItemIdSet; - -Inventory::Inventory(Character *p, bool d): - mPoss(&p->getPossessions()), mInvMsg(GPMSG_INVENTORY), - mEqmMsg(GPMSG_EQUIP), mClient(p), mDelayed(d) -{ -} - -Inventory::~Inventory() -{ - commit(false); -} - -void Inventory::restart() +Inventory::Inventory(Character *p): + mPoss(&p->getPossessions()), mCharacter(p) { - mInvMsg.clear(); - mInvMsg.writeInt16(GPMSG_INVENTORY); -} - -void Inventory::cancel() -{ - assert(mDelayed); - Possessions &poss = mClient->getPossessions(); - if (mPoss != &poss) - { - delete mPoss; - mPoss = &poss; - } - restart(); -} - -void Inventory::commit(bool doRestart) -{ - Possessions &poss = mClient->getPossessions(); - /* Sends changes, whether delayed or not. */ - if (mInvMsg.getLength() > 2) - { - /* Send the message to the client directly. Perhaps this should be - done through an update flag, too? */ - gameHandler->sendTo(mClient, mInvMsg); - } - if (mPoss != &poss) - { - if (mDelayed) - { - /* - * Search for any and all changes to equipment. - * Search through equipment for changes between old and new equipment. - * Send changes directly when there is a change. - * Even when equipment references to invy slots are the same, it still - * needs to be searched for changes to the internal equiment slot - * usage. - * This is probably the worst part of doing this in delayed mode. - */ - IdSlotMap oldEquip, newEquip; - { - EquipData::const_iterator it1, it2, it1_end, it2_end; - for (it1 = mPoss->equipSlots.begin(), - it1_end = mPoss->equipSlots.end(); - it1 != it1_end; - ++it1) - { -#ifdef INV_CONST_BOUND_DEBUG - IdSlotMap::const_iterator temp2, temp = -#endif - newEquip.insert( - newEquip.upper_bound(it1->second), - std::make_pair(it1->second, it1->first)); -#ifdef INV_CONST_BOUND_DEBUG - if (temp != - --(temp2 = newEquip.upper_bound(it1->second))) - throw; -#endif - } - for (it2 = poss.equipSlots.begin(), - it2_end = poss.equipSlots.end(); - it2 != it2_end; - ++it2) - oldEquip.insert( - oldEquip.upper_bound(it2->second), - std::make_pair(it2->second, it2->first)); - } - { - IdSlotMap::const_iterator it1 = newEquip.begin(), - it2 = oldEquip.begin(), - it1_end = newEquip.end(), - it2_end = oldEquip.end(), - temp1, temp2; - while (it1 != it1_end || it2 != it2_end) - { - if (it1 == it1_end) - { - if (it2 == it2_end) - break; - equip_sub(0, it1); - } - else if (it2 == it2_end) - equip_sub(newEquip.count(it2->first), it2); - else if (it1->first == it2->first) - { - double invSlot = it1->first; - while ((it1 != it1_end && it1->first == invSlot) || - (it2 != it2_end && it2->first == invSlot)) - { - /* - * Item is still equipped, but need to check - * that the slots didn't change. - */ - if (it1->second == it2->second) - { - // No change. - ++it1; - ++it2; - continue; - } - unsigned int itemId = - mPoss->inventory.at(it1->first).itemId; - changeEquipment(itemId, itemId); - break; - } - } - else if (it1->first > it2->first) - equip_sub(newEquip.count(it2->first), it2); - else // it1->first < it2->first - equip_sub(0, it1); - } - } - } - poss = *mPoss; - delete mPoss; - mPoss = &poss; - } - - /* Update server sided states if in delayed mode. If we are not in - delayed mode, the server sided states already reflect the changes - that have just been sent to the client. */ - - if (mEqmMsg.getLength() > 2) - gameHandler->sendTo(mClient, mEqmMsg); - - if (doRestart) - restart(); -} - -void Inventory::equip_sub(unsigned int newCount, IdSlotMap::const_iterator &it) -{ - const unsigned int invSlot = it->first; - unsigned int count = 0, eqSlot = it->second; - mEqmMsg.writeInt16(invSlot); - mEqmMsg.writeInt8(newCount); - do { - if (newCount) - { - if (it->second != eqSlot) - { - mEqmMsg.writeInt8(eqSlot); - mEqmMsg.writeInt8(count); - count = 1; - eqSlot = it->second; - } - ++count; - } - if (itemManager->isEquipSlotVisible(it->second)) - mClient->raiseUpdateFlags(UPDATEFLAG_LOOKSCHANGE); - } while ((++it)->first == invSlot); - if (count) - { - mEqmMsg.writeInt8(eqSlot); - mEqmMsg.writeInt8(count); - } - mEqmMsg.writeInt16(invSlot); - changeEquipment(newCount ? 0 : mPoss->inventory.at(invSlot).itemId, - newCount ? mPoss->inventory.at(invSlot).itemId : 0); -} - -void Inventory::prepare() -{ - if (!mDelayed) - return; - - Possessions *poss = &mClient->getPossessions(); - if (mPoss == poss) - mPoss = new Possessions(*poss); } void Inventory::sendFull() const @@ -238,43 +55,42 @@ void Inventory::sendFull() const k != k_end; ++k) { - m.writeInt8(k->first); // equip slot - m.writeInt16(k->second); // inventory slot + m.writeInt16(k->first); // Equip slot id + m.writeInt16(k->second.itemId); // Item id + m.writeInt16(k->second.itemInstance); // Item instance } - gameHandler->sendTo(mClient, m); + gameHandler->sendTo(mCharacter, m); } -void Inventory::initialise() +void Inventory::initialize() { - assert(!mDelayed); - - InventoryData::iterator it1; - EquipData::const_iterator it2, it2_end = mPoss->equipSlots.end(); /* - * Apply all exists triggers. - * Remove unknown inventory items. + * Construct a set of item Ids to keep track of duplicate item Ids. */ - - ItemIdSet itemIds; + std::set<unsigned int> itemIds; /* * Construct a set of itemIds to keep track of duplicate itemIds. */ + InventoryData::iterator it1; for (it1 = mPoss->inventory.begin(); it1 != mPoss->inventory.end();) { ItemClass *item = itemManager->getItem(it1->second.itemId); if (item) { + // If the insertion succeeded, it's the first time we're + // adding the item in the inventory. Hence, we can trigger + // item presence in inventory effect. if (itemIds.insert(it1->second.itemId).second) - item->useTrigger(mClient, ITT_IN_INVY); + item->useTrigger(mCharacter, ITT_IN_INVY); ++it1; } else { LOG_WARN("Inventory: deleting unknown item type " << it1->second.itemId << " from the inventory of '" - << mClient->getName() + << mCharacter->getName() << "'!"); mPoss->inventory.erase(it1++); } @@ -282,46 +98,57 @@ void Inventory::initialise() itemIds.clear(); - typedef std::set<unsigned int> SlotSet; - SlotSet equipment; - /* - * Construct a set of slot references from equipment to keep track of - * duplicate slot usage. + * Equipment effects can be cumulative if more than one item instance + * is equipped, but we check to trigger the item presence in equipment + * effect only based on the first item instance insertion. */ - for (it2 = mPoss->equipSlots.begin(); it2 != it2_end; ++it2) + EquipData::iterator it2; + for (it2 = mPoss->equipSlots.begin(); it2 != mPoss->equipSlots.end();) { - if (equipment.insert(it2->second).second) + ItemClass *item = itemManager->getItem(it2->second.itemId); + if (item) { - /* - * Perform checks for equipped items - check that all needed slots are available. - */ - // TODO - Not needed for testing everything else right now, but - // will be needed for production - /* - * Apply all equip triggers. - */ - itemManager->getItem(mPoss->inventory.at(it2->second).itemId) - ->useTrigger(mClient, ITT_EQUIP); + // TODO: Check equip conditions. + // If not all needed slots are there, put the item back + // in the inventory. + } + else + { + LOG_WARN("Equipment: deleting unknown item id " + << it2->second.itemId << " from the equipment of '" + << mCharacter->getName() + << "'!"); + mPoss->equipSlots.erase(++it2); + continue; + } + + /* + * Apply all equip triggers at first item instance insertion + */ + if (itemIds.insert(it2->second.itemInstance).second) + { + itemManager->getItem(it2->second.itemId) + ->useTrigger(mCharacter, ITT_EQUIP); } - } - equipment.clear(); + ++it2; + } - checkSize(); + checkInventorySize(); } -void Inventory::checkSize() +void Inventory::checkInventorySize() { /* * Check that the inventory size is greater than or equal to the size * needed. - * If not, forcibly delete (drop?) items from the end until it is. + * If not, forcibly drop items from the end until it is. * Check that inventory capacity is greater than or equal to zero. - * If not, forcibly delete (drop?) items from the end until it is. + * If not, forcibly drop items from the end until it is. */ while (mPoss->inventory.size() > INVENTORY_SLOTS - || mClient->getModifiedAttribute(ATTR_INV_CAPACITY) < 0) + || mCharacter->getModifiedAttribute(ATTR_INV_CAPACITY) < 0) { LOG_WARN("Inventory: oversize inventory! Deleting '" << mPoss->inventory.rbegin()->second.amount @@ -330,11 +157,26 @@ void Inventory::checkSize() << "' from slot '" << mPoss->inventory.rbegin()->first << "' of character '" - << mClient->getName() + << mCharacter->getName() << "'!"); - // FIXME Should probably be dropped rather than deleted. + + // Remove the items from inventory removeFromSlot(mPoss->inventory.rbegin()->first, mPoss->inventory.rbegin()->second.amount); + + // Drop them on the floor + ItemClass *ic = itemManager->getItem(mPoss->inventory.rbegin()->first); + int nb = mPoss->inventory.rbegin()->second.amount; + Item *item = new Item(ic, nb); + item->setMap(mCharacter->getMap()); + item->setPosition(mCharacter->getPosition()); + if (!GameState::insert(item)) + { + // Warn about drop failure + LOG_WARN("Impossible to drop " << nb << " item(s) id: " + << ic->getDatabaseID() << " for character: '" + << mCharacter->getName() << "'!"); + } } } @@ -346,13 +188,19 @@ unsigned int Inventory::getItem(unsigned int slot) const unsigned int Inventory::insert(unsigned int itemId, unsigned int amount) { - unsigned int maxPerSlot = itemManager->getItem(itemId)->getMaxPerSlot(); if (!itemId || !amount) return 0; - prepare(); + + MessageOut invMsg(GPMSG_INVENTORY); + unsigned int maxPerSlot = itemManager->getItem(itemId)->getMaxPerSlot(); + + LOG_DEBUG("Inventory: Inserting " << amount << " item(s) Id: " << itemId + << " for character '" << mCharacter->getName() << "'."); + InventoryData::iterator it, it_end = mPoss->inventory.end(); // Add to slots with existing items of this type first. for (it = mPoss->inventory.begin(); it != it_end; ++it) + { if (it->second.itemId == itemId) { // If the slot is full, try the next slot @@ -360,31 +208,35 @@ unsigned int Inventory::insert(unsigned int itemId, unsigned int amount) continue; // Add everything that'll fit to the stack - unsigned short spaceleft = maxPerSlot - it->second.amount; - if (spaceleft >= amount) + unsigned short spaceLeft = maxPerSlot - it->second.amount; + if (spaceLeft >= amount) { it->second.amount += amount; amount = 0; + LOG_DEBUG("Everything inserted at slot id: " << it->first); } else { - it->second.amount += spaceleft; - amount -= spaceleft; + it->second.amount += spaceLeft; + amount -= spaceLeft; + LOG_DEBUG(spaceLeft << " item(s) inserted at slot id: " + << it->first); } - mInvMsg.writeInt16(it->first); - mInvMsg.writeInt16(itemId); - mInvMsg.writeInt16(it->second.amount); + invMsg.writeInt16(it->first); + invMsg.writeInt16(itemId); + invMsg.writeInt16(it->second.amount); if (!amount) - return 0; + break; } + } int slot = 0; // We still have some left, so add to blank slots. for (it = mPoss->inventory.begin();; ++it) { if (!amount) - return 0; + break; int lim = (it == it_end) ? INVENTORY_SLOTS : it->first; while (amount && slot < lim) { @@ -392,15 +244,21 @@ unsigned int Inventory::insert(unsigned int itemId, unsigned int amount) mPoss->inventory[slot].itemId = itemId; mPoss->inventory[slot].amount = additions; amount -= additions; - mInvMsg.writeInt16(slot++); // Last read, so also increment - mInvMsg.writeInt16(itemId); - mInvMsg.writeInt16(additions); + LOG_DEBUG(additions << " item(s) inserted at slot id: " << slot); + invMsg.writeInt16(slot++); // Last read, so also increment + invMsg.writeInt16(itemId); + invMsg.writeInt16(additions); } ++slot; // Skip the slot that the iterator points to - if (it == it_end) break; + if (it == it_end) + break; } - checkSize(); + // Send that first, before checking potential removals + if (invMsg.getLength() > 2) + gameHandler->sendTo(mCharacter, invMsg); + + checkInventorySize(); return amount; } @@ -416,73 +274,73 @@ unsigned int Inventory::count(unsigned int itemId) const return nb; } -unsigned int Inventory::remove(unsigned int itemId, unsigned int amount, bool force) +unsigned int Inventory::remove(unsigned int itemId, unsigned int amount) { - prepare(); - bool inv = false, - eq = !itemManager->getItem(itemId)->getItemEquipData().empty(); + if (!itemId || !amount) + return amount; + + LOG_DEBUG("Inventory: Request remove of " << amount << " item(s) id: " + << itemId << " for character: '" << mCharacter->getName() + << "'."); + + MessageOut invMsg(GPMSG_INVENTORY); + bool triggerLeaveInventory = true; for (InventoryData::iterator it = mPoss->inventory.begin(), - it_end = mPoss->inventory.end(); - it != it_end; ++it) + it_end = mPoss->inventory.end(); it != it_end; ++it) + { if (it->second.itemId == itemId) { if (amount) { - if (eq) - { - // If the item is equippable, we have additional checks to make. - bool ch = false; - for (EquipData::iterator it2 = mPoss->equipSlots.begin(), - it2_end = mPoss->equipSlots.end(); - it2 != it2_end; - ++it2) - if (it2->second == it->first) - { - if (force) - unequip(it2); - else - ch = inv = true; - break; - } - if (ch && !force) - continue; - } unsigned int sub = std::min(amount, it->second.amount); amount -= sub; it->second.amount -= sub; - mInvMsg.writeInt16(it->first); + invMsg.writeInt16(it->first); if (it->second.amount) { - mInvMsg.writeInt16(it->second.itemId); - mInvMsg.writeInt16(it->second.amount); + invMsg.writeInt16(it->second.itemId); + invMsg.writeInt16(it->second.amount); // Some still exist, and we have none left to remove, so // no need to run leave invy triggers. if (!amount) - return 0; + triggerLeaveInventory = false; + LOG_DEBUG("Slot id: " << it->first << " has now " + << it->second.amount << "item(s)."); } else { - mInvMsg.writeInt16(0); + invMsg.writeInt16(0); mPoss->inventory.erase(it); + LOG_DEBUG("Slot id: " << it->first << " is now empty."); } } else + { // We found an instance of them existing and have none left to // remove, so no need to run leave invy triggers. - return 0; + triggerLeaveInventory = false; + } } - if (force) - itemManager->getItem(itemId)->useTrigger(mClient, ITT_LEAVE_INVY); - // Rather inefficient, but still usable for now assuming small invy size. - // FIXME - return inv && !force ? remove(itemId, amount, true) : amount; + } + + if (triggerLeaveInventory) + itemManager->getItem(itemId)->useTrigger(mCharacter, ITT_LEAVE_INVY); + + if (invMsg.getLength() > 2) + gameHandler->sendTo(mCharacter, invMsg); + + return amount; } -unsigned int Inventory::move(unsigned int slot1, unsigned int slot2, unsigned int amount) +unsigned int Inventory::move(unsigned int slot1, unsigned int slot2, + unsigned int amount) { + LOG_DEBUG(amount << " item(s) requested to move from: " << slot1 << " to " + << slot2 << " for character: '" << mCharacter->getName() << "'."); + if (!amount || slot1 == slot2 || slot2 >= INVENTORY_SLOTS) return amount; - prepare(); + InventoryData::iterator it1 = mPoss->inventory.find(slot1), it2 = mPoss->inventory.find(slot2), inv_end = mPoss->inventory.end(); @@ -490,14 +348,7 @@ unsigned int Inventory::move(unsigned int slot1, unsigned int slot2, unsigned in if (it1 == inv_end) return amount; - EquipData::iterator it, it_end = mPoss->equipSlots.end(); - for (it = mPoss->equipSlots.begin(); - it != it_end; - ++it) - if (it->second == slot1) - // Bad things will happen when you can stack multiple equippable - // items in the same slot anyway. - it->second = slot2; + MessageOut invMsg(GPMSG_INVENTORY); unsigned int nb = std::min(amount, it1->second.amount); if (it2 == inv_end) @@ -511,63 +362,113 @@ unsigned int Inventory::move(unsigned int slot1, unsigned int slot2, unsigned in it1->second.amount -= nb; amount -= nb; - mInvMsg.writeInt16(slot1); // Slot + //Save the itemId in case of deletion of the iterator + unsigned int itemId = it1->second.itemId; + invMsg.writeInt16(slot1); // Slot if (it1->second.amount) { - mInvMsg.writeInt16(it1->second.itemId); // Item Id - mInvMsg.writeInt16(it1->second.amount); // Amount + invMsg.writeInt16(it1->second.itemId); // Item Id + invMsg.writeInt16(it1->second.amount); // Amount + LOG_DEBUG("Left " << amount << " item(s) id:" + << it1->second.itemId << " into slot: " << slot1); } else { - mInvMsg.writeInt16(0); + invMsg.writeInt16(0); mPoss->inventory.erase(it1); + LOG_DEBUG("Slot: " << slot1 << " is now empty."); } - mInvMsg.writeInt16(slot2); // Slot - mInvMsg.writeInt16(it1->second.itemId); // Item Id (same as slot 1) - mInvMsg.writeInt16(nb); // Amount + invMsg.writeInt16(slot2); // Slot + invMsg.writeInt16(itemId); // Item Id (same as slot 1) + invMsg.writeInt16(nb); // Amount + LOG_DEBUG("Slot: " << slot2 << " has now " << nb << " of item id: " + << itemId); } else { // Slot2 exists. if (it2->second.itemId != it1->second.itemId) - return amount; // Cannot stack items of a different type. - nb = std::min(itemManager->getItem(it1->second.itemId)->getMaxPerSlot() - - it2->second.amount, - nb); - - it1->second.amount -= nb; - it2->second.amount += nb; - amount -= nb; - - mInvMsg.writeInt16(slot1); // Slot - if (it1->second.amount) - { - mInvMsg.writeInt16(it1->second.itemId); // Item Id - mInvMsg.writeInt16(it1->second.amount); // Amount + { + // Swap items when they are of a different type + // and when all the amount of slot 1 is moving onto slot 2. + if (amount >= it1->second.amount) + { + unsigned int itemId = it1->second.itemId; + unsigned int amount = it1->second.amount; + it1->second.itemId = it2->second.itemId; + it1->second.amount = it2->second.amount; + it2->second.itemId = itemId; + it2->second.amount = amount; + + // Sending swapped slots. + invMsg.writeInt16(slot1); + invMsg.writeInt16(it1->second.itemId); + invMsg.writeInt16(it1->second.amount); + invMsg.writeInt16(slot2); + invMsg.writeInt16(it2->second.itemId); + invMsg.writeInt16(it2->second.amount); + LOG_DEBUG("Swapping items in slots " << slot1 + << " and " << slot2); + } + else + { + // Cannot partially stack items of a different type. + LOG_DEBUG("Cannot move " << amount << " item(s) from slot " + << slot1 << " to " << slot2); + return amount; + } } - else + else // Same item type on slot 2. { - mInvMsg.writeInt16(0); - mPoss->inventory.erase(it1); + // Number of items moving + nb = std::min(itemManager->getItem( + it1->second.itemId)->getMaxPerSlot() + - it2->second.amount, nb); + + // If nothing can move, we can abort + if (!nb) + return amount; + + it1->second.amount -= nb; + it2->second.amount += nb; + amount -= nb; + + invMsg.writeInt16(slot1); // Slot + if (it1->second.amount) + { + invMsg.writeInt16(it1->second.itemId); // Item Id + invMsg.writeInt16(it1->second.amount); // Amount + } + else + { + invMsg.writeInt16(0); + mPoss->inventory.erase(it1); + } + invMsg.writeInt16(slot2); // Slot + invMsg.writeInt16(it2->second.itemId); // Item Id + invMsg.writeInt16(it2->second.amount); // Amount } - mInvMsg.writeInt16(slot2); // Slot - mInvMsg.writeInt16(it2->second.itemId); // Item Id - mInvMsg.writeInt16(it2->second.amount); // Amount } + + if (invMsg.getLength() > 2) + gameHandler->sendTo(mCharacter, invMsg); + return amount; } unsigned int Inventory::removeFromSlot(unsigned int slot, unsigned int amount) { - prepare(); - InventoryData::iterator it = mPoss->inventory.find(slot); // When the given slot doesn't exist, we can't remove anything if (it == mPoss->inventory.end()) return amount; - // Check if an item of the same class exists elsewhere in the inventory + LOG_DEBUG("Inventory: Request Removal of " << amount << " item(s) in slot: " + << slot << " for character: '" << mCharacter->getName() << "'."); + + MessageOut invMsg(GPMSG_INVENTORY); + // Check if an item of the same id exists elsewhere in the inventory bool exists = false; for (InventoryData::const_iterator it2 = mPoss->inventory.begin(), it2_end = mPoss->inventory.end(); @@ -580,212 +481,319 @@ unsigned int Inventory::removeFromSlot(unsigned int slot, unsigned int amount) break; } } - if (!exists && it->second.itemId) { - if (ItemClass *ic = itemManager->getItem(it->second.itemId)) - ic->useTrigger(mClient, ITT_LEAVE_INVY); - } + + // We check whether it's the last slot where we can find that item id. + bool lastSlotOfItemRemaining = false; + if (!exists && it->second.itemId) + lastSlotOfItemRemaining = true; unsigned int sub = std::min(amount, it->second.amount); amount -= sub; it->second.amount -= sub; - mInvMsg.writeInt16(it->first); + invMsg.writeInt16(it->first); if (it->second.amount) { - mInvMsg.writeInt16(it->second.itemId); - mInvMsg.writeInt16(it->second.amount); + invMsg.writeInt16(it->second.itemId); + invMsg.writeInt16(it->second.amount); } else { - mInvMsg.writeInt16(0); + invMsg.writeInt16(0); + + // The item(s) was(were) the last one(s) in the inventory. + if (lastSlotOfItemRemaining) + { + if (ItemClass *ic = itemManager->getItem(it->second.itemId)) + ic->useTrigger(mCharacter, ITT_LEAVE_INVY); + } mPoss->inventory.erase(it); } + + if (invMsg.getLength() > 2) + gameHandler->sendTo(mCharacter, invMsg); + return amount; } -void Inventory::changeEquipment(unsigned int oldId, unsigned int newId) +void Inventory::updateEquipmentTrigger(unsigned int oldId, unsigned int newId) { if (!oldId && !newId) return; - changeEquipment(oldId ? itemManager->getItem(oldId) : 0, + updateEquipmentTrigger(oldId ? itemManager->getItem(oldId) : 0, newId ? itemManager->getItem(newId) : 0); } -void Inventory::changeEquipment(ItemClass *oldI, ItemClass *newI) +void Inventory::updateEquipmentTrigger(ItemClass *oldI, ItemClass *newI) { // This should only be called when applying changes, either directly // in non-delayed mode or when the changes are committed in delayed mode. if (!oldI && !newI) return; if (oldI && newI) - oldI->useTrigger(mClient, ITT_EQUIPCHG); + oldI->useTrigger(mCharacter, ITT_EQUIPCHG); else if (oldI) - oldI->useTrigger(mClient, ITT_UNEQUIP); + oldI->useTrigger(mCharacter, ITT_UNEQUIP); else if (newI) - newI->useTrigger(mClient, ITT_EQUIP); + newI->useTrigger(mCharacter, ITT_EQUIP); } -bool Inventory::equip(int slot, bool override) +unsigned int Inventory::getNewEquipItemInstance() { - if (mPoss->equipSlots.count(slot)) + unsigned int itemInstance = 1; + + for (EquipData::const_iterator it = mPoss->equipSlots.begin(), + it_end = mPoss->equipSlots.end(); it != it_end; ++it) + { + if (it->second.itemInstance == itemInstance) + { + ++itemInstance; + it = mPoss->equipSlots.begin(); + } + } + + return itemInstance; +} + +bool Inventory::checkEquipmentCapacity(unsigned int equipmentSlot, + unsigned int capacityRequested) +{ + int capacity = itemManager->getEquipSlotCapacity(equipmentSlot); + + // If the equipement slot doesn't exist, we can't equip on it. + if (capacity <= 0) + return false; + + // Test whether the slot capacity requested is reached. + for (EquipData::const_iterator it = mPoss->equipSlots.begin(), + it_end = mPoss->equipSlots.end(); it != it_end; ++it) + { + if (it->first == equipmentSlot) + { + if (it->second.itemInstance != 0) + { + capacity--; + } + } + } + + assert(capacity >= 0); // A should never happen case. + + if (capacity < (int)capacityRequested) return false; + + return true; +} + +bool Inventory::equip(int inventorySlot) +{ + // Test inventory slot existence InventoryData::iterator it; - if ((it = mPoss->inventory.find(slot)) == mPoss->inventory.end()) + if ((it = mPoss->inventory.find(inventorySlot)) == mPoss->inventory.end()) + { return false; - const ItemEquipsInfo &eq = itemManager->getItem(it->second.itemId)->getItemEquipData(); - if (eq.empty()) + LOG_DEBUG("No existing item in inventory at slot: " << inventorySlot); + } + + // Test the equipment scripted requirements + if (!testEquipScriptRequirements(it->second.itemId)) return false; - ItemEquipInfo const *ovd = 0; - // Iterate through all possible combinations of slots - for (ItemEquipsInfo::const_iterator it2 = eq.begin(), - it2_end = eq.end(); - it2 != it2_end; - ++it2) + + // Test the equip requirements. If none, it's not an equipable item. + const ItemEquipsInfo &equipInfoList = + itemManager->getItem(it->second.itemId)->getItemEquipData(); + if (equipInfoList.empty()) { - // Iterate through this combination of slots. - /* - * 0 = all ok, slots free - * 1 = possible if other items are unequipped first - * 2 = impossible, requires too many slots even with other equipment being removed - */ - int fail = 0; - ItemEquipInfo::const_iterator it3, it3_end; - for (it3 = it2->begin(), - it3_end = it2->end(); - it3 != it3_end; - ++it3) + LOG_DEBUG("No equip requirements for item id: " << it->second.itemId + << " at slot: " << inventorySlot); + return false; + } + + // Iterate through all slots requirements. + std::list<unsigned int> 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. + + // If not enough total space in the equipment slot is available, + // we cannot equip. + if (itemManager->getEquipSlotCapacity(it2->first) < it2->second) { - // it3 -> { slot id, number required } - unsigned int max = itemManager->getMaxSlotsFromId(it3->first), - used = mPoss->equipSlots.count(it3->first); - if (max - used >= it3->second) - continue; - else if (max >= it3->second) + LOG_DEBUG("Not enough equip capacity at slot: " << it2->first + << ", total available: " + << itemManager->getEquipSlotCapacity(it2->first) + << ", required: " << it2->second); + return false; + } + + // Test whether some item(s) is(are) to be unequipped first. + if (!checkEquipmentCapacity(it2->first, it2->second)) + { + // And test whether the unequip action would succeed first. + if (testUnequipScriptRequirements(it2->first) + && hasInventoryEnoughSpace(it2->first)) { - fail |= 1; - if (override) - continue; - else - break; + equipSlotsToUnequipFirst.push_back(it2->first); } else { - fail |= 2; - break; + // 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; } } - switch (fail) + } + + // Potential Pre-unequipment process + for (std::list<unsigned int>::const_iterator it3 = + equipSlotsToUnequipFirst.begin(); + it3 != equipSlotsToUnequipFirst.end(); ++it3) + { + if (!unequip(*it3)) { - case 0: - /* - * Clean fit. Equip and apply immediately. - */ - if (!mDelayed) { - mEqmMsg.writeInt16(slot); // Inventory slot - mEqmMsg.writeInt8(it2->size()); // Equip slot type count - } - for (it3 = it2->begin(), - it3_end = it2->end(); - it3 != it3_end; - ++it3) + // Something went wrong even when we tested the unequipment process. + LOG_WARN("Unable to unequip even when unequip was tested. " + "Character : " << mCharacter->getName() + << ", unequip slot: " << *it3); + return false; + } + } + + // Actually equip the item now that the requirements has met. + //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. + + // 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 = 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; + + // We've found an existing equip slot + if (it4->first == it2->first) { - if (!mDelayed) { - mEqmMsg.writeInt8(it3->first); // Equip slot - mEqmMsg.writeInt8(it3->second); // How many are used + // 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; } - /* - * This bit can be somewhat inefficient, but is far better for - * average case assuming most equip use one slot max for each - * type and infrequently (<1/3) two of each type max. - * If the reader cares, you're more than welcome to add - * compile time options optimising for other usage. - * For now, this is adequate assuming `normal' usage. - */ - for (unsigned int i = 0; i < it3->second; ++i) - mPoss->equipSlots.insert( - std::make_pair(it3->first, slot)); } - if (!mDelayed) - changeEquipment(0, it->second.itemId); - return true; - case 1: - /* - * Definitions earlier in the item file have precedence (even if it - * means requiring unequipping more), so no need to store more - * than the first. - */ - if (override && !ovd) - ovd = &*it2; // Iterator -> object -> pointer. - break; - case 2: - default: - /* - * Since slots are currently static (and I don't see any reason to - * change this right now), something probably went wrong. - * The logic to catch this is here rather than in the item manager - * just in case non-static equip slots do want to be - * implemented later. This would not be a trivial task, - * however. - */ - LOG_WARN("Inventory - item '" << it->second.itemId << - "' cannot be equipped, even by unequipping other items!"); - break; } - } - // We didn't find a clean equip. - if (ovd) - { - /* - * We did find an equip that works if we unequip other items, and we can override. - * Process unequip triggers for all items we have to unequip. - * Process equip triggers for new item. - * Attempt to reequip any equipment we had to remove, but disallowing override. - */ - // TODO - this would increase ease of use substatially, add as soon as - // there is time to do so. + // 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); + + // A should never happen case + assert(maxCapacity >= capacityUsed + capacityLeft); - return false; // Return true when this section is complete + while (capacityLeft) + { + EquipmentItem equipItem(it->second.itemId, itemInstance); + mPoss->equipSlots.insert( + std::make_pair<unsigned int, EquipmentItem> + (it2->first, equipItem)); + --capacityLeft; + } + } + + // Equip slot + equipMsg.writeInt16(it2->first); + // Capacity used + equipMsg.writeInt16(it2->second); } - /* - * We cannot equip, either because we could not find any valid equip process - * or because we found a dirty equip and weren't allowed to override. - */ - return false; -} -bool Inventory::unequip(EquipData::iterator it) -{ - return unequip(it->second, &it); + // New item trigger + updateEquipmentTrigger(0, it->second.itemId); + + // Remove item from inventory + removeFromSlot(inventorySlot, 1); + + if (equipMsg.getLength() > 2) + gameHandler->sendTo(mCharacter, equipMsg); + + return true; } -bool Inventory::unequip(unsigned int slot, EquipData::iterator *itp) +bool Inventory::unequip(unsigned int equipmentSlot) { - prepare(); - EquipData::iterator it = itp ? *itp : mPoss->equipSlots.begin(), - it_end = mPoss->equipSlots.end(); + // map of { itemInstance, itemId } + std::map<unsigned int, unsigned int> itemIdListToInventory; bool changed = false; - // Erase all equip entries that point to the given inventory slot - while (it != it_end) + MessageOut equipMsg(GPMSG_EQUIP); + equipMsg.writeInt16(0); // Item Id, useless in case of unequip. + equipMsg.writeInt16(1); // Number of slot types touched, + // 1 in case of unequip. + + // 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 == slot) + if (it->first == equipmentSlot && it->second.itemId != 0) { + if (!it->second.itemInstance) + continue; + + // Add the item to the inventory list if not already present there + std::map<unsigned int, unsigned int>::const_iterator it2 = + itemIdListToInventory.find(it->second.itemInstance); + if (it2 == itemIdListToInventory.end()) + { + itemIdListToInventory.insert( + std::make_pair<unsigned int, unsigned int> + (it->second.itemInstance, it->second.itemId)); + } + changed = true; - mPoss->equipSlots.erase(it++); - } - else - { - ++it; + it->second.itemId = 0; + it->second.itemInstance = 0; } } - if (changed && !mDelayed) + // Apply unequip trigger(s), and move the item(s) back to inventory. + for (std::map<unsigned int, unsigned int>::const_iterator it2 = + itemIdListToInventory.begin(), it2_end = itemIdListToInventory.end(); + it2 != it2_end; ++it2) + { + updateEquipmentTrigger(it2->second, 0); + insert(it2->second, 1); + } + + if (changed) { - changeEquipment(mPoss->inventory.at(slot).itemId, 0); - mEqmMsg.writeInt16(slot); - mEqmMsg.writeInt8(0); + equipMsg.writeInt16(equipmentSlot); + equipMsg.writeInt16(0); // Capacity used, set to 0 to unequip. } + if (equipMsg.getLength() > 2) + gameHandler->sendTo(mCharacter, equipMsg); + return changed; } diff --git a/src/game-server/inventory.h b/src/game-server/inventory.h index bd9da5c3..06f55e06 100644 --- a/src/game-server/inventory.h +++ b/src/game-server/inventory.h @@ -24,37 +24,6 @@ #include "game-server/character.h" #include "net/messageout.h" -/*enum -{ -// Equipment rules: -// 1 torso equipment - EQUIP_TORSO_SLOT = 0, -// 1 arms equipment - EQUIP_ARMS_SLOT = 1, -// 1 head equipment - EQUIP_HEAD_SLOT = 2, -// 1 legs equipment - EQUIP_LEGS_SLOT = 3, -// 1 feet equipment - EQUIP_FEET_SLOT = 4, -// 2 rings - EQUIP_RING1_SLOT = 5, - EQUIP_RING2_SLOT = 6, -// 1 necklace - EQUIP_NECKLACE_SLOT = 7, -// Fight: -// 2 one-handed weapons -// or 1 two-handed weapon -// or 1 one-handed weapon + 1 shield. - EQUIP_FIGHT1_SLOT = 8, - EQUIP_FIGHT2_SLOT = 9, -// Projectile: -// this item does not amount to one, it only indicates the chosen projectile. - EQUIP_PROJECTILE_SLOT = 10, - - EQUIP_CLIENT_INVENTORY = 32 -};*/ - class ItemClass; /** @@ -66,30 +35,15 @@ class Inventory /** * Creates a view on the possessions of a character. - * @param delayed If the changes need to be cancelable. */ - Inventory(Character *, bool delayed = false); + Inventory(Character *); /** * Commits delayed changes if applicable. * Sends the update message to the client. */ - ~Inventory(); - - /** - * Commits changes. - * Exclusive to delayed mode. - * @param doRestart Whether to prepare the inventory for more changes - after this. If you are unsure, it is safe (though not - terribly efficient) to leave this as true. - */ - void commit(bool doRestart = true); - - /** - * Cancels changes. - * Exclusive to delayed mode. - */ - void cancel(); + ~Inventory() + {} /** * Sends complete inventory status to the client. @@ -100,29 +54,21 @@ class Inventory * Ensures the inventory is sane and apply equipment modifiers. * Should be run only once and the very first time. */ - void initialise(); + void initialize(); /** * Equips item from given inventory slot. - * @param slot The slot in which the target item is in. - * @param override Whether this item can unequip other items to equip - * itself. If true, items that are unequipped will be - * attempted to be reequipped, but with override disabled. + * @param inventorySlot The slot in which the target item is in. * @returns whether the item could be equipped. */ - bool equip(int slot, bool override = true); + bool equip(int inventorySlot); /** * Unequips item from given equipment slot. - * @param it Starting iterator. When the only parameter, also extracts - * slot number from it. - * Used so that when we already have an iterator to the first - * occurence from a previous operation we can start from - * there. + * @param equipmentSlot Where to remove item(s). * @returns Whether it was unequipped. */ - bool unequip(EquipData::iterator it); - bool unequip(unsigned int slot, EquipData::iterator *itp = 0); + bool unequip(unsigned int equipmentSlot); /** * Inserts some items into the inventory. @@ -132,16 +78,16 @@ class Inventory /** * Removes some items from inventory. - * @param force If set to true, also remove any equipment encountered * @return number of items not removed. */ - unsigned int remove(unsigned int itemId, unsigned int amount, bool force = false); + unsigned int remove(unsigned int itemId, unsigned int amount); /** * Moves some items from the first slot to the second one. * @returns number of items not moved. */ - unsigned int move(unsigned int slot1, unsigned int slot2, unsigned int amount); + unsigned int move(unsigned int slot1, unsigned int slot2, + unsigned int amount); /** * Removes some items from inventory. @@ -160,51 +106,61 @@ class Inventory unsigned int getItem(unsigned int slot) const; private: - /** - * Make sure that changes are being done on a copy, not directly. - * No effect when not in delayed mode. + * Tell whether the equipment slot has enough room in an equipment slot. + * @param equipmentSlot the slot in equipement to check. + * @param capacityRequested the capacity needed. */ - void prepare(); + bool checkEquipmentCapacity(unsigned int equipmentSlot, + unsigned int capacityRequested); /** - * Starts a new notification message. + * Test whether the inventory has enough space to welcome + * the willing-to-be equipment slot. + * @todo */ - void restart(); + bool hasInventoryEnoughSpace(unsigned int equipmentSlot) + { return false; } + /** + * Test the items unequipment requirements. + * This is especially useful for scripted equipment. + * @todo + */ + bool testUnequipScriptRequirements(unsigned int equipementSlot) + { return true; } /** - * Check the inventory is within the slot limit and capacity. - * Forcibly delete items from the end if it is not. - * @todo Drop items instead? + * Test the items equipment for scripted requirements. + * @todo */ - void checkSize(); + bool testEquipScriptRequirements(unsigned int itemId) + { return true; } /** - * Helper function for equip() when computing changes to equipment - * When newCount is 0, the item is being unequipped. + * Return an equip item instance id unique to the item used, + * per character. + * This is used to differenciate some items that can be equipped + * multiple times, like one-handed weapons for instance. */ - // inventory slot -> {equip slots} - typedef std::multimap<unsigned int, unsigned short> IdSlotMap; - void equip_sub(unsigned int newCount, IdSlotMap::const_iterator &it); + unsigned int getNewEquipItemInstance(); /** - * Changes equipment and adjusts character attributes. + * Check the inventory is within the slot limit and capacity. + * Forcibly delete items from the end if it is not. + * @todo Drop items instead? */ - void changeEquipment(unsigned int oldId, unsigned int itemId); - void changeEquipment(ItemClass *oldI, ItemClass *newI); + void checkInventorySize(); - Possessions *mPoss; /**< Pointer to the modified possessions. */ /** - * Update message containing inventory changes. - * Note that in sendFull(), this is reused to send all full changes - * (for both inventory and equipment) + * Apply equipment triggers. */ - MessageOut mInvMsg; - MessageOut mEqmMsg; /**< Update message containing equipment changes */ - Character *mClient; /**< Character to notify. */ - bool mDelayed; /**< Delayed changes. */ -}; + void updateEquipmentTrigger(unsigned int oldId, unsigned int itemId); + void updateEquipmentTrigger(ItemClass *oldI, ItemClass *newI); + Possessions *mPoss; /**< Pointer to the modified possessions. */ + + Character *mCharacter; /**< Character to notify. */ +}; #endif diff --git a/src/game-server/item.h b/src/game-server/item.h index 1c7639c5..caef386a 100644 --- a/src/game-server/item.h +++ b/src/game-server/item.h @@ -28,7 +28,9 @@ class Being; class Script; -typedef std::list< std::pair< unsigned int, unsigned int> > ItemEquipInfo; +// 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; /** diff --git a/src/game-server/itemmanager.cpp b/src/game-server/itemmanager.cpp index d21c791c..074b8343 100644 --- a/src/game-server/itemmanager.cpp +++ b/src/game-server/itemmanager.cpp @@ -51,6 +51,14 @@ void ItemManager::deinitialize() { delete i->second; } + + for (std::map< unsigned int, EquipSlotInfo* >::iterator it = + mEquipSlotsInfo.begin(), it_end = mEquipSlotsInfo.end(); it != it_end; + ++it) + { + delete it->second; + } + mItemClasses.clear(); mItemClassesByName.clear(); } @@ -71,52 +79,22 @@ unsigned int ItemManager::getDatabaseVersion() const return mItemDatabaseVersion; } -const std::string &ItemManager::getEquipNameFromId(unsigned int id) const -{ - return mEquipSlots.at(id).first; -} - -unsigned int ItemManager::getEquipIdFromName(const std::string &name) const -{ - for (unsigned int i = 0; i < mEquipSlots.size(); ++i) - if (name == mEquipSlots.at(i).first) - return i; - LOG_WARN("Item Manager: attempt to find equip id from name \"" << - name << "\" not found, defaulting to 0!"); - return 0; -} - -unsigned int ItemManager::getMaxSlotsFromId(unsigned int id) const +unsigned int ItemManager::getEquipSlotIdFromName(const std::string &name) const { - return mEquipSlots.at(id).second; + EquipSlotInfo *slotInfo = mNamedEquipSlotsInfo.find(name); + return slotInfo ? slotInfo->slotId : 0; } -unsigned int ItemManager::getVisibleSlotCount() const +unsigned int ItemManager::getEquipSlotCapacity(unsigned int id) const { - if (!mVisibleEquipSlotCount) - { - for (VisibleEquipSlots::const_iterator it = mVisibleEquipSlots.begin(), - it_end = mVisibleEquipSlots.end(); - it != it_end; - ++it) - { - mVisibleEquipSlotCount += mEquipSlots.at(*it).second; - } - } - return mVisibleEquipSlotCount; + EquipSlotsInfo::const_iterator i = mEquipSlotsInfo.find(id); + return i != mEquipSlotsInfo.end() ? i->second->slotCapacity : 0; } bool ItemManager::isEquipSlotVisible(unsigned int id) const { - for (VisibleEquipSlots::const_iterator it = mVisibleEquipSlots.begin(), - it_end = mVisibleEquipSlots.end(); - it != it_end; - ++it) - { - if (*it == id) - return true; - } - return false; + EquipSlotsInfo::const_iterator i = mEquipSlotsInfo.find(id); + return i != mEquipSlotsInfo.end() ? i->second->visibleSlot : false; } void ItemManager::readEquipSlotsFile() @@ -133,42 +111,61 @@ void ItemManager::readEquipSlotsFile() LOG_INFO("Loading equip slots: " << mEquipSlotsFile); - unsigned totalCount = 0; + unsigned totalCapacity = 0; unsigned slotCount = 0; - unsigned visibleSlotCount = 0; + mVisibleEquipSlotCount = 0; for_each_xml_child_node(node, rootNode) { if (xmlStrEqual(node->name, BAD_CAST "slot")) { + const int slotId = XML::getProperty(node, "id", 0); const std::string name = XML::getProperty(node, "name", std::string()); - const int count = XML::getProperty(node, "count", 0); + const int capacity = XML::getProperty(node, "capacity", 0); - if (name.empty() || count <= 0) + if (slotId <= 0 || name.empty() || capacity <= 0) { - LOG_WARN("Item Manager: equip slot has no name or zero count"); + LOG_WARN("Item Manager: equip slot " << slotId + << ": (" << name << ") has no name or zero count. " + "The slot has been ignored."); + continue; } - else + + bool visible = XML::getBoolProperty(node, "visible", false); + if (visible) { - bool visible = XML::getProperty(node, "visible", "false") != "false"; - if (visible) + if (++mVisibleEquipSlotCount > 7) { - mVisibleEquipSlots.push_back(mEquipSlots.size()); - if (++visibleSlotCount > 7) - LOG_WARN("Item Manager: More than 7 visible equip slot!" - "This will not work with current netcode!"); + LOG_WARN("Item Manager: More than 7 visible equip slot!" + "This will not work with current netcode!"); } - mEquipSlots.push_back(std::pair<std::string, unsigned int> - (name, count)); - totalCount += count; - ++slotCount; } + + EquipSlotsInfo::iterator i = mEquipSlotsInfo.find(slotId); + + if (i != mEquipSlotsInfo.end()) + { + LOG_WARN("Item Manager: Ignoring duplicate definition " + "of equip slot '" << slotId << "'!"); + continue; + } + + LOG_DEBUG("Adding equip slot, id: " << slotId << ", name: " << name + << ", capacity: " << capacity << ", visible? " << visible); + EquipSlotInfo *equipSlotInfo = + new EquipSlotInfo(slotId, name, capacity, visible); + mEquipSlotsInfo.insert( + std::make_pair<unsigned int, EquipSlotInfo*>(slotId, equipSlotInfo)); + mNamedEquipSlotsInfo.insert(name, equipSlotInfo); + + totalCapacity += capacity; + ++slotCount; } } LOG_INFO("Loaded '" << slotCount << "' slot types with '" - << totalCount << "' slots."); + << totalCapacity << "' slots."); } void ItemManager::readItemsFile() @@ -263,7 +260,6 @@ void ItemManager::readItemNode(xmlNodePtr itemNode) void ItemManager::readEquipNode(xmlNodePtr equipNode, ItemClass *item) { - ItemEquipInfo req; for_each_xml_child_node(subNode, equipNode) { if (xmlStrEqual(subNode->name, BAD_CAST "slot")) @@ -274,17 +270,26 @@ void ItemManager::readEquipNode(xmlNodePtr equipNode, ItemClass *item) LOG_WARN("Item Manager: empty equip slot definition!"); continue; } - req.push_back(std::make_pair(getEquipIdFromName(slot), + 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))); + } + else + { + // When its name is given + item->mEquip.push_back(std::make_pair(getEquipSlotIdFromName(slot), XML::getProperty(subNode, "required", 1))); + } } } - if (req.empty()) + if (item->mEquip.empty()) { LOG_WARN("Item Manager: empty equip requirement " "definition for item " << item->getDatabaseID() << "!"); return; } - item->mEquip.push_back(req); } void ItemManager::readEffectNode(xmlNodePtr effectNode, ItemClass *item) diff --git a/src/game-server/itemmanager.h b/src/game-server/itemmanager.h index c310df44..9eee326e 100644 --- a/src/game-server/itemmanager.h +++ b/src/game-server/itemmanager.h @@ -30,6 +30,24 @@ class ItemClass; +struct EquipSlotInfo +{ + EquipSlotInfo(): + slotId(0), slotCapacity(0), visibleSlot(false) + {} + + EquipSlotInfo(unsigned int id, const std::string &name, + unsigned int capacity, bool visible): + slotId(id), slotName(name), slotCapacity(capacity), visibleSlot(visible) + {} + + unsigned int slotId; + std::string slotName; + unsigned int slotCapacity; + bool visibleSlot; +}; + + class ItemManager { public: @@ -73,13 +91,12 @@ class ItemManager */ unsigned int getDatabaseVersion() const; - const std::string &getEquipNameFromId(unsigned int id) const; - - unsigned int getEquipIdFromName(const std::string &name) const; + unsigned int getEquipSlotIdFromName(const std::string &name) const; - unsigned int getMaxSlotsFromId(unsigned int id) const; + unsigned int getEquipSlotCapacity(unsigned int id) const; - unsigned int getVisibleSlotCount() const; + unsigned int getVisibleEquipSlotCount() const + { return mVisibleEquipSlotCount; } bool isEquipSlotVisible(unsigned int id) const; @@ -94,19 +111,22 @@ class ItemManager void readEffectNode(xmlNodePtr effectNode, ItemClass *item); typedef std::map< int, ItemClass * > ItemClasses; - // Map a string (name of slot) with (str-id, max-per-equip-slot) - typedef std::vector< std::pair< std::string, unsigned int > > EquipSlots; + ItemClasses mItemClasses; /**< Item reference */ + utils::NameMap<ItemClass*> mItemClassesByName; + + // Map an equip slot id with the equip slot info. + typedef std::map< unsigned int, EquipSlotInfo* > EquipSlotsInfo; // Reference to the vector position of equipSlots typedef std::vector< unsigned int > VisibleEquipSlots; - ItemClasses mItemClasses; /**< Item reference */ - utils::NameMap<ItemClass*> mItemClassesByName; - EquipSlots mEquipSlots; - VisibleEquipSlots mVisibleEquipSlots; + EquipSlotsInfo mEquipSlotsInfo; + // Map a string (name of slot) with (str-id, max-per-equip-slot) + // We only keep a pointer to it: The id map will take care of deletion. + utils::NameMap<EquipSlotInfo* > mNamedEquipSlotsInfo; std::string mItemsFile; std::string mEquipSlotsFile; - mutable unsigned int mVisibleEquipSlotCount; // Cache + unsigned int mVisibleEquipSlotCount; // Cache /** Version of the loaded items database file.*/ unsigned int mItemDatabaseVersion; diff --git a/src/game-server/state.cpp b/src/game-server/state.cpp index 03d4b71c..d8f9b183 100644 --- a/src/game-server/state.cpp +++ b/src/game-server/state.cpp @@ -108,8 +108,8 @@ static void updateMap(MapComposite *map) */ static void serializeLooks(Character *ch, MessageOut &msg, bool full) { - const Possessions &poss = ch->getPossessions(); - unsigned int nb_slots = itemManager->getVisibleSlotCount(); + const EquipData &equipData = ch->getPossessions().getEquipment(); + unsigned int nb_slots = itemManager->getVisibleEquipSlotCount(); // Bitmask describing the changed entries. int changed = (1 << nb_slots) - 1; @@ -125,8 +125,8 @@ static void serializeLooks(Character *ch, MessageOut &msg, bool full) // Partially build both kinds of packet, to get their sizes. unsigned int mask_full = 0, mask_diff = 0; unsigned int nb_full = 0, nb_diff = 0; - std::map<unsigned int, unsigned int>::const_iterator it = - poss.equipSlots.begin(); + std::map<unsigned int, EquipmentItem>::const_iterator it = + equipData.begin(); for (unsigned int i = 0; i < nb_slots; ++i) { if (changed & (1 << i)) @@ -135,7 +135,7 @@ static void serializeLooks(Character *ch, MessageOut &msg, bool full) ++nb_diff; mask_diff |= 1 << i; } - if (it == poss.equipSlots.end() || it->first > i) continue; + if (it == equipData.end() || it->first > i) continue; ItemClass *eq; items[i] = it->first && (eq = itemManager->getItem(it->first)) ? eq->getSpriteID() : 0; diff --git a/src/game-server/trade.cpp b/src/game-server/trade.cpp index 51509307..e1779d2f 100644 --- a/src/game-server/trade.cpp +++ b/src/game-server/trade.cpp @@ -130,7 +130,7 @@ void Trade::agree(Character *c) // Check if both player has the objects in their inventories // and enouth money, then swap them. - Inventory v1(mChar1, true), v2(mChar2, true); + Inventory v1(mChar1), v2(mChar2); if (mChar1->getAttribute(mCurrencyId) >= mMoney1 - mMoney2 && mChar2->getAttribute(mCurrencyId) >= mMoney2 - mMoney1 && perform(mItems1, v1, v2) && @@ -143,8 +143,6 @@ void Trade::agree(Character *c) } else { - v1.cancel(); - v2.cancel(); cancel(); return; } |