00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <string>
00024 #include <map>
00025
00026 #include "game-server/item.hpp"
00027
00028 #include "game-server/attackzone.hpp"
00029 #include "game-server/being.hpp"
00030 #include "scripting/script.hpp"
00031
00032 WeaponType weaponTypeFromString (const std::string &name)
00033 {
00034 static std::map<const std::string, WeaponType> table;
00035
00036 if (table.empty())
00037 {
00038 table["knife"] = WPNTYPE_KNIFE;
00039 table["sword"] = WPNTYPE_SWORD;
00040 table["polearm"] = WPNTYPE_POLEARM;
00041 table["staff"] = WPNTYPE_STAFF;
00042 table["whip"] = WPNTYPE_WHIP;
00043 table["bow"] = WPNTYPE_BOW;
00044 table["shooting"] = WPNTYPE_SHOOTING;
00045 table["mace"] = WPNTYPE_MACE;
00046 table["axe"] = WPNTYPE_AXE;
00047 table["thrown"] = WPNTYPE_THROWN;
00048 }
00049
00050 std::map<const std::string, WeaponType>::iterator val = table.find(name);
00051
00052 return val == table.end() ? WPNTYPE_NONE : (*val).second;
00053 }
00054
00055 ItemType itemTypeFromString (const std::string &name)
00056 {
00057 static std::map<const std::string, ItemType> table;
00058
00059 if (table.empty())
00060 {
00061 table["generic"] = ITEM_UNUSABLE;
00062 table["usable"] = ITEM_USABLE;
00063 table["equip-1hand"] = ITEM_EQUIPMENT_ONE_HAND_WEAPON;
00064 table["equip-2hand"] = ITEM_EQUIPMENT_TWO_HANDS_WEAPON;
00065 table["equip-torso"] = ITEM_EQUIPMENT_TORSO;
00066 table["equip-arms"] = ITEM_EQUIPMENT_ARMS;
00067 table["equip-head"] = ITEM_EQUIPMENT_HEAD;
00068 table["equip-legs"] = ITEM_EQUIPMENT_LEGS;
00069 table["equip-shield"] = ITEM_EQUIPMENT_SHIELD;
00070 table["equip-ring"] = ITEM_EQUIPMENT_RING;
00071 table["equip-necklace"] = ITEM_EQUIPMENT_NECKLACE;
00072 table["equip-feet"] = ITEM_EQUIPMENT_FEET;
00073 table["equip-ammo"] = ITEM_EQUIPMENT_AMMO;
00074 table["hairsprite"] = ITEM_HAIRSPRITE;
00075 table["racesprite"] = ITEM_RACESPRITE;
00076 }
00077
00078 std::map<const std::string, ItemType>::iterator val = table.find(name);
00079
00080 return val == table.end() ? ITEM_UNKNOWN : (*val).second;
00081 }
00082
00083 int ItemModifiers::getValue(int type) const
00084 {
00085 for (std::vector< ItemModifier >::const_iterator i = mModifiers.begin(),
00086 i_end = mModifiers.end(); i != i_end; ++i)
00087 {
00088 if (i->type == type) return i->value;
00089 }
00090 return 0;
00091 }
00092
00093 int ItemModifiers::getAttributeValue(int attr) const
00094 {
00095 return getValue(MOD_ATTRIBUTE + attr);
00096 }
00097
00098 void ItemModifiers::setValue(int type, int value)
00099 {
00100 if (value)
00101 {
00102 ItemModifier m;
00103 m.type = type;
00104 m.value = value;
00105 mModifiers.push_back(m);
00106 }
00107 }
00108
00109 void ItemModifiers::setAttributeValue(int attr, int value)
00110 {
00111 setValue(MOD_ATTRIBUTE + attr, value);
00112 }
00113
00114 void ItemModifiers::applyAttributes(Being *b) const
00115 {
00116
00117
00118
00119
00120 int lifetime = getValue(MOD_LIFETIME);
00121 for (std::vector< ItemModifier >::const_iterator i = mModifiers.begin(),
00122 i_end = mModifiers.end(); i != i_end; ++i)
00123 {
00124 if (i->type < MOD_ATTRIBUTE) continue;
00125 b->applyModifier(i->type - MOD_ATTRIBUTE, i->value, lifetime);
00126 }
00127 }
00128
00129 void ItemModifiers::cancelAttributes(Being *b) const
00130 {
00131 for (std::vector< ItemModifier >::const_iterator i = mModifiers.begin(),
00132 i_end = mModifiers.end(); i != i_end; ++i)
00133 {
00134 if (i->type < MOD_ATTRIBUTE) continue;
00135 b->applyModifier(i->type - MOD_ATTRIBUTE, -i->value);
00136 }
00137 }
00138
00139 ItemClass::~ItemClass()
00140 {
00141 if (mAttackZone) delete mAttackZone;
00142 if (mScript) delete mScript;
00143 }
00144
00145 bool ItemClass::use(Being *itemUser)
00146 {
00147 if (mType != ITEM_USABLE) return false;
00148 if (mScript)
00149 {
00150 mScript->prepare("use");
00151 mScript->push(itemUser);
00152 mScript->push(mDatabaseID);
00153 mScript->execute();
00154 }
00155 mModifiers.applyAttributes(itemUser);
00156 return true;
00157 }