z_mirror/flib
git clone https://git.raiguard.me/z_mirror/flib
a9f2be7
trunk
1--- Functions for working with trains. 2--- ```lua 3--- local flib_train = require("__flib__.train") 4--- ``` 5--- @class flib_train 6local flib_train ={} 7 8local table =table 9 10--- Get the main locomotive in a given train. 11--- @param train LuaTrain 12--- @return LuaEntity? locomotive The primary locomotive entity or `nil` when no locomotive was found 13function flib_train . get_main_locomotive ( train ) 14if 15train . valid 16and train . locomotives 17and ( # train . locomotives . front_movers > 0 or # train . locomotives . back_movers > 0 ) 18then 19return train . locomotives . front_movers and train . locomotives . front_movers [ 1 ] or train . locomotives . back_movers [ 1 ] 20end 21end 22 23--- Get the backer_name of the main locomotive in a given train. 24--- @param train LuaTrain 25--- @return string? backer_name The backer_name of the primary locomotive or `nil` when no locomotive was found 26function flib_train . get_backer_name ( train ) 27local loco =flib_train . get_main_locomotive ( train ) 28return loco and loco . backer_name 29end 30 31--- Rotate a single carriage of a train. 32--- @param entity LuaEntity 33--- @return boolean rotated `true` when rotation was successful. 34function flib_train . rotate_carriage ( entity ) 35local disconnected_back =entity . disconnect_rolling_stock ( defines . rail_direction . back ) 36local disconnected_front =entity . disconnect_rolling_stock ( defines . rail_direction . front ) 37entity . rotate ( {} ) 38-- Only reconnect the side that was disconnected 39local reconnected_front =disconnected_front 40local reconnected_back =disconnected_back 41if disconnected_back then 42reconnected_back =entity . connect_rolling_stock ( defines . rail_direction . front ) 43end 44if disconnected_front then 45reconnected_front =entity . connect_rolling_stock ( defines . rail_direction . back ) 46end 47 48if disconnected_front and not reconnected_front then 49return false 50end 51if disconnected_back and not reconnected_back then 52return false 53end 54return true 55end 56 57--- Create a string representing train composition, and return a count of locomotives and wagons in the train. 58--- - `<L<`, `>L>`: Locomotives 59--- - `C`: Cargo wagon 60--- - `F`: Fluid wagon 61--- - `A`: Artillery wagon 62--- @param train LuaTrain 63--- @return string? composition The composition string, or `nil` if the train was invalid. 64--- @return TrainCompositionCounts? 65function flib_train . get_composition_string ( train ) 66if train and train . valid then 67local carriages =train . carriages 68local string_table ={} 69local count_wagons , count_loco_front , count_loco_back , i =0 , 0 , 0 , 0 70local locos_front =train . locomotives . front_movers 71for _ , carriage in pairs ( carriages ) do 72i =i + 1 73if carriage . type == "locomotive" then 74local faces_forward =false 75for _ , loco in pairs ( locos_front ) do 76if carriage . unit_number == loco . unit_number then 77faces_forward =true 78break 79end 80end 81if faces_forward then 82string_table [ i ] ="<L<" 83count_loco_front =count_loco_front + 1 84else 85string_table [ i ] =">L>" 86count_loco_back =count_loco_back + 1 87end 88elseif carriage . type == "cargo-wagon" then 89count_wagons =count_wagons + 1 90string_table [ i ] ="C" 91elseif carriage . type == "fluid-wagon" then 92count_wagons =count_wagons + 1 93string_table [ i ] ="F" 94elseif carriage . type == "artillery-wagon" then 95count_wagons =count_wagons + 1 96string_table [ i ] ="A" 97else 98count_wagons =count_wagons + 1 99string_table [ i ] ="?" 100end 101end 102return table . concat ( string_table ), 103{ 104total =i , 105wagons =count_wagons , 106front_movers =count_loco_front , 107back_movers =count_loco_back , 108} 109end 110end 111 112--- Open train GUI for one player. 113--- @param player_index number 114--- @param train LuaTrain 115--- @return boolean `true` if the GUI was opened. 116function flib_train . open_gui ( player_index , train ) 117if train and train . valid and game . players [ player_index ] then 118local loco =flib_train . get_main_locomotive ( train ) 119if loco and loco . valid then 120game . players [ player_index ]. opened =loco 121return true 122end 123end 124return false 125end 126 127return flib_train 128 129--- @class TrainCompositionCounts 130--- @field total number The total number of rolling stocks in the train. 131--- @field wagons number The number of wagons in the train. 132--- @field front_movers number The number of front-facing locomotives in the train. 133--- @field back_movers number The number of back-facing locomotives in the train.