summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Sehmisch <tmw@crushnet.org>2008-05-19 15:07:45 +0000
committerPhilipp Sehmisch <tmw@crushnet.org>2008-05-19 15:07:45 +0000
commit502d5c892c92b4a71eceae1d275032100d42dd23 (patch)
tree81f29c3ce0bda3d2a4221a485c9695d4e56eb568
parent4b4b6d0865f5f04f73e04926fb9e6b610f0a71a7 (diff)
downloadmanaserv-502d5c892c92b4a71eceae1d275032100d42dd23.tar.gz
manaserv-502d5c892c92b4a71eceae1d275032100d42dd23.tar.bz2
manaserv-502d5c892c92b4a71eceae1d275032100d42dd23.tar.xz
manaserv-502d5c892c92b4a71eceae1d275032100d42dd23.zip
Implemented NPC names. Implemented the theoretical possibility to have named monsters along the way. Note that the syntax of the LUA functions for creating NPCs has changed.
-rw-r--r--ChangeLog11
-rw-r--r--data/scripts/libtmw.lua8
-rw-r--r--data/test.lua8
-rw-r--r--src/game-server/being.hpp11
-rw-r--r--src/game-server/character.cpp6
-rw-r--r--src/game-server/character.hpp11
-rw-r--r--src/game-server/mapreader.cpp2
-rw-r--r--src/game-server/npc.cpp3
-rw-r--r--src/game-server/npc.hpp2
-rw-r--r--src/game-server/state.cpp2
-rw-r--r--src/scripting/lua.cpp18
-rw-r--r--src/scripting/script.cpp3
-rw-r--r--src/scripting/script.hpp7
13 files changed, 61 insertions, 31 deletions
diff --git a/ChangeLog b/ChangeLog
index 45cb551e..1dfe86e3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2008-05-19 Philipp Sehmisch <tmw@crushnet.org>
+
+ * src/game-server/being.cpp, src/game-server/character.cpp,
+ src/game-server/character.hpp, src/game-server/mapreader.cpp,
+ src/game-server/npc.hpp, src/game-server/npc.cpp,
+ src/game-server/state.cpp, src/scripting/script.cpp,
+ src/scripting/script.hpp, src/scripting/lua.cpp, data/scripts/libtmw.lua,
+ data/test.lua: Implemented NPC names. Implemented the theoretical
+ possibility to have named monsters along the way. Note that the syntax of
+ the LUA functions for creating NPCs has changed.
+
2008-05-18 Roderic Morris <roderic@ccs.neu.edu>
* src/game-server/command.cpp: Provide feedback when admin commands
diff --git a/data/scripts/libtmw.lua b/data/scripts/libtmw.lua
index 0eabf1bc..681b4340 100644
--- a/data/scripts/libtmw.lua
+++ b/data/scripts/libtmw.lua
@@ -38,8 +38,8 @@ local timer
-- Creates an NPC and associates the given handler.
-- Note: Cannot be called until map initialization has started.
-function create_npc(id, x, y, talkfunct, updatefunct)
- local npc = tmw.npc_create(id, x, y)
+function create_npc(name, id, x, y, talkfunct, updatefunct)
+ local npc = tmw.npc_create(name, id, x, y)
if talkfunct then npc_talk_functs[npc] = talkfunct end
if updatefunct then npc_update_functs[npc] = updatefunct end
end
@@ -228,10 +228,10 @@ end
-- Called by the game for creating NPCs embedded into maps.
-- Delays the creation until map initialization is performed.
-- Note: Assumes that the "npc_handler" global field contains the NPC handler.
-function create_npc_delayed(id, x, y)
+function create_npc_delayed(name, id, x, y)
-- Bind the name to a local variable first, as it will be reused.
local h = npc_handler
- atinit(function() create_npc(id, x, y, h, nil) end)
+ atinit(function() create_npc(name, id, x, y, h, nil) end)
npc_handler = nil
end
diff --git a/data/test.lua b/data/test.lua
index 06d6136d..944413a7 100644
--- a/data/test.lua
+++ b/data/test.lua
@@ -18,10 +18,10 @@
require "data/scripts/npclib"
atinit(function()
- create_npc(200, 50 * 32 + 16, 19 * 32 + 16, npc1_talk, npclib.walkaround_small)
- create_npc(201, 51 * 32 + 16, 25 * 32 + 16, npc4_talk, npclib.walkaround_wide)
- create_npc(126, 45 * 32 + 16, 25 * 32 + 16, npc5_talk, npclib.walkaround_map)
- create_npc(122, 58 * 32 + 16, 15 * 32 + 16, npc6_talk, npc6_update)
+ create_npc("Test NPC", 200, 50 * 32 + 16, 19 * 32 + 16, npc1_talk, npclib.walkaround_small)
+ create_npc("Teleporter", 201, 51 * 32 + 16, 25 * 32 + 16, npc4_talk, npclib.walkaround_wide)
+ create_npc("Spider Tamer", 126, 45 * 32 + 16, 25 * 32 + 16, npc5_talk, npclib.walkaround_map)
+ create_npc("Guard", 122, 58 * 32 + 16, 15 * 32 + 16, npc6_talk, npc6_update)
tmw.trigger_create(56 * 32, 32 * 32, 64, 64, "patrol_waypoint", 1, true)
tmw.trigger_create(63 * 32, 32 * 32, 64, 64, "patrol_waypoint", 2, true)
diff --git a/src/game-server/being.hpp b/src/game-server/being.hpp
index b725bfee..6c7ff398 100644
--- a/src/game-server/being.hpp
+++ b/src/game-server/being.hpp
@@ -218,6 +218,16 @@ class Being : public MovingObject
*/
virtual void modifiedAttribute(int) {}
+ /** Gets the name of the being. */
+ std::string const &
+ getName() const
+ { return mName; }
+
+ /** Sets the name of the being. */
+ void
+ setName(const std::string& name)
+ { mName = name; }
+
protected:
static const int TICKS_PER_HP_REGENERATION = 100;
Action mAction;
@@ -227,6 +237,7 @@ class Being : public MovingObject
Being(Being const &rhs);
Being &operator=(Being const &rhs);
+ std::string mName; /**< Name of the being. */
Hits mHitsTaken; /**< List of punches taken since last update */
AttributeModifiers mModifiers; /**< Currently modified attributes. */
int mHpRegenTimer; /**< timer for hp regeneration*/
diff --git a/src/game-server/character.cpp b/src/game-server/character.cpp
index e21de7c9..f169bd26 100644
--- a/src/game-server/character.cpp
+++ b/src/game-server/character.cpp
@@ -59,7 +59,7 @@ Character::Character(MessageIn &msg):
mExperience.resize(CHAR_SKILL_NB, 0);
// Get character data.
mDatabaseID = msg.readLong();
- mName = msg.readString();
+ setName(msg.readString());
deserializeCharacterData(*this, msg);
for (int i = CHAR_ATTR_BEGIN; i < CHAR_ATTR_END; ++i)
{
@@ -120,7 +120,7 @@ void Character::respawn()
{
if (mAction != DEAD)
{
- LOG_WARN("Character \""<<mName<<"\" tried to respawn without being dead");
+ LOG_WARN("Character \""<<getName()<<"\" tried to respawn without being dead");
return;
}
@@ -395,7 +395,7 @@ void Character::levelup()
levelupMsg.writeShort(mCharacterPoints);
levelupMsg.writeShort(mCorrectionPoints);
gameHandler->sendTo(this, levelupMsg);
- LOG_INFO(mName<<" reached level "<<mLevel);
+ LOG_INFO(getName()<<" reached level "<<mLevel);
}
AttribmodResponseCode Character::useCharacterPoint(size_t attribute)
diff --git a/src/game-server/character.hpp b/src/game-server/character.hpp
index 393a18d7..5e26b96c 100644
--- a/src/game-server/character.hpp
+++ b/src/game-server/character.hpp
@@ -139,16 +139,6 @@ class Character : public Being
setDatabaseID(int id)
{ mDatabaseID = id; }
- /** Gets the name of the character. */
- std::string const &
- getName() const
- { return mName; }
-
- /** Sets the name of the character. */
- void
- setName(const std::string& name)
- { mName = name; }
-
/** Gets the gender of the character (male or female). */
int
getGender() const
@@ -335,7 +325,6 @@ class Character : public Being
std::vector<unsigned int> mExperience; /**< experience collected for each skill.*/
- std::string mName; /**< Name of the character. */
int mDatabaseID; /**< Character's database ID. */
unsigned char mGender; /**< Gender of the character. */
unsigned char mHairStyle; /**< Hair Style of the character. */
diff --git a/src/game-server/mapreader.cpp b/src/game-server/mapreader.cpp
index 39d209d6..3d0a82ff 100644
--- a/src/game-server/mapreader.cpp
+++ b/src/game-server/mapreader.cpp
@@ -323,7 +323,7 @@ static Map *readMap(xmlNodePtr node, std::string const &path, MapComposite *comp
if (npcId != -1 && scriptText != NULL)
{
- s->loadNPC(npcId, objX, objY, scriptText);
+ s->loadNPC(objName, npcId, objX, objY, scriptText);
}
else
{
diff --git a/src/game-server/npc.cpp b/src/game-server/npc.cpp
index abc425ec..fd462c50 100644
--- a/src/game-server/npc.cpp
+++ b/src/game-server/npc.cpp
@@ -24,9 +24,10 @@
#include "game-server/npc.hpp"
#include "scripting/script.hpp"
-NPC::NPC(int id, Script *s):
+NPC::NPC(const std::string &name, int id, Script *s):
Being(OBJECT_NPC, 65535), mScript(s), mID(id)
{
+ setName(name);
}
void NPC::update()
diff --git a/src/game-server/npc.hpp b/src/game-server/npc.hpp
index 2d790c4c..c4d8b384 100644
--- a/src/game-server/npc.hpp
+++ b/src/game-server/npc.hpp
@@ -34,7 +34,7 @@ class Character;
class NPC : public Being
{
public:
- NPC(int id, Script *);
+ NPC(const std::string &name, int id, Script *);
void update();
diff --git a/src/game-server/state.cpp b/src/game-server/state.cpp
index 03c6e3c1..a129e889 100644
--- a/src/game-server/state.cpp
+++ b/src/game-server/state.cpp
@@ -273,12 +273,14 @@ static void informPlayer(MapComposite *map, Character *p)
{
Monster *q = static_cast< Monster * >(o);
enterMsg.writeShort(q->getSpecy()->getType());
+ enterMsg.writeString(q->getName());
} break;
case OBJECT_NPC:
{
NPC *q = static_cast< NPC * >(o);
enterMsg.writeShort(q->getNPC());
+ enterMsg.writeString(q->getName());
} break;
default:
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index a8663f75..9f160bca 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -68,6 +68,8 @@ class LuaScript: public Script
void push(int);
+ void push(const std::string &);
+
void push(Thing *);
int execute();
@@ -180,11 +182,11 @@ static int LuaNpc_Choice(lua_State *s)
/**
* Callback for creating a NPC on the current map with the current script.
- * tmw.npc_create(int id, int x, int y): npc
+ * tmw.npc_create(string name, int id, int x, int y): npc
*/
static int LuaNpc_Create(lua_State *s)
{
- if (!lua_isnumber(s, 1) || !lua_isnumber(s, 2) || !lua_isnumber(s, 3))
+ if (!lua_isstring(s, 1), !lua_isnumber(s, 2) || !lua_isnumber(s, 3) || !lua_isnumber(s, 4))
{
raiseScriptError(s, "npc_create called with incorrect parameters.");
return 0;
@@ -192,7 +194,7 @@ static int LuaNpc_Create(lua_State *s)
lua_pushlightuserdata(s, (void *)&registryKey);
lua_gettable(s, LUA_REGISTRYINDEX);
Script *t = static_cast<Script *>(lua_touserdata(s, -1));
- NPC *q = new NPC(lua_tointeger(s, 1), t);
+ NPC *q = new NPC(lua_tostring(s, 1), lua_tointeger(s, 2), t);
MapComposite *m = t->getMap();
if (!m)
{
@@ -200,7 +202,7 @@ static int LuaNpc_Create(lua_State *s)
return 0;
}
q->setMap(m);
- q->setPosition(Point(lua_tointeger(s, 2), lua_tointeger(s, 3)));
+ q->setPosition(Point(lua_tointeger(s, 3), lua_tointeger(s, 4)));
bool b = GameState::insert(q);
/* Do not try to deal with a failure there. There are some serious issues
if an insertion failed on an almost empty map. */
@@ -711,6 +713,14 @@ void LuaScript::push(int v)
++nbArgs;
}
+void LuaScript::push(std::string const &v)
+{
+ assert(nbArgs >= 0);
+ lua_pushstring(mState, v.c_str());
+ ++nbArgs;
+}
+
+
void LuaScript::push(Thing *v)
{
assert(nbArgs >= 0);
diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp
index 0ebc2b4a..408bf28a 100644
--- a/src/scripting/script.cpp
+++ b/src/scripting/script.cpp
@@ -77,10 +77,11 @@ void Script::loadFile(std::string const &name)
}
}
-void Script::loadNPC(int id, int x, int y, char const *prog)
+void Script::loadNPC(std::string const &name, int id, int x, int y, char const *prog)
{
load(prog);
prepare("create_npc_delayed");
+ push(name);
push(id);
push(x);
push(y);
diff --git a/src/scripting/script.hpp b/src/scripting/script.hpp
index 963c51f2..6a101edf 100644
--- a/src/scripting/script.hpp
+++ b/src/scripting/script.hpp
@@ -74,7 +74,7 @@ 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(int id, int x, int y, char const *);
+ virtual void loadNPC(std::string const &name, int id, int x, int y, char const *);
/**
* Called every tick for the script to manage its data.
@@ -94,6 +94,11 @@ class Script
virtual void push(int) = 0;
/**
+ * Pushes a string argument for the function being prepared.
+ */
+ virtual void push(std::string const &) = 0;
+
+ /**
* 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