diff options
author | Erik Schilling <ablu.erikschilling@googlemail.com> | 2013-02-04 16:19:21 +0100 |
---|---|---|
committer | Erik Schilling <ablu.erikschilling@googlemail.com> | 2013-02-04 17:43:24 +0100 |
commit | f7d71e54c0b5fdaa9e8aa1e69e615b9f80d124e4 (patch) | |
tree | 24a1d06ef633fe3a1bf4943846666f48aaf0c32a /src/game-server/being.cpp | |
parent | 5255be5a5457e287464606478df62300a44d8479 (diff) | |
download | manaserv-f7d71e54c0b5fdaa9e8aa1e69e615b9f80d124e4.tar.gz manaserv-f7d71e54c0b5fdaa9e8aa1e69e615b9f80d124e4.tar.bz2 manaserv-f7d71e54c0b5fdaa9e8aa1e69e615b9f80d124e4.tar.xz manaserv-f7d71e54c0b5fdaa9e8aa1e69e615b9f80d124e4.zip |
Moved attribute (re)calculation to the scripts
This introduces two callbacks:
- on_update_derived_attribute -> Called to recalculate other derived
attributes.
- on_recalculate_base_attribute -> Called to recalculate a base attribute
(only called for characters. However the
function passed as callback can be useful
for recalculating the derived attributes
as well)
Monsters no longer block recalculation of attributes except HP and Speed.
I saw no sense to keep this.
Fixed constant value in libmana-constants.lua
Dropped bool type of the recalculation functions. It would be difficult to
keep it while pushing all to the script engine and it was unused anyway.
All in all this adds a LOT more flexibillity to projects since they can now
adapt all attributes in the way they want.
Diffstat (limited to 'src/game-server/being.cpp')
-rw-r--r-- | src/game-server/being.cpp | 84 |
1 files changed, 39 insertions, 45 deletions
diff --git a/src/game-server/being.cpp b/src/game-server/being.cpp index d7df2df9..2c334b37 100644 --- a/src/game-server/being.cpp +++ b/src/game-server/being.cpp @@ -34,6 +34,11 @@ #include "game-server/statusmanager.h" #include "utils/logger.h" #include "utils/speedconv.h" +#include "scripting/scriptmanager.h" + + +Script::Ref Being::mRecalculateDerivedAttributesCallback; +Script::Ref Being::mRecalculateBaseAttributeCallback; Being::Being(EntityType type): Actor(type), @@ -574,75 +579,64 @@ void Being::setModAttribute(unsigned, double) return; } -bool Being::recalculateBaseAttribute(unsigned attr) +void Being::recalculateBaseAttribute(unsigned attr) { LOG_DEBUG("Being: Received update attribute recalculation request for " << attr << "."); if (!mAttributes.count(attr)) { LOG_DEBUG("Being::recalculateBaseAttribute: " << attr << " not found!"); - return false; + return; } - double newBase = getAttribute(attr); - switch (attr) + // Handle speed conversion inside the engine + if (attr == ATTR_MOVE_SPEED_RAW) { - case ATTR_HP_REGEN: - { - double hpPerSec = getModifiedAttribute(ATTR_VIT) * 0.05; - newBase = (hpPerSec * TICKS_PER_HP_REGENERATION / 10); - } - break; - case ATTR_HP: - double diff; - if ((diff = getModifiedAttribute(ATTR_HP) - - getModifiedAttribute(ATTR_MAX_HP)) > 0) - newBase -= diff; - break; - case ATTR_MAX_HP: - newBase = ((getModifiedAttribute(ATTR_VIT) + 3) - * (getModifiedAttribute(ATTR_VIT) + 20)) * 0.125; - break; - case ATTR_MOVE_SPEED_TPS: - newBase = 3.0 + getModifiedAttribute(ATTR_AGI) * 0.08; // Provisional. - break; - case ATTR_MOVE_SPEED_RAW: - newBase = utils::tpsToRawSpeed( - getModifiedAttribute(ATTR_MOVE_SPEED_TPS)); - break; - case ATTR_INV_CAPACITY: - // Provisional - newBase = 2000.0 + getModifiedAttribute(ATTR_STR) * 180.0; - break; - } - if (newBase != getAttribute(attr)) - { - setAttribute(attr, newBase); - return true; + double newBase = utils::tpsToRawSpeed( + getModifiedAttribute(ATTR_MOVE_SPEED_TPS)); + if (newBase != getAttribute(attr)) + setAttribute(attr, newBase); + return; } - LOG_DEBUG("Being: No changes to sync for attribute '" << attr << "'."); - return false; + + if (!mRecalculateBaseAttributeCallback.isValid()) + return; + + Script *script = ScriptManager::currentState(); + script->setMap(getMap()); + script->prepare(mRecalculateBaseAttributeCallback); + script->push(this); + script->push(attr); + script->execute(); } void Being::updateDerivedAttributes(unsigned attr) { LOG_DEBUG("Being: Updating derived attribute(s) of: " << attr); + + // Handle default actions before handing over to the script engine switch (attr) { case ATTR_MAX_HP: - updateDerivedAttributes(ATTR_HP); case ATTR_HP: raiseUpdateFlags(UPDATEFLAG_HEALTHCHANGE); break; case ATTR_MOVE_SPEED_TPS: - if (getAttribute(attr) > 0.0f) - setAttribute(ATTR_MOVE_SPEED_RAW, utils::tpsToRawSpeed( - getModifiedAttribute(ATTR_MOVE_SPEED_TPS))); - break; - default: - // Do nothing + // Does not make a lot of sense to have in the scripts. + // So handle it here: + recalculateBaseAttribute(ATTR_MOVE_SPEED_RAW); break; } + + if (!mRecalculateDerivedAttributesCallback.isValid()) + return; + + Script *script = ScriptManager::currentState(); + script->setMap(getMap()); + script->prepare(mRecalculateDerivedAttributesCallback); + script->push(this); + script->push(attr); + script->execute(); } void Being::applyStatusEffect(int id, int timer) |