mirror of
https://github.com/Xevion/project-cybersyn.git
synced 2025-12-08 14:08:04 -06:00
made depot polling event based
This commit is contained in:
@@ -226,32 +226,9 @@ end
|
|||||||
|
|
||||||
|
|
||||||
---@param map_data MapData
|
---@param map_data MapData
|
||||||
local function tick_poll_depot(map_data)
|
function poll_depot(map_data, depot)
|
||||||
local depot_id
|
|
||||||
do--get next depot id
|
|
||||||
local tick_data = map_data.tick_data
|
|
||||||
while true do
|
|
||||||
if tick_data.network == nil then
|
|
||||||
tick_data.network_name, tick_data.network = next(map_data.trains_available, tick_data.network_name)
|
|
||||||
if tick_data.network == nil then
|
|
||||||
tick_data.train_id = nil
|
|
||||||
map_data.tick_state = STATE_POLL_STATIONS
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
tick_data.train_id, depot_id = next(tick_data.network, tick_data.train_id)
|
|
||||||
if depot_id then
|
|
||||||
break
|
|
||||||
else
|
|
||||||
tick_data.network = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local depot = map_data.depots[depot_id]
|
|
||||||
local comb = depot.entity_comb
|
local comb = depot.entity_comb
|
||||||
if depot.network_name and comb.valid and (comb.status == defines.entity_status.working or comb.status == defines.entity_status.low_power) then
|
if depot.network_name then
|
||||||
depot.priority = 0
|
depot.priority = 0
|
||||||
depot.network_flag = 1
|
depot.network_flag = 1
|
||||||
local signals = comb.get_merged_signals(defines.circuit_connector_id.combinator_input)
|
local signals = comb.get_merged_signals(defines.circuit_connector_id.combinator_input)
|
||||||
@@ -269,12 +246,9 @@ local function tick_poll_depot(map_data)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
|
||||||
depot.priority = 0
|
|
||||||
depot.network_flag = 0
|
|
||||||
end
|
end
|
||||||
return false
|
|
||||||
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)
|
||||||
@@ -284,12 +258,19 @@ local function tick_poll_station(map_data, mod_settings)
|
|||||||
local all_names = map_data.economy.all_names
|
local all_names = map_data.economy.all_names
|
||||||
|
|
||||||
while true do
|
while true do
|
||||||
local station_id, station = next(map_data.stations, tick_data.station_id)
|
tick_data.i = (tick_data.i or 0) + 1
|
||||||
tick_data.station_id = station_id
|
if tick_data.i > #map_data.all_station_ids then
|
||||||
if station == nil then
|
tick_data.i = nil
|
||||||
map_data.tick_state = STATE_DISPATCH
|
map_data.tick_state = STATE_DISPATCH
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
local station_id = map_data.all_station_ids[tick_data.i]
|
||||||
|
local station = map_data.stations[station_id]
|
||||||
|
if station == nil then
|
||||||
|
table_remove(map_data.all_station_ids, tick_data.i)
|
||||||
|
tick_data.i = tick_data.i - 1
|
||||||
|
return false
|
||||||
|
end
|
||||||
if station.display_update then
|
if station.display_update then
|
||||||
update_combinator_display(station.entity_comb1, station.display_failed_request)
|
update_combinator_display(station.entity_comb1, station.display_failed_request)
|
||||||
station.display_update = station.display_failed_request
|
station.display_update = station.display_failed_request
|
||||||
@@ -303,7 +284,7 @@ local function tick_poll_station(map_data, mod_settings)
|
|||||||
station.network_flag = mod_settings.network_flag
|
station.network_flag = mod_settings.network_flag
|
||||||
local signals = get_signals(station)
|
local signals = get_signals(station)
|
||||||
station.tick_signals = signals
|
station.tick_signals = signals
|
||||||
table_clear(station.p_count_or_r_threshold_per_item)
|
station.p_count_or_r_threshold_per_item = {}
|
||||||
if signals then
|
if signals then
|
||||||
for k, v in pairs(signals) do
|
for k, v in pairs(signals) do
|
||||||
local item_name = v.signal.name
|
local item_name = v.signal.name
|
||||||
@@ -379,7 +360,7 @@ local function tick_dispatch(map_data, mod_settings)
|
|||||||
|
|
||||||
local r_stations = tick_data.r_stations
|
local r_stations = tick_data.r_stations
|
||||||
local p_stations = tick_data.p_stations
|
local p_stations = tick_data.p_stations
|
||||||
if not (p_stations and #r_stations > 0 and #p_stations > 0) then
|
if not p_stations then
|
||||||
while true do
|
while true do
|
||||||
local size = #all_names
|
local size = #all_names
|
||||||
if size == 0 then
|
if size == 0 then
|
||||||
@@ -445,7 +426,7 @@ local function tick_dispatch(map_data, mod_settings)
|
|||||||
local best_depot = nil
|
local best_depot = nil
|
||||||
local best_dist = INF
|
local best_dist = INF
|
||||||
local highest_prior = -INF
|
local highest_prior = -INF
|
||||||
local could_have_been_serviced = false
|
local can_be_serviced = false
|
||||||
for j, p_station_id in ipairs(p_stations) do
|
for j, p_station_id in ipairs(p_stations) do
|
||||||
local p_station = stations[p_station_id]
|
local p_station = stations[p_station_id]
|
||||||
if p_station and p_station.p_count_or_r_threshold_per_item[item_name] >= r_threshold then
|
if p_station and p_station.p_count_or_r_threshold_per_item[item_name] >= r_threshold then
|
||||||
@@ -458,17 +439,22 @@ local function tick_dispatch(map_data, mod_settings)
|
|||||||
best_dist = d
|
best_dist = d
|
||||||
best_depot = depot
|
best_depot = depot
|
||||||
highest_prior = prior
|
highest_prior = prior
|
||||||
|
can_be_serviced = true
|
||||||
elseif d < INF then
|
elseif d < INF then
|
||||||
could_have_been_serviced = true
|
can_be_serviced = true
|
||||||
best = j
|
best = j
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if best_depot then
|
if
|
||||||
|
best_depot and (
|
||||||
|
best_depot.entity_comb.status == defines.entity_status.working or
|
||||||
|
best_depot.entity_comb.status == defines.entity_status.low_power)
|
||||||
|
then
|
||||||
send_train_between(map_data, r_station_id, table_remove(p_stations, best), best_depot, item_name)
|
send_train_between(map_data, r_station_id, table_remove(p_stations, best), best_depot, item_name)
|
||||||
else
|
else
|
||||||
if could_have_been_serviced then
|
if can_be_serviced then
|
||||||
send_missing_train_alert_for_stops(r_station.entity_stop, stations[p_stations[best]].entity_stop)
|
send_missing_train_alert_for_stops(r_station.entity_stop, stations[p_stations[best]].entity_stop)
|
||||||
end
|
end
|
||||||
r_station.display_failed_request = true
|
r_station.display_failed_request = true
|
||||||
@@ -485,14 +471,10 @@ function tick(map_data, mod_settings)
|
|||||||
map_data.economy.all_p_stations = {}
|
map_data.economy.all_p_stations = {}
|
||||||
map_data.economy.all_r_stations = {}
|
map_data.economy.all_r_stations = {}
|
||||||
map_data.economy.all_names = {}
|
map_data.economy.all_names = {}
|
||||||
map_data.tick_state = STATE_POLL_DEPOTS
|
map_data.tick_state = STATE_POLL_STATIONS
|
||||||
end
|
end
|
||||||
|
|
||||||
if map_data.tick_state == STATE_POLL_DEPOTS then
|
if map_data.tick_state == STATE_POLL_STATIONS then
|
||||||
for i = 1, 3 do
|
|
||||||
if tick_poll_depot(map_data) then break end
|
|
||||||
end
|
|
||||||
elseif map_data.tick_state == STATE_POLL_STATIONS then
|
|
||||||
for i = 1, 2 do
|
for i = 1, 2 do
|
||||||
if tick_poll_station(map_data, mod_settings) then break end
|
if tick_poll_station(map_data, mod_settings) then break end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -45,6 +45,5 @@ STATION_LAYOUT_NOT_CARGO = "[NF]"
|
|||||||
LONGEST_INSERTER_REACH = 2
|
LONGEST_INSERTER_REACH = 2
|
||||||
|
|
||||||
STATE_INIT = 0
|
STATE_INIT = 0
|
||||||
STATE_POLL_DEPOTS = 1
|
STATE_POLL_STATIONS = 1
|
||||||
STATE_POLL_STATIONS = 2
|
STATE_DISPATCH = 2
|
||||||
STATE_DISPATCH = 3
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
---@field public to_output {[uint]: LuaEntity}
|
---@field public to_output {[uint]: LuaEntity}
|
||||||
---@field public to_stop {[uint]: LuaEntity}
|
---@field public to_stop {[uint]: LuaEntity}
|
||||||
---@field public stations {[uint]: Station}
|
---@field public stations {[uint]: Station}
|
||||||
|
---@field public all_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 trains_available {[string]: {[uint]: uint}} --{[network_name]: {[train_id]: depot_id}}
|
||||||
@@ -77,14 +78,6 @@
|
|||||||
---@type CybersynModSettings
|
---@type CybersynModSettings
|
||||||
mod_settings = {}
|
mod_settings = {}
|
||||||
|
|
||||||
local pairs = pairs
|
|
||||||
---@param tab {}
|
|
||||||
function table_clear(tab)
|
|
||||||
for k, _ in pairs(tab) do
|
|
||||||
tab[k] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function init_global()
|
function init_global()
|
||||||
global.total_ticks = 0
|
global.total_ticks = 0
|
||||||
global.tick_state = STATE_INIT
|
global.tick_state = STATE_INIT
|
||||||
@@ -98,6 +91,7 @@ function init_global()
|
|||||||
global.to_output = {}
|
global.to_output = {}
|
||||||
global.to_stop = {}
|
global.to_stop = {}
|
||||||
global.stations = {}
|
global.stations = {}
|
||||||
|
global.all_station_ids = {}
|
||||||
global.depots = {}
|
global.depots = {}
|
||||||
global.trains = {}
|
global.trains = {}
|
||||||
global.trains_available = {}
|
global.trains_available = {}
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ local function add_available_train(map_data, depot, train_id)
|
|||||||
local train = map_data.trains[train_id]
|
local train = map_data.trains[train_id]
|
||||||
train.depot_name = depot.entity_stop.backer_name
|
train.depot_name = depot.entity_stop.backer_name
|
||||||
train.depot = depot
|
train.depot = depot
|
||||||
|
poll_depot(map_data, depot)
|
||||||
end
|
end
|
||||||
---@param map_data MapData
|
---@param map_data MapData
|
||||||
---@param depot Depot
|
---@param depot Depot
|
||||||
@@ -102,6 +103,7 @@ local function on_depot_built(map_data, stop, comb)
|
|||||||
}
|
}
|
||||||
map_data.depots[stop.unit_number] = depot
|
map_data.depots[stop.unit_number] = depot
|
||||||
set_depot_from_comb_state(depot)
|
set_depot_from_comb_state(depot)
|
||||||
|
poll_depot(map_data, depot)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function on_depot_broken(map_data, depot)
|
local function on_depot_broken(map_data, depot)
|
||||||
@@ -140,7 +142,9 @@ local function on_station_built(map_data, stop, comb1, comb2)
|
|||||||
p_count_or_r_threshold_per_item = {},
|
p_count_or_r_threshold_per_item = {},
|
||||||
}
|
}
|
||||||
set_station_from_comb_state(station)
|
set_station_from_comb_state(station)
|
||||||
map_data.stations[stop.unit_number] = station
|
local id = stop.unit_number--[[@as uint]]
|
||||||
|
map_data.stations[id] = station
|
||||||
|
map_data.all_station_ids[#map_data.all_station_ids + 1] = id
|
||||||
|
|
||||||
update_station_if_auto(map_data, station, nil)
|
update_station_if_auto(map_data, station, nil)
|
||||||
end
|
end
|
||||||
@@ -363,6 +367,7 @@ local function on_combinator_broken(map_data, 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)
|
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
|
||||||
|
|||||||
@@ -5,14 +5,16 @@ local migrations_table = {
|
|||||||
["0.2.0"] = function()
|
["0.2.0"] = function()
|
||||||
---@type MapData
|
---@type MapData
|
||||||
local map_data = global
|
local map_data = global
|
||||||
for k, station in pairs(map_data.stations) do
|
map_data.tick_state = STATE_INIT
|
||||||
|
map_data.all_station_ids = {}
|
||||||
|
for id, station in pairs(map_data.stations) do
|
||||||
station.p_count_or_r_threshold_per_item = {}
|
station.p_count_or_r_threshold_per_item = {}
|
||||||
station.p_threshold = nil
|
station.p_threshold = nil
|
||||||
station.is_all = nil
|
station.is_all = nil
|
||||||
set_station_from_comb_state(station)
|
set_station_from_comb_state(station)
|
||||||
set_combinator_operation(station.entity_comb1, OPERATION_PRIMARY_IO)
|
set_combinator_operation(station.entity_comb1, OPERATION_PRIMARY_IO)
|
||||||
|
map_data.all_station_ids[#map_data.all_station_ids + 1] = id
|
||||||
end
|
end
|
||||||
map_data.tick_state = STATE_INIT
|
|
||||||
end,
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user