added type annotations

This commit is contained in:
Monica Moniot
2022-10-25 13:20:17 -04:00
parent b29efea3ea
commit dd06652c74
5 changed files with 210 additions and 106 deletions

View File

@@ -1,6 +1,8 @@
--By Mami
local send_missing_train_alert_for_stop_icon = {name = MISSING_TRAIN_NAME, type = "fluid"}
---@param r_stop LuaEntity
---@param p_stop LuaEntity
function send_missing_train_alert_for_stops(r_stop, p_stop)
for _, player in pairs(r_stop.force.players) do
player.add_custom_alert(
@@ -13,26 +15,34 @@ function send_missing_train_alert_for_stops(r_stop, p_stop)
end
local send_lost_train_alert_icon = {name = LOST_TRAIN_NAME, type = "fluid"}
---@param train LuaTrain
function send_lost_train_alert(train)
for _, player in pairs(train.force.players) do
player.add_custom_alert(
train,
send_lost_train_alert_icon,
{"cybersyn-messages.lost-train"},
true
)
local loco = train.front_stock or train.back_stock
if loco then
for _, player in pairs(loco.force.players) do
player.add_custom_alert(
loco,
send_lost_train_alert_icon,
{"cybersyn-messages.lost-train"},
true
)
end
end
end
local send_nonempty_train_in_depot_alert_icon = {name = NONEMPTY_TRAIN_NAME, type = "fluid"}
---@param train LuaTrain
function send_nonempty_train_in_depot_alert(train)
for _, player in pairs(train.force.players) do
player.add_custom_alert(
train,
send_nonempty_train_in_depot_alert_icon,
{"cybersyn-messages.nonempty-train"},
true
)
local loco = train.front_stock or train.back_stock
if loco then
for _, player in pairs(loco.force.players) do
player.add_custom_alert(
loco,
send_nonempty_train_in_depot_alert_icon,
{"cybersyn-messages.nonempty-train"},
true
)
end
end
end

View File

@@ -4,6 +4,8 @@ local math = math
local INF = math.huge
local create_loading_order_condition = {type = "inactivity", compare_type = "and", ticks = 120}
---@param stop LuaEntity
---@param manifest Manifest
function create_loading_order(stop, manifest)
local condition = {}
for _, item in ipairs(manifest) do
@@ -25,24 +27,32 @@ function create_loading_order(stop, manifest)
end
local create_unloading_order_condition = {{type = "empty", compare_type = "and"}}
---@param stop LuaEntity
function create_unloading_order(stop)
return {station = stop.backer_name, wait_conditions = create_unloading_order_condition}
end
local create_inactivity_order_condition = {{type = "inactivity", compare_type = "and", ticks = 120}}
---@param depot_name string
function create_inactivity_order(depot_name)
return {station = depot_name, wait_conditions = create_inactivity_order_condition}
end
local create_direct_to_station_order_condition = {{type = "time", compare_type = "and", ticks = 0}}
---@param stop LuaEntity
local function create_direct_to_station_order(stop)
return {rail = stop.connected_rail, rail_direction = stop.connected_rail_direction}
end
---@param depot_name string
function create_depot_schedule(depot_name)
return {current = 1, records = {create_inactivity_order(depot_name)}}
end
---@param depot_name string
---@param p_stop LuaEntity
---@param r_stop LuaEntity
---@param manifest Manifest
function create_manifest_schedule(depot_name, p_stop, r_stop, manifest)
return {current = 1, records = {
create_inactivity_order(depot_name),
@@ -53,16 +63,19 @@ function create_manifest_schedule(depot_name, p_stop, r_stop, manifest)
}}
end
---@param station Station
local function get_signals(station)
if station.comb1.valid then
local signals = station.comb1.get_merged_signals(defines.circuit_connector_id.combinator_input)
if station.entity_comb1.valid then
local signals = station.entity_comb1.get_merged_signals(defines.circuit_connector_id.combinator_input)
return signals
else
return nil
end
end
---@param map_data MapData
---@param comb LuaEntity
---@param signals ConstantCombinatorParameters[]?
function set_combinator_output(map_data, comb, signals)
if comb.valid then
local out = map_data.to_output[comb.unit_number]
@@ -76,8 +89,10 @@ function set_combinator_output(map_data, comb, signals)
end
end
---@param map_data MapData
---@param station Station
local function set_comb2(map_data, station)
if station.comb2 then
if station.entity_comb2 then
local deliveries = station.deliveries
local signals = {}
for item_name, count in pairs(deliveries) do
@@ -85,10 +100,13 @@ local function set_comb2(map_data, station)
local item_type = game.item_prototypes[item_name].type
signals[i] = {index = i, signal = {type = item_type, name = item_name}, count = count}
end
set_combinator_output(map_data, station.comb2, signals)
set_combinator_output(map_data, station.entity_comb2, signals)
end
end
---@param map_data MapData
---@param station Station
---@param manifest Manifest
function remove_manifest(map_data, station, manifest, sign)
local deliveries = station.deliveries
for i, item in ipairs(manifest) do
@@ -101,8 +119,11 @@ function remove_manifest(map_data, station, manifest, sign)
station.deliveries_total = station.deliveries_total - 1
end
---@param map_data MapData
---@param station Station
---@param signal SignalID
local function get_thresholds(map_data, station, signal)
local comb2 = station.comb2
local comb2 = station.entity_comb2
if comb2 and comb2.valid then
local count = comb2.get_merged_signal(signal, defines.circuit_connector_id.combinator_input)
if count > 0 then
@@ -114,16 +135,25 @@ local function get_thresholds(map_data, station, signal)
return station.r_threshold, station.p_threshold
end
---@param stop0 LuaEntity
---@param stop1 LuaEntity
local function get_stop_dist(stop0, stop1)
return get_distance(stop0.position, stop1.position)
end
---@param station Station
---@param layout_id uint
local function station_accepts_layout(station, layout_id)
return true
end
---@param map_data MapData
---@param r_station_id uint
---@param p_station_id uint
---@param item_type string
local function get_valid_train(map_data, r_station_id, p_station_id, item_type)
--NOTE: this code is the critical section for run-time optimization
local r_station = map_data.stations[r_station_id]
@@ -147,7 +177,7 @@ local function get_valid_train(map_data, r_station_id, p_station_id, item_type)
((is_fluid and train.fluid_capacity > 0) or (not is_fluid and train.item_slot_capacity > 0))
and station_accepts_layout(r_station, train.layout_id)
and station_accepts_layout(p_station, train.layout_id)
and train.entity_stop.station
and train.entity.station
then
valid_train_exists = true
--check if exists valid path
@@ -170,6 +200,12 @@ local function get_valid_train(map_data, r_station_id, p_station_id, item_type)
end
---@param map_data MapData
---@param r_station_id uint
---@param p_station_id uint
---@param train Train
---@param primary_item_name string
---@param economy Economy
local function send_train_between(map_data, r_station_id, p_station_id, train, primary_item_name, economy)
local r_station = map_data.stations[r_station_id]
local p_station = map_data.stations[p_station_id]
@@ -185,7 +221,7 @@ local function send_train_between(map_data, r_station_id, p_station_id, train, p
local item_type = v.signal.type
if item_name and item_type and item_type ~= "virtual" then
local effective_item_count = item_count + (r_station.deliveries[item_name] or 0)
local r_threshold, p_threshold = get_thresholds(map_data, r_station, v)
local r_threshold, p_threshold = get_thresholds(map_data, r_station, v.signal)
if -effective_item_count >= r_threshold then
requests[item_name] = -effective_item_count
end
@@ -201,7 +237,7 @@ local function send_train_between(map_data, r_station_id, p_station_id, train, p
local item_type = v.signal.type
if item_name and item_type and item_type ~= "virtual" then
local effective_item_count = item_count + (p_station.deliveries[item_name] or 0)
local r_threshold, p_threshold = get_thresholds(map_data, r_station, v)
local r_threshold, p_threshold = get_thresholds(map_data, r_station, v.signal)
if effective_item_count >= p_threshold then
local r = requests[item_name]
if r then
@@ -293,7 +329,7 @@ local function send_train_between(map_data, r_station_id, p_station_id, train, p
set_comb2(map_data, r_station)
end
---@param map_data MapData
function tick(map_data, mod_settings)
local total_ticks = map_data.total_ticks
local stations = map_data.stations
@@ -340,24 +376,26 @@ function tick(map_data, mod_settings)
local item_name = v.signal.name
local item_count = v.count
local effective_item_count = item_count + (station.deliveries[item_name] or 0)
local r_threshold, p_threshold = get_thresholds(map_data, station, v)
if -effective_item_count >= r_threshold then
if r_stations_all[item_name] == nil then
r_stations_all[item_name] = {}
p_stations_all[item_name] = {}
all_items[#all_items + 1] = item_name
all_items[#all_items + 1] = v.signal.type
local r_threshold, p_threshold = get_thresholds(map_data, station, v.signal)
if item_name then
if -effective_item_count >= r_threshold then
if r_stations_all[item_name] == nil then
r_stations_all[item_name] = {}
p_stations_all[item_name] = {}
all_items[#all_items + 1] = item_name
all_items[#all_items + 1] = v.signal.type
end
table.insert(r_stations_all[item_name], station_id)
elseif effective_item_count >= p_threshold then
if r_stations_all[item_name] == nil then
r_stations_all[item_name] = {}
p_stations_all[item_name] = {}
all_items[#all_items + 1] = item_name
all_items[#all_items + 1] = v.signal.type
end
table.insert(p_stations_all[item_name], station_id)
end
table.insert(r_stations_all[item_name], station_id)
elseif effective_item_count >= p_threshold then
if r_stations_all[item_name] == nil then
r_stations_all[item_name] = {}
p_stations_all[item_name] = {}
all_items[#all_items + 1] = item_name
all_items[#all_items + 1] = v.signal.type
end
table.insert(p_stations_all[item_name], station_id)
end
end
end

View File

@@ -1,60 +1,54 @@
--By Mami
---@class MapData
---@field public total_ticks uint
---@field public layout_top_id uint
---@field public to_output {[uint]: LuaEntity}
---@field public to_stop {[uint]: LuaEntity}
---@field public stations {[uint]: Station}
---@field public depots {[uint]: LuaEntity}
---@field public trains {[uint]: Train}
---@field public trains_available {[uint]: boolean}
---@field public layouts {[uint]: string}
---@field public layout_train_count {[uint]: int}
---@field public train_classes {[string]: TrainClass}
---@class Station
---@field public deliveries_total int
---@field public priority int
---@field public last_delivery_tick int
---@field public r_threshold int >= 0
---@field public p_threshold int >= 0
---@field public locked_slots int >= 0
---@field public entity_stop LuaEntity
---@field public entity_comb1 LuaEntity
---@field public entity_comb2 LuaEntity?
---@field public wagon_combs {[int]: LuaEntity}?--allowed to be invalid entities
---@field public deliveries {[string]: int}
---@field public train_class string
---@field public accepted_layouts TrainClass
---@field public layout_pattern string?
---@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 status int
---@field public p_station_id uint
---@field public r_station_id uint
---@field public manifest Manifest
---@alias Manifest {}[]
---@alias TrainClass {[uint]: boolean}
---@alias cybersyn.global MapData
---@class Economy
---@field public r_stations_all {[string]: uint[]}
---@field public p_stations_all {[string]: uint[]}
---@field public all_items string[]
---@field public total_ticks uint
--[[
global: {
total_ticks: int
layout_top_id: int
to_output: {[comb_unit_number]: LuaEntity}
to_stop: {[comb_unit_number]: LuaEntity}
stations: {[stop_id]: Station}
depots: {[stop_id]: LuaEntity}
trains: {[train_id]: Train}
trains_available: {[train_id]: bool}
layouts: {[layout_id]: Layout}
layout_train_count: {[layout_id]: int}
train_classes: {[string]: TrainClass}
}
Station: {
deliveries_total: int
priority: int
last_delivery_tick: int
r_threshold: int >= 0
p_threshold: int >= 0
locked_slots: int >= 0
entity_stop: LuaEntity
entity_comb1: LuaEntity
entity_comb2: LuaEntity?
wagon_combs: {[int]: LuaEntity}--allowed to be invalid entities
deliveries: {
[item_name]: int
}
deliveries: {
[item_name]: item-type
}
train_class: string
accepted_layouts: TrainClass
layout_pattern: string|nil
}
Train: {
entity: LuaEntity
layout_id: int
item_slot_capacity: int
fluid_capacity: int
depot_name: string
status: int
p_station_id: stop_id
r_station_id: stop_id
manifest: [{
name: string
type: string
count: int
}]
}
TrainClass: {
[layout_id]: bool
}
Layout: string
]]
--TODO: only init once
mod_settings = {}
mod_settings.tps = settings.global["cybersyn-ticks-per-second"].value

View File

@@ -1,6 +1,9 @@
--By Mami
local area = require("__flib__.area")
---@param map_data MapData
---@param train Train
---@param train_id uint
function remove_train(map_data, train, train_id)
map_data.trains[train_id] = nil
map_data.trains_available[train_id] = nil
@@ -18,6 +21,8 @@ function remove_train(map_data, train, train_id)
end
end
---@param map_data MapData
---@param train Train
function update_train_layout(map_data, train)
local carriages = train.entity.carriages
local layout = ""
@@ -68,17 +73,18 @@ function update_train_layout(map_data, train)
train.fluid_capacity = fluid_capacity
end
---@param map_data MapData
---@param station Station
local function reset_station_layout(map_data, station)
--station.entity
local station_rail = station.entity.connected_rail
local station_rail = station.entity_stop.connected_rail
local rail_direction_from_station
if station.entity.connected_rail_direction == defines.rail_direction.front then
if station.entity_stop.connected_rail_direction == defines.rail_direction.front then
rail_direction_from_station = defines.rail_direction.back
else
rail_direction_from_station = defines.rail_direction.front
end
local station_direction = station.entity.direction
local surface = station.entity.surface
local station_direction = station.entity_stop.direction
local surface = station.entity_stop.surface
local middle_x = station_rail.position.x
local middle_y = station_rail.position.y
local reach = LONGEST_INSERTER_REACH + 1 - DELTA
@@ -167,6 +173,9 @@ local function reset_station_layout(map_data, station)
end
end
---@param map_data MapData
---@param station Station
---@param train_class_name string
function set_station_train_class(map_data, station, train_class_name)
if train_class_name == TRAIN_CLASS_AUTO then
if station.train_class ~= TRAIN_CLASS_AUTO then
@@ -182,12 +191,16 @@ function set_station_train_class(map_data, station, train_class_name)
end
end
---@param map_data MapData
---@param station Station
function update_station_if_auto(map_data, station)
if station.train_class == TRAIN_CLASS_AUTO then
reset_station_layout(map_data, station)
end
end
---@param map_data MapData
---@param rail LuaEntity
function update_station_from_rail(map_data, rail)
--TODO: search further?
local entity = rail.get_rail_segment_entity(nil, false)
@@ -195,11 +208,15 @@ function update_station_from_rail(map_data, rail)
update_station_if_auto(map_data, map_data.stations[entity.unit_number])
end
end
---@param map_data MapData
---@param pump LuaEntity
function update_station_from_pump(map_data, pump)
if pump.pump_rail_target then
update_station_from_rail(map_data, pump.pump_rail_target)
end
end
---@param map_data MapData
---@param inserter LuaEntity
function update_station_from_inserter(map_data, inserter)
--TODO: check if correct
local surface = inserter.surface

View File

@@ -1,5 +1,7 @@
--By Mami
---@param map_data MapData
---@param train Train
local function on_failed_delivery(map_data, train)
--NOTE: must change train status to STATUS_D or remove it from tracked trains after this call
local is_p_delivery_made = train.status ~= STATUS_D_TO_P and train.status ~= STATUS_P
@@ -7,7 +9,7 @@ local function on_failed_delivery(map_data, train)
local station = map_data.stations[train.p_station_id]
remove_manifest(map_data, station, train.manifest, 1)
if train.status == STATUS_P then
set_combinator_output(map_data, station.comb1, nil)
set_combinator_output(map_data, station.entity_comb1, nil)
end
end
local is_r_delivery_made = train.status == STATUS_R_TO_D
@@ -15,7 +17,7 @@ local function on_failed_delivery(map_data, train)
local station = map_data.stations[train.r_station_id]
remove_manifest(map_data, station, train.manifest, -1)
if train.status == STATUS_R then
set_combinator_output(map_data, station.comb1, nil)
set_combinator_output(map_data, station.entity_comb1, nil)
end
end
train.r_station_id = 0
@@ -23,6 +25,10 @@ local function on_failed_delivery(map_data, train)
train.manifest = nil
end
---@param map_data MapData
---@param stop LuaEntity
---@param comb1 LuaEntity
---@param comb2 LuaEntity
local function on_station_built(map_data, stop, comb1, comb2)
local station = {
entity_stop = stop,
@@ -44,6 +50,9 @@ local function on_station_built(map_data, stop, comb1, comb2)
update_station_if_auto(map_data, station)
end
---@param map_data MapData
---@param station_id uint
---@param station Station
local function on_station_broken(map_data, station_id, station)
if station.deliveries_total > 0 then
--search for trains coming to the destroyed station
@@ -66,6 +75,10 @@ local function on_station_broken(map_data, station_id, station)
map_data.stations[station_id] = nil
end
---@param map_data MapData
---@param stop LuaEntity
---@param comb_operation string
---@param comb_forbidden LuaEntity?
local function search_for_station_combinator(map_data, stop, comb_operation, comb_forbidden)
local pos_x = stop.position.x
local pos_y = stop.position.y
@@ -88,6 +101,8 @@ local function search_for_station_combinator(map_data, stop, comb_operation, com
end
end
---@param map_data MapData
---@param comb LuaEntity
local function on_combinator_built(map_data, comb)
local pos_x = comb.position.x
local pos_y = comb.position.y
@@ -116,6 +131,7 @@ local function on_combinator_built(map_data, comb)
position = comb.position,
force = comb.force
})
assert(out)
comb.connect_neighbour({
target_entity = out,
source_wire_id = defines.circuit_connector_id.combinator_output,
@@ -190,6 +206,8 @@ local function on_combinator_built(map_data, comb)
end
end
end
---@param map_data MapData
---@param comb LuaEntity
local function on_combinator_broken(map_data, comb)
local out = map_data.to_output[comb.unit_number]
local stop = map_data.to_stop[comb.unit_number]
@@ -223,12 +241,16 @@ local function on_combinator_broken(map_data, comb)
map_data.to_output[comb.unit_number] = nil
map_data.to_stop[comb.unit_number] = nil
end
---@param map_data MapData
---@param comb LuaEntity
local function on_combinator_updated(map_data, comb)
--NOTE: this is the lazy way to implement updates and is not robust
on_combinator_broken(map_data, comb)
on_combinator_built(map_data, comb)
end
---@param map_data MapData
---@param stop LuaEntity
local function on_stop_built(map_data, stop)
local pos_x = stop.position.x
local pos_y = stop.position.y
@@ -261,6 +283,8 @@ local function on_stop_built(map_data, stop)
map_data.depots[stop.unit_number] = depot_comb
end
end
---@param map_data MapData
---@param stop LuaEntity
local function on_stop_broken(map_data, stop)
local pos_x = stop.position.x
local pos_y = stop.position.y
@@ -283,6 +307,8 @@ local function on_stop_broken(map_data, stop)
end
map_data.depots[stop.unit_number] = nil
end
---@param map_data MapData
---@param stop LuaEntity
local function on_station_rename(map_data, stop)
--search for trains coming to the renamed station
local station_id = stop.unit_number
@@ -308,6 +334,7 @@ local function on_station_rename(map_data, stop)
end
---@param map_data MapData
local function find_and_add_all_stations_from_nothing(map_data)
for _, surface in pairs(game.surfaces) do
local entities = surface.find_entities_filtered({name = COMBINATOR_NAME})
@@ -320,6 +347,8 @@ local function find_and_add_all_stations_from_nothing(map_data)
end
---@param map_data MapData
---@param train_entity LuaTrain
local function on_train_arrives_depot(map_data, train_entity)
local contents = train_entity.get_contents()
local train = map_data.trains[train_entity.id]
@@ -370,6 +399,9 @@ local function on_train_arrives_depot(map_data, train_entity)
send_nonempty_train_in_depot_alert(train_entity)
end
end
---@param map_data MapData
---@param stop LuaEntity
---@param train Train
local function on_train_arrives_buffer(map_data, stop, train)
local station_id = stop.unit_number
if train.manifest then
@@ -383,6 +415,11 @@ local function on_train_arrives_buffer(map_data, stop, train)
signals[i] = {index = i, signal = {type = item.type, name = item.name}, count = item.count}
end
set_combinator_output(map_data, station.comb1, signals)
if station.wagon_combs then
for i, entity in ipairs(station.wagon_combs) do
end
end
end
elseif train.status == STATUS_P_TO_R then
if train.r_station_id == station_id then
@@ -406,22 +443,27 @@ local function on_train_arrives_buffer(map_data, stop, train)
remove_train(map_data, train, train.entity.id)
end
end
---@param map_data MapData
---@param train Train
local function on_train_leaves_station(map_data, train)
if train.manifest then
if train.status == STATUS_P then
train.status = STATUS_P_TO_R
local station = map_data.stations[train.p_station_id]
remove_manifest(map_data, station, train.manifest, 1)
set_combinator_output(map_data, station.comb1, nil)
set_combinator_output(map_data, station.entity_comb1, nil)
elseif train.status == STATUS_R then
train.status = STATUS_R_TO_D
local station = map_data.stations[train.r_station_id]
remove_manifest(map_data, station, train.manifest, -1)
set_combinator_output(map_data, station.comb1, nil)
set_combinator_output(map_data, station.entity_comb1, nil)
end
end
end
---@param map_data MapData
---@param train Train
local function on_train_broken(map_data, train)
if train.manifest then
on_failed_delivery(map_data, train)
@@ -431,6 +473,9 @@ local function on_train_broken(map_data, train)
end
end
end
---@param map_data MapData
---@param pre_train_id uint
---@param train_entity LuaEntity
local function on_train_modified(map_data, pre_train_id, train_entity)
local train = map_data.trains[pre_train_id]
if train then
@@ -528,7 +573,7 @@ local function on_surface_removed(event)
local train_stops = surface.find_entities_filtered({type = "train-stop"})
for _, entity in pairs(train_stops) do
if entity.name == "train-stop" then
on_station_broken(global, entity)
on_stop_broken(global, entity)
end
end
end