summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilipp Sehmisch <tmw@crushnet.org>2008-03-15 02:39:52 +0000
committerPhilipp Sehmisch <tmw@crushnet.org>2008-03-15 02:39:52 +0000
commit20cc45b27ecb5264b1e71e99e233b8e4b4aaea50 (patch)
tree37a13fdeac32b60bcccea8362ddf26ca53c2428e
parent68c0625ee9a3a01090f0bc93612bf84dd71eaa67 (diff)
downloadmanaserv-20cc45b27ecb5264b1e71e99e233b8e4b4aaea50.tar.gz
manaserv-20cc45b27ecb5264b1e71e99e233b8e4b4aaea50.tar.bz2
manaserv-20cc45b27ecb5264b1e71e99e233b8e4b4aaea50.tar.xz
manaserv-20cc45b27ecb5264b1e71e99e233b8e4b4aaea50.zip
Added pathblocking rules to NPCs, added script bindings to query being positions and created script infrastructure for regularily called script functions. This allows NPCs which wander around automatically.
-rw-r--r--ChangeLog12
-rw-r--r--data/scripts/libtmw.lua16
-rw-r--r--data/test.lua41
-rw-r--r--src/game-server/npc.hpp14
-rw-r--r--src/scripting/lua.cpp30
5 files changed, 90 insertions, 23 deletions
diff --git a/ChangeLog b/ChangeLog
index 77ea9585..582624bf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,9 +3,15 @@
* src/game-server/map.cpp, src/game-server/movingobject.hpp,
src/game-server/object.hpp: Fixed a crash caused by the new blocking
system (thanks to peavey for reporting).
- * src/scripting/lua.cpp: Implemented script binding for controllig
- movement of beings.
- * data/test.lua: Implemented NPC which moves around when talked to.
+ * src/scripting/lua.cpp: Implemented script bindings for querying
+ positions and controllig movement of beings.
+ * data/scripts/libtmw.lua, data/test.lua: Implemented script
+ infrastructure for regularily called NPC function.
+ * data/test.lua: Implemented NPCs which move around. One automatically
+ and one when talked to.
+ * src/game-server/npc.hpp: Added pathblocking rules to NPCs (blocked
+ by walls, mosters, other NPCs and player characters but do not block
+ player characters.
2008-03-13 Philipp Sehmisch <tmw@crushnet.org>
diff --git a/data/scripts/libtmw.lua b/data/scripts/libtmw.lua
index 662488e3..d55d8b41 100644
--- a/data/scripts/libtmw.lua
+++ b/data/scripts/libtmw.lua
@@ -4,7 +4,8 @@
-- Table that associates to each NPC pointer the handler function that is
-- called when a player starts talking to an NPC.
-local npcs = {}
+local npc_talk_functs = {}
+local npc_update_functs = {}
-- Table that associates to each Character pointer its state with respect to
-- NPCs (only one at a time). A state is an array with four fields:
@@ -24,9 +25,10 @@ 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, handler)
+function create_npc(id, x, y, talkfunct, updatefunct)
local npc = tmw.npc_create(id, x, y)
- npcs[npc] = handler
+ if talkfunct then npc_talk_functs[npc] = talkfunct end
+ if updatefunct then npc_update_functs[npc] = updatefunct end
end
-- Waits for the player to acknowledge the previous message, if any.
@@ -121,7 +123,7 @@ end
-- Creates a coroutine based on the registered NPC handler.
function npc_start(npc, ch)
states[ch] = nil
- local h = npcs[npc]
+ local h = npc_talk_functs[npc]
if not h then return end
local w = { npc, coroutine.create(h) }
if process_npc(w, npc, ch) then
@@ -177,6 +179,8 @@ end
-- Called by the game every tick for each NPC.
function npc_update(npc)
+ local h = npc_update_functs[npc];
+ if h then h(npc) end;
end
-- Called by the game every tick.
@@ -214,7 +218,7 @@ end
function create_npc_delayed(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) end)
+ atinit(function() create_npc(id, x, y, h, nil) end)
npc_handler = nil
end
@@ -235,4 +239,4 @@ end
tmw.chr_money = function(ch)
return tmw.chr_inv_count(ch, 0)
-end
+end \ No newline at end of file
diff --git a/data/test.lua b/data/test.lua
index d4a161d1..3bc630fb 100644
--- a/data/test.lua
+++ b/data/test.lua
@@ -3,13 +3,13 @@
--------------
atinit(function()
- create_npc(110, 50 * 32 + 16, 19 * 32 + 16, my_npc1)
- create_npc(108, 51 * 32 + 16, 25 * 32 + 16, my_npc4)
- create_npc(126, 45 * 32 + 16, 25 * 32 + 16, my_npc5)
- create_npc(122, 58 * 32 + 16, 15 * 32 + 16, my_npc6)
+ create_npc(110, 50 * 32 + 16, 19 * 32 + 16, npc1_talk, nil)
+ create_npc(108, 51 * 32 + 16, 25 * 32 + 16, npc4_talk, nil)
+ create_npc(126, 45 * 32 + 16, 25 * 32 + 16, npc5_talk, npc5_update)
+ create_npc(122, 58 * 32 + 16, 15 * 32 + 16, npc6_talk, nil)
end)
-function my_npc1(npc, ch)
+function npc1_talk(npc, ch)
do_message(npc, ch, "Hello! I am the testing NPC.")
do_message(npc, ch, "This message is just here for testing intertwined connections.")
do_message(npc, ch, "What do you want?")
@@ -49,7 +49,7 @@ function my_npc1(npc, ch)
end
end
-function my_npc4(npc, ch)
+function npc4_talk(npc, ch)
do_message(npc, ch, "Where do you want to go?")
local v = do_choice(npc, ch, "Map 1", "Map 3")
if v >= 1 and v <= 2 then
@@ -65,27 +65,40 @@ function my_npc4(npc, ch)
end
end
-function my_npc5(npc, ch)
+function npc5_talk(npc, ch)
do_message(npc, ch, "I am the spider tamer. Do you want me to spawn some spiders?")
local answer = do_choice(npc, ch, "Yes", "No");
if answer == 1 then
- tmw.monster_create(1012, 44 * 32 + 16, 24 * 32 + 16)
- tmw.monster_create(1012, 44 * 32 + 16, 26 * 32 + 16)
- tmw.monster_create(1012, 46 * 32 + 16, 24 * 32 + 16)
- tmw.monster_create(1012, 46 * 32 + 16, 26 * 32 + 16)
+ local x = tmw.posX(npc)
+ local y = tmw.posY(npc)
+ tmw.monster_create(1012, x + 32, y + 32)
+ tmw.monster_create(1012, x - 32, y + 32)
+ tmw.monster_create(1012, x + 32, y - 32)
+ tmw.monster_create(1012, x - 32, y - 32)
end
end
+local spidertamer_timer = 0
+
+function npc5_update(npc)
+ spidertamer_timer = spidertamer_timer + 1
+ if spidertamer_timer == 50 then
+ spidertamer_timer = 0
+ local x = math.random(-64, 64) + tmw.posX(npc)
+ local y = math.random(-64, 64) + tmw.posY(npc)
+ tmw.being_walk(npc, x, y, 500)
+ end
+end
local guard_position = 1
-function my_npc6(npc, ch)
-
+function npc6_talk(npc, ch)
+
if guard_position == 1 then
tmw.being_walk(npc, 61 * 32 + 16, 15 * 32 + 16, 400)
guard_position = 2
else
tmw.being_walk(npc, 55 * 32 + 16, 15 * 32 + 16, 400)
guard_position = 1
- end
+ end
end \ No newline at end of file
diff --git a/src/game-server/npc.hpp b/src/game-server/npc.hpp
index 7c909d25..2d790c4c 100644
--- a/src/game-server/npc.hpp
+++ b/src/game-server/npc.hpp
@@ -54,6 +54,20 @@ class NPC : public Being
int getNPC() const
{ return mID; }
+ /**
+ * Gets the way an NPC is blocked by other things on the map
+ */
+ virtual unsigned char getWalkMask() const
+ { return 0x83; } // blocked like a monster by walls, monsters and characters ( bin 1000 0011)
+
+ protected:
+
+ /**
+ * Gets the way a monster blocks pathfinding for other objects
+ */
+ virtual Map::BlockType getBlockType() const
+ { return Map::BLOCKTYPE_CHARACTER; } //blocks like a player character
+
private:
Script *mScript; /**< Script describing NPC behavior. */
unsigned short mID; /**< ID of the NPC. */
diff --git a/src/scripting/lua.cpp b/src/scripting/lua.cpp
index 7437af83..3d706619 100644
--- a/src/scripting/lua.cpp
+++ b/src/scripting/lua.cpp
@@ -400,6 +400,34 @@ static int LuaBeing_Walk(lua_State *s)
}
/**
+ * Function for getting the x-coordinate of the position of a being
+ */
+static int LuaPosX(lua_State *s)
+{
+ lua_pushlightuserdata(s, (void *)&registryKey);
+ lua_gettable(s, LUA_REGISTRYINDEX);
+
+ int x = getBeing(s, 1)->getPosition().x;
+ lua_pushinteger(s, x);
+
+ return 1;
+}
+
+/**
+ * Function for getting the y-coordinate of the position of a being
+ */
+static int LuaPosY(lua_State *s)
+{
+ lua_pushlightuserdata(s, (void *)&registryKey);
+ lua_gettable(s, LUA_REGISTRYINDEX);
+
+ int y = getBeing(s, 1)->getPosition().y;
+ lua_pushinteger(s, y);
+
+ return 1;
+}
+
+/**
* Callback for creating a monster on the current map.
* tmw.monster_create(int type, int x, int y)
*/
@@ -524,6 +552,8 @@ LuaScript::LuaScript():
{ "chr_set_quest", &LuaChr_SetQuest },
{ "monster_create", &LuaMonster_Create },
{ "being_walk", &LuaBeing_Walk },
+ { "posX", &LuaPosX },
+ { "posY", &LuaPosY },
{ NULL, NULL }
};
luaL_register(mState, "tmw", callbacks);