MCPcopy Create free account
hub / github.com/OpenTTD/OpenTTD / ArrangeTrains

Function ArrangeTrains

src/train_cmd.cpp:1150–1184  ·  view source on GitHub ↗

* Arrange the trains in the wanted way. * @param dst_head The destination chain of the to be moved vehicle. * @param dst The destination for the to be moved vehicle. * @param src_head The source chain of the to be moved vehicle. * @param src The to be moved vehicle. * @param move_chain Whether to move all vehicles after src or not. */

Source from the content-addressed store, hash-verified

1148 * @param move_chain Whether to move all vehicles after src or not.
1149 */
1150static void ArrangeTrains(Train **dst_head, Train *dst, Train **src_head, Train *src, bool move_chain)
1151{
1152 /* First determine the front of the two resulting trains */
1153 if (*src_head == *dst_head) {
1154 /* If we aren't moving part(s) to a new train, we are just moving the
1155 * front back and there is not destination head. */
1156 *dst_head = nullptr;
1157 } else if (*dst_head == nullptr) {
1158 /* If we are moving to a new train the head of the move train would become
1159 * the head of the new vehicle. */
1160 *dst_head = src;
1161 }
1162
1163 if (src == *src_head) {
1164 /* If we are moving the front of a train then we are, in effect, creating
1165 * a new head for the train. Point to that. Unless we are moving the whole
1166 * train in which case there is not 'source' train anymore.
1167 * In case we are a multiheaded part we want the complete thing to come
1168 * with us, so src->GetNextUnit(), however... when we are e.g. a wagon
1169 * that is followed by a rear multihead we do not want to include that. */
1170 *src_head = move_chain ? nullptr :
1171 (src->IsMultiheaded() ? src->GetNextUnit() : src->GetNextVehicle());
1172 }
1173
1174 /* Now it's just simply removing the part that we are going to move from the
1175 * source train and *if* the destination is a not a new train add the chain
1176 * at the destination location. */
1177 RemoveFromConsist(src, move_chain);
1178 if (*dst_head != src) InsertInConsist(dst, src);
1179
1180 /* Now normalise the dual heads, that is move the dual heads around in such
1181 * a way that the head and rear of a dual head are in the same train */
1182 NormaliseDualHeads(*src_head);
1183 NormaliseDualHeads(*dst_head);
1184}
1185
1186/**
1187 * Normalise the head of the train again, i.e. that is tell the world that

Callers 2

CmdMoveRailVehicleFunction · 0.85
CmdSellRailWagonFunction · 0.85

Calls 6

RemoveFromConsistFunction · 0.85
InsertInConsistFunction · 0.85
NormaliseDualHeadsFunction · 0.85
IsMultiheadedMethod · 0.80
GetNextUnitMethod · 0.80
GetNextVehicleMethod · 0.45

Tested by

no test coverage detected