summaryrefslogtreecommitdiff
path: root/src/game-server/npc.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-03-10 23:38:36 +0100
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2012-03-11 16:56:56 +0100
commitb822dcee52d15d41c4186a250e73b85b16c9dc39 (patch)
tree45515c75175b67fe458701f3a70bf0ee8b14bee5 /src/game-server/npc.cpp
parent2dd3c5c06978584e3e076609554f225ffbabd3e2 (diff)
downloadmanaserv-b822dcee52d15d41c4186a250e73b85b16c9dc39.tar.gz
manaserv-b822dcee52d15d41c4186a250e73b85b16c9dc39.tar.bz2
manaserv-b822dcee52d15d41c4186a250e73b85b16c9dc39.tar.xz
manaserv-b822dcee52d15d41c4186a250e73b85b16c9dc39.zip
Removed the create_npc wrapper and the last two NPC callbacks
When creating an NPC, you now provide its optional talk and update functions directly rather than them being stored in a table on the Lua side and then called in response to a global callback. Also fixed an issue with a missing gender parameter to the delayed NPC creation callback used by NPCs defined on the map (found by Erik while reviewing this patch). Reviewed-by: Erik Schilling
Diffstat (limited to 'src/game-server/npc.cpp')
-rw-r--r--src/game-server/npc.cpp46
1 files changed, 29 insertions, 17 deletions
diff --git a/src/game-server/npc.cpp b/src/game-server/npc.cpp
index aa032c98..5583c3d5 100644
--- a/src/game-server/npc.cpp
+++ b/src/game-server/npc.cpp
@@ -19,13 +19,12 @@
*/
#include "game-server/character.h"
+#include "game-server/gamehandler.h"
#include "game-server/npc.h"
+#include "net/messageout.h"
#include "scripting/script.h"
#include "scripting/scriptmanager.h"
-Script::Ref NPC::mStartCallback;
-Script::Ref NPC::mUpdateCallback;
-
NPC::NPC(const std::string &name, int id):
Being(OBJECT_NPC),
mID(id),
@@ -34,7 +33,14 @@ NPC::NPC(const std::string &name, int id):
setName(name);
}
-void NPC::enable(bool enabled)
+NPC::~NPC()
+{
+ Script *script = ScriptManager::currentState();
+ script->unref(mTalkCallback);
+ script->unref(mUpdateCallback);
+}
+
+void NPC::setEnabled(bool enabled)
{
mEnabled = enabled;
}
@@ -52,7 +58,7 @@ void NPC::update()
void NPC::prompt(Character *ch, bool restart)
{
- if (!mEnabled || !mStartCallback.isValid())
+ if (!mEnabled || !mTalkCallback.isValid())
return;
Script *script = ScriptManager::currentState();
@@ -61,12 +67,10 @@ void NPC::prompt(Character *ch, bool restart)
{
Script::Thread *thread = script->newThread();
thread->mMap = getMap();
- script->prepare(mStartCallback);
+ script->prepare(mTalkCallback);
script->push(this);
script->push(ch);
-
- if (!script->resume())
- ch->setNpcThread(thread);
+ ch->startNpcThread(thread, getPublicID());
}
else
{
@@ -75,8 +79,7 @@ void NPC::prompt(Character *ch, bool restart)
return;
script->prepareResume(thread);
- if (script->resume())
- ch->setNpcThread(0);
+ ch->resumeNpcThread();
}
}
@@ -92,8 +95,7 @@ void NPC::select(Character *ch, int index)
Script *script = ScriptManager::currentState();
script->prepareResume(thread);
script->push(index);
- if (script->resume())
- ch->setNpcThread(0);
+ ch->resumeNpcThread();
}
void NPC::integerReceived(Character *ch, int value)
@@ -108,8 +110,7 @@ void NPC::integerReceived(Character *ch, int value)
Script *script = ScriptManager::currentState();
script->prepareResume(thread);
script->push(value);
- if (script->resume())
- ch->setNpcThread(0);
+ ch->resumeNpcThread();
}
void NPC::stringReceived(Character *ch, const std::string &value)
@@ -124,6 +125,17 @@ void NPC::stringReceived(Character *ch, const std::string &value)
Script *script = ScriptManager::currentState();
script->prepareResume(thread);
script->push(value);
- if (script->resume())
- ch->setNpcThread(0);
+ ch->resumeNpcThread();
+}
+
+void NPC::setTalkCallback(Script::Ref function)
+{
+ ScriptManager::currentState()->unref(mTalkCallback);
+ mTalkCallback = function;
+}
+
+void NPC::setUpdateCallback(Script::Ref function)
+{
+ ScriptManager::currentState()->unref(mUpdateCallback);
+ mUpdateCallback = function;
}