diff options
author | Philipp Sehmisch <tmw@crushnet.org> | 2008-02-21 17:59:40 +0000 |
---|---|---|
committer | Philipp Sehmisch <tmw@crushnet.org> | 2008-02-21 17:59:40 +0000 |
commit | 12c9a7508dfa863087035f6dc2f73866789927f1 (patch) | |
tree | 749b96a4738fcc199c8a7c23bf890f01aefb2e06 | |
parent | d1649dfdf5628fb49d1c2c50085cd318a87a515f (diff) | |
download | manaserv-12c9a7508dfa863087035f6dc2f73866789927f1.tar.gz manaserv-12c9a7508dfa863087035f6dc2f73866789927f1.tar.bz2 manaserv-12c9a7508dfa863087035f6dc2f73866789927f1.tar.xz manaserv-12c9a7508dfa863087035f6dc2f73866789927f1.zip |
Moved the functions which translate strings into enumerations from the XML library into the item and monstermanager modules and reimplemented them using std::maps.
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | src/game-server/item.cpp | 55 | ||||
-rw-r--r-- | src/game-server/item.hpp | 5 | ||||
-rw-r--r-- | src/game-server/itemmanager.cpp | 4 | ||||
-rw-r--r-- | src/game-server/monstermanager.cpp | 24 | ||||
-rw-r--r-- | src/utils/xml.cpp | 58 | ||||
-rw-r--r-- | src/utils/xml.hpp | 14 |
7 files changed, 90 insertions, 75 deletions
@@ -11,6 +11,11 @@ * src/defines.h, src/game-server/monstermanager.cpp, src/game-server/monster.cpp, src/game-server/monster.hpp: Implemented getting attack distance and attack properties from monsters.xml. + * src/util/xml.cpp, src/util/xml.hpp, src/game-server/item.hpp, + src/game-server/item.cpp, src/game-server/monstermanager.cpp, + src/game-server/itemmanager.cpp: Moved the functions which translate + strings into enumerations from the XML library into the item and + monstermanager modules and reimplemented them using std::maps. 2008-02-20 Philipp Sehmisch <tmw@crushnet.org> diff --git a/src/game-server/item.cpp b/src/game-server/item.cpp index 23df3b0e..f14374b6 100644 --- a/src/game-server/item.cpp +++ b/src/game-server/item.cpp @@ -21,10 +21,65 @@ * $Id$ */ + +#include <string> +#include <map> + #include "game-server/item.hpp" #include "game-server/being.hpp" +WeaponType weaponTypeFromString (const std::string &name) +{ + static std::map<const std::string, WeaponType> table; + + if (table.empty()) + { + table["knife"] = WPNTYPE_KNIFE; + table["sword"] = WPNTYPE_SWORD; + table["polearm"] = WPNTYPE_POLEARM; + table["staff"] = WPNTYPE_STAFF; + table["whip"] = WPNTYPE_WHIP; + table["bow"] = WPNTYPE_BOW; + table["shooting"] = WPNTYPE_SHOOTING; + table["mace"] = WPNTYPE_MACE; + table["axe"] = WPNTYPE_AXE; + table["thrown"] = WPNTYPE_THROWN; + } + + std::map<const std::string, WeaponType>::iterator val = table.find(name); + + return val == table.end() ? WPNTYPE_NONE : (*val).second; +} + +ItemType itemTypeFromString (const std::string &name) +{ + static std::map<const std::string, ItemType> table; + + if (table.empty()) + { + table["generic"] = ITEM_UNUSABLE; + table["usable"] = ITEM_USABLE; + table["equip-1hand"] = ITEM_EQUIPMENT_ONE_HAND_WEAPON; + table["equip-2hand"] = ITEM_EQUIPMENT_TWO_HANDS_WEAPON; + table["equip-torso"] = ITEM_EQUIPMENT_TORSO; + table["equip-arms"] = ITEM_EQUIPMENT_ARMS; + table["equip-head"] = ITEM_EQUIPMENT_HEAD; + table["equip-legs"] = ITEM_EQUIPMENT_LEGS; + table["equip-shield"] = ITEM_EQUIPMENT_SHIELD; + table["equip-ring"] = ITEM_EQUIPMENT_RING; + table["equip-necklace"] = ITEM_EQUIPMENT_NECKLACE; + table["equip-feet"] = ITEM_EQUIPMENT_FEET; + table["equip-ammo"] = ITEM_EQUIPMENT_AMMO; + table["hairsprite"] = ITEM_HAIRSPRITE; + table["racesprite"] = ITEM_RACESPRITE; + } + + std::map<const std::string, ItemType>::iterator val = table.find(name); + + return val == table.end() ? ITEM_UNKNOWN : (*val).second; +} + int ItemModifiers::getValue(int type) const { for (std::vector< ItemModifier >::const_iterator i = mModifiers.begin(), diff --git a/src/game-server/item.hpp b/src/game-server/item.hpp index 018cac48..7b21ad87 100644 --- a/src/game-server/item.hpp +++ b/src/game-server/item.hpp @@ -53,6 +53,9 @@ enum ItemType ITEM_UNKNOWN }; +ItemType itemTypeFromString (const std::string &name); + + /** * Enumeration of available weapon's types. */ @@ -71,6 +74,8 @@ enum WeaponType WPNTYPE_THROWN }; +WeaponType weaponTypeFromString (const std::string &name); + /** * State effects to beings, and actors. * States can be multiple for the same being. diff --git a/src/game-server/itemmanager.cpp b/src/game-server/itemmanager.cpp index 78c7f04a..211334ff 100644 --- a/src/game-server/itemmanager.cpp +++ b/src/game-server/itemmanager.cpp @@ -91,7 +91,7 @@ void ItemManager::reload() } std::string sItemType = XML::getProperty(node, "type", ""); - ItemType itemType = XML::itemTypeFromString(sItemType); + ItemType itemType = itemTypeFromString(sItemType); if (itemType == ITEM_UNKNOWN) { @@ -128,7 +128,7 @@ void ItemManager::reload() itemType == ITEM_EQUIPMENT_TWO_HANDS_WEAPON) { std::string sWeaponType = XML::getProperty(node, "weapon-type", ""); - WeaponType weaponType = XML::weaponTypeFromString(sWeaponType); + WeaponType weaponType = weaponTypeFromString(sWeaponType); if (weaponType == WPNTYPE_NONE) { LOG_WARN(itemReferenceFile<<": Unknown weapon type \"" diff --git a/src/game-server/monstermanager.cpp b/src/game-server/monstermanager.cpp index 69ad5c08..a8239b8d 100644 --- a/src/game-server/monstermanager.cpp +++ b/src/game-server/monstermanager.cpp @@ -36,6 +36,28 @@ typedef std::map< int, MonsterClass * > MonsterClasses; static MonsterClasses monsterClasses; /**< Monster reference */ static std::string monsterReferenceFile; +Element elementFromString (const std::string &name) +{ + static std::map<const std::string, Element> table; + + if (table.empty()) + { + table["neutral"] = ELEMENT_NEUTRAL; + table["fire"] = ELEMENT_FIRE; + table["water"] = ELEMENT_WATER; + table["earth"] = ELEMENT_EARTH; + table["air"] = ELEMENT_AIR; + table["lightning"] = ELEMENT_LIGHTNING; + table["metal"] = ELEMENT_METAL; + table["wood"] = ELEMENT_WOOD; + table["ice"] = ELEMENT_ICE; + } + + std::map<const std::string, Element>::iterator val = table.find(name); + + return val == table.end() ? ELEMENT_ILLEGAL : (*val).second; +} + void MonsterManager::initialize(std::string const &file) { monsterReferenceFile = file; @@ -209,7 +231,7 @@ void MonsterManager::reload() att->range = XML::getProperty(subnode, "range", 1); att->angle = XML::getProperty(subnode, "angle", 1); std::string sElement = XML::getProperty(subnode, "element", "neutral"); - att->element = XML::elementFromString(sElement); + att->element = elementFromString(sElement); std::string sType = XML::getProperty(subnode, "type", "physical"); if (sType == "physical") {att->type = DAMAGE_PHYSICAL; } else if (sType == "magical" || sType == "magic") {att->type = DAMAGE_MAGICAL; } diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp index f71e5bfd..f5a3ef5a 100644 --- a/src/utils/xml.cpp +++ b/src/utils/xml.cpp @@ -64,62 +64,4 @@ std::string getProperty(xmlNodePtr node, char const *name, std::string const &de return def; } -unsigned int elementFromString(std::string name) -{ - if (name=="neutral") return ELEMENT_NEUTRAL; - else if (name=="fire") return ELEMENT_FIRE; - else if (name=="water") return ELEMENT_WATER; - else if (name=="earth") return ELEMENT_EARTH; - else if (name=="air") return ELEMENT_AIR; - else if (name=="lightning") return ELEMENT_LIGHTNING; - else if (name=="metal") return ELEMENT_METAL; - else if (name=="wood") return ELEMENT_WOOD; - else if (name=="ice") return ELEMENT_ICE; - { - return ELEMENT_ILLEGAL; - } -} - -ItemType itemTypeFromString (std::string name) -{ - if (name=="generic") return ITEM_UNUSABLE; - else if (name=="usable") return ITEM_USABLE; - else if (name=="equip-1hand") return ITEM_EQUIPMENT_ONE_HAND_WEAPON; - else if (name=="equip-2hand") return ITEM_EQUIPMENT_TWO_HANDS_WEAPON; - else if (name=="equip-torso") return ITEM_EQUIPMENT_TORSO; - else if (name=="equip-arms") return ITEM_EQUIPMENT_ARMS; - else if (name=="equip-head") return ITEM_EQUIPMENT_HEAD; - else if (name=="equip-legs") return ITEM_EQUIPMENT_LEGS; - else if (name=="equip-shield") return ITEM_EQUIPMENT_SHIELD; - else if (name=="equip-ring") return ITEM_EQUIPMENT_RING; - else if (name=="equip-necklace") return ITEM_EQUIPMENT_NECKLACE; - else if (name=="equip-feet") return ITEM_EQUIPMENT_FEET; - else if (name=="equip-ammo") return ITEM_EQUIPMENT_AMMO; - else if (name=="hairsprite") return ITEM_HAIRSPRITE; - else if (name=="racesprite") return ITEM_RACESPRITE; - else - { - return ITEM_UNKNOWN; - } -} - -WeaponType weaponTypeFromString (std::string name) -{ - if (name=="knife") return WPNTYPE_KNIFE; - else if (name=="sword") return WPNTYPE_SWORD; - else if (name=="polearm") return WPNTYPE_POLEARM; - else if (name=="staff") return WPNTYPE_STAFF; - else if (name=="whip") return WPNTYPE_WHIP; - else if (name=="bow") return WPNTYPE_BOW; - else if (name=="shooting") return WPNTYPE_SHOOTING; - else if (name=="mace") return WPNTYPE_MACE; - else if (name=="axe") return WPNTYPE_AXE; - else if (name=="thrown") return WPNTYPE_THROWN; - else - { - return WPNTYPE_NONE; - } -} - - } // namespace XML diff --git a/src/utils/xml.hpp b/src/utils/xml.hpp index d2ab62e7..6383de9c 100644 --- a/src/utils/xml.hpp +++ b/src/utils/xml.hpp @@ -47,20 +47,6 @@ namespace XML */ double getFloatProperty(xmlNodePtr node, const char *name, double def); - - - /** - * Converts a string into an element number - */ - unsigned int elementFromString(std::string name); - /** - * Converts a string into an ItemType number - */ - ItemType itemTypeFromString (std::string name); - /** - * Converts a string into a weaponType number - */ - WeaponType weaponTypeFromString (std::string name); } #define for_each_xml_child_node(var, parent) \ |