summaryrefslogtreecommitdiff
path: root/mesecons/actionqueue.lua
diff options
context:
space:
mode:
authorJeija <jeija@mesecons.net>2014-01-11 10:04:32 +0100
committerJeija <jeija@mesecons.net>2014-01-11 14:57:56 +0100
commitf1211f7dae58ff4298b6bf4fcaa572e7995ab5e2 (patch)
treec5e385d8e820ed89012915ff32c46841116e1788 /mesecons/actionqueue.lua
parent93fb489bdb8c13989c398b37be3135004f7b0bbc (diff)
downloadmesecons-f1211f7dae58ff4298b6bf4fcaa572e7995ab5e2.tar
mesecons-f1211f7dae58ff4298b6bf4fcaa572e7995ab5e2.tar.gz
mesecons-f1211f7dae58ff4298b6bf4fcaa572e7995ab5e2.tar.bz2
mesecons-f1211f7dae58ff4298b6bf4fcaa572e7995ab5e2.tar.xz
mesecons-f1211f7dae58ff4298b6bf4fcaa572e7995ab5e2.zip
Add ActionQueue priority system
This makes effectors nearer to the source of the action (the receptor) update first. This defines behaviour for this piston circuit: http://i.imgur.com/9Pp2Mzb.png And defines, that this memory circuit does not work from this direction: http://i.imgur.com/jJn0aFh.png But it will work when using the switch from the other side: http://i.imgur.com/nvw0oZB.png Only if two effectors have the same distance, there is nothing we can do about it, behaviour is not defined. "Distance" is determined by the stack size of recursions in turnon / turnoff. Priorities are between 0 (lowest) and 1 (highest).
Diffstat (limited to 'mesecons/actionqueue.lua')
-rw-r--r--mesecons/actionqueue.lua28
1 files changed, 24 insertions, 4 deletions
diff --git a/mesecons/actionqueue.lua b/mesecons/actionqueue.lua
index 9d31426..a00054a 100644
--- a/mesecons/actionqueue.lua
+++ b/mesecons/actionqueue.lua
@@ -6,15 +6,19 @@ end
-- If add_action with twice the same overwritecheck and same position are called, the first one is overwritten
-- use overwritecheck nil to never overwrite, but just add the event to the queue
-function mesecon.queue:add_action(pos, func, params, time, overwritecheck)
+-- priority specifies the order actions are executed within one globalstep, highest by default
+-- should be between 0 and 1
+function mesecon.queue:add_action(pos, func, params, time, overwritecheck, priority)
-- Create Action Table:
time = time or 0 -- time <= 0 --> execute, time > 0 --> wait time until execution
+ priority = priority or 1
overwritecheck = overwritecheck or {}
action = { pos=mesecon:tablecopy(pos),
func=func,
params=mesecon:tablecopy(params),
time=time,
- owcheck=mesecon:tablecopy(overwritecheck)}
+ owcheck=(overwritecheck and mesecon:tablecopy(overwritecheck)) or nil,
+ priority=priority}
--print(dump(action))
-- if not using the queue, (MESECONS_GLOBALSTEP off), just execute the function an we're done
@@ -42,6 +46,18 @@ end
-- this makes sure that resuming mesecons circuits when restarting minetest works fine
-- However, even that does not work in some cases, that's why we delay the time the globalsteps
-- start to be execute by 5 seconds
+local get_highest_priority = function (actions)
+ local highestp = 0, highesti
+ for i, ac in ipairs(actions) do
+ if ac.priority > highestp then
+ highestp = ac.priority
+ highesti = i
+ end
+ end
+
+ return highesti
+end
+
local m_time = 0
minetest.register_globalstep(function (dtime)
m_time = m_time + dtime
@@ -53,10 +69,14 @@ minetest.register_globalstep(function (dtime)
if action.time > 0 then
action.time = action.time - dtime
table.insert(mesecon.queue.actions, action) -- will be handled another time
- else -- execute and remove
- mesecon.queue:execute(action)
end
end
+
+ while(#actions > 0) do -- execute highest priorities first, until all are executed
+ local hp = get_highest_priority(actions)
+ mesecon.queue:execute(actions[hp])
+ table.remove(actions, hp)
+ end
end)
function mesecon.queue:execute(action)