diff options
author | Philipp Sehmisch <tmw@crushnet.org> | 2008-05-31 03:58:12 +0000 |
---|---|---|
committer | Philipp Sehmisch <tmw@crushnet.org> | 2008-05-31 03:58:12 +0000 |
commit | 2b8318623e7064c6370234e64ae26622dd9dd433 (patch) | |
tree | 368519dd7e503f909a81eb769921d3fb9ee2bd33 /data | |
parent | f1fc55460f145fb28ac6eef8f8f807b9f9f1c1ea (diff) | |
download | manaserv-2b8318623e7064c6370234e64ae26622dd9dd433.tar.gz manaserv-2b8318623e7064c6370234e64ae26622dd9dd433.tar.bz2 manaserv-2b8318623e7064c6370234e64ae26622dd9dd433.tar.xz manaserv-2b8318623e7064c6370234e64ae26622dd9dd433.zip |
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.
Diffstat (limited to 'data')
-rw-r--r-- | data/scripts/libtmw.lua | 59 | ||||
-rw-r--r-- | data/test.lua | 11 |
2 files changed, 70 insertions, 0 deletions
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) |