mirror of
https://github.com/Xevion/project-cybersyn.git
synced 2025-12-06 05:16:06 -06:00
migrate global to storage
This commit is contained in:
@@ -142,32 +142,32 @@ mod_settings = {}
|
||||
IS_SE_PRESENT = nil
|
||||
|
||||
function init_global()
|
||||
global.total_ticks = 0
|
||||
global.tick_state = STATE_INIT
|
||||
global.tick_data = {}
|
||||
global.economy = {
|
||||
storage.total_ticks = 0
|
||||
storage.tick_state = STATE_INIT
|
||||
storage.tick_data = {}
|
||||
storage.economy = {
|
||||
all_r_stations = {},
|
||||
all_p_stations = {},
|
||||
all_names = {},
|
||||
}
|
||||
global.to_comb = {}
|
||||
global.to_comb_params = {}
|
||||
global.to_output = {}
|
||||
global.to_stop = {}
|
||||
global.stations = {}
|
||||
global.active_station_ids = {}
|
||||
global.warmup_station_ids = {}
|
||||
global.warmup_station_cycles = {}
|
||||
global.depots = {}
|
||||
global.trains = {}
|
||||
global.available_trains = {}
|
||||
global.layouts = {}
|
||||
global.layout_train_count = {}
|
||||
global.layout_top_id = 1
|
||||
global.refuelers = {}
|
||||
global.to_refuelers = {}
|
||||
global.each_refuelers = {}
|
||||
global.perf_cache = {}
|
||||
storage.to_comb = {}
|
||||
storage.to_comb_params = {}
|
||||
storage.to_output = {}
|
||||
storage.to_stop = {}
|
||||
storage.stations = {}
|
||||
storage.active_station_ids = {}
|
||||
storage.warmup_station_ids = {}
|
||||
storage.warmup_station_cycles = {}
|
||||
storage.depots = {}
|
||||
storage.trains = {}
|
||||
storage.available_trains = {}
|
||||
storage.layouts = {}
|
||||
storage.layout_train_count = {}
|
||||
storage.layout_top_id = 1
|
||||
storage.refuelers = {}
|
||||
storage.to_refuelers = {}
|
||||
storage.each_refuelers = {}
|
||||
storage.perf_cache = {}
|
||||
|
||||
IS_SE_PRESENT = remote.interfaces["space-exploration"] ~= nil
|
||||
end
|
||||
|
||||
@@ -65,7 +65,7 @@ end
|
||||
local function handle_close(e)
|
||||
local element = e.element
|
||||
if not element then return end
|
||||
local comb = global.to_comb[element.tags.id]
|
||||
local comb = storage.to_comb[element.tags.id]
|
||||
if not comb or not comb.valid then return end
|
||||
local player = game.get_player(e.player_index)
|
||||
if not player then return end
|
||||
@@ -80,7 +80,7 @@ end
|
||||
local function handle_drop_down(e)
|
||||
local element = e.element
|
||||
if not element then return end
|
||||
local comb = global.to_comb[element.tags.id]
|
||||
local comb = storage.to_comb[element.tags.id]
|
||||
if not comb or not comb.valid then return end
|
||||
|
||||
set_visibility(element.parent.parent.parent.parent, element.selected_index)
|
||||
@@ -99,25 +99,25 @@ local function handle_drop_down(e)
|
||||
return
|
||||
end
|
||||
|
||||
combinator_update(global, comb)
|
||||
combinator_update(storage, comb)
|
||||
end
|
||||
---@param e EventData.on_gui_switch_state_changed
|
||||
local function handle_pr_switch(e)
|
||||
local element = e.element
|
||||
if not element then return end
|
||||
local comb = global.to_comb[element.tags.id]
|
||||
local comb = storage.to_comb[element.tags.id]
|
||||
if not comb or not comb.valid then return end
|
||||
|
||||
local is_pr_state = (element.switch_state == "none" and 0) or (element.switch_state == "left" and 1) or 2
|
||||
set_comb_is_pr_state(comb, is_pr_state)
|
||||
|
||||
combinator_update(global, comb)
|
||||
combinator_update(storage, comb)
|
||||
end
|
||||
---@param e EventData.on_gui_elem_changed
|
||||
local function handle_network(e)
|
||||
local element = e.element
|
||||
if not element then return end
|
||||
local comb = global.to_comb[element.tags.id]
|
||||
local comb = storage.to_comb[element.tags.id]
|
||||
if not comb or not comb.valid then return end
|
||||
|
||||
local signal = element.elem_value--[[@as SignalID]]
|
||||
@@ -127,29 +127,29 @@ local function handle_network(e)
|
||||
end
|
||||
set_comb_network_name(comb, signal)
|
||||
|
||||
combinator_update(global, comb)
|
||||
combinator_update(storage, comb)
|
||||
end
|
||||
---@param e EventData.on_gui_checked_state_changed
|
||||
local function handle_setting(e)
|
||||
local element = e.element
|
||||
if not element then return end
|
||||
local comb = global.to_comb[element.tags.id]
|
||||
local comb = storage.to_comb[element.tags.id]
|
||||
if not comb or not comb.valid then return end
|
||||
|
||||
set_comb_setting(comb, element.tags.bit--[[@as int]], element.state)
|
||||
|
||||
combinator_update(global, comb)
|
||||
combinator_update(storage, comb)
|
||||
end
|
||||
---@param e EventData.on_gui_checked_state_changed
|
||||
local function handle_setting_flip(e)
|
||||
local element = e.element
|
||||
if not element then return end
|
||||
local comb = global.to_comb[element.tags.id]
|
||||
local comb = storage.to_comb[element.tags.id]
|
||||
if not comb or not comb.valid then return end
|
||||
|
||||
set_comb_setting(comb, element.tags.bit--[[@as int]], not element.state)
|
||||
|
||||
combinator_update(global, comb)
|
||||
combinator_update(storage, comb)
|
||||
end
|
||||
|
||||
local function on_gui_opened(event)
|
||||
@@ -191,7 +191,7 @@ end
|
||||
---@param comb LuaEntity
|
||||
---@param player LuaPlayer
|
||||
function gui_opened(comb, player)
|
||||
combinator_update(global, comb, true)
|
||||
combinator_update(storage, comb, true)
|
||||
|
||||
local rootgui = player.gui.screen
|
||||
local selected_index, signal, switch_state, bits = get_comb_gui_settings(comb)
|
||||
|
||||
@@ -171,17 +171,17 @@ function actions.change_surface(Gui, _, e)
|
||||
end
|
||||
|
||||
function actions.clear_history(Gui)
|
||||
global.flags.deleted_history = true
|
||||
storage.flags.deleted_history = true
|
||||
Gui:schedule_update()
|
||||
end
|
||||
|
||||
function actions.delete_alert(Gui, msg)
|
||||
global.active_data.alerts_to_delete[msg.alert_id] = true
|
||||
storage.active_data.alerts_to_delete[msg.alert_id] = true
|
||||
Gui:schedule_update()
|
||||
end
|
||||
|
||||
function actions.delete_all_alerts(Gui)
|
||||
global.flags.deleted_all_alerts = true
|
||||
storage.flags.deleted_all_alerts = true
|
||||
Gui:schedule_update()
|
||||
end
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ function alerts_tab.update(self)
|
||||
local search_surface = state.surface
|
||||
|
||||
local ltn_alerts = state.ltn_data.alerts
|
||||
local alerts_to_delete = global.active_data.alerts_to_delete
|
||||
local alerts_to_delete = storage.active_data.alerts_to_delete
|
||||
|
||||
local scroll_pane = refs.scroll_pane
|
||||
local children = scroll_pane.children
|
||||
@@ -95,7 +95,7 @@ function alerts_tab.update(self)
|
||||
step = 1
|
||||
end
|
||||
|
||||
if not global.flags.deleted_all_alerts then
|
||||
if not storage.flags.deleted_all_alerts then
|
||||
for sorted_index = start, finish, step do
|
||||
local alert_id = sorted_alerts[sorted_index]
|
||||
local alerts_entry = ltn_alerts[alert_id]
|
||||
|
||||
@@ -95,7 +95,7 @@ function history_tab.update(self)
|
||||
step = 1
|
||||
end
|
||||
|
||||
if not global.flags.deleted_history then
|
||||
if not storage.flags.deleted_history then
|
||||
for sorted_index = start, finish, step do
|
||||
local history_id = sorted_history[sorted_index]
|
||||
local history_entry = ltn_history[history_id]
|
||||
|
||||
@@ -257,7 +257,7 @@ inventory_tab.handle = {}
|
||||
function inventory_tab.wrapper(e, handler)
|
||||
local player = game.get_player(e.player_index)
|
||||
if not player then return end
|
||||
local player_data = global.manager.players[e.player_index]
|
||||
local player_data = storage.manager.players[e.player_index]
|
||||
handler(player, player_data, player_data.refs, e)
|
||||
end
|
||||
|
||||
|
||||
@@ -81,9 +81,9 @@ local function create_player(player_index)
|
||||
refs = manager.create(player),
|
||||
selected_tab = "stations_tab",
|
||||
}
|
||||
global.manager.players[player_index] = player_data
|
||||
storage.manager.players[player_index] = player_data
|
||||
|
||||
--manager.update(global, player, player_data)
|
||||
--manager.update(storage, player, player_data)
|
||||
--top_left_button_update(player, player_data)
|
||||
end
|
||||
|
||||
@@ -92,7 +92,7 @@ function manager_gui.on_player_created(e)
|
||||
end
|
||||
|
||||
function manager_gui.on_player_removed(e)
|
||||
global.manager.players[e.player_index] = nil
|
||||
storage.manager.players[e.player_index] = nil
|
||||
end
|
||||
|
||||
--script.on_event(defines.events.on_player_joined_game, function(e)
|
||||
@@ -107,14 +107,14 @@ function manager_gui.on_runtime_mod_setting_changed(e)
|
||||
local player = game.get_player(e.player_index)
|
||||
if not player then return end
|
||||
|
||||
local player_data = global.manager.players[e.player_index]
|
||||
local player_data = storage.manager.players[e.player_index]
|
||||
player_data.disable_top_left_button = player.mod_settings["cybersyn-disable-top-left-button"].value
|
||||
top_left_button_update(player, player_data)
|
||||
end
|
||||
end
|
||||
|
||||
commands.add_command("cybersyn_rebuild_manager_windows", nil, function(command)
|
||||
local manager_data = global.manager
|
||||
local manager_data = storage.manager
|
||||
if manager_data then
|
||||
|
||||
---@param v PlayerData
|
||||
@@ -161,39 +161,39 @@ end
|
||||
|
||||
|
||||
function manager_gui.on_migration()
|
||||
if not global.manager then
|
||||
if not storage.manager then
|
||||
manager_gui.on_init()
|
||||
end
|
||||
|
||||
for i, p in pairs(game.players) do
|
||||
if global.manager.players[i] == nil then
|
||||
if storage.manager.players[i] == nil then
|
||||
create_player(i)
|
||||
end
|
||||
end
|
||||
|
||||
for i, v in pairs(global.manager.players) do
|
||||
for i, v in pairs(storage.manager.players) do
|
||||
manager_gui.reset_player(i, v)
|
||||
end
|
||||
|
||||
init_items(global.manager)
|
||||
init_items(storage.manager)
|
||||
end
|
||||
|
||||
function manager_gui.on_init()
|
||||
global.manager = {
|
||||
storage.manager = {
|
||||
players = {},
|
||||
}
|
||||
init_items(global.manager)
|
||||
init_items(storage.manager)
|
||||
end
|
||||
--gui.handle_events()
|
||||
|
||||
---@param global cybersyn.global
|
||||
function manager_gui.tick(global)
|
||||
local manager_data = global.manager
|
||||
function manager_gui.tick(storage)
|
||||
local manager_data = storage.manager
|
||||
if manager_data then
|
||||
for i, v in pairs(manager_data.players) do
|
||||
if v.is_manager_open then
|
||||
local query_limit = settings.get_player_settings(i)["cybersyn-manager-result-limit"].value
|
||||
manager.update(global, v, query_limit)
|
||||
manager.update(storage, v, query_limit)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -172,7 +172,7 @@ manager.handle = {}
|
||||
function manager.wrapper(e, handler)
|
||||
local player = game.get_player(e.player_index)
|
||||
if not player then return end
|
||||
local player_data = global.manager.players[e.player_index]
|
||||
local player_data = storage.manager.players[e.player_index]
|
||||
handler(player, player_data, player_data.refs, e)
|
||||
end
|
||||
|
||||
|
||||
@@ -272,7 +272,7 @@ stations_tab.handle = {}
|
||||
function stations_tab.wrapper(e, handler)
|
||||
local player = game.get_player(e.player_index)
|
||||
if not player then return end
|
||||
local player_data = global.manager.players[e.player_index]
|
||||
local player_data = storage.manager.players[e.player_index]
|
||||
handler(player, player_data, player_data.refs, e)
|
||||
end
|
||||
|
||||
@@ -282,7 +282,7 @@ end
|
||||
function stations_tab.handle.open_station_gui(player, player_data, refs, e)
|
||||
local station_id = e.element.tags.station_id
|
||||
--- @type Station
|
||||
local station = global.stations[station_id]
|
||||
local station = storage.stations[station_id]
|
||||
local station_entity = station.entity_stop
|
||||
local station_comb1 = station.entity_comb1
|
||||
local station_comb2 = station.entity_comb2
|
||||
|
||||
@@ -275,7 +275,7 @@ trains_tab.handle = {}
|
||||
function trains_tab.wrapper(e, handler)
|
||||
local player = game.get_player(e.player_index)
|
||||
if not player then return end
|
||||
local player_data = global.manager.players[e.player_index]
|
||||
local player_data = storage.manager.players[e.player_index]
|
||||
handler(player, player_data, player_data.refs, e)
|
||||
end
|
||||
|
||||
@@ -284,7 +284,7 @@ end
|
||||
function trains_tab.handle.open_train_gui(player, player_data, refs, e)
|
||||
local train_id = e.element.tags.train_id
|
||||
--- @type Train
|
||||
local train = global.trains[train_id]
|
||||
local train = storage.trains[train_id]
|
||||
local train_entity = train.entity
|
||||
|
||||
if not train_entity or not train_entity.valid then
|
||||
|
||||
@@ -65,18 +65,18 @@ function remove_train(map_data, train_id, train)
|
||||
remove_available_train(map_data, train_id, train)
|
||||
|
||||
local layout_id = train.layout_id
|
||||
local count = global.layout_train_count[layout_id]
|
||||
local count = storage.layout_train_count[layout_id]
|
||||
if count <= 1 then
|
||||
global.layout_train_count[layout_id] = nil
|
||||
global.layouts[layout_id] = nil
|
||||
for _, stop in pairs(global.stations) do
|
||||
storage.layout_train_count[layout_id] = nil
|
||||
storage.layouts[layout_id] = nil
|
||||
for _, stop in pairs(storage.stations) do
|
||||
stop.accepted_layouts[layout_id] = nil
|
||||
end
|
||||
for _, stop in pairs(global.refuelers) do
|
||||
for _, stop in pairs(storage.refuelers) do
|
||||
stop.accepted_layouts[layout_id] = nil
|
||||
end
|
||||
else
|
||||
global.layout_train_count[layout_id] = count - 1
|
||||
storage.layout_train_count[layout_id] = count - 1
|
||||
end
|
||||
|
||||
map_data.trains[train_id] = nil
|
||||
|
||||
@@ -642,17 +642,17 @@ local function on_built(event)
|
||||
if not entity or not entity.valid then return end
|
||||
|
||||
if entity.name == "train-stop" then
|
||||
on_stop_built_or_updated(global, entity)
|
||||
on_stop_built_or_updated(storage, entity)
|
||||
elseif entity.name == COMBINATOR_NAME then
|
||||
on_combinator_built(global, entity)
|
||||
on_combinator_built(storage, entity)
|
||||
elseif entity.type == "inserter" then
|
||||
update_stop_from_inserter(global, entity)
|
||||
update_stop_from_inserter(storage, entity)
|
||||
elseif entity.type == "loader-1x1" then
|
||||
update_stop_from_loader(global, entity)
|
||||
update_stop_from_loader(storage, entity)
|
||||
elseif entity.type == "pump" then
|
||||
update_stop_from_pump(global, entity)
|
||||
update_stop_from_pump(storage, entity)
|
||||
elseif entity.type == "straight-rail" or entity.type == "curved-rail" then
|
||||
update_stop_from_rail(global, entity)
|
||||
update_stop_from_rail(storage, entity)
|
||||
end
|
||||
end
|
||||
local function on_broken(event)
|
||||
@@ -660,22 +660,22 @@ local function on_broken(event)
|
||||
if not entity or not entity.valid then return end
|
||||
|
||||
if entity.name == "train-stop" then
|
||||
on_stop_broken(global, entity)
|
||||
on_stop_broken(storage, entity)
|
||||
elseif entity.name == COMBINATOR_NAME then
|
||||
on_combinator_broken(global, entity)
|
||||
on_combinator_broken(storage, entity)
|
||||
elseif entity.type == "inserter" then
|
||||
update_stop_from_inserter(global, entity, entity)
|
||||
update_stop_from_inserter(storage, entity, entity)
|
||||
elseif entity.type == "loader-1x1" then
|
||||
update_stop_from_loader(global, entity, entity)
|
||||
update_stop_from_loader(storage, entity, entity)
|
||||
elseif entity.type == "pump" then
|
||||
update_stop_from_pump(global, entity, entity)
|
||||
update_stop_from_pump(storage, entity, entity)
|
||||
elseif entity.type == "straight-rail" or entity.type == "curved-rail" then
|
||||
update_stop_from_rail(global, entity, nil)
|
||||
update_stop_from_rail(storage, entity, nil)
|
||||
elseif entity.train then
|
||||
local train_id = entity.train.id
|
||||
local train = global.trains[train_id]
|
||||
local train = storage.trains[train_id]
|
||||
if train then
|
||||
on_train_broken(global, train_id, train)
|
||||
on_train_broken(storage, train_id, train)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -684,7 +684,7 @@ local function on_rotate(event)
|
||||
if not entity or not entity.valid then return end
|
||||
|
||||
if entity.type == "inserter" then
|
||||
update_stop_from_inserter(global, entity)
|
||||
update_stop_from_inserter(storage, entity)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -694,7 +694,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.valid and entity.name == "train-stop" then
|
||||
on_stop_broken(global, entity)
|
||||
on_stop_broken(storage, entity)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -706,13 +706,13 @@ local function on_paste(event)
|
||||
if not entity or not entity.valid then return end
|
||||
|
||||
if entity.name == COMBINATOR_NAME then
|
||||
combinator_update(global, entity, true)
|
||||
combinator_update(storage, entity, true)
|
||||
end
|
||||
end
|
||||
|
||||
local function on_rename(event)
|
||||
if event.entity.name == "train-stop" then
|
||||
on_stop_rename(global, event.entity, event.old_name)
|
||||
on_stop_rename(storage, event.entity, event.old_name)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -753,7 +753,7 @@ local function setup_se_compat()
|
||||
---@param event {}
|
||||
script.on_event(se_on_train_teleport_started_event, function(event)
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
local old_id = event.old_train_id_1
|
||||
|
||||
local train = map_data.trains[old_id]
|
||||
@@ -765,7 +765,7 @@ local function setup_se_compat()
|
||||
---@param event {}
|
||||
script.on_event(se_on_train_teleport_finished_event, function(event)
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
---@type LuaTrain
|
||||
local train_entity = event.train
|
||||
---@type uint
|
||||
@@ -878,20 +878,20 @@ local function register_tick()
|
||||
if mod_settings.manager_enabled and mod_settings.manager_ups == mod_settings.tps and mod_settings.tps > DELTA then
|
||||
local nth_tick = ceil(60/mod_settings.tps)--[[@as uint]]
|
||||
script.on_nth_tick(nth_tick, function()
|
||||
tick(global, mod_settings)
|
||||
manager.tick(global)
|
||||
tick(storage, mod_settings)
|
||||
manager.tick(storage)
|
||||
end)
|
||||
else
|
||||
if mod_settings.tps > DELTA then
|
||||
local nth_tick_main = ceil(60/mod_settings.tps)--[[@as uint]]
|
||||
script.on_nth_tick(nth_tick_main, function()
|
||||
tick(global, mod_settings)
|
||||
tick(storage, mod_settings)
|
||||
end)
|
||||
end
|
||||
if mod_settings.manager_enabled and mod_settings.manager_ups > DELTA then
|
||||
local nth_tick_manager = ceil(60/mod_settings.manager_ups)--[[@as uint]]
|
||||
script.on_nth_tick(nth_tick_manager, function()
|
||||
manager.tick(global)
|
||||
manager.tick(storage)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -8,7 +8,7 @@ local check_debug_revision
|
||||
local migrations_table = {
|
||||
["1.0.6"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
for k, v in pairs(map_data.available_trains) do
|
||||
for id, _ in pairs(v) do
|
||||
local train = map_data.trains[id]
|
||||
@@ -24,7 +24,7 @@ local migrations_table = {
|
||||
end,
|
||||
["1.0.7"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
map_data.available_trains = {}
|
||||
for id, v in pairs(map_data.trains) do
|
||||
v.parked_at_depot_id = v.depot_id
|
||||
@@ -46,7 +46,7 @@ local migrations_table = {
|
||||
end,
|
||||
["1.0.8"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
for id, station in pairs(map_data.stations) do
|
||||
local params = get_comb_params(station.entity_comb1)
|
||||
if params.operation == MODE_PRIMARY_IO_FAILED_REQUEST then
|
||||
@@ -62,7 +62,7 @@ local migrations_table = {
|
||||
end,
|
||||
["1.1.0"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
map_data.refuelers = {}
|
||||
map_data.to_refuelers = {}
|
||||
for id, station in pairs(map_data.stations) do
|
||||
@@ -87,13 +87,13 @@ local migrations_table = {
|
||||
end,
|
||||
["1.1.2"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
map_data.refuelers = map_data.refuelers or {}
|
||||
map_data.to_refuelers = map_data.to_refuelers or {}
|
||||
end,
|
||||
["1.1.3"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
for k, v in pairs(map_data.refuelers) do
|
||||
if not v.entity_comb.valid or not v.entity_stop.valid then
|
||||
map_data.refuelers[k] = nil
|
||||
@@ -102,7 +102,7 @@ local migrations_table = {
|
||||
end,
|
||||
["1.2.0"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
|
||||
map_data.each_refuelers = {}
|
||||
map_data.se_tele_old_id = nil
|
||||
@@ -158,7 +158,7 @@ local migrations_table = {
|
||||
end,
|
||||
["1.2.2"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
local setting = settings.global["cybersyn-invert-sign"]
|
||||
setting.value = true
|
||||
settings.global["cybersyn-invert-sign"] = setting
|
||||
@@ -211,18 +211,18 @@ local migrations_table = {
|
||||
send_alert_depot_of_train_broken(map_data, train.entity)
|
||||
end
|
||||
local layout_id = train.layout_id
|
||||
local count = global.layout_train_count[layout_id]
|
||||
local count = storage.layout_train_count[layout_id]
|
||||
if count <= 1 then
|
||||
global.layout_train_count[layout_id] = nil
|
||||
global.layouts[layout_id] = nil
|
||||
for _, stop in pairs(global.stations) do
|
||||
storage.layout_train_count[layout_id] = nil
|
||||
storage.layouts[layout_id] = nil
|
||||
for _, stop in pairs(storage.stations) do
|
||||
stop.accepted_layouts[layout_id] = nil
|
||||
end
|
||||
for _, stop in pairs(global.refuelers) do
|
||||
for _, stop in pairs(storage.refuelers) do
|
||||
stop.accepted_layouts[layout_id] = nil
|
||||
end
|
||||
else
|
||||
global.layout_train_count[layout_id] = count - 1
|
||||
storage.layout_train_count[layout_id] = count - 1
|
||||
end
|
||||
map_data.trains[train_id] = nil
|
||||
end
|
||||
@@ -237,14 +237,14 @@ local migrations_table = {
|
||||
end,
|
||||
["1.2.3"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
for _, station in pairs(map_data.stations) do
|
||||
set_station_from_comb(station)
|
||||
end
|
||||
end,
|
||||
["1.2.5"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
local setting = settings.global["cybersyn-invert-sign"]
|
||||
setting.value = true
|
||||
settings.global["cybersyn-invert-sign"] = setting
|
||||
@@ -272,7 +272,7 @@ local migrations_table = {
|
||||
end,
|
||||
["1.2.10"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
map_data.warmup_station_cycles = {}
|
||||
|
||||
local is_registered = {}
|
||||
@@ -298,7 +298,7 @@ local migrations_table = {
|
||||
end,
|
||||
["1.2.15"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
|
||||
for _, e in pairs(map_data.refuelers) do
|
||||
if e.network_flag then
|
||||
@@ -321,7 +321,7 @@ local migrations_table = {
|
||||
end,
|
||||
["1.2.16"] = function()
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
if not map_data.manager then
|
||||
map_data.manager = {
|
||||
players = {},
|
||||
@@ -335,18 +335,18 @@ local migrations_table = {
|
||||
--STATUS_R_TO_D = 5
|
||||
---@param data ConfigurationChangedData
|
||||
function on_config_changed(data)
|
||||
global.tick_state = STATE_INIT
|
||||
global.tick_data = {}
|
||||
global.perf_cache = {}
|
||||
storage.tick_state = STATE_INIT
|
||||
storage.tick_data = {}
|
||||
storage.perf_cache = {}
|
||||
|
||||
flib_migration.on_config_changed(data, migrations_table)
|
||||
|
||||
IS_SE_PRESENT = remote.interfaces["space-exploration"] ~= nil
|
||||
if IS_SE_PRESENT and not global.se_tele_old_id then
|
||||
global.se_tele_old_id = {}
|
||||
if IS_SE_PRESENT and not storage.se_tele_old_id then
|
||||
storage.se_tele_old_id = {}
|
||||
end
|
||||
if global.debug_revision ~= debug_revision then
|
||||
global.debug_revision = debug_revision
|
||||
if storage.debug_revision ~= debug_revision then
|
||||
storage.debug_revision = debug_revision
|
||||
if debug_revision then
|
||||
on_debug_revision_change()
|
||||
end
|
||||
@@ -357,5 +357,5 @@ end
|
||||
---It does not have access to game
|
||||
---NOTE 2: Everything in this section must be idempotent
|
||||
function on_debug_revision_change()
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
end
|
||||
|
||||
@@ -127,9 +127,9 @@ end
|
||||
---@param ... string|int
|
||||
function interface.read_global(...)
|
||||
--this can read anything off of cybersyn's map_data
|
||||
--so interface.read_global("trains", 31415, "manifest") == global.trains[31415].manifest (or nil if train 31415 does not exist)
|
||||
--the second return value is how many parameters could be processed before a nil value was encountered (in the above example it's useful for telling apart global.trains[31415] == nil vs global.trains[31415].manifest == nil)
|
||||
local base = global
|
||||
--so interface.read_global("trains", 31415, "manifest") == storage.trains[31415].manifest (or nil if train 31415 does not exist)
|
||||
--the second return value is how many parameters could be processed before a nil value was encountered (in the above example it's useful for telling apart storage.trains[31415] == nil vs storage.trains[31415].manifest == nil)
|
||||
local base = storage
|
||||
local depth = 0
|
||||
for i, v in ipairs({...}) do
|
||||
depth = i
|
||||
@@ -140,19 +140,19 @@ function interface.read_global(...)
|
||||
end
|
||||
---@param id uint
|
||||
function interface.get_station(id)
|
||||
return global.stations[id]
|
||||
return storage.stations[id]
|
||||
end
|
||||
---@param id uint
|
||||
function interface.get_depot(id)
|
||||
return global.depots[id]
|
||||
return storage.depots[id]
|
||||
end
|
||||
---@param id uint
|
||||
function interface.get_refueler(id)
|
||||
return global.refuelers[id]
|
||||
return storage.refuelers[id]
|
||||
end
|
||||
---@param id uint
|
||||
function interface.get_train(id)
|
||||
return global.trains[id]
|
||||
return storage.trains[id]
|
||||
end
|
||||
---@param train_entity LuaTrain
|
||||
function interface.get_train_id_from_luatrain(train_entity)
|
||||
@@ -164,7 +164,7 @@ function interface.get_id_from_stop(stop)
|
||||
end
|
||||
---@param comb LuaEntity
|
||||
function interface.get_id_from_comb(comb)
|
||||
local stop = global.to_stop[comb.unit_number]
|
||||
local stop = storage.to_stop[comb.unit_number]
|
||||
if stop then
|
||||
return stop.unit_number
|
||||
end
|
||||
@@ -179,7 +179,7 @@ end
|
||||
---@param key string
|
||||
---@param value any
|
||||
function interface.write_setting(key, value)
|
||||
--be careful that the value you write is of the correct type specified in global.lua
|
||||
--be careful that the value you write is of the correct type specified in storage.lua
|
||||
--these settings are not saved and have to be set on load and on init
|
||||
mod_settings[key] = value
|
||||
end
|
||||
@@ -187,28 +187,28 @@ end
|
||||
|
||||
---@param comb LuaEntity
|
||||
function interface.combinator_update(comb)
|
||||
combinator_update(global, comb)
|
||||
combinator_update(storage, comb)
|
||||
end
|
||||
|
||||
---@param train_id uint
|
||||
function interface.update_train_layout(train_id)
|
||||
local train = global.trains[train_id]
|
||||
local train = storage.trains[train_id]
|
||||
assert(train)
|
||||
local old_layout_id = train.layout_id
|
||||
local count = global.layout_train_count[old_layout_id]
|
||||
local count = storage.layout_train_count[old_layout_id]
|
||||
if count <= 1 then
|
||||
global.layout_train_count[old_layout_id] = nil
|
||||
global.layouts[old_layout_id] = nil
|
||||
for _, stop in pairs(global.stations) do
|
||||
storage.layout_train_count[old_layout_id] = nil
|
||||
storage.layouts[old_layout_id] = nil
|
||||
for _, stop in pairs(storage.stations) do
|
||||
stop.accepted_layouts[old_layout_id] = nil
|
||||
end
|
||||
for _, stop in pairs(global.refuelers) do
|
||||
for _, stop in pairs(storage.refuelers) do
|
||||
stop.accepted_layouts[old_layout_id] = nil
|
||||
end
|
||||
else
|
||||
global.layout_train_count[old_layout_id] = count - 1
|
||||
storage.layout_train_count[old_layout_id] = count - 1
|
||||
end
|
||||
set_train_layout(global, train)
|
||||
set_train_layout(storage, train)
|
||||
end
|
||||
---@param layout_pattern (0|1|2|3)[]
|
||||
---@param layout (0|1|2)[]
|
||||
@@ -226,40 +226,40 @@ end
|
||||
function interface.reset_stop_layout(stop_id, forbidden_entity, force_update)
|
||||
local is_station = true
|
||||
---@type Refueler|Station
|
||||
local stop = global.stations[stop_id]
|
||||
local stop = storage.stations[stop_id]
|
||||
if not stop then
|
||||
is_station = false
|
||||
stop = global.refuelers[stop_id]
|
||||
stop = storage.refuelers[stop_id]
|
||||
end
|
||||
assert(stop)
|
||||
if force_update or not stop.allows_all_trains then
|
||||
reset_stop_layout(global, stop, is_station, forbidden_entity)
|
||||
reset_stop_layout(storage, stop, is_station, forbidden_entity)
|
||||
end
|
||||
end
|
||||
---@param rail LuaEntity
|
||||
---@param forbidden_entity LuaEntity?
|
||||
---@param force_update boolean?
|
||||
function interface.update_stop_from_rail(rail, forbidden_entity, force_update)
|
||||
update_stop_from_rail(global, rail, forbidden_entity, force_update)
|
||||
update_stop_from_rail(storage, rail, forbidden_entity, force_update)
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------
|
||||
--[[unsafe API]]
|
||||
------------------------------------------------------------------
|
||||
--NOTE: The following functions can cause serious longterm damage to someone's world if they are given bad parameters. Please refer to global.lua for type information. Use caution.
|
||||
--NOTE: The following functions can cause serious longterm damage to someone's world if they are given bad parameters. Please refer to storage.lua for type information. Use caution.
|
||||
--If there is any useful function missing from this API I'd be happy to add it. Join the Cybersyn discord to request it be added.
|
||||
|
||||
---@param value any
|
||||
---@param ... string|int
|
||||
function interface.write_global(value, ...)
|
||||
--this can write anything into cybersyn's map_data, please be very careful with anything you write, it can cause permanent damage
|
||||
--so interface.write_global(nil, "trains", 31415, "manifest") will cause global.trains[31415].manifest = nil (or return false if train 31415 does not exist)
|
||||
--so interface.write_global(nil, "trains", 31415, "manifest") will cause storage.trains[31415].manifest = nil (or return false if train 31415 does not exist)
|
||||
local params = {...}
|
||||
local size = #params
|
||||
local key = params[size]
|
||||
assert(key ~= nil)
|
||||
local base = global
|
||||
local base = storage
|
||||
for i = 1, size - 1 do
|
||||
base = base[params[i]]
|
||||
if not base then return false end
|
||||
@@ -272,38 +272,38 @@ end
|
||||
---@param manifest Manifest
|
||||
---@param sign -1|1
|
||||
function interface.remove_manifest_from_station_deliveries(station_id, manifest, sign)
|
||||
local station = global.stations[station_id]
|
||||
local station = storage.stations[station_id]
|
||||
assert(station)
|
||||
return remove_manifest(global, station, manifest, sign)
|
||||
return remove_manifest(storage, station, manifest, sign)
|
||||
end
|
||||
---@param r_station_id uint
|
||||
---@param p_station_id uint
|
||||
---@param train_id uint
|
||||
function interface.create_manifest(r_station_id, p_station_id, train_id)
|
||||
local train = global.trains[train_id]
|
||||
assert(global.stations[r_station_id] and global.stations[p_station_id] and train and train.is_available)
|
||||
return create_manifest(global, r_station_id, p_station_id, train_id)
|
||||
local train = storage.trains[train_id]
|
||||
assert(storage.stations[r_station_id] and storage.stations[p_station_id] and train and train.is_available)
|
||||
return create_manifest(storage, r_station_id, p_station_id, train_id)
|
||||
end
|
||||
---@param r_station_id uint
|
||||
---@param p_station_id uint
|
||||
---@param train_id uint
|
||||
---@param manifest Manifest
|
||||
function interface.create_delivery(r_station_id, p_station_id, train_id, manifest)
|
||||
local train = global.trains[train_id]
|
||||
assert(global.stations[r_station_id] and global.stations[p_station_id] and train and train.is_available and manifest)
|
||||
return create_delivery(global, r_station_id, p_station_id, train_id, manifest)
|
||||
local train = storage.trains[train_id]
|
||||
assert(storage.stations[r_station_id] and storage.stations[p_station_id] and train and train.is_available and manifest)
|
||||
return create_delivery(storage, r_station_id, p_station_id, train_id, manifest)
|
||||
end
|
||||
---@param train_id uint
|
||||
function interface.fail_delivery(train_id)
|
||||
local train = global.trains[train_id]
|
||||
local train = storage.trains[train_id]
|
||||
assert(train)
|
||||
return on_failed_delivery(global, train_id, train)
|
||||
return on_failed_delivery(storage, train_id, train)
|
||||
end
|
||||
---@param train_id uint
|
||||
function interface.remove_train(train_id)
|
||||
local train = global.trains[train_id]
|
||||
local train = storage.trains[train_id]
|
||||
assert(train)
|
||||
return remove_train(global, train_id, train)
|
||||
return remove_train(storage, train_id, train)
|
||||
end
|
||||
|
||||
---@param train_id uint
|
||||
@@ -311,26 +311,26 @@ function interface.add_available_train(train_id)
|
||||
--This function marks a train as available but not in a depot so it can do depot bypass, be sure the train has no active deliveries before calling this
|
||||
--available trains can be chosen by the dispatcher to be rescheduled and dispatched for a new delivery
|
||||
--when this train parks at a depot add_available_train_to_depot will be called on it automatically
|
||||
local train = global.trains[train_id]
|
||||
local train = storage.trains[train_id]
|
||||
assert(train)
|
||||
add_available_train(global, train_id, train)
|
||||
add_available_train(storage, train_id, train)
|
||||
end
|
||||
---@param depot_id uint
|
||||
---@param train_id uint
|
||||
function interface.add_available_train_to_depot(train_id, depot_id)
|
||||
--This function marks a train as available and in a depot, be sure the train has no active deliveries before calling this
|
||||
--available trains can be chosen by the dispatcher to be rescheduled and dispatched for a new delivery
|
||||
local train = global.trains[train_id]
|
||||
local depot = global.depots[depot_id]
|
||||
local train = storage.trains[train_id]
|
||||
local depot = storage.depots[depot_id]
|
||||
assert(train and depot)
|
||||
return add_available_train_to_depot(global, mod_settings, train_id, train, depot_id, depot)
|
||||
return add_available_train_to_depot(storage, mod_settings, train_id, train, depot_id, depot)
|
||||
end
|
||||
---@param train_id uint
|
||||
function interface.remove_available_train(train_id)
|
||||
--this function removes a train from the available trains list so it cannot be rescheduled and dispatched. if the train was not already available nothing will happen
|
||||
local train = global.trains[train_id]
|
||||
local train = storage.trains[train_id]
|
||||
assert(train)
|
||||
return remove_available_train(global, train_id, train)
|
||||
return remove_available_train(storage, train_id, train)
|
||||
end
|
||||
|
||||
------------------------------------------------------------------
|
||||
|
||||
@@ -404,15 +404,15 @@ end
|
||||
function on_train_built(event)
|
||||
local train_e = event.train
|
||||
if event.old_train_id_1 then
|
||||
on_train_modified(global, event.old_train_id_1)
|
||||
on_train_modified(storage, event.old_train_id_1)
|
||||
end
|
||||
if event.old_train_id_2 then
|
||||
on_train_modified(global, event.old_train_id_2)
|
||||
on_train_modified(storage, event.old_train_id_2)
|
||||
end
|
||||
end
|
||||
function on_train_changed(event)
|
||||
---@type MapData
|
||||
local map_data = global
|
||||
local map_data = storage
|
||||
local train_e = event.train--[[@as LuaTrain]]
|
||||
if not train_e.valid then return end
|
||||
local train_id = train_e.id
|
||||
|
||||
Reference in New Issue
Block a user