z_mirror/flib

git clone https://git.raiguard.me/z_mirror/flib

Caleb HeuerRemove require guardsa9f2be7

trunk
4.5 KiB133 linesraw
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)
14  if
15    train.valid
16    and train.locomotives
17    and (#train.locomotives.front_movers > 0 or #train.locomotives.back_movers > 0)
18  then
19    return train.locomotives.front_movers and train.locomotives.front_movers[1] or train.locomotives.back_movers[1]
20  end
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)
27  local loco = flib_train.get_main_locomotive(train)
28  return 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)
35  local disconnected_back = entity.disconnect_rolling_stock(defines.rail_direction.back)
36  local disconnected_front = entity.disconnect_rolling_stock(defines.rail_direction.front)
37  entity.rotate({})
38  -- Only reconnect the side that was disconnected
39  local reconnected_front = disconnected_front
40  local reconnected_back = disconnected_back
41  if disconnected_back then
42    reconnected_back = entity.connect_rolling_stock(defines.rail_direction.front)
43  end
44  if disconnected_front then
45    reconnected_front = entity.connect_rolling_stock(defines.rail_direction.back)
46  end
47
48  if disconnected_front and not reconnected_front then
49    return false
50  end
51  if disconnected_back and not reconnected_back then
52    return false
53  end
54  return 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)
66  if train and train.valid then
67    local carriages = train.carriages
68    local string_table = {}
69    local count_wagons, count_loco_front, count_loco_back, i = 0, 0, 0, 0
70    local locos_front = train.locomotives.front_movers
71    for _, carriage in pairs(carriages) do
72      i = i + 1
73      if carriage.type == "locomotive" then
74        local faces_forward = false
75        for _, loco in pairs(locos_front) do
76          if carriage.unit_number == loco.unit_number then
77            faces_forward = true
78            break
79          end
80        end
81        if faces_forward then
82          string_table[i] = "<L<"
83          count_loco_front = count_loco_front + 1
84        else
85          string_table[i] = ">L>"
86          count_loco_back = count_loco_back + 1
87        end
88      elseif carriage.type == "cargo-wagon" then
89        count_wagons = count_wagons + 1
90        string_table[i] = "C"
91      elseif carriage.type == "fluid-wagon" then
92        count_wagons = count_wagons + 1
93        string_table[i] = "F"
94      elseif carriage.type == "artillery-wagon" then
95        count_wagons = count_wagons + 1
96        string_table[i] = "A"
97      else
98        count_wagons = count_wagons + 1
99        string_table[i] = "?"
100      end
101    end
102    return table.concat(string_table),
103      {
104        total = i,
105        wagons = count_wagons,
106        front_movers = count_loco_front,
107        back_movers = count_loco_back,
108      }
109  end
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)
117  if train and train.valid and game.players[player_index] then
118    local loco = flib_train.get_main_locomotive(train)
119    if loco and loco.valid then
120      game.players[player_index].opened = loco
121      return true
122    end
123  end
124  return 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.