summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog9
-rw-r--r--data/scripts/libtmw.lua59
-rw-r--r--data/test.lua11
3 files changed, 79 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 798442ae..5b6b074a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2008-05-31 Philipp Sehmisch <tmw@crushnet.org>
+
+ * data/script/libtmw.lua: Scripted a scheduler which can execute script
+ functions at regular intervals or in a given number of seconds. This will
+ be very useful for any quest or effect script where timing is important.
+ * data/test.lua: Used the script scheduler to schedule 3 demo tasks: a 5
+ second cronjob and two fixed point tasks where the second is scheduled by
+ the first (I'll make them do something more useful soon).
+
2008-05-30 Roderic Morris <roderic@ccs.neu.edu>
* src/account-server/accounthandler.cpp: Register chat clients with
diff --git a/data/scripts/libtmw.lua b/data/scripts/libtmw.lua
index 7125162c..8ea6462e 100644
--- a/data/scripts/libtmw.lua
+++ b/data/scripts/libtmw.lua
@@ -213,8 +213,12 @@ function npc_update(npc)
end
-- Called by the game every tick.
+-- Checks for scheduled function calls
-- Cleans obsolete connections.
function update()
+ -- check the scheduler
+ check_schedule()
+
-- Run every minute only, in order not to overload the server.
if not timer then return end
timer = timer - 1
@@ -260,6 +264,61 @@ function initialize()
init_fun = nil
end
+
+-- SCHEDULER
+
+-- Table of scheduled jobs. A job is an array with 3 elements:
+-- 0: the UNIX timestamp when it is executed
+-- 1: the function which is executed
+-- 2: nil when it is a one-time job. Repetition interval is seconds when it is
+-- a repeated job.
+local scheduler_jobs = {}
+
+-- compare function used to sort the scheduler_jobs table.
+-- the jobs which come first are at the end of the table.
+local function job_cmp(job1, job2)
+ return (job1[0] > job2[0])
+end
+
+-- checks for jobs which have to be executed, executes them and reschedules
+-- them when they are repeated jobs.
+function check_schedule()
+ if #scheduler_jobs==0 then return end
+ while os.time() > scheduler_jobs[#scheduler_jobs][0] do
+ -- retreive the job and remove it from the schedule
+ job = scheduler_jobs[#scheduler_jobs]
+ table.remove(scheduler_jobs)
+ -- reschedule the job when it is a repeated job
+ if job[2] then
+ schedule_every(job[2], job[1])
+ end
+ -- execute the job
+ job[1]()
+ end
+end
+
+-- schedules a function call to be executed once in n seconds
+function schedule_in(seconds, funct)
+ local job = {}
+ job[0] = os.time() + seconds
+ job[1] = funct
+ job[2] = nil
+ table.insert(scheduler_jobs, job)
+ table.sort(scheduler_jobs, job_cmp)
+end
+
+-- schedules a function call to be executed at regular intervals of n seconds
+function schedule_every(seconds, funct)
+ local job = {}
+ job[0] = os.time() + seconds
+ job[1] = funct
+ job[2] = seconds
+ table.insert(scheduler_jobs, job)
+ table.sort(scheduler_jobs, job_cmp)
+end
+
+
+
-- Below are some convenience methods added to the engine API
tmw.chr_money_change = function(ch, amount)
diff --git a/data/test.lua b/data/test.lua
index e33f2de1..ad006a52 100644
--- a/data/test.lua
+++ b/data/test.lua
@@ -27,6 +27,17 @@ atinit(function()
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)
+ schedule_every(5, function()
+ print("This is the schedule_every 5 seconds task")
+ end)
+
+ schedule_in(25, function()
+ print("This is the schedule_in 25 seconds after map initialisation task")
+ schedule_in(7, function()
+ print("This task was scheduled 7 seconds ago by the other schedule_in task")
+ end)
+ end)
+
end)