summaryrefslogtreecommitdiff
path: root/data/test.lua
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-08-10 14:17:38 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-08-10 14:17:38 +0000
commitf990980f80ab1523086edba1bed222741d716fa0 (patch)
tree0ce0257ca854ea06949f543227a301a7f553e1a9 /data/test.lua
parentcb45a65e1020bf129225dd20c57bf64314cef2c8 (diff)
downloadmanaserv-f990980f80ab1523086edba1bed222741d716fa0.tar.gz
manaserv-f990980f80ab1523086edba1bed222741d716fa0.tar.bz2
manaserv-f990980f80ab1523086edba1bed222741d716fa0.tar.xz
manaserv-f990980f80ab1523086edba1bed222741d716fa0.zip
Improved helper functions for Lua scripts. Associated scripts to maps.
Diffstat (limited to 'data/test.lua')
-rw-r--r--data/test.lua79
1 files changed, 63 insertions, 16 deletions
diff --git a/data/test.lua b/data/test.lua
index 3552acc7..5a1327a6 100644
--- a/data/test.lua
+++ b/data/test.lua
@@ -4,6 +4,11 @@
-- NOTE: Could be put into a separate library
+function create_npc(id, x, y, handler)
+ local npc = tmw.obj_create_npc(id, x, y)
+ npcs[npc] = handler
+end
+
function do_message(npc, ch, msg)
tmw.msg_npc_message(npc, ch, msg)
coroutine.yield(1)
@@ -14,37 +19,47 @@ function do_choice(npc, ch, msg)
return coroutine.yield(2)
end
+-- Called whenever a player starts talking to an NPC.
+-- Creates a coroutine based on the register NPC handler.
function npc_start(npc, ch)
- -- TODO: choose the handler depending on the npc type
- local co = coroutine.create(my_npc_handler)
+ local h = npcs[npc]
+ if not h then return end
+ local co = coroutine.create(h)
local b, v = coroutine.resume(co, npc, ch)
if b and v then
- npcs[ch] = {npc, co, v}
+ states[ch] = {npc, co, v, 5}
+ if not timer then
+ timer = 600
+ end
end
end
+-- Called whenever a player keeps talking to an NPC.
+-- Checks that the NPC expects it, and resumes the respective coroutine.
function npc_next(npc, ch)
- local w = npcs[ch]
+ local w = states[ch]
if w and w[1] == npc and w[3] == 1 then
- local co = w[2]
- local b, v = coroutine.resume(co)
+ local b, v = coroutine.resume(w[2])
if b and v then
- npcs[ch] = {npc, co, v}
+ w[3] = v
+ w[4] = 5
else
- npcs[ch] = nil
+ states[ch] = nil
end
end
end
+-- Called whenever a player selects a particular reply.
+-- Checks that the NPC expects it, and resumes the respective coroutine.
function npc_choose(npc, ch, u)
- local w = npcs[ch]
+ local w = states[ch]
if w and w[1] == npc and w[3] == 2 then
- local co = w[2]
- local b, v = coroutine.resume(co, u)
+ local b, v = coroutine.resume(w[2], u)
if b and v then
- npcs[ch] = {npc, co, v}
+ w[3] = v
+ w[4] = 5
else
- npcs[ch] = nil
+ states[ch] = nil
end
end
end
@@ -53,20 +68,41 @@ function npc_update(npc)
end
function update()
- -- TODO: delete obsolete entries of the npcs table
+ -- Run every minute only, in order not to overload the server.
+ if not timer then return end
+ timer = timer - 10
+ if timer ~= 0 then return end
+ -- Free connections that have been inactive for 3-4 minutes.
+ for k, w in pairs(states) do
+ local t = w[4] - 1
+ if t == 0 then
+ states[k] = nil
+ else
+ w[4] = t
+ end
+ end
+ -- Restart timer if there are still some pending states.
+ if next(states) then
+ timer = 600
+ else
+ timer = nil
+ end
end
npcs = {}
+states = {}
--------------
-- Map code --
--------------
function initialize()
- tmw.obj_create_npc(110, 50 * 32 + 16, 19 * 32 + 16)
+ create_npc(110, 50 * 32 + 16, 19 * 32 + 16, my_npc1)
+ create_npc(107, 53 * 32 + 16, 21 * 32 + 16, my_npc2)
+ create_npc(107, 53 * 32 + 16, 23 * 32 + 16, my_npc3)
end
-function my_npc_handler(npc, ch)
+function my_npc1(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?")
@@ -76,3 +112,14 @@ function my_npc_handler(npc, ch)
end
end
+npc2_times = 1
+
+function my_npc2(npc, ch)
+ do_message(npc, ch, "You know what?")
+ do_message(npc, ch, string.format("I have already asked this question %d times today.", npc2_times))
+ npc2_times = npc2_times + 1
+end
+
+function my_npc3(npc, ch)
+ do_message(npc, ch, "Don't you think the guy behind me is my evil twin?")
+end