From 282ca19e33b79f4468ce6402406ddb1397f2d115 Mon Sep 17 00:00:00 2001 From: Guillaume Melquiond Date: Wed, 3 Jan 2007 20:54:03 +0000 Subject: Starting to work on adding items to the map. Nothing concrete for now, just a few simplifications. Fixed file end-of-lines along the way. --- ChangeLog | 11 ++ src/Makefile.am | 4 +- src/game-server/being.cpp | 20 ++-- src/game-server/being.hpp | 11 -- src/game-server/gamehandler.cpp | 8 +- src/game-server/inventory.cpp | 198 +++++++++++++++------------------ src/game-server/inventory.hpp | 103 +++++++---------- src/game-server/item.cpp | 48 ++++++++ src/game-server/item.hpp | 232 +++++++++++++++++++++++++++++++++++++++ src/game-server/itemmanager.cpp | 16 ++- src/game-server/itemmanager.hpp | 43 +------- src/game-server/mapcomposite.cpp | 2 +- src/game-server/mapcomposite.hpp | 6 +- src/game-server/player.cpp | 19 ++-- src/game-server/player.hpp | 23 ++-- src/item.cpp | 48 -------- src/item.h | 206 ---------------------------------- 17 files changed, 466 insertions(+), 532 deletions(-) create mode 100644 src/game-server/item.cpp create mode 100644 src/game-server/item.hpp delete mode 100644 src/item.cpp delete mode 100644 src/item.h diff --git a/ChangeLog b/ChangeLog index d38516eb..45f40698 100644 --- a/ChangeLog +++ b/ChangeLog @@ -31,6 +31,17 @@ * src/defines.h: Fixed enumerations. * src/games-server/gamehandler.cpp, src/game-server/player.cpp: Commented in inventory code back. + * src/item.h, src/item.cpp: Moved to src/game-server directory and + changed header extension to hpp. + * src/game-server/itemmanager.hpp, src/game-server/itemmanager.cpp, + src/game-server/inventory.hpp, src/game-server/inventory.cpp, + src/game-server/player.hpp, src/game-server/gamehandler.cpp, + src/game-server/player.cpp: Simplified Inventory interface a bit. + Renamed Item to ItemClass to reflect its role. Added a Item class for + Objects lying on the world map. + * src/game-server/being.hpp: Removed counted pointers. + * src/game-server/mapcomposite.hpp, src/game-server/mapcomposite.cpp, + src/game-server/being.cpp: Fixed end of lines. 2007-01-02 Guillaume Melquiond diff --git a/src/Makefile.am b/src/Makefile.am index 255b25e7..8f6f2c56 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -68,8 +68,6 @@ tmwserv_game_SOURCES = \ controller.h \ controller.cpp \ defines.h \ - item.h \ - item.cpp \ playerdata.hpp \ point.h \ resourcemanager.h \ @@ -84,6 +82,8 @@ tmwserv_game_SOURCES = \ game-server/gamehandler.cpp \ game-server/inventory.hpp \ game-server/inventory.cpp \ + game-server/item.hpp \ + game-server/item.cpp \ game-server/itemmanager.hpp \ game-server/itemmanager.cpp \ game-server/map.hpp \ diff --git a/src/game-server/being.cpp b/src/game-server/being.cpp index 64986c26..da8f1f2c 100644 --- a/src/game-server/being.cpp +++ b/src/game-server/being.cpp @@ -24,16 +24,16 @@ #include "game-server/mapcomposite.hpp" #include "utils/logger.h" -void Being::damage(Damage damage) -{ - int HPloss; - - HPloss = damage; // TODO: Implement complex damage calculation here - - mHitpoints -= HPloss; - mHitsTaken.push_back(HPloss); - LOG_DEBUG("Being " << getPublicID() << " got hit", 0); -} +void Being::damage(Damage damage) +{ + int HPloss; + + HPloss = damage; // TODO: Implement complex damage calculation here + + mHitpoints -= HPloss; + mHitsTaken.push_back(HPloss); + LOG_DEBUG("Being " << getPublicID() << " got hit", 0); +} void Being::performAttack(MapComposite *map) { diff --git a/src/game-server/being.hpp b/src/game-server/being.hpp index 32b30b1e..327783ce 100644 --- a/src/game-server/being.hpp +++ b/src/game-server/being.hpp @@ -29,7 +29,6 @@ #include "defines.h" #include "game-server/object.hpp" -#include "utils/countedptr.h" class MapComposite; @@ -201,14 +200,4 @@ class Being : public MovingObject Hits mHitsTaken; /**< List of punches taken since last update */ }; -/** - * Type definition for a smart pointer to Being. - */ -typedef utils::CountedPtr BeingPtr; - -/** - * Type definition for a list of Beings. - */ -typedef std::vector Beings; - #endif // _TMWSERV_BEING_H_ diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp index f7a3905a..08e043f1 100644 --- a/src/game-server/gamehandler.cpp +++ b/src/game-server/gamehandler.cpp @@ -234,19 +234,19 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) case PGMSG_PICKUP: { // add item to inventory (this is too simplistic atm) - unsigned int itemId = message.readLong(); + int itemId = message.readLong(); // remove the item from world map // send feedback - computer.character->addItem(itemId); + computer.character->insertItem(itemId, 1); result.writeShort(GPMSG_PICKUP_RESPONSE); result.writeByte(ERRMSG_OK); } break; case PGMSG_USE_ITEM: { - unsigned int itemId = message.readLong(); + int itemId = message.readLong(); result.writeShort(GPMSG_USE_RESPONSE); @@ -273,7 +273,7 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message) case PGMSG_EQUIP: { message.readLong(); // ItemId: Not useful, the inventory knows it - char slot = message.readByte(); + int slot = message.readByte(); result.writeShort(GPMSG_EQUIP_RESPONSE); result.writeByte(computer.character->equip(slot) ? diff --git a/src/game-server/inventory.cpp b/src/game-server/inventory.cpp index b3b8a254..c5913e15 100644 --- a/src/game-server/inventory.cpp +++ b/src/game-server/inventory.cpp @@ -21,38 +21,37 @@ * $Id$ */ +#include + #include "game-server/inventory.hpp" #include "game-server/itemmanager.hpp" // --------- // Items // --------- -unsigned char -Inventory::getInventoryFreeSlot() +int Inventory::getInventoryFreeSlot() { for (int a = 0; a < MAX_ITEMS_IN_INVENTORY; a++) { - if (itemList.at(a).amount == 0) + if (itemList[a].amount == 0) return a; } return INVENTORY_FULL; } -unsigned char -Inventory::getSlotFromId(const unsigned int itemId) +int Inventory::getSlotFromId(int itemId) { for (int a = 0; a < MAX_ITEMS_IN_INVENTORY; a++) { - if (itemList.at(a).itemId == itemId) + if (itemList[a].itemId == itemId) return a; } return INVENTORY_FULL; } -bool -Inventory::hasItem(unsigned int itemId, - bool searchInInventory, - bool searchInEquipment) +bool Inventory::hasItem(int itemId, + bool searchInInventory, + bool searchInEquipment) { bool hasItem = false; // Search in inventory @@ -82,44 +81,51 @@ Inventory::hasItem(unsigned int itemId, return hasItem; } -short -Inventory::addItem(unsigned int itemId, unsigned char amount) +int Inventory::insertItem(int itemId, int amount) { + if (amount <= 0) + { + return 0; + } + // We get the max number of item we can have in the same slot // for the given item. - unsigned char maxPerSlot = itemManager->getMaxPerSlot(itemId); + int maxPerSlot = itemManager->getItem(itemId)->getMaxPerSlot(); // 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; + int amountToAdd = amount; // Parsing inventory - unsigned char currentAmountInSlot = 0; - std::vector::iterator iter = itemList.begin(); - for (iter = itemList.begin(); iter != itemList.end(); ++iter) + for (std::vector< StoredItem >::iterator iter = itemList.begin(), + iter_end = itemList.end(); iter != iter_end; ++iter) { - currentAmountInSlot = iter->amount; + int currentAmountInSlot = iter->amount; // If a slot has got place for some more of such an item. if (iter->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) + int possibleAmount = maxPerSlot - currentAmountInSlot; + assert(possibleAmount > 0); + if (possibleAmount < amountToAdd) { - iter->amount += (maxPerSlot - currentAmountInSlot); - amountToAdd -= (maxPerSlot - currentAmountInSlot); + iter->amount += possibleAmount; + amountToAdd -= possibleAmount; } else // there is enough to add everything. { iter->amount += amountToAdd; amountToAdd = 0; // Ok! + break; } } // Or if there is an empty slot. else if (iter->amount == 0) { // We add the item in the new slot (setting also the Id.) + iter->itemId = itemId; if (maxPerSlot < amountToAdd) { iter->amount = maxPerSlot; @@ -129,28 +135,31 @@ Inventory::addItem(unsigned int itemId, unsigned char amount) { iter->amount += amountToAdd; amountToAdd = 0; // Ok! + break; } - iter->itemId = itemId; } } - return (short)(amount - amountToAdd); + return amount - amountToAdd; } -short -Inventory::removeItem(unsigned int itemId, unsigned char amount) +int Inventory::removeItemById(int itemId, int amount) { + if (amount <= 0) + { + return 0; + } + // We'll remove items in slots with the item type // until it's all done or until the inventory will be all parsed. // Searching for items with the same Id in the inventory - unsigned char amountToRemove = amount; + int amountToRemove = amount; // Parsing inventory - unsigned char currentAmountInSlot = 0; - std::vector::iterator iter = itemList.begin(); - for (iter = itemList.begin(); iter != itemList.end(); ++iter) + for (std::vector< StoredItem >::iterator iter = itemList.begin(), + iter_end = itemList.end(); iter != iter_end; ++iter) { - currentAmountInSlot = iter->amount; + int currentAmountInSlot = iter->amount; // If a slot has got such an item, remove it if (iter->itemId == itemId && currentAmountInSlot > 0) { @@ -166,76 +175,68 @@ Inventory::removeItem(unsigned int itemId, unsigned char amount) { iter->amount -= amountToRemove; amountToRemove = 0; // Ok! + break; } } } - return (short)(amount - amountToRemove); + return amount - amountToRemove; } -short -Inventory::removeItem(unsigned char slot, unsigned char amount) +int Inventory::removeItemBySlot(int slot, int amount) { - if (itemList.at(slot).amount < amount) + if (amount <= 0) { - unsigned char value = itemList.at(slot).amount; - itemList.at(slot).amount = 0; - return (short)value; + return 0; + } + + if (itemList[slot].amount < amount) + { + int value = itemList[slot].amount; + itemList[slot].amount = 0; + return value; } else { - itemList.at(slot).amount -= amount; + itemList[slot].amount -= amount; return amount; } } -bool -Inventory::use(unsigned char slot, BeingPtr itemUser) -{ - return false; // TODO -} - -bool -Inventory::use(unsigned int itemId, BeingPtr itemUser) -{ - return false; // TODO -} - // --------- // Equipment // --------- -unsigned char -Inventory::equipItem(unsigned int itemId) +int Inventory::equipItem(int itemId) { // First, we look for the item in the player's inventory. if (!hasItem(itemId, true, false)) return false; - unsigned char availableSlots = 0, firstSlot = 0, secondSlot = 0; + int availableSlots = 0, firstSlot = 0, secondSlot = 0; - unsigned short itemType = itemManager->getItemType(itemId); + int itemType = itemManager->getItem(itemId)->getType(); switch (itemType) { case ITEM_EQUIPMENT_TWO_HANDS_WEAPON: // Special case 1, the two one-handed weapons are to be placed back // in the inventory, if there are any. - if (equippedItemList.at(EQUIP_FIGHT1_SLOT).itemId > 0) + if (equippedItemList[EQUIP_FIGHT1_SLOT].itemId > 0) { // Slot 1 full // old two-handed weapon case: - if (equippedItemList.at(EQUIP_FIGHT1_SLOT).itemType + if (equippedItemList[EQUIP_FIGHT1_SLOT].itemType == ITEM_EQUIPMENT_TWO_HANDS_WEAPON) { - equippedItemList.at(EQUIP_FIGHT2_SLOT).itemId = 0; - equippedItemList.at(EQUIP_FIGHT2_SLOT).itemType = 0; + equippedItemList[EQUIP_FIGHT2_SLOT].itemId = 0; + equippedItemList[EQUIP_FIGHT2_SLOT].itemType = 0; } - if (unequipItem_(equippedItemList.at(EQUIP_FIGHT1_SLOT).itemId, - EQUIP_FIGHT1_SLOT) == INVENTORY_FULL) + if (unequipItem_(equippedItemList[EQUIP_FIGHT1_SLOT].itemId, + EQUIP_FIGHT1_SLOT) == INVENTORY_FULL) return INVENTORY_FULL; } - if (equippedItemList.at(EQUIP_FIGHT2_SLOT).itemId > 0) + if (equippedItemList[EQUIP_FIGHT2_SLOT].itemId > 0) { // Slot 2 full - if (unequipItem_(equippedItemList.at(EQUIP_FIGHT2_SLOT).itemId, - EQUIP_FIGHT2_SLOT) == INVENTORY_FULL) + if (unequipItem_(equippedItemList[EQUIP_FIGHT2_SLOT].itemId, + EQUIP_FIGHT2_SLOT) == INVENTORY_FULL) return INVENTORY_FULL; } // Only the slot 1 needs to be updated. @@ -248,19 +249,18 @@ Inventory::equipItem(unsigned int itemId) // Case 1: Reloading if (equippedProjectiles.itemId == itemId) { - equippedProjectiles.amount += removeItem(itemId, - (255 - equippedProjectiles.amount)); + equippedProjectiles.amount += + removeItemById(itemId, 255 - equippedProjectiles.amount); return EQUIP_PROJECTILES_SLOT; } else // Case 2: Changing projectiles. { - short added; - added = addItem(equippedProjectiles.itemId, - equippedProjectiles.amount); + int added = insertItem(equippedProjectiles.itemId, + equippedProjectiles.amount); if (added == equippedProjectiles.amount) { // Ok, we can equip equippedProjectiles.itemId = itemId; - equippedProjectiles.amount = removeItem(itemId, 255); + equippedProjectiles.amount = removeItemById(itemId, 255); return EQUIP_PROJECTILES_SLOT; } else // Some were unequipped. @@ -316,10 +316,10 @@ Inventory::equipItem(unsigned int itemId) switch (availableSlots) { case 1: - if (equippedItemList.at(firstSlot).itemId > 0) + if (equippedItemList[firstSlot].itemId > 0) { - if (unequipItem_(equippedItemList.at(firstSlot).itemId, - firstSlot) != INVENTORY_FULL) + if (unequipItem_(equippedItemList[firstSlot].itemId, + firstSlot) != INVENTORY_FULL) return equipItem_(itemId, itemType, firstSlot); else return INVENTORY_FULL; @@ -331,25 +331,25 @@ Inventory::equipItem(unsigned int itemId) break; case 2: - if (equippedItemList.at(firstSlot).itemId > 0) + if (equippedItemList[firstSlot].itemId > 0) { // If old weapon is two-handed one, we can unequip // the first slot only, and clean the second. - if (equippedItemList.at(firstSlot).itemId == + if (equippedItemList[firstSlot].itemId == ITEM_EQUIPMENT_TWO_HANDS_WEAPON) { - if (unequipItem_(equippedItemList.at(firstSlot).itemId, + if (unequipItem_(equippedItemList[firstSlot].itemId, firstSlot) != INVENTORY_FULL) return equipItem_(itemId, itemType, firstSlot); else return INVENTORY_FULL; } - if (equippedItemList.at(secondSlot).itemId > 0) + if (equippedItemList[secondSlot].itemId > 0) { // Both slots are full, // we remove the first one to equip - if (unequipItem_(equippedItemList.at(firstSlot).itemId, - firstSlot) != INVENTORY_FULL) + if (unequipItem_(equippedItemList[firstSlot].itemId, + firstSlot) != INVENTORY_FULL) return equipItem_(itemId, itemType, firstSlot); else return INVENTORY_FULL; @@ -370,48 +370,26 @@ Inventory::equipItem(unsigned int itemId) } } -bool -Inventory::equipItem(unsigned char inventorySlot, unsigned char equipmentSlot) -{ - return false; // TODO -} - -bool -Inventory::unequipItem(unsigned int itemId) -{ - return false; // TODO -} - -bool -Inventory::unequipItem(unsigned char inventorySlot, - unsigned char equipmentSlot) -{ - return false; // TODO -} - -unsigned char -Inventory::equipItem_(unsigned int itemId, - unsigned int itemType, - unsigned char equipmentSlot) +int Inventory::equipItem_(int itemId, + int itemType, + int equipmentSlot) { - if (removeItem(itemId, 1) == 1) + if (removeItemById(itemId, 1) == 1) { - equippedItemList.at(equipmentSlot).itemId = itemId; - equippedItemList.at(equipmentSlot).itemType = itemType; + equippedItemList[equipmentSlot].itemId = itemId; + equippedItemList[equipmentSlot].itemType = itemType; return equipmentSlot; } else return NO_ITEM_TO_EQUIP; } -unsigned char -Inventory::unequipItem_(unsigned int itemId, - unsigned char equipmentSlot) +int Inventory::unequipItem_(int itemId, int equipmentSlot) { - if (addItem(itemId, 1) == 1) + if (insertItem(itemId, 1) == 1) { - equippedItemList.at(equipmentSlot).itemId = 0; - equippedItemList.at(equipmentSlot).itemType = 0; + equippedItemList[equipmentSlot].itemId = 0; + equippedItemList[equipmentSlot].itemType = 0; return equipmentSlot; } else diff --git a/src/game-server/inventory.hpp b/src/game-server/inventory.hpp index 61bc5818..1fa66373 100644 --- a/src/game-server/inventory.hpp +++ b/src/game-server/inventory.hpp @@ -70,7 +70,7 @@ enum */ struct StoredItem { - unsigned int itemId; + int itemId; unsigned char amount; }; @@ -79,7 +79,7 @@ struct StoredItem */ struct EquippedItem { - unsigned int itemId; + int itemId; short itemType; }; @@ -95,61 +95,50 @@ class Inventory * Convenience function to get slot from ItemId. * If more than one occurence is found, the first is given. */ - unsigned char - getSlotFromId(unsigned int itemId); + int getSlotFromId(int itemId); /** - * Return StoredItem + * Returns item. */ - StoredItem - getStoredItemAt(unsigned char slot) const { return itemList[slot]; }; + StoredItem const &getStoredItemAt(int slot) const + { return itemList[slot]; }; /** - * Search in inventory and equipment if an item is present. + * Looks in inventory and equipment whether an item is present or not. */ - bool - hasItem(unsigned int itemId, - bool searchInInventory = true, - bool searchInEquipment = true); + bool hasItem(int itemId, + bool searchInInventory = true, + bool searchInEquipment = true); /** * Tells an item's amount */ - unsigned short - getItemAmount(unsigned char slot) const { return itemList[slot].amount; }; + int getItemAmount(int slot) const + { return itemList[slot].amount; }; /** - * Return Item reference Id + * Returns item reference ID. */ - unsigned int - getItemId(unsigned char slot) const { return itemList[slot].itemId; }; + int getItemId(int slot) const + { return itemList[slot].itemId; }; /** - * add an item with amount - * (don't create it if amount was 0) - * @return short value: Indicates the number of items added. + * Adds a given amount of items. + * @return Number of items really added. */ - short - addItem(unsigned int itemId, unsigned char amount = 1); + int insertItem(int itemId, int 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. + * Removes an item given by ID. + * @return Number of items really removed. */ - short - removeItem(unsigned int itemId, unsigned char amount = 0); + int removeItemById(int itemId, int amount); /** - * 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. + * Removes an item given by slot. + * @return Number of items really removed. */ - short - removeItem(unsigned char slot, unsigned char amount = 0); + int removeItemBySlot(int slot, int amount); /** * Equip an item searched by its id. @@ -157,48 +146,30 @@ class Inventory * @return unsigned char value: Returns the slot if successful * or the error code if not. */ - unsigned char - equipItem(unsigned int itemId); + int equipItem(int itemId); /** * Unequip an item searched by its id. * Can unequip more than one item at a time. */ - bool - unequipItem(unsigned int itemId); + bool unequipItem(int itemId); /** - * Equip an item searched by its slot index. + * Equips an item searched by its slot index. */ - bool - equipItem(unsigned char inventorySlot, unsigned char equipmentSlot); + bool equipItem(int inventorySlot, int equipmentSlot); /** - * Unequip an equipped item searched by its slot index. + * Unequips an equipped item searched by its slot index. */ - bool - unequipItem(unsigned char inventorySlot, unsigned char equipmentSlot); - - /** - * The function called to use an item applying - * only the modifiers - */ - bool - use(unsigned char slot, BeingPtr itemUser); - - /** - * The function called to use an item applying - * only the modifiers - */ - bool - use(unsigned int itemId, BeingPtr itemUser); + bool unequipItem(int inventorySlot, int equipmentSlot); private: /** - * Give the first free slot number in itemList. + * Gives the first free slot number in itemList. */ - unsigned char getInventoryFreeSlot(); + int getInventoryFreeSlot(); /** * Quick equip an equipment with a given equipSlot, @@ -206,9 +177,9 @@ class Inventory * @return the equipment slot if successful, * the error code, if not. */ - unsigned char equipItem_(unsigned int itemId, - unsigned int itemType, - unsigned char equipmentSlot); + int equipItem_(int itemId, + int itemType, + int equipmentSlot); /** * Quick unequip an equipment with a given equipSlot, @@ -216,8 +187,8 @@ class Inventory * @return the Equipment slot if successful, * the error code, if not. */ - unsigned char unequipItem_(unsigned int itemId, - unsigned char equipmentSlot); + int unequipItem_(int itemId, + int equipmentSlot); // Stored items in inventory and equipment diff --git a/src/game-server/item.cpp b/src/game-server/item.cpp new file mode 100644 index 00000000..7eb9afb6 --- /dev/null +++ b/src/game-server/item.cpp @@ -0,0 +1,48 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include "game-server/item.hpp" + +bool ItemClass::use(Being *itemUser) +{ + bool usedSuccessfully = true; + // Applying Modifiers for a given lifetime + // TODO + + // Calling a script if scriptName != "" + if (!mScriptName.empty()) + { + if (runScript(itemUser) && usedSuccessfully) + return true; + else + return false; + } + else + return usedSuccessfully; +} + +bool ItemClass::runScript(Being *itemUser) +{ + //TODO + return true; +} diff --git a/src/game-server/item.hpp b/src/game-server/item.hpp new file mode 100644 index 00000000..283e685c --- /dev/null +++ b/src/game-server/item.hpp @@ -0,0 +1,232 @@ +/* + * The Mana World Server + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef _TMWSERV_ITEM +#define _TMWSERV_ITEM + +#include "game-server/player.hpp" + +/** + * Enumeration of available Item types. + */ +enum +{ + ITEM_UNUSABLE = 0, + ITEM_USABLE, // 1 + ITEM_EQUIPMENT_ONE_HAND_WEAPON, // 2 + ITEM_EQUIPMENT_TWO_HANDS_WEAPON, // 3 + ITEM_EQUIPMENT_BREST, // 4 + ITEM_EQUIPMENT_ARMS, // 5 + ITEM_EQUIPMENT_HEAD, // 6 + ITEM_EQUIPMENT_LEGS, // 7 + ITEM_EQUIPMENT_SHIELD, // 8 + ITEM_EQUIPMENT_RING, // 9 + ITEM_EQUIPMENT_NECKLACE, // 10 + ITEM_EQUIPMENT_FEET, // 11 + ITEM_EQUIPMENT_PROJECTILE // 12 +}; + +/** + * Enumeration of available weapon's types. + */ +enum +{ + WPNTYPE_NONE = 0, + WPNTYPE_KNIFE, // 1 + WPNTYPE_SWORD, // 2 + WPNTYPE_SPEAR, // 3 + WPNTYPE_JAVELIN, // 4 + WPNTYPE_ROD, // 5 + WPNTYPE_STAFF, // 6 + WPNTYPE_WIPE, // 7 + WPNTYPE_PROJECTILE, // 8 + WPNTYPE_BOOMERANG, // 9 + WPNTYPE_BOW, // 10 + WPNTYPE_SICKLE, // 11 + WPNTYPE_CROSSBOW, // 12 + WPNTYPE_STICK, // 13 + WPNTYPE_HAMMER, // 14 + WPNTYPE_AXE, // 15 + WPNTYPE_HAND_PROECTILE // 16 +}; + +/** + * States attribute effects to beings, and actors. + * States can be multiple for the same being. + */ +enum +{ + STATE_NORMAL = 0, + STATE_POISONED, + STATE_STONED, + STATE_STUNNED, + STATE_SLOWED, + STATE_TIRED, + STATE_MAD, + STATE_BERSERK, + STATE_HASTED, + STATE_FLOATING, + + STATE_NOT_POISONED, + STATE_NOT_STONED, + STATE_NOT_STUNNED, + STATE_NOT_SLOWED, + STATE_NOT_TIRED, + STATE_NOT_MAD, + STATE_NOT_BERSERK, + STATE_NOT_HASTED, + STATE_NOT_FLOATING +}; + +/** + * statistics modifiers. + * once for usables. + * Permanent for equipment. + */ +struct Modifiers +{ + // General + unsigned char element; /**< Item Element */ + unsigned char beingStateEffect; /**< Being State (dis)alteration */ + unsigned short lifetime; /**< Modifiers lifetime in seconds. */ + + // Caracteristics Modifiers + short rawStats[NB_RSTAT]; /**< Raw Stats modifiers */ + short computedStats[NB_CSTAT]; /**< Computed Stats modifiers */ + + short hp; /**< HP modifier */ + short mp; /**< MP Modifier */ + + // Weapon + unsigned short range; /**< Weapon Item Range */ + unsigned char weaponType; /**< Weapon Type enum */ +}; + + +/** + * Class for simple reference to item information. + */ +class ItemClass +{ + public: + ItemClass(int type) + : mType(type) + {} + + /** + * The function called to use an item applying + * only the modifiers (for simple items...) + */ + bool use(Being *itemUser); + + /** + * Gets item type. + */ + int getType() const + { return mType; } + + /** + * Gets item weight. + */ + int getWeight() const + { return mWeight; } + + /** + * Sets item weight. + */ + void setWeight(int weight) + { mWeight = weight; } + + /** + * Gets unit cost of these items. + */ + int getCost() const + { return mCost; } + + /** + * Sets unit cost of these items. + */ + void setCost(int cost) + { mCost = cost; } + + /** + * Gets max item per slot. + */ + int getMaxPerSlot() const + { return mMaxPerSlot; } + + /** + * Sets max item per slot. + */ + void setMaxPerSlot(int perSlot) + { mMaxPerSlot = perSlot; } + + /** + * Gets item modifiers. + */ + Modifiers const &getModifiers() const + { return mModifiers; } + + /** + * Sets item modifiers. + */ + void setModifiers(Modifiers const &modifiers) + { mModifiers = modifiers; } + + /** + * Sets associated script name. + */ + void setScriptName(std::string const &name) + { mScriptName = name; } + + private: + + /** + * Runs the associated script when using the item, if any. + */ + bool runScript(Being *itemUser); + + // Item reference information + unsigned char mType; /**< Type: usable, equipment. */ + unsigned short mWeight; /**< Weight of the item. */ + unsigned short mCost; /**< Unit cost the item. */ + unsigned short mMaxPerSlot; /**< Max item amount per slot in inventory. */ + std::string mScriptName; /**< Item script. */ + Modifiers mModifiers; /**< Item modifiers. */ +}; + +class Item: public Object +{ + public: + Item(ItemClass *type) + : Object(OBJECT_ITEM), mType(type) + {} + + ItemClass *getItemClass() const + { return mType; } + + private: + ItemClass *mType; +}; + +#endif diff --git a/src/game-server/itemmanager.cpp b/src/game-server/itemmanager.cpp index 01b957a6..f4a5b751 100644 --- a/src/game-server/itemmanager.cpp +++ b/src/game-server/itemmanager.cpp @@ -98,13 +98,17 @@ ItemManager::ItemManager(std::string const &itemReferenceFile) modifiers.hp = XML::getProperty(node, "hp", 0); modifiers.mp = XML::getProperty(node, "mp", 0); modifiers.range = XML::getProperty(node, "range", 0); - modifiers.weaponType = (WeaponType)XML::getProperty(node, "weapon_type", 0); - modifiers.beingStateEffect = (BeingStateEffect)XML::getProperty(node, "status_effect", 0); - - ItemPtr item(new Item(modifiers, itemType, weight, - value, scriptName, maxPerSlot)); + modifiers.weaponType = XML::getProperty(node, "weapon_type", 0); + modifiers.beingStateEffect = XML::getProperty(node, "status_effect", 0); + + ItemClass *item = new ItemClass(itemType); + item->setWeight(weight); + item->setCost(value); + item->setMaxPerSlot(maxPerSlot); + item->setScriptName(scriptName); + item->setModifiers(modifiers); mItemReference[id] = item; - nbItems++; + ++nbItems; if (maxPerSlot == 0) { diff --git a/src/game-server/itemmanager.hpp b/src/game-server/itemmanager.hpp index a5ebc8f3..64bf5ae2 100644 --- a/src/game-server/itemmanager.hpp +++ b/src/game-server/itemmanager.hpp @@ -26,12 +26,11 @@ #include -#include "item.h" +#include "game-server/item.hpp" /** * The Item Manager loads the item reference database * and also offers an API to items information, and more. - * For item objects, see the WorldItem class. */ class ItemManager { @@ -39,50 +38,16 @@ class ItemManager /** * Constructor (loads item reference file) */ - ItemManager(const std::string &itemReferenceFile); + ItemManager(std::string const &itemReferenceFile); /** * Gives an Item having the demanded information. */ - ItemPtr getItem(const unsigned int itemId) + ItemClass *getItem(int itemId) { return mItemReference[itemId]; }; - bool use(BeingPtr beingPtr, const unsigned int itemId) - { return mItemReference[itemId].get()->use(beingPtr); }; - - /** - * Return item Type - */ - unsigned short getItemType(const unsigned int itemId) - { return mItemReference[itemId].get()->getItemType(); }; - - /** - * Return Weight of item - */ - unsigned int getWeight(const unsigned int itemId) - { return mItemReference[itemId].get()->getWeight(); }; - - /** - * Return gold value of item - */ - unsigned int getGoldValue(const unsigned int itemId) - { 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 - getItemModifiers(const unsigned int itemId) - { return mItemReference[itemId].get()->getItemModifiers(); }; - private: - std::map mItemReference; /**< Item reference */ + std::map< int, ItemClass * > mItemReference; /**< Item reference */ }; extern ItemManager *itemManager; diff --git a/src/game-server/mapcomposite.cpp b/src/game-server/mapcomposite.cpp index 1b824447..21648bc1 100644 --- a/src/game-server/mapcomposite.cpp +++ b/src/game-server/mapcomposite.cpp @@ -418,7 +418,7 @@ ZoneIterator MapComposite::getAroundPlayerIterator(MovingObject *obj, int radius } fillRegion(r2, obj->getPosition(), radius); return ZoneIterator(r2, this); -} +} bool MapComposite::insert(Thing *ptr) { diff --git a/src/game-server/mapcomposite.hpp b/src/game-server/mapcomposite.hpp index 526b36e2..1996950e 100644 --- a/src/game-server/mapcomposite.hpp +++ b/src/game-server/mapcomposite.hpp @@ -23,7 +23,7 @@ #ifndef _TMW_SERVER_MAPCOMPOSITE_ #define _TMW_SERVER_MAPCOMPOSITE_ - + #include class Map; @@ -183,12 +183,12 @@ class MapComposite { /** * Gets an iterator on the objects inside a given rectangle. */ - ZoneIterator getInsideRectangleIterator(Rectangle const &) const; + ZoneIterator getInsideRectangleIterator(Rectangle const &) const; /** * Gets an iterator on the objects around a given object. */ - ZoneIterator getAroundObjectIterator(Object *, int radius) const; + ZoneIterator getAroundObjectIterator(Object *, int radius) const; /** * Gets an iterator on the objects around the old and new positions of diff --git a/src/game-server/player.cpp b/src/game-server/player.cpp index 294239f9..99884661 100644 --- a/src/game-server/player.cpp +++ b/src/game-server/player.cpp @@ -56,32 +56,27 @@ void Player::update() } } -void Player::setInventory(const Inventory &inven) +bool Player::insertItem(int itemId, int amount) { - inventory = inven; + return inventory.insertItem(itemId, amount); } -bool Player::addItem(unsigned int itemId, unsigned char amount) +bool Player::removeItem(int itemId, int amount) { - return inventory.addItem(itemId, amount); + return inventory.removeItemById(itemId, amount); } -bool Player::removeItem(unsigned int itemId, unsigned char amount) -{ - return inventory.removeItem(itemId, amount); -} - -bool Player::hasItem(unsigned int itemId) +bool Player::hasItem(int itemId) { return inventory.hasItem(itemId); } -bool Player::equip(unsigned char slot) +bool Player::equip(int slot) { return false; // TODO } -bool Player::unequip(unsigned char slot) +bool Player::unequip(int slot) { return false; // TODO } diff --git a/src/game-server/player.hpp b/src/game-server/player.hpp index 4ecef616..eaf4d9e9 100644 --- a/src/game-server/player.hpp +++ b/src/game-server/player.hpp @@ -46,53 +46,48 @@ class Player : public Being, public PlayerData /** * Updates the internal status. */ - void update(); - + void update(); + /** * Sets inventory. */ - void - setInventory(const Inventory &inven); + void setInventory(Inventory const &inven) + { inventory = inven; } /** * Adds item with ID to inventory. * * @return Item add success/failure */ - bool - addItem(unsigned int itemId, unsigned char amount = 1); + bool insertItem(int itemId, int amount); /** * Removes item with ID from inventory. * * @return Item delete success/failure */ - bool - removeItem(unsigned int itemId, unsigned char amount = 0); + bool removeItem(int itemId, int amount); /** * Checks if character has an item. * * @return true if being has item, false otherwise */ - bool - hasItem(unsigned int itemId); + bool hasItem(int itemId); /** * Equips item with ID in equipment slot. * * @return Equip success/failure */ - bool - equip(unsigned char slot); + bool equip(int slot); /** * Un-equips item. * * @return Un-equip success/failure */ - bool - unequip(unsigned char slot); + bool unequip(int slot); /** * Set attacking state diff --git a/src/item.cpp b/src/item.cpp deleted file mode 100644 index 013c2c69..00000000 --- a/src/item.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * The Mana World Server - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * The Mana World is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ - */ - -#include "item.h" - -bool Item::use(BeingPtr itemUser) -{ - bool usedSuccessfully = true; - // Applying Modifiers for a given lifetime - // TODO - - // Calling a script if scriptName != "" - if (mScriptName != "") - { - if(runScript(itemUser) && usedSuccessfully) - return true; - else - return false; - } - else - return usedSuccessfully; -} - -bool Item::runScript(BeingPtr itemUser) -{ - //TODO - return true; -} diff --git a/src/item.h b/src/item.h deleted file mode 100644 index 3ac9c68d..00000000 --- a/src/item.h +++ /dev/null @@ -1,206 +0,0 @@ -/* - * The Mana World Server - * Copyright 2004 The Mana World Development Team - * - * This file is part of The Mana World. - * - * The Mana World is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * any later version. - * - * The Mana World is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with The Mana World; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * $Id$ - */ - -#ifndef ITEM_H -#define ITEM_H - -#include "playerdata.hpp" -#include "game-server/being.hpp" - -/** - * Enumeration of available Item types. - */ -typedef enum ItemType { - ITEM_UNUSABLE = 0, - ITEM_USABLE, // 1 - ITEM_EQUIPMENT_ONE_HAND_WEAPON, // 2 - ITEM_EQUIPMENT_TWO_HANDS_WEAPON, // 3 - ITEM_EQUIPMENT_BREST, // 4 - ITEM_EQUIPMENT_ARMS, // 5 - ITEM_EQUIPMENT_HEAD, // 6 - ITEM_EQUIPMENT_LEGS, // 7 - ITEM_EQUIPMENT_SHIELD, // 8 - ITEM_EQUIPMENT_RING, // 9 - ITEM_EQUIPMENT_NECKLACE, // 10 - ITEM_EQUIPMENT_FEET, // 11 - ITEM_EQUIPMENT_PROJECTILE // 12 -}; - -/** - * Enumeration of available weapon's types. - */ -typedef enum WeaponType { - WPNTYPE_NONE = 0, - WPNTYPE_KNIFE, // 1 - WPNTYPE_SWORD, // 2 - WPNTYPE_SPEAR, // 3 - WPNTYPE_JAVELIN, // 4 - WPNTYPE_ROD, // 5 - WPNTYPE_STAFF, // 6 - WPNTYPE_WIPE, // 7 - WPNTYPE_PROJECTILE, // 8 - WPNTYPE_BOOMERANG, // 9 - WPNTYPE_BOW, // 10 - WPNTYPE_SICKLE, // 11 - WPNTYPE_CROSSBOW, // 12 - WPNTYPE_STICK, // 13 - WPNTYPE_HAMMER, // 14 - WPNTYPE_AXE, // 15 - WPNTYPE_HAND_PROECTILE // 16 -}; - -/** - * States attribute effects to beings, and actors. - * States can be multiple for the same being. - */ -typedef enum BeingStateEffect { - STATE_NORMAL = 0, - STATE_POISONED, - STATE_STONED, - STATE_STUNNED, - STATE_SLOWED, - STATE_TIRED, - STATE_MAD, - STATE_BERSERK, - STATE_HASTED, - STATE_FLOATING, - - STATE_NOT_POISONED, - STATE_NOT_STONED, - STATE_NOT_STUNNED, - STATE_NOT_SLOWED, - STATE_NOT_TIRED, - STATE_NOT_MAD, - STATE_NOT_BERSERK, - STATE_NOT_HASTED, - STATE_NOT_FLOATING -}; - -/** - * statistics modifiers. - * once for usables. - * Permanent for equipment. - */ -struct Modifiers -{ - // General - unsigned char element; /**< Item Element */ - BeingStateEffect beingStateEffect; /**< Being State (dis)alteration */ - unsigned short lifetime; /**< Modifiers lifetime in seconds. */ - - // Caracteristics Modifiers - short rawStats[NB_RSTAT]; /**< Raw Stats modifiers */ - short computedStats[NB_CSTAT]; /**< Computed Stats modifiers */ - - int hp; /**< HP modifier */ - int mp; /**< MP Modifier */ - - // Weapon - unsigned short range; /**< Weapon Item Range */ - WeaponType weaponType; /**< Weapon Type enum */ -}; - - -/** - * Class for simple reference to item information. - * See WorldItem to get full featured Item Objects. - */ -class Item -{ - public: - - Item(Modifiers modifiers, - unsigned short itemType = 0, - unsigned int weight = 0, - unsigned int value = 0, - std::string scriptName = "", - unsigned short maxPerSlot = 0): - mItemType(itemType), - mWeight(weight), - mValue(value), - mScriptName(scriptName), - mMaxPerSlot(maxPerSlot), - mModifiers(modifiers) {} - - virtual ~Item() throw() { } - - /** - * The function called to use an item applying - * only the modifiers (for simple items...) - */ - bool use(BeingPtr itemUser); - - /** - * Return item Type - */ - unsigned short getItemType() const { return mItemType; }; - - /** - * Return Weight of item - */ - unsigned int getWeight() const { return mWeight; }; - - /** - * Return gold value of item - */ - unsigned int getGoldValue() const { return mValue; }; - - /** - * Return max item per slot - */ - unsigned short getMaxPerSlot() const { return mMaxPerSlot; }; - - /** - * Return item's modifiers - */ - Modifiers - getItemModifiers() const { return mModifiers; }; - - private: - - /** - * Runs the associated script when using the item, if any. - */ - bool runScript(BeingPtr itemUser); - - // Item reference information - unsigned short mItemType; /**< ItemType: Usable, equipment */ - unsigned int mWeight; /**< Weight of the item */ - unsigned int mValue; /**< Gold value of the item */ - std::string mScriptName; /**< item's script. None if =="" */ - unsigned short mMaxPerSlot; /**< Max item amount per slot in inventory */ - - Modifiers mModifiers; /**< Item's Modifiers */ -}; - -/** - * Type definition for a smart pointer to Item. - */ -typedef utils::CountedPtr ItemPtr; - -/** - * Type definition for a list of Items. - */ -typedef std::vector Items; - -#endif -- cgit v1.2.3-70-g09d2