Merge pull request #157 from Zoryn4163/main

Initial 2.0 / SA update
This commit is contained in:
Tetlanesh
2024-10-22 17:55:03 +02:00
committed by GitHub
22 changed files with 240 additions and 202 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
.vscode/launch.json
.vscode/settings.json
.vscode/factorio/*
.idea

View File

@@ -1,13 +1,13 @@
{
"name": "cybersyn",
"version": "1.3.0",
"version": "2.0.0",
"title": "Project Cybersyn",
"author": "Mami",
"factorio_version": "1.1",
"factorio_version": "2.0",
"description": "Creates a feature-rich train logistics network through cybernetic combinators. With just this mod you can coordinate the economic inputs and outputs of your entire megabase.",
"dependencies": [
"base",
"flib >= 0.12.0",
"flib >= 0.15.0",
"? space-exploration >= 0.6.94",
"? miniloader",
"? nullius",

View File

@@ -11,7 +11,7 @@ combinator_entity.radius_visualisation_specification = {
--offset = {0, .5},
distance = 1.5,
}
combinator_entity.active_energy_usage = "10KW"
combinator_entity.active_energy_usage = "10kW"
if mods["nullius"] then
combinator_entity.localised_name = { "entity-name.cybersyn-combinator" }
@@ -227,7 +227,7 @@ combinator_out_entity.next_upgrade = nil
combinator_out_entity.minable = nil
combinator_out_entity.selection_box = nil
combinator_out_entity.collision_box = nil
combinator_out_entity.collision_mask = {}
combinator_out_entity.collision_mask = { layers = {} }
combinator_out_entity.item_slot_count = 500
combinator_out_entity.circuit_wire_max_distance = 3
combinator_out_entity.flags = {"not-blueprintable", "not-deconstructable", "placeable-off-grid"}

View File

@@ -224,7 +224,9 @@ styles.ltnm_table_inset_frame_dark = {
styles.ltnm_table_row_frame_light = {
type = "frame_style",
parent = "statistics_table_item_frame",
--parent = "statistics_table_item_frame",
--this is likely incorrect, unsure what the 2.0 equivalent is
parent = "neutral_message_frame",
top_padding = 8,
bottom_padding = 8,
left_padding = 8,
@@ -283,6 +285,13 @@ styles.ltnm_main_warning_frame = {
-- LABEL STYLES
--I am unsure what this was supposed to be in 1.1
local default_orange_color = {
r = 255,
g = 128,
b = 0
}
local hovered_label_color = {
r = 0.5 * (1 + default_orange_color.r),
g = 0.5 * (1 + default_orange_color.g),

View File

@@ -13,7 +13,7 @@ cybersyn_tech = flib.copy_prototype(data.raw["technology"]["automated-rail-trans
cybersyn_tech.icon = "__cybersyn__/graphics/icons/tech.png"
cybersyn_tech.icon_size = 256
cybersyn_tech.prerequisites = {
"rail-signals",
"automated-rail-transportation",
"circuit-network",
}
cybersyn_tech.effects = {

View File

@@ -8,13 +8,13 @@ local string_len = string.len
local DEFINES_WORKING = defines.entity_status.working
local DEFINES_LOW_POWER = defines.entity_status.low_power
local DEFINES_COMBINATOR_INPUT = defines.circuit_connector_id.combinator_input
--local DEFINES_COMBINATOR_INPUT = defines.circuit_connector_id.combinator_input
---@param map_data MapData
---@param item_name string
function get_stack_size(map_data, item_name)
return game.item_prototypes[item_name].stack_size
return prototypes.item[item_name].stack_size
end
---@param item_order table<string, int>
@@ -486,7 +486,7 @@ function set_train_from_comb(mod_settings, train, comb)
train.network_mask = mod_settings.network_mask
end
train.priority = mod_settings.priority
local signals = comb.get_merged_signals(defines.circuit_connector_id.combinator_input)
local signals = comb.get_signals(defines.wire_connector_id.circuit_red, defines.wire_connector_id.circuit_green)
if signals then
for k, v in pairs(signals) do
local item_name = v.signal.name
@@ -534,7 +534,7 @@ function set_refueler_from_comb(map_data, mod_settings, id, refueler)
refueler.network_mask = mod_settings.network_mask
end
local signals = refueler.entity_comb.get_merged_signals(DEFINES_COMBINATOR_INPUT)
local signals = refueler.entity_comb.get_signals(defines.wire_connector_id.circuit_red, defines.wire_connector_id.circuit_green)
if signals then
for k, v in pairs(signals) do
local item_name = v.signal.name
@@ -689,7 +689,40 @@ end
function set_combinator_output(map_data, comb, signals)
local out = map_data.to_output[comb.unit_number]
if out.valid then
out.get_or_create_control_behavior().parameters = signals
--out.get_or_create_control_behavior().parameters = signals
local constBehaviour = out.get_or_create_control_behavior()
if constBehaviour.sections == nil or constBehaviour.sections_count == 0 then
constBehaviour.add_section()
end
if constBehaviour.sections and constBehaviour.sections_count > 0 then
if constBehaviour.sections_count > 1 then
--only the default section, messy but whatever
local i = 1
for _,v in pairs(constBehaviour.sections) do
if i ~= 1 then
constBehaviour.removeSection(i)
end
i = i + 1
end
end
local primarySection = constBehaviour.get_section(1)
local filters = {}
if signals ~= nil then
for _,v in pairs(signals) do
local filt = {
type = v.signal.type,
name = v.signal.name,
quality = nil,
comparator = nil
}
table.insert(filters, filt)
end
end
primarySection.filters = filters
end
end
end
@@ -702,13 +735,13 @@ function get_signals(station)
---@type Signal[]?
local comb2_signals = nil
if status1 == DEFINES_WORKING or status1 == DEFINES_LOW_POWER then
comb1_signals = comb1.get_merged_signals(DEFINES_COMBINATOR_INPUT)
comb1_signals = comb1.get_signals(defines.wire_connector_id.circuit_red, defines.wire_connector_id.circuit_green)
end
local comb2 = station.entity_comb2
if comb2 then
local status2 = comb2.status
if status2 == DEFINES_WORKING or status2 == DEFINES_LOW_POWER then
comb2_signals = comb2.get_merged_signals(DEFINES_COMBINATOR_INPUT)
comb2_signals = comb2.get_signals(defines.wire_connector_id.circuit_red, defines.wire_connector_id.circuit_green)
end
end
return comb1_signals, comb2_signals
@@ -723,7 +756,7 @@ function set_comb2(map_data, station)
local signals = {}
for item_name, count in pairs(deliveries) do
local i = #signals + 1
local is_fluid = game.item_prototypes[item_name] == nil--NOTE: this is expensive
local is_fluid = prototypes.item[item_name] == nil--NOTE: this is expensive
signals[i] = {index = i, signal = {type = is_fluid and "fluid" or "item", name = item_name}, count = sign*count}
end
set_combinator_output(map_data, station.entity_comb2, signals)

View File

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

View File

@@ -1,5 +1,5 @@
--By Mami
local flib_gui = require("__flib__.gui-lite")
local flib_gui = require("__flib__.gui")
local RED = "utility/status_not_working"
local GREEN = "utility/status_working"
@@ -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)
@@ -202,12 +202,12 @@ function gui_opened(comb, player)
{type="flow", name="titlebar", children={
{type="label", style="frame_title", caption={"cybersyn-gui.combinator-title"}, elem_mods={ignored_by_interaction=true}},
{type="empty-widget", style="flib_titlebar_drag_handle", elem_mods={ignored_by_interaction=true}},
{type="sprite-button", style="frame_action_button", mouse_button_filter={"left"}, sprite="utility/close_white", hovered_sprite="utility/close_black", name=COMBINATOR_NAME, handler=handle_close, tags={id=comb.unit_number}}
{type="sprite-button", style="frame_action_button", mouse_button_filter={"left"}, sprite="utility/close", hovered_sprite="utility/close", name=COMBINATOR_NAME, handler=handle_close, tags={id=comb.unit_number}}
}},
{type="frame", name="frame", style="inside_shallow_frame_with_padding", style_mods={padding=12, bottom_padding=9}, children={
{type="flow", name="vflow", direction="vertical", style_mods={horizontal_align="left"}, children={
--status
{type="flow", style="status_flow", direction="horizontal", style_mods={vertical_align="center", horizontally_stretchable=true, bottom_padding=4}, children={
{type="flow", style="flib_titlebar_flow", direction="horizontal", style_mods={vertical_align="center", horizontally_stretchable=true, bottom_padding=4}, children={
{type="sprite", sprite=STATUS_SPRITES[comb.status] or STATUS_SPRITES_DEFAULT, style="status_image", style_mods={stretch_image_to_widget_size=true}},
{type="label", caption={STATUS_NAMES[comb.status] or STATUS_NAMES_DEFAULT}}
}},
@@ -216,7 +216,7 @@ function gui_opened(comb, player)
{type="entity-preview", name="preview", style="wide_entity_button"},
}},
--drop down
{type="label", style="heading_3_label", caption={"cybersyn-gui.operation"}, style_mods={top_padding=8}},
{type="label", style="heading_2_label", caption={"cybersyn-gui.operation"}, style_mods={top_padding=8}},
{type="flow", name="top", direction="horizontal", style_mods={vertical_align="center"}, children={
{type="drop-down", style_mods={top_padding=3, right_margin=8}, handler=handle_drop_down, tags={id=comb.unit_number}, selected_index=selected_index, items={
{"cybersyn-gui.comb1"},
@@ -230,7 +230,7 @@ function gui_opened(comb, player)
}},
---choose-elem-button
{type="line", style_mods={top_padding=10}},
{type="label", name="network_label", style="heading_3_label", caption={"cybersyn-gui.network"}, style_mods={top_padding=8}},
{type="label", name="network_label", style="heading_2_label", caption={"cybersyn-gui.network"}, style_mods={top_padding=8}},
{type="flow", name="bottom", direction="horizontal", style_mods={vertical_align="top"}, children={
{type="choose-elem-button", name="network", style="slot_button_in_shallow_frame", elem_type="signal", tooltip={"cybersyn-gui.network-tooltip"}, signal=signal, style_mods={bottom_margin=1, right_margin=6, top_margin=2}, handler=handle_network, tags={id=comb.unit_number}},
{type="flow", name="depot", direction="vertical", style_mods={horizontal_align="left"}, children={

View File

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

View File

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

View File

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

View File

@@ -1,4 +1,4 @@
local gui = require("__flib__.gui-lite")
local gui = require("__flib__.gui")
local util = require("scripts.gui.util")
local templates = require("scripts.gui.templates")
@@ -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

View File

@@ -1,4 +1,4 @@
local gui = require("__flib__.gui-lite")
local gui = require("__flib__.gui")
local mod_gui = require("__core__.lualib.mod-gui")
local manager = require("scripts.gui.manager")
@@ -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
@@ -135,7 +135,7 @@ local function init_items(manager)
manager.item_order = item_order
local i = 1
for _, protos in pairs{game.item_prototypes, game.fluid_prototypes} do
for _, protos in pairs{prototypes.item, game.fluid_prototypes} do
--- @type (LuaItemPrototype|LuaFluidPrototype)[]
local all_items = {}
for _, proto in pairs(protos) do
@@ -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

View File

@@ -1,4 +1,4 @@
local gui = require("__flib__.gui-lite")
local gui = require("__flib__.gui")
local constants = require("scripts.gui.constants")
@@ -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

View File

@@ -1,4 +1,4 @@
local gui = require("__flib__.gui-lite")
local gui = require("__flib__.gui")
local constants = require("scripts.gui.constants")
local util = require("scripts.gui.util")
@@ -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

View File

@@ -1,5 +1,5 @@
local train_util = require("__flib__.train")
local gui = require("__flib__.gui-lite")
local gui = require("__flib__.gui")
local constants = require("constants")
local util = require("scripts.gui.util")
@@ -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

View File

@@ -1,4 +1,4 @@
local gui = require("__flib__.gui-lite")
local gui = require("__flib__.gui")
local format = require("__flib__.format")
local util = {}

View File

@@ -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
@@ -378,7 +378,7 @@ function set_refueler_combs(map_data, refueler, train)
else
name = a.name
end
if game.item_prototypes[name] then
if prototypes.item[name] then
wagon_signals[1] = {index = 1, signal = {type = "item", name = a.name}, count = 1}
end
end

View File

@@ -259,16 +259,11 @@ local function on_combinator_built(map_data, comb)
force = comb.force
})
assert(out, "cybersyn: could not spawn combinator controller")
comb.connect_neighbour({
target_entity = out,
source_circuit_id = defines.circuit_connector_id.combinator_output,
wire = defines.wire_type.green,
})
comb.connect_neighbour({
target_entity = out,
source_circuit_id = defines.circuit_connector_id.combinator_output,
wire = defines.wire_type.red,
})
local wireConnectorRed = comb.get_wire_connector(defines.wire_connector_id.circuit_red, true)
local wireConnectorGreen = comb.get_wire_connector(defines.wire_connector_id.circuit_green, true)
wireConnectorRed.connect_to(out.get_wire_connector(defines.wire_connector_id.circuit_red, true))
wireConnectorGreen.connect_to(out.get_wire_connector(defines.wire_connector_id.circuit_green, true))
local control = get_comb_control(comb)
local params = control.parameters
@@ -642,17 +637,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 +655,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 +679,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 +689,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 +701,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 +748,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 +760,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 +873,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

View File

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

View File

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

View File

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