From a049b56bb049122e6358859cb7697207632e07a1 Mon Sep 17 00:00:00 2001 From: Monica Moniot Date: Thu, 1 Dec 2022 14:36:21 -0500 Subject: [PATCH] fixed scheduling for SE --- cybersyn/changelog.txt | 5 ++++ cybersyn/info.json | 2 +- cybersyn/scripts/central-planning.lua | 2 +- cybersyn/scripts/factorio-api.lua | 36 ++++++++++++++++----------- cybersyn/scripts/global.lua | 5 ++-- cybersyn/scripts/main.lua | 2 ++ cybersyn/scripts/migrations.lua | 2 ++ 7 files changed, 35 insertions(+), 19 deletions(-) diff --git a/cybersyn/changelog.txt b/cybersyn/changelog.txt index 32e1c1c..db07a64 100644 --- a/cybersyn/changelog.txt +++ b/cybersyn/changelog.txt @@ -94,3 +94,8 @@ Date: 2022-11-30 Features: - Added depot bypass - Increased inactivity time so burner inserters are fast enough to trigger it +--------------------------------------------------------------------------------------------------- +Version: 1.0.7 +Date: 2022-11-30 + Features: + - Fixed a crash relating to depot bypass through space elevators diff --git a/cybersyn/info.json b/cybersyn/info.json index 34aa5e9..544767c 100644 --- a/cybersyn/info.json +++ b/cybersyn/info.json @@ -1,6 +1,6 @@ { "name": "cybersyn", - "version": "1.0.6", + "version": "1.0.7", "title": "Project Cybersyn", "author": "Mami", "factorio_version": "1.1", diff --git a/cybersyn/scripts/central-planning.lua b/cybersyn/scripts/central-planning.lua index f30b296..ffd6a2c 100644 --- a/cybersyn/scripts/central-planning.lua +++ b/cybersyn/scripts/central-planning.lua @@ -203,7 +203,7 @@ local function send_train_between(map_data, r_station_id, p_station_id, train_id train.manifest = manifest train.last_manifest_tick = map_data.total_ticks - set_manifest_schedule(train.entity, train.depot_name, p_station.entity_stop, r_station.entity_stop, manifest, depot_id ~= nil) + set_manifest_schedule(train.entity, train.depot_name, train.se_depot_surface_i, p_station.entity_stop, r_station.entity_stop, manifest, depot_id ~= nil) set_comb2(map_data, p_station) set_comb2(map_data, r_station) if p_station.entity_comb1.valid then diff --git a/cybersyn/scripts/factorio-api.lua b/cybersyn/scripts/factorio-api.lua index 58bc008..9b9a3a8 100644 --- a/cybersyn/scripts/factorio-api.lua +++ b/cybersyn/scripts/factorio-api.lua @@ -106,21 +106,27 @@ function rename_manifest_schedule(train, stop, old_name) train.schedule = schedule end +---@param elevator_name string +---@param is_train_in_orbit boolean +local function se_create_elevator_order(elevator_name, is_train_in_orbit) + return {station = elevator_name..(is_train_in_orbit and SE_ELEVATOR_ORBIT_SUFFIX or SE_ELEVATOR_PLANET_SUFFIX)} +end ---@param train LuaTrain ---@param depot_name string +---@param d_surface_i int ---@param p_stop LuaEntity ---@param r_stop LuaEntity ---@param manifest Manifest ---@param start_at_depot boolean? -function set_manifest_schedule(train, depot_name, p_stop, r_stop, manifest, start_at_depot) +function set_manifest_schedule(train, depot_name, d_surface_i, p_stop, r_stop, manifest, start_at_depot) --NOTE: train must be on same surface as depot_stop - local d_surface = train.front_stock.surface + local t_surface = train.front_stock.surface local p_surface = p_stop.surface local r_surface = r_stop.surface - local d_surface_i = d_surface.index + local t_surface_i = t_surface.index local p_surface_i = p_surface.index local r_surface_i = r_surface.index - if d_surface_i == p_surface_i and p_surface_i == r_surface_i then + if t_surface_i == p_surface_i and p_surface_i == r_surface_i then train.schedule = {current = start_at_depot and 1 or 2, records = { create_inactivity_order(depot_name), create_direct_to_station_order(p_stop), @@ -129,32 +135,32 @@ function set_manifest_schedule(train, depot_name, p_stop, r_stop, manifest, star create_unloading_order(r_stop), }} return - elseif IS_SE_PRESENT and (d_surface_i == p_surface_i or p_surface_i == r_surface_i or r_surface_i == d_surface_i) then - local d_zone = remote.call("space-exploration", "get_zone_from_surface_index", {surface_index = d_surface_i}) - local other_zone = remote.call("space-exploration", "get_zone_from_surface_index", {surface_index = (d_surface_i == p_surface_i) and r_surface_i or p_surface_i}) - local is_train_in_orbit = other_zone.orbit_index == d_zone.index - if is_train_in_orbit or d_zone.orbit_index == other_zone.index then - local elevator_name = se_get_space_elevator_name(d_surface) + elseif IS_SE_PRESENT and (t_surface_i == p_surface_i or p_surface_i == r_surface_i or r_surface_i == t_surface_i) then + local t_zone = remote.call("space-exploration", "get_zone_from_surface_index", {surface_index = t_surface_i}) + local other_zone = remote.call("space-exploration", "get_zone_from_surface_index", {surface_index = (t_surface_i == p_surface_i) and r_surface_i or p_surface_i}) + local is_train_in_orbit = other_zone.orbit_index == t_zone.index + if is_train_in_orbit or t_zone.orbit_index == other_zone.index then + local elevator_name = se_get_space_elevator_name(t_surface) if elevator_name then local records = {create_inactivity_order(depot_name)} - if d_surface_i == p_surface_i then + if t_surface_i == p_surface_i then records[#records + 1] = create_direct_to_station_order(p_stop) else - records[#records + 1] = {station = elevator_name..(is_train_in_orbit and SE_ELEVATOR_ORBIT_SUFFIX or SE_ELEVATOR_PLANET_SUFFIX)} + records[#records + 1] = se_create_elevator_order(elevator_name, is_train_in_orbit) is_train_in_orbit = not is_train_in_orbit end records[#records + 1] = create_loading_order(p_stop, manifest) if p_surface_i ~= r_surface_i then - records[#records + 1] = {station = elevator_name..(is_train_in_orbit and SE_ELEVATOR_ORBIT_SUFFIX or SE_ELEVATOR_PLANET_SUFFIX)} + records[#records + 1] = se_create_elevator_order(elevator_name, is_train_in_orbit) is_train_in_orbit = not is_train_in_orbit end records[#records + 1] = create_unloading_order(r_stop) if r_surface_i ~= d_surface_i then - records[#records + 1] = {station = elevator_name..(is_train_in_orbit and SE_ELEVATOR_ORBIT_SUFFIX or SE_ELEVATOR_PLANET_SUFFIX)} + records[#records + 1] = se_create_elevator_order(elevator_name, is_train_in_orbit) is_train_in_orbit = not is_train_in_orbit end - train.schedule = {current = 1, records = records} + train.schedule = {current = start_at_depot and 1 or 2, records = records} return end end diff --git a/cybersyn/scripts/global.lua b/cybersyn/scripts/global.lua index a09d155..58de442 100644 --- a/cybersyn/scripts/global.lua +++ b/cybersyn/scripts/global.lua @@ -61,11 +61,12 @@ ---@field public is_available true? ---@field public depot_id uint? ---@field public depot_name string +---@field public se_depot_surface_i uint --se only ---@field public network_name string? ---@field public network_flag int ---@field public priority int ----@field public se_awaiting_removal any? ----@field public se_awaiting_rename any? +---@field public se_awaiting_removal any? --se only +---@field public se_awaiting_rename any? --se only ---@alias Manifest {}[] ---@alias cybersyn.global MapData diff --git a/cybersyn/scripts/main.lua b/cybersyn/scripts/main.lua index d502834..dac7148 100644 --- a/cybersyn/scripts/main.lua +++ b/cybersyn/scripts/main.lua @@ -89,6 +89,7 @@ local function add_available_train_to_depot(map_data, train_id, train, depot_id, train.status = STATUS_D train.depot_id = depot_id train.depot_name = depot.entity_stop.backer_name + train.se_depot_surface_i = depot.entity_stop.surface.index train.network_name = network_name train.network_flag = mod_settings.network_flag train.priority = 0 @@ -568,6 +569,7 @@ local function on_station_rename(map_data, stop, old_name) if depot and depot.available_train_id then local train = map_data.trains[depot.available_train_id--[[@as uint]]] train.depot_name = stop.backer_name + --train.se_depot_surface_i = stop.surface.index end end end diff --git a/cybersyn/scripts/migrations.lua b/cybersyn/scripts/migrations.lua index 3c47f48..00dfcf3 100644 --- a/cybersyn/scripts/migrations.lua +++ b/cybersyn/scripts/migrations.lua @@ -160,6 +160,8 @@ local migrations_table = { if v.is_available then map_data.available_trains[v.network_name--[[@as string]]][id] = true end + --NOTE: we are guessing here because this information was never saved + v.se_depot_surface_i = v.entity.front_stock.surface.index end end end,