From c7125a43f14c20fb87a46d1ad3d1ecde8286556a Mon Sep 17 00:00:00 2001 From: Monica Moniot Date: Tue, 22 Nov 2022 22:37:09 -0500 Subject: [PATCH] simplified api --- TODO | 1 + cybersyn/info.json | 2 +- cybersyn/scripts/central-planning.lua | 41 ++------ cybersyn/scripts/factorio-api.lua | 14 +-- cybersyn/scripts/global.lua | 16 +-- cybersyn/scripts/layout.lua | 5 +- cybersyn/scripts/main.lua | 137 +++++++++++++++----------- cybersyn/scripts/migrations.lua | 26 +++++ 8 files changed, 131 insertions(+), 111 deletions(-) diff --git a/TODO b/TODO index 52bf5a6..03b6a8a 100644 --- a/TODO +++ b/TODO @@ -6,3 +6,4 @@ models & art space elevator compat railloader compat major bug with copy-paste when the operation is changed by blueprint but it gets copied to the old settings before it's checked for update +catch inserter rotation diff --git a/cybersyn/info.json b/cybersyn/info.json index e27bf34..048d289 100644 --- a/cybersyn/info.json +++ b/cybersyn/info.json @@ -1,6 +1,6 @@ { "name": "cybersyn", - "version": "0.4.1", + "version": "0.4.2", "title": "Project Cybersyn", "author": "Mami", "factorio_version": "1.1", diff --git a/cybersyn/scripts/central-planning.lua b/cybersyn/scripts/central-planning.lua index cf43714..5f4ebb2 100644 --- a/cybersyn/scripts/central-planning.lua +++ b/cybersyn/scripts/central-planning.lua @@ -62,7 +62,7 @@ local function get_valid_train(map_data, r_station_id, p_station_id, item_type, local valid_train_exists = false local is_fluid = item_type == "fluid" - local trains = map_data.trains_available[network_name] + local trains = map_data.available_trains[network_name] if trains then for train_id, depot_id in pairs(trains) do local depot = map_data.depots[depot_id] @@ -73,14 +73,14 @@ local function get_valid_train(map_data, r_station_id, p_station_id, item_type, local capacity = (is_fluid and train.fluid_capacity) or train.item_slot_capacity if capacity >= min_slots_to_move and - btest(netand, depot.network_flag) and + btest(netand, train.network_flag) and (r_station.allows_all_trains or r_station.accepted_layouts[layout_id]) and (p_station.allows_all_trains or p_station.accepted_layouts[layout_id]) then valid_train_exists = true --check if exists valid path --check if path is shortest so we prioritize locality - local d_to_p_dist = get_stop_dist(depot.entity_stop, p_station.entity_stop) - DEPOT_PRIORITY_MULT*depot.priority + local d_to_p_dist = get_stop_dist(depot.entity_stop, p_station.entity_stop) - DEPOT_PRIORITY_MULT*train.priority local dist = d_to_p_dist if capacity > best_capacity or (capacity == best_capacity and dist < best_dist) then @@ -110,9 +110,9 @@ local function send_train_between(map_data, r_station_id, p_station_id, depot, p local economy = map_data.economy local r_station = map_data.stations[r_station_id] local p_station = map_data.stations[p_station_id] - local train = map_data.trains[depot.available_train] + local train = map_data.trains[depot.available_train_id] ---@type string - local network_name = depot.network_name + local network_name = r_station.network_name local manifest = {} @@ -210,7 +210,7 @@ local function send_train_between(map_data, r_station_id, p_station_id, depot, p end end - remove_available_train(map_data, depot) + remove_available_train(map_data, train, depot) train.status = STATUS_D_TO_P train.p_station_id = p_station_id train.r_station_id = r_station_id @@ -227,33 +227,6 @@ local function send_train_between(map_data, r_station_id, p_station_id, depot, p end end - ----@param map_data MapData -function poll_depot(map_data, depot) - local comb = depot.entity_comb - if depot.network_name then - depot.priority = 0 - depot.network_flag = 1 - local signals = comb.get_merged_signals(defines.circuit_connector_id.combinator_input) - if signals then - for k, v in pairs(signals) do - local item_name = v.signal.name - local item_count = v.count - if item_name then - if item_name == SIGNAL_PRIORITY then - depot.priority = item_count - end - if item_name == depot.network_name then - depot.network_flag = item_count - end - end - end - end - else - depot.network_flag = 0 - end -end - ---@param map_data MapData ---@param mod_settings CybersynModSettings local function tick_poll_station(map_data, mod_settings) @@ -494,7 +467,7 @@ function tick(map_data, mod_settings) for i, id in pairs(map_data.warmup_station_ids) do 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 + if station.last_delivery_tick + mod_settings.warmup_time*mod_settings.tps >= map_data.total_ticks then--TODO: bug HERE map_data.active_station_ids[#map_data.active_station_ids + 1] = id map_data.warmup_station_ids[i] = nil end diff --git a/cybersyn/scripts/factorio-api.lua b/cybersyn/scripts/factorio-api.lua index 4a3f54d..0752077 100644 --- a/cybersyn/scripts/factorio-api.lua +++ b/cybersyn/scripts/factorio-api.lua @@ -78,12 +78,6 @@ function get_comb_secondary_state(param) local bits = param.second_constant or 0 return bits%2 == 1, floor(bits/2)%3 end ----@param depot Depot -function set_depot_from_comb_state(depot) - local param = get_comb_params(depot.entity_comb) - local signal = param.first_signal - depot.network_name = signal and signal.name or nil -end ---@param station Station function set_station_from_comb_state(station) --NOTE: this does nothing to update currently active deliveries @@ -117,6 +111,14 @@ function set_comb_is_pr_state(comb, is_pr_state) return param end +---@param comb LuaEntity +function get_comb_network_name(comb) + local control = comb.get_or_create_control_behavior()--[[@as LuaArithmeticCombinatorControlBehavior]] + local signal = control.parameters.first_signal + + return signal and signal.name +end + ---@param comb LuaEntity ---@param signal SignalID? function set_comb_network_name(comb, signal) diff --git a/cybersyn/scripts/global.lua b/cybersyn/scripts/global.lua index 9ab230c..60a4c63 100644 --- a/cybersyn/scripts/global.lua +++ b/cybersyn/scripts/global.lua @@ -12,7 +12,7 @@ ---@field public warmup_station_ids uint[] ---@field public depots {[uint]: Depot} ---@field public trains {[uint]: Train} ----@field public trains_available {[string]: {[uint]: uint}} --{[network_name]: {[train_id]: depot_id}} +---@field public available_trains {[string]: {[uint]: uint}} --{[network_name]: {[train_id]: depot_id}} ---@field public layouts {[uint]: string} ---@field public layout_train_count {[uint]: int} ---@field public tick_state uint @@ -43,25 +43,25 @@ ---@field public display_update true? ---@class Depot ----@field public priority int --transient ---@field public entity_stop LuaEntity ---@field public entity_comb LuaEntity ----@field public network_name string? ----@field public network_flag int --transient ----@field public available_train uint? +---@field public available_train_id uint?--train_id ---@class Train ---@field public entity LuaTrain ---@field public layout_id uint ---@field public item_slot_capacity int ---@field public fluid_capacity int ----@field public depot_name string ----@field public depot Depot? ---@field public status int ---@field public p_station_id uint ---@field public r_station_id uint ---@field public manifest Manifest ---@field public has_filtered_wagon boolean +---@field public depot_id uint? +---@field public depot_name string +---@field public network_name string +---@field public network_flag int +---@field public priority int ---@alias Manifest {}[] ---@alias TrainClass {[uint]: true} @@ -100,7 +100,7 @@ function init_global() global.warmup_station_ids = {} global.depots = {} global.trains = {} - global.trains_available = {} + global.available_trains = {} global.layouts = {} global.layout_train_count = {} global.layout_top_id = 1 diff --git a/cybersyn/scripts/layout.lua b/cybersyn/scripts/layout.lua index 7e86c1a..479cc23 100644 --- a/cybersyn/scripts/layout.lua +++ b/cybersyn/scripts/layout.lua @@ -22,8 +22,9 @@ end ---@param train Train ---@param train_id uint function remove_train(map_data, train, train_id) - if train.depot then - remove_available_train(map_data, train.depot) + if train.depot_id then + local depot = map_data.depots[train.depot_id] + remove_available_train(map_data, train, depot) end local layout_id = train.layout_id local count = map_data.layout_train_count[layout_id] diff --git a/cybersyn/scripts/main.lua b/cybersyn/scripts/main.lua index a68b927..4754cc6 100644 --- a/cybersyn/scripts/main.lua +++ b/cybersyn/scripts/main.lua @@ -51,41 +51,63 @@ end ---@param map_data MapData ----@param depot Depot +---@param depot_id uint ---@param train_id uint -local function add_available_train(map_data, depot, train_id) - if depot.network_name then - local network = map_data.trains_available[depot.network_name] +local function add_available_train(map_data, depot_id, train_id) + local depot = map_data.depots[depot_id] + local train = map_data.trains[train_id] + local comb = depot.entity_comb + local network_name = get_comb_network_name(comb) + if network_name then + local network = map_data.available_trains[network_name] if not network then network = {} - map_data.trains_available[depot.network_name] = network + map_data.available_trains[network_name] = network end network[train_id] = depot.entity_stop.unit_number end - depot.available_train = train_id - local train = map_data.trains[train_id] + depot.available_train_id = train_id + train.depot_id = depot_id train.depot_name = depot.entity_stop.backer_name - train.depot = depot - poll_depot(map_data, depot) -end ----@param map_data MapData ----@param depot Depot -function remove_available_train(map_data, depot) - if depot.available_train then - if depot.network_name then - local network = map_data.trains_available[depot.network_name] - if network then - network[depot.available_train] = nil - if next(network) == nil then - map_data.trains_available[depot.network_name] = nil + train.network_name = network_name + train.network_flag = mod_settings.network_flag + train.priority = 0 + if network_name then + local signals = comb.get_merged_signals(defines.circuit_connector_id.combinator_input) + if signals then + for k, v in pairs(signals) do + local item_name = v.signal.name + local item_count = v.count + if item_name then + if item_name == SIGNAL_PRIORITY then + train.priority = item_count + end + if item_name == network_name then + train.network_flag = item_count + end end end end - local train = map_data.trains[depot.available_train] - train.depot = nil - depot.available_train = nil end end +---@param map_data MapData +---@param train Train +---@param depot Depot +function remove_available_train(map_data, train, depot) + ---@type uint + local train_id = depot.available_train_id + if train.network_name then + local network = map_data.available_trains[train.network_name] + if network then + network[train_id] = nil + if next(network) == nil then + map_data.available_trains[train.network_name] = nil + end + end + end + train.depot_id = nil + depot.available_train_id = nil +end ---@param map_data MapData @@ -95,22 +117,21 @@ local function on_depot_built(map_data, stop, comb) local depot = { entity_stop = stop, entity_comb = comb, - --network_name = nil, - priority = 0, - network_flag = 0, + --available_train = nil, } map_data.depots[stop.unit_number] = depot - set_depot_from_comb_state(depot) - poll_depot(map_data, depot) end +---@param map_data MapData +---@param depot Depot local function on_depot_broken(map_data, depot) - --remove train - if depot.available_train then - --NOTE: we could remove the schedule from this train - --local train = map_data.trains[depot.available_train] - map_data.trains[depot.available_train] = nil - remove_available_train(map_data, depot) + local train_id = depot.available_train_id + if train_id then + local train = map_data.trains[train_id] + train.entity.schedule = nil + send_lost_train_alert(train.entity, depot.entity_stop.backer_name) + remove_available_train(map_data, train, depot) + map_data.trains[train_id] = nil end map_data.depots[depot.entity_stop.unit_number] = nil end @@ -319,16 +340,14 @@ function on_combinator_network_updated(map_data, comb, network_name) station.network_name = network_name end else - local depot = map_data.depots[stop.unit_number] - if depot.entity_comb == comb then - if depot.available_train then - ---@type uint - local train_id = depot.available_train - remove_available_train(map_data, depot) - depot.network_name = network_name - add_available_train(map_data, depot, train_id) - else - depot.network_name = network_name + local depot_id = stop.unit_number + local depot = map_data.depots[depot_id] + if depot and depot.entity_comb == comb then + local train_id = depot.available_train_id + if train_id then + local train = map_data.trains[train_id] + remove_available_train(map_data, train, depot) + add_available_train(map_data, depot_id, train_id) end end end @@ -369,8 +388,6 @@ local function on_combinator_broken(map_data, comb) local depot_comb = search_for_station_combinator(map_data, stop, OPERATION_DEPOT, comb) if depot_comb then depot.entity_comb = depot_comb - set_depot_from_comb_state(depot) - poll_depot(map_data, depot) else on_depot_broken(map_data, depot) end @@ -516,8 +533,8 @@ local function on_station_rename(map_data, stop) end else local depot = map_data.depots[station_id] - if depot and depot.available_train then - local train = map_data.trains[depot.available_train] + if depot and depot.available_train_id then + local train = map_data.trains[depot.available_train_id] train.depot_name = stop.backer_name end end @@ -537,9 +554,9 @@ local function find_and_add_all_stations_from_nothing(map_data) end ---@param map_data MapData ----@param depot Depot +---@param depot_id uint ---@param train_entity LuaTrain -local function on_train_arrives_depot(map_data, depot, train_entity) +local function on_train_arrives_depot(map_data, depot_id, train_entity) local contents = train_entity.get_contents() local train_id = train_entity.id local train = map_data.trains[train_id] @@ -550,14 +567,14 @@ local function on_train_arrives_depot(map_data, depot, train_entity) train.r_station_id = 0 train.manifest = nil train.status = STATUS_D - add_available_train(map_data, depot, train_id) + add_available_train(map_data, depot_id, train_id) else if train.manifest then on_failed_delivery(map_data, train) send_unexpected_train_alert(train.entity) end train.status = STATUS_D - add_available_train(map_data, depot, train_id) + add_available_train(map_data, depot_id, train_id) end if next(contents) ~= nil then --train still has cargo @@ -569,8 +586,6 @@ local function on_train_arrives_depot(map_data, depot, train_entity) end elseif next(contents) == nil then train = { - --depot_name = train_entity.station.backer_name, - --depot = depot, status = STATUS_D, entity = train_entity, layout_id = 0, @@ -582,7 +597,8 @@ local function on_train_arrives_depot(map_data, depot, train_entity) } update_train_layout(map_data, train) map_data.trains[train_id] = train - add_available_train(map_data, depot, train_id) + add_available_train(map_data, depot_id, train_id) + local schedule = create_depot_schedule(train.depot_name) train_entity.schedule = schedule else @@ -654,8 +670,9 @@ local function on_train_leaves_station(map_data, train) set_comb1(map_data, station, nil) unset_wagon_combs(map_data, station) end - elseif train.depot then - remove_available_train(map_data, train.depot) + elseif train.depot_id then + local depot = map_data.depots[train.depot_id] + remove_available_train(map_data, train, depot) end end @@ -752,9 +769,9 @@ local function on_train_changed(event) on_train_arrives_buffer(global, stop, train) end else - local depot = global.depots[stop.unit_number] - if depot then - on_train_arrives_depot(global, depot, train_e) + local depot_id = stop.unit_number + if global.depots[depot_id] then + on_train_arrives_depot(global, depot_id, train_e) end end end diff --git a/cybersyn/scripts/migrations.lua b/cybersyn/scripts/migrations.lua index 1b0679c..db32a3e 100644 --- a/cybersyn/scripts/migrations.lua +++ b/cybersyn/scripts/migrations.lua @@ -49,6 +49,32 @@ local migrations_table = { station.allow_all_trains = nil end end, + ["0.4.2"] = function() + ---@type MapData + local map_data = global + map_data.tick_state = STATE_INIT + map_data.available_trains = map_data.trains_available + for id, train in pairs(map_data.trains) do + local depot = train.depot + if depot then + train.depot_id = depot.entity_comb.unit_number + train.network_name = depot.network_name + train.network_flag = depot.network_flag + train.priority = depot.priority + else + train.network_name = "" + train.network_flag = 0 + train.priority = 0 + end + end + for id, depot in pairs(map_data.depots) do + map_data.depots[id] = { + entity_comb = depot.entity_comb, + entity_stop = depot.entity_stop, + available_train_id = depot.available_train, + } + end + end, } ---@param data ConfigurationChangedData