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

@@ -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