summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/game-server/being.cpp42
-rw-r--r--src/game-server/being.hpp11
-rw-r--r--src/game-server/gamehandler.cpp13
-rw-r--r--src/game-server/item.cpp30
-rw-r--r--src/game-server/item.hpp17
-rw-r--r--src/game-server/itemmanager.cpp4
6 files changed, 45 insertions, 72 deletions
diff --git a/src/game-server/being.cpp b/src/game-server/being.cpp
index aafb076a..bc690231 100644
--- a/src/game-server/being.cpp
+++ b/src/game-server/being.cpp
@@ -221,29 +221,18 @@ void Being::setAction(Action action)
}
}
-void Being::addModifier(AttributeModifier const &mod)
+void Being::applyModifier(int attr, int amount, int duration, int lvl)
{
- mModifiers.push_back(mod);
- mAttributes[mod.attr].mod += mod.value;
- modifiedAttribute(mod.attr);
-}
-
-void Being::removeEquipmentModifier(int attr, int value)
-{
- bool found = false;
- for (AttributeModifiers::iterator i = mModifiers.begin(),
- i_end = mModifiers.end(); i != i_end; ++i)
+ if (duration)
{
- found = i->level == 0 && i->attr == attr && i->value == value;
- if (found)
- {
- // Remove one equivalent modifier.
- mModifiers.erase(i);
- break;
- }
+ AttributeModifier mod;
+ mod.attr = attr;
+ mod.value = amount;
+ mod.duration = duration;
+ mod.level = lvl;
+ mModifiers.push_back(mod);
}
- assert(found);
- mAttributes[attr].mod -= value;
+ mAttributes[attr].mod += amount;
modifiedAttribute(attr);
}
@@ -275,14 +264,13 @@ void Being::update()
AttributeModifiers::iterator i = mModifiers.begin();
while (i != mModifiers.end())
{
- if (i->duration)
+ --i->duration;
+ if (!i->duration)
{
- --i->duration;
- if (!i->duration)
- {
- i = mModifiers.erase(i);
- continue;
- }
+ mAttributes[i->attr].mod -= i->value;
+ modifiedAttribute(i->attr);
+ i = mModifiers.erase(i);
+ continue;
}
++i;
}
diff --git a/src/game-server/being.hpp b/src/game-server/being.hpp
index 6c5b2669..a390a4bf 100644
--- a/src/game-server/being.hpp
+++ b/src/game-server/being.hpp
@@ -197,13 +197,12 @@ class Being : public MovingObject
/**
* Adds a modifier to one attribute.
+ * @param duration If non-zero, creates a temporary modifier that
+ * expires after \p duration ticks.
+ * @param lvl If non-zero, indicates that a temporary modifier can be
+ * dispelled prematuraly by a spell of given level.
*/
- void addModifier(AttributeModifier const &);
-
- /**
- * Removes a modifier due to an equipment.
- */
- void removeEquipmentModifier(int attr, int value);
+ void applyModifier(int attr, int value, int duration = 0, int lvl = 0);
/**
* Removes all the modifiers with a level low enough.
diff --git a/src/game-server/gamehandler.cpp b/src/game-server/gamehandler.cpp
index 59cb9b31..f02cc450 100644
--- a/src/game-server/gamehandler.cpp
+++ b/src/game-server/gamehandler.cpp
@@ -216,6 +216,19 @@ void GameHandler::processMessage(NetComputer *comp, MessageIn &message)
}
} break;
+ case PGMSG_USE_ITEM:
+ {
+ int slot = message.readByte();
+ Inventory inv(computer.character);
+ if (ItemClass *ic = ItemManager::getItem(inv.getItem(slot)))
+ {
+ if (ic->use(computer.character))
+ {
+ inv.removeFromSlot(slot, 1);
+ }
+ }
+ } break;
+
case PGMSG_DROP:
{
int slot = message.readByte();
diff --git a/src/game-server/item.cpp b/src/game-server/item.cpp
index 1b32cdfa..23df3b0e 100644
--- a/src/game-server/item.cpp
+++ b/src/game-server/item.cpp
@@ -58,19 +58,16 @@ void ItemModifiers::setAttributeValue(int attr, int value)
void ItemModifiers::applyAttributes(Being *b) const
{
+ /* Note: if someone puts a "lifetime" property on an equipment, strange
+ behavior will occur, as its effect will be canceled twice. While this
+ could be desirable for some "cursed" items, it is probably an error
+ that should be detected somewhere else. */
int lifetime = getValue(MOD_LIFETIME);
for (std::vector< ItemModifier >::const_iterator i = mModifiers.begin(),
i_end = mModifiers.end(); i != i_end; ++i)
{
if (i->type < MOD_ATTRIBUTE) continue;
-
- AttributeModifier am;
- am.attr = i->type - MOD_ATTRIBUTE;
- am.duration = lifetime;
- am.value = i->value;
- // TODO: No spell currently.
- am.level = 0;
- b->addModifier(am);
+ b->applyModifier(i->type - MOD_ATTRIBUTE, i->value, lifetime);
}
}
@@ -80,25 +77,14 @@ void ItemModifiers::cancelAttributes(Being *b) const
i_end = mModifiers.end(); i != i_end; ++i)
{
if (i->type < MOD_ATTRIBUTE) continue;
- b->removeEquipmentModifier(i->type - MOD_ATTRIBUTE, i->value);
+ b->applyModifier(i->type - MOD_ATTRIBUTE, -i->value);
}
}
bool ItemClass::use(Being *itemUser)
{
- bool usedSuccessfully = true;
- // Applying Modifiers for a given lifetime
- // TODO
-
- // Calling a script if scriptName != ""
- if (!mScriptName.empty())
- return (runScript(itemUser) && usedSuccessfully);
+ if (mType != ITEM_USABLE) return false;
- return usedSuccessfully;
-}
-
-bool ItemClass::runScript(Being *itemUser)
-{
- //TODO
+ mModifiers.applyAttributes(itemUser);
return true;
}
diff --git a/src/game-server/item.hpp b/src/game-server/item.hpp
index b2bbd303..84695429 100644
--- a/src/game-server/item.hpp
+++ b/src/game-server/item.hpp
@@ -24,7 +24,6 @@
#ifndef _TMWSERV_ITEM
#define _TMWSERV_ITEM
-#include <string>
#include <vector>
#include "game-server/object.hpp"
@@ -178,8 +177,8 @@ class ItemClass
{}
/**
- * The function called to use an item applying
- * only the modifiers (for simple items...)
+ * Applies the modifiers of an item to a given user.
+ * @return true if the item was sucessfully used and should be removed.
*/
bool use(Being *itemUser);
@@ -238,12 +237,6 @@ class ItemClass
{ mModifiers = modifiers; }
/**
- * Sets associated script name.
- */
- void setScriptName(std::string const &name)
- { mScriptName = name; }
-
- /**
* Gets database ID.
*/
int getDatabaseID()
@@ -263,11 +256,6 @@ class ItemClass
private:
- /**
- * Runs the associated script when using the item, if any.
- */
- bool runScript(Being *itemUser);
-
// Item reference information
unsigned short mDatabaseID;
unsigned short mSpriteID; /**< The sprite that should be shown to the character */
@@ -275,7 +263,6 @@ class ItemClass
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. */
ItemModifiers mModifiers; /**< Item modifiers. */
};
diff --git a/src/game-server/itemmanager.cpp b/src/game-server/itemmanager.cpp
index 3ee4ef48..2df0df7c 100644
--- a/src/game-server/itemmanager.cpp
+++ b/src/game-server/itemmanager.cpp
@@ -95,7 +95,7 @@ void ItemManager::initialize(std::string const &itemReferenceFile)
modifiers.setValue(MOD_WEAPON_RANGE, XML::getProperty(node, "range", 0));
modifiers.setValue(MOD_WEAPON_DAMAGE, XML::getProperty(node, "attack", 0));
modifiers.setValue(MOD_ELEMENT_TYPE, XML::getProperty(node, "element", 0));
- modifiers.setValue(MOD_LIFETIME, XML::getProperty(node, "lifetime", 0));
+ modifiers.setValue(MOD_LIFETIME, XML::getProperty(node, "lifetime", 0) * 10);
modifiers.setAttributeValue(BASE_ATTR_HP, XML::getProperty(node, "hp", 0));
// FIXME: decide on one single spelling for defense/defence
modifiers.setAttributeValue(BASE_ATTR_PHY_RES, XML::getProperty(node, "defense", 0));
@@ -134,7 +134,7 @@ void ItemManager::initialize(std::string const &itemReferenceFile)
item->setWeight(weight);
item->setCost(value);
item->setMaxPerSlot(maxPerSlot);
- item->setScriptName(scriptName);
+ //item->setScriptName(scriptName);
item->setModifiers(modifiers);
item->setSpriteID(sprite ? sprite : id);
itemClasses[id] = item;