Experimental 1.2.2 (#21)

---------------------------------------------------------------------------------------------------
Version: 1.2.2
Date: 2022-12-29
  Features:
    - Added a station combinator setting to enable or disable the inactivity condition in a train's orders, disabled by default (but not in <=1.2.1 worlds)
    - Added a depot combinator setting to enable depot bypass, enabled by default
    - Added a depot combinator setting to force trains to park at the same depot, enabled by default
    - Added network "each" for depots
    - Added a map setting to modify the default locked slots per cargo wagon value
    - Added a map setting to modify the default priority value
    - Added a map setting to allow trains with cargo at depots, disabled by default
  Changes:
    - Inverted the sign of combinator outputs, a map setting has been added to maintain backwards compatibility with <=1.2.1 worlds
    - Overhauled the wagon control combinator algorithm to spread items out between cargo wagons
    - Trains with cargo held in the depot now check if they have been emptied and reset when they have
    - Cargo capacity is now prioritized over distance when choosing trains
    - Increased the default request threshold to 2000
    - Improved English localization
  Bugfixes:
    - Fixed a bug where trains with cargo sometimes weren't getting held at depots
    - Fixed a crash caused by changing a station combinator to the "each" network during a bad tick
    - Fixed a crash when changing a refueler away from network each
    - Multiple rare bugs and crashes relating to wagon control combinators are fixed
    - Fixed a bug with refueler direct orders not being applied after moving through a space elevator
    - Fixed a bug where filtered slots sometimes weren't being removed
---------------------------------------------------------------------------------------------------
This commit is contained in:
Monica Moniot
2022-12-29 10:02:07 -06:00
committed by GitHub
parent c48d1d3025
commit 7d2f0c2ccd
19 changed files with 833 additions and 511 deletions

View File

@@ -48,6 +48,18 @@ function on_failed_delivery(map_data, train_id, train)
unset_wagon_combs(map_data, station)
end
end
if train.has_filtered_wagon then
train.has_filtered_wagon = nil
for carriage_i, carriage in ipairs(train.entity.cargo_wagons) do
local inv = carriage.get_inventory(defines.inventory.cargo_wagon)
if inv and inv.is_filtered() then
---@type uint
for i = 1, #inv do
inv.set_filter(i, nil)
end
end
end
end
train.r_station_id = nil
train.p_station_id = nil
train.manifest = nil
@@ -60,14 +72,21 @@ end
---@param train_id uint
---@param train Train
function add_available_train(map_data, train_id, train)
local network_name = train.network_name
if network_name then
local network = map_data.available_trains[network_name]
if not network then
network = {}
map_data.available_trains[network_name] = network
if train.network_name then
local f, a
if train.network_name == NETWORK_EACH then
f, a = next, train.network_flag
else
f, a = once, train.network_name
end
for network_name in f, a do
local network = map_data.available_trains[network_name]
if not network then
network = {}
map_data.available_trains[network_name] = network
end
network[train_id] = true
end
network[train_id] = true
train.is_available = true
interface_raise_train_available(train_id)
end
@@ -80,77 +99,56 @@ end
---@param train Train
function add_available_train_to_depot(map_data, mod_settings, train_id, train, depot_id, depot)
local comb = depot.entity_comb
local network_name = get_comb_network_name(comb)
if network_name then
local network = map_data.available_trains[network_name]
if not network then
network = {}
map_data.available_trains[network_name] = network
end
network[train_id] = true
train.is_available = true
end
set_train_from_comb(mod_settings, train, comb)
depot.available_train_id = train_id
train.depot_id = depot_id
train.status = STATUS_D
train.parked_at_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
if network_name then
local signals = comb.get_merged_signals(defines.circuit_connector_id.combinator_input)
if signals then
for k, v in pairs(signals) do
local item_name = v.signal.name
local item_count = v.count
if item_name then
if item_name == SIGNAL_PRIORITY then
train.priority = item_count
end
if item_name == network_name then
train.network_flag = item_count
end
end
end
end
interface_raise_train_available(train_id)
end
add_available_train(map_data, train_id, train)
end
---@param map_data MapData
---@param train_id uint
---@param train Train
function remove_available_train(map_data, train_id, train)
---@type uint
if train.is_available and train.network_name then
local network = map_data.available_trains[train.network_name--[[@as string]]]
if network then
network[train_id] = nil
if next(network) == nil then
map_data.available_trains[train.network_name] = nil
if train.is_available then
train.is_available = nil
local f, a
if train.network_name == NETWORK_EACH then
f, a = next, train.network_flag
else
f, a = once, train.network_name
end
for network_name in f, a do
local network = map_data.available_trains[network_name]
if network then
network[train_id] = nil
if next(network) == nil then
map_data.available_trains[network_name] = nil
end
end
end
train.is_available = nil
local depot = map_data.depots[train.depot_id]
if depot.available_train_id == train_id then
depot.available_train_id = nil
return true
end
end
return false
end
---@param map_data MapData
---@param depot_id uint
---@param train_entity LuaTrain
local function on_train_arrives_depot(map_data, depot_id, train_entity)
local contents = train_entity.get_contents()
local fluid_contents = train_entity.get_fluid_contents()
local is_train_empty = next(contents) == nil and next(fluid_contents) == nil
local is_train_empty = next(train_entity.get_contents()) == nil and next(train_entity.get_fluid_contents()) == nil
local train_id = train_entity.id
local train = map_data.trains[train_id]
if train then
if train.status == STATUS_TO_D then
elseif train.status == STATUS_TO_D_BYPASS or train.status == STATUS_D then
--shouldn't be possible to get train.status == STATUS_D
elseif train.status == STATUS_TO_D_BYPASS or train.status == STATUS_D then
remove_available_train(map_data, train_id, train)
elseif mod_settings.react_to_train_early_to_depot then
if train.manifest then
@@ -160,21 +158,19 @@ local function on_train_arrives_depot(map_data, depot_id, train_entity)
else
return
end
if is_train_empty then
if is_train_empty or mod_settings.allow_cargo_in_depot then
local old_status = train.status
add_available_train_to_depot(map_data, mod_settings, train_id, train, depot_id, map_data.depots[depot_id])
set_depot_schedule(train_entity, train.depot_name)
local depot = map_data.depots[depot_id]
add_available_train_to_depot(map_data, mod_settings, train_id, train, depot_id, depot)
set_depot_schedule(train_entity, depot.entity_stop.backer_name)
interface_raise_train_status_changed(train_id, old_status, STATUS_D)
else
--train still has cargo
if mod_settings.react_to_nonempty_train_in_depot then
lock_train_to_depot(train_entity)
remove_train(map_data, train_id, train)
send_alert_nonempty_train_in_depot(map_data, train_entity)
end
interface_raise_train_nonempty_in_depot(depot_id, train_entity, train_id)
lock_train_to_depot(train_entity)
remove_train(map_data, train_id, train)
send_alert_nonempty_train_in_depot(map_data, train_entity)
end
elseif is_train_empty then
elseif is_train_empty or mod_settings.allow_cargo_in_depot then
--NOTE: only place where new Train
train = {
entity = train_entity,
@@ -188,22 +184,25 @@ local function on_train_arrives_depot(map_data, depot_id, train_entity)
last_manifest_tick = map_data.total_ticks,
has_filtered_wagon = nil,
--is_available = add_available_train_to_depot,
--parked_at_depot_id = add_available_train_to_depot,
--depot_name = add_available_train_to_depot,
--depot_id = add_available_train_to_depot,
--use_any_depot = add_available_train_to_depot,
--disable_bypass = add_available_train_to_depot,
--network_name = add_available_train_to_depot,
--network_flag = add_available_train_to_depot,
--priority = add_available_train_to_depot,
}
}--[[@as Train]]
set_train_layout(map_data, train)
map_data.trains[train_id] = train
add_available_train_to_depot(map_data, mod_settings, train_id, train, depot_id, map_data.depots[depot_id])
local depot = map_data.depots[depot_id]
add_available_train_to_depot(map_data, mod_settings, train_id, train, depot_id, depot)
set_depot_schedule(train_entity, train.depot_name)
set_depot_schedule(train_entity, depot.entity_stop.backer_name)
interface_raise_train_created(train_id, depot_id)
else
if mod_settings.react_to_nonempty_train_in_depot then
send_alert_nonempty_train_in_depot(map_data, train_entity)
end
lock_train_to_depot(train_entity)
send_alert_nonempty_train_in_depot(map_data, train_entity)
end
if not is_train_empty then
interface_raise_train_nonempty_in_depot(depot_id, train_entity)
end
end
@@ -218,7 +217,7 @@ local function on_train_arrives_station(map_data, station_id, train_id, train)
if train.p_station_id == station_id then
train.status = STATUS_P
local station = map_data.stations[station_id]
set_comb1(map_data, station, train.manifest, 1)
set_comb1(map_data, station, train.manifest, mod_settings.invert_sign and 1 or -1)
set_p_wagon_combs(map_data, station, train)
interface_raise_train_status_changed(train_id, STATUS_TO_P, STATUS_P)
end
@@ -226,7 +225,7 @@ local function on_train_arrives_station(map_data, station_id, train_id, train)
if train.r_station_id == station_id then
train.status = STATUS_R
local station = map_data.stations[station_id]
set_comb1(map_data, station, train.manifest, -1)
set_comb1(map_data, station, train.manifest, mod_settings.invert_sign and -1 or 1)
set_r_wagon_combs(map_data, station, train)
interface_raise_train_status_changed(train_id, STATUS_TO_R, STATUS_R)
end
@@ -316,45 +315,54 @@ local function on_train_leaves_stop(map_data, mod_settings, train_id, train)
end
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
if not train.disable_bypass then
train.status = STATUS_TO_D_BYPASS
add_available_train(map_data, train_id, train)
interface_raise_train_status_changed(train_id, STATUS_R, STATUS_TO_D_BYPASS)
return
end
else
local refuelers = map_data.to_refuelers[train.network_name]
if refuelers then
local best_refueler_id = nil
local best_dist = INF
local best_prior = -INF
for id, _ in pairs(refuelers) do
local refueler = map_data.refuelers[id]
set_refueler_from_comb(map_data, mod_settings, id)
local f, a
if train.network_name == NETWORK_EACH then
f, a = next, train.network_flag
else
f, a = once, train.network_name
end
for network_name in f, a do
local refuelers = map_data.to_refuelers[network_name]
if refuelers then
local best_refueler_id = nil
local best_dist = INF
local best_prior = -INF
for id, _ in pairs(refuelers) do
local refueler = map_data.refuelers[id]
set_refueler_from_comb(map_data, mod_settings, id)
local refueler_network_flag = refueler.network_name == NETWORK_EACH and (refueler.network_flag[train.network_name] or 0) or refueler.network_flag
if btest(train.network_flag, refueler_network_flag) and (refueler.allows_all_trains or refueler.accepted_layouts[train.layout_id]) and refueler.trains_total < refueler.entity_stop.trains_limit then
local accepted = false
local dist = nil
if refueler.priority == best_prior then
dist = get_stop_dist(train.entity.front_stock, refueler.entity_stop)
accepted = dist < best_dist
end
if accepted or refueler.priority > best_prior then
best_refueler_id = id
best_dist = dist or get_stop_dist(train.entity.front_stock, refueler.entity_stop)
best_prior = refueler.priority
local refueler_network_flag = get_network_flag(refueler, network_name)
local train_network_flag = get_network_flag(train, network_name)
if btest(train_network_flag, refueler_network_flag) and (refueler.allows_all_trains or refueler.accepted_layouts[train.layout_id]) and refueler.trains_total < refueler.entity_stop.trains_limit then
local accepted = false
local dist = nil
if refueler.priority == best_prior then
dist = get_stop_dist(train.entity.front_stock, refueler.entity_stop)
accepted = dist < best_dist
end
if accepted or refueler.priority > best_prior then
best_refueler_id = id
best_dist = dist or get_stop_dist(train.entity.front_stock, refueler.entity_stop)
best_prior = refueler.priority
end
end
end
end
if best_refueler_id then
train.status = STATUS_TO_F
train.refueler_id = best_refueler_id
local refueler = map_data.refuelers[best_refueler_id]
refueler.trains_total = refueler.trains_total + 1
add_refueler_schedule(map_data, train.entity, refueler.entity_stop)
interface_raise_train_status_changed(train_id, STATUS_R, STATUS_TO_F)
return
if best_refueler_id then
train.status = STATUS_TO_F
train.refueler_id = best_refueler_id
local refueler = map_data.refuelers[best_refueler_id]
refueler.trains_total = refueler.trains_total + 1
add_refueler_schedule(map_data, train.entity, refueler.entity_stop)
interface_raise_train_status_changed(train_id, STATUS_R, STATUS_TO_F)
return
end
end
end
end
@@ -367,7 +375,7 @@ local function on_train_leaves_stop(map_data, mod_settings, train_id, train)
refueler.trains_total = refueler.trains_total - 1
unset_wagon_combs(map_data, refueler)
set_combinator_output(map_data, refueler.entity_comb, nil)
if mod_settings.depot_bypass_enabled then
if not train.disable_bypass then
train.status = STATUS_TO_D_BYPASS
add_available_train(map_data, train_id, train)
else