diff options
-rw-r--r-- | src/game-server/character.cpp | 20 | ||||
-rw-r--r-- | src/game-server/character.hpp | 5 | ||||
-rw-r--r-- | src/scripting/script.cpp | 17 | ||||
-rw-r--r-- | src/scripting/script.hpp | 5 |
4 files changed, 34 insertions, 13 deletions
diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp index 60b767a3..d45edd15 100644 --- a/src/game-server/character.cpp +++ b/src/game-server/character.cpp @@ -177,6 +177,12 @@ void Character::perform() } +void Character::died() +{ + Being::died(); + Script::execute_global_event_function("on_chr_death", this); +} + void Character::respawn() { if (mAction != DEAD) @@ -191,19 +197,7 @@ void Character::respawn() mTarget = NULL; // execute respawn script - bool isScriptHandled = false; - Script *script = Script::global_event_script; - if (script) - { - script->setMap(getMap()); - script->prepare("on_chr_death_accept"); - script->push(this); - script->execute(); - isScriptHandled = true; // TODO: don't set to true when execution failed - script->setMap(NULL); - } - - if (!isScriptHandled) + if (!Script::execute_global_event_function("on_chr_death_accept", this)) { // script-controlled respawning didn't work - fall back to // hardcoded logic diff --git a/src/game-server/character.hpp b/src/game-server/character.hpp index 714df772..056b39f1 100644 --- a/src/game-server/character.hpp +++ b/src/game-server/character.hpp @@ -74,6 +74,11 @@ class Character : public Being void perform(); /** + * Executes the global die script and calls the base class function + */ + virtual void died(); + + /** * makes the character respawn */ void respawn(); diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp index 12c4417a..06f99dfc 100644 --- a/src/scripting/script.cpp +++ b/src/scripting/script.cpp @@ -23,6 +23,7 @@ #include "scripting/script.hpp" +#include "game-server/being.hpp" #include "game-server/resourcemanager.hpp" #include "utils/logger.h" @@ -95,3 +96,19 @@ void Script::loadNPC(const std::string &name, int id, int x, int y, push(y); execute(); } + +bool Script::execute_global_event_function(const std::string &function, Being* obj) +{ + bool isScriptHandled = false; + Script *script = Script::global_event_script; + if (script) + { + script->setMap(obj->getMap()); + script->prepare(function); + script->push(obj); + script->execute(); + script->setMap(NULL); + isScriptHandled = true; // TODO: don't set to true when execution failed + } + return isScriptHandled; +} diff --git a/src/scripting/script.hpp b/src/scripting/script.hpp index 74925e8b..2c8481dd 100644 --- a/src/scripting/script.hpp +++ b/src/scripting/script.hpp @@ -131,6 +131,11 @@ class Script virtual void processRemoveEvent(Thing* thing) = 0; + /** + * Runs a function in the global event script file + */ + static bool execute_global_event_function(const std::string &function, Being *obj); + static Script* global_event_script; // the global event script protected: |