From ba5b55f3eba0aa3898c5fe42de9838b22473c24a Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Tue, 28 Feb 2012 23:37:23 +0100 Subject: 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 --- src/game-server/character.cpp | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'src/game-server/character.cpp') 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. -- cgit v1.2.3-60-g2f50