diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-02-26 22:06:10 +0100 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2012-03-02 18:12:07 +0100 |
commit | 34ac0d64e23f2b2d3981dbb0ea72157f334805dd (patch) | |
tree | 114b1f7a65956c097f7a59078292c9fa29c6451f /src/game-server/monster.cpp | |
parent | e896d0b0125b48e12d43d99ace4498e56d968d50 (diff) | |
download | manaserv-34ac0d64e23f2b2d3981dbb0ea72157f334805dd.tar.gz manaserv-34ac0d64e23f2b2d3981dbb0ea72157f334805dd.tar.xz manaserv-34ac0d64e23f2b2d3981dbb0ea72157f334805dd.zip |
Merged all the different Lua states into one
No more Lua state for each status effect, monster, item effect or map. All
scripts are loaded into the same state. This should be more efficient overall
and make it easier to implement dynamic reloading of the scripts in the
future.
Now, this introduces the problem of name collisions between different Lua
scripts. For now this is solved by using more specific function names, like
'tick_plague' and 'tick_jump' rather than just 'tick'. The plan is however
to get rid of these globals, and register these callbacks from the script,
so that they can be local functions without the danger of colliding with
other scripts.
Reviewed-by: Erik Schilling
Reviewed-by: Yohann Ferreira
Diffstat (limited to 'src/game-server/monster.cpp')
-rw-r--r-- | src/game-server/monster.cpp | 42 |
1 files changed, 15 insertions, 27 deletions
diff --git a/src/game-server/monster.cpp b/src/game-server/monster.cpp index 6e40fb2..21eeea7 100644 --- a/src/game-server/monster.cpp +++ b/src/game-server/monster.cpp @@ -29,6 +29,7 @@ #include "game-server/mapcomposite.h" #include "game-server/state.h" #include "scripting/script.h" +#include "scripting/scriptmanager.h" #include "utils/logger.h" #include "utils/speedconv.h" @@ -49,7 +50,6 @@ static MonsterTargetEventDispatch monsterTargetEventDispatch; Monster::Monster(MonsterClass *specy): Being(OBJECT_MONSTER), mSpecy(specy), - mScript(NULL), mTargetListener(&monsterTargetEventDispatch), mOwner(NULL), mCurrentAttack(NULL) @@ -108,10 +108,6 @@ Monster::Monster(MonsterClass *specy): Monster::~Monster() { - // Remove the monster's script if it has one - if (mScript) - delete mScript; - // Remove death listeners. for (std::map<Being *, int>::iterator i = mAnger.begin(), i_end = mAnger.end(); i != i_end; ++i) @@ -145,15 +141,15 @@ void Monster::perform() int hit = performAttack(mTarget, dmg); if (! mCurrentAttack->scriptFunction.empty() - && mScript && hit > -1) { - mScript->setMap(getMap()); - mScript->prepare(mCurrentAttack->scriptFunction); - mScript->push(this); - mScript->push(mTarget); - mScript->push(hit); - mScript->execute(); + Script *script = ScriptManager::currentState(); + script->setMap(getMap()); + script->prepare(mCurrentAttack->scriptFunction); + script->push(this); + script->push(mTarget); + script->push(hit); + script->execute(); } } } @@ -183,13 +179,12 @@ void Monster::update() } return; } - else if(mScript) - { - mScript->setMap(getMap()); - mScript->prepare("update"); - mScript->push(this); - mScript->execute(); - } + + Script *script = ScriptManager::currentState(); + script->setMap(getMap()); + script->prepare("update_monster"); + script->push(this); + script->execute(); // Cancel the rest when we are currently performing an attack if (isTimerRunning(T_M_ATTACK_TIME)) @@ -324,10 +319,6 @@ void Monster::update() void Monster::loadScript(const std::string &scriptName) { - // A script may have already been loaded for this monster - delete mScript; - mScript = 0; - if (scriptName.length() == 0) return; @@ -336,10 +327,7 @@ void Monster::loadScript(const std::string &scriptName) if (ResourceManager::exists(filename.str())) { LOG_INFO("Loading monster script: " << filename.str()); - std::string engineName = - Script::determineEngineByFilename(filename.str()); - mScript = Script::create(engineName); - mScript->loadFile(filename.str()); + ScriptManager::currentState()->loadFile(filename.str()); } else { |