summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog2
-rw-r--r--src/Makefile.am1
-rw-r--r--src/game-server/npc.cpp58
-rw-r--r--src/game-server/npc.hpp14
-rw-r--r--src/game-server/testing.cpp36
-rw-r--r--src/scripting/lua.cpp17
-rw-r--r--src/scripting/script.hpp12
7 files changed, 89 insertions, 51 deletions
diff --git a/ChangeLog b/ChangeLog
index 38fd6c4..e220fc5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -10,6 +10,8 @@
scripting interface.
* src/configure.ac, src/Makefile.am, src/scripting/lua.cpp: Added Lua
scripting engine.
+ * src/game-server/npc.cpp, src/game-server/npc.hpp,
+ src/game-server/testing.cpp: Converted NPC class to scripting engine.
2007-08-08 Guillaume Melquiond <guillaume.melquiond@gmail.com>
diff --git a/src/Makefile.am b/src/Makefile.am
index bbc64b8..588f3ca 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -112,6 +112,7 @@ tmwserv_game_SOURCES = \
game-server/movingobject.hpp \
game-server/movingobject.cpp \
game-server/npc.hpp \
+ game-server/npc.cpp \
game-server/object.hpp \
game-server/spawnarea.hpp \
game-server/spawnarea.cpp \
diff --git a/src/game-server/npc.cpp b/src/game-server/npc.cpp
new file mode 100644
index 0000000..abc425e
--- /dev/null
+++ b/src/game-server/npc.cpp
@@ -0,0 +1,58 @@
+/*
+ * The Mana World Server
+ * Copyright 2007 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with The Mana World; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+#include "game-server/character.hpp"
+#include "game-server/npc.hpp"
+#include "scripting/script.hpp"
+
+NPC::NPC(int id, Script *s):
+ Being(OBJECT_NPC, 65535), mScript(s), mID(id)
+{
+}
+
+void NPC::update()
+{
+ if (!mScript) return;
+ mScript->prepare("npc_update");
+ mScript->push(this);
+ mScript->execute();
+}
+
+void NPC::prompt(Character *ch, bool restart)
+{
+ if (!mScript) return;
+ mScript->prepare(restart ? "npc_start" : "npc_next");
+ mScript->push(this);
+ mScript->push(ch);
+ mScript->execute();
+}
+
+void NPC::select(Character *ch, int v)
+{
+ if (!mScript) return;
+ mScript->prepare("npc_choose");
+ mScript->push(this);
+ mScript->push(ch);
+ mScript->push(v);
+ mScript->execute();
+}
+
diff --git a/src/game-server/npc.hpp b/src/game-server/npc.hpp
index 44bbf4b..7c909d2 100644
--- a/src/game-server/npc.hpp
+++ b/src/game-server/npc.hpp
@@ -25,27 +25,28 @@
#include "game-server/being.hpp"
+class Script;
+class Character;
+
/**
* Class describing a non-player character.
*/
class NPC : public Being
{
public:
- NPC(int id): Being(OBJECT_NPC, 65535), mID(id) {}
+ NPC(int id, Script *);
- void update() {}
+ void update();
/**
* Prompts NPC.
- * TODO: should not be virtual, should invoke a scripting engine instead.
*/
- virtual void prompt(Character *, bool restart) = 0;
+ void prompt(Character *, bool restart);
/**
* Selects an NPC proposition.
- * TODO: should not be virtual, should invoke a scripting engine instead.
*/
- virtual void select(Character *, int) = 0;
+ void select(Character *, int);
/**
* Gets NPC ID.
@@ -54,6 +55,7 @@ class NPC : public Being
{ return mID; }
private:
+ Script *mScript; /**< Script describing NPC behavior. */
unsigned short mID; /**< ID of the NPC. */
};
diff --git a/src/game-server/testing.cpp b/src/game-server/testing.cpp
index 7b875ea..1915620 100644
--- a/src/game-server/testing.cpp
+++ b/src/game-server/testing.cpp
@@ -13,43 +13,13 @@
#include "game-server/npc.hpp"
#include "game-server/state.hpp"
#include "net/messageout.hpp"
+#include "scripting/script.hpp"
// For testing purpose only, the NPC class is not meant to be inherited!!
struct DummyNPC: NPC
{
- DummyNPC(): NPC(110) {}
-
- void prompt(Character *q, bool restart)
- {
- if (restart)
- {
- MessageOut msg(GPMSG_NPC_MESSAGE);
- msg.writeShort(getPublicID());
- std::string text = "What do you want?";
- msg.writeString(text, text.length());
- gameHandler->sendTo(q, msg);
- }
- else
- {
- MessageOut msg(GPMSG_NPC_CHOICE);
- msg.writeShort(getPublicID());
- std::string text = "Guns! Lots of guns!:Nothing";
- msg.writeString(text, text.length());
- gameHandler->sendTo(q, msg);
- }
- }
-
- void select(Character *q, int c)
- {
- if (c == 1)
- {
- MessageOut msg(GPMSG_NPC_MESSAGE);
- msg.writeShort(getPublicID());
- std::string text = "Sorry, this is a heroic-fantasy game, I do not have any gun.";
- msg.writeString(text, text.length());
- gameHandler->sendTo(q, msg);
- }
- }
+ DummyNPC(): NPC(110, Script::create("lua", "test.lua"))
+ {}
};
static void dropItem(MapComposite *map, int x, int y, int type)
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index b22d287..099e326 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -47,7 +47,7 @@ class LuaScript: public Script
void push(int);
- void push(Character *);
+ void push(Thing *);
int execute();
@@ -90,8 +90,11 @@ void LuaScript::push(int v)
++nbArgs;
}
-void LuaScript::push(Character *v)
+void LuaScript::push(Thing *v)
{
+ assert(nbArgs >= 0);
+ lua_pushlightuserdata(mState, v);
+ ++nbArgs;
}
int LuaScript::execute()
@@ -99,13 +102,15 @@ int LuaScript::execute()
assert(nbArgs >= 0);
int res = lua_pcall(mState, nbArgs, 1, 0);
nbArgs = -1;
- if (res || !lua_isnumber(mState, 0))
+ if (res || !lua_isnumber(mState, 1))
{
- LOG_ERROR("Failure while calling Lua function: "
- << lua_tostring(mState, 0));
+ LOG_WARN("Failure while calling Lua function: error=" << res
+ << ", type=" << lua_typename(mState, lua_type(mState, 1))
+ << ", message=" << lua_tostring(mState, 1));
+ lua_pop(mState, 1);
return 0;
}
- res = lua_tointeger(mState, 0);
+ res = lua_tointeger(mState, 1);
lua_pop(mState, 1);
return res;
}
diff --git a/src/scripting/script.hpp b/src/scripting/script.hpp
index c167dda..5862e50 100644
--- a/src/scripting/script.hpp
+++ b/src/scripting/script.hpp
@@ -26,7 +26,7 @@
#include <string>
-class Character;
+class Thing;
/**
* Abstract interface for calling functions written in an external language.
@@ -67,12 +67,12 @@ class Script
virtual void push(int) = 0;
/**
- * Pushes a pointer to Character for the function being prepared.
- * It usually is the character doing the action. The interface can
- * pass the pointer as an opaque value to the scripting engine, if
- * needed.
+ * Pushes a pointer argument to a game entity.
+ * The interface can pass the pointer as an opaque value to the
+ * scripting engine, if needed. This value will usually be passed
+ * by the script to some callabck functions.
*/
- virtual void push(Character *) = 0;
+ virtual void push(Thing *) = 0;
/**
* Executes the function being prepared.