summaryrefslogtreecommitdiff
path: root/src/game-server/character.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-02-28 23:37:23 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-03-02 18:12:20 +0100
commitba5b55f3eba0aa3898c5fe42de9838b22473c24a (patch)
tree465e70d3bc4671b75f1af763d866eafa459f5d6f /src/game-server/character.cpp
parent05bb880a3b0ebe401bac83b7a2400aa315161f9c (diff)
downloadmanaserv-ba5b55f3eba0aa3898c5fe42de9838b22473c24a.tar.gz
manaserv-ba5b55f3eba0aa3898c5fe42de9838b22473c24a.tar.bz2
manaserv-ba5b55f3eba0aa3898c5fe42de9838b22473c24a.tar.xz
manaserv-ba5b55f3eba0aa3898c5fe42de9838b22473c24a.zip
Use callbacks for handling character death and respawn
Rather than relying on the availability of global functions with certain predefined names, the Lua script now calls API functions to set which function should be called on these global events. This mechanism should make it easier to avoid name collisions in the global namespace, which is important now that there is only a single script state. For these global events this was not likely to become a problem, but this solution can also be used for callbacks on specific item or monster types, or even allow setting callbacks on certain instances. Reviewed-by: Erik Schilling Reviewed-by: Yohann Ferreira
Diffstat (limited to 'src/game-server/character.cpp')
-rw-r--r--src/game-server/character.cpp26
1 files changed, 22 insertions, 4 deletions
diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp
index 819da1c1..b5051764 100644
--- a/src/game-server/character.cpp
+++ b/src/game-server/character.cpp
@@ -52,6 +52,24 @@ const float Character::EXPCURVE_FACTOR = 10.0f;
const float Character::LEVEL_SKILL_PRECEDENCE_FACTOR = 0.75f;
const float Character::EXP_LEVEL_FLEXIBILITY = 1.0f;
+Script::Ref Character::mDeathCallback = Script::NoRef;
+Script::Ref Character::mDeathAcceptedCallback = Script::NoRef;
+
+static bool executeCallback(Script::Ref function, Character *character)
+{
+ if (function == Script::NoRef)
+ return false;
+
+ Script *script = ScriptManager::currentState();
+ script->setMap(character->getMap());
+ script->prepare(function);
+ script->push(character);
+ script->execute();
+ script->setMap(0);
+ return true;
+}
+
+
Character::Character(MessageIn &msg):
Being(OBJECT_CHARACTER),
mClient(NULL),
@@ -197,7 +215,7 @@ void Character::perform()
void Character::died()
{
Being::died();
- ScriptManager::executeGlobalEventFunction("on_chr_death", this);
+ executeCallback(mDeathCallback, this);
}
void Character::respawn()
@@ -214,11 +232,11 @@ void Character::respawn()
// Reset target
mTarget = NULL;
- // Execute respawn script
- if (ScriptManager::executeGlobalEventFunction("on_chr_death_accept", this))
+ // Execute respawn callback when set
+ if (executeCallback(mDeathAcceptedCallback, this))
return;
- // Script-controlled respawning didn't work - fall back to hardcoded logic.
+ // No script respawn callback set - fall back to hardcoded logic
mAttributes[ATTR_HP].setBase(mAttributes[ATTR_MAX_HP].getModifiedAttribute());
updateDerivedAttributes(ATTR_HP);
// Warp back to spawn point.