simplified api

This commit is contained in:
Monica Moniot
2022-11-22 22:37:09 -05:00
parent 99201beee8
commit c7125a43f1
8 changed files with 131 additions and 111 deletions

1
TODO
View File

@@ -6,3 +6,4 @@ models & art
space elevator compat space elevator compat
railloader 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 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

View File

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

View File

@@ -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 valid_train_exists = false
local is_fluid = item_type == "fluid" 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 if trains then
for train_id, depot_id in pairs(trains) do for train_id, depot_id in pairs(trains) do
local depot = map_data.depots[depot_id] 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 local capacity = (is_fluid and train.fluid_capacity) or train.item_slot_capacity
if if
capacity >= min_slots_to_move and 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 (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]) (p_station.allows_all_trains or p_station.accepted_layouts[layout_id])
then then
valid_train_exists = true valid_train_exists = true
--check if exists valid path --check if exists valid path
--check if path is shortest so we prioritize locality --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 local dist = d_to_p_dist
if capacity > best_capacity or (capacity == best_capacity and dist < best_dist) then 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 economy = map_data.economy
local r_station = map_data.stations[r_station_id] local r_station = map_data.stations[r_station_id]
local p_station = map_data.stations[p_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 ---@type string
local network_name = depot.network_name local network_name = r_station.network_name
local manifest = {} local manifest = {}
@@ -210,7 +210,7 @@ local function send_train_between(map_data, r_station_id, p_station_id, depot, p
end end
end end
remove_available_train(map_data, depot) remove_available_train(map_data, train, depot)
train.status = STATUS_D_TO_P train.status = STATUS_D_TO_P
train.p_station_id = p_station_id train.p_station_id = p_station_id
train.r_station_id = r_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
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 map_data MapData
---@param mod_settings CybersynModSettings ---@param mod_settings CybersynModSettings
local function tick_poll_station(map_data, mod_settings) 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 for i, id in pairs(map_data.warmup_station_ids) do
local station = map_data.stations[id] local station = map_data.stations[id]
if station then 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.active_station_ids[#map_data.active_station_ids + 1] = id
map_data.warmup_station_ids[i] = nil map_data.warmup_station_ids[i] = nil
end end

View File

@@ -78,12 +78,6 @@ function get_comb_secondary_state(param)
local bits = param.second_constant or 0 local bits = param.second_constant or 0
return bits%2 == 1, floor(bits/2)%3 return bits%2 == 1, floor(bits/2)%3
end 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 ---@param station Station
function set_station_from_comb_state(station) function set_station_from_comb_state(station)
--NOTE: this does nothing to update currently active deliveries --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 return param
end 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 comb LuaEntity
---@param signal SignalID? ---@param signal SignalID?
function set_comb_network_name(comb, signal) function set_comb_network_name(comb, signal)

View File

@@ -12,7 +12,7 @@
---@field public warmup_station_ids uint[] ---@field public warmup_station_ids uint[]
---@field public depots {[uint]: Depot} ---@field public depots {[uint]: Depot}
---@field public trains {[uint]: Train} ---@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 layouts {[uint]: string}
---@field public layout_train_count {[uint]: int} ---@field public layout_train_count {[uint]: int}
---@field public tick_state uint ---@field public tick_state uint
@@ -43,25 +43,25 @@
---@field public display_update true? ---@field public display_update true?
---@class Depot ---@class Depot
---@field public priority int --transient
---@field public entity_stop LuaEntity ---@field public entity_stop LuaEntity
---@field public entity_comb LuaEntity ---@field public entity_comb LuaEntity
---@field public network_name string? ---@field public available_train_id uint?--train_id
---@field public network_flag int --transient
---@field public available_train uint?
---@class Train ---@class Train
---@field public entity LuaTrain ---@field public entity LuaTrain
---@field public layout_id uint ---@field public layout_id uint
---@field public item_slot_capacity int ---@field public item_slot_capacity int
---@field public fluid_capacity int ---@field public fluid_capacity int
---@field public depot_name string
---@field public depot Depot?
---@field public status int ---@field public status int
---@field public p_station_id uint ---@field public p_station_id uint
---@field public r_station_id uint ---@field public r_station_id uint
---@field public manifest Manifest ---@field public manifest Manifest
---@field public has_filtered_wagon boolean ---@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 Manifest {}[]
---@alias TrainClass {[uint]: true} ---@alias TrainClass {[uint]: true}
@@ -100,7 +100,7 @@ function init_global()
global.warmup_station_ids = {} global.warmup_station_ids = {}
global.depots = {} global.depots = {}
global.trains = {} global.trains = {}
global.trains_available = {} global.available_trains = {}
global.layouts = {} global.layouts = {}
global.layout_train_count = {} global.layout_train_count = {}
global.layout_top_id = 1 global.layout_top_id = 1

View File

@@ -22,8 +22,9 @@ end
---@param train Train ---@param train Train
---@param train_id uint ---@param train_id uint
function remove_train(map_data, train, train_id) function remove_train(map_data, train, train_id)
if train.depot then if train.depot_id then
remove_available_train(map_data, train.depot) local depot = map_data.depots[train.depot_id]
remove_available_train(map_data, train, depot)
end end
local layout_id = train.layout_id local layout_id = train.layout_id
local count = map_data.layout_train_count[layout_id] local count = map_data.layout_train_count[layout_id]

View File

@@ -51,40 +51,62 @@ end
---@param map_data MapData ---@param map_data MapData
---@param depot Depot ---@param depot_id uint
---@param train_id uint ---@param train_id uint
local function add_available_train(map_data, depot, train_id) local function add_available_train(map_data, depot_id, train_id)
if depot.network_name then local depot = map_data.depots[depot_id]
local network = map_data.trains_available[depot.network_name] 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 if not network then
network = {} network = {}
map_data.trains_available[depot.network_name] = network map_data.available_trains[network_name] = network
end end
network[train_id] = depot.entity_stop.unit_number network[train_id] = depot.entity_stop.unit_number
end end
depot.available_train = train_id depot.available_train_id = train_id
local train = map_data.trains[train_id] train.depot_id = depot_id
train.depot_name = depot.entity_stop.backer_name train.depot_name = depot.entity_stop.backer_name
train.depot = depot train.network_name = network_name
poll_depot(map_data, depot) 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
end
end end
---@param map_data MapData ---@param map_data MapData
---@param train Train
---@param depot Depot ---@param depot Depot
function remove_available_train(map_data, depot) function remove_available_train(map_data, train, depot)
if depot.available_train then ---@type uint
if depot.network_name then local train_id = depot.available_train_id
local network = map_data.trains_available[depot.network_name] if train.network_name then
local network = map_data.available_trains[train.network_name]
if network then if network then
network[depot.available_train] = nil network[train_id] = nil
if next(network) == nil then if next(network) == nil then
map_data.trains_available[depot.network_name] = nil map_data.available_trains[train.network_name] = nil
end end
end end
end end
local train = map_data.trains[depot.available_train] train.depot_id = nil
train.depot = nil depot.available_train_id = nil
depot.available_train = nil
end
end end
@@ -95,22 +117,21 @@ local function on_depot_built(map_data, stop, comb)
local depot = { local depot = {
entity_stop = stop, entity_stop = stop,
entity_comb = comb, entity_comb = comb,
--network_name = nil, --available_train = nil,
priority = 0,
network_flag = 0,
} }
map_data.depots[stop.unit_number] = depot map_data.depots[stop.unit_number] = depot
set_depot_from_comb_state(depot)
poll_depot(map_data, depot)
end end
---@param map_data MapData
---@param depot Depot
local function on_depot_broken(map_data, depot) local function on_depot_broken(map_data, depot)
--remove train local train_id = depot.available_train_id
if depot.available_train then if train_id then
--NOTE: we could remove the schedule from this train local train = map_data.trains[train_id]
--local train = map_data.trains[depot.available_train] train.entity.schedule = nil
map_data.trains[depot.available_train] = nil send_lost_train_alert(train.entity, depot.entity_stop.backer_name)
remove_available_train(map_data, depot) remove_available_train(map_data, train, depot)
map_data.trains[train_id] = nil
end end
map_data.depots[depot.entity_stop.unit_number] = nil map_data.depots[depot.entity_stop.unit_number] = nil
end end
@@ -319,16 +340,14 @@ function on_combinator_network_updated(map_data, comb, network_name)
station.network_name = network_name station.network_name = network_name
end end
else else
local depot = map_data.depots[stop.unit_number] local depot_id = stop.unit_number
if depot.entity_comb == comb then local depot = map_data.depots[depot_id]
if depot.available_train then if depot and depot.entity_comb == comb then
---@type uint local train_id = depot.available_train_id
local train_id = depot.available_train if train_id then
remove_available_train(map_data, depot) local train = map_data.trains[train_id]
depot.network_name = network_name remove_available_train(map_data, train, depot)
add_available_train(map_data, depot, train_id) add_available_train(map_data, depot_id, train_id)
else
depot.network_name = network_name
end end
end 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) local depot_comb = search_for_station_combinator(map_data, stop, OPERATION_DEPOT, comb)
if depot_comb then if depot_comb then
depot.entity_comb = depot_comb depot.entity_comb = depot_comb
set_depot_from_comb_state(depot)
poll_depot(map_data, depot)
else else
on_depot_broken(map_data, depot) on_depot_broken(map_data, depot)
end end
@@ -516,8 +533,8 @@ local function on_station_rename(map_data, stop)
end end
else else
local depot = map_data.depots[station_id] local depot = map_data.depots[station_id]
if depot and depot.available_train then if depot and depot.available_train_id then
local train = map_data.trains[depot.available_train] local train = map_data.trains[depot.available_train_id]
train.depot_name = stop.backer_name train.depot_name = stop.backer_name
end end
end end
@@ -537,9 +554,9 @@ local function find_and_add_all_stations_from_nothing(map_data)
end end
---@param map_data MapData ---@param map_data MapData
---@param depot Depot ---@param depot_id uint
---@param train_entity LuaTrain ---@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 contents = train_entity.get_contents()
local train_id = train_entity.id local train_id = train_entity.id
local train = map_data.trains[train_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.r_station_id = 0
train.manifest = nil train.manifest = nil
train.status = STATUS_D train.status = STATUS_D
add_available_train(map_data, depot, train_id) add_available_train(map_data, depot_id, train_id)
else else
if train.manifest then if train.manifest then
on_failed_delivery(map_data, train) on_failed_delivery(map_data, train)
send_unexpected_train_alert(train.entity) send_unexpected_train_alert(train.entity)
end end
train.status = STATUS_D train.status = STATUS_D
add_available_train(map_data, depot, train_id) add_available_train(map_data, depot_id, train_id)
end end
if next(contents) ~= nil then if next(contents) ~= nil then
--train still has cargo --train still has cargo
@@ -569,8 +586,6 @@ local function on_train_arrives_depot(map_data, depot, train_entity)
end end
elseif next(contents) == nil then elseif next(contents) == nil then
train = { train = {
--depot_name = train_entity.station.backer_name,
--depot = depot,
status = STATUS_D, status = STATUS_D,
entity = train_entity, entity = train_entity,
layout_id = 0, layout_id = 0,
@@ -582,7 +597,8 @@ local function on_train_arrives_depot(map_data, depot, train_entity)
} }
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(map_data, depot, train_id) add_available_train(map_data, depot_id, train_id)
local schedule = create_depot_schedule(train.depot_name) local schedule = create_depot_schedule(train.depot_name)
train_entity.schedule = schedule train_entity.schedule = schedule
else else
@@ -654,8 +670,9 @@ local function on_train_leaves_station(map_data, 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)
end end
elseif train.depot then elseif train.depot_id then
remove_available_train(map_data, train.depot) local depot = map_data.depots[train.depot_id]
remove_available_train(map_data, train, depot)
end end
end end
@@ -752,9 +769,9 @@ local function on_train_changed(event)
on_train_arrives_buffer(global, stop, train) on_train_arrives_buffer(global, stop, train)
end end
else else
local depot = global.depots[stop.unit_number] local depot_id = stop.unit_number
if depot then if global.depots[depot_id] then
on_train_arrives_depot(global, depot, train_e) on_train_arrives_depot(global, depot_id, train_e)
end end
end end
end end

View File

@@ -49,6 +49,32 @@ local migrations_table = {
station.allow_all_trains = nil station.allow_all_trains = nil
end end
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 ---@param data ConfigurationChangedData