summaryrefslogtreecommitdiff
path: root/src/scripting
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/scripting
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/scripting')
-rw-r--r--src/scripting/lua.cpp60
-rw-r--r--src/scripting/luascript.cpp13
-rw-r--r--src/scripting/luascript.h2
-rw-r--r--src/scripting/script.cpp6
-rw-r--r--src/scripting/script.h13
5 files changed, 54 insertions, 40 deletions
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index a416771f..273bdf42 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -112,20 +112,6 @@ static int on_update(lua_State *s)
return 0;
}
-static int on_npc_start(lua_State *s)
-{
- luaL_checktype(s, 1, LUA_TFUNCTION);
- NPC::setStartCallback(getScript(s));
- return 0;
-}
-
-static int on_npc_update(lua_State *s)
-{
- luaL_checktype(s, 1, LUA_TFUNCTION);
- NPC::setUpdateCallback(getScript(s));
- return 0;
-}
-
static int on_create_npc_delayed(lua_State *s)
{
luaL_checktype(s, 1, LUA_TFUNCTION);
@@ -316,8 +302,10 @@ static int npc_ask_string(lua_State *s)
}
/**
- * mana.npc_create(string name, int id, int gender, int x, int y): NPC*
- * Callback for creating a NPC on the current map with the current script.
+ * mana.npc_create(string name, int id, int gender, int x, int y,
+ * function talk, function update): NPC*
+ *
+ * Callback for creating a NPC on the current map.
*/
static int npc_create(lua_State *s)
{
@@ -327,33 +315,36 @@ static int npc_create(lua_State *s)
const int x = luaL_checkint(s, 4);
const int y = luaL_checkint(s, 5);
+ if (!lua_isnoneornil(s, 6))
+ luaL_checktype(s, 6, LUA_TFUNCTION);
+ if (!lua_isnoneornil(s, 7))
+ luaL_checktype(s, 7, LUA_TFUNCTION);
+
MapComposite *m = checkCurrentMap(s);
NPC *q = new NPC(name, id);
q->setGender(getGender(gender));
q->setMap(m);
q->setPosition(Point(x, y));
+
+ if (lua_isfunction(s, 6))
+ {
+ lua_pushvalue(s, 6);
+ q->setTalkCallback(luaL_ref(s, LUA_REGISTRYINDEX));
+ }
+
+ if (lua_isfunction(s, 7))
+ {
+ lua_pushvalue(s, 7);
+ q->setUpdateCallback(luaL_ref(s, LUA_REGISTRYINDEX));
+ }
+
GameState::enqueueInsert(q);
lua_pushlightuserdata(s, q);
return 1;
}
/**
- * mana.npc_end(NPC*, Character*): void
- * Callback for ending a NPC conversation with the given character.
- */
-static int npc_end(lua_State *s)
-{
- NPC *p = checkNPC(s, 1);
- Character *q = checkCharacter(s, 2);
-
- MessageOut msg(GPMSG_NPC_CLOSE);
- msg.writeInt16(p->getPublicID());
- gameHandler->sendTo(q, msg);
- return 0;
-}
-
-/**
* mana.npc_post(NPC*, Character*): void
* Callback for sending a NPC_POST.
*/
@@ -376,7 +367,7 @@ static int npc_post(lua_State *s)
static int npc_enable(lua_State *s)
{
NPC *p = checkNPC(s, 1);
- p->enable(true);
+ p->setEnabled(true);
GameState::enqueueInsert(p);
return 0;
}
@@ -388,7 +379,7 @@ static int npc_enable(lua_State *s)
static int npc_disable(lua_State *s)
{
NPC *p = checkNPC(s, 1);
- p->enable(false);
+ p->setEnabled(false);
GameState::remove(p);
return 0;
}
@@ -2152,8 +2143,6 @@ LuaScript::LuaScript():
{ "on_being_death", &on_being_death },
{ "on_being_remove", &on_being_remove },
{ "on_update", &on_update },
- { "on_npc_start", &on_npc_start },
- { "on_npc_update", &on_npc_update },
{ "on_create_npc_delayed", &on_create_npc_delayed },
{ "on_map_initialize", &on_map_initialize },
{ "on_craft", &on_craft },
@@ -2243,7 +2232,6 @@ LuaScript::LuaScript():
{ "item_drop", &item_drop },
{ "item_get_name", &item_get_name },
{ "npc_ask_integer", &npc_ask_integer },
- { "npc_end", &npc_end },
{ "npc_ask_string", &npc_ask_string },
{ "log", &log },
{ "get_distance", &get_distance },
diff --git a/src/scripting/luascript.cpp b/src/scripting/luascript.cpp
index e45588b9..36adb912 100644
--- a/src/scripting/luascript.cpp
+++ b/src/scripting/luascript.cpp
@@ -198,6 +198,15 @@ void LuaScript::assignCallback(Script::Ref &function)
function.value = luaL_ref(mRootState, LUA_REGISTRYINDEX);
}
+void LuaScript::unref(Ref &ref)
+{
+ if (ref.isValid())
+ {
+ luaL_unref(mRootState, LUA_REGISTRYINDEX, ref.value);
+ ref.value = -1;
+ }
+}
+
void LuaScript::load(const char *prog, const char *name)
{
int res = luaL_loadbuffer(mRootState, prog, std::strlen(prog), name);
@@ -262,7 +271,7 @@ void LuaScript::getQuestCallback(Character *q,
script->prepareResume(thread);
script->push(value);
- script->resume();
+ q->resumeNpcThread();
}
/**
@@ -280,7 +289,7 @@ void LuaScript::getPostCallback(Character *q,
script->prepareResume(thread);
script->push(sender);
script->push(letter);
- script->resume();
+ q->resumeNpcThread();
}
diff --git a/src/scripting/luascript.h b/src/scripting/luascript.h
index 57b8dc1a..955fe21c 100644
--- a/src/scripting/luascript.h
+++ b/src/scripting/luascript.h
@@ -64,6 +64,8 @@ class LuaScript : public Script
void assignCallback(Ref &function);
+ void unref(Ref &ref);
+
static void getQuestCallback(Character *,
const std::string &value,
Script *);
diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp
index 6312d1ce..074546ef 100644
--- a/src/scripting/script.cpp
+++ b/src/scripting/script.cpp
@@ -111,7 +111,10 @@ bool Script::loadFile(const std::string &name)
}
}
-void Script::loadNPC(const std::string &name, int id, int x, int y,
+void Script::loadNPC(const std::string &name,
+ int id,
+ ManaServ::BeingGender gender,
+ int x, int y,
const char *prog)
{
if (!mCreateNpcDelayedCallback.isValid())
@@ -124,6 +127,7 @@ void Script::loadNPC(const std::string &name, int id, int x, int y,
prepare(mCreateNpcDelayedCallback);
push(name);
push(id);
+ push(gender);
push(x);
push(y);
execute();
diff --git a/src/scripting/script.h b/src/scripting/script.h
index b55f204e..f6d6d180 100644
--- a/src/scripting/script.h
+++ b/src/scripting/script.h
@@ -22,6 +22,7 @@
#define SCRIPTING_SCRIPT_H
#include "common/inventorydata.h"
+#include "common/manaserv_protocol.h"
#include "game-server/eventlistener.h"
#include <list>
@@ -61,6 +62,7 @@ class Script
{
public:
Ref() : value(-1) {}
+ Ref(int value) : value(value) {}
bool isValid() const { return value != -1; }
int value;
};
@@ -111,7 +113,10 @@ class Script
* Loads a chunk of text and considers it as an NPC handler. This
* handler will later be used to create the given NPC.
*/
- virtual void loadNPC(const std::string &name, int id, int x, int y,
+ virtual void loadNPC(const std::string &name,
+ int id,
+ ManaServ::BeingGender gender,
+ int x, int y,
const char *);
/**
@@ -187,6 +192,12 @@ class Script
virtual void assignCallback(Ref &function) = 0;
/**
+ * Unreferences the script object given by \a ref, if any, and sets
+ * \a ref to invalid.
+ */
+ virtual void unref(Ref &ref) = 0;
+
+ /**
* Returns the currently executing thread, or null when no thread is
* currently executing.
*/