Skip to content

Commit df6829e

Browse files
author
Jeija
committedMar 19, 2014
Remove timer() from LuaController and make interrupt() use the ActionQueue so that it will keep working when restarting the server
1 parent 39a0e56 commit df6829e

File tree

2 files changed

+27
-67
lines changed

2 files changed

+27
-67
lines changed
 

‎mesecons/actionqueue.lua

+10-9
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ end
66

77
-- If add_action with twice the same overwritecheck and same position are called, the first one is overwritten
88
-- use overwritecheck nil to never overwrite, but just add the event to the queue
9-
-- priority specifies the order actions are executed within one globalstep, highest by default
9+
-- priority specifies the order actions are executed within one globalstep, highest first
1010
-- should be between 0 and 1
1111
function mesecon.queue:add_action(pos, func, params, time, overwritecheck, priority)
1212
-- Create Action Table:
1313
time = time or 0 -- time <= 0 --> execute, time > 0 --> wait time until execution
1414
priority = priority or 1
15-
action = { pos=mesecon:tablecopy(pos),
16-
func=func,
17-
params=mesecon:tablecopy(params),
18-
time=time,
19-
owcheck=(overwritecheck and mesecon:tablecopy(overwritecheck)) or nil,
20-
priority=priority}
15+
local action = { pos=mesecon:tablecopy(pos),
16+
func=func,
17+
params=mesecon:tablecopy(params),
18+
time=time,
19+
owcheck=(overwritecheck and mesecon:tablecopy(overwritecheck)) or nil,
20+
priority=priority}
2121

2222
-- if not using the queue, (MESECONS_GLOBALSTEP off), just execute the function an we're done
2323
if not MESECONS_GLOBALSTEP and action.time == 0 then
@@ -50,7 +50,7 @@ end
5050
-- However, even that does not work in some cases, that's why we delay the time the globalsteps
5151
-- start to be execute by 5 seconds
5252
local get_highest_priority = function (actions)
53-
local highestp = 0, highesti
53+
local highestp = -1, highesti
5454
for i, ac in ipairs(actions) do
5555
if ac.priority > highestp then
5656
highestp = ac.priority
@@ -70,7 +70,8 @@ minetest.register_globalstep(function (dtime)
7070

7171
mesecon.queue.actions = {}
7272

73-
-- sort actions in execute now (actions_now) and for later (mesecon.queue.actions)
73+
-- sort actions into two categories:
74+
-- those toexecute now (actions_now) and those to execute later (mesecon.queue.actions)
7475
for i, ac in ipairs(actions) do
7576
if ac.time > 0 then
7677
ac.time = ac.time - dtime -- executed later

‎mesecons_luacontroller/init.lua

+17-58
Original file line numberDiff line numberDiff line change
@@ -192,48 +192,21 @@ local safe_serialize = function(value)
192192
return minetest.serialize(deep_copy(value))
193193
end
194194

195-
local interrupt = function(params)
196-
lc_update(params.pos, {type="interrupt", iid = params.iid})
197-
end
195+
mesecon.queue:add_function("lc_interrupt", function (pos, iid, luac_id)
196+
-- There is no luacontroller anymore / it has been reprogrammed / replaced
197+
if (minetest.get_meta(pos):get_int("luac_id") ~= luac_id) then return end
198+
lc_update(pos, {type="interrupt", iid = iid})
199+
end)
198200

199201
local getinterrupt = function(pos)
200202
local interrupt = function (time, iid) -- iid = interrupt id
201203
if type(time) ~= "number" then return end
202-
local iid = iid or math.random()
203-
local meta = minetest.get_meta(pos)
204-
local interrupts = minetest.deserialize(meta:get_string("lc_interrupts")) or {}
205-
local found = false
206-
local search = safe_serialize(iid)
207-
for _, i in ipairs(interrupts) do
208-
if safe_serialize(i) == search then
209-
found = true
210-
break
211-
end
212-
end
213-
if not found then
214-
table.insert(interrupts, iid)
215-
meta:set_string("lc_interrupts", safe_serialize(interrupts))
216-
end
217-
minetest.after(time, interrupt, {pos=pos, iid = iid})
204+
luac_id = minetest.get_meta(pos):get_int("luac_id")
205+
mesecon.queue:add_action(pos, "lc_interrupt", {iid, luac_id}, time, iid, 1)
218206
end
219207
return interrupt
220208
end
221209

222-
local handle_timer = function(pos, elapsed)
223-
local err = lc_update(pos, {type="timer"})
224-
if err then print(err) end
225-
return false
226-
end
227-
228-
local gettimer = function(pos)
229-
local timer = function (time)
230-
if type(time) ~= "number" then return end
231-
local nodetimer = minetest.get_node_timer(pos)
232-
nodetimer:start(time)
233-
end
234-
return timer
235-
end
236-
237210
local getdigiline_send = function(pos)
238211
if not digiline then return end
239212
-- Send messages on next serverstep
@@ -255,7 +228,6 @@ local create_environment = function(pos, mem, event)
255228
pin = merge_portstates(vports, rports),
256229
port = vports,
257230
interrupt = getinterrupt(pos),
258-
timer = gettimer(pos),
259231
digiline_send = getdigiline_send(pos),
260232
mem = mem,
261233
tostring = tostring,
@@ -334,7 +306,6 @@ local do_overheat = function (pos, meta)
334306
if overheat(meta) then
335307
local node = minetest.get_node(pos)
336308
minetest.swap_node(pos, {name = BASENAME.."_burnt", param2 = node.param2})
337-
minetest.get_meta(pos):set_string("lc_interrupts", "")
338309
minetest.after(0.2, overheat_off, pos) -- wait for pending operations
339310
return true
340311
end
@@ -348,20 +319,6 @@ local save_memory = function(meta, mem)
348319
meta:set_string("lc_memory", safe_serialize(mem))
349320
end
350321

351-
local interrupt_allow = function (meta, event)
352-
if event.type ~= "interrupt" then return true end
353-
354-
local interrupts = minetest.deserialize(meta:get_string("lc_interrupts")) or {}
355-
local search = safe_serialize(event.iid)
356-
for _, i in ipairs(interrupts) do
357-
if safe_serialize(i) == search then
358-
return true
359-
end
360-
end
361-
362-
return false
363-
end
364-
365322
local ports_invalid = function (var)
366323
if type(var) == "table" then
367324
return false
@@ -375,7 +332,6 @@ end
375332

376333
lc_update = function (pos, event)
377334
local meta = minetest.get_meta(pos)
378-
if not interrupt_allow(meta, event) then return end
379335
if do_overheat(pos, meta) then return end
380336

381337
-- load code & mem from memory
@@ -412,10 +368,10 @@ local reset_meta = function(pos, code, errmsg)
412368
"image_button_exit[9.72,-0.25;0.425,0.4;jeija_close_window.png;exit;]"..
413369
"label[0.1,5;"..errmsg.."]")
414370
meta:set_int("heat", 0)
371+
meta:set_int("luac_id", math.random(1, 1000000))
415372
end
416373

417374
local reset = function (pos)
418-
minetest.get_meta(pos):set_string("lc_interrupts", "")
419375
action(pos, {a=false, b=false, c=false, d=false})
420376
end
421377

@@ -541,21 +497,23 @@ minetest.register_node(nodename, {
541497
reset(pos)
542498
reset_meta(pos, fields.code)
543499
local err = lc_update(pos, {type="program"})
544-
if err then print(err) end
545-
reset_meta(pos, fields.code, err)
500+
if err then
501+
print(err)
502+
reset_meta(pos, fields.code, err)
503+
end
546504
end,
547505
on_timer = handle_timer,
548506
sounds = default.node_sound_stone_defaults(),
549507
mesecons = mesecons,
550508
digiline = digiline,
551-
is_luacontroller = true,
552509
virtual_portstates = { a = a == 1, -- virtual portstates are
553510
b = b == 1, -- the ports the the
554511
c = c == 1, -- controller powers itself
555512
d = d == 1},-- so those that light up
556513
after_dig_node = function (pos, node)
557514
mesecon:receptor_off(pos, output_rules)
558515
end,
516+
is_luacontroller = true,
559517
})
560518
end
561519
end
@@ -588,11 +546,12 @@ minetest.register_node(BASENAME .. "_burnt", {
588546
reset(pos)
589547
reset_meta(pos, fields.code)
590548
local err = lc_update(pos, {type="program"})
591-
if err then print(err) end
592-
reset_meta(pos, fields.code, err)
549+
if err then
550+
print(err)
551+
reset_meta(pos, fields.code, err)
552+
end
593553
end,
594554
sounds = default.node_sound_stone_defaults(),
595-
is_luacontroller = true,
596555
virtual_portstates = {a = false, b = false, c = false, d = false},
597556
})
598557

0 commit comments

Comments
 (0)
Please sign in to comment.