|
| 1 | +--[[ |
| 2 | +Helper functions for managing trains and rolling stock. |
| 3 | +Provides utilities for naming trains, identifying locomotives, and describing train state. |
| 4 | +]] |
| 5 | + |
| 6 | +local mod = {} |
| 7 | + |
| 8 | +---Get the identifying locomotive for a train (the one with the lowest unit_number). |
| 9 | +---This provides a stable reference point for train-level operations. |
| 10 | +---@param rolling_stock LuaEntity A rolling stock entity (locomotive, wagon, etc) |
| 11 | +---@return LuaEntity? The locomotive with the lowest unit_number, or nil if no locomotives |
| 12 | +function mod.get_identifying_locomotive(rolling_stock) |
| 13 | + local train = rolling_stock.train |
| 14 | + if not train then return nil end |
| 15 | + |
| 16 | + local locomotives = train.locomotives |
| 17 | + |
| 18 | + -- No locomotive can be both a front and back mover, so check each list separately |
| 19 | + local identifying_loco = nil |
| 20 | + |
| 21 | + -- Check front movers |
| 22 | + if locomotives.front_movers then |
| 23 | + for _, loco in ipairs(locomotives.front_movers) do |
| 24 | + if not identifying_loco or loco.unit_number < identifying_loco.unit_number then identifying_loco = loco end |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + -- Check back movers |
| 29 | + if locomotives.back_movers then |
| 30 | + for _, loco in ipairs(locomotives.back_movers) do |
| 31 | + if not identifying_loco or loco.unit_number < identifying_loco.unit_number then identifying_loco = loco end |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + return identifying_loco |
| 36 | +end |
| 37 | + |
| 38 | +---Set the name of all locomotives in a train. |
| 39 | +---@param rolling_stock LuaEntity A rolling stock entity (locomotive, wagon, etc) |
| 40 | +---@param name string The name to set |
| 41 | +function mod.set_name(rolling_stock, name) |
| 42 | + local train = rolling_stock.train |
| 43 | + if not train then return end |
| 44 | + |
| 45 | + local locomotives = train.locomotives |
| 46 | + |
| 47 | + -- Set name on all front movers |
| 48 | + if locomotives.front_movers then |
| 49 | + for _, loco in ipairs(locomotives.front_movers) do |
| 50 | + loco.backer_name = name |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + -- Set name on all back movers |
| 55 | + if locomotives.back_movers then |
| 56 | + for _, loco in ipairs(locomotives.back_movers) do |
| 57 | + loco.backer_name = name |
| 58 | + end |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | +---Get the name of a train. |
| 63 | +---@param rolling_stock LuaEntity A rolling stock entity (locomotive, wagon, etc) |
| 64 | +---@return string The train name (backer_name of identifying locomotive), or fallback |
| 65 | +function mod.get_name(rolling_stock) |
| 66 | + local identifying_loco = mod.get_identifying_locomotive(rolling_stock) |
| 67 | + if identifying_loco then return identifying_loco.backer_name end |
| 68 | + |
| 69 | + -- No locomotive: fallback based on whether we have a train |
| 70 | + local train = rolling_stock.train |
| 71 | + if train then |
| 72 | + return "id " .. train.id |
| 73 | + else |
| 74 | + return "unnamed train" |
| 75 | + end |
| 76 | +end |
| 77 | + |
| 78 | +---Add train state information to a message builder. |
| 79 | +---@param msgbuilder fa.MessageBuilder The message builder to add fragments to |
| 80 | +---@param rolling_stock LuaEntity A rolling stock entity (locomotive, wagon, etc) |
| 81 | +---@return boolean True if state was added, false if no state to report |
| 82 | +function mod.push_state_message(msgbuilder, rolling_stock) |
| 83 | + local train = rolling_stock.train |
| 84 | + if not train then return false end |
| 85 | + |
| 86 | + -- Check if in manual mode |
| 87 | + if train.manual_mode then |
| 88 | + msgbuilder:fragment({ "fa.train-state-manual" }) |
| 89 | + return true |
| 90 | + end |
| 91 | + |
| 92 | + -- Check if headed to a station |
| 93 | + local path_end_stop = train.path_end_stop |
| 94 | + if path_end_stop then |
| 95 | + local station_name = path_end_stop.backer_name |
| 96 | + if station_name and station_name ~= "" then |
| 97 | + msgbuilder:fragment({ "fa.train-state-headed-to", station_name }) |
| 98 | + return true |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + -- No verbalizable state |
| 103 | + return false |
| 104 | +end |
| 105 | + |
| 106 | +return mod |
0 commit comments