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

Function CheckIfTrainNeedsService

src/train_cmd.cpp:4144–4179  ·  view source on GitHub ↗

* Check whether a train needs service, and if so, find a depot or service it. * @return v %Train to check. */

Source from the content-addressed store, hash-verified

4142 * @return v %Train to check.
4143 */
4144static void CheckIfTrainNeedsService(Train *v)
4145{
4146 if (Company::Get(v->owner)->settings.vehicle.servint_trains == 0 || !v->NeedsAutomaticServicing()) return;
4147 if (v->IsChainInDepot()) {
4148 VehicleServiceInDepot(v);
4149 return;
4150 }
4151
4152 uint max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty;
4153
4154 FindDepotData tfdd = FindClosestTrainDepot(v, max_penalty);
4155 /* Only go to the depot if it is not too far out of our way. */
4156 if (tfdd.best_length == UINT_MAX || tfdd.best_length > max_penalty) {
4157 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
4158 /* If we were already heading for a depot but it has
4159 * suddenly moved farther away, we continue our normal
4160 * schedule? */
4161 v->current_order.MakeDummy();
4162 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
4163 }
4164 return;
4165 }
4166
4167 DepotID depot = GetDepotIndex(tfdd.tile);
4168
4169 if (v->current_order.IsType(OT_GOTO_DEPOT) &&
4170 v->current_order.GetDestination() != depot &&
4171 !Chance16(3, 16)) {
4172 return;
4173 }
4174
4175 SetBit(v->gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
4176 v->current_order.MakeGoToDepot(depot, OrderDepotTypeFlag::Service, OrderNonStopFlag::NoIntermediate, OrderDepotActionFlag::NearestDepot);
4177 v->dest_tile = tfdd.tile;
4178 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
4179}
4180
4181/** Calendar day handler. */
4182void Train::OnNewCalendarDay()

Callers 2

ChooseTrainTrackFunction · 0.85
OnNewEconomyDayMethod · 0.85

Calls 12

VehicleServiceInDepotFunction · 0.85
FindClosestTrainDepotFunction · 0.85
SetWindowWidgetDirtyFunction · 0.85
GetDepotIndexFunction · 0.85
Chance16Function · 0.85
SetBitFunction · 0.85
IsTypeMethod · 0.80
MakeDummyMethod · 0.80
GetDestinationMethod · 0.80
MakeGoToDepotMethod · 0.80
IsChainInDepotMethod · 0.45

Tested by

no test coverage detected