summaryrefslogtreecommitdiff
path: root/data/scripts
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-08-15 06:35:03 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-08-15 06:35:03 +0000
commit99548fb47fee447b5f22d5338501ac574086c4fd (patch)
treefcef60aed6d7d0f04437249815af36c25461c5dd /data/scripts
parent2fa455ff7870dc09d75bb89a897c7c1d26eb9020 (diff)
downloadmanaserv-99548fb47fee447b5f22d5338501ac574086c4fd.tar.gz
manaserv-99548fb47fee447b5f22d5338501ac574086c4fd.tar.bz2
manaserv-99548fb47fee447b5f22d5338501ac574086c4fd.tar.xz
manaserv-99548fb47fee447b5f22d5338501ac574086c4fd.zip
Added helper functions for loading files and NPCs as scripts. Put Lua helpers into a separate library automatically loaded into new contexts.
Diffstat (limited to 'data/scripts')
-rw-r--r--data/scripts/libtmw.lua111
1 files changed, 111 insertions, 0 deletions
diff --git a/data/scripts/libtmw.lua b/data/scripts/libtmw.lua
new file mode 100644
index 00000000..c4a14b6f
--- /dev/null
+++ b/data/scripts/libtmw.lua
@@ -0,0 +1,111 @@
+------------------
+-- Support code --
+------------------
+
+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)
+end
+
+function do_choice(npc, ch, ...)
+ tmw.msg_npc_choice(npc, ch, ...)
+ 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)
+ 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
+ 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 = states[ch]
+ if w and w[1] == npc and w[3] == 1 then
+ local b, v = coroutine.resume(w[2])
+ if b and v then
+ w[3] = v
+ w[4] = 5
+ else
+ 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 = states[ch]
+ if w and w[1] == npc and w[3] == 2 then
+ local b, v = coroutine.resume(w[2], u)
+ if b and v then
+ w[3] = v
+ w[4] = 5
+ else
+ states[ch] = nil
+ end
+ end
+end
+
+function npc_update(npc)
+end
+
+function update()
+ -- 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
+
+function atinit(f)
+ init_fun[#init_fun + 1] = f
+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)
+ npc_handler = nil
+end
+
+function initialize()
+ for i,f in ipairs(init_fun) do
+ f()
+ end
+ init_fun = nil
+end
+
+npcs = {}
+states = {}
+init_fun = {}
+