diff options
author | Chuck Miller <shadowmil@gmail.com> | 2009-09-16 21:11:34 -0400 |
---|---|---|
committer | Chuck Miller <shadowmil@gmail.com> | 2009-09-16 21:11:34 -0400 |
commit | 1702f4fe35a59d6fbeb6864902da168e2138a592 (patch) | |
tree | 933da76597552d52e730de23ac8dff32d9551c89 /src/game-server | |
parent | b529177f03fffbe98b478672122ff87d25d74d3f (diff) | |
download | manaserv-1702f4fe35a59d6fbeb6864902da168e2138a592.tar.gz manaserv-1702f4fe35a59d6fbeb6864902da168e2138a592.tar.bz2 manaserv-1702f4fe35a59d6fbeb6864902da168e2138a592.tar.xz manaserv-1702f4fe35a59d6fbeb6864902da168e2138a592.zip |
Allow for basic scripted monsters
Diffstat (limited to 'src/game-server')
-rw-r--r-- | src/game-server/monster.cpp | 33 | ||||
-rw-r--r-- | src/game-server/monster.hpp | 12 |
2 files changed, 45 insertions, 0 deletions
diff --git a/src/game-server/monster.cpp b/src/game-server/monster.cpp index 2de90787..f7d2bac6 100644 --- a/src/game-server/monster.cpp +++ b/src/game-server/monster.cpp @@ -24,8 +24,10 @@ #include "game-server/character.hpp" #include "game-server/collisiondetection.hpp" #include "game-server/item.hpp" +#include "game-server/resourcemanager.hpp" #include "game-server/mapcomposite.hpp" #include "game-server/state.hpp" +#include "scripting/script.hpp" #include "utils/logger.h" #include <cmath> @@ -60,6 +62,7 @@ static MonsterTargetEventDispatch monsterTargetEventDispatch; Monster::Monster(MonsterClass *specy): Being(OBJECT_MONSTER), mSpecy(specy), + mScript(NULL), mCountDown(0), mTargetListener(&monsterTargetEventDispatch), mOwner(NULL), @@ -94,6 +97,10 @@ 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) @@ -152,6 +159,13 @@ void Monster::update() } return; } + else if(mScript) + { + mScript->setMap(getMap()); + mScript->prepare("update"); + mScript->push(this); + mScript->execute(); + } if (mAction == ATTACK) { @@ -276,6 +290,25 @@ void Monster::update() } } +void Monster::loadScript(std::string &scriptName) +{ + if (mScript) + return; // A script has already been loaded for this monster + + std::stringstream filename; + filename << "scripts/monster/" << scriptName; + if (ResourceManager::exists(filename.str())) // file exists! + { + LOG_INFO("Loading monster script: " << filename.str()); + mScript = Script::create("lua"); + mScript->loadFile(filename.str()); + } else { + LOG_WARN("Could not find script file \"" << filename.str() << "\" for monster"); + } + + +} + int Monster::calculatePositionPriority(Point position, int targetPriority) { Point thisPos = getPosition(); diff --git a/src/game-server/monster.hpp b/src/game-server/monster.hpp index 21b6cf2c..8dc2102b 100644 --- a/src/game-server/monster.hpp +++ b/src/game-server/monster.hpp @@ -23,11 +23,13 @@ #include <map> #include <vector> +#include <string> #include "game-server/being.hpp" #include "game-server/eventlistener.hpp" class ItemClass; +class Script; /** * Structure containing an item class and its probability to be dropped @@ -235,6 +237,11 @@ class Monster : public Being void perform(); /** + * Loads a script file for this monster + */ + void loadScript(std::string &scriptName); + + /** * */ virtual int getAttackType() const @@ -276,6 +283,11 @@ class Monster : public Being MonsterClass *mSpecy; + /** + * Stores script for the monster, null if no script exist + */ + Script *mScript; + /** Count down till next random movement (temporary). */ int mCountDown; |