diff --git a/cybersyn/changelog.txt b/cybersyn/changelog.txt index a687159..d6e64db 100644 --- a/cybersyn/changelog.txt +++ b/cybersyn/changelog.txt @@ -41,4 +41,5 @@ Date: 2022-11-24 Features: - Added a stuck train alert - Improved localization - - Fixed Bug with fluid cargo not being detected by depots + - Fixed bug with fluid cargo not being detected by depots + - Greatly improved automatic train blacklist logic diff --git a/cybersyn/scripts/layout.lua b/cybersyn/scripts/layout.lua index 479cc23..e418754 100644 --- a/cybersyn/scripts/layout.lua +++ b/cybersyn/scripts/layout.lua @@ -351,9 +351,11 @@ local function reset_station_layout(map_data, station, forbidden_entity) local type_filter = {"inserter", "pump", "arithmetic-combinator"} local wagon_number = 0 local pattern_length = 1 + local is_break = false for i = 1, 100 do local rail, rail_direction, rail_connection_direction = pre_rail.get_connected_rail({rail_direction = rail_direction_from_station, rail_connection_direction = defines.rail_connection_direction.straight}) if not rail or rail_connection_direction ~= defines.rail_connection_direction.straight or not rail.valid then + is_break = true break end pre_rail = rail @@ -433,7 +435,10 @@ local function reset_station_layout(map_data, station, forbidden_entity) search_area = area.move(search_area, area_delta) end end - layout_pattern = string_sub(layout_pattern, 1, pattern_length)..STATION_LAYOUT_NA.."*$" + layout_pattern = string_sub(layout_pattern, 1, pattern_length) + if is_break then + layout_pattern = layout_pattern..STATION_LAYOUT_NA.."*$" + end station.layout_pattern = layout_pattern local accepted_layouts = station.accepted_layouts for id, layout in pairs(map_data.layouts) do @@ -457,45 +462,40 @@ end ---@param map_data MapData ---@param rail LuaEntity ---@param forbidden_entity LuaEntity? -function force_update_station_from_rail(map_data, rail, forbidden_entity) - --NOTE: should we search further or better? it would be more expensive - local entity = rail.get_rail_segment_entity(defines.rail_direction.back, false) - if entity and entity.valid and entity.name == "train-stop" then - local station = map_data.stations[entity.unit_number] - if station then - reset_station_layout(map_data, station, forbidden_entity) +---@param force boolean? +function update_station_from_rail(map_data, rail, forbidden_entity, force) + --NOTE: is this a correct way to figure out the direction? + ---@type defines.rail_direction + local rail_direction = defines.rail_direction.back + local entity = rail.get_rail_segment_entity(rail_direction, false) + if not entity then + rail_direction = defines.rail_direction.front + entity = rail.get_rail_segment_entity(rail_direction, false) + end + for i = 1, 100 do + if not entity or not entity.valid then + return end - else - entity = rail.get_rail_segment_entity(defines.rail_direction.front, false) - if entity and entity.valid and entity.name == "train-stop" then + if entity.name == "train-stop" then local station = map_data.stations[entity.unit_number] if station then - reset_station_layout(map_data, station, forbidden_entity) - end - end - end -end ----@param map_data MapData ----@param rail LuaEntity ----@param forbidden_entity LuaEntity? -function update_station_from_rail(map_data, rail, forbidden_entity) - --NOTE: should we search further or better? it would be more expensive - local entity = rail.get_rail_segment_entity(defines.rail_direction.back, false) - if entity and entity.valid and entity.name == "train-stop" then - local station = map_data.stations[entity.unit_number] - if station then - update_station_if_auto(map_data, station, forbidden_entity) - end - else - entity = rail.get_rail_segment_entity(defines.rail_direction.front, false) - if entity and entity.valid and entity.name == "train-stop" then - local station = map_data.stations[entity.unit_number] - if station then - update_station_if_auto(map_data, station, forbidden_entity) + if force then + reset_station_layout(map_data, station, forbidden_entity) + else + update_station_if_auto(map_data, station, forbidden_entity) + end end + return end + + rail = rail.get_connected_rail({rail_direction = rail_direction, rail_connection_direction = defines.rail_connection_direction.straight})--[[@as LuaEntity]] + if not rail or not rail.valid then + return + end + entity = rail.get_rail_segment_entity(rail_direction, false) end end + ---@param map_data MapData ---@param pump LuaEntity ---@param forbidden_entity LuaEntity? diff --git a/cybersyn/scripts/main.lua b/cybersyn/scripts/main.lua index c729ebe..2c6f0f2 100644 --- a/cybersyn/scripts/main.lua +++ b/cybersyn/scripts/main.lua @@ -287,7 +287,7 @@ local function on_combinator_built(map_data, comb) if op == OPERATION_WAGON_MANIFEST then if rail then - force_update_station_from_rail(map_data, rail, nil) + update_station_from_rail(map_data, rail, nil, true) end elseif op == OPERATION_DEPOT then if stop then