Fixed combinator displays

This commit is contained in:
Monica Moniot
2022-12-02 12:51:53 -05:00
parent 30fe2946e6
commit e86562fb41
8 changed files with 107 additions and 75 deletions

4
TODO
View File

@@ -1,6 +1,5 @@
bugs: bugs:
request threshold is being bypassed somehow request threshold is being bypassed somehow (hasn't been seen in a long time)
wagon control combinators don't work parallel to the tracks
major: major:
do hardcore testing do hardcore testing
@@ -12,4 +11,3 @@ minor:
close gui when the combinator is destroyed close gui when the combinator is destroyed
do not play close sound when a different gui is opened do not play close sound when a different gui is opened
gui can desync if settings are changed outside of it while it is open gui can desync if settings are changed outside of it while it is open
go over init and make it agree with type info

View File

@@ -99,3 +99,9 @@ Version: 1.0.7
Date: 2022-12-1 Date: 2022-12-1
Features: Features:
- Fixed a crash relating to depot bypass through space elevators - Fixed a crash relating to depot bypass through space elevators
---------------------------------------------------------------------------------------------------
Version: 1.0.8
Date: 2022-12-1
Features:
- Fixed a bug with combinator displays not updating correctly
- Improved combinator display performance

View File

@@ -1,6 +1,6 @@
{ {
"name": "cybersyn", "name": "cybersyn",
"version": "1.0.7", "version": "1.0.8",
"title": "Project Cybersyn", "title": "Project Cybersyn",
"author": "Mami", "author": "Mami",
"factorio_version": "1.1", "factorio_version": "1.1",

View File

@@ -9,8 +9,6 @@ local band = bit32.band
local table_remove = table.remove local table_remove = table.remove
local random = math.random local random = math.random
---@param map_data MapData ---@param map_data MapData
---@param station Station ---@param station Station
---@param manifest Manifest ---@param manifest Manifest
@@ -24,8 +22,9 @@ function remove_manifest(map_data, station, manifest, sign)
end end
set_comb2(map_data, station) set_comb2(map_data, station)
station.deliveries_total = station.deliveries_total - 1 station.deliveries_total = station.deliveries_total - 1
if station.deliveries_total == 0 and station.entity_comb1.valid then if station.deliveries_total == 0 and station.display_state >= 2 then
set_comb_operation_with_check(map_data, station.entity_comb1, OPERATION_PRIMARY_IO) station.display_state = station.display_state - 2
update_display(map_data, station)
end end
end end
@@ -206,11 +205,14 @@ local function send_train_between(map_data, r_station_id, p_station_id, train_id
set_comb2(map_data, p_station) set_comb2(map_data, p_station)
set_comb2(map_data, r_station) set_comb2(map_data, r_station)
if p_station.entity_comb1.valid then
set_comb_operation_with_check(map_data, p_station.entity_comb1, OPERATION_PRIMARY_IO_ACTIVE) if p_station.display_state < 2 then
p_station.display_state = 2
update_display(map_data, p_station)
end end
if r_station.entity_comb1.valid then if r_station.display_state < 2 then
set_comb_operation_with_check(map_data, r_station.entity_comb1, OPERATION_PRIMARY_IO_ACTIVE) r_station.display_state = 2
update_display(map_data, r_station)
end end
end end
end end
@@ -258,11 +260,6 @@ local function tick_poll_station(map_data, mod_settings)
station_id = map_data.active_station_ids[tick_data.i] station_id = map_data.active_station_ids[tick_data.i]
station = map_data.stations[station_id] station = map_data.stations[station_id]
if station then if station then
if station.display_update then
update_combinator_display(map_data, station.entity_comb1, station.display_failed_request)
station.display_update = station.display_failed_request
station.display_failed_request = nil
end
if station.network_name and station.deliveries_total < station.entity_stop.trains_limit then if station.network_name and station.deliveries_total < station.entity_stop.trains_limit then
break break
end end
@@ -325,6 +322,9 @@ local function tick_poll_station(map_data, mod_settings)
end end
stations[#stations + 1] = station_id stations[#stations + 1] = station_id
station.p_count_or_r_threshold_per_item[item_name] = r_threshold station.p_count_or_r_threshold_per_item[item_name] = r_threshold
elseif station.display_state%2 == 1 then
station.display_state = station.display_state - 1
update_display(map_data, station)
end end
end end
if flag then if flag then
@@ -391,9 +391,9 @@ local function tick_dispatch(map_data, mod_settings)
else else
for i, id in ipairs(r_stations) do for i, id in ipairs(r_stations) do
local station = stations[id] local station = stations[id]
if station then if station and station.display_state%2 == 0 then
station.display_failed_request = true station.display_state = station.display_state + 1
station.display_update = true update_display(map_data, station)
end end
end end
end end
@@ -419,6 +419,13 @@ local function tick_dispatch(map_data, mod_settings)
end end
end end
if not r_station_i then if not r_station_i then
for i, id in ipairs(r_stations) do
local station = stations[id]
if station and station.display_state%2 == 0 then
station.display_state = station.display_state + 1
update_display(map_data, station)
end
end
return false return false
end end
@@ -464,8 +471,10 @@ local function tick_dispatch(map_data, mod_settings)
if can_be_serviced then if can_be_serviced then
send_missing_train_alert_for_stops(r_station.entity_stop, stations[p_stations[best_i]].entity_stop) send_missing_train_alert_for_stops(r_station.entity_stop, stations[p_stations[best_i]].entity_stop)
end end
r_station.display_failed_request = true if r_station.display_state%2 == 0 then
r_station.display_update = true r_station.display_state = r_station.display_state + 1
update_display(map_data, r_station)
end
end end
table_remove(r_stations, r_station_i) table_remove(r_stations, r_station_i)

View File

@@ -256,7 +256,7 @@ end
---@param map_data MapData ---@param map_data MapData
---@param unit_number uint ---@param unit_number uint
---@param params ArithmeticCombinatorParameters ---@param params ArithmeticCombinatorParameters
function has_comb_params_changed(map_data, unit_number, params) local function has_comb_params_changed(map_data, unit_number, params)
local old_params = map_data.to_comb_params[unit_number] local old_params = map_data.to_comb_params[unit_number]
if params.operation ~= old_params.operation then if params.operation ~= old_params.operation then
@@ -278,41 +278,26 @@ function has_comb_params_changed(map_data, unit_number, params)
return false return false
end end
---@param map_data MapData ---@param map_data MapData
---@param comb LuaEntity ---@param station Station
---@param op string function update_display(map_data, station)
function set_comb_operation_with_check(map_data, comb, op) local comb = station.entity_comb1
---@type uint if comb.valid then
local unit_number = comb.unit_number local unit_number = comb.unit_number--[[@as uint]]
local control = get_comb_control(comb) local control = get_comb_control(comb)
local params = control.parameters local params = control.parameters
if not has_comb_params_changed(map_data, unit_number, params) then if not has_comb_params_changed(map_data, unit_number, params) then
params.operation = op if station.display_state >= 2 then
control.parameters = params params.operation = OPERATION_PRIMARY_IO_ACTIVE
if (op == OPERATION_PRIMARY_IO_ACTIVE or op == OPERATION_PRIMARY_IO_FAILED_REQUEST) then control.parameters = params
params.operation = OPERATION_PRIMARY_IO params.operation = OPERATION_PRIMARY_IO
end elseif station.display_state == 1 then
map_data.to_comb_params[unit_number] = params
end
end
---@param map_data MapData
---@param comb LuaEntity
---@param is_failed boolean
function update_combinator_display(map_data, comb, is_failed)
---@type uint
local unit_number = comb.unit_number
local control = get_comb_control(comb)
local params = control.parameters
if not has_comb_params_changed(map_data, unit_number, params) then
if is_failed then
if params.operation == OPERATION_PRIMARY_IO then
params.operation = OPERATION_PRIMARY_IO_FAILED_REQUEST params.operation = OPERATION_PRIMARY_IO_FAILED_REQUEST
control.parameters = params control.parameters = params
params.operation = OPERATION_PRIMARY_IO params.operation = OPERATION_PRIMARY_IO
map_data.to_comb_params[unit_number] = params else
params.operation = OPERATION_PRIMARY_IO
control.parameters = params
end end
elseif params.operation == OPERATION_PRIMARY_IO_FAILED_REQUEST then
params.operation = OPERATION_PRIMARY_IO
control.parameters = params
map_data.to_comb_params[unit_number] = params map_data.to_comb_params[unit_number] = params
end end
end end

View File

@@ -20,6 +20,9 @@
---@field public se_tele_old_id {[any]: uint} ---@field public se_tele_old_id {[any]: uint}
---@class Station ---@class Station
---@field public entity_stop LuaEntity
---@field public entity_comb1 LuaEntity
---@field public entity_comb2 LuaEntity?
---@field public is_p boolean ---@field public is_p boolean
---@field public is_r boolean ---@field public is_r boolean
---@field public allows_all_trains boolean ---@field public allows_all_trains boolean
@@ -28,19 +31,15 @@
---@field public priority int --transient ---@field public priority int --transient
---@field public r_threshold int >= 0 --transient ---@field public r_threshold int >= 0 --transient
---@field public locked_slots int >= 0 --transient ---@field public locked_slots int >= 0 --transient
---@field public entity_stop LuaEntity
---@field public entity_comb1 LuaEntity
---@field public entity_comb2 LuaEntity?
---@field public wagon_combs {[int]: LuaEntity}?--NOTE: allowed to be invalid entities or combinators with the wrong operation, these must be checked and lazy deleted when found
---@field public deliveries {[string]: int}
---@field public network_name string? ---@field public network_name string?
---@field public network_flag int --transient ---@field public network_flag int --transient
---@field public wagon_combs {[int]: LuaEntity}?--NOTE: allowed to be invalid entities or combinators with the wrong operation, these must be checked and lazy deleted when found
---@field public deliveries {[string]: int}
---@field public accepted_layouts {[uint]: true?} ---@field public accepted_layouts {[uint]: true?}
---@field public layout_pattern {[uint]: int} ---@field public layout_pattern {[uint]: int}
---@field public tick_signals {[uint]: Signal}? --transient ---@field public tick_signals {[uint]: Signal}? --transient
---@field public p_count_or_r_threshold_per_item {[string]: int} --transient ---@field public p_count_or_r_threshold_per_item {[string]: int} --transient
---@field public display_failed_request true? ---@field public display_state 0|1|2|3 --low bit is if this station's request has failed, high bit is if a train is heading to this station
---@field public display_update true?
---@class Depot ---@class Depot
---@field public entity_stop LuaEntity ---@field public entity_stop LuaEntity
@@ -57,7 +56,7 @@
---@field public r_station_id uint ---@field public r_station_id uint
---@field public manifest Manifest ---@field public manifest Manifest
---@field public last_manifest_tick int ---@field public last_manifest_tick int
---@field public has_filtered_wagon boolean ---@field public has_filtered_wagon true?
---@field public is_available true? ---@field public is_available true?
---@field public parked_at_depot_id uint? ---@field public parked_at_depot_id uint?
---@field public depot_name string ---@field public depot_name string
@@ -73,7 +72,7 @@
---@alias cybersyn.global MapData ---@alias cybersyn.global MapData
---@class Economy ---@class Economy
---could contain invalid stations ---could contain invalid stations or stations with modified settings from when they were first appended
---@field public all_r_stations {[string]: uint[]} --{[network_name:item_name]: station_id} ---@field public all_r_stations {[string]: uint[]} --{[network_name:item_name]: station_id}
---@field public all_p_stations {[string]: uint[]} --{[network_name:item_name]: station_id} ---@field public all_p_stations {[string]: uint[]} --{[network_name:item_name]: station_id}
---@field public all_names (string|SignalID)[] ---@field public all_names (string|SignalID)[]

View File

@@ -69,11 +69,12 @@ local function add_available_train(map_data, train_id, train)
end end
end end
---@param map_data MapData ---@param map_data MapData
---@param mod_settings CybersynModSettings
---@param depot_id uint ---@param depot_id uint
---@param depot Depot ---@param depot Depot
---@param train_id uint ---@param train_id uint
---@param train Train ---@param train Train
local function add_available_train_to_depot(map_data, train_id, train, depot_id, depot) local function add_available_train_to_depot(map_data, mod_settings, train_id, train, depot_id, depot)
local comb = depot.entity_comb local comb = depot.entity_comb
local network_name = get_comb_network_name(comb) local network_name = get_comb_network_name(comb)
if network_name then if network_name then
@@ -133,10 +134,11 @@ end
---@param stop LuaEntity ---@param stop LuaEntity
---@param comb LuaEntity ---@param comb LuaEntity
local function on_depot_built(map_data, stop, comb) local function on_depot_built(map_data, stop, comb)
--NOTE: only place where new Depot
local depot = { local depot = {
entity_stop = stop, entity_stop = stop,
entity_comb = comb, entity_comb = comb,
--available_train = nil, available_train_id = nil,
} }
map_data.depots[stop.unit_number] = depot map_data.depots[stop.unit_number] = depot
end end
@@ -159,21 +161,28 @@ end
---@param comb1 LuaEntity ---@param comb1 LuaEntity
---@param comb2 LuaEntity ---@param comb2 LuaEntity
local function on_station_built(map_data, stop, comb1, comb2) local function on_station_built(map_data, stop, comb1, comb2)
--NOTE: only place where new Station
local station = { local station = {
entity_stop = stop, entity_stop = stop,
entity_comb1 = comb1, entity_comb1 = comb1,
entity_comb2 = comb2, entity_comb2 = comb2,
wagon_combs = nil, --is_p = set_station_from_comb_state,
--is_r = set_station_from_comb_state,
--allows_all_trains = set_station_from_comb_state,
deliveries_total = 0, deliveries_total = 0,
last_delivery_tick = map_data.total_ticks, last_delivery_tick = map_data.total_ticks,
priority = 0, priority = 0,
r_threshold = 0, r_threshold = 0,
locked_slots = 0, locked_slots = 0,
--network_name = set_station_from_comb_state,
network_flag = 0, network_flag = 0,
wagon_combs = nil,
deliveries = {}, deliveries = {},
accepted_layouts = {}, accepted_layouts = {},
layout_pattern = nil, layout_pattern = nil,
tick_signals = nil,
p_count_or_r_threshold_per_item = {}, p_count_or_r_threshold_per_item = {},
display_state = 0,
} }
set_station_from_comb_state(station) set_station_from_comb_state(station)
local id = stop.unit_number--[[@as uint]] local id = stop.unit_number--[[@as uint]]
@@ -367,7 +376,7 @@ function on_combinator_network_updated(map_data, comb, network_name)
if train_id then if train_id then
local train = map_data.trains[train_id] local train = map_data.trains[train_id]
remove_available_train(map_data, train_id, train) remove_available_train(map_data, train_id, train)
add_available_train_to_depot(map_data, train_id, train, depot_id, depot) add_available_train_to_depot(map_data, mod_settings, train_id, train, depot_id, depot)
end end
end end
end end
@@ -611,7 +620,7 @@ local function on_train_arrives_depot(map_data, depot_id, train_entity)
end end
if is_train_empty then if is_train_empty then
remove_available_train(map_data, train_id, train) remove_available_train(map_data, train_id, train)
add_available_train_to_depot(map_data, train_id, train, depot_id, map_data.depots[depot_id]) add_available_train_to_depot(map_data, mod_settings, train_id, train, depot_id, map_data.depots[depot_id])
set_depot_schedule(train_entity, train.depot_name) set_depot_schedule(train_entity, train.depot_name)
else else
--train still has cargo --train still has cargo
@@ -620,20 +629,28 @@ local function on_train_arrives_depot(map_data, depot_id, train_entity)
send_nonempty_train_in_depot_alert(train_entity) send_nonempty_train_in_depot_alert(train_entity)
end end
elseif is_train_empty then elseif is_train_empty then
--NOTE: only place where new Train
train = { train = {
status = STATUS_D,
entity = train_entity, entity = train_entity,
layout_id = 0, --layout_id = update_train_layout,
item_slot_capacity = 0, --item_slot_capacity = update_train_layout,
fluid_capacity = 0, --fluid_capacity = update_train_layout,
--status = add_available_train_to_depot,
p_station_id = 0, p_station_id = 0,
r_station_id = 0, r_station_id = 0,
manifest = nil,
last_manifest_tick = map_data.total_ticks, last_manifest_tick = map_data.total_ticks,
--manifest = nil, has_filtered_wagon = nil,
--is_available = add_available_train_to_depot,
--parked_at_depot_id = add_available_train_to_depot,
--depot_name = add_available_train_to_depot,
--network_name = add_available_train_to_depot,
--network_flag = add_available_train_to_depot,
--priority = add_available_train_to_depot,
} }
update_train_layout(map_data, train) update_train_layout(map_data, train)
map_data.trains[train_id] = train map_data.trains[train_id] = train
add_available_train_to_depot(map_data, train_id, train, depot_id, map_data.depots[depot_id]) add_available_train_to_depot(map_data, mod_settings, train_id, train, depot_id, map_data.depots[depot_id])
set_depot_schedule(train_entity, train.depot_name) set_depot_schedule(train_entity, train.depot_name)
else else
@@ -691,7 +708,7 @@ local function on_train_leaves_station(map_data, mod_settings, train_id, train)
set_comb1(map_data, station, nil) set_comb1(map_data, station, nil)
unset_wagon_combs(map_data, station) unset_wagon_combs(map_data, station)
if train.has_filtered_wagon then if train.has_filtered_wagon then
train.has_filtered_wagon = false train.has_filtered_wagon = nil
for carriage_i, carriage in ipairs(train.entity.cargo_wagons) do for carriage_i, carriage in ipairs(train.entity.cargo_wagons) do
local inv = carriage.get_inventory(defines.inventory.cargo_wagon) local inv = carriage.get_inventory(defines.inventory.cargo_wagon)
if inv and inv.is_filtered() then if inv and inv.is_filtered() then

View File

@@ -166,6 +166,24 @@ local migrations_table = {
end end
end end
end, end,
["1.0.8"] = function()
---@type MapData
local map_data = global
map_data.tick_state = STATE_INIT
map_data.tick_data = {}
for id, station in pairs(map_data.stations) do
local params = get_comb_params(station.entity_comb1)
if params.operation == OPERATION_PRIMARY_IO_FAILED_REQUEST then
station.display_state = 1
elseif params.operation == OPERATION_PRIMARY_IO_ACTIVE then
station.display_state = 2
else
station.display_state = 0
end
station.display_failed_request = nil
station.update_display = nil
end
end,
} }
---@param data ConfigurationChangedData ---@param data ConfigurationChangedData