mirror of
https://github.com/minetest-mods/digilines.git
synced 2025-03-28 07:10:24 +01:00
Fix according to comments
This commit is contained in:
parent
6ed20ff143
commit
eb891e156b
@ -8,22 +8,25 @@ local batched_signals = {}
|
|||||||
local interval_to_batch = 0.1
|
local interval_to_batch = 0.1
|
||||||
-- Maximum number of signals in batch
|
-- Maximum number of signals in batch
|
||||||
local max_signals_in_batch = 100
|
local max_signals_in_batch = 100
|
||||||
|
-- Time of last signal for each chest
|
||||||
|
local last_signal_time_for_chest = {}
|
||||||
|
|
||||||
-- Sends the current batch message of a Digiline chest
|
-- Sends the current batch message of a Digiline chest
|
||||||
-- pos: the position of the Digilines chest node
|
-- pos: the position of the Digilines chest node
|
||||||
-- channel: the channel to which the message will be sent
|
-- channel: the channel to which the message will be sent
|
||||||
local function send_and_clear_batch(pos, channel)
|
local function send_and_clear_batch(pos, channel)
|
||||||
local pos_text = vector.to_string(pos)
|
local pos_hash = minetest.hash_node_position(pos)
|
||||||
if #batched_signals[pos_text] == 1 then
|
if #batched_signals[pos_hash] == 1 then
|
||||||
-- If there is only one signal is the batch, don't send it in a batch
|
-- If there is only one signal is the batch, don't send it in a batch
|
||||||
digilines.receptor_send(pos, digilines.rules.default, next(batched_signals[pos_text]))
|
digilines.receptor_send(pos, digilines.rules.default, next(batched_signals[pos_hash]))
|
||||||
else
|
else
|
||||||
digilines.receptor_send(pos, digilines.rules.default, channel, {
|
digilines.receptor_send(pos, digilines.rules.default, channel, {
|
||||||
action = "batch",
|
action = "batch",
|
||||||
signals = batched_signals[pos_text]
|
signals = batched_signals[pos_hash]
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
batched_signals[pos_text] = nil
|
batched_signals[pos_hash] = nil
|
||||||
|
last_signal_time_for_chest[pos_hash] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Sends a message onto the Digilines network.
|
-- Sends a message onto the Digilines network.
|
||||||
@ -47,16 +50,15 @@ local function send_message(pos, action, stack, from_slot, to_slot, side)
|
|||||||
}
|
}
|
||||||
|
|
||||||
-- Check if we need to include the current signal into batch
|
-- Check if we need to include the current signal into batch
|
||||||
-- Store "prev_time" in metadata as a string to avoid integer overflow
|
local pos_hash = minetest.hash_node_position(pos)
|
||||||
local prev_time = tonumber(meta:get_string("prev_time")) or 0
|
local prev_time = last_signal_time_for_chest[pos_hash] or 0
|
||||||
local cur_time = minetest.get_us_time()
|
local cur_time = minetest.get_us_time()
|
||||||
meta:set_string("prev_time", tostring(cur_time))
|
last_signal_time_for_chest[pos_hash] = cur_time
|
||||||
if cur_time - prev_time < 1000000 * interval_to_batch then
|
if cur_time - prev_time < 1000000 * interval_to_batch then
|
||||||
local pos_text = vector.to_string(pos)
|
batched_signals[pos_hash] = batched_signals[pos_hash] or {}
|
||||||
batched_signals[pos_text] = batched_signals[pos_text] or {}
|
table.insert(batched_signals[pos_hash], msg)
|
||||||
table.insert(batched_signals[pos_text], msg)
|
|
||||||
local node_timer = minetest.get_node_timer(pos)
|
local node_timer = minetest.get_node_timer(pos)
|
||||||
if #batched_signals[pos_text] >= max_signals_in_batch then
|
if #batched_signals[pos_hash] >= max_signals_in_batch then
|
||||||
-- Send the batch immediately if it's full
|
-- Send the batch immediately if it's full
|
||||||
node_timer:stop()
|
node_timer:stop()
|
||||||
send_and_clear_batch(pos, channel)
|
send_and_clear_batch(pos, channel)
|
||||||
@ -234,7 +236,7 @@ minetest.register_node("digilines:chest", {
|
|||||||
inv:set_size("main", 8*4)
|
inv:set_size("main", 8*4)
|
||||||
end,
|
end,
|
||||||
on_destruct = function(pos)
|
on_destruct = function(pos)
|
||||||
batched_signals[vector.to_string(pos)] = nil
|
batched_signals[minetest.hash_node_position(pos)] = nil
|
||||||
end,
|
end,
|
||||||
after_place_node = tubescan,
|
after_place_node = tubescan,
|
||||||
after_dig_node = tubescan,
|
after_dig_node = tubescan,
|
||||||
@ -374,7 +376,7 @@ minetest.register_node("digilines:chest", {
|
|||||||
end,
|
end,
|
||||||
on_timer = function(pos, _)
|
on_timer = function(pos, _)
|
||||||
-- Send all the batched signals when enough time since the last signal passed
|
-- Send all the batched signals when enough time since the last signal passed
|
||||||
if not batched_signals[vector.to_string(pos)] then
|
if not batched_signals[minetest.hash_node_position(pos)] then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local channel = minetest.get_meta(pos):get_string("channel")
|
local channel = minetest.get_meta(pos):get_string("channel")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user