OpenTTD Source 20260512-master-g20b387b91f
train_cmd.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "error.h"
13#include "command_func.h"
14#include "error_func.h"
16#include "news_func.h"
17#include "company_func.h"
18#include "newgrf_sound.h"
19#include "newgrf_text.h"
20#include "strings_func.h"
21#include "viewport_func.h"
22#include "vehicle_func.h"
23#include "sound_func.h"
24#include "ai/ai.hpp"
25#include "game/game.hpp"
26#include "newgrf_station.h"
27#include "effectvehicle_func.h"
28#include "network/network.h"
29#include "core/random_func.hpp"
30#include "company_base.h"
31#include "newgrf.h"
32#include "order_backup.h"
33#include "zoom_func.h"
34#include "newgrf_debug.h"
35#include "framerate_type.h"
36#include "train_cmd.h"
37#include "misc_cmd.h"
40
41#include "table/strings.h"
42#include "table/train_sprites.h"
43
44#include "safeguards.h"
45
46static Track ChooseTrainTrack(Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool force_res, bool *got_reservation, bool mark_stuck);
47static bool TrainCheckIfLineEnds(Train *v, bool reverse = true);
48bool TrainController(Train *v, Vehicle *nomove, bool reverse = true); // Also used in vehicle_sl.cpp.
50static void CheckIfTrainNeedsService(Train *v);
51static void CheckNextTrainTile(Train *v);
52
53static const uint8_t _vehicle_initial_x_fract[4] = {10, 8, 4, 8};
54static const uint8_t _vehicle_initial_y_fract[4] = { 8, 4, 8, 10};
55
57template <>
59{
60 return image_index < lengthof(_engine_sprite_base);
61}
62
63
70{
71 if (!CargoSpec::Get(cargo)->is_freight) return 1;
72 return _settings_game.vehicle.freight_trains;
73}
74
77{
78 bool first = true;
79
80 for (const Train *v : Train::Iterate()) {
81 if (v->First() == v && !v->vehstatus.Test(VehState::Crashed)) {
82 for (const Train *u = v->GetMovingFront(), *w = v->GetMovingNext(); w != nullptr; u = w, w = w->GetMovingNext()) {
83 if (u->track != TRACK_BIT_DEPOT) {
84 if ((w->track != TRACK_BIT_DEPOT &&
85 std::max(abs(u->x_pos - w->x_pos), abs(u->y_pos - w->y_pos)) != u->CalcNextVehicleOffset()) ||
86 (w->track == TRACK_BIT_DEPOT && TicksToLeaveDepot(u) <= 0)) {
87 ShowErrorMessage(GetEncodedString(STR_BROKEN_VEHICLE_LENGTH, v->index, v->owner), {}, WL_CRITICAL);
88
89 if (!_networking && first) {
90 first = false;
91 Command<Commands::Pause>::Post(PauseMode::Error, true);
92 }
93 /* Break so we warn only once for each train. */
94 break;
95 }
96 }
97 }
98 }
99 }
100}
101
109{
110 uint16_t max_speed = UINT16_MAX;
111
112 assert(this->IsFrontEngine() || this->IsFreeWagon());
113
114 const RailVehicleInfo *rvi_v = RailVehInfo(this->engine_type);
115 EngineID first_engine = this->IsFrontEngine() ? this->engine_type : EngineID::Invalid();
116 this->gcache.cached_total_length = 0;
117 this->compatible_railtypes = {};
118
119 bool train_can_tilt = true;
120 int16_t min_curve_speed_mod = INT16_MAX;
121
122 for (Train *u = this; u != nullptr; u = u->Next()) {
123 const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
124
125 /* Check the this->first cache. */
126 assert(u->First() == this);
127
128 /* update the 'first engine' */
129 u->gcache.first_engine = this == u ? EngineID::Invalid() : first_engine;
130 u->railtypes = rvi_u->railtypes;
131
132 if (u->IsEngine()) first_engine = u->engine_type;
133
134 /* Set user defined data to its default value */
135 u->tcache.user_def_data = rvi_u->user_def_data;
136 this->InvalidateNewGRFCache();
137 u->InvalidateNewGRFCache();
138 }
139
140 for (Train *u = this; u != nullptr; u = u->Next()) {
141 /* Update user defined data (must be done before other properties) */
142 u->tcache.user_def_data = GetVehicleProperty(u, PROP_TRAIN_USER_DATA, u->tcache.user_def_data);
143 this->InvalidateNewGRFCache();
144 u->InvalidateNewGRFCache();
145 }
146
147 for (Train *u = this; u != nullptr; u = u->Next()) {
148 const Engine *e_u = u->GetEngine();
149 const RailVehicleInfo *rvi_u = &e_u->VehInfo<RailVehicleInfo>();
150
151 if (!e_u->info.misc_flags.Test(EngineMiscFlag::RailTilts)) train_can_tilt = false;
152 min_curve_speed_mod = std::min(min_curve_speed_mod, u->GetCurveSpeedModifier());
153
154 /* Cache wagon override sprite group. nullptr is returned if there is none */
155 u->tcache.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->gcache.first_engine);
156
157 /* Reset colour map */
158 u->colourmap = PAL_NONE;
159
160 /* Update powered-wagon-status and visual effect */
161 u->UpdateVisualEffect(true);
162
163 if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RailVehicleType::Wagon &&
164 UsesWagonOverride(u) && !HasBit(u->vcache.cached_vis_effect, VE_DISABLE_WAGON_POWER)) {
165 /* wagon is powered */
166 u->flags.Set(VehicleRailFlag::PoweredWagon); // cache 'powered' status
167 } else {
168 u->flags.Reset(VehicleRailFlag::PoweredWagon);
169 }
170
171 if (!u->IsArticulatedPart()) {
172 /* Do not count powered wagons for the compatible railtypes, as wagons always
173 have railtype normal */
174 if (rvi_u->power > 0) {
175 this->compatible_railtypes.Set(GetAllPoweredRailTypes(u->railtypes));
176 }
177
178 /* Some electric engines can be allowed to run on normal rail. It happens to all
179 * existing electric engines when elrails are disabled and then re-enabled */
180 if (u->flags.Test(VehicleRailFlag::AllowedOnNormalRail)) {
181 u->railtypes.Set(RAILTYPE_RAIL);
182 u->compatible_railtypes.Set(RAILTYPE_RAIL);
183 }
184
185 /* max speed is the minimum of the speed limits of all vehicles in the consist */
186 if ((rvi_u->railveh_type != RailVehicleType::Wagon || _settings_game.vehicle.wagon_speed_limits) && !UsesWagonOverride(u)) {
187 uint16_t speed = GetVehicleProperty(u, PROP_TRAIN_SPEED, rvi_u->max_speed);
188 if (speed != 0) max_speed = std::min(speed, max_speed);
189 }
190 }
191
192 uint16_t new_cap = e_u->DetermineCapacity(u);
193 if (allowed_changes.Test(ConsistChangeFlag::Capacity)) {
194 /* Update vehicle capacity. */
195 if (u->cargo_cap > new_cap) u->cargo.Truncate(new_cap);
196 u->refit_cap = std::min(new_cap, u->refit_cap);
197 u->cargo_cap = new_cap;
198 } else {
199 /* Verify capacity hasn't changed. */
200 if (new_cap != u->cargo_cap) ShowNewGrfVehicleError(u->engine_type, STR_NEWGRF_BROKEN, STR_NEWGRF_BROKEN_CAPACITY, GRFBug::VehCapacity, true);
201 }
202 u->vcache.cached_cargo_age_period = GetVehicleProperty(u, PROP_TRAIN_CARGO_AGE_PERIOD, e_u->info.cargo_age_period);
203
204 /* check the vehicle length (callback) */
205 uint16_t veh_len = CALLBACK_FAILED;
206 if (e_u->GetGRF() != nullptr && e_u->GetGRF()->grf_version >= 8) {
207 /* Use callback 36 */
208 veh_len = GetVehicleProperty(u, PROP_TRAIN_SHORTEN_FACTOR, CALLBACK_FAILED);
209
210 if (veh_len != CALLBACK_FAILED && veh_len >= VEHICLE_LENGTH) {
212 }
213 } else if (e_u->info.callback_mask.Test(VehicleCallbackMask::Length)) {
214 /* Use callback 11 */
215 veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, u->engine_type, u);
216 }
217 if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor;
218 veh_len = VEHICLE_LENGTH - Clamp(veh_len, 0, VEHICLE_LENGTH - 1);
219
220 if (allowed_changes.Test(ConsistChangeFlag::Length)) {
221 /* Update vehicle length. */
222 u->gcache.cached_veh_length = veh_len;
223 } else {
224 /* Verify length hasn't changed. */
225 if (veh_len != u->gcache.cached_veh_length) VehicleLengthChanged(u);
226 }
227
228 this->gcache.cached_total_length += u->gcache.cached_veh_length;
229 this->InvalidateNewGRFCache();
230 u->InvalidateNewGRFCache();
231 }
232
233 /* store consist weight/max speed in cache */
234 this->vcache.cached_max_speed = max_speed;
235 this->tcache.cached_tilt = train_can_tilt;
236 this->tcache.cached_curve_speed_mod = min_curve_speed_mod;
237 this->tcache.cached_max_curve_speed = this->GetCurveSpeedLimit();
238
239 /* recalculate cached weights and power too (we do this *after* the rest, so it is known which wagons are powered and need extra weight added) */
240 this->CargoChanged();
241
242 if (this->IsFrontEngine()) {
243 this->UpdateAcceleration();
248
249 /* If the consist is changed while in a depot, the vehicle view window must be invalidated to update the availability of refitting. */
251 }
252}
253
264int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *moving_front, int *station_ahead, int *station_length)
265{
266 const Train *consist = moving_front->First();
267 const Station *st = Station::Get(station_id);
268 *station_ahead = st->GetPlatformLength(tile, DirToDiagDir(moving_front->GetMovingDirection())) * TILE_SIZE;
269 *station_length = st->GetPlatformLength(tile) * TILE_SIZE;
270
271 /* Default to the middle of the station for stations stops that are not in
272 * the order list like intermediate stations when non-stop is disabled */
274 if (consist->gcache.cached_total_length >= *station_length) {
275 /* The train is longer than the station, make it stop at the far end of the platform */
277 } else if (consist->current_order.IsType(OT_GOTO_STATION) && consist->current_order.GetDestination() == station_id) {
278 osl = consist->current_order.GetStopLocation();
279 }
280
281 /* The stop location of the FRONT! of the train */
282 int stop;
283 switch (osl) {
284 default: NOT_REACHED();
285
287 stop = consist->gcache.cached_total_length;
288 break;
289
291 stop = *station_length - (*station_length - consist->gcache.cached_total_length) / 2;
292 break;
293
295 stop = *station_length;
296 break;
297 }
298
299 /* Subtract half the front vehicle length of the train so we get the real
300 * stop location of the train. */
301 uint8_t rounding = consist->IsDrivingBackwards() ? 2 : 1;
302 return stop - (consist->gcache.cached_veh_length + rounding) / 2;
303}
304
305
311{
312 assert(this->First() == this);
313
314 static const int absolute_max_speed = UINT16_MAX;
315 int max_speed = absolute_max_speed;
316
317 if (_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) return max_speed;
318
319 int curvecount[2] = {0, 0};
320
321 /* first find the curve speed limit */
322 int numcurve = 0;
323 int sum = 0;
324 int pos = 0;
325 int lastpos = -1;
326 for (const Train *u = this; u->Next() != nullptr; u = u->Next(), pos += u->gcache.cached_veh_length) {
327 Direction this_dir = u->direction;
328 Direction next_dir = u->Next()->direction;
329
330 DirDiff dirdiff = DirDifference(this_dir, next_dir);
331 if (dirdiff == DIRDIFF_SAME) continue;
332
333 if (dirdiff == DIRDIFF_45LEFT) curvecount[0]++;
334 if (dirdiff == DIRDIFF_45RIGHT) curvecount[1]++;
335 if (dirdiff == DIRDIFF_45LEFT || dirdiff == DIRDIFF_45RIGHT) {
336 if (lastpos != -1) {
337 numcurve++;
338 sum += pos - lastpos;
339 if (pos - lastpos <= static_cast<int>(VEHICLE_LENGTH) && max_speed > 88) {
340 max_speed = 88;
341 }
342 }
343 lastpos = pos;
344 }
345
346 /* if we have a 90 degree turn, fix the speed limit to 60 */
347 if (dirdiff == DIRDIFF_90LEFT || dirdiff == DIRDIFF_90RIGHT) {
348 max_speed = 61;
349 }
350 }
351
352 if (numcurve > 0 && max_speed > 88) {
353 if (curvecount[0] == 1 && curvecount[1] == 1) {
354 max_speed = absolute_max_speed;
355 } else {
356 sum = CeilDiv(sum, VEHICLE_LENGTH);
357 sum /= numcurve;
358 max_speed = 232 - (13 - Clamp(sum, 1, 12)) * (13 - Clamp(sum, 1, 12));
359 }
360 }
361
362 if (max_speed != absolute_max_speed) {
363 /* Apply the current railtype's curve speed advantage */
364 const RailTypeInfo *rti = GetRailTypeInfo(GetRailType(this->tile));
365 max_speed += (max_speed / 2) * rti->curve_speed;
366
367 if (this->tcache.cached_tilt) {
368 /* Apply max_speed bonus of 20% for a tilting train */
369 max_speed += max_speed / 5;
370 }
371
372 /* Apply max_speed modifier (cached value is fixed-point binary with 8 fractional bits)
373 * and clamp the result to an acceptable range. */
374 max_speed += (max_speed * this->tcache.cached_curve_speed_mod) / 256;
375 max_speed = Clamp(max_speed, 2, absolute_max_speed);
376 }
377
378 return static_cast<uint16_t>(max_speed);
379}
380
386{
387 const Train *moving_front = this->GetMovingFront();
388 int max_speed = _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL ?
389 this->gcache.cached_max_track_speed :
390 this->tcache.cached_max_curve_speed;
391
392 if (_settings_game.vehicle.train_acceleration_model == AM_REALISTIC && IsRailStationTile(moving_front->tile)) {
393 StationID sid = GetStationIndex(moving_front->tile);
394 if (this->current_order.ShouldStopAtStation(this, sid)) {
395 int station_ahead;
396 int station_length;
397 int stop_at = GetTrainStopLocation(sid, moving_front->tile, moving_front, &station_ahead, &station_length);
398
399 /* The distance to go is whatever is still ahead of the train minus the
400 * distance from the train's stop location to the end of the platform */
401 int distance_to_go = station_ahead / TILE_SIZE - (station_length - stop_at) / TILE_SIZE;
402
403 if (distance_to_go > 0) {
404 int st_max_speed = 120;
405
406 int delta_v = this->cur_speed / (distance_to_go + 1);
407 if (max_speed > (this->cur_speed - delta_v)) {
408 st_max_speed = this->cur_speed - (delta_v / 10);
409 }
410
411 st_max_speed = std::max(st_max_speed, 25 * distance_to_go);
412 max_speed = std::min(max_speed, st_max_speed);
413 }
414 }
415 }
416
417 for (const Train *u = this; u != nullptr; u = u->Next()) {
418 if (_settings_game.vehicle.train_acceleration_model == AM_REALISTIC && u->track == TRACK_BIT_DEPOT) {
419 constexpr int DEPOT_SPEED_LIMIT = 61;
420 max_speed = std::min(max_speed, DEPOT_SPEED_LIMIT);
421 break;
422 }
423
424 /* Vehicle is on the middle part of a bridge. */
425 if (u->track == TRACK_BIT_WORMHOLE && !u->vehstatus.Test(VehState::Hidden)) {
426 max_speed = std::min<int>(max_speed, GetBridgeSpec(GetBridgeType(u->tile))->speed);
427 }
428 }
429
430 max_speed = std::min<int>(max_speed, this->current_order.GetMaxSpeed());
431
432 /* If the train is going backwards, without a leading cab, restrict its speed. */
433 if (!moving_front->CanLeadTrain()) {
434 constexpr int BACKWARDS_NO_CAB_SPEED_LIMIT = 32;
435 max_speed = std::min<int>(max_speed, BACKWARDS_NO_CAB_SPEED_LIMIT);
436 }
437
438 return std::min<int>(max_speed, this->gcache.cached_max_track_speed);
439}
440
443{
444 assert(this->IsFrontEngine() || this->IsFreeWagon());
445
446 uint power = this->gcache.cached_power;
447 uint weight = this->gcache.cached_weight;
448 assert(weight != 0);
449 this->acceleration = Clamp(power / weight * 4, 1, 255);
450}
451
457{
458 if (this->gcache.cached_veh_length != 8 && this->flags.Test(VehicleRailFlag::Flipped) && !EngInfo(this->engine_type)->misc_flags.Test(EngineMiscFlag::RailFlips)) {
459 int reference_width = TRAININFO_DEFAULT_VEHICLE_WIDTH;
460
461 const Engine *e = this->GetEngine();
462 if (e->GetGRF() != nullptr && IsCustomVehicleSpriteNum(e->VehInfo<RailVehicleInfo>().image_index)) {
463 reference_width = e->GetGRF()->traininfo_vehicle_width;
464 }
465
466 return ScaleSpriteTrad((this->gcache.cached_veh_length - (int)VEHICLE_LENGTH) * reference_width / (int)VEHICLE_LENGTH);
467 }
468 return 0;
469}
470
477{
478 int reference_width = TRAININFO_DEFAULT_VEHICLE_WIDTH;
479 int vehicle_pitch = 0;
480
481 const Engine *e = this->GetEngine();
482 if (e->GetGRF() != nullptr && IsCustomVehicleSpriteNum(e->VehInfo<RailVehicleInfo>().image_index)) {
483 reference_width = e->GetGRF()->traininfo_vehicle_width;
484 vehicle_pitch = e->GetGRF()->traininfo_vehicle_pitch;
485 }
486
487 if (offset != nullptr) {
488 if (this->flags.Test(VehicleRailFlag::Flipped) && !EngInfo(this->engine_type)->misc_flags.Test(EngineMiscFlag::RailFlips)) {
489 offset->x = ScaleSpriteTrad(((int)this->gcache.cached_veh_length - (int)VEHICLE_LENGTH / 2) * reference_width / (int)VEHICLE_LENGTH);
490 } else {
491 offset->x = ScaleSpriteTrad(reference_width) / 2;
492 }
493 offset->y = ScaleSpriteTrad(vehicle_pitch);
494 }
495 return ScaleSpriteTrad(this->gcache.cached_veh_length * reference_width / VEHICLE_LENGTH);
496}
497
498static SpriteID GetDefaultTrainSprite(uint8_t spritenum, Direction direction)
499{
500 assert(IsValidImageIndex<VehicleType::Train>(spritenum));
501 return ((direction + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]) + _engine_sprite_base[spritenum];
502}
503
511{
512 uint8_t spritenum = this->spritenum;
513
515
516 if (IsCustomVehicleSpriteNum(spritenum)) {
518 GetCustomVehicleSprite(this, direction, image_type, result);
519 if (result->IsValid()) return;
520
522 }
523
525 SpriteID sprite = GetDefaultTrainSprite(spritenum, direction);
526
527 if (this->cargo.StoredCount() >= this->cargo_cap / 2U) sprite += _wagon_full_adder[spritenum];
528
529 result->Set(sprite);
530}
531
532static void GetRailIcon(EngineID engine, bool rear_head, int &y, EngineImageType image_type, VehicleSpriteSeq *result)
533{
534 const Engine *e = Engine::Get(engine);
535 Direction dir = rear_head ? DIR_E : DIR_W;
536 uint8_t spritenum = e->VehInfo<RailVehicleInfo>().image_index;
537
538 if (IsCustomVehicleSpriteNum(spritenum)) {
539 GetCustomVehicleIcon(engine, dir, image_type, result);
540 if (result->IsValid()) {
541 if (e->GetGRF() != nullptr) {
543 }
544 return;
545 }
546
547 spritenum = Engine::Get(engine)->original_image_index;
548 }
549
550 if (rear_head) spritenum++;
551
552 result->Set(GetDefaultTrainSprite(spritenum, DIR_W));
553}
554
555void DrawTrainEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
556{
557 const GRFFile *grf = Engine::Get(engine)->GetGRF();
558 int vehicle_width = ScaleSpriteTrad(grf == nullptr ? TRAININFO_DEFAULT_VEHICLE_WIDTH : grf->traininfo_vehicle_width);
559
560 if (RailVehInfo(engine)->railveh_type == RailVehicleType::Multihead) {
561 int yf = y;
562 int yr = y;
563
564 VehicleSpriteSeq seqf, seqr;
565 GetRailIcon(engine, false, yf, image_type, &seqf);
566 GetRailIcon(engine, true, yr, image_type, &seqr);
567
568 Rect rectf, rectr;
569 seqf.GetBounds(&rectf);
570 seqr.GetBounds(&rectr);
571
572 preferred_x = Clamp(preferred_x,
573 left - UnScaleGUI(rectf.left) + vehicle_width / 2,
574 right - UnScaleGUI(rectr.right) - (vehicle_width - vehicle_width / 2));
575
576 seqf.Draw(preferred_x - vehicle_width / 2, yf, pal, pal == PALETTE_CRASH);
577 seqr.Draw(preferred_x + (vehicle_width - vehicle_width / 2), yr, pal, pal == PALETTE_CRASH);
578 } else {
580 GetRailIcon(engine, false, y, image_type, &seq);
581
582 Rect rect;
583 seq.GetBounds(&rect);
584 preferred_x = Clamp(preferred_x,
585 left - UnScaleGUI(rect.left),
586 right - UnScaleGUI(rect.right));
587
588 seq.Draw(preferred_x, y, pal, pal == PALETTE_CRASH);
589 }
590}
591
601void GetTrainSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
602{
603 int y = 0;
604
606 GetRailIcon(engine, false, y, image_type, &seq);
607
608 Rect rect;
609 seq.GetBounds(&rect);
610
611 width = UnScaleGUI(rect.Width());
612 height = UnScaleGUI(rect.Height());
613 xoffs = UnScaleGUI(rect.left);
614 yoffs = UnScaleGUI(rect.top);
615
616 if (RailVehInfo(engine)->railveh_type == RailVehicleType::Multihead) {
617 const GRFFile *grf = Engine::Get(engine)->GetGRF();
618 int vehicle_width = ScaleSpriteTrad(grf == nullptr ? TRAININFO_DEFAULT_VEHICLE_WIDTH : grf->traininfo_vehicle_width);
619
620 GetRailIcon(engine, true, y, image_type, &seq);
621 seq.GetBounds(&rect);
622
623 /* Calculate values relative to an imaginary center between the two sprites. */
624 width = vehicle_width + UnScaleGUI(rect.right) - xoffs;
625 height = std::max<uint>(height, UnScaleGUI(rect.Height()));
626 xoffs = xoffs - vehicle_width / 2;
627 yoffs = std::min(yoffs, UnScaleGUI(rect.top));
628 }
629}
630
636static std::vector<VehicleID> GetFreeWagonsInDepot(TileIndex tile)
637{
638 std::vector<VehicleID> free_wagons;
639
640 for (Vehicle *v : VehiclesOnTile(tile)) {
641 if (v->type != VehicleType::Train) continue;
642 if (v->vehstatus.Test(VehState::Crashed)) continue;
643 if (!Train::From(v)->IsFreeWagon()) continue;
644
645 free_wagons.push_back(v->index);
646 }
647
648 /* Sort by vehicle index for consistency across clients. */
649 std::ranges::sort(free_wagons);
650 return free_wagons;
651}
652
661static CommandCost CmdBuildRailWagon(DoCommandFlags flags, TileIndex tile, const Engine *e, Vehicle **ret)
662{
663 const RailVehicleInfo *rvi = &e->VehInfo<RailVehicleInfo>();
664
665 /* Check that the wagon can drive on the track in question */
666 if (!IsCompatibleRail(rvi->railtypes, GetRailType(tile))) return CMD_ERROR;
667
668 if (flags.Test(DoCommandFlag::Execute)) {
669 Train *v = Train::Create();
670 *ret = v;
671 v->spritenum = rvi->image_index;
672
673 v->engine_type = e->index;
674 v->gcache.first_engine = EngineID::Invalid(); // needs to be set before first callback
675
677
678 v->direction = DiagDirToDir(dir);
679 v->tile = tile;
680
681 int x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
682 int y = TileY(tile) * TILE_SIZE | _vehicle_initial_y_fract[dir];
683
684 v->x_pos = x;
685 v->y_pos = y;
686 v->z_pos = GetSlopePixelZ(x, y, true);
690
691 v->SetWagon();
692
693 v->SetFreeWagon();
695
697 assert(IsValidCargoType(v->cargo_type));
698 v->cargo_cap = rvi->capacity;
699 v->refit_cap = 0;
700
701 v->railtypes = rvi->railtypes;
702
706 v->sprite_cache.sprite_seq.Set(SPR_IMG_QUERY);
707 v->random_bits = Random();
708
710
712 if (prob.has_value()) v->flags.Set(VehicleRailFlag::Flipped, prob.value());
714
715 v->UpdatePosition();
718
720
721 /* Try to connect the vehicle to one of free chains of wagons. */
722 for (VehicleID vehicle : GetFreeWagonsInDepot(tile)) {
723 if (vehicle == v->index) continue;
724
725 const Train *w = Train::Get(vehicle);
726 if (w->engine_type != v->engine_type) continue;
727 if (w->First() == v) continue;
728
729 if (Command<Commands::MoveRailVehicle>::Do(DoCommandFlag::Execute, v->index, w->Last()->index, true).Succeeded()) {
730 break;
731 }
732 }
733 }
734
735 return CommandCost();
736}
737
743{
744 assert(u->IsEngine());
745 for (VehicleID vehicle : GetFreeWagonsInDepot(u->tile)) {
746 if (Command<Commands::MoveRailVehicle>::Do(DoCommandFlag::Execute, vehicle, u->index, true).Failed()) {
747 break;
748 }
749 }
750}
751
752static void AddRearEngineToMultiheadedTrain(Train *v)
753{
754 Train *u = Train::Create();
755 v->value >>= 1;
756 u->value = v->value;
757 u->direction = v->direction;
758 u->owner = v->owner;
759 u->tile = v->tile;
760 u->x_pos = v->x_pos;
761 u->y_pos = v->y_pos;
762 u->z_pos = v->z_pos;
764 u->vehstatus = v->vehstatus;
766 u->spritenum = v->spritenum + 1;
767 u->cargo_type = v->cargo_type;
769 u->cargo_cap = v->cargo_cap;
770 u->refit_cap = v->refit_cap;
771 u->railtypes = v->railtypes;
772 u->engine_type = v->engine_type;
775 u->build_year = v->build_year;
776 u->sprite_cache.sprite_seq.Set(SPR_IMG_QUERY);
777 u->random_bits = Random();
778 v->SetMultiheaded();
779 u->SetMultiheaded();
780 v->SetNext(u);
782 if (prob.has_value()) u->flags.Set(VehicleRailFlag::Flipped, prob.value());
783 u->UpdatePosition();
784
785 /* Now we need to link the front and rear engines together */
788}
789
798CommandCost CmdBuildRailVehicle(DoCommandFlags flags, TileIndex tile, const Engine *e, Vehicle **ret)
799{
800 const RailVehicleInfo *rvi = &e->VehInfo<RailVehicleInfo>();
801
802 if (rvi->railveh_type == RailVehicleType::Wagon) return CmdBuildRailWagon(flags, tile, e, ret);
803
804 /* Check if depot and new engine uses the same kind of tracks *
805 * We need to see if the engine got power on the tile to avoid electric engines in non-electric depots */
806 if (!HasPowerOnRail(rvi->railtypes, GetRailType(tile))) return CMD_ERROR;
807
808 if (flags.Test(DoCommandFlag::Execute)) {
810 int x = TileX(tile) * TILE_SIZE + _vehicle_initial_x_fract[dir];
811 int y = TileY(tile) * TILE_SIZE + _vehicle_initial_y_fract[dir];
812
813 Train *v = Train::Create();
814 *ret = v;
815 v->direction = DiagDirToDir(dir);
816 v->tile = tile;
818 v->x_pos = x;
819 v->y_pos = y;
820 v->z_pos = GetSlopePixelZ(x, y, true);
823 v->spritenum = rvi->image_index;
825 assert(IsValidCargoType(v->cargo_type));
826 v->cargo_cap = rvi->capacity;
827 v->refit_cap = 0;
828 v->last_station_visited = StationID::Invalid();
829 v->last_loading_station = StationID::Invalid();
830
831 v->engine_type = e->index;
832 v->gcache.first_engine = EngineID::Invalid(); // needs to be set before first callback
833
834 v->reliability = e->reliability;
837
838 v->railtypes = rvi->railtypes;
839
840 v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_trains);
844 v->sprite_cache.sprite_seq.Set(SPR_IMG_QUERY);
845 v->random_bits = Random();
846
848 v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
849
851
852 v->SetFrontEngine();
853 v->SetEngine();
854
856 if (prob.has_value()) v->flags.Set(VehicleRailFlag::Flipped, prob.value());
857 v->UpdatePosition();
858
860 AddRearEngineToMultiheadedTrain(v);
861 } else {
863 }
864
867
869 }
870
871 return CommandCost();
872}
873
874static Train *FindGoodVehiclePos(const Train *src)
875{
876 EngineID eng = src->engine_type;
877
878 for (VehicleID vehicle : GetFreeWagonsInDepot(src->tile)) {
879 Train *dst = Train::Get(vehicle);
880
881 /* check so all vehicles in the line have the same engine. */
882 Train *t = dst;
883 while (t->engine_type == eng) {
884 t = t->Next();
885 if (t == nullptr) return dst;
886 }
887 }
888
889 return nullptr;
890}
891
893typedef std::vector<Train *> TrainList;
894
900static void MakeTrainBackup(TrainList &list, Train *t)
901{
902 for (; t != nullptr; t = t->Next()) list.push_back(t);
903}
904
910{
911 /* No train, nothing to do. */
912 if (list.empty()) return;
913
914 Train *prev = nullptr;
915 /* Iterate over the list and rebuild it. */
916 for (Train *t : list) {
917 if (prev != nullptr) {
918 prev->SetNext(t);
919 } else if (t->Previous() != nullptr) {
920 /* Make sure the head of the train is always the first in the chain. */
921 t->Previous()->SetNext(nullptr);
922 }
923 prev = t;
924 }
925}
926
932static void RemoveFromConsist(Train *part, bool chain = false)
933{
934 Train *tail;
935
936 if (chain) {
937 /* We're moving several vehicles, find the last one in the chain. */
938 tail = part;
939 while (tail->Next() != nullptr) tail = tail->Next();
940 } else {
941 /* We're just moving one vehicle, but make sure we get all the articulated parts. */
942 tail = part->GetLastEnginePart();
943 }
944
945 /* Unlink at the front, but make it point to the next
946 * vehicle after the to be remove part. */
947 if (part->Previous() != nullptr) part->Previous()->SetNext(tail->Next());
948
949 /* Unlink at the back */
950 tail->SetNext(nullptr);
951}
952
958static void InsertInConsist(Train *dst, Train *chain)
959{
960 /* We do not want to add something in the middle of an articulated part. */
961 assert(dst != nullptr && (dst->Next() == nullptr || !dst->Next()->IsArticulatedPart()));
962
963 chain->Last()->SetNext(dst->Next());
964 dst->SetNext(chain);
965}
966
973{
974 for (; t != nullptr; t = t->GetNextVehicle()) {
975 if (!t->IsMultiheaded() || !t->IsEngine()) continue;
976
977 /* Make sure that there are no free cars before next engine */
978 Train *u;
979 for (u = t; u->Next() != nullptr && !u->Next()->IsEngine(); u = u->Next()) {}
980
981 if (u == t->other_multiheaded_part) continue;
982
983 /* Remove the part from the 'wrong' train */
985 /* And add it to the 'right' train */
987 }
988}
989
994static void NormaliseSubtypes(Train *chain)
995{
996 /* Nothing to do */
997 if (chain == nullptr) return;
998
999 /* We must be the first in the chain. */
1000 assert(chain->Previous() == nullptr);
1001
1002 /* Set the appropriate bits for the first in the chain. */
1003 if (chain->IsWagon()) {
1004 chain->SetFreeWagon();
1005 } else {
1006 assert(chain->IsEngine());
1007 chain->SetFrontEngine();
1008 }
1009
1010 /* Now clear the bits for the rest of the chain */
1011 for (Train *t = chain->Next(); t != nullptr; t = t->Next()) {
1012 t->ClearFreeWagon();
1013 t->ClearFrontEngine();
1014 }
1015}
1016
1026static CommandCost CheckNewTrain(Train *original_dst, Train *dst, Train *original_src, Train *src)
1027{
1028 /* Just add 'new' engines and subtract the original ones.
1029 * If that's less than or equal to 0 we can be sure we did
1030 * not add any engines (read: trains) along the way. */
1031 if ((src != nullptr && src->IsEngine() ? 1 : 0) +
1032 (dst != nullptr && dst->IsEngine() ? 1 : 0) -
1033 (original_src != nullptr && original_src->IsEngine() ? 1 : 0) -
1034 (original_dst != nullptr && original_dst->IsEngine() ? 1 : 0) <= 0) {
1035 return CommandCost();
1036 }
1037
1038 /* Get a free unit number and check whether it's within the bounds.
1039 * There will always be a maximum of one new train. */
1040 if (GetFreeUnitNumber(VehicleType::Train) <= _settings_game.vehicle.max_trains) return CommandCost();
1041
1042 return CommandCost(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
1043}
1044
1051{
1052 /* No multi-part train, no need to check. */
1053 if (t == nullptr || t->Next() == nullptr) return CommandCost();
1054
1055 /* The maximum length for a train. For each part we decrease this by one
1056 * and if the result is negative the train is simply too long. */
1057 int allowed_len = _settings_game.vehicle.max_train_length * TILE_SIZE - t->gcache.cached_veh_length;
1058
1059 /* For free-wagon chains, check if they are within the max_train_length limit. */
1060 if (!t->IsEngine()) {
1061 t = t->Next();
1062 while (t != nullptr) {
1063 allowed_len -= t->gcache.cached_veh_length;
1064
1065 t = t->Next();
1066 }
1067
1068 if (allowed_len < 0) return CommandCost(STR_ERROR_TRAIN_TOO_LONG);
1069 return CommandCost();
1070 }
1071
1072 Train *head = t;
1073 Train *prev = t;
1074
1075 /* Break the prev -> t link so it always holds within the loop. */
1076 t = t->Next();
1077 prev->SetNext(nullptr);
1078
1079 /* Make sure the cache is cleared. */
1080 head->InvalidateNewGRFCache();
1081
1082 while (t != nullptr) {
1083 allowed_len -= t->gcache.cached_veh_length;
1084
1085 Train *next = t->Next();
1086
1087 /* Unlink the to-be-added piece; it is already unlinked from the previous
1088 * part due to the fact that the prev -> t link is broken. */
1089 t->SetNext(nullptr);
1090
1091 /* Don't check callback for articulated or rear dual headed parts */
1092 if (!t->IsArticulatedPart() && !t->IsRearDualheaded()) {
1093 /* Back up and clear the first_engine data to avoid using wagon override group */
1094 EngineID first_engine = t->gcache.first_engine;
1095 t->gcache.first_engine = EngineID::Invalid();
1096
1097 /* We don't want the cache to interfere. head's cache is cleared before
1098 * the loop and after each callback does not need to be cleared here. */
1100
1101 std::array<int32_t, 1> regs100;
1102 uint16_t callback = GetVehicleCallbackParent(CBID_TRAIN_ALLOW_WAGON_ATTACH, 0, 0, head->engine_type, t, head, regs100);
1103
1104 /* Restore original first_engine data */
1105 t->gcache.first_engine = first_engine;
1106
1107 /* We do not want to remember any cached variables from the test run */
1109 head->InvalidateNewGRFCache();
1110
1111 if (callback != CALLBACK_FAILED) {
1112 /* A failing callback means everything is okay */
1113 StringID error = STR_NULL;
1114
1115 if (head->GetGRF()->grf_version < 8) {
1116 if (callback == 0xFD) error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
1117 if (callback < 0xFD) error = GetGRFStringID(head->GetGRFID(), GRFSTR_MISC_GRF_TEXT + callback);
1118 if (callback >= 0x100) ErrorUnknownCallbackResult(head->GetGRFID(), CBID_TRAIN_ALLOW_WAGON_ATTACH, callback);
1119 } else {
1120 if (callback < 0x400) {
1121 error = GetGRFStringID(head->GetGRFID(), GRFSTR_MISC_GRF_TEXT + callback);
1122 } else {
1123 switch (callback) {
1124 case 0x400: // allow if railtypes match (always the case for OpenTTD)
1125 case 0x401: // allow
1126 break;
1127
1128 case 0x40F:
1129 error = GetGRFStringID(head->GetGRFID(), static_cast<GRFStringID>(regs100[0]));
1130 break;
1131
1132 default: // unknown reason -> disallow
1133 case 0x402: // disallow attaching
1134 error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
1135 break;
1136 }
1137 }
1138 }
1139
1140 if (error != STR_NULL) return CommandCost(error);
1141 }
1142 }
1143
1144 /* And link it to the new part. */
1145 prev->SetNext(t);
1146 prev = t;
1147 t = next;
1148 }
1149
1150 if (allowed_len < 0) return CommandCost(STR_ERROR_TRAIN_TOO_LONG);
1151 return CommandCost();
1152}
1153
1164static CommandCost ValidateTrains(Train *original_dst, Train *dst, Train *original_src, Train *src, bool check_limit)
1165{
1166 /* Check whether we may actually construct the trains. */
1168 if (ret.Failed()) return ret;
1169 ret = CheckTrainAttachment(dst);
1170 if (ret.Failed()) return ret;
1171
1172 /* Check whether we need to build a new train. */
1173 return check_limit ? CheckNewTrain(original_dst, dst, original_src, src) : CommandCost();
1174}
1175
1184static void ArrangeTrains(Train **dst_head, Train *dst, Train **src_head, Train *src, bool move_chain)
1185{
1186 /* First determine the front of the two resulting trains */
1187 if (*src_head == *dst_head) {
1188 /* If we aren't moving part(s) to a new train, we are just moving the
1189 * front back and there is not destination head. */
1190 *dst_head = nullptr;
1191 } else if (*dst_head == nullptr) {
1192 /* If we are moving to a new train the head of the move train would become
1193 * the head of the new vehicle. */
1194 *dst_head = src;
1195 }
1196
1197 if (src == *src_head) {
1198 /* If we are moving the front of a train then we are, in effect, creating
1199 * a new head for the train. Point to that. Unless we are moving the whole
1200 * train in which case there is not 'source' train anymore.
1201 * In case we are a multiheaded part we want the complete thing to come
1202 * with us, so src->GetNextUnit(), however... when we are e.g. a wagon
1203 * that is followed by a rear multihead we do not want to include that. */
1204 *src_head = move_chain ? nullptr :
1205 (src->IsMultiheaded() ? src->GetNextUnit() : src->GetNextVehicle());
1206 }
1207
1208 /* Now it's just simply removing the part that we are going to move from the
1209 * source train and *if* the destination is a not a new train add the chain
1210 * at the destination location. */
1211 RemoveFromConsist(src, move_chain);
1212 if (*dst_head != src) InsertInConsist(dst, src);
1213
1214 /* Now normalise the dual heads, that is move the dual heads around in such
1215 * a way that the head and rear of a dual head are in the same train */
1216 NormaliseDualHeads(*src_head);
1217 NormaliseDualHeads(*dst_head);
1218}
1219
1225static void NormaliseTrainHead(Train *head)
1226{
1227 /* Not much to do! */
1228 if (head == nullptr) return;
1229
1230 /* Tell the 'world' the train changed. */
1232 UpdateTrainGroupID(head);
1233
1234 /* Not a front engine, i.e. a free wagon chain. No need to do more. */
1235 if (!head->IsFrontEngine()) return;
1236
1237 /* Update the refit button and window */
1240
1241 /* If we don't have a unit number yet, set one. */
1242 if (head->unitnumber != 0) return;
1243 head->unitnumber = Company::Get(head->owner)->freeunits[head->type].UseID(GetFreeUnitNumber(VehicleType::Train));
1244}
1245
1255CommandCost CmdMoveRailVehicle(DoCommandFlags flags, VehicleID src_veh, VehicleID dest_veh, bool move_chain)
1256{
1257 Train *src = Train::GetIfValid(src_veh);
1258 if (src == nullptr) return CMD_ERROR;
1259
1260 CommandCost ret = CheckOwnership(src->owner);
1261 if (ret.Failed()) return ret;
1262
1263 /* Do not allow moving crashed vehicles inside the depot, it is likely to cause asserts later */
1264 if (src->vehstatus.Test(VehState::Crashed)) return CMD_ERROR;
1265
1266 /* if nothing is selected as destination, try and find a matching vehicle to drag to. */
1267 Train *dst;
1268 if (dest_veh == VehicleID::Invalid()) {
1269 dst = (src->IsEngine() || flags.Test(DoCommandFlag::AutoReplace)) ? nullptr : FindGoodVehiclePos(src);
1270 } else {
1271 dst = Train::GetIfValid(dest_veh);
1272 if (dst == nullptr) return CMD_ERROR;
1273
1274 ret = CheckOwnership(dst->owner);
1275 if (ret.Failed()) return ret;
1276
1277 /* Do not allow appending to crashed vehicles, too */
1278 if (dst->vehstatus.Test(VehState::Crashed)) return CMD_ERROR;
1279 }
1280
1281 /* if an articulated part is being handled, deal with its parent vehicle */
1282 src = src->GetFirstEnginePart();
1283 if (dst != nullptr) {
1284 dst = dst->GetFirstEnginePart();
1285 }
1286
1287 /* don't move the same vehicle.. */
1288 if (src == dst) return CommandCost();
1289
1290 /* locate the head of the two chains */
1291 Train *src_head = src->First();
1292 Train *dst_head;
1293 if (dst != nullptr) {
1294 dst_head = dst->First();
1295 if (dst_head->tile != src_head->tile) return CMD_ERROR;
1296 /* Now deal with articulated part of destination wagon */
1297 dst = dst->GetLastEnginePart();
1298 } else {
1299 dst_head = nullptr;
1300 }
1301
1302 if (src->IsRearDualheaded()) return CommandCost(STR_ERROR_REAR_ENGINE_FOLLOW_FRONT);
1303
1304 /* When moving all wagons, we can't have the same src_head and dst_head */
1305 if (move_chain && src_head == dst_head) return CommandCost();
1306
1307 /* When moving a multiheaded part to be place after itself, bail out. */
1308 if (!move_chain && dst != nullptr && dst->IsRearDualheaded() && src == dst->other_multiheaded_part) return CommandCost();
1309
1310 /* Check if all vehicles in the source train are stopped inside a depot. */
1311 if (!src_head->IsStoppedInDepot()) return CommandCost(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
1312
1313 /* Check if all vehicles in the destination train are stopped inside a depot. */
1314 if (dst_head != nullptr && !dst_head->IsStoppedInDepot()) return CommandCost(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
1315
1316 /* First make a backup of the order of the trains. That way we can do
1317 * whatever we want with the order and later on easily revert. */
1318 TrainList original_src;
1319 TrainList original_dst;
1320
1321 MakeTrainBackup(original_src, src_head);
1322 MakeTrainBackup(original_dst, dst_head);
1323
1324 /* Also make backup of the original heads as ArrangeTrains can change them.
1325 * For the destination head we do not care if it is the same as the source
1326 * head because in that case it's just a copy. */
1327 Train *original_src_head = src_head;
1328 Train *original_dst_head = (dst_head == src_head ? nullptr : dst_head);
1329
1330 /* We want this information from before the rearrangement, but execute this after the validation.
1331 * original_src_head can't be nullptr; src is by definition != nullptr, so src_head can't be nullptr as
1332 * src->GetFirst() always yields non-nullptr, so eventually original_src_head != nullptr as well. */
1333 bool original_src_head_front_engine = original_src_head->IsFrontEngine();
1334 bool original_dst_head_front_engine = original_dst_head != nullptr && original_dst_head->IsFrontEngine();
1335
1336 /* (Re)arrange the trains in the wanted arrangement. */
1337 ArrangeTrains(&dst_head, dst, &src_head, src, move_chain);
1338
1339 if (!flags.Test(DoCommandFlag::AutoReplace)) {
1340 /* If the autoreplace flag is set we do not need to test for the validity
1341 * because we are going to revert the train to its original state. As we
1342 * assume the original state was correct autoreplace can skip this. */
1343 ret = ValidateTrains(original_dst_head, dst_head, original_src_head, src_head, true);
1344 if (ret.Failed()) {
1345 /* Restore the train we had. */
1346 RestoreTrainBackup(original_src);
1347 RestoreTrainBackup(original_dst);
1348 return ret;
1349 }
1350 }
1351
1352 /* do it? */
1353 if (flags.Test(DoCommandFlag::Execute)) {
1354 /* Remove old heads from the statistics */
1355 if (original_src_head_front_engine) GroupStatistics::CountVehicle(original_src_head, -1);
1356 if (original_dst_head_front_engine) GroupStatistics::CountVehicle(original_dst_head, -1);
1357
1358 /* First normalise the sub types of the chains. */
1359 NormaliseSubtypes(src_head);
1360 NormaliseSubtypes(dst_head);
1361
1362 /* There are 14 different cases:
1363 * 1) front engine gets moved to a new train, it stays a front engine.
1364 * a) the 'next' part is a wagon that becomes a free wagon chain.
1365 * b) the 'next' part is an engine that becomes a front engine.
1366 * c) there is no 'next' part, nothing else happens
1367 * 2) front engine gets moved to another train, it is not a front engine anymore
1368 * a) the 'next' part is a wagon that becomes a free wagon chain.
1369 * b) the 'next' part is an engine that becomes a front engine.
1370 * c) there is no 'next' part, nothing else happens
1371 * 3) front engine gets moved to later in the current train, it is not a front engine anymore.
1372 * a) the 'next' part is a wagon that becomes a free wagon chain.
1373 * b) the 'next' part is an engine that becomes a front engine.
1374 * 4) free wagon gets moved
1375 * a) the 'next' part is a wagon that becomes a free wagon chain.
1376 * b) the 'next' part is an engine that becomes a front engine.
1377 * c) there is no 'next' part, nothing else happens
1378 * 5) non front engine gets moved and becomes a new train, nothing else happens
1379 * 6) non front engine gets moved within a train / to another train, nothing happens
1380 * 7) wagon gets moved, nothing happens
1381 */
1382 if (src == original_src_head && src->IsEngine() && !src->IsFrontEngine()) {
1383 /* Cases #2 and #3: the front engine gets trashed. */
1384 CloseWindowById(WC_VEHICLE_VIEW, src->index);
1386 CloseWindowById(WC_VEHICLE_REFIT, src->index);
1391
1392 if (src_head != nullptr && src_head->IsFrontEngine()) {
1393 /* Cases #?b: Transfer order, unit number and other stuff
1394 * to the new front engine. */
1395 src_head->orders = src->orders;
1396 if (src_head->orders != nullptr) src_head->AddToShared(src);
1397 src_head->CopyVehicleConfigAndStatistics(src);
1398 }
1399 /* Remove stuff not valid anymore for non-front engines. */
1401 src->ReleaseUnitNumber();
1402 src->name.clear();
1403 }
1404
1405 /* We weren't a front engine but are becoming one. So
1406 * we should be put in the default group. */
1407 if (original_src_head != src && dst_head == src) {
1410 }
1411
1412 /* Handle 'new engine' part of cases #1b, #2b, #3b, #4b and #5 in NormaliseTrainHead. */
1413 NormaliseTrainHead(src_head);
1414 NormaliseTrainHead(dst_head);
1415
1416 /* Add new heads to statistics.
1417 * This should be done after NormaliseTrainHead due to engine total limit checks in GetFreeUnitNumber. */
1418 if (src_head != nullptr && src_head->IsFrontEngine()) GroupStatistics::CountVehicle(src_head, 1);
1419 if (dst_head != nullptr && dst_head->IsFrontEngine()) GroupStatistics::CountVehicle(dst_head, 1);
1420
1422 CheckCargoCapacity(src_head);
1423 CheckCargoCapacity(dst_head);
1424 }
1425
1426 if (src_head != nullptr) src_head->First()->MarkDirty();
1427 if (dst_head != nullptr) dst_head->First()->MarkDirty();
1428
1429 /* We are undoubtedly changing something in the depot and train list. */
1432 } else {
1433 /* We don't want to execute what we're just tried. */
1434 RestoreTrainBackup(original_src);
1435 RestoreTrainBackup(original_dst);
1436 }
1437
1438 return CommandCost();
1439}
1440
1453CommandCost CmdSellRailWagon(DoCommandFlags flags, Vehicle *t, bool sell_chain, bool backup_order, ClientID user)
1454{
1456 Train *first = v->First();
1457
1458 if (v->IsRearDualheaded()) return CommandCost(STR_ERROR_REAR_ENGINE_FOLLOW_FRONT);
1459
1460 /* First make a backup of the order of the train. That way we can do
1461 * whatever we want with the order and later on easily revert. */
1462 TrainList original;
1463 MakeTrainBackup(original, first);
1464
1465 /* We need to keep track of the new head and the head of what we're going to sell. */
1466 Train *new_head = first;
1467 Train *sell_head = nullptr;
1468
1469 /* Split the train in the wanted way. */
1470 ArrangeTrains(&sell_head, nullptr, &new_head, v, sell_chain);
1471
1472 /* We don't need to validate the second train; it's going to be sold. */
1473 CommandCost ret = ValidateTrains(nullptr, nullptr, first, new_head, !flags.Test(DoCommandFlag::AutoReplace));
1474 if (ret.Failed()) {
1475 /* Restore the train we had. */
1476 RestoreTrainBackup(original);
1477 return ret;
1478 }
1479
1480 if (first->orders == nullptr && !OrderList::CanAllocateItem()) {
1481 /* Restore the train we had. */
1482 RestoreTrainBackup(original);
1483 return CommandCost(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
1484 }
1485
1487 for (Train *part = sell_head; part != nullptr; part = part->Next()) cost.AddCost(-part->value);
1488
1489 /* do it? */
1490 if (flags.Test(DoCommandFlag::Execute)) {
1491 /* First normalise the sub types of the chain. */
1492 NormaliseSubtypes(new_head);
1493
1494 if (v == first && !sell_chain && new_head != nullptr && new_head->IsFrontEngine()) {
1495 if (v->IsEngine()) {
1496 /* We are selling the front engine. In this case we want to
1497 * 'give' the order, unit number and such to the new head. */
1498 new_head->orders = first->orders;
1499 new_head->AddToShared(first);
1500 DeleteVehicleOrders(first);
1501
1502 /* Copy other important data from the front engine */
1503 new_head->CopyVehicleConfigAndStatistics(first);
1504 }
1505 GroupStatistics::CountVehicle(new_head, 1); // after copying over the profit, if required
1506 } else if (v->IsPrimaryVehicle() && backup_order) {
1507 OrderBackup::Backup(v, user);
1508 }
1509
1510 /* We need to update the information about the train. */
1511 NormaliseTrainHead(new_head);
1512
1513 /* We are undoubtedly changing something in the depot and train list. */
1516
1517 /* Actually delete the sold 'goods' */
1518 delete sell_head;
1519 } else {
1520 /* We don't want to execute what we're just tried. */
1521 RestoreTrainBackup(original);
1522 }
1523
1524 return cost;
1525}
1526
1528{
1529 /* Set common defaults. */
1530 this->bounds = {{-1, -1, 0}, {3, 3, 6}, {}};
1531
1532 /* Set if flipped and engine is NOT flagged with custom flip handling. */
1533 int flipped = this->flags.Test(VehicleRailFlag::Flipped) && !EngInfo(this->engine_type)->misc_flags.Test(EngineMiscFlag::RailFlips);
1534 /* If flipped and vehicle length is odd, we need to adjust the bounding box offset slightly. */
1535 int flip_offs = flipped && (this->gcache.cached_veh_length & 1);
1536
1537 Direction dir = this->direction;
1538 if (flipped) dir = ReverseDir(dir);
1539
1540 if (!IsDiagonalDirection(dir)) {
1541 static const Point _sign_table[] = {
1542 /* x, y */
1543 {-1, -1}, // DIR_N
1544 {-1, 1}, // DIR_E
1545 { 1, 1}, // DIR_S
1546 { 1, -1}, // DIR_W
1547 };
1548
1549 int half_shorten = (VEHICLE_LENGTH - this->gcache.cached_veh_length + flipped) / 2;
1550
1551 /* For all straight directions, move the bound box to the centre of the vehicle, but keep the size. */
1552 this->bounds.offset.x -= half_shorten * _sign_table[DirToDiagDir(dir)].x;
1553 this->bounds.offset.y -= half_shorten * _sign_table[DirToDiagDir(dir)].y;
1554 } else {
1555 switch (dir) {
1556 /* Shorten southern corner of the bounding box according the vehicle length
1557 * and center the bounding box on the vehicle. */
1558 case DIR_NE:
1559 this->bounds.origin.x = -(this->gcache.cached_veh_length + 1) / 2 + flip_offs;
1560 this->bounds.extent.x = this->gcache.cached_veh_length;
1561 this->bounds.offset.x = 1;
1562 break;
1563
1564 case DIR_NW:
1565 this->bounds.origin.y = -(this->gcache.cached_veh_length + 1) / 2 + flip_offs;
1566 this->bounds.extent.y = this->gcache.cached_veh_length;
1567 this->bounds.offset.y = 1;
1568 break;
1569
1570 /* Move northern corner of the bounding box down according to vehicle length
1571 * and center the bounding box on the vehicle. */
1572 case DIR_SW:
1573 this->bounds.origin.x = -(this->gcache.cached_veh_length) / 2 - flip_offs;
1574 this->bounds.extent.x = this->gcache.cached_veh_length;
1575 this->bounds.offset.x = 1 - (VEHICLE_LENGTH - this->gcache.cached_veh_length);
1576 break;
1577
1578 case DIR_SE:
1579 this->bounds.origin.y = -(this->gcache.cached_veh_length) / 2 - flip_offs;
1580 this->bounds.extent.y = this->gcache.cached_veh_length;
1581 this->bounds.offset.y = 1 - (VEHICLE_LENGTH - this->gcache.cached_veh_length);
1582 break;
1583
1584 default:
1585 NOT_REACHED();
1586 }
1587 }
1588}
1589
1594static void MarkTrainAsStuck(Train *consist)
1595{
1596 if (!consist->flags.Test(VehicleRailFlag::Stuck)) {
1597 /* It is the first time the problem occurred, set the "train stuck" flag. */
1599
1600 consist->wait_counter = 0;
1601
1602 /* Stop train */
1603 consist->cur_speed = 0;
1604 consist->subspeed = 0;
1605 consist->SetLastSpeed();
1606
1608 }
1609}
1610
1618static void SwapTrainFlags(uint16_t *swap_flag1, uint16_t *swap_flag2)
1619{
1620 uint16_t flag1 = *swap_flag1;
1621 uint16_t flag2 = *swap_flag2;
1622
1623 /* Clear the flags */
1624 ClrBit(*swap_flag1, GVF_GOINGUP_BIT);
1625 ClrBit(*swap_flag1, GVF_GOINGDOWN_BIT);
1626 ClrBit(*swap_flag2, GVF_GOINGUP_BIT);
1627 ClrBit(*swap_flag2, GVF_GOINGDOWN_BIT);
1628
1629 /* Reverse the rail-flags (if needed) */
1630 if (HasBit(flag1, GVF_GOINGUP_BIT)) {
1631 SetBit(*swap_flag2, GVF_GOINGDOWN_BIT);
1632 } else if (HasBit(flag1, GVF_GOINGDOWN_BIT)) {
1633 SetBit(*swap_flag2, GVF_GOINGUP_BIT);
1634 }
1635 if (HasBit(flag2, GVF_GOINGUP_BIT)) {
1636 SetBit(*swap_flag1, GVF_GOINGDOWN_BIT);
1637 } else if (HasBit(flag2, GVF_GOINGDOWN_BIT)) {
1638 SetBit(*swap_flag1, GVF_GOINGUP_BIT);
1639 }
1640}
1641
1647static void UpdateStatusAfterSwap(Train *v, bool reverse = true)
1648{
1649 /* Maybe reverse the direction. */
1650 if (reverse) v->direction = ReverseDir(v->direction);
1651
1652 /* Call the proper EnterTile function unless we are in a wormhole. */
1653 if (v->track != TRACK_BIT_WORMHOLE) {
1654 VehicleEnterTile(v, v->tile, v->x_pos, v->y_pos);
1655 } else {
1656 /* VehicleEnterTile_TunnelBridge() sets TRACK_BIT_WORMHOLE when the vehicle
1657 * is on the last bit of the bridge head (frame == TILE_SIZE - 1).
1658 * If we were swapped with such a vehicle, we have set TRACK_BIT_WORMHOLE,
1659 * when we shouldn't have. Check if this is the case. */
1660 TileIndex vt = TileVirtXY(v->x_pos, v->y_pos);
1662 VehicleEnterTile(v, vt, v->x_pos, v->y_pos);
1663 if (v->track != TRACK_BIT_WORMHOLE && IsBridgeTile(v->tile)) {
1664 /* We have just left the wormhole, possibly set the
1665 * "goingdown" bit. UpdateInclination() can be used
1666 * because we are at the border of the tile. */
1667 v->UpdatePosition();
1668 v->UpdateInclination(true, true);
1669 return;
1670 }
1671 }
1672 }
1673
1674 v->UpdatePosition();
1675 v->UpdateViewport(true, true);
1676}
1677
1684void ReverseTrainSwapVeh(Train *v, int l, int r)
1685{
1686 Train *a, *b;
1687
1688 /* locate vehicles to swap */
1689 for (a = v; l != 0; l--) a = a->Next();
1690 for (b = v; r != 0; r--) b = b->Next();
1691
1692 if (a != b) {
1693 /* swap the hidden bits */
1694 {
1695 bool a_hidden = a->vehstatus.Test(VehState::Hidden);
1696 bool b_hidden = b->vehstatus.Test(VehState::Hidden);
1697 b->vehstatus.Set(VehState::Hidden, a_hidden);
1698 a->vehstatus.Set(VehState::Hidden, b_hidden);
1699 }
1700
1701 std::swap(a->track, b->track);
1702 std::swap(a->direction, b->direction);
1703 std::swap(a->x_pos, b->x_pos);
1704 std::swap(a->y_pos, b->y_pos);
1705 std::swap(a->tile, b->tile);
1706 std::swap(a->z_pos, b->z_pos);
1707
1709
1712 } else {
1713 /* Swap GVF_GOINGUP_BIT/GVF_GOINGDOWN_BIT.
1714 * This is a little bit redundant way, a->gv_flags will
1715 * be (re)set twice, but it reduces code duplication */
1718 }
1719}
1720
1726static bool IsTrain(const Vehicle *v)
1727{
1728 return v->type == VehicleType::Train;
1729}
1730
1738{
1739 assert(IsLevelCrossingTile(tile));
1740
1741 return HasVehicleOnTile(tile, IsTrain);
1742}
1743
1751{
1752 if (v->type != VehicleType::Train || v->vehstatus.Test(VehState::Crashed)) return false;
1753
1754 const Train *t = Train::From(v);
1755 if (!t->IsMovingFront()) return false;
1756
1757 return TrainApproachingCrossingTile(t) == tile;
1758}
1759
1760
1768{
1769 assert(IsLevelCrossingTile(tile));
1770
1772 TileIndex tile_from = tile + TileOffsByDiagDir(dir);
1773
1774 if (HasVehicleOnTile(tile_from, [&](const Vehicle *v) {
1775 return TrainApproachingCrossingEnum(v, tile);
1776 })) return true;
1777
1778 dir = ReverseDiagDir(dir);
1779 tile_from = tile + TileOffsByDiagDir(dir);
1780
1781 return HasVehicleOnTile(tile_from, [&](const Vehicle *v) {
1782 return TrainApproachingCrossingEnum(v, tile);
1783 });
1784}
1785
1791static inline bool CheckLevelCrossing(TileIndex tile)
1792{
1793 /* reserved || train on crossing || train approaching crossing */
1795}
1796
1804static void UpdateLevelCrossingTile(TileIndex tile, bool sound, bool force_barred)
1805{
1806 assert(IsLevelCrossingTile(tile));
1807 bool set_barred;
1808
1809 /* We force the crossing to be barred when an adjacent crossing is barred, otherwise let it decide for itself. */
1810 set_barred = force_barred || CheckLevelCrossing(tile);
1811
1812 /* The state has changed */
1813 if (set_barred != IsCrossingBarred(tile)) {
1814 if (set_barred && sound && _settings_client.sound.ambient) SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
1815 SetCrossingBarred(tile, set_barred);
1816 MarkTileDirtyByTile(tile);
1817 }
1818}
1819
1826void UpdateLevelCrossing(TileIndex tile, bool sound, bool force_bar)
1827{
1828 if (!IsLevelCrossingTile(tile)) return;
1829
1830 bool forced_state = force_bar;
1831
1832 const Axis axis = GetCrossingRoadAxis(tile);
1833 const DiagDirection dir1 = AxisToDiagDir(axis);
1834 const DiagDirection dir2 = ReverseDiagDir(dir1);
1835
1836 /* Check if an adjacent crossing is barred. */
1837 for (DiagDirection dir : { dir1, dir2 }) {
1838 for (TileIndex t = tile; !forced_state && t < Map::Size() && IsLevelCrossingTile(t) && GetCrossingRoadAxis(t) == axis; t = TileAddByDiagDir(t, dir)) {
1839 forced_state |= CheckLevelCrossing(t);
1840 }
1841 }
1842
1843 /* Now that we know whether all tiles in this crossing should be barred or open,
1844 * we need to update those tiles. We start with the tile itself, then look along the road axis. */
1845 UpdateLevelCrossingTile(tile, sound, forced_state);
1846 for (DiagDirection dir : { dir1, dir2 }) {
1847 for (TileIndex t = TileAddByDiagDir(tile, dir); t < Map::Size() && IsLevelCrossingTile(t) && GetCrossingRoadAxis(t) == axis; t = TileAddByDiagDir(t, dir)) {
1848 UpdateLevelCrossingTile(t, sound, forced_state);
1849 }
1850 }
1851}
1852
1859{
1860 const DiagDirection dir1 = AxisToDiagDir(road_axis);
1861 const DiagDirection dir2 = ReverseDiagDir(dir1);
1862 for (DiagDirection dir : { dir1, dir2 }) {
1863 const TileIndex t = TileAddByDiagDir(tile, dir);
1864 if (t < Map::Size() && IsLevelCrossingTile(t) && GetCrossingRoadAxis(t) == road_axis) {
1866 }
1867 }
1868}
1869
1876{
1877 const DiagDirection dir1 = AxisToDiagDir(road_axis);
1878 const DiagDirection dir2 = ReverseDiagDir(dir1);
1879 for (DiagDirection dir : { dir1, dir2 }) {
1880 const TileIndexDiff diff = TileOffsByDiagDir(dir);
1881 bool occupied = false;
1882 for (TileIndex t = tile + diff; t < Map::Size() && IsLevelCrossingTile(t) && GetCrossingRoadAxis(t) == road_axis; t += diff) {
1883 occupied |= CheckLevelCrossing(t);
1884 }
1885 if (occupied) {
1886 /* Mark the immediately adjacent tile dirty */
1887 const TileIndex t = tile + diff;
1888 if (t < Map::Size() && IsLevelCrossingTile(t) && GetCrossingRoadAxis(t) == road_axis) {
1890 }
1891 } else {
1892 /* Unbar the crossing tiles in this direction as necessary */
1893 for (TileIndex t = tile + diff; t < Map::Size() && IsLevelCrossingTile(t) && GetCrossingRoadAxis(t) == road_axis; t += diff) {
1894 if (IsCrossingBarred(t)) {
1895 /* The crossing tile is barred, unbar it and continue to check the next tile */
1896 SetCrossingBarred(t, false);
1898 } else {
1899 /* The crossing tile is already unbarred, mark the tile dirty and stop checking */
1901 break;
1902 }
1903 }
1904 }
1905 }
1906}
1907
1913static inline void MaybeBarCrossingWithSound(TileIndex tile)
1914{
1915 if (!IsCrossingBarred(tile)) {
1916 SetCrossingReservation(tile, true);
1917 UpdateLevelCrossing(tile, true);
1918 }
1919}
1920
1921
1927static void AdvanceWagonsBeforeSwap(Train *moving_front)
1928{
1929 Train *base = moving_front;
1930 Train *first = base; // first vehicle to move
1931 Train *last = moving_front->GetMovingBack(); // last vehicle to move
1932 uint length = CountVehiclesInChain(moving_front->First());
1933
1934 while (length > 2) {
1935 last = last->GetMovingPrev();
1936 first = first->GetMovingNext();
1937
1938 int differential = base->CalcNextVehicleOffset() - last->CalcNextVehicleOffset();
1939
1940 /* do not update images now
1941 * negative differential will be handled in AdvanceWagonsAfterSwap() */
1942 for (int i = 0; i < differential; i++) TrainController(first, last->GetMovingNext());
1943
1944 base = first; // == base->GetMovingNext()
1945 length -= 2;
1946 }
1947}
1948
1949
1955static void AdvanceWagonsAfterSwap(Train *moving_front)
1956{
1957 /* first of all, fix the situation when the train was entering a depot */
1958 Train *dep = moving_front; // last vehicle in front of just left depot
1959 while (dep->GetMovingNext() != nullptr && (dep->track == TRACK_BIT_DEPOT || dep->GetMovingNext()->track != TRACK_BIT_DEPOT)) {
1960 dep = dep->GetMovingNext(); // find first vehicle outside of a depot, with next vehicle inside a depot
1961 }
1962
1963 Train *leave = dep->GetMovingNext(); // first vehicle in a depot we are leaving now
1964
1965 if (leave != nullptr) {
1966 /* 'pull' next wagon out of the depot, so we won't miss it (it could stay in depot forever) */
1967 int d = TicksToLeaveDepot(dep);
1968
1969 if (d <= 0) {
1970 leave->vehstatus.Reset(VehState::Hidden); // move it out of the depot
1971 leave->track = TrackToTrackBits(GetRailDepotTrack(leave->tile));
1972 for (int i = 0; i >= d; i--) TrainController(leave, nullptr); // maybe move it, and maybe let another wagon leave
1973 }
1974 } else {
1975 dep = nullptr; // no vehicle in a depot, so no vehicle leaving a depot
1976 }
1977
1978 Train *base = moving_front;
1979 Train *first = base; // first vehicle to move
1980 Train *last = moving_front->GetMovingBack(); // last vehicle to move
1981 uint length = CountVehiclesInChain(moving_front->First());
1982
1983 /* We have to make sure all wagons that leave a depot because of train reversing are moved correctly
1984 * they have already correct spacing, so we have to make sure they are moved how they should */
1985 bool nomove = (dep == nullptr); // If there is no vehicle leaving a depot, limit the number of wagons moved immediately.
1986
1987 while (length > 2) {
1988 /* we reached vehicle (originally) in front of a depot, stop now
1989 * (we would move wagons that are already moved with new wagon length). */
1990 if (base == dep) break;
1991
1992 /* the last wagon was that one leaving a depot, so do not move it anymore */
1993 if (last == dep) nomove = true;
1994
1995 last = last->GetMovingPrev();
1996 first = first->GetMovingNext();
1997
1998 int differential = last->CalcNextVehicleOffset() - base->CalcNextVehicleOffset();
1999
2000 /* do not update images now */
2001 for (int i = 0; i < differential; i++) TrainController(first, (nomove ? last->GetMovingNext() : nullptr));
2002
2003 base = first; // == base->GetMovingNext()
2004 length -= 2;
2005 }
2006}
2007
2008static bool IsWholeTrainInsideDepot(const Train *v)
2009{
2010 for (const Train *u = v; u != nullptr; u = u->Next()) {
2011 if (u->track != TRACK_BIT_DEPOT || u->tile != v->tile) return false;
2012 }
2013 return true;
2014}
2015
2020static void ReverseTrainDirection(Train *consist)
2021{
2022 Train *moving_front = consist->GetMovingFront();
2023 if (IsRailDepotTile(moving_front->tile)) {
2024 if (IsWholeTrainInsideDepot(consist)) return;
2026 }
2027
2028 /* Clear path reservation in front if train is not stuck. */
2030
2031 /* Check if we were approaching a rail/road-crossing */
2032 TileIndex crossing = TrainApproachingCrossingTile(moving_front);
2033
2034 /* Check if we should back up or flip the train. */
2035 if (consist->vehicle_flags.Test(VehicleFlag::DrivingBackwards) || _settings_game.difficulty.train_flip_reverse_allowed == TrainFlipReversingAllowed::None || consist->Last()->CanLeadTrain()) {
2036 /* The train will back up. */
2038
2039 for (Train *u = consist; u != nullptr; u = u->Next()) {
2040 /* Invert going up/down */
2044 }
2045 UpdateStatusAfterSwap(u, false);
2046 }
2047 /* We may have entered a depot and stopped driving backwards. */
2048 moving_front = consist->GetMovingFront();
2049 } else {
2050 /* The train will flip. */
2051 int r = CountVehiclesInChain(consist) - 1; // number of vehicles - 1
2052
2053 AdvanceWagonsBeforeSwap(moving_front);
2054
2055 /* swap start<>end, start+1<>end-1, ... */
2056 int l = 0;
2057 do {
2058 ReverseTrainSwapVeh(consist, l++, r--);
2059 } while (l <= r);
2060
2061 AdvanceWagonsAfterSwap(moving_front);
2062 }
2063
2064 if (IsRailDepotTile(moving_front->tile)) {
2066 }
2067
2070
2071 /* recalculate cached data */
2072 consist->ConsistChanged(CCF_TRACK);
2073
2074 /* update all images */
2075 for (Train *u = consist; u != nullptr; u = u->Next()) u->UpdateViewport(false, false);
2076
2077 /* update crossing we were approaching */
2078 if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);
2079
2080 /* maybe we are approaching crossing now, after reversal */
2081 crossing = TrainApproachingCrossingTile(moving_front);
2082 if (crossing != INVALID_TILE) MaybeBarCrossingWithSound(crossing);
2083
2084 /* If we are inside a depot after reversing, don't bother with path reserving. */
2085 if (moving_front->track == TRACK_BIT_DEPOT) {
2086 /* Can't be stuck here as inside a depot is always a safe tile. */
2089 return;
2090 }
2091
2092 /* VehicleExitDir does not always produce the desired dir for depots and
2093 * tunnels/bridges that is needed for UpdateSignalsOnSegment. */
2094 DiagDirection dir = VehicleExitDir(moving_front->GetMovingDirection(), moving_front->track);
2095 if (IsRailDepotTile(moving_front->tile) || IsTileType(moving_front->tile, TileType::TunnelBridge)) dir = INVALID_DIAGDIR;
2096
2097 if (UpdateSignalsOnSegment(moving_front->tile, dir, consist->owner) == SIGSEG_PBS || _settings_game.pf.reserve_paths) {
2098 /* If we are currently on a tile with conventional signals, we can't treat the
2099 * current tile as a safe tile or we would enter a PBS block without a reservation. */
2100 bool first_tile_okay = !HasBlockSignalOnTrackdir(moving_front->tile, moving_front->GetVehicleTrackdir());
2101
2102 /* If we are on a depot tile facing outwards, do not treat the current tile as safe. */
2103 if (IsRailDepotTile(moving_front->tile) && TrackdirToExitdir(moving_front->GetVehicleTrackdir()) == GetRailDepotDirection(moving_front->tile)) first_tile_okay = false;
2104
2105 if (IsRailStationTile(moving_front->tile)) SetRailStationPlatformReservation(moving_front->tile, TrackdirToExitdir(moving_front->GetVehicleTrackdir()), true);
2106 if (TryPathReserve(consist, false, first_tile_okay)) {
2107 /* Do a look-ahead now in case our current tile was already a safe tile. */
2108 CheckNextTrainTile(consist);
2109 } else if (consist->current_order.GetType() != OT_LOADING) {
2110 /* Do not wait for a way out when we're still loading */
2111 MarkTrainAsStuck(consist);
2112 }
2113 } else if (consist->flags.Test(VehicleRailFlag::Stuck)) {
2114 /* A train not inside a PBS block can't be stuck. */
2116 consist->wait_counter = 0;
2117 }
2118}
2119
2127CommandCost CmdReverseTrainDirection(DoCommandFlags flags, VehicleID veh_id, bool reverse_single_veh)
2128{
2129 Train *v = Train::GetIfValid(veh_id);
2130 if (v == nullptr) return CMD_ERROR;
2131
2133 if (ret.Failed()) return ret;
2134
2135 if (reverse_single_veh) {
2136 /* turn a single unit around */
2137
2138 if (v->IsMultiheaded() || EngInfo(v->engine_type)->callback_mask.Test(VehicleCallbackMask::ArticEngine)) {
2139 return CommandCost(STR_ERROR_CAN_T_REVERSE_DIRECTION_RAIL_VEHICLE_MULTIPLE_UNITS);
2140 }
2141
2142 Train *front = v->First();
2143 /* make sure the vehicle is stopped in the depot */
2144 if (!front->IsStoppedInDepot()) {
2145 return CommandCost(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
2146 }
2147
2148 if (flags.Test(DoCommandFlag::Execute)) {
2150
2153 SetWindowDirty(WC_VEHICLE_DETAILS, front->index);
2156 }
2157 } else {
2158 /* turn the whole train around */
2159 if (!v->IsPrimaryVehicle()) return CMD_ERROR;
2160 if (v->vehstatus.Test(VehState::Crashed) || v->breakdown_ctr != 0) return CMD_ERROR;
2161
2162 if (flags.Test(DoCommandFlag::Execute)) {
2163 /* Properly leave the station if we are loading and won't be loading anymore */
2164 if (v->current_order.IsType(OT_LOADING)) {
2165 const Vehicle *moving_back = v->GetMovingBack();
2166
2167 /* not a station || different station --> leave the station */
2168 if (!IsTileType(moving_back->tile, TileType::Station) || GetStationIndex(moving_back->tile) != GetStationIndex(v->GetMovingFront()->tile)) {
2169 v->LeaveStation();
2170 }
2171 }
2172
2173 /* We cancel any 'skip signal at dangers' here */
2176
2177 if (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL && v->cur_speed != 0) {
2179 } else {
2180 v->cur_speed = 0;
2181 v->SetLastSpeed();
2184 }
2185
2186 /* Unbunching data is no longer valid. */
2188 }
2189 }
2190 return CommandCost();
2191}
2192
2204{
2207
2208 const Train *moving_front = t->GetMovingFront();
2209 TileIndex next_tile = TileAddByDiagDir(moving_front->tile, TrackdirToExitdir(moving_front->GetVehicleTrackdir()));
2210 if (next_tile == INVALID_TILE || !IsTileType(next_tile, TileType::Railway) || !HasSignals(next_tile)) return TFP_STUCK;
2211 TrackBits new_tracks = DiagdirReachesTracks(TrackdirToExitdir(moving_front->GetVehicleTrackdir())) & GetTrackBits(next_tile);
2212 return new_tracks != TRACK_BIT_NONE && HasSignalOnTrack(next_tile, FindFirstTrack(new_tracks)) ? TFP_SIGNAL : TFP_STUCK;
2213}
2214
2221CommandCost CmdForceTrainProceed(DoCommandFlags flags, VehicleID veh_id)
2222{
2223 Train *t = Train::GetIfValid(veh_id);
2224 if (t == nullptr) return CMD_ERROR;
2225
2226 if (!t->IsPrimaryVehicle()) return CMD_ERROR;
2227
2229 if (ret.Failed()) return ret;
2230
2231
2232 if (flags.Test(DoCommandFlag::Execute)) {
2235
2236 /* Unbunching data is no longer valid. */
2238 }
2239
2240 return CommandCost();
2241}
2242
2250static FindDepotData FindClosestTrainDepot(Train *v, int max_distance)
2251{
2252 assert(!v->vehstatus.Test(VehState::Crashed));
2253
2254 return YapfTrainFindNearestDepot(v, max_distance);
2255}
2256
2258{
2259 FindDepotData tfdd = FindClosestTrainDepot(this, 0);
2260 if (tfdd.best_length == UINT_MAX) return ClosestDepot();
2261
2262 return ClosestDepot(tfdd.tile, GetDepotIndex(tfdd.tile), tfdd.reverse);
2263}
2264
2265void Train::PlayLeaveStationSound(bool force) const
2266{
2267 static const SoundFx sfx[] = {
2273 };
2274
2275 if (PlayVehicleSound(this, VSE_START, force)) return;
2276
2277 SndPlayVehicleFx(sfx[to_underlying(RailVehInfo(this->engine_type)->engclass)], this);
2278}
2279
2284static void CheckNextTrainTile(Train *consist)
2285{
2286 /* Don't do any look-ahead if path_backoff_interval is 255. */
2287 if (_settings_game.pf.path_backoff_interval == 255) return;
2288
2289 const Train *moving_front = consist->GetMovingFront();
2290
2291 /* Exit if we are inside a depot. */
2292 if (moving_front->track == TRACK_BIT_DEPOT) return;
2293
2294 switch (consist->current_order.GetType()) {
2295 /* Exit if we reached our destination depot. */
2296 case OT_GOTO_DEPOT:
2297 if (moving_front->tile == consist->dest_tile) return;
2298 break;
2299
2300 case OT_GOTO_WAYPOINT:
2301 /* If we reached our waypoint, make sure we see that. */
2302 if (IsRailWaypointTile(moving_front->tile) && GetStationIndex(moving_front->tile) == consist->current_order.GetDestination()) ProcessOrders(consist);
2303 break;
2304
2305 case OT_NOTHING:
2306 case OT_LEAVESTATION:
2307 case OT_LOADING:
2308 /* Exit if the current order doesn't have a destination, but the train has orders. */
2309 if (consist->GetNumOrders() > 0) return;
2310 break;
2311
2312 default:
2313 break;
2314 }
2315 /* Exit if we are on a station tile and are going to stop. */
2316 if (IsRailStationTile(moving_front->tile) && consist->current_order.ShouldStopAtStation(consist, GetStationIndex(moving_front->tile))) return;
2317
2318 Trackdir td = moving_front->GetVehicleTrackdir();
2319
2320 /* On a tile with a red non-pbs signal, don't look ahead. */
2321 if (HasBlockSignalOnTrackdir(moving_front->tile, td) && GetSignalStateByTrackdir(moving_front->tile, td) == SIGNAL_STATE_RED) return;
2322
2323 CFollowTrackRail ft(consist);
2324 if (!ft.Follow(moving_front->tile, td)) return;
2325
2327 /* Next tile is not reserved. */
2330 /* If the next tile is a PBS signal, try to make a reservation. */
2334 }
2335 ChooseTrainTrack(consist, ft.new_tile, ft.exitdir, tracks, false, nullptr, false);
2336 }
2337 }
2338 }
2339}
2340
2347{
2348 /* bail out if not all wagons are in the same depot or not in a depot at all */
2349 for (const Train *u = v; u != nullptr; u = u->Next()) {
2350 if (u->track != TRACK_BIT_DEPOT || u->tile != v->tile) return false;
2351 }
2352
2353 /* if the train got no power, then keep it in the depot */
2354 if (v->gcache.cached_power == 0) {
2357 return true;
2358 }
2359
2360 /* Check if we should wait here for unbunching. */
2361 if (v->IsWaitingForUnbunching()) return true;
2362
2363 SigSegState seg_state;
2364
2365 if (v->force_proceed == TFP_NONE) {
2366 /* force proceed was not pressed */
2367 if (++v->wait_counter < 37) {
2369 return true;
2370 }
2371
2372 v->wait_counter = 0;
2373
2374 seg_state = _settings_game.pf.reserve_paths ? SIGSEG_PBS : UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
2375 if (seg_state == SIGSEG_FULL || HasDepotReservation(v->tile)) {
2376 /* Full and no PBS signal in block or depot reserved, can't exit. */
2378 return true;
2379 }
2380 } else {
2381 seg_state = _settings_game.pf.reserve_paths ? SIGSEG_PBS : UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
2382 }
2383
2384 /* We are leaving a depot, but have to go to the exact same one; re-enter. */
2385 if (v->current_order.IsType(OT_GOTO_DEPOT) && v->tile == v->dest_tile) {
2386 /* Service when depot has no reservation. */
2388 return true;
2389 }
2390
2391 /* Only leave when we can reserve a path to our destination. */
2392 if (seg_state == SIGSEG_PBS && !TryPathReserve(v) && v->force_proceed == TFP_NONE) {
2393 /* No path and no force proceed. */
2396 return true;
2397 }
2398
2399 SetDepotReservation(v->tile, true);
2400 if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(v->tile);
2401
2406
2407 v->track = TRACK_BIT_X;
2408 if (v->direction & 2) v->track = TRACK_BIT_Y;
2409
2411 v->cur_speed = 0;
2412
2413 v->UpdateViewport(true, true);
2414 v->UpdatePosition();
2416 v->UpdateAcceleration();
2418
2419 return false;
2420}
2421
2428static void ClearPathReservation(const Train *v, TileIndex tile, Trackdir track_dir)
2429{
2430 DiagDirection dir = TrackdirToExitdir(track_dir);
2431
2433 /* Are we just leaving a tunnel/bridge? */
2434 if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(dir)) {
2436
2437 if (TunnelBridgeIsFree(tile, end, v).Succeeded()) {
2438 /* Free the reservation only if no other train is on the tiles. */
2439 SetTunnelBridgeReservation(tile, false);
2440 SetTunnelBridgeReservation(end, false);
2441
2442 if (_settings_client.gui.show_track_reservation) {
2443 if (IsBridge(tile)) {
2444 MarkBridgeDirty(tile);
2445 } else {
2446 MarkTileDirtyByTile(tile);
2448 }
2449 }
2450 }
2451 }
2452 } else if (IsRailStationTile(tile)) {
2453 TileIndex new_tile = TileAddByDiagDir(tile, dir);
2454 /* If the new tile is not a further tile of the same station, we
2455 * clear the reservation for the whole platform. */
2456 if (!IsCompatibleTrainStationTile(new_tile, tile)) {
2458 }
2459 } else {
2460 /* Any other tile */
2461 UnreserveRailTrack(tile, TrackdirToTrack(track_dir));
2462 }
2463}
2464
2470{
2471 assert(consist->IsFrontEngine());
2472
2473 const Train *moving_front = consist->GetMovingFront();
2474 TileIndex tile = moving_front->tile;
2475 Trackdir td = moving_front->GetVehicleTrackdir();
2476 bool free_tile = tile != moving_front->tile || !(IsRailStationTile(moving_front->tile) || IsTileType(moving_front->tile, TileType::TunnelBridge));
2477 StationID station_id = IsRailStationTile(moving_front->tile) ? GetStationIndex(moving_front->tile) : StationID::Invalid();
2478
2479 /* Can't be holding a reservation if we enter a depot. */
2480 if (IsRailDepotTile(tile) && TrackdirToExitdir(td) != GetRailDepotDirection(tile)) return;
2481 if (moving_front->track == TRACK_BIT_DEPOT) {
2482 /* Front engine is in a depot. We enter if some part is not in the depot. */
2483 for (const Train *u = consist; u != nullptr; u = u->Next()) {
2484 if (u->track != TRACK_BIT_DEPOT || u->tile != consist->tile) return;
2485 }
2486 }
2487 /* Don't free reservation if it's not ours. */
2489
2490 CFollowTrackRail ft(consist, GetAllCompatibleRailTypes(consist->railtypes));
2491 while (ft.Follow(tile, td)) {
2492 tile = ft.new_tile;
2494 td = RemoveFirstTrackdir(&bits);
2495 assert(bits == TRACKDIR_BIT_NONE);
2496
2497 if (!IsValidTrackdir(td)) break;
2498
2499 if (IsTileType(tile, TileType::Railway)) {
2500 if (HasSignalOnTrackdir(tile, td) && !IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)))) {
2501 /* Conventional signal along trackdir: remove reservation and stop. */
2503 break;
2504 }
2505 if (HasPbsSignalOnTrackdir(tile, td)) {
2506 if (GetSignalStateByTrackdir(tile, td) == SIGNAL_STATE_RED) {
2507 /* Red PBS signal? Can't be our reservation, would be green then. */
2508 break;
2509 } else {
2510 /* Turn the signal back to red. */
2512 MarkTileDirtyByTile(tile);
2513 }
2514 } else if (HasPbsSignalOnTrackdir(tile, ReverseTrackdir(td))) {
2515 /* Reservation passes an opposing path signal. Mark signal for update to re-establish the proper default state. */
2517 } else if (HasSignalOnTrackdir(tile, ReverseTrackdir(td)) && IsOnewaySignal(tile, TrackdirToTrack(td))) {
2518 break;
2519 }
2520 }
2521
2522 /* Don't free first station/bridge/tunnel if we are on it. */
2523 if (free_tile || (!(ft.is_station && GetStationIndex(ft.new_tile) == station_id) && !ft.is_tunnel && !ft.is_bridge)) ClearPathReservation(consist, tile, td);
2524
2525 free_tile = true;
2526 }
2527
2529}
2530
2531static const uint8_t _initial_tile_subcoord[6][4][3] = {
2532{{ 15, 8, 1 }, { 0, 0, 0 }, { 0, 8, 5 }, { 0, 0, 0 }},
2533{{ 0, 0, 0 }, { 8, 0, 3 }, { 0, 0, 0 }, { 8, 15, 7 }},
2534{{ 0, 0, 0 }, { 7, 0, 2 }, { 0, 7, 6 }, { 0, 0, 0 }},
2535{{ 15, 8, 2 }, { 0, 0, 0 }, { 0, 0, 0 }, { 8, 15, 6 }},
2536{{ 15, 7, 0 }, { 8, 0, 4 }, { 0, 0, 0 }, { 0, 0, 0 }},
2537{{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 8, 4 }, { 7, 15, 0 }},
2538};
2539
2553static Track DoTrainPathfind(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool do_track_reservation, PBSTileInfo *dest, TileIndex *final_dest)
2554{
2555 if (final_dest != nullptr) *final_dest = INVALID_TILE;
2556 return YapfTrainChooseTrack(v, tile, enterdir, tracks, path_found, do_track_reservation, dest, final_dest);
2557}
2558
2567static PBSTileInfo ExtendTrainReservation(const Train *v, TrackBits *new_tracks, DiagDirection *enterdir)
2568{
2570
2571 CFollowTrackRail ft(v);
2572
2573 std::vector<std::pair<TileIndex, Trackdir>> signals_set_to_red;
2574
2575 TileIndex tile = origin.tile;
2576 Trackdir cur_td = origin.trackdir;
2577 while (ft.Follow(tile, cur_td)) {
2579 /* Possible signal tile. */
2581 }
2582
2585 if (ft.new_td_bits == TRACKDIR_BIT_NONE) break;
2586 }
2587
2588 /* Station, depot or waypoint are a possible target. */
2589 bool target_seen = ft.is_station || (IsTileType(ft.new_tile, TileType::Railway) && !IsPlainRail(ft.new_tile));
2590 if (target_seen || KillFirstBit(ft.new_td_bits) != TRACKDIR_BIT_NONE) {
2591 /* Choice found or possible target encountered.
2592 * On finding a possible target, we need to stop and let the pathfinder handle the
2593 * remaining path. This is because we don't know if this target is in one of our
2594 * orders, so we might cause pathfinding to fail later on if we find a choice.
2595 * This failure would cause a bogus call to TryReserveSafePath which might reserve
2596 * a wrong path not leading to our next destination. */
2598
2599 /* If we did skip some tiles, backtrack to the first skipped tile so the pathfinder
2600 * actually starts its search at the first unreserved tile. */
2601 if (ft.tiles_skipped != 0) ft.new_tile -= TileOffsByDiagDir(ft.exitdir) * ft.tiles_skipped;
2602
2603 /* Choice found, path valid but not okay. Save info about the choice tile as well. */
2604 if (new_tracks != nullptr) *new_tracks = TrackdirBitsToTrackBits(ft.new_td_bits);
2605 if (enterdir != nullptr) *enterdir = ft.exitdir;
2606 return PBSTileInfo(ft.new_tile, ft.old_td, false);
2607 }
2608
2609 tile = ft.new_tile;
2610 cur_td = FindFirstTrackdir(ft.new_td_bits);
2611
2612 Trackdir rev_td = ReverseTrackdir(cur_td);
2613 if (IsSafeWaitingPosition(v, tile, cur_td, true, _settings_game.pf.forbid_90_deg)) {
2614 bool wp_free = IsWaitingPositionFree(v, tile, cur_td, _settings_game.pf.forbid_90_deg);
2615 if (!(wp_free && TryReserveRailTrack(tile, TrackdirToTrack(cur_td)))) break;
2616 /* Green path signal opposing the path? Turn to red. */
2617 if (HasPbsSignalOnTrackdir(tile, rev_td) && GetSignalStateByTrackdir(tile, rev_td) == SIGNAL_STATE_GREEN) {
2618 signals_set_to_red.emplace_back(tile, rev_td);
2620 MarkTileDirtyByTile(tile);
2621 }
2622 /* Safe position is all good, path valid and okay. */
2623 return PBSTileInfo(tile, cur_td, true);
2624 }
2625
2626 if (!TryReserveRailTrack(tile, TrackdirToTrack(cur_td))) break;
2627
2628 /* Green path signal opposing the path? Turn to red. */
2629 if (HasPbsSignalOnTrackdir(tile, rev_td) && GetSignalStateByTrackdir(tile, rev_td) == SIGNAL_STATE_GREEN) {
2630 signals_set_to_red.emplace_back(tile, rev_td);
2632 MarkTileDirtyByTile(tile);
2633 }
2634 }
2635
2636 if (ft.err == CFollowTrackRail::EC_OWNER || ft.err == CFollowTrackRail::EC_NO_WAY) {
2637 /* End of line, path valid and okay. */
2638 return PBSTileInfo(ft.old_tile, ft.old_td, true);
2639 }
2640
2641 /* Sorry, can't reserve path, back out. */
2642 tile = origin.tile;
2643 cur_td = origin.trackdir;
2644 TileIndex stopped = ft.old_tile;
2645 Trackdir stopped_td = ft.old_td;
2646 while (tile != stopped || cur_td != stopped_td) {
2647 if (!ft.Follow(tile, cur_td)) break;
2648
2651 assert(ft.new_td_bits != TRACKDIR_BIT_NONE);
2652 }
2654
2655 tile = ft.new_tile;
2656 cur_td = FindFirstTrackdir(ft.new_td_bits);
2657
2658 UnreserveRailTrack(tile, TrackdirToTrack(cur_td));
2659 }
2660
2661 /* Re-instate green signals we turned to red. */
2662 for (auto [sig_tile, td] : signals_set_to_red) {
2664 }
2665
2666 /* Path invalid. */
2667 return PBSTileInfo();
2668}
2669
2680static bool TryReserveSafeTrack(const Train *v, TileIndex tile, Trackdir td, bool override_railtype)
2681{
2682 return YapfTrainFindNearestSafeTile(v, tile, td, override_railtype);
2683}
2684
2686class VehicleOrderSaver {
2687private:
2688 Train *v;
2689 Order old_order;
2690 TileIndex old_dest_tile;
2691 StationID old_last_station_visited;
2692 VehicleOrderID index;
2693 bool suppress_implicit_orders;
2694 bool restored;
2695
2696public:
2697 VehicleOrderSaver(Train *_v) :
2698 v(_v),
2699 old_order(_v->current_order),
2700 old_dest_tile(_v->dest_tile),
2701 old_last_station_visited(_v->last_station_visited),
2702 index(_v->cur_real_order_index),
2703 suppress_implicit_orders(HasBit(_v->gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS)),
2704 restored(false)
2705 {
2706 }
2707
2711 void Restore()
2712 {
2713 this->v->current_order = this->old_order;
2714 this->v->dest_tile = this->old_dest_tile;
2715 this->v->last_station_visited = this->old_last_station_visited;
2716 AssignBit(this->v->gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS, suppress_implicit_orders);
2717 this->restored = true;
2718 }
2719
2724 {
2725 if (!this->restored) this->Restore();
2726 }
2727
2733 bool SwitchToNextOrder(bool skip_first)
2734 {
2735 if (this->v->GetNumOrders() == 0) return false;
2736
2737 if (skip_first) ++this->index;
2738
2739 int depth = 0;
2740
2741 do {
2742 /* Wrap around. */
2743 if (this->index >= this->v->GetNumOrders()) this->index = 0;
2744
2745 Order *order = this->v->GetOrder(this->index);
2746 assert(order != nullptr);
2747
2748 switch (order->GetType()) {
2749 case OT_GOTO_DEPOT:
2750 /* Skip service in depot orders when the train doesn't need service. */
2751 if (order->GetDepotOrderType().Test(OrderDepotTypeFlag::Service) && !this->v->NeedsServicing()) break;
2752 [[fallthrough]];
2753 case OT_GOTO_STATION:
2754 case OT_GOTO_WAYPOINT:
2755 this->v->current_order = *order;
2756 return UpdateOrderDest(this->v, order, 0, true);
2757 case OT_CONDITIONAL: {
2758 VehicleOrderID next = ProcessConditionalOrder(order, this->v);
2759 if (next != INVALID_VEH_ORDER_ID) {
2760 depth++;
2761 this->index = next;
2762 /* Don't increment next, so no break here. */
2763 continue;
2764 }
2765 break;
2766 }
2767 default:
2768 break;
2769 }
2770 /* Don't increment inside the while because otherwise conditional
2771 * orders can lead to an infinite loop. */
2772 ++this->index;
2773 depth++;
2774 } while (this->index != this->v->cur_real_order_index && depth < this->v->GetNumOrders());
2775
2776 return false;
2777 }
2778};
2779
2780/* choose a track */
2781static Track ChooseTrainTrack(Train *consist, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool force_res, bool *got_reservation, bool mark_stuck)
2782{
2783 Track best_track = INVALID_TRACK;
2784 bool do_track_reservation = _settings_game.pf.reserve_paths || force_res;
2785 bool changed_signal = false;
2786 TileIndex final_dest = INVALID_TILE;
2787
2788 assert((tracks & ~TRACK_BIT_MASK) == 0);
2789
2790 if (got_reservation != nullptr) *got_reservation = false;
2791
2792 /* Don't use tracks here as the setting to forbid 90 deg turns might have been switched between reservation and now. */
2793 TrackBits res_tracks = (TrackBits)(GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir));
2794 /* Do we have a suitable reserved track? */
2795 if (res_tracks != TRACK_BIT_NONE) return FindFirstTrack(res_tracks);
2796
2797 /* Quick return in case only one possible track is available */
2798 if (KillFirstBit(tracks) == TRACK_BIT_NONE) {
2799 Track track = FindFirstTrack(tracks);
2800 /* We need to check for signals only here, as a junction tile can't have signals. */
2801 if (track != INVALID_TRACK && HasPbsSignalOnTrackdir(tile, TrackEnterdirToTrackdir(track, enterdir))) {
2802 do_track_reservation = true;
2803 changed_signal = true;
2805 } else if (!do_track_reservation) {
2806 return track;
2807 }
2808 best_track = track;
2809 }
2810
2811 const Train *moving_front = consist->GetMovingFront();
2812
2813 PBSTileInfo res_dest(tile, INVALID_TRACKDIR, false);
2814 DiagDirection dest_enterdir = enterdir;
2815 if (do_track_reservation) {
2816 res_dest = ExtendTrainReservation(consist, &tracks, &dest_enterdir);
2817 if (res_dest.tile == INVALID_TILE) {
2818 /* Reservation failed? */
2819 if (mark_stuck) MarkTrainAsStuck(consist);
2820 if (changed_signal) SetSignalStateByTrackdir(tile, TrackEnterdirToTrackdir(best_track, enterdir), SIGNAL_STATE_RED);
2821 return FindFirstTrack(tracks);
2822 }
2823 if (res_dest.okay) {
2824 /* Got a valid reservation that ends at a safe target, quick exit. */
2825 if (got_reservation != nullptr) *got_reservation = true;
2826 if (changed_signal) MarkTileDirtyByTile(tile);
2827 TryReserveRailTrack(moving_front->tile, TrackdirToTrack(moving_front->GetVehicleTrackdir()));
2828 return best_track;
2829 }
2830
2831 /* Check if the train needs service here, so it has a chance to always find a depot.
2832 * Also check if the current order is a service order so we don't reserve a path to
2833 * the destination but instead to the next one if service isn't needed. */
2834 CheckIfTrainNeedsService(consist);
2835 if (consist->current_order.IsType(OT_DUMMY) || consist->current_order.IsType(OT_CONDITIONAL) || consist->current_order.IsType(OT_GOTO_DEPOT)) ProcessOrders(consist);
2836 }
2837
2838 /* Save the current train order. The destructor will restore the old order on function exit. */
2839 VehicleOrderSaver orders(consist);
2840
2841 /* If the current tile is the destination of the current order and
2842 * a reservation was requested, advance to the next order.
2843 * Don't advance on a depot order as depots are always safe end points
2844 * for a path and no look-ahead is necessary. This also avoids a
2845 * problem with depot orders not part of the order list when the
2846 * order list itself is empty. */
2847 if (consist->current_order.IsType(OT_LEAVESTATION)) {
2848 orders.SwitchToNextOrder(false);
2849 } else if (consist->current_order.IsType(OT_LOADING) || (!consist->current_order.IsType(OT_GOTO_DEPOT) && (
2850 consist->current_order.IsType(OT_GOTO_STATION) ?
2851 IsRailStationTile(moving_front->tile) && consist->current_order.GetDestination() == GetStationIndex(moving_front->tile) :
2852 moving_front->tile == consist->dest_tile))) {
2853 orders.SwitchToNextOrder(true);
2854 }
2855
2856 if (res_dest.tile != INVALID_TILE && !res_dest.okay) {
2857 /* Pathfinders are able to tell that route was only 'guessed'. */
2858 bool path_found = true;
2859 TileIndex new_tile = res_dest.tile;
2860
2861 Track next_track = DoTrainPathfind(consist, new_tile, dest_enterdir, tracks, path_found, do_track_reservation, &res_dest, &final_dest);
2862 if (new_tile == tile) best_track = next_track;
2863 consist->HandlePathfindingResult(path_found);
2864 }
2865
2866 /* No track reservation requested -> finished. */
2867 if (!do_track_reservation) return best_track;
2868
2869 /* A path was found, but could not be reserved. */
2870 if (res_dest.tile != INVALID_TILE && !res_dest.okay) {
2871 if (mark_stuck) MarkTrainAsStuck(consist);
2873 return best_track;
2874 }
2875
2876 /* No possible reservation target found, we are probably lost. */
2877 if (res_dest.tile == INVALID_TILE) {
2878 /* Try to find any safe destination. */
2879 PBSTileInfo origin = FollowTrainReservation(consist);
2880 if (TryReserveSafeTrack(consist, origin.tile, origin.trackdir, false)) {
2881 TrackBits res = GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir);
2882 best_track = FindFirstTrack(res);
2883 TryReserveRailTrack(moving_front->tile, TrackdirToTrack(moving_front->GetVehicleTrackdir()));
2884 if (got_reservation != nullptr) *got_reservation = true;
2885 if (changed_signal) MarkTileDirtyByTile(tile);
2886 } else {
2888 if (mark_stuck) MarkTrainAsStuck(consist);
2889 }
2890 return best_track;
2891 }
2892
2893 if (got_reservation != nullptr) *got_reservation = true;
2894
2895 /* Reservation target found and free, check if it is safe. */
2896 while (!IsSafeWaitingPosition(consist, res_dest.tile, res_dest.trackdir, true, _settings_game.pf.forbid_90_deg)) {
2897 /* Extend reservation until we have found a safe position. */
2898 DiagDirection exitdir = TrackdirToExitdir(res_dest.trackdir);
2899 TileIndex next_tile = TileAddByDiagDir(res_dest.tile, exitdir);
2901 if (Rail90DegTurnDisallowed(GetTileRailType(res_dest.tile), GetTileRailType(next_tile))) {
2902 reachable &= ~TrackCrossesTracks(TrackdirToTrack(res_dest.trackdir));
2903 }
2904
2905 /* Get next order with destination. */
2906 if (orders.SwitchToNextOrder(true)) {
2907 PBSTileInfo cur_dest;
2908 bool path_found;
2909 DoTrainPathfind(consist, next_tile, exitdir, reachable, path_found, true, &cur_dest, nullptr);
2910 if (cur_dest.tile != INVALID_TILE) {
2911 res_dest = cur_dest;
2912 if (res_dest.okay) continue;
2913 /* Path found, but could not be reserved. */
2915 if (mark_stuck) MarkTrainAsStuck(consist);
2916 if (got_reservation != nullptr) *got_reservation = false;
2917 changed_signal = false;
2918 break;
2919 }
2920 }
2921 /* No order or no safe position found, try any position. */
2922 if (!TryReserveSafeTrack(consist, res_dest.tile, res_dest.trackdir, true)) {
2924 if (mark_stuck) MarkTrainAsStuck(consist);
2925 if (got_reservation != nullptr) *got_reservation = false;
2926 changed_signal = false;
2927 }
2928 break;
2929 }
2930
2931 TryReserveRailTrack(moving_front->tile, TrackdirToTrack(moving_front->GetVehicleTrackdir()));
2932
2933 if (changed_signal) MarkTileDirtyByTile(tile);
2934
2935 orders.Restore();
2936 if (consist->current_order.IsType(OT_GOTO_DEPOT) &&
2938 final_dest != INVALID_TILE && IsRailDepotTile(final_dest)) {
2939 consist->current_order.SetDestination(GetDepotIndex(final_dest));
2940 consist->dest_tile = final_dest;
2942 }
2943
2944 return best_track;
2945}
2946
2955bool TryPathReserve(Train *consist, bool mark_as_stuck, bool first_tile_okay)
2956{
2957 assert(consist->IsFrontEngine());
2958
2959 const Train *moving_front = consist->GetMovingFront();
2960
2961 /* We have to handle depots specially as the track follower won't look
2962 * at the depot tile itself but starts from the next tile. If we are still
2963 * inside the depot, a depot reservation can never be ours. */
2964 if (moving_front->track == TRACK_BIT_DEPOT) {
2965 if (HasDepotReservation(moving_front->tile)) {
2966 if (mark_as_stuck) MarkTrainAsStuck(consist);
2967 return false;
2968 } else {
2969 /* Depot not reserved, but the next tile might be. */
2970 TileIndex next_tile = TileAddByDiagDir(moving_front->tile, GetRailDepotDirection(moving_front->tile));
2971 if (HasReservedTracks(next_tile, DiagdirReachesTracks(GetRailDepotDirection(moving_front->tile)))) return false;
2972 }
2973 }
2974
2975 Vehicle *other_train = nullptr;
2976 PBSTileInfo origin = FollowTrainReservation(consist, &other_train);
2977 /* The path we are driving on is already blocked by some other train.
2978 * This can only happen in certain situations when mixing path and
2979 * block signals or when changing tracks and/or signals.
2980 * Exit here as doing any further reservations will probably just
2981 * make matters worse. */
2982 if (other_train != nullptr && other_train->index != consist->index) {
2983 if (mark_as_stuck) MarkTrainAsStuck(consist);
2984 return false;
2985 }
2986 /* If we have a reserved path and the path ends at a safe tile, we are finished already. */
2987 if (origin.okay && (moving_front->tile != origin.tile || first_tile_okay)) {
2988 /* Can't be stuck then. */
2991 return true;
2992 }
2993
2994 /* If we are in a depot, tentatively reserve the depot. */
2995 if (moving_front->track == TRACK_BIT_DEPOT) {
2996 SetDepotReservation(moving_front->tile, true);
2997 if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(moving_front->tile);
2998 }
2999
3000 DiagDirection exitdir = TrackdirToExitdir(origin.trackdir);
3001 TileIndex new_tile = TileAddByDiagDir(origin.tile, exitdir);
3003
3005
3006 bool res_made = false;
3007 ChooseTrainTrack(consist, new_tile, exitdir, reachable, true, &res_made, mark_as_stuck);
3008
3009 if (!res_made) {
3010 /* Free the depot reservation as well. */
3011 if (moving_front->track == TRACK_BIT_DEPOT) SetDepotReservation(moving_front->tile, false);
3012 return false;
3013 }
3014
3015 if (consist->flags.Test(VehicleRailFlag::Stuck)) {
3016 consist->wait_counter = 0;
3018 }
3020 return true;
3021}
3022
3028static bool CheckReverseTrain(const Train *consist)
3029{
3030 const Train *moving_front = consist->GetMovingFront();
3031 if (_settings_game.difficulty.train_flip_reverse_allowed == TrainFlipReversingAllowed::EndOfLineOnly ||
3032 moving_front->track == TRACK_BIT_DEPOT || moving_front->track == TRACK_BIT_WORMHOLE ||
3033 !(moving_front->GetMovingDirection() & 1)) {
3034 return false;
3035 }
3036
3037 assert(moving_front->track != TRACK_BIT_NONE);
3038
3039 return YapfTrainCheckReverse(consist);
3040}
3041
3048{
3049 if (station == this->last_station_visited) this->last_station_visited = StationID::Invalid();
3050
3051 const Station *st = Station::Get(station);
3053 /* The destination station has no trainstation tiles. */
3055 return TileIndex{};
3056 }
3057
3058 return st->xy;
3059}
3060
3063{
3064 Train *v = this;
3065 do {
3066 v->colourmap = PAL_NONE;
3067 v->UpdateViewport(true, false);
3068 } while ((v = v->Next()) != nullptr);
3069
3070 /* need to update acceleration and cached values since the goods on the train changed. */
3071 this->CargoChanged();
3072 this->UpdateAcceleration();
3073}
3074
3083{
3084 switch (_settings_game.vehicle.train_acceleration_model) {
3085 default: NOT_REACHED();
3086 case AM_ORIGINAL:
3087 return this->DoUpdateSpeed(this->acceleration * (this->GetAccelerationStatus() == AS_BRAKE ? -4 : 2), 0, this->GetCurrentMaxSpeed());
3088
3089 case AM_REALISTIC:
3090 return this->DoUpdateSpeed(this->GetAcceleration(), this->GetAccelerationStatus() == AS_BRAKE ? 0 : 2, this->GetCurrentMaxSpeed());
3091 }
3092}
3093
3099static void TrainEnterStation(Train *consist, StationID station)
3100{
3101 consist->last_station_visited = station;
3102
3103 /* check if a train ever visited this station before */
3104 Station *st = Station::Get(station);
3105 if (!(st->had_vehicle_of_type & HVOT_TRAIN)) {
3106 st->had_vehicle_of_type |= HVOT_TRAIN;
3108 GetEncodedString(STR_NEWS_FIRST_TRAIN_ARRIVAL, st->index),
3110 consist->index,
3111 st->index
3112 );
3113 AI::NewEvent(consist->owner, new ScriptEventStationFirstVehicle(st->index, consist->index));
3114 Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, consist->index));
3115 }
3116
3117 consist->force_proceed = TFP_NONE;
3118 InvalidateWindowData(WC_VEHICLE_VIEW, consist->index);
3119
3120 consist->BeginLoading();
3121
3122 TileIndex tile = consist->GetMovingFront()->tile;
3124 TriggerStationAnimation(st, tile, StationAnimationTrigger::VehicleArrives);
3125}
3126
3134static inline bool CheckCompatibleRail(const Train *v, TileIndex tile, bool check_railtype)
3135{
3136 return IsTileOwner(tile, v->owner) &&
3137 (!check_railtype || !v->IsFrontEngine() || v->compatible_railtypes.Test(GetRailType(tile)));
3138}
3139
3142 uint8_t small_turn;
3143 uint8_t large_turn;
3144 uint8_t z_up;
3145 uint8_t z_down;
3146};
3147
3150 /* normal accel */
3151 {256 / 4, 256 / 2, 256 / 4, 2},
3152 {256 / 4, 256 / 2, 256 / 4, 2},
3153 {0, 256 / 2, 256 / 4, 2},
3154};
3155
3161static inline void AffectSpeedByZChange(Train *consist, int z_diff)
3162{
3163 if (z_diff == 0 || _settings_game.vehicle.train_acceleration_model != AM_ORIGINAL) return;
3164
3165 const AccelerationSlowdownParams *asp = &_accel_slowdown[static_cast<int>(consist->GetAccelerationType())];
3166
3167 if (z_diff > 0) {
3168 consist->cur_speed -= (consist->cur_speed * asp->z_up >> 8);
3169 } else {
3170 uint16_t spd = consist->cur_speed + asp->z_down;
3171 if (spd <= consist->gcache.cached_max_track_speed) consist->cur_speed = spd;
3172 }
3173}
3174
3175static bool TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
3176{
3177 if (IsTileType(tile, TileType::Railway) &&
3180 Trackdir trackdir = FindFirstTrackdir(tracks);
3181 if (UpdateSignalsOnSegment(tile, TrackdirToExitdir(trackdir), GetTileOwner(tile)) == SIGSEG_PBS && HasSignalOnTrackdir(tile, trackdir)) {
3182 /* A PBS block with a non-PBS signal facing us? */
3183 if (!IsPbsSignal(GetSignalType(tile, TrackdirToTrack(trackdir)))) return true;
3184 }
3185 }
3186 return false;
3187}
3188
3191{
3192 for (const Train *u = this; u != nullptr; u = u->Next()) {
3193 switch (u->track) {
3194 case TRACK_BIT_WORMHOLE:
3196 break;
3197 case TRACK_BIT_DEPOT:
3198 break;
3199 default:
3201 break;
3202 }
3203 }
3204}
3205
3212uint Train::Crash(bool flooded)
3213{
3214 uint victims = 0;
3215 if (this->IsFrontEngine()) {
3216 victims += 2; // driver
3217
3218 /* Remove the reserved path in front of the train if it is not stuck.
3219 * Also clear all reserved tracks the train is currently on. */
3221 for (const Train *v = this; v != nullptr; v = v->Next()) {
3224 /* ClearPathReservation will not free the wormhole exit
3225 * if the train has just entered the wormhole. */
3227 }
3228 }
3229
3230 /* we may need to update crossing we were approaching,
3231 * but must be updated after the train has been marked crashed */
3233 if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);
3234
3235 /* Remove the loading indicators (if any) */
3237 }
3238
3239 victims += this->GroundVehicleBase::Crash(flooded);
3240
3241 this->crash_anim_pos = flooded ? 4000 : 1; // max 4440, disappear pretty fast when flooded
3242 return victims;
3243}
3244
3251static uint TrainCrashed(Train *v)
3252{
3253 uint victims = 0;
3254
3255 /* do not crash train twice */
3256 if (!v->vehstatus.Test(VehState::Crashed)) {
3257 victims = v->Crash();
3258 TileIndex tile = v->GetMovingFront()->tile;
3259 AI::NewEvent(v->owner, new ScriptEventVehicleCrashed(v->index, tile, ScriptEventVehicleCrashed::CRASH_TRAIN, victims, v->owner));
3260 Game::NewEvent(new ScriptEventVehicleCrashed(v->index, tile, ScriptEventVehicleCrashed::CRASH_TRAIN, victims, v->owner));
3261 }
3262
3263 /* Try to re-reserve track under already crashed train too.
3264 * Crash() clears the reservation! */
3266
3267 return victims;
3268}
3269
3276static uint CheckTrainCollision(Vehicle *v, Train *moving_front)
3277{
3278 /* Make sure we are a train, and are not in a depot. */
3279 if (v->type != VehicleType::Train) return 0;
3280
3281 /* We can't crash into trains in a depot. */
3282 if (Train::From(v)->track == TRACK_BIT_DEPOT) return 0;
3283
3284 /* Do not crash into trains of another company. */
3285 if (v->owner != moving_front->First()->owner) return 0;
3286
3287 /* Do not collide with our own wagons */
3288 if (v->First() == moving_front->First()) return 0;
3289
3290 int x_diff = v->x_pos - moving_front->x_pos;
3291 int y_diff = v->y_pos - moving_front->y_pos;
3292
3293 /* Do fast calculation to check whether trains are not in close vicinity
3294 * and quickly reject trains distant enough for any collision.
3295 * Differences are shifted by 7, mapping range [-7 .. 8] into [0 .. 15]
3296 * Differences are then ORed and then we check for any higher bits */
3297 uint hash = (y_diff + 7) | (x_diff + 7);
3298 if (hash & ~15) return 0;
3299
3300 /* Slower check using multiplication */
3301 int min_diff = (Train::From(v)->gcache.cached_veh_length + 1) / 2 + (moving_front->gcache.cached_veh_length + 1) / 2 - 1;
3302 if (x_diff * x_diff + y_diff * y_diff > min_diff * min_diff) return 0;
3303
3304 /* Happens when there is a train under bridge next to bridge head */
3305 if (abs(v->z_pos - moving_front->z_pos) > 5) return 0;
3306
3307 /* Crash both trains. Two statements required to guarantee execution
3308 * order because RandomRange() is involved. */
3309 uint num_victims = TrainCrashed(moving_front->First());
3310 return num_victims + TrainCrashed(Train::From(v)->First());
3311}
3312
3321static bool CheckTrainCollision(Train *moving_front)
3322{
3323 /* can't collide in depot */
3324 if (moving_front->track == TRACK_BIT_DEPOT) return false;
3325
3326 assert(moving_front->track == TRACK_BIT_WORMHOLE || TileVirtXY(moving_front->x_pos, moving_front->y_pos) == moving_front->tile);
3327
3328 uint num_victims = 0;
3329
3330 /* find colliding vehicles */
3331 if (moving_front->track == TRACK_BIT_WORMHOLE) {
3332 for (Vehicle *u : VehiclesOnTile(moving_front->tile)) {
3333 num_victims += CheckTrainCollision(u, moving_front);
3334 }
3335 for (Vehicle *u : VehiclesOnTile(GetOtherTunnelBridgeEnd(moving_front->tile))) {
3336 num_victims += CheckTrainCollision(u, moving_front);
3337 }
3338 } else {
3339 for (Vehicle *u : VehiclesNearTileXY(moving_front->x_pos, moving_front->y_pos, 7)) {
3340 num_victims += CheckTrainCollision(u, moving_front);
3341 }
3342 }
3343
3344 /* any dead -> no crash */
3345 if (num_victims == 0) return false;
3346
3347 AddTileNewsItem(GetEncodedString(STR_NEWS_TRAIN_CRASH, num_victims), NewsType::Accident, moving_front->tile);
3348
3349 ModifyStationRatingAround(moving_front->tile, moving_front->First()->owner, -160, 30);
3350 if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_13_TRAIN_COLLISION, moving_front);
3351 return true;
3352}
3353
3361bool TrainController(Train *v, Vehicle *nomove, bool reverse)
3362{
3363 Train *first = v->First();
3364 Train *prev;
3365 bool direction_changed = false; // has direction of any part changed?
3366
3367 /* For every vehicle after and including the given vehicle */
3368 for (prev = v->GetMovingPrev(); v != nomove; prev = v, v = v->GetMovingNext()) {
3369 DiagDirection enterdir = DIAGDIR_BEGIN;
3370 bool update_signals_crossing = false; // will we update signals or crossing state?
3371
3373 if (v->track != TRACK_BIT_WORMHOLE) {
3374 /* Not inside tunnel */
3375 if (gp.old_tile == gp.new_tile) {
3376 /* Staying in the old tile */
3377 if (v->track == TRACK_BIT_DEPOT) {
3378 /* Inside depot */
3379 gp.x = v->x_pos;
3380 gp.y = v->y_pos;
3381 } else {
3382 /* Not inside depot */
3383
3384 /* Reverse when we are at the end of the track already, do not move to the new position */
3385 if (v->IsMovingFront() && !TrainCheckIfLineEnds(v, reverse)) return false;
3386
3387 auto vets = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
3388 if (vets.Test(VehicleEnterTileState::CannotEnter)) {
3389 goto invalid_rail;
3390 }
3392 /* The new position is the end of the platform */
3394 }
3395 }
3396 } else {
3397 /* A new tile is about to be entered. */
3398
3399 /* Determine what direction we're entering the new tile from */
3400 enterdir = DiagdirBetweenTiles(gp.old_tile, gp.new_tile);
3401 assert(IsValidDiagDirection(enterdir));
3402
3403 /* Get the status of the tracks in the new tile and mask
3404 * away the bits that aren't reachable. */
3406 TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(enterdir);
3407
3408 TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs;
3409 TrackBits red_signals = TrackdirBitsToTrackBits(TrackStatusToRedSignals(ts) & reachable_trackdirs);
3410
3411 TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
3412 if (Rail90DegTurnDisallowed(GetTileRailType(gp.old_tile), GetTileRailType(gp.new_tile)) && prev == nullptr) {
3413 /* We allow wagons to make 90 deg turns, because forbid_90_deg
3414 * can be switched on halfway a turn */
3416 }
3417
3418 if (bits == TRACK_BIT_NONE) goto invalid_rail;
3419
3420 /* Check if the new tile constrains tracks that are compatible
3421 * with the current train, if not, bail out. */
3422 if (!CheckCompatibleRail(v->First(), gp.new_tile, v->IsMovingFront())) goto invalid_rail;
3423
3424 TrackBits chosen_track;
3425 if (v->IsMovingFront()) {
3426 /* Currently the locomotive is active. Determine which one of the
3427 * available tracks to choose */
3428 chosen_track = TrackToTrackBits(ChooseTrainTrack(first, gp.new_tile, enterdir, bits, false, nullptr, true));
3429 assert(chosen_track & (bits | GetReservedTrackbits(gp.new_tile)));
3430
3431 if (first->force_proceed != TFP_NONE && IsPlainRailTile(gp.new_tile) && HasSignals(gp.new_tile)) {
3432 /* For each signal we find decrease the counter by one.
3433 * We start at two, so the first signal we pass decreases
3434 * this to one, then if we reach the next signal it is
3435 * decreased to zero and we won't pass that new signal. */
3436 Trackdir dir = FindFirstTrackdir(trackdirbits);
3437 if (HasSignalOnTrackdir(gp.new_tile, dir) ||
3440 /* However, we do not want to be stopped by PBS signals
3441 * entered via the back. */
3442 first->force_proceed = (first->force_proceed == TFP_SIGNAL) ? TFP_STUCK : TFP_NONE;
3444 }
3445 }
3446
3447 /* Check if it's a red signal and that force proceed is not clicked. */
3448 if ((red_signals & chosen_track) && first->force_proceed == TFP_NONE) {
3449 /* In front of a red signal */
3450 Trackdir i = FindFirstTrackdir(trackdirbits);
3451
3452 /* Don't handle stuck trains here. */
3453 if (first->flags.Test(VehicleRailFlag::Stuck)) return false;
3454
3456 first->cur_speed = 0;
3457 first->subspeed = 0;
3458 first->progress = 255; // make sure that every bit of acceleration will hit the signal again, so speed stays 0.
3459 if (!_settings_game.pf.reverse_at_signals || ++first->wait_counter < _settings_game.pf.wait_oneway_signal * Ticks::DAY_TICKS * 2) return false;
3460 } else if (HasSignalOnTrackdir(gp.new_tile, i)) {
3461 first->cur_speed = 0;
3462 first->subspeed = 0;
3463 first->progress = 255; // make sure that every bit of acceleration will hit the signal again, so speed stays 0.
3464 if (!_settings_game.pf.reverse_at_signals || ++first->wait_counter < _settings_game.pf.wait_twoway_signal * Ticks::DAY_TICKS * 2) {
3465 DiagDirection exitdir = TrackdirToExitdir(i);
3466 TileIndex o_tile = TileAddByDiagDir(gp.new_tile, exitdir);
3467
3468 exitdir = ReverseDiagDir(exitdir);
3469
3470 /* check if a train is waiting on the other side */
3471 if (!HasVehicleOnTile(o_tile, [&exitdir](const Vehicle *u) {
3472 if (u->type != VehicleType::Train || u->vehstatus.Test(VehState::Crashed)) return false;
3473 const Train *t = Train::From(u);
3474
3475 /* not front engine of a train, inside wormhole or depot, crashed */
3476 if (!t->IsFrontEngine() || !(t->track & TRACK_BIT_MASK)) return false;
3477
3478 if (t->cur_speed > 5 || VehicleExitDir(t->direction, t->track) != exitdir) return false;
3479
3480 return true;
3481 })) return false;
3482 }
3483 }
3484
3485 /* If we would reverse but are currently in a PBS block and
3486 * reversing of stuck trains is disabled, don't reverse.
3487 * This does not apply if the reason for reversing is a one-way
3488 * signal blocking us, because a train would then be stuck forever. */
3489 if (!_settings_game.pf.reverse_at_signals && !HasOnewaySignalBlockingTrackdir(gp.new_tile, i) &&
3490 UpdateSignalsOnSegment(v->tile, enterdir, v->owner) == SIGSEG_PBS) {
3491 first->wait_counter = 0;
3492 return false;
3493 }
3494 goto reverse_train_direction;
3495 } else {
3496 TryReserveRailTrack(gp.new_tile, TrackBitsToTrack(chosen_track), false);
3497 }
3498 } else {
3499 /* The wagon is active, simply follow the prev vehicle. */
3500 if (prev->tile == gp.new_tile) {
3501 /* Choose the same track as prev */
3502 if (prev->track == TRACK_BIT_WORMHOLE) {
3503 /* Vehicles entering tunnels enter the wormhole earlier than for bridges.
3504 * However, just choose the track into the wormhole. */
3505 assert(IsTunnel(prev->tile));
3506 chosen_track = bits;
3507 } else {
3508 chosen_track = prev->track;
3509 }
3510 } else {
3511 /* Choose the track that leads to the tile where prev is.
3512 * This case is active if 'prev' is already on the second next tile, when 'v' just enters the next tile.
3513 * I.e. when the tile between them has only space for a single vehicle like
3514 * 1) horizontal/vertical track tiles and
3515 * 2) some orientations of tunnel entries, where the vehicle is already inside the wormhole at 8/16 from the tile edge.
3516 * Is also the train just reversing, the wagon inside the tunnel is 'on' the tile of the opposite tunnel entry.
3517 */
3518 static const TrackBits _connecting_track[DIAGDIR_END][DIAGDIR_END] = {
3523 };
3524 DiagDirection exitdir = DiagdirBetweenTiles(gp.new_tile, prev->tile);
3525 assert(IsValidDiagDirection(exitdir));
3526 chosen_track = _connecting_track[enterdir][exitdir];
3527 }
3528 chosen_track &= bits;
3529 }
3530
3531 /* Make sure chosen track is a valid track */
3532 assert(
3533 chosen_track == TRACK_BIT_X || chosen_track == TRACK_BIT_Y ||
3534 chosen_track == TRACK_BIT_UPPER || chosen_track == TRACK_BIT_LOWER ||
3535 chosen_track == TRACK_BIT_LEFT || chosen_track == TRACK_BIT_RIGHT);
3536
3537 /* Update XY to reflect the entrance to the new tile, and select the direction to use */
3538 const uint8_t *b = _initial_tile_subcoord[FindFirstBit(chosen_track)][enterdir];
3539 gp.x = (gp.x & ~0xF) | b[0];
3540 gp.y = (gp.y & ~0xF) | b[1];
3541 Direction chosen_dir = (Direction)b[2];
3542
3543 /* Call the landscape function and tell it that the vehicle entered the tile */
3544 auto vets = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
3545 if (vets.Test(VehicleEnterTileState::CannotEnter)) {
3546 goto invalid_rail;
3547 }
3548
3550 Track track = FindFirstTrack(chosen_track);
3551 Trackdir tdir = TrackDirectionToTrackdir(track, chosen_dir);
3552 if (v->IsMovingFront() && HasPbsSignalOnTrackdir(gp.new_tile, tdir)) {
3555 }
3556
3557 /* Clear any track reservation when the last vehicle leaves the tile */
3558 if (v->GetMovingNext() == nullptr) ClearPathReservation(v, v->tile, v->GetVehicleTrackdir());
3559
3560 v->tile = gp.new_tile;
3561
3563 first->ConsistChanged(CCF_TRACK);
3564 }
3565
3566 v->track = chosen_track;
3567 assert(v->track);
3568 }
3569
3570 /* We need to update signal status, but after the vehicle position hash
3571 * has been updated by UpdateInclination() */
3572 update_signals_crossing = true;
3573
3574 if (chosen_dir != v->GetMovingDirection()) {
3575 if (prev == nullptr && _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) {
3576 const AccelerationSlowdownParams *asp = &_accel_slowdown[static_cast<int>(v->GetAccelerationType())];
3577 DirDiff diff = DirDifference(v->direction, chosen_dir);
3578 v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? asp->small_turn : asp->large_turn) * v->cur_speed >> 8;
3579 }
3580 direction_changed = true;
3581 v->SetMovingDirection(chosen_dir);
3582 }
3583
3584 if (v->IsMovingFront()) {
3585 first->wait_counter = 0;
3586
3587 /* If we are approaching a crossing that is reserved, play the sound now. */
3588 TileIndex crossing = TrainApproachingCrossingTile(v); // We know we are the moving front, so we can check v.
3589 if (crossing != INVALID_TILE && HasCrossingReservation(crossing) && _settings_client.sound.ambient) SndPlayTileFx(SND_0E_LEVEL_CROSSING, crossing);
3590
3591 /* Always try to extend the reservation when entering a tile. */
3592 CheckNextTrainTile(first);
3593 }
3594
3596 /* The new position is the location where we want to stop */
3598 }
3599 }
3600 } else {
3602 /* Perform look-ahead on tunnel exit. */
3603 if (v->IsMovingFront()) {
3605 CheckNextTrainTile(first);
3606 }
3607 /* Prevent v->UpdateInclination() being called with wrong parameters.
3608 * This could happen if the train was reversed inside the tunnel/bridge. */
3609 if (gp.old_tile == gp.new_tile) {
3611 }
3612 } else {
3613 v->x_pos = gp.x;
3614 v->y_pos = gp.y;
3615 v->UpdatePosition();
3616 if (!v->vehstatus.Test(VehState::Hidden)) v->Vehicle::UpdateViewport(true);
3617 continue;
3618 }
3619 }
3620
3621 /* update image of train, as well as delta XY */
3622 v->UpdateDeltaXY();
3623
3624 v->x_pos = gp.x;
3625 v->y_pos = gp.y;
3626 v->UpdatePosition();
3627
3628 /* update the Z position of the vehicle */
3629 int old_z = v->UpdateInclination(gp.new_tile != gp.old_tile, false);
3630
3631 if (prev == nullptr) {
3632 /* This is the first vehicle in the train */
3633 AffectSpeedByZChange(first, v->z_pos - old_z);
3634 }
3635
3636 if (update_signals_crossing) {
3637 if (v->IsMovingFront()) {
3638 if (TrainMovedChangeSignals(gp.new_tile, enterdir)) {
3639 /* We are entering a block with PBS signals right now, but
3640 * not through a PBS signal. This means we don't have a
3641 * reservation right now. As a conventional signal will only
3642 * ever be green if no other train is in the block, getting
3643 * a path should always be possible. If the player built
3644 * such a strange network that it is not possible, the train
3645 * will be marked as stuck and the player has to deal with
3646 * the problem. */
3647 if ((!HasReservedTracks(gp.new_tile, v->track) &&
3649 !TryPathReserve(first)) {
3650 MarkTrainAsStuck(first);
3651 }
3652 }
3653 }
3654
3655 /* Signals can only change when the first
3656 * (above) or the last vehicle moves. */
3657 if (v->GetMovingNext() == nullptr) {
3658 TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir));
3660 }
3661 }
3662
3663 /* Do not check on every tick to save some computing time. */
3664 if (v->IsMovingFront() && first->tick_counter % _settings_game.pf.path_backoff_interval == 0) CheckNextTrainTile(first);
3665 }
3666
3667 if (direction_changed) first->tcache.cached_max_curve_speed = first->GetCurveSpeedLimit();
3668
3669 return true;
3670
3671invalid_rail:
3672 /* We've reached end of line?? */
3673 if (prev != nullptr) FatalError("Disconnecting train");
3674
3675reverse_train_direction:
3676 if (reverse) {
3677 first->wait_counter = 0;
3678 first->cur_speed = 0;
3679 first->subspeed = 0;
3680 ReverseTrainDirection(first);
3681 }
3682
3683 return false;
3684}
3685
3686static bool IsRailStationPlatformOccupied(TileIndex tile)
3687{
3689
3690 for (TileIndex t = tile; IsCompatibleTrainStationTile(t, tile); t -= delta) {
3691 if (HasVehicleOnTile(t, IsTrain)) return true;
3692 }
3693 for (TileIndex t = tile + delta; IsCompatibleTrainStationTile(t, tile); t += delta) {
3694 if (HasVehicleOnTile(t, IsTrain)) return true;
3695 }
3696
3697 return false;
3698}
3699
3707static void DeleteLastWagon(Train *v)
3708{
3709 Train *first = v->First();
3710
3711 /* Go to the last wagon and delete the link pointing there
3712 * new_last is then the one-before-last wagon, and v the last
3713 * one which will physically be removed */
3714 Train *new_last = v;
3715 for (; v->Next() != nullptr; v = v->Next()) new_last = v;
3716 new_last->SetNext(nullptr);
3717
3718 if (first != v) {
3719 /* Recalculate cached train properties */
3721 /* Update the depot window in case a part of the consist is in a depot. */
3724 }
3725
3726 /* 'v' shouldn't be accessed after it has been deleted */
3727 TrackBits trackbits = v->track;
3728 TileIndex tile = v->tile;
3729 Owner owner = v->owner;
3730
3731 delete v;
3732 v = nullptr; // make sure nobody will try to read 'v' anymore
3733
3734 if (trackbits == TRACK_BIT_WORMHOLE) {
3735 /* Vehicle is inside a wormhole, v->track contains no useful value then. */
3737 }
3738
3739 Track track = TrackBitsToTrack(trackbits);
3740 if (HasReservedTracks(tile, trackbits)) {
3741 UnreserveRailTrack(tile, track);
3742
3743 /* If there are still crashed vehicles on the tile, give the track reservation to them */
3744 TrackBits remaining_trackbits = TRACK_BIT_NONE;
3745 for (const Vehicle *u : VehiclesOnTile(tile)) {
3746 if (u->type != VehicleType::Train || !u->vehstatus.Test(VehState::Crashed)) continue;
3747 TrackBits train_tbits = Train::From(u)->track;
3748 if (train_tbits == TRACK_BIT_WORMHOLE) {
3749 /* Vehicle is inside a wormhole, u->track contains no useful value then. */
3750 remaining_trackbits |= DiagDirToDiagTrackBits(GetTunnelBridgeDirection(u->tile));
3751 } else if (train_tbits != TRACK_BIT_DEPOT) {
3752 remaining_trackbits |= train_tbits;
3753 }
3754 }
3755
3756 /* It is important that these two are the first in the loop, as reservation cannot deal with every trackbit combination */
3757 assert(TRACK_BEGIN == TRACK_X && TRACK_Y == TRACK_BEGIN + 1);
3758 for (Track t : SetTrackBitIterator(remaining_trackbits)) TryReserveRailTrack(tile, t);
3759 }
3760
3761 /* check if the wagon was on a road/rail-crossing */
3763
3764 if (IsRailStationTile(tile)) {
3765 bool occupied = IsRailStationPlatformOccupied(tile);
3767 SetRailStationPlatformReservation(tile, dir, occupied);
3769 }
3770
3771 /* Update signals */
3774 } else {
3775 SetSignalsOnBothDir(tile, track, owner);
3776 }
3777}
3778
3784{
3785 static const DirDiff delta[] = {
3787 };
3788
3789 do {
3790 /* We don't need to twist around vehicles if they're not visible */
3791 if (!v->vehstatus.Test(VehState::Hidden)) {
3792 v->direction = ChangeDir(v->direction, delta[GB(Random(), 0, 2)]);
3793 /* Refrain from updating the z position of the vehicle when on
3794 * a bridge, because UpdateInclination() will put the vehicle under
3795 * the bridge in that case */
3796 if (v->track != TRACK_BIT_WORMHOLE) {
3797 v->UpdatePosition();
3798 v->UpdateInclination(false, true);
3799 } else {
3800 v->UpdateViewport(false, true);
3801 }
3802 }
3803 } while ((v = v->Next()) != nullptr);
3804}
3805
3812{
3813 int state = ++v->crash_anim_pos;
3814
3815 if (state == 4 && !v->vehstatus.Test(VehState::Hidden)) {
3817 }
3818
3819 uint32_t r;
3820 if (state <= 200 && Chance16R(1, 7, r)) {
3821 int index = (r * 10 >> 16);
3822
3823 Vehicle *u = v;
3824 do {
3825 if (--index < 0) {
3826 r = Random();
3827
3829 GB(r, 8, 3) + 2,
3830 GB(r, 16, 3) + 2,
3831 GB(r, 0, 3) + 5,
3833 break;
3834 }
3835 } while ((u = u->Next()) != nullptr);
3836 }
3837
3838 if (state <= 240 && !(v->tick_counter & 3)) ChangeTrainDirRandomly(v);
3839
3840 if (state >= 4440 && !(v->tick_counter & 0x1F)) {
3841 bool ret = v->Next() != nullptr;
3842 DeleteLastWagon(v);
3843 return ret;
3844 }
3845
3846 return true;
3847}
3848
3850static const uint16_t _breakdown_speeds[16] = {
3851 225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15
3852};
3853
3854
3863static bool TrainApproachingLineEnd(Train *moving_front, bool signal, bool reverse)
3864{
3865 /* Calc position within the current tile */
3866 uint x = moving_front->x_pos & 0xF;
3867 uint y = moving_front->y_pos & 0xF;
3868
3869 Direction vdir = moving_front->GetMovingDirection();
3870
3871 /* for diagonal directions, 'x' will be 0..15 -
3872 * for other directions, it will be 1, 3, 5, ..., 15 */
3873 switch (vdir) {
3874 case DIR_N : x = ~x + ~y + 25; break;
3875 case DIR_NW: x = y; [[fallthrough]];
3876 case DIR_NE: x = ~x + 16; break;
3877 case DIR_E : x = ~x + y + 9; break;
3878 case DIR_SE: x = y; break;
3879 case DIR_S : x = x + y - 7; break;
3880 case DIR_W : x = ~y + x + 9; break;
3881 default: break;
3882 }
3883
3884 Train *consist = moving_front->First();
3885
3886 /* Do not reverse when approaching red signal. Make sure the vehicle's front
3887 * does not cross the tile boundary when we do reverse, but as the vehicle's
3888 * location is based on their center, use half a vehicle's length as offset.
3889 * Multiply the half-length by two for straight directions to compensate that
3890 * we only get odd x offsets there. */
3891 uint8_t rounding = moving_front->IsDrivingBackwards() ? 0 : 1;
3892 if (!signal && x + (moving_front->gcache.cached_veh_length + rounding) / 2 * (IsDiagonalDirection(vdir) ? 1 : 2) >= TILE_SIZE) {
3893 /* we are too near the tile end, reverse now */
3894 consist->cur_speed = 0;
3895 if (reverse) ReverseTrainDirection(consist);
3896 return false;
3897 }
3898
3899 /* slow down */
3901 uint16_t break_speed = _breakdown_speeds[x & 0xF];
3902 if (break_speed < consist->cur_speed) consist->cur_speed = break_speed;
3903
3904 return true;
3905}
3906
3907
3913static bool TrainCanLeaveTile(const Train *moving_front)
3914{
3915 /* Exit if inside a tunnel/bridge or a depot */
3916 if (moving_front->track == TRACK_BIT_WORMHOLE || moving_front->track == TRACK_BIT_DEPOT) return false;
3917
3918 TileIndex tile = moving_front->tile;
3919
3920 /* entering a tunnel/bridge? */
3923 if (DiagDirToDir(dir) == moving_front->GetMovingDirection()) return false;
3924 }
3925
3926 /* entering a depot? */
3927 if (IsRailDepotTile(tile)) {
3929 if (DiagDirToDir(dir) == moving_front->GetMovingDirection()) return false;
3930 }
3931
3932 return true;
3933}
3934
3935
3944{
3945 assert(moving_front->IsMovingFront());
3946 assert(!moving_front->First()->vehstatus.Test(VehState::Crashed));
3947
3948 if (!TrainCanLeaveTile(moving_front)) return INVALID_TILE;
3949
3950 DiagDirection dir = VehicleExitDir(moving_front->GetMovingDirection(), moving_front->track);
3951 TileIndex tile = moving_front->tile + TileOffsByDiagDir(dir);
3952
3953 /* not a crossing || wrong axis || unusable rail (wrong type or owner) */
3954 if (!IsLevelCrossingTile(tile) || DiagDirToAxis(dir) == GetCrossingRoadAxis(tile) ||
3955 !CheckCompatibleRail(moving_front->First(), tile, true)) {
3956 return INVALID_TILE;
3957 }
3958
3959 return tile;
3960}
3961
3962
3970static bool TrainCheckIfLineEnds(Train *moving_front, bool reverse)
3971{
3972 /* First, handle broken down train */
3973
3974 Train *consist = moving_front->First();
3975 int t = consist->breakdown_ctr;
3976 if (t > 1) {
3978
3979 uint16_t break_speed = _breakdown_speeds[GB(~t, 4, 4)];
3980 if (break_speed < consist->cur_speed) consist->cur_speed = break_speed;
3981 } else {
3983 }
3984
3985 if (!TrainCanLeaveTile(moving_front)) return true;
3986
3987 /* Determine the non-diagonal direction in which we will exit this tile */
3988 DiagDirection dir = VehicleExitDir(moving_front->GetMovingDirection(), moving_front->track);
3989 /* Calculate next tile */
3990 TileIndex tile = moving_front->tile + TileOffsByDiagDir(dir);
3991
3992 /* Determine the track status on the next tile */
3994 TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(dir);
3995
3996 TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs;
3997 TrackdirBits red_signals = TrackStatusToRedSignals(ts) & reachable_trackdirs;
3998
3999 /* We are sure the train is not entering a depot, it is detected above */
4000
4001 /* mask unreachable track bits if we are forbidden to do 90deg turns */
4002 TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
4003 if (Rail90DegTurnDisallowed(GetTileRailType(moving_front->tile), GetTileRailType(tile))) {
4004 bits &= ~TrackCrossesTracks(FindFirstTrack(moving_front->track));
4005 }
4006
4007 /* no suitable trackbits at all || unusable rail (wrong type or owner) */
4008 if (bits == TRACK_BIT_NONE || !CheckCompatibleRail(consist, tile, true)) {
4009 return TrainApproachingLineEnd(moving_front, false, reverse);
4010 }
4011
4012 /* approaching red signal */
4013 if ((trackdirbits & red_signals) != 0) return TrainApproachingLineEnd(moving_front, true, reverse);
4014
4015 /* approaching a rail/road crossing? then make it red */
4017
4018 return true;
4019}
4020
4027static bool TrainLocoHandler(Train *consist, bool mode)
4028{
4029 /* train has crashed? */
4030 if (consist->vehstatus.Test(VehState::Crashed)) {
4031 return mode ? true : HandleCrashedTrain(consist); // 'this' can be deleted here
4032 }
4033
4034 if (consist->force_proceed != TFP_NONE) {
4037 }
4038
4039 /* train is broken down? */
4040 if (consist->HandleBreakdown()) return true;
4041
4042 if (consist->flags.Test(VehicleRailFlag::Reversing) && consist->cur_speed == 0) {
4043 ReverseTrainDirection(consist);
4044 }
4045
4046 /* exit if train is stopped */
4047 if (consist->vehstatus.Test(VehState::Stopped) && consist->cur_speed == 0) return true;
4048
4049 bool valid_order = !consist->current_order.IsType(OT_NOTHING) && consist->current_order.GetType() != OT_CONDITIONAL;
4050 if (ProcessOrders(consist) && CheckReverseTrain(consist)) {
4051 consist->wait_counter = 0;
4052 consist->cur_speed = 0;
4053 consist->subspeed = 0;
4055 ReverseTrainDirection(consist);
4056 return true;
4057 } else if (consist->flags.Test(VehicleRailFlag::LeavingStation)) {
4058 /* Try to reserve a path when leaving the station as we
4059 * might not be marked as wanting a reservation, e.g.
4060 * when an overlength train gets turned around in a station. */
4061 const Train *moving_front = consist->GetMovingFront();
4062 DiagDirection dir = VehicleExitDir(moving_front->GetMovingDirection(), moving_front->track);
4063 if (IsRailDepotTile(moving_front->tile) || IsTileType(moving_front->tile, TileType::TunnelBridge)) dir = INVALID_DIAGDIR;
4064
4065 if (UpdateSignalsOnSegment(moving_front->tile, dir, consist->owner) == SIGSEG_PBS || _settings_game.pf.reserve_paths) {
4066 TryPathReserve(consist, true, true);
4067 }
4069 }
4070
4071 consist->HandleLoading(mode);
4072
4073 if (consist->current_order.IsType(OT_LOADING)) return true;
4074
4075 if (CheckTrainStayInDepot(consist)) return true;
4076
4077 if (!mode) consist->ShowVisualEffect();
4078
4079 /* We had no order but have an order now, do look ahead. */
4080 if (!valid_order && !consist->current_order.IsType(OT_NOTHING)) {
4081 CheckNextTrainTile(consist);
4082 }
4083
4084 /* Handle stuck trains. */
4085 if (!mode && consist->flags.Test(VehicleRailFlag::Stuck)) {
4086 ++consist->wait_counter;
4087
4088 /* Should we try reversing this tick if still stuck? */
4089 bool turn_around = consist->wait_counter % (_settings_game.pf.wait_for_pbs_path * Ticks::DAY_TICKS) == 0 && _settings_game.pf.reverse_at_signals;
4090
4091 if (!turn_around && consist->wait_counter % _settings_game.pf.path_backoff_interval != 0 && consist->force_proceed == TFP_NONE) return true;
4092 if (!TryPathReserve(consist)) {
4093 /* Still stuck. */
4094 if (turn_around) ReverseTrainDirection(consist);
4095
4096 if (consist->flags.Test(VehicleRailFlag::Stuck) && consist->wait_counter > 2 * _settings_game.pf.wait_for_pbs_path * Ticks::DAY_TICKS) {
4097 /* Show message to player. */
4098 if (_settings_client.gui.lost_vehicle_warn && consist->owner == _local_company) {
4099 AddVehicleAdviceNewsItem(AdviceType::TrainStuck, GetEncodedString(STR_NEWS_TRAIN_IS_STUCK, consist->index), consist->index);
4100 }
4101 consist->wait_counter = 0;
4102 }
4103 /* Exit if force proceed not pressed, else reset stuck flag anyway. */
4104 if (consist->force_proceed == TFP_NONE) return true;
4106 consist->wait_counter = 0;
4108 }
4109 }
4110
4111 if (consist->current_order.IsType(OT_LEAVESTATION)) {
4112 consist->current_order.Free();
4114 return true;
4115 }
4116
4117 int j = consist->UpdateSpeed();
4118
4119 /* we need to invalidate the widget if we are stopping from 'Stopping 0 km/h' to 'Stopped' */
4120 if (consist->cur_speed == 0 && consist->vehstatus.Test(VehState::Stopped)) {
4121 /* If we manually stopped, we're not force-proceeding anymore. */
4122 consist->force_proceed = TFP_NONE;
4123 InvalidateWindowData(WC_VEHICLE_VIEW, consist->index);
4124 }
4125
4126 Train* moving_front = consist->GetMovingFront();
4127 int adv_spd = moving_front->GetAdvanceDistance();
4128 if (j < adv_spd) {
4129 /* if the vehicle has speed 0, update the last_speed field. */
4130 if (consist->cur_speed == 0) consist->SetLastSpeed();
4131 } else {
4132 TrainCheckIfLineEnds(moving_front);
4133 moving_front = moving_front->GetMovingFront();
4134 /* Loop until the train has finished moving. */
4135 for (;;) {
4136 j -= adv_spd;
4137 TrainController(moving_front, nullptr);
4138 moving_front = moving_front->GetMovingFront();
4139 /* Don't continue to move if the train crashed. */
4140 if (CheckTrainCollision(moving_front)) break;
4141 /* Determine distance to next map position */
4142 adv_spd = moving_front->GetAdvanceDistance();
4143
4144 /* No more moving this tick */
4145 if (j < adv_spd || consist->cur_speed == 0) break;
4146
4147 OrderType order_type = consist->current_order.GetType();
4148 /* Do not skip waypoints (incl. 'via' stations) when passing through at full speed. */
4149 if ((order_type == OT_GOTO_WAYPOINT || order_type == OT_GOTO_STATION) &&
4151 IsTileType(moving_front->tile, TileType::Station) &&
4152 consist->current_order.GetDestination() == GetStationIndex(moving_front->tile)) {
4153 ProcessOrders(consist);
4154 }
4155 }
4156 consist->SetLastSpeed();
4157 }
4158
4159 for (Train *u = consist; u != nullptr; u = u->Next()) {
4160 if (u->vehstatus.Test(VehState::Hidden)) continue;
4161
4162 u->UpdateViewport(false, false);
4163 }
4164
4165 if (consist->progress == 0) consist->progress = j; // Save unused spd for next time, if TrainController didn't set progress
4166
4167 return true;
4168}
4169
4175{
4176 Money cost = 0;
4177 const Train *v = this;
4178
4179 do {
4180 const Engine *e = v->GetEngine();
4181 if (e->VehInfo<RailVehicleInfo>().running_cost_class == Price::Invalid) continue;
4182
4183 uint cost_factor = GetVehicleProperty(v, PROP_TRAIN_RUNNING_COST_FACTOR, e->VehInfo<RailVehicleInfo>().running_cost);
4184 if (cost_factor == 0) continue;
4185
4186 /* Halve running cost for multiheaded parts */
4187 if (v->IsMultiheaded()) cost_factor /= 2;
4188
4189 cost += GetPrice(e->VehInfo<RailVehicleInfo>().running_cost_class, cost_factor, e->GetGRF());
4190 } while ((v = v->GetNextVehicle()) != nullptr);
4191
4192 return cost;
4193}
4194
4200{
4201 this->tick_counter++;
4202
4203 if (this->IsFrontEngine()) {
4205
4206 if (!this->vehstatus.Test(VehState::Stopped) || this->cur_speed > 0) this->running_ticks++;
4207
4208 this->current_order_time++;
4209
4210 if (!TrainLocoHandler(this, false)) return false;
4211
4212 return TrainLocoHandler(this, true);
4213 } else if (this->IsFreeWagon() && this->vehstatus.Test(VehState::Crashed)) {
4214 /* Delete flooded standalone wagon chain */
4215 if (++this->crash_anim_pos >= 4400) {
4216 delete this;
4217 return false;
4218 }
4219 }
4220
4221 return true;
4222}
4223
4229{
4230 if (Company::Get(v->owner)->settings.vehicle.servint_trains == 0 || !v->NeedsAutomaticServicing()) return;
4231 if (v->IsChainInDepot()) {
4233 return;
4234 }
4235
4236 uint max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty;
4237
4238 FindDepotData tfdd = FindClosestTrainDepot(v, max_penalty);
4239 /* Only go to the depot if it is not too far out of our way. */
4240 if (tfdd.best_length == UINT_MAX || tfdd.best_length > max_penalty) {
4241 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
4242 /* If we were already heading for a depot but it has
4243 * suddenly moved farther away, we continue our normal
4244 * schedule? */
4247 }
4248 return;
4249 }
4250
4251 DepotID depot = GetDepotIndex(tfdd.tile);
4252
4253 if (v->current_order.IsType(OT_GOTO_DEPOT) &&
4254 v->current_order.GetDestination() != depot &&
4255 !Chance16(3, 16)) {
4256 return;
4257 }
4258
4261 v->dest_tile = tfdd.tile;
4263}
4264
4267{
4268 AgeVehicle(this);
4269}
4270
4273{
4274 EconomyAgeVehicle(this);
4275
4276 if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
4277
4278 if (this->IsFrontEngine()) {
4280
4282
4283 CheckOrders(this);
4284
4285 /* update destination */
4286 if (this->current_order.IsType(OT_GOTO_STATION)) {
4287 TileIndex tile = Station::Get(this->current_order.GetDestination().ToStationID())->train_station.tile;
4288 if (tile != INVALID_TILE) this->dest_tile = tile;
4289 }
4290
4291 if (this->running_ticks != 0) {
4292 /* running costs */
4294
4295 this->profit_this_year -= cost.GetCost();
4296 this->running_ticks = 0;
4297
4299
4302 }
4303 }
4304}
4305
4311{
4312 if (this->vehstatus.Test(VehState::Crashed)) return INVALID_TRACKDIR;
4313
4314 if (this->track == TRACK_BIT_DEPOT) {
4315 /* We'll assume the train is facing outwards */
4316 return DiagDirToDiagTrackdir(GetRailDepotDirection(this->tile)); // Train in depot
4317 }
4318
4319 if (this->track == TRACK_BIT_WORMHOLE) {
4320 /* train in tunnel or on bridge, so just use its direction and assume a diagonal track */
4322 }
4323
4324 return TrackDirectionToTrackdir(FindFirstTrack(this->track), this->GetMovingDirection());
4325}
4326
4327uint16_t Train::GetMaxWeight() const
4328{
4329 uint16_t weight = CargoSpec::Get(this->cargo_type)->WeightOfNUnitsInTrain(this->GetEngine()->DetermineCapacity(this));
4330
4331 /* Vehicle weight is not added for articulated parts. */
4332 if (!this->IsArticulatedPart()) {
4333 weight += GetVehicleProperty(this, PROP_TRAIN_WEIGHT, RailVehInfo(this->engine_type)->weight);
4334 }
4335
4336 /* Powered wagons have extra weight added. */
4337 if (this->flags.Test(VehicleRailFlag::PoweredWagon)) {
4338 weight += RailVehInfo(this->gcache.first_engine)->pow_wag_weight;
4339 }
4340
4341 return weight;
4342}
Base functions for all AIs.
void AddArticulatedParts(Vehicle *first)
Add the remaining articulated parts to the given vehicle.
void CheckConsistencyOfArticulatedVehicle(const Vehicle *v)
Checks whether the specs of freshly build articulated vehicles are consistent with the information sp...
Functions related to articulated vehicles.
void CheckCargoCapacity(Vehicle *v)
Check the capacity of all vehicles in a chain and spread cargo if needed.
@ BuiltAsPrototype
Vehicle is a prototype (accepted as exclusive preview).
@ DrivingBackwards
Vehicle is driving backwards.
constexpr T AssignBit(T &x, const uint8_t y, bool value)
Assigns a bit in a variable.
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T ToggleBit(T &x, const uint8_t y)
Toggles a bit in a variable.
constexpr T KillFirstBit(T value)
Clear the first bit in an integer.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
const BridgeSpec * GetBridgeSpec(BridgeType i)
Get the specification of a bridge type.
Definition bridge.h:60
bool IsBridgeTile(Tile t)
checks if there is a bridge on this tile
Definition bridge_map.h:35
BridgeType GetBridgeType(Tile t)
Determines the type of bridge on a tile.
Definition bridge_map.h:56
bool IsBridge(Tile t)
Checks if this is a bridge, instead of a tunnel.
Definition bridge_map.h:24
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:110
CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:22
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
Definition ai_core.cpp:221
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Flip()
Flip all bits.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
Common return value for all commands.
void AddCost(const Money &cost)
Adds the given cost to the cost of the command.
Money GetCost() const
The costs as made up to this moment.
bool Failed() const
Did this command fail?
uint32_t GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition engine.cpp:182
uint16_t reliability_spd_dec
Speed of reliability decay between services (per day).
Definition engine_base.h:50
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
uint DetermineCapacity(const Vehicle *v, uint16_t *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition engine.cpp:226
EngineFlags flags
Flags of the engine.
Definition engine_base.h:57
uint8_t original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition engine_base.h:61
TimerGameCalendar::Date GetLifeLengthInDays() const
Returns the vehicle's (not model's!) life length in days.
Definition engine.cpp:468
CargoType GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition engine_base.h:94
uint16_t reliability
Current reliability of the engine.
Definition engine_base.h:49
static void NewEvent(class ScriptEvent *event)
Queue a new event for the game script.
RAII class for measuring multi-step elements of performance.
This struct contains all the info that is needed to draw and construct tracks.
Definition rail.h:115
uint8_t curve_speed
Multiplier for curve maximum speed advantage.
Definition rail.h:195
static constexpr TimerGameTick::Ticks DAY_TICKS
1 day is 74 ticks; TimerGameCalendar::date_fract used to be uint16_t and incremented by 885.
static Date date
Current date in days (day counter).
static Year year
Current year, starting at 0.
static Date date
Current date in days (day counter).
This class will save the current order of a vehicle and restore it on destruction.
void Restore()
Restore the saved order to the vehicle.
~VehicleOrderSaver()
Restore the saved order to the vehicle, if Restore() has not already been called.
bool SwitchToNextOrder(bool skip_first)
Set the current vehicle order to the next order in the order list.
Iterate over all vehicles near a given world coordinate.
Iterate over all vehicles on a tile.
Functions related to commands.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
@ Execute
execute the given command
@ NoCargoCapacityCheck
when autoreplace/autorenew is in progress, this shall prevent truncating the amount of cargo in the v...
@ AutoReplace
autoreplace/autorenew is in progress, this shall disable vehicle limits when building,...
Definition of stuff that is very close to a company, like the company struct itself.
CommandCost CheckOwnership(Owner owner, TileIndex tile)
Check whether the current owner owns something.
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
CompanyID _current_company
Company currently doing an action.
void SubtractMoneyFromCompanyFract(CompanyID company, const CommandCost &cst)
Subtract money from a company, including the money fraction.
Functions related to companies.
DepotID GetDepotIndex(Tile t)
Get the index of which depot is attached to the tile.
Definition depot_map.h:56
PoolID< uint16_t, struct DepotIDTag, 64000, 0xFFFF > DepotID
Type for the unique identifier of depots.
Definition depot_type.h:15
DirDiff DirDifference(Direction d0, Direction d1)
Calculate the difference between two directions.
Direction DiagDirToDir(DiagDirection dir)
Convert a DiagDirection to a Direction.
DiagDirection ReverseDiagDir(DiagDirection d)
Returns the reverse direction of the given DiagDirection.
Direction ReverseDir(Direction d)
Return the reverse of a direction.
bool IsValidDiagDirection(DiagDirection d)
Checks if an integer value is a valid DiagDirection.
Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
DiagDirection AxisToDiagDir(Axis a)
Converts an Axis to a DiagDirection.
bool IsDiagonalDirection(Direction dir)
Checks if a given Direction is diagonal.
Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
DirDiff
Enumeration for the difference between two directions.
@ DIRDIFF_90LEFT
Angle of 90 degrees left.
@ DIRDIFF_45LEFT
Angle of 45 degrees left.
@ DIRDIFF_45RIGHT
Angle of 45 degrees right.
@ DIRDIFF_SAME
Both directions faces to the same direction.
@ DIRDIFF_90RIGHT
Angle of 90 degrees right.
Direction
Defines the 8 directions on the map.
@ DIR_SW
Southwest.
@ DIR_NW
Northwest.
@ DIR_N
North.
@ DIR_SE
Southeast.
@ DIR_S
South.
@ DIR_NE
Northeast.
@ DIR_W
West.
@ DIR_E
East.
Axis
Allow incrementing of DiagDirDiff variables.
DiagDirection
Enumeration for diagonal directions.
@ DIAGDIR_END
Used for iterations.
@ DIAGDIR_BEGIN
Used for iterations.
@ INVALID_DIAGDIR
Flag for an invalid DiagDirection.
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition economy.cpp:938
@ TrainRun
Running costs trains.
@ NewVehicles
New vehicles.
@ Invalid
Invalid base price.
EffectVehicle * CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, EffectVehicleType type)
Create an effect vehicle above a particular vehicle.
Functions related to effect vehicles.
@ EV_EXPLOSION_SMALL
Various explosions.
@ EV_EXPLOSION_LARGE
Various explosions.
@ RailFlips
Rail vehicle has old depot-flip handling.
@ RailTilts
Rail vehicle tilts in curves.
@ VE_DISABLE_WAGON_POWER
Flag to disable wagon power.
Definition engine_type.h:68
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF > EngineID
Unique identification number of an engine.
Definition engine_type.h:26
@ ExclusivePreview
This vehicle is in the exclusive preview stage, either being used or being offered to a company.
@ Multihead
indicates a combination of two locomotives
Definition engine_type.h:33
@ Wagon
simple wagon, not motorized
Definition engine_type.h:34
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23).
Definition enum_type.hpp:21
Functions related to errors.
@ WL_CRITICAL
Critical errors, the MessageBox is shown in all cases.
Definition error.h:27
void ShowErrorMessage(EncodedString &&summary_msg, int x, int y, CommandCost &cc)
Display an error message in a window.
Error reporting related functions.
fluid_settings_t * settings
FluidSynth settings handle.
Types for recording game performance data.
@ PFE_GL_TRAINS
Time spent processing trains.
Base functions for all Games.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
@ AS_BRAKE
We want to stop.
@ GVF_SUPPRESS_IMPLICIT_ORDERS
Disable insertion and removal of automatic orders until the vehicle completes the real order.
@ GVF_GOINGDOWN_BIT
Vehicle is currently going downhill. (Cached track information for acceleration).
@ GVF_GOINGUP_BIT
Vehicle is currently going uphill. (Cached track information for acceleration).
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
void SetTrainGroupID(Train *v, GroupID grp)
Affect the groupID of a train to new_g.
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
static constexpr GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition group_type.h:18
TrackStatus GetTileTrackStatus(TileIndex tile, TransportType mode, RoadTramType sub_mode, DiagDirection side)
Returns information about trackdirs and signal states.
int GetSlopePixelZ(int x, int y, bool ground_vehicle)
Return world Z coordinate of a given point of a tile.
#define Rect
Macro that prevents name conflicts between included headers.
#define Point
Macro that prevents name conflicts between included headers.
DiagDirection DiagdirBetweenTiles(TileIndex tile_from, TileIndex tile_to)
Determines the DiagDirection to get from one tile to another.
Definition map_func.h:627
static TileIndex TileVirtXY(uint x, uint y)
Get a tile from the virtual XY-coordinate.
Definition map_func.h:407
TileIndex TileAddByDiagDir(TileIndex tile, DiagDirection dir)
Adds a DiagDir to a tile.
Definition map_func.h:615
TileIndexDiff TileOffsByAxis(Axis axis)
Convert an Axis to a TileIndexDiff.
Definition map_func.h:559
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:429
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:419
TileIndexDiff TileOffsByDiagDir(DiagDirection dir)
Convert a DiagDirection to a TileIndexDiff.
Definition map_func.h:574
int32_t TileIndexDiff
An offset value between two tiles.
Definition map_type.h:23
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition math_func.hpp:23
constexpr uint CeilDiv(uint a, uint b)
Computes ceil(a / b) for non-negative a and b.
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
Miscellaneous command definitions.
void HideFillingPercent(TextEffectID *te_id)
Hide vehicle loading indicators.
Definition misc_gui.cpp:582
bool _networking
are we in networking mode?
Definition network.cpp:67
Basic functions/variables used all over the place.
ClientID
'Unique' identifier to be given to clients
Base for the NewGRF implementation.
@ Trains
Trains feature.
Definition newgrf.h:73
@ ArticEngine
Add articulated engines (trains and road vehicles).
@ Length
Vehicle length (trains and road vehicles).
@ CBID_VEHICLE_LENGTH
Vehicle length, returns the amount of 1/8's the vehicle is shorter for trains and RVs.
@ CBID_TRAIN_ALLOW_WAGON_ATTACH
Determine whether a wagon can be attached to an already existing train.
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
void ErrorUnknownCallbackResult(uint32_t grfid, uint16_t cbid, uint16_t cb_res)
Record that a NewGRF returned an unknown/invalid callback result.
@ VehCapacity
Capacity of vehicle changes when not refitting or arranging.
Functions/types related to NewGRF debugging.
void InvalidateNewGRFInspectWindow(GrfSpecFeature feature, uint index)
Invalidate the inspect window for a given feature and index.
void DeleteNewGRFInspectWindow(GrfSpecFeature feature, uint index)
Delete inspect window for a given feature and index.
uint16_t GetVehicleCallbackParent(CallbackID callback, uint32_t param1, uint32_t param2, EngineID engine, const Vehicle *v, const Vehicle *parent, std::span< int32_t > regs100)
Evaluate a newgrf callback for vehicles with a different vehicle for parent scope.
bool UsesWagonOverride(const Vehicle *v)
Check if a wagon is currently using a wagon override.
uint16_t GetVehicleCallback(CallbackID callback, uint32_t param1, uint32_t param2, EngineID engine, const Vehicle *v, std::span< int32_t > regs100)
Evaluate a newgrf callback for vehicles.
std::optional< bool > TestVehicleBuildProbability(Vehicle *v, BuildProbabilityType type)
Test for vehicle build probability type.
@ Reversed
Change the rail vehicle should be reversed when purchased.
@ PROP_TRAIN_SHORTEN_FACTOR
Shorter vehicles.
@ PROP_TRAIN_USER_DATA
User defined data for vehicle variable 0x42.
@ PROP_TRAIN_WEIGHT
Weight in t (if dualheaded: for each single vehicle).
@ PROP_TRAIN_CARGO_AGE_PERIOD
Number of ticks before carried cargo is aged.
@ PROP_TRAIN_RUNNING_COST_FACTOR
Yearly runningcost (if dualheaded: sum of both vehicles).
@ PROP_TRAIN_SPEED
Max. speed: 1 unit = 1/1.6 mph = 1 km-ish/h.
bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event, bool force)
Checks whether a NewGRF wants to play a different vehicle sound effect.
Functions related to NewGRF provided sounds.
@ VSE_START
Vehicle starting, i.e. leaving, the station.
void TriggerStationRandomisation(BaseStation *st, TileIndex trigger_tile, StationRandomTrigger trigger, CargoType cargo_type)
Trigger station randomisation.
Header file for NewGRF stations.
StringID GetGRFStringID(uint32_t grfid, GRFStringID stringid)
Returns the index for this stringid associated with its grfID.
Header of Action 04 "universal holder" structure and functions.
StrongType::Typedef< uint32_t, struct GRFStringIDTag, StrongType::Compare, StrongType::Integer > GRFStringID
Type for GRF-internal string IDs.
static constexpr GRFStringID GRFSTR_MISC_GRF_TEXT
Miscellaneous GRF text range.
Functions related to news.
void AddVehicleNewsItem(EncodedString &&headline, NewsType type, VehicleID vehicle, StationID station=StationID::Invalid())
Adds a newsitem referencing a vehicle.
Definition news_func.h:32
void AddVehicleAdviceNewsItem(AdviceType advice_type, EncodedString &&headline, VehicleID vehicle)
Adds a vehicle-advice news item.
Definition news_func.h:43
@ ArrivalCompany
First vehicle arrived for company.
Definition news_type.h:30
@ ArrivalOther
First vehicle arrived for competitor.
Definition news_type.h:31
@ Accident
An accident or disaster has occurred.
Definition news_type.h:32
@ TrainStuck
The train got stuck and needs to be unstuck manually.
Definition news_type.h:56
@ Error
A game paused because a (critical) error.
Definition openttd.h:72
Functions related to order backups.
bool ProcessOrders(Vehicle *v)
Handle the orders of a vehicle and determine the next place to go to if needed.
bool UpdateOrderDest(Vehicle *v, const Order *order, int conditional_depth, bool pbs_look_ahead)
Update the vehicle's destination tile from an order.
void CheckOrders(const Vehicle *v)
Check the orders of a vehicle, to see if there are invalid orders and stuff.
void DeleteVehicleOrders(Vehicle *v, bool keep_orderlist, bool reset_order_indices)
Delete all orders from a vehicle.
VehicleOrderID ProcessConditionalOrder(const Order *order, const Vehicle *v)
Process a conditional order and determine the next order.
OrderStopLocation
Where to stop the trains.
Definition order_type.h:97
@ NearEnd
Stop at the near end of the platform.
Definition order_type.h:98
@ FarEnd
Stop at the far end of the platform.
Definition order_type.h:100
@ Middle
Stop at the middle of the platform.
Definition order_type.h:99
@ NonStop
The vehicle will not stop at any stations it passes except the destination, aka non-stop.
Definition order_type.h:88
@ GoVia
The vehicle will stop at any station it passes except the destination, aka via.
Definition order_type.h:89
uint8_t VehicleOrderID
The index of an order within its current vehicle (not pool related).
Definition order_type.h:18
@ NearestDepot
Send the vehicle to the nearest depot.
Definition order_type.h:119
@ Service
This depot order is because of the servicing limit.
Definition order_type.h:108
static const VehicleOrderID INVALID_VEH_ORDER_ID
Invalid vehicle order index (sentinel).
Definition order_type.h:39
OrderType
Order types.
Definition order_type.h:50
void SetRailStationPlatformReservation(TileIndex start, DiagDirection dir, bool b)
Set the reservation for a complete station platform.
Definition pbs.cpp:57
TrackBits GetReservedTrackbits(TileIndex t)
Get the reserved trackbits for any tile, regardless of type.
Definition pbs.cpp:24
bool TryReserveRailTrack(TileIndex tile, Track t, bool trigger_stations)
Try to reserve a specific track on a tile.
Definition pbs.cpp:80
bool IsWaitingPositionFree(const Train *v, TileIndex tile, Trackdir trackdir, bool forbid_90deg)
Check if a safe position is free.
Definition pbs.cpp:439
void UnreserveRailTrack(TileIndex tile, Track t)
Lift the reservation of a specific track on a tile.
Definition pbs.cpp:144
PBSTileInfo FollowTrainReservation(const Train *consist, Vehicle **train_on_res)
Follow a train reservation to the last tile.
Definition pbs.cpp:301
bool IsSafeWaitingPosition(const Train *v, TileIndex tile, Trackdir trackdir, bool include_line_end, bool forbid_90deg)
Determine whether a certain track on a tile is a safe position to end a path.
Definition pbs.cpp:395
bool HasReservedTracks(TileIndex tile, TrackBits tracks)
Check whether some of tracks is reserved on a tile.
Definition pbs.h:58
RailType GetTileRailType(Tile tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition rail.cpp:39
int TicksToLeaveDepot(const Train *v)
Compute number of ticks when next wagon will leave a depot.
RailTypes GetAllPoweredRailTypes(RailTypes railtypes)
Returns all powered railtypes for a set of railtypes.
Definition rail.h:325
bool IsCompatibleRail(RailType enginetype, RailType tiletype)
Checks if an engine of the given RailType can drive on a tile with a given RailType.
Definition rail.h:352
bool HasPowerOnRail(RailType enginetype, RailType tiletype)
Checks if an engine of the given RailType got power on a tile with a given RailType.
Definition rail.h:377
bool Rail90DegTurnDisallowed(RailType rt1, RailType rt2, bool def=_settings_game.pf.forbid_90_deg)
Test if 90 degree turns are disallowed between two railtypes.
Definition rail.h:411
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition rail.h:301
RailTypes GetAllCompatibleRailTypes(RailTypes railtypes)
Returns all compatible railtypes for a set of railtypes.
Definition rail.h:313
std::vector< Train * > TrainList
Helper type for lists/vectors of trains.
Definition rail_cmd.cpp:44
bool HasOnewaySignalBlockingTrackdir(Tile tile, Trackdir td)
Is a one-way signal blocking the trackdir?
Definition rail_map.h:548
static RailTileType GetRailTileType(Tile t)
Returns the RailTileType (normal with or without signals, waypoint or depot).
Definition rail_map.h:36
static bool IsPlainRail(Tile t)
Returns whether this is plain rails, with or without signals.
Definition rail_map.h:49
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition rail_map.h:115
bool HasSignalOnTrackdir(Tile tile, Trackdir trackdir)
Checks for the presence of signals along the given trackdir on the given rail tile.
Definition rail_map.h:491
TrackBits GetTrackBits(Tile tile)
Gets the track bits of the given tile.
Definition rail_map.h:136
static bool IsPlainRailTile(Tile t)
Checks whether the tile is a rail tile or rail tile with signals.
Definition rail_map.h:60
bool IsPbsSignal(SignalType s)
Checks whether the given signal is a path based signal.
Definition rail_map.h:292
@ Signals
Normal rail tile with signals.
Definition rail_map.h:25
Track GetRailDepotTrack(Tile t)
Returns the track of a depot, ignoring direction.
Definition rail_map.h:182
DiagDirection GetRailDepotDirection(Tile t)
Returns the direction the depot is facing to.
Definition rail_map.h:171
void SetSignalStateByTrackdir(Tile tile, Trackdir trackdir, SignalState state)
Sets the state of the signal along the given trackdir.
Definition rail_map.h:520
bool HasSignalOnTrack(Tile tile, Track track)
Checks for the presence of signals (either way) on the given track on the given rail tile.
Definition rail_map.h:475
bool HasPbsSignalOnTrackdir(Tile tile, Trackdir td)
Is a pbs signal present along the trackdir?
Definition rail_map.h:535
bool IsOnewaySignal(Tile t, Track track)
Is the signal at the given track on a tile a one way signal?
Definition rail_map.h:358
SignalType GetSignalType(Tile t, Track track)
Get the signal type for a track on a tile.
Definition rail_map.h:304
void SetDepotReservation(Tile t, bool b)
Set the reservation state of the depot.
Definition rail_map.h:269
bool HasDepotReservation(Tile t)
Get the reservation state of the depot.
Definition rail_map.h:257
bool HasSignals(Tile t)
Checks if a rail tile has signals.
Definition rail_map.h:72
SignalState GetSignalStateByTrackdir(Tile tile, Trackdir trackdir)
Gets the state of the signal along the given trackdir.
Definition rail_map.h:506
static bool IsRailDepotTile(Tile t)
Is this tile rail tile and a rail depot?
Definition rail_map.h:105
bool HasBlockSignalOnTrackdir(Tile tile, Trackdir td)
Check whether a block signal is present along the trackdir.
Definition rail_map.h:560
@ RAILTYPE_RAIL
Standard non-electric rails.
Definition rail_type.h:27
Pseudo random number generator.
bool Chance16(const uint32_t a, const uint32_t b, const std::source_location location=std::source_location::current())
Flips a coin with given probability.
bool Chance16R(const uint32_t a, const uint32_t b, uint32_t &r, const std::source_location location=std::source_location::current())
Flips a coin with a given probability and saves the randomize-number in a variable.
void UpdateLevelCrossing(TileIndex tile, bool sound=true, bool force_bar=false)
Update a level crossing to barred or open (crossing may include multiple adjacent tiles).
bool HasCrossingReservation(Tile t)
Get the reservation state of the rail crossing.
Definition road_map.h:390
bool IsLevelCrossingTile(Tile t)
Return whether a tile is a level crossing tile.
Definition road_map.h:79
Axis GetCrossingRoadAxis(Tile t)
Get the road axis of a level crossing.
Definition road_map.h:335
void SetCrossingBarred(Tile t, bool barred)
Set the bar state of a level crossing.
Definition road_map.h:438
Axis GetCrossingRailAxis(Tile t)
Get the rail axis of a level crossing.
Definition road_map.h:347
bool IsCrossingBarred(Tile t)
Check if the level crossing is barred.
Definition road_map.h:426
void SetCrossingReservation(Tile t, bool b)
Set the reservation state of the rail crossing.
Definition road_map.h:403
@ Invalid
Invalid marker.
Definition road_type.h:41
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
@ EndOfLineOnly
Trains can only flip when the track ends.
@ None
Trains cannot flip anywhere and must back up if the track ends.
SigSegState UpdateSignalsOnSegment(TileIndex tile, DiagDirection side, Owner owner)
Update signals, starting at one side of a tile Will check tile next to this at opposite side too.
Definition signal.cpp:654
void UpdateSignalsInBuffer()
Update signals in buffer Called from 'outside'.
Definition signal.cpp:580
void AddSideToSignalBuffer(TileIndex tile, DiagDirection side, Owner owner)
Add side of tile to signal update buffer.
Definition signal.cpp:628
void SetSignalsOnBothDir(TileIndex tile, Track track, Owner owner)
Update signals at segments that are at both ends of given (existent or non-existent) track.
Definition signal.cpp:672
SigSegState
State of the signal segment.
Definition signal_func.h:55
@ SIGSEG_PBS
Segment is a PBS segment.
Definition signal_func.h:58
@ SIGSEG_FULL
Occupied by a train.
Definition signal_func.h:57
@ SIGTYPE_PBS
normal pbs signal
Definition signal_type.h:28
@ SIGNAL_STATE_RED
The signal is red.
Definition signal_type.h:43
@ SIGNAL_STATE_GREEN
The signal is green.
Definition signal_type.h:44
Functions related to sound.
SoundFx
Sound effects from baseset.
Definition sound_type.h:46
@ SND_04_DEPARTURE_STEAM
2 == 0x02 Station departure: steam engine
Definition sound_type.h:50
@ SND_41_DEPARTURE_MAGLEV
65 == 0x41 Station departure: maglev engine
Definition sound_type.h:113
@ SND_13_TRAIN_COLLISION
15 == 0x11 Train+train crash
Definition sound_type.h:65
@ SND_0E_LEVEL_CROSSING
12 == 0x0C Train passes through level crossing
Definition sound_type.h:60
@ SND_0A_DEPARTURE_TRAIN
8 == 0x08 Station departure: diesel and electric engine
Definition sound_type.h:56
@ SND_47_DEPARTURE_MONORAIL
71 == 0x47 Station departure: monorail engine
Definition sound_type.h:119
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition sprites.h:1619
void ModifyStationRatingAround(TileIndex tile, Owner owner, int amount, uint radius)
Forcibly modify station ratings near a given tile.
bool IsRailWaypointTile(Tile t)
Is this tile a station tile and a rail waypoint?
bool IsCompatibleTrainStationTile(Tile test_tile, Tile station_tile)
Check if a tile is a valid continuation to a railstation tile.
bool IsRailStationTile(Tile t)
Is this tile a station tile and a rail station?
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:28
Axis GetRailStationAxis(Tile t)
Get the rail direction of a rail station.
@ HVOT_TRAIN
Station has seen a train.
@ Train
Station with train station.
@ VehicleArrives
Trigger platform when train arrives.
@ VehicleArrives
Trigger platform when train arrives.
Definition of base types and functions in a cross-platform compatible way.
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
EncodedString GetEncodedString(StringID str)
Encode a string with no parameters into an encoded string.
Definition strings.cpp:90
Functions related to OTTD's strings.
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Data structure for storing engine speed changes of an acceleration type.
uint8_t large_turn
Speed change due to a large turn.
uint8_t z_up
Fraction to remove when moving up.
uint8_t small_turn
Speed change due to a small turn.
uint8_t z_down
Fraction to add when moving down.
std::string name
Name of vehicle.
TimerGameTick::Ticks current_order_time
How many ticks have passed since this order started.
VehicleOrderID cur_real_order_index
The index to the current real (non-implicit) order.
VehicleFlags vehicle_flags
Used for gradual loading and other miscellaneous things (.
void ResetDepotUnbunching()
Resets all the data used for depot unbunching.
TileIndex xy
Base tile of the station.
StationFacilities facilities
The facilities that this station has.
TileArea train_station
Tile area the train 'station' part covers.
VehicleType type
Type of vehicle.
uint16_t speed
maximum travel speed (1 unit = 1/1.6 mph = 1 km-ish/h)
Definition bridge.h:39
bool Follow(TileIndex old_tile, Trackdir old_td)
Main follower routine.
bool is_tunnel
last turn passed tunnel
bool is_bridge
last turn passed bridge ramp
int tiles_skipped
number of skipped tunnel or station tiles
DiagDirection exitdir
exit direction (leaving the old tile)
TrackdirBits new_td_bits
the new set of available trackdirs
TileIndex new_tile
the new tile (the vehicle has entered)
Trackdir old_td
the trackdir (the vehicle was on) before move
bool is_station
last turn passed station
TileIndex old_tile
the origin (vehicle moved from) before move
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:139
Structure to return information about the closest depot location, and whether it could be found.
T y
Y coordinate.
T x
X coordinate.
uint16_t cargo_age_period
Number of ticks before carried cargo is aged.
EngineMiscFlags misc_flags
Miscellaneous flags.
VehicleCallbackMasks callback_mask
Bitmask of vehicle callbacks that have to be called.
Helper container to find a depot.
uint best_length
The distance towards the depot in penalty, or UINT_MAX if not found.
bool reverse
True if reversing is necessary for the train to get to this depot.
TileIndex tile
The tile of the depot.
Dynamic data of a loaded NewGRF.
Definition newgrf.h:118
uint traininfo_vehicle_width
Width (in pixels) of a 8/8 train vehicle in depot GUI and vehicle details.
Definition newgrf.h:159
int traininfo_vehicle_pitch
Vertical offset for drawing train images in depot GUI and vehicle details.
Definition newgrf.h:158
Position information of a vehicle after it moved.
TileIndex new_tile
Tile of the vehicle after moving.
int y
x and y position of the vehicle after moving
TileIndex old_tile
Current tile of the vehicle.
EngineID first_engine
Cached EngineID of the front vehicle. EngineID::Invalid() for the front vehicle itself.
uint32_t cached_power
Total power of the consist (valid only for the first engine).
uint16_t cached_total_length
Length of the whole vehicle (valid only for the first engine).
uint8_t cached_veh_length
Length of this vehicle in units of 1/VEHICLE_LENGTH of normal length. It is cached because this can b...
bool IsChainInDepot() const override
Check whether the whole vehicle chain is in the depot.
void SetFreeWagon()
Set a vehicle as a free wagon.
int UpdateInclination(bool new_tile, bool update_delta)
Checks if the vehicle is in a slope and sets the required flags in that case.
bool CanLeadTrain() const
Check if this vehicle can lead a train.
void SetMultiheaded()
Set a vehicle as a multiheaded engine.
bool IsEngine() const
Check if a vehicle is an engine (can be first in a consist).
bool IsRearDualheaded() const
Tell if we are dealing with the rear end of a multiheaded engine.
bool IsMultiheaded() const
Check if the vehicle is a multiheaded engine.
void SetEngine()
Set engine status.
void SetWagon()
Set a vehicle to be a wagon.
void SetFrontEngine()
Set front engine state.
void ClearFreeWagon()
Clear a vehicle from being a free wagon.
bool IsWagon() const
Check if a vehicle is a wagon.
uint Crash(bool flooded) override
Common code executed for crashed ground vehicles.
uint DoUpdateSpeed(uint accel, int min_speed, int max_speed)
void ClearFrontEngine()
Remove the front engine state.
void SetLastSpeed()
Update the GUI variant of the current speed of the vehicle.
static void CountVehicle(const Vehicle *v, int delta)
Update num_vehicle when adding or removing a vehicle.
static uint Size()
Get the size of the map.
Definition map_func.h:280
VehicleSpriteSeq sprite_seq
Vehicle appearance.
static void Backup(const Vehicle *v, uint32_t user)
Create an order backup for the given vehicle.
If you change this, keep in mind that it is also saved in 2 other places:
Definition order_base.h:34
OrderDepotTypeFlags GetDepotOrderType() const
What caused us going to the depot?
Definition order_base.h:170
DestinationID GetDestination() const
Gets the destination of this order.
Definition order_base.h:100
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition order_base.h:67
OrderStopLocation GetStopLocation() const
Where must we stop at the platform?
Definition order_base.h:164
OrderType GetType() const
Get the type of order of this order.
Definition order_base.h:73
void MakeDummy()
Makes this order a Dummy order.
void SetDestination(DestinationID destination)
Sets the destination of this order.
Definition order_base.h:107
OrderDepotActionFlags GetDepotActionType() const
What are we going to do when in the depot.
Definition order_base.h:176
void Free()
'Free' the order
Definition order_cmd.cpp:47
bool ShouldStopAtStation(const Vehicle *v, StationID station) const
Check whether the given vehicle should stop at the given station based on this order and the non-stop...
void MakeGoToDepot(DestinationID destination, OrderDepotTypeFlags order, OrderNonStopFlags non_stop_type=OrderNonStopFlag::NonStop, OrderDepotActionFlags action={}, CargoType cargo=CARGO_NO_REFIT)
Makes this order a Go To Depot order.
Definition order_cmd.cpp:73
OrderNonStopFlags GetNonStopType() const
At which stations must we stop?
Definition order_base.h:158
TileIndex tile
The base tile of the area.
This struct contains information about the end of a reserved path.
Definition pbs.h:26
Trackdir trackdir
The reserved trackdir on the tile.
Definition pbs.h:28
TileIndex tile
Tile the path ends, INVALID_TILE if no valid path was found.
Definition pbs.h:27
bool okay
True if tile is a safe waiting position, false otherwise.
Definition pbs.h:29
static Pool::IterateWrapper< Vehicle > Iterate(size_t from=0)
static Engine * Get(auto index)
static T * Create(Targs &&... args)
static Vehicle * GetIfValid(auto index)
Information about a rail vehicle.
Definition engine_type.h:74
uint16_t power
Power of engine (hp); For multiheaded engines the sum of both engine powers.
Definition engine_type.h:82
uint8_t user_def_data
Property 0x25: "User-defined bit mask" Used only for (very few) NewGRF vehicles.
Definition engine_type.h:94
uint8_t running_cost
Running cost of engine; For multiheaded engines the sum of both running costs.
Definition engine_type.h:84
uint8_t shorten_factor
length on main map for this type is 8 - shorten_factor
Definition engine_type.h:91
RailTypes railtypes
Railtypes, mangled if elrail is disabled.
Definition engine_type.h:78
uint16_t pow_wag_power
Extra power applied to consist if wagon should be powered.
Definition engine_type.h:88
uint16_t max_speed
Maximum speed (1 unit = 1/1.6 mph = 1 km-ish/h).
Definition engine_type.h:81
RailVehicleType railveh_type
Type of rail vehicle.
Definition engine_type.h:76
uint8_t capacity
Cargo capacity of vehicle; For multiheaded engines the capacity of each single engine.
Definition engine_type.h:87
int Width() const
Get width of Rect.
int Height() const
Get height of Rect.
static Station * Get(auto index)
T * GetMovingFront() const
Get the moving front of the vehicle chain.
T * GetMovingPrev() const
Get the previous vehicle in the vehicle chain, relative to its current movement.
T * Next() const
Get next vehicle in the chain.
T * Previous() const
Get previous vehicle in the chain.
static Train * From(Vehicle *v)
T * First() const
Get the first vehicle in the chain.
T * GetNextVehicle() const
Get the next real (non-articulated part) vehicle in the consist.
void UpdateViewport(bool force_update, bool update_delta)
Update vehicle sprite- and position caches.
T * GetMovingNext() const
Get the next vehicle in the vehicle chain, relative to its current movement.
T * GetLastEnginePart()
Get the last part of an articulated engine.
T * GetFirstEnginePart()
Get the first part of an articulated engine.
T * Last()
Get the last vehicle in the chain.
T * GetMovingBack() const
Get the moving back of the vehicle chain.
Station data structure.
uint GetPlatformLength(TileIndex tile, DiagDirection dir) const override
Determines the REMAINING length of a platform, starting at (and including) the given tile.
Definition station.cpp:292
uint16_t cached_max_curve_speed
max consist speed limited by curves
Definition train.h:84
'Train' is either a loco or a wagon.
Definition train.h:97
void PlayLeaveStationSound(bool force=false) const override
Play the sound associated with leaving the station.
void UpdateAcceleration()
Update acceleration of the train from the cached power and weight.
void OnNewCalendarDay() override
Calendar day handler.
Train * GetNextUnit() const
Get the next real (non-articulated part and non rear part of dualheaded engine) vehicle in the consis...
Definition train.h:156
Trackdir GetVehicleTrackdir() const override
Get the tracks of the train vehicle.
void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const override
Get the sprite to display the train.
Train * other_multiheaded_part
Link between the two ends of a multiheaded engine.
Definition train.h:105
RailTypes railtypes
On which rail types the train can run.
Definition train.h:108
uint16_t crash_anim_pos
Crash animation counter.
Definition train.h:99
bool Tick() override
Update train vehicle data for a tick.
void ReserveTrackUnderConsist() const
Tries to reserve track under whole train consist.
TileIndex GetOrderStationLocation(StationID station) override
Get the location of the next station to visit.
TrainForceProceeding force_proceed
How the train should behave when it encounters next obstacle.
Definition train.h:111
int GetDisplayImageWidth(Point *offset=nullptr) const
Get the width of a train vehicle image in the GUI.
ClosestDepot FindClosestDepot() override
Find the closest depot for this vehicle and tell us the location, DestinationID and whether we should...
TrackBits track
On which track the train currently is.
Definition train.h:110
void UpdateDeltaXY() override
Updates the x and y offsets and the size of the sprite used for this vehicle.
VehicleRailFlags flags
Which flags has this train currently set.
Definition train.h:98
int CalcNextVehicleOffset() const
Calculate the offset from this vehicle's center to the following center taking the vehicle lengths in...
Definition train.h:180
uint16_t GetMaxWeight() const override
Calculates the weight value that this vehicle will have when fully loaded with its current cargo.
uint16_t GetCurveSpeedLimit() const
Computes train speed limit caused by curves.
bool IsPrimaryVehicle() const override
Whether this is the primary vehicle in the chain.
Definition train.h:124
void MarkDirty() override
Goods at the consist have changed, update the graphics, cargo, and acceleration.
int UpdateSpeed()
This function looks at the vehicle and updates its speed (cur_speed and subspeed) variables.
uint Crash(bool flooded=false) override
The train vehicle crashed!
VehicleAccelerationModel GetAccelerationType() const
Allows to know the acceleration type of a vehicle.
Definition train.h:194
Train(VehicleID index)
Create new Train object.
Definition train.h:114
AccelStatus GetAccelerationStatus() const
Checks the current acceleration status of this vehicle.
Definition train.h:291
TrainCache tcache
Set of cached variables, recalculated on load and each time a vehicle is added to/removed from the co...
Definition train.h:102
void OnNewEconomyDay() override
Economy day handler.
int GetCursorImageOffset() const
Get the offset for train image when it is used as cursor.
void ConsistChanged(ConsistChangeFlags allowed_changes)
Recalculates the cached stuff of a train.
Money GetRunningCost() const override
Get running cost for the train consist.
RailTypes compatible_railtypes
With which rail types the train is compatible.
Definition train.h:107
uint16_t wait_counter
Ticks waiting in front of a signal, ticks being stuck or a counter for forced proceeding through sign...
Definition train.h:100
int GetCurrentMaxSpeed() const override
Calculates the maximum speed of the vehicle under its current conditions.
Sprite sequence for a vehicle part.
bool IsValid() const
Check whether the sequence contains any sprites.
void GetBounds(Rect *bounds) const
Determine shared bounds of all sprites.
Definition vehicle.cpp:123
void Set(SpriteID sprite)
Assign a single sprite to the sequence.
void Draw(int x, int y, PaletteID default_pal, bool force_pal) const
Draw the sprite sequence.
Definition vehicle.cpp:151
Vehicle data structure.
EngineID engine_type
The type of engine used for this vehicle.
int32_t z_pos
z coordinate.
Direction direction
facing
const Engine * GetEngine() const
Retrieves the engine of the vehicle.
Definition vehicle.cpp:748
Direction GetMovingDirection() const
Get the moving direction of this vehicle chain.
void IncrementRealOrderIndex()
Advanced cur_real_order_index to the next real order, keeps care of the wrap-around and invalidates t...
bool IsStoppedInDepot() const
Check whether the vehicle is in the depot and stopped.
bool IsMovingFront() const
Is this vehicle the moving front of the vehicle chain?
Order * GetOrder(int index) const
Returns order 'index' of a vehicle or nullptr when it doesn't exists.
void LeaveStation()
Perform all actions when leaving a station.
Definition vehicle.cpp:2365
void AddToShared(Vehicle *shared_chain)
Adds this vehicle to a shared vehicle chain.
Definition vehicle.cpp:3004
VehicleCargoList cargo
The cargo this vehicle is carrying.
TimerGameEconomy::Date date_of_last_service
Last economy date the vehicle had a service at a depot.
uint16_t cargo_cap
total capacity
StationID last_loading_station
Last station the vehicle has stopped at and could possibly leave from with any cargo loaded.
VehicleOrderID GetNumOrders() const
Get the number of orders this vehicle has.
void SetMovingDirection(Direction d)
Set the movement direction of this vehicle chain.
uint16_t random_bits
Bits used for randomized variational spritegroups.
void ReleaseUnitNumber()
Release the vehicle's unit number.
Definition vehicle.cpp:2435
uint8_t day_counter
Increased by one for each day.
void HandleLoading(bool mode=false)
Handle the loading of the vehicle; when not it skips through dummy orders and does nothing in all oth...
Definition vehicle.cpp:2446
Money profit_this_year
Profit this year << 8, low 8 bits are fract.
SpriteID colourmap
NOSAVE: cached colour mapping.
uint8_t breakdown_ctr
Counter for managing breakdown events.
uint GetAdvanceDistance()
Determines the vehicle "progress" needed for moving a step.
GroupID group_id
Index of group Pool array.
VehStates vehstatus
Status.
TimerGameCalendar::Date date_of_last_service_newgrf
Last calendar date the vehicle had a service at a depot, unchanged by the date cheat to protect again...
uint8_t subspeed
fractional speed
bool IsArticulatedPart() const
Check if the vehicle is an articulated part of an engine.
void LeaveUnbunchingDepot()
Leave an unbunching depot and calculate the next departure time for shared order vehicles.
Definition vehicle.cpp:2526
CargoType cargo_type
type of cargo this vehicle is carrying
uint8_t acceleration
used by train & aircraft
Vehicle * First() const
Get the first vehicle of this vehicle chain.
Order current_order
The current order (+ status, like: loading).
void HandlePathfindingResult(bool path_found)
Handle the pathfinding result, especially the lost status.
Definition vehicle.cpp:792
Vehicle * Next() const
Get the next vehicle of this vehicle.
int32_t y_pos
y coordinate.
int32_t x_pos
x coordinate.
const GRFFile * GetGRF() const
Retrieve the NewGRF the vehicle is tied to.
Definition vehicle.cpp:758
OrderList * orders
Pointer to the order list for this vehicle.
Money value
Value of the vehicle.
uint16_t refit_cap
Capacity left over from before last refit.
void InvalidateNewGRFCache()
Invalidates cached NewGRF variables.
VehicleCache vcache
Cache of often used vehicle values.
uint32_t GetGRFID() const
Retrieve the GRF ID of the NewGRF the vehicle is tied to.
Definition vehicle.cpp:768
SpriteBounds bounds
Bounding box of vehicle.
void BeginLoading()
Prepare everything to begin the loading when arriving at a station.
Definition vehicle.cpp:2220
uint8_t spritenum
currently displayed sprite index 0xfd == custom sprite, 0xfe == custom second head sprite 0xff == res...
uint16_t cur_speed
current speed
uint8_t cargo_subtype
Used for livery refits (NewGRF variations).
bool IsFrontEngine() const
Check if the vehicle is a front engine.
bool IsWaitingForUnbunching() const
Check whether a vehicle inside a depot is waiting for unbunching.
Definition vehicle.cpp:2573
TextEffectID fill_percent_te_id
a text-effect id to a loading indicator object
void SetNext(Vehicle *next)
Set the next vehicle of this vehicle.
Definition vehicle.cpp:2968
TimerGameCalendar::Date max_age
Maximum age.
MutableSpriteCache sprite_cache
Cache of sprites and values related to recalculating them, see MutableSpriteCache.
uint16_t reliability
Reliability.
bool HandleBreakdown()
Handle all of the aspects of a vehicle breakdown This includes adding smoke and sounds,...
Definition vehicle.cpp:1374
uint8_t progress
The percentage (if divided by 256) this vehicle already crossed the tile unit.
uint16_t reliability_spd_dec
Reliability decrease speed.
uint8_t tick_counter
Increased by one for each tick.
TileIndex tile
Current tile index.
TileIndex dest_tile
Heading for this tile.
void CopyVehicleConfigAndStatistics(Vehicle *src)
Copy certain configurations and statistics of a vehicle after successful autoreplace/renew The functi...
void UpdatePosition()
Update the position of the vehicle.
Definition vehicle.cpp:1699
StationID last_station_visited
The last station we stopped at.
bool IsDrivingBackwards() const
Is this vehicle moving backwards?
void ShowVisualEffect() const
Draw visual effects (smoke and/or sparks) for a vehicle chain.
Definition vehicle.cpp:2817
TimerGameCalendar::Year build_year
Year the vehicle has been built.
Owner owner
Which company owns the vehicle?
UnitID unitnumber
unit number, for display purposes only
bool NeedsAutomaticServicing() const
Checks if the current order should be interrupted for a service-in-depot order.
Definition vehicle.cpp:292
uint8_t running_ticks
Number of ticks this vehicle was not stopped this day.
@ EnteredStation
The vehicle entered a station.
Definition tile_cmd.h:25
@ CannotEnter
The vehicle cannot enter the tile.
Definition tile_cmd.h:27
@ EnteredWormhole
The vehicle either entered a bridge, tunnel or depot tile (this includes the last tile of the bridge/...
Definition tile_cmd.h:26
VehicleEnterTileStates VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y)
Call the tile callback function for a vehicle entering a tile.
Definition vehicle.cpp:1856
static bool IsTileType(Tile tile, TileType type)
Checks if a tile is a given tiletype.
Definition tile_map.h:150
bool IsTileOwner(Tile tile, Owner owner)
Checks if a tile belongs to the given owner.
Definition tile_map.h:214
Owner GetTileOwner(Tile tile)
Returns the owner of a tile.
Definition tile_map.h:178
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15
@ TunnelBridge
Tunnel entry/exit and bridge heads.
Definition tile_type.h:58
@ Station
A tile of a station or airport.
Definition tile_type.h:54
@ Railway
A tile with railway.
Definition tile_type.h:50
Definition of the game-calendar-timer.
Definition of the game-economy-timer.
Trackdir RemoveFirstTrackdir(TrackdirBits *trackdirs)
Removes first Trackdir from TrackdirBits and returns it.
Definition track_func.h:156
Track TrackdirToTrack(Trackdir trackdir)
Returns the Track that a given Trackdir represents.
Definition track_func.h:262
TrackBits TrackToTrackBits(Track track)
Maps a Track to the corresponding TrackBits value.
Definition track_func.h:77
TrackdirBits TrackBitsToTrackdirBits(TrackBits bits)
Converts TrackBits to TrackdirBits while allowing both directions.
Definition track_func.h:319
DiagDirection VehicleExitDir(Direction direction, TrackBits track)
Determine the side in which the vehicle will leave the tile.
Definition track_func.h:719
TrackdirBits TrackStatusToTrackdirBits(TrackStatus ts)
Returns the present-trackdir-information of a TrackStatus.
Definition track_func.h:354
Track TrackBitsToTrack(TrackBits tracks)
Converts TrackBits to Track.
Definition track_func.h:193
TrackBits TrackCrossesTracks(Track track)
Maps a track to all tracks that make 90 deg turns with it.
Definition track_func.h:422
Trackdir ReverseTrackdir(Trackdir trackdir)
Maps a trackdir to the reverse trackdir.
Definition track_func.h:247
bool TracksOverlap(TrackBits bits)
Checks if the given tracks overlap, ie form a crossing.
Definition track_func.h:650
Trackdir TrackDirectionToTrackdir(Track track, Direction dir)
Maps a track and a full (8-way) direction to the trackdir that represents the track running in the gi...
Definition track_func.h:503
bool IsValidTrackdir(Trackdir trackdir)
Checks if a Trackdir is valid for non-road vehicles.
Definition track_func.h:52
Trackdir FindFirstTrackdir(TrackdirBits trackdirs)
Returns first Trackdir from TrackdirBits or INVALID_TRACKDIR.
Definition track_func.h:211
TrackdirBits TrackdirCrossesTrackdirs(Trackdir trackdir)
Maps a trackdir to all trackdirs that make 90 deg turns with it.
Definition track_func.h:611
TrackdirBits TrackdirReachesTrackdirs(Trackdir trackdir)
Maps a trackdir to the trackdirs that can be reached from it (ie, when entering the next tile.
Definition track_func.h:589
TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
Returns all trackdirs that can be reached when entering a tile from a given (diagonal) direction.
Definition track_func.h:560
Track FindFirstTrack(TrackBits tracks)
Returns first Track from TrackBits or INVALID_TRACK.
Definition track_func.h:177
Trackdir TrackEnterdirToTrackdir(Track track, DiagDirection diagdir)
Maps a track and an (4-way) dir to the trackdir that represents the track with the entry in the given...
Definition track_func.h:488
TrackBits DiagdirReachesTracks(DiagDirection diagdir)
Returns all tracks that can be reached when entering a tile from a given (diagonal) direction.
Definition track_func.h:578
TrackBits DiagDirToDiagTrackBits(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track bits incidating with that diagdir.
Definition track_func.h:529
Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal trackdir that runs in that direction.
Definition track_func.h:542
TrackdirBits TrackStatusToRedSignals(TrackStatus ts)
Returns the red-signal-information of a TrackStatus.
Definition track_func.h:378
DiagDirection TrackdirToExitdir(Trackdir trackdir)
Maps a trackdir to the (4-way) direction the tile is exited when following that trackdir.
Definition track_func.h:441
Track DiagDirToDiagTrack(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal track incidating with that diagdir.
Definition track_func.h:517
TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
Discards all directional information from a TrackdirBits value.
Definition track_func.h:308
TrackBits
Allow incrementing of Track variables.
Definition track_type.h:35
@ TRACK_BIT_WORMHOLE
Bitflag for a wormhole (used for tunnels).
Definition track_type.h:52
@ TRACK_BIT_UPPER
Upper track.
Definition track_type.h:39
@ TRACK_BIT_DEPOT
Bitflag for a depot.
Definition track_type.h:53
@ TRACK_BIT_LEFT
Left track.
Definition track_type.h:41
@ TRACK_BIT_Y
Y-axis track.
Definition track_type.h:38
@ TRACK_BIT_NONE
No track.
Definition track_type.h:36
@ TRACK_BIT_X
X-axis track.
Definition track_type.h:37
@ TRACK_BIT_MASK
Bitmask for the first 6 bits.
Definition track_type.h:51
@ TRACK_BIT_LOWER
Lower track.
Definition track_type.h:40
@ TRACK_BIT_RIGHT
Right track.
Definition track_type.h:42
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:66
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition track_type.h:85
TrackdirBits
Allow incrementing of Trackdir variables.
Definition track_type.h:97
@ TRACKDIR_BIT_NONE
No track build.
Definition track_type.h:98
Track
These are used to specify a single track.
Definition track_type.h:19
@ INVALID_TRACK
Flag for an invalid track.
Definition track_type.h:28
@ TRACK_Y
Track along the y-axis (north-west to south-east).
Definition track_type.h:22
@ TRACK_BEGIN
Used for iterations.
Definition track_type.h:20
@ TRACK_X
Track along the x-axis (north-east to south-west).
Definition track_type.h:21
@ Capacity
Allow vehicles to change capacity.
Definition train.h:48
@ Length
Allow vehicles to change length.
Definition train.h:47
EnumBitSet< ConsistChangeFlag, uint8_t > ConsistChangeFlags
Bitset of the ConsistChangeFlag elements.
Definition train.h:51
static constexpr ConsistChangeFlags CCF_TRACK
Valid changes while vehicle is driving, and possibly changing tracks.
Definition train.h:53
bool TryPathReserve(Train *v, bool mark_as_stuck=false, bool first_tile_okay=false)
Try to reserve a path to a safe position.
int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *moving_front, int *station_ahead, int *station_length)
Get the stop location of (the center) of the front vehicle of a train at a platform of a station.
void FreeTrainTrackReservation(const Train *v)
Free the reserved path in front of a vehicle.
@ Reversed
Used for vehicle var 0xFE bit 8 (toggled each time the train is reversed, accurate for first vehicle ...
Definition train.h:31
@ LeavingStation
Train is just leaving a station.
Definition train.h:33
@ PoweredWagon
Wagon is powered.
Definition train.h:27
@ Reversing
Train is slowing down to reverse.
Definition train.h:26
@ Stuck
Train can't get a path reservation.
Definition train.h:32
@ AllowedOnNormalRail
Electric train engine is allowed to run on normal rail. *‍/.
Definition train.h:30
@ Flipped
Reverse the visible direction of the vehicle.
Definition train.h:28
TrainForceProceeding
Modes for ignoring signals.
Definition train.h:39
@ TFP_SIGNAL
Ignore next signal, after the signal ignore being stuck.
Definition train.h:42
@ TFP_NONE
Normal operation.
Definition train.h:40
@ TFP_STUCK
Proceed till next signal, but ignore being stuck till then. This includes force leaving depots.
Definition train.h:41
static constexpr ConsistChangeFlags CCF_ARRANGE
Valid changes for arranging the consist in a depot.
Definition train.h:57
static CommandCost CmdBuildRailWagon(DoCommandFlags flags, TileIndex tile, const Engine *e, Vehicle **ret)
Build a railroad wagon.
void FreeTrainTrackReservation(const Train *consist)
Free the reserved path in front of a vehicle.
static void NormaliseTrainHead(Train *head)
Normalise the head of the train again, i.e.
static CommandCost ValidateTrains(Train *original_dst, Train *dst, Train *original_src, Train *src, bool check_limit)
Validate whether we are going to create valid trains.
static bool CheckTrainStayInDepot(Train *v)
Will the train stay in the depot the next tick?
static FindDepotData FindClosestTrainDepot(Train *v, int max_distance)
Try to find a depot nearby.
CommandCost CmdForceTrainProceed(DoCommandFlags flags, VehicleID veh_id)
Force a train through a red signal.
void UpdateLevelCrossing(TileIndex tile, bool sound, bool force_bar)
Update a level crossing to barred or open (crossing may include multiple adjacent tiles).
int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *moving_front, int *station_ahead, int *station_length)
Get the stop location of (the center) of the front vehicle of a train at a platform of a station.
static bool CheckCompatibleRail(const Train *v, TileIndex tile, bool check_railtype)
Check if the vehicle is compatible with the specified tile.
static void AdvanceWagonsBeforeSwap(Train *moving_front)
Advances wagons for train reversing, needed for variable length wagons.
static void MakeTrainBackup(TrainList &list, Train *t)
Make a backup of a train into a train list.
static void ArrangeTrains(Train **dst_head, Train *dst, Train **src_head, Train *src, bool move_chain)
Arrange the trains in the wanted way.
static bool TryReserveSafeTrack(const Train *v, TileIndex tile, Trackdir td, bool override_railtype)
Try to reserve any path to a safe tile, ignoring the vehicle's destination.
static const uint16_t _breakdown_speeds[16]
Maximum speeds for train that is broken down or approaching line end.
static void InsertInConsist(Train *dst, Train *chain)
Inserts a chain into the train at dst.
void UpdateAdjacentLevelCrossingTilesOnLevelCrossingRemoval(TileIndex tile, Axis road_axis)
Update adjacent level crossing tiles in this multi-track crossing, due to removal of a level crossing...
static void SwapTrainFlags(uint16_t *swap_flag1, uint16_t *swap_flag2)
Swap the two up/down flags in two ways:
static void MarkTrainAsStuck(Train *consist)
Mark a train as stuck and stop it if it isn't stopped right now.
static void UpdateLevelCrossingTile(TileIndex tile, bool sound, bool force_barred)
Sets a level crossing tile to the correct state.
static void CheckNextTrainTile(Train *v)
Check if the train is on the last reserved tile and try to extend the path then.
static bool HandleCrashedTrain(Train *v)
Handle a crashed train.
void NormalizeTrainVehInDepot(const Train *u)
Move all free vehicles in the depot to the train.
CommandCost CmdReverseTrainDirection(DoCommandFlags flags, VehicleID veh_id, bool reverse_single_veh)
Reverse train.
static bool CheckLevelCrossing(TileIndex tile)
Check if a level crossing should be barred.
static CommandCost CheckNewTrain(Train *original_dst, Train *dst, Train *original_src, Train *src)
Check/validate whether we may actually build a new train.
CommandCost CmdBuildRailVehicle(DoCommandFlags flags, TileIndex tile, const Engine *e, Vehicle **ret)
Build a railroad vehicle.
static void UpdateStatusAfterSwap(Train *v, bool reverse=true)
Updates some variables after swapping the vehicle.
uint8_t FreightWagonMult(CargoType cargo)
Return the cargo weight multiplier to use for a rail vehicle.
Definition train_cmd.cpp:69
static uint CheckTrainCollision(Vehicle *v, Train *moving_front)
Collision test function.
bool TryPathReserve(Train *consist, bool mark_as_stuck, bool first_tile_okay)
Try to reserve a path to a safe position.
static uint TrainCrashed(Train *v)
Marks train as crashed and creates an AI event.
static void NormaliseDualHeads(Train *t)
Normalise the dual heads in the train, i.e.
static CommandCost CheckTrainAttachment(Train *t)
Check whether the train parts can be attached.
static const AccelerationSlowdownParams _accel_slowdown[]
Speed update fractions for each acceleration type.
CommandCost CmdMoveRailVehicle(DoCommandFlags flags, VehicleID src_veh, VehicleID dest_veh, bool move_chain)
Move a rail vehicle around inside the depot.
static Track DoTrainPathfind(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool do_track_reservation, PBSTileInfo *dest, TileIndex *final_dest)
Perform pathfinding for a train.
static void TrainEnterStation(Train *consist, StationID station)
Trains enters a station, send out a news item if it is the first train, and start loading.
static bool TrainLocoHandler(Train *consist, bool mode)
Per-tick handler of each front engine.
static bool TrainCanLeaveTile(const Train *moving_front)
Determines whether train would like to leave the tile.
static void AffectSpeedByZChange(Train *consist, int z_diff)
Modify the speed of the vehicle due to a change in altitude.
static void ClearPathReservation(const Train *v, TileIndex tile, Trackdir track_dir)
Clear the reservation of tile that was just left by a wagon on track_dir.
static void ReverseTrainDirection(Train *consist)
Turn a train around.
static bool IsTrain(const Vehicle *v)
Check if the vehicle is a train.
void CheckTrainsLengths()
Checks if lengths of all rail vehicles are valid.
Definition train_cmd.cpp:76
static bool CheckReverseTrain(const Train *consist)
Can the train reverse?
static void MaybeBarCrossingWithSound(TileIndex tile)
Bars crossing and plays ding-ding sound if not barred already.
bool TrainOnCrossing(TileIndex tile)
Check if a level crossing tile has a train on it.
static void ChangeTrainDirRandomly(Train *v)
Rotate all vehicles of a (crashed) train chain randomly to animate the crash.
void MarkDirtyAdjacentLevelCrossingTiles(TileIndex tile, Axis road_axis)
Find adjacent level crossing tiles in this multi-track crossing and mark them dirty.
static void NormaliseSubtypes(Train *chain)
Normalise the sub types of the parts in this chain.
bool TrainController(Train *v, Vehicle *nomove, bool reverse=true)
Move a vehicle chain one movement stop forwards.
CommandCost CmdSellRailWagon(DoCommandFlags flags, Vehicle *t, bool sell_chain, bool backup_order, ClientID user)
Sell a (single) train wagon/engine.
bool IsValidImageIndex< VehicleType::Train >(uint8_t image_index)
Helper to check whether an image index is valid for a particular vehicle.
Definition train_cmd.cpp:58
static TrainForceProceeding DetermineNextTrainForceProceeding(const Train *t)
Determine to what force_proceed should be changed.
static void RemoveFromConsist(Train *part, bool chain=false)
Remove the given wagon from its consist.
static bool TrainCheckIfLineEnds(Train *v, bool reverse=true)
Checks for line end.
static void RestoreTrainBackup(TrainList &list)
Restore the train from the backup list.
static void DeleteLastWagon(Train *v)
Deletes/Clears the last wagon of a crashed train.
static bool TrainApproachingLineEnd(Train *moving_front, bool signal, bool reverse)
Train is approaching line end, slow down and possibly reverse.
static bool TrainApproachingCrossing(TileIndex tile)
Finds a vehicle approaching rail-road crossing.
void ReverseTrainSwapVeh(Train *v, int l, int r)
Swap vehicles l and r in consist v, and reverse their direction.
static PBSTileInfo ExtendTrainReservation(const Train *v, TrackBits *new_tracks, DiagDirection *enterdir)
Extend a train path as far as possible.
static TileIndex TrainApproachingCrossingTile(const Train *v)
Determines whether train is approaching a rail-road crossing (thus making it barred).
static void AdvanceWagonsAfterSwap(Train *moving_front)
Advances wagons for train reversing, needed for variable length wagons.
void GetTrainSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
Get the size of the sprite of a train sprite heading west, or both heads (used for lists).
static void CheckIfTrainNeedsService(Train *v)
Check whether a train needs service, and if so, find a depot or service it.
static bool TrainApproachingCrossingEnum(const Vehicle *v, TileIndex tile)
Checks if a train is approaching a rail-road crossing.
static std::vector< VehicleID > GetFreeWagonsInDepot(TileIndex tile)
Get a list of free wagons in a depot.
Command definitions related to trains.
Sprites to use for trains.
static const uint8_t _engine_sprite_and[]
For how many directions do we have sprites?
static const uint8_t _engine_sprite_add[]
Non-zero for multihead trains.
@ TRANSPORT_RAIL
Transport by train.
bool IsTunnel(Tile t)
Is this a tunnel (entrance)?
Definition tunnel_map.h:23
void MarkBridgeDirty(TileIndex begin, TileIndex end, DiagDirection direction, uint bridge_height)
Mark bridge tiles dirty.
DiagDirection GetTunnelBridgeDirection(Tile t)
Get the direction pointing to the other end.
TileIndex GetOtherTunnelBridgeEnd(Tile t)
Determines type of the wormhole and returns its other end.
void SetTunnelBridgeReservation(Tile t, bool b)
Set the reservation state of the rail tunnel/bridge.
void ShowNewGrfVehicleError(EngineID engine, StringID part1, StringID part2, GRFBug bug_type, bool critical)
Displays a "NewGrf Bug" error message for a engine, and pauses the game if not networking.
Definition vehicle.cpp:337
void VehicleEnterDepot(Vehicle *v)
Vehicle entirely entered the depot, update its status, orders, vehicle windows, service it,...
Definition vehicle.cpp:1562
UnitID GetFreeUnitNumber(VehicleType type)
Get an unused unit number for a vehicle (if allowed).
Definition vehicle.cpp:1913
void VehicleLengthChanged(const Vehicle *u)
Logs a bug in GRF and shows a warning message if this is for the first time this happened.
Definition vehicle.cpp:363
void VehicleServiceInDepot(Vehicle *v)
Service a vehicle and all subsequent vehicles in the consist.
Definition vehicle.cpp:187
void CheckVehicleBreakdown(Vehicle *v)
Periodic check for a vehicle to maybe break down.
Definition vehicle.cpp:1318
GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v)
Get position information of a vehicle when moving one pixel in the direction it is facing.
Definition vehicle.cpp:1802
void DecreaseVehicleValue(Vehicle *v)
Decrease the value of a vehicle.
Definition vehicle.cpp:1297
void EconomyAgeVehicle(Vehicle *v)
Update economy age of a vehicle.
Definition vehicle.cpp:1440
CommandCost TunnelBridgeIsFree(TileIndex tile, TileIndex endtile, const Vehicle *ignore)
Finds vehicle in tunnel / bridge.
Definition vehicle.cpp:581
void AgeVehicle(Vehicle *v)
Update age of a vehicle.
Definition vehicle.cpp:1452
@ Crashed
Vehicle is crashed.
@ TrainSlowing
Train is slowing down.
@ Hidden
Vehicle is not visible.
@ DefaultPalette
Use default vehicle palette.
@ Stopped
Vehicle is stopped by the player.
Functions related to vehicles.
bool IsValidImageIndex(uint8_t image_index)
Helper to check whether an image index is valid for a particular vehicle.
@ CUSTOM_VEHICLE_SPRITENUM_REVERSED
Vehicle sprite from NewGRF with reverse driving direction (from articulation callback).
bool HasVehicleOnTile(TileIndex tile, UnaryPred &&predicate)
Loop over vehicles on a tile, and check whether a predicate is true for any of them.
@ VIWD_CONSIST_CHANGED
Vehicle composition was changed.
Definition vehicle_gui.h:37
EngineImageType
Visualisation contexts of vehicles and engines.
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF > VehicleID
The type all our vehicle IDs have.
@ Train
Train vehicle type.
static const uint VEHICLE_LENGTH
The length of a vehicle in tile units.
@ WID_VV_REFIT
Open the refit window.
@ WID_VV_START_STOP
Start or stop this vehicle, and show information about the current state.
Functions related to (drawing on) viewports.
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
Definition window.cpp:1209
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting).
Definition window.cpp:3230
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition window.cpp:3322
void SetWindowWidgetDirty(WindowClass cls, WindowNumber number, WidgetID widget_index)
Mark a particular widget in a particular window as dirty (in need of repainting).
Definition window.cpp:3216
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting).
Definition window.cpp:3200
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition window.cpp:3340
@ WC_VEHICLE_ORDERS
Vehicle orders; Window numbers:
@ WC_VEHICLE_DEPOT
Depot view; Window numbers:
@ WC_TRAINS_LIST
Trains list; Window numbers:
@ WC_VEHICLE_REFIT
Vehicle refit; Window numbers:
@ WC_VEHICLE_DETAILS
Vehicle details; Window numbers:
@ WC_COMPANY
Company view; Window numbers:
@ WC_VEHICLE_VIEW
Vehicle view; Window numbers:
@ WC_VEHICLE_TIMETABLE
Vehicle timetable; Window numbers:
FindDepotData YapfTrainFindNearestDepot(const Train *v, int max_distance)
Used when user sends train to the nearest depot or if train needs servicing using YAPF.
bool YapfTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir td, bool override_railtype)
Try to extend the reserved path of a train to the nearest safe tile using YAPF.
bool YapfTrainCheckReverse(const Train *v)
Returns true if it is better to reverse the train before leaving station using YAPF.
Track YapfTrainChooseTrack(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool reserve_track, struct PBSTileInfo *target, TileIndex *dest)
Finds the best path for given train using YAPF.
Base includes/functions for YAPF.
Functions related to zooming.
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition zoom_func.h:107
int UnScaleGUI(int value)
Short-hand to apply GUI zoom level.
Definition zoom_func.h:77