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

Function CheckNewTrain

src/train_cmd.cpp:992–1009  ·  view source on GitHub ↗

* Check/validate whether we may actually build a new train. * @note All vehicles are/were 'heads' of their chains. * @param original_dst The original destination chain. * @param dst The destination chain after constructing the train. * @param original_src The original source chain. * @param src The source chain after constructing the train. * @return possible error of this

Source from the content-addressed store, hash-verified

990 * @return possible error of this command.
991 */
992static CommandCost CheckNewTrain(Train *original_dst, Train *dst, Train *original_src, Train *src)
993{
994 /* Just add 'new' engines and subtract the original ones.
995 * If that's less than or equal to 0 we can be sure we did
996 * not add any engines (read: trains) along the way. */
997 if ((src != nullptr && src->IsEngine() ? 1 : 0) +
998 (dst != nullptr && dst->IsEngine() ? 1 : 0) -
999 (original_src != nullptr && original_src->IsEngine() ? 1 : 0) -
1000 (original_dst != nullptr && original_dst->IsEngine() ? 1 : 0) <= 0) {
1001 return CommandCost();
1002 }
1003
1004 /* Get a free unit number and check whether it's within the bounds.
1005 * There will always be a maximum of one new train. */
1006 if (GetFreeUnitNumber(VEH_TRAIN) <= _settings_game.vehicle.max_trains) return CommandCost();
1007
1008 return CommandCost(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
1009}
1010
1011/**
1012 * Check whether the train parts can be attached.

Callers 1

ValidateTrainsFunction · 0.85

Calls 3

CommandCostClass · 0.85
GetFreeUnitNumberFunction · 0.85
IsEngineMethod · 0.80

Tested by

no test coverage detected