diff --git a/cybersyn/changelog.txt b/cybersyn/changelog.txt index 9a6819b..18b154a 100644 --- a/cybersyn/changelog.txt +++ b/cybersyn/changelog.txt @@ -1,4 +1,9 @@ --------------------------------------------------------------------------------------------------- +Version: 1.3.0 +Date: 2022-1-7 + Bugfixes: + - Fixed a bug where it was possible for a single station to be updated twice per dispatch cycle, which could cause a crash +--------------------------------------------------------------------------------------------------- Version: 1.2.9 Date: 2022-1-7 Bugfixes: diff --git a/cybersyn/info.json b/cybersyn/info.json index e8a647e..4af65bd 100644 --- a/cybersyn/info.json +++ b/cybersyn/info.json @@ -1,6 +1,6 @@ { "name": "cybersyn", - "version": "1.2.9", + "version": "1.3.0", "title": "Project Cybersyn", "author": "Mami", "factorio_version": "1.1", diff --git a/cybersyn/scripts/central-planning.lua b/cybersyn/scripts/central-planning.lua index 4fb97d5..64a6b01 100644 --- a/cybersyn/scripts/central-planning.lua +++ b/cybersyn/scripts/central-planning.lua @@ -523,7 +523,7 @@ local function tick_poll_station(map_data, mod_settings) end station_id = map_data.active_station_ids[tick_data.i] station = map_data.stations[station_id] - if station then + if station and not station.is_warming_up then if station.network_name then break end @@ -725,22 +725,35 @@ function tick_init(map_data, mod_settings) map_data.economy.all_r_stations = {} map_data.economy.all_names = {} - for i, id in pairs(map_data.warmup_station_ids) do + local i = 1 + while i <= #map_data.warmup_station_ids do + local id = map_data.warmup_station_ids[i] local station = map_data.stations[id] if station then - if station.last_delivery_tick + mod_settings.warmup_time*mod_settings.tps < map_data.total_ticks then - map_data.active_station_ids[#map_data.active_station_ids + 1] = id - map_data.warmup_station_ids[i] = nil - if station.entity_comb1.valid then - combinator_update(map_data, station.entity_comb1) - else - on_station_broken(map_data, id, station) + local cycles = map_data.warmup_station_cycles[id] + --force a station to wait at least 1 cycle so we can be sure active_station_ids was flushed of duplicates + if cycles > 0 then + if station.last_delivery_tick + mod_settings.warmup_time*mod_settings.tps < map_data.total_ticks then + station.is_warming_up = nil + map_data.active_station_ids[#map_data.active_station_ids + 1] = id + map_data.warmup_station_ids[i] = nil + map_data.warmup_station_cycles[id] = nil + if station.entity_comb1.valid then + combinator_update(map_data, station.entity_comb1) + else + on_station_broken(map_data, id, station) + end end + else + map_data.warmup_station_cycles[id] = cycles + 1 end + i = i + 1 else - map_data.warmup_station_ids[i] = nil + table_remove(map_data.warmup_station_ids, i) + map_data.warmup_station_cycles[id] = nil end end + if map_data.queue_station_update then for id, _ in pairs(map_data.queue_station_update) do local station = map_data.stations[id] @@ -787,7 +800,7 @@ function tick(map_data, mod_settings) end elseif map_data.tick_state == STATE_DISPATCH then for i = 1, mod_settings.update_rate do - tick_dispatch(map_data, mod_settings) + if tick_dispatch(map_data, mod_settings) then break end end end else diff --git a/cybersyn/scripts/global.lua b/cybersyn/scripts/global.lua index a0ad3b2..c2557c0 100644 --- a/cybersyn/scripts/global.lua +++ b/cybersyn/scripts/global.lua @@ -9,6 +9,7 @@ ---@field public stations {[uint]: Station} ---@field public active_station_ids uint[] ---@field public warmup_station_ids uint[] +---@field public warmup_station_cycles {[uint]: int} ---@field public queue_station_update {[uint]: true?}? ---@field public depots {[uint]: Depot} ---@field public refuelers {[uint]: Refueler} @@ -49,6 +50,7 @@ ---@field public item_p_counts {[string]: int} --transient ---@field public item_thresholds {[string]: int}? --transient ---@field public display_state int +---@field public is_warming_up true? ---@class Depot ---@field public entity_stop LuaEntity @@ -146,6 +148,7 @@ function init_global() global.stations = {} global.active_station_ids = {} global.warmup_station_ids = {} + global.warmup_station_cycles = {} global.depots = {} global.trains = {} global.available_trains = {} diff --git a/cybersyn/scripts/main.lua b/cybersyn/scripts/main.lua index 260fe76..a0bd752 100644 --- a/cybersyn/scripts/main.lua +++ b/cybersyn/scripts/main.lua @@ -1,6 +1,7 @@ --By Mami local ceil = math.ceil local table_insert = table.insert +local table_remove = table.remove ---@param map_data MapData @@ -138,10 +139,25 @@ local function on_station_built(map_data, stop, comb1, comb2) item_p_counts = {}, item_thresholds = nil, display_state = 0, + is_warming_up = true, } local id = stop.unit_number--[[@as uint]] + map_data.stations[id] = station + + --prevent the same station from warming up multiple times + if map_data.warmup_station_cycles[id] then + --enforce FIFO + for i, v in ipairs(map_data.warmup_station_ids) do + if v == id then + table_remove(map_data.warmup_station_ids, i) + break + end + end + end map_data.warmup_station_ids[#map_data.warmup_station_ids + 1] = id + map_data.warmup_station_cycles[id] = 0 + if not map_data.queue_station_update then map_data.queue_station_update = {} end diff --git a/cybersyn/scripts/migrations.lua b/cybersyn/scripts/migrations.lua index 44ae173..7af6e10 100644 --- a/cybersyn/scripts/migrations.lua +++ b/cybersyn/scripts/migrations.lua @@ -267,6 +267,32 @@ local migrations_table = { train.use_any_depot = true end end, + ["1.3.0"] = function() + ---@type MapData + local map_data = global + map_data.warmup_station_cycles = {} + + local is_registered = {} + + for i = #map_data.warmup_station_ids, 1, -1 do + local id = map_data.warmup_station_ids[i] + if is_registered[id] then + table.remove(map_data.warmup_station_ids, i) + else + is_registered[id] = true + map_data.warmup_station_cycles[id] = 0 + end + end + + for i = #map_data.active_station_ids, 1, -1 do + local id = map_data.active_station_ids[i] + if is_registered[id] then + table.remove(map_data.active_station_ids, i) + else + is_registered[id] = true + end + end + end } --STATUS_R_TO_D = 5