diff --git a/cybersyn/scripts/global.lua b/cybersyn/scripts/global.lua index e5a5226..fb85e85 100644 --- a/cybersyn/scripts/global.lua +++ b/cybersyn/scripts/global.lua @@ -97,7 +97,8 @@ ---@field public all_p_stations {[string]: uint[]} --{["network_name:item_name"]: station_id} ---@field public all_names (string|SignalID)[] ---NOTE: any setting labeled as an interface setting can only be changed through the remote-interface, these settings are not save and have to be set at initialization +--NOTE: any setting labeled as an "interface setting" can only be changed through the remote-interface, these settings are not save and have to be set at initialization +--As a modder using the remote-interface, you may override any of these settings, including user settings. They will have to be overriden at initialization and whenever a user tries to change one. ---@class CybersynModSettings ---@field public tps double ---@field public update_rate int diff --git a/cybersyn/scripts/main.lua b/cybersyn/scripts/main.lua index 84c219f..aa233c8 100644 --- a/cybersyn/scripts/main.lua +++ b/cybersyn/scripts/main.lua @@ -672,6 +672,7 @@ local function on_settings_changed(event) script.on_nth_tick(nil) end end + interface_raise_on_mod_settings_changed(event) end local function setup_se_compat() diff --git a/cybersyn/scripts/remote-interface.lua b/cybersyn/scripts/remote-interface.lua index 9d6f825..f1085c5 100644 --- a/cybersyn/scripts/remote-interface.lua +++ b/cybersyn/scripts/remote-interface.lua @@ -26,6 +26,7 @@ local on_train_stuck = nil local on_train_teleport_started = nil local on_train_teleported = nil local on_tick_init = nil +local on_mod_settings_changed = nil local interface = {} ------------------------------------------------------------------ @@ -104,6 +105,10 @@ function interface.get_on_tick_init() if not on_tick_init then on_tick_init = script_generate_event_name() end return on_tick_init end +function interface.get_on_mod_settings_changed() + if not on_mod_settings_changed then on_mod_settings_changed = script_generate_event_name() end + return on_mod_settings_changed +end ------------------------------------------------------------------ @@ -537,3 +542,8 @@ function interface_raise_tick_init() }) end end +function interface_raise_on_mod_settings_changed(e) + if on_mod_settings_changed then + raise_event(on_mod_settings_changed, e) + end +end diff --git a/cybersyn/scripts/train-events.lua b/cybersyn/scripts/train-events.lua index 60e62a6..818e8d5 100644 --- a/cybersyn/scripts/train-events.lua +++ b/cybersyn/scripts/train-events.lua @@ -1,4 +1,5 @@ --By Mami +local min = math.min local INF = math.huge ---@param map_data MapData @@ -293,26 +294,28 @@ local function on_train_leaves_stop(map_data, mod_settings, train_id, train) train.r_station_id = nil train.manifest = nil --add to available trains for depot bypass - local fuel_fill = 0 - local total_slots = 0 - for k, v in pairs(train.entity.locomotives) do - if v[1] then - local inv = v[1].get_fuel_inventory() + local fuel_fill = INF + for _, v in pairs(train.entity.locomotives) do + for _, loco in pairs(v) do + local inv = loco.get_fuel_inventory() if inv then local inv_size = #inv - total_slots = total_slots + inv_size - for i = 1, inv_size do - local item = inv[i--[[@as uint]]] - local count = item.count - if count > 0 then - fuel_fill = fuel_fill + count/get_stack_size(map_data, item.name) + if inv_size > 0 then + local fuel_total = 0 + ---@type uint + for i = 1, inv_size do + local item = inv[i] + if item.valid_for_read then + fuel_total = fuel_total + item.count/get_stack_size(map_data, item.name) + end end + fuel_fill = min(fuel_fill, fuel_total/inv_size) end end end end - if total_slots == 0 or fuel_fill/total_slots > mod_settings.fuel_threshold then - --if total_slots == 0, it's probably a modded electric train + if fuel_fill > mod_settings.fuel_threshold then + --if fuel_fill == INF, it's probably a modded electric train if mod_settings.depot_bypass_enabled then train.status = STATUS_TO_D_BYPASS add_available_train(map_data, train_id, train)