Skip to content

Commit

Permalink
debug and maybe fix chests that stop working
Browse files Browse the repository at this point in the history
  • Loading branch information
bengardner committed Nov 20, 2023
1 parent e2cddf7 commit 6f9813c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
18 changes: 8 additions & 10 deletions src/GlobalState.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ function M.inner_setup()
end

if global.mod.scan_queues == nil or true then
global.mod.entity_priority = {} -- key=unum, val=priority
M.reset_queues()
end

Expand Down Expand Up @@ -168,7 +167,8 @@ function M.reset_queues()

global.mod.scan_queues = {} -- list of queues to process
for idx = 1, constants.QUEUE_COUNT do
global.mod.scan_queues[idx] = Queue.new()
-- key=unit_nubmer, val=priority
global.mod.scan_queues[idx] = {}
end

if global.mod.chests ~= nil then
Expand Down Expand Up @@ -904,15 +904,13 @@ end
That will put the entity in the previous queue slot.
]]
function M.queue_insert(unit_number, priority)
-- clamp priority to 0 .. QUEUE_COUNT-2
-- clamp priority to 0..QUEUE_COUNT-2 (inclusive)
priority = math.max(0, math.min(priority, constants.QUEUE_COUNT - 2))
-- save the priority for next time
global.mod.entity_priority[unit_number] = priority

-- q_idx is where to put it, Priority 0 => scan_index + 1
-- scan_index is 1-based, so we subtract 1 and then add 1 (scan_index - 1 + 1 + priority)
local q_idx = 1 + (global.mod.scan_index + priority) % constants.QUEUE_COUNT
Queue.push(global.mod.scan_queues[q_idx], unit_number)
global.mod.scan_queues[q_idx][unit_number] = priority

--print(string.format("[%s] ADD q %s unum %s si=%s p=%s qx=%s", game.tick, q_idx, unit_number, global.mod.scan_index, priority, constants.QUEUE_COUNT))
end
Expand All @@ -930,14 +928,14 @@ function M.update_queue_lists(update_entity)
.value

local qs = global.mod.scan_queues
local q_idx = global.mod.scan_index
local q_idx = global.mod.scan_index or 1
if q_idx < 1 or q_idx > #qs then
q_idx = 1
end
local q = qs[q_idx]

for _ = 1, MAX_ENTITIES_TO_UPDATE do
local unit_number = Queue.pop(q)
local unit_number, old_pri = next(q)
if unit_number == nil then
-- nothing to process in this queue. step to the next one if past the deadline
if game.tick >= (global.mod.scan_deadline or 0) then
Expand All @@ -947,10 +945,10 @@ function M.update_queue_lists(update_entity)
end
return
end
--print(string.format("[%s] PROC q %s unum %s", game.tick, q_idx, unit_number))
q[unit_number] = nil
--print(string.format("[%s] PROC q %s unum=%s pri=%s", game.tick, q_idx, unit_number, old_pri))

-- the UPDATE_STATUS are actually relative priorities.
local old_pri = global.mod.entity_priority[unit_number] or 0
local pri_adj = update_entity(unit_number, old_pri)
-- nil means entity is invalid.
if pri_adj ~= nil then
Expand Down
31 changes: 25 additions & 6 deletions src/NetworkChest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ local function update_network_chest_configured_common(info, inv, contents)
local n_free = GlobalState.get_insert_count(item)
local n_transfer = math.min(n_extra, n_free)
if n_transfer > 0 then
status = GlobalState.UPDATE_STATUS.UPDATED
-- status = GlobalState.UPDATE_STATUS.UPDATE_PRI_INC
inv.remove({name=item, count=n_transfer})
GlobalState.increment_item_count(item, n_transfer)
info.recent_items[item] = game.tick
Expand Down Expand Up @@ -1056,7 +1056,10 @@ local function update_entity_chest(unit_number, info)
elseif entity.name == "network-chest-requester" then
update_network_chest_requester(info)
return GlobalState.UPDATE_STATUS.UPDATE_BULK
else
clog("What is this chest? %s", entity.name)
end
return GlobalState.UPDATE_STATUS.NOT_UPDATED
end

local function update_entity_tank(unit_number, info)
Expand Down Expand Up @@ -1086,27 +1089,43 @@ local function update_entity(unit_number, priority)
info = GlobalState.get_chest_info(unit_number)
if info ~= nil then
info.service_tick = game.tick
return update_entity_chest(unit_number, info, priority)
local retval = update_entity_chest(unit_number, info)
if retval == nil then
clog("chest drop: %s", serpent.line(info))
end
return retval
end

info = GlobalState.get_tank_info(unit_number)
if info ~= nil then
info.service_tick = game.tick
return update_entity_tank(unit_number, info, priority)
local retval = update_entity_tank(unit_number, info)
if retval == nil then
clog("tank drop: %s", serpent.line(info))
end
return retval
end

local entity = GlobalState.get_logistic_entity(unit_number)
if entity ~= nil then
return M.update_entity_logistic(entity)
local retval = M.update_entity_logistic(entity)
if retval == nil then
clog("logistic drop: %s", serpent.line(info))
end
return retval
end

entity = GlobalState.get_vehicle_entity(unit_number)
if entity ~= nil then
return M.update_entity_vehicle(entity)
local retval = M.update_entity_vehicle(entity)
if retval == nil then
clog("vehicle drop: %s", serpent.line(info))
end
return retval
end

-- unknown/invalid unit_number
clog("Dropped: unum %s", unit_number)
clog("Dropped: unknown unum %s", unit_number)
return nil
end

Expand Down

0 comments on commit 6f9813c

Please sign in to comment.