OpenTTD Source 20260531-master-g0e951f3528
roadveh_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 "roadveh.h"
12#include "command_func.h"
13#include "error_func.h"
14#include "news_func.h"
15#include "station_base.h"
16#include "company_func.h"
18#include "newgrf_sound.h"
20#include "strings_func.h"
21#include "tunnelbridge_map.h"
24#include "vehicle_func.h"
25#include "sound_func.h"
26#include "ai/ai.hpp"
27#include "game/game.hpp"
28#include "depot_map.h"
29#include "effectvehicle_func.h"
30#include "roadstop_base.h"
31#include "core/random_func.hpp"
32#include "company_base.h"
33#include "core/backup_type.hpp"
34#include "newgrf.h"
35#include "zoom_func.h"
36#include "framerate_type.h"
37#include "roadveh_cmd.h"
38#include "road_cmd.h"
39#include "newgrf_roadstop.h"
40
41#include "table/strings.h"
42
43#include "safeguards.h"
44
45static const uint16_t _roadveh_images[] = {
46 0xCD4, 0xCDC, 0xCE4, 0xCEC, 0xCF4, 0xCFC, 0xD0C, 0xD14,
47 0xD24, 0xD1C, 0xD2C, 0xD04, 0xD1C, 0xD24, 0xD6C, 0xD74,
48 0xD7C, 0xC14, 0xC1C, 0xC24, 0xC2C, 0xC34, 0xC3C, 0xC4C,
49 0xC54, 0xC64, 0xC5C, 0xC6C, 0xC44, 0xC5C, 0xC64, 0xCAC,
50 0xCB4, 0xCBC, 0xD94, 0xD9C, 0xDA4, 0xDAC, 0xDB4, 0xDBC,
51 0xDCC, 0xDD4, 0xDE4, 0xDDC, 0xDEC, 0xDC4, 0xDDC, 0xDE4,
52 0xE2C, 0xE34, 0xE3C, 0xC14, 0xC1C, 0xC2C, 0xC3C, 0xC4C,
53 0xC5C, 0xC64, 0xC6C, 0xC74, 0xC84, 0xC94, 0xCA4
54};
55
56static const uint16_t _roadveh_full_adder[] = {
57 0, 88, 0, 0, 0, 0, 48, 48,
58 48, 48, 0, 0, 64, 64, 0, 16,
59 16, 0, 88, 0, 0, 0, 0, 48,
60 48, 48, 48, 0, 0, 64, 64, 0,
61 16, 16, 0, 88, 0, 0, 0, 0,
62 48, 48, 48, 48, 0, 0, 64, 64,
63 0, 16, 16, 0, 8, 8, 8, 8,
64 0, 0, 0, 8, 8, 8, 8
65};
66static_assert(lengthof(_roadveh_images) == lengthof(_roadveh_full_adder));
67
69template <>
70bool IsValidImageIndex<VehicleType::Road>(uint8_t image_index)
71{
72 return image_index < lengthof(_roadveh_images);
73}
74
79
85{
86 assert(this->IsFrontEngine());
88}
89
96{
97 int reference_width = ROADVEHINFO_DEFAULT_VEHICLE_WIDTH;
98
99 if (offset != nullptr) {
100 offset->x = ScaleSpriteTrad(reference_width) / 2;
101 offset->y = 0;
102 }
103 return ScaleSpriteTrad(this->gcache.cached_veh_length * reference_width / VEHICLE_LENGTH);
104}
105
106static void GetRoadVehIcon(EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
107{
108 const Engine *e = Engine::Get(engine);
109 uint8_t spritenum = e->VehInfo<RoadVehicleInfo>().image_index;
110
111 if (IsCustomVehicleSpriteNum(spritenum)) {
112 GetCustomVehicleIcon(engine, Direction::W, image_type, result);
113 if (result->IsValid()) return;
114
115 spritenum = e->original_image_index;
116 }
117
118 assert(IsValidImageIndex<VehicleType::Road>(spritenum));
119 result->Set(to_underlying(Direction::W) + _roadveh_images[spritenum]);
120}
121
123{
124 uint8_t spritenum = this->spritenum;
125
126 if (IsCustomVehicleSpriteNum(spritenum)) {
128 GetCustomVehicleSprite(this, direction, image_type, result);
129 if (result->IsValid()) return;
130
132 }
133
135 SpriteID sprite = to_underlying(direction) + _roadveh_images[spritenum];
136
137 if (this->cargo.StoredCount() >= this->cargo_cap / 2U) sprite += _roadveh_full_adder[spritenum];
138
139 result->Set(sprite);
140}
141
152void DrawRoadVehEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
153{
155 GetRoadVehIcon(engine, image_type, &seq);
156
157 Rect rect;
158 seq.GetBounds(&rect);
159 preferred_x = Clamp(preferred_x,
160 left - UnScaleGUI(rect.left),
161 right - UnScaleGUI(rect.right));
162
163 seq.Draw(preferred_x, y, pal, pal == PALETTE_CRASH);
164}
165
175void GetRoadVehSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
176{
178 GetRoadVehIcon(engine, image_type, &seq);
179
180 Rect rect;
181 seq.GetBounds(&rect);
182
183 width = UnScaleGUI(rect.Width());
184 height = UnScaleGUI(rect.Height());
185 xoffs = UnScaleGUI(rect.left);
186 yoffs = UnScaleGUI(rect.top);
187}
188
194static uint GetRoadVehLength(const RoadVehicle *v)
195{
196 const Engine *e = v->GetEngine();
197 uint length = VEHICLE_LENGTH;
198
199 uint16_t veh_len = CALLBACK_FAILED;
200 if (e->GetGRF() != nullptr && e->GetGRF()->grf_version >= 8) {
201 /* Use callback 36 */
202 veh_len = GetVehicleProperty(v, PROP_ROADVEH_SHORTEN_FACTOR, CALLBACK_FAILED);
203 if (veh_len != CALLBACK_FAILED && veh_len >= VEHICLE_LENGTH) ErrorUnknownCallbackResult(e->GetGRFID(), CBID_VEHICLE_LENGTH, veh_len);
204 } else {
205 /* Use callback 11 */
206 veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, v->engine_type, v);
207 }
208 if (veh_len == CALLBACK_FAILED) veh_len = e->VehInfo<RoadVehicleInfo>().shorten_factor;
209 if (veh_len != 0) {
210 length -= Clamp(veh_len, 0, VEHICLE_LENGTH - 1);
211 }
212
213 return length;
214}
215
222void RoadVehUpdateCache(RoadVehicle *v, bool same_length)
223{
224 assert(v->type == VehicleType::Road);
225 assert(v->IsFrontEngine());
226
228
230
231 for (RoadVehicle *u = v; u != nullptr; u = u->Next()) {
232 /* Check the v->first cache. */
233 assert(u->First() == v);
234
235 /* Update the 'first engine' */
236 u->gcache.first_engine = (v == u) ? EngineID::Invalid() : v->engine_type;
237
238 /* Update the length of the vehicle. */
239 uint veh_len = GetRoadVehLength(u);
240 /* Verify length hasn't changed. */
241 if (same_length && veh_len != u->gcache.cached_veh_length) VehicleLengthChanged(u);
242
243 u->gcache.cached_veh_length = veh_len;
244 v->gcache.cached_total_length += u->gcache.cached_veh_length;
245
246 /* Update visual effect */
247 u->UpdateVisualEffect();
248
249 /* Update cargo aging period. */
250 u->vcache.cached_cargo_age_period = GetVehicleProperty(u, PROP_ROADVEH_CARGO_AGE_PERIOD, EngInfo(u->engine_type)->cargo_age_period);
251 }
252
253 uint max_speed = GetVehicleProperty(v, PROP_ROADVEH_SPEED, 0);
254 v->vcache.cached_max_speed = (max_speed != 0) ? max_speed * 4 : RoadVehInfo(v->engine_type)->max_speed;
255}
256
265CommandCost CmdBuildRoadVehicle(DoCommandFlags flags, TileIndex tile, const Engine *e, Vehicle **ret)
266{
267 /* Check that the vehicle can drive on the road in question */
268 RoadType rt = e->VehInfo<RoadVehicleInfo>().roadtype;
269 const RoadTypeInfo *rti = GetRoadTypeInfo(rt);
270 if (!HasTileAnyRoadType(tile, rti->powered_roadtypes)) return CommandCost(STR_ERROR_DEPOT_WRONG_DEPOT_TYPE);
271
272 if (flags.Test(DoCommandFlag::Execute)) {
273 const RoadVehicleInfo *rvi = &e->VehInfo<RoadVehicleInfo>();
274
276 *ret = v;
278 v->owner = _current_company;
279
280 v->tile = tile;
281 int x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
282 int y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
283 v->x_pos = x;
284 v->y_pos = y;
285 v->z_pos = GetSlopePixelZ(x, y, true);
286
287 v->state = RVSB_IN_DEPOT;
289
290 v->spritenum = rvi->image_index;
291 v->cargo_type = e->GetDefaultCargoType();
292 assert(IsValidCargoType(v->cargo_type));
293 v->cargo_cap = rvi->capacity;
294 v->refit_cap = 0;
295
296 v->last_station_visited = StationID::Invalid();
297 v->last_loading_station = StationID::Invalid();
298 v->engine_type = e->index;
299 v->gcache.first_engine = EngineID::Invalid(); // needs to be set before first callback
300
301 v->reliability = e->reliability;
302 v->reliability_spd_dec = e->reliability_spd_dec;
303 v->max_age = e->GetLifeLengthInDays();
304
305 v->SetServiceInterval(Company::Get(v->owner)->settings.vehicle.servint_roadveh);
306
307 v->date_of_last_service = TimerGameEconomy::date;
308 v->date_of_last_service_newgrf = TimerGameCalendar::date;
309 v->build_year = TimerGameCalendar::year;
310
311 v->sprite_cache.sprite_seq.Set(SPR_IMG_QUERY);
312 v->random_bits = Random();
313 v->SetFrontEngine();
314
315 v->roadtype = rt;
316 v->compatible_roadtypes = rti->powered_roadtypes;
317 v->gcache.cached_veh_length = VEHICLE_LENGTH;
318
320 v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
321
323 v->InvalidateNewGRFCacheOfChain();
324
325 /* Call various callbacks after the whole consist has been constructed */
326 for (RoadVehicle *u = v; u != nullptr; u = u->Next()) {
327 u->cargo_cap = u->GetEngine()->DetermineCapacity(u);
328 u->refit_cap = 0;
329 v->InvalidateNewGRFCache();
330 u->InvalidateNewGRFCache();
331 }
333 /* Initialize cached values for realistic acceleration. */
334 if (_settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) v->CargoChanged();
335
336 v->UpdatePosition();
337
339 }
340
341 return CommandCost();
342}
343
344static FindDepotData FindClosestRoadDepot(const RoadVehicle *v, int max_distance)
345{
346 if (IsRoadDepotTile(v->tile)) return FindDepotData(v->tile, 0);
347
348 return YapfRoadVehicleFindNearestDepot(v, max_distance);
349}
350
352{
353 FindDepotData rfdd = FindClosestRoadDepot(this, 0);
354 if (rfdd.best_length == UINT_MAX) return ClosestDepot();
355
356 return ClosestDepot(rfdd.tile, GetDepotIndex(rfdd.tile));
357}
358
365CommandCost CmdTurnRoadVeh(DoCommandFlags flags, VehicleID veh_id)
366{
368 if (v == nullptr) return CMD_ERROR;
369
370 if (!v->IsPrimaryVehicle()) return CMD_ERROR;
371
373 if (ret.Failed()) return ret;
374
375 if (v->vehstatus.Any({VehState::Stopped, VehState::Crashed}) ||
376 v->breakdown_ctr != 0 ||
377 v->overtaking != 0 ||
378 v->state == RVSB_WORMHOLE ||
379 v->IsInDepot() ||
380 v->current_order.IsType(OT_LOADING)) {
381 return CMD_ERROR;
382 }
383
385
387
388 if (flags.Test(DoCommandFlag::Execute)) {
389 v->reverse_ctr = 180;
390
391 /* Unbunching data is no longer valid. */
393 }
394
395 return CommandCost();
396}
397
398
400{
401 for (RoadVehicle *v = this; v != nullptr; v = v->Next()) {
402 v->colourmap = PAL_NONE;
403 v->UpdateViewport(true, false);
404 }
405 this->CargoChanged();
406}
407
409{
410 /* Set common defaults. */
411 this->bounds = {{-1, -1, 0}, {3, 3, 6}, {}};
412
413 if (!IsDiagonalDirection(this->direction)) {
414 static const DiagDirectionIndexArray<Point> _sign_table{{{
415 /* x, y */
416 {-1, -1}, // Direction::N
417 {-1, 1}, // Direction::E
418 { 1, 1}, // Direction::S
419 { 1, -1}, // Direction::W
420 }}};
421
422 int half_shorten = (VEHICLE_LENGTH - this->gcache.cached_veh_length) / 2;
423
424 /* For all straight directions, move the bound box to the centre of the vehicle, but keep the size. */
425 this->bounds.offset.x -= half_shorten * _sign_table[DirToDiagDir(this->direction)].x;
426 this->bounds.offset.y -= half_shorten * _sign_table[DirToDiagDir(this->direction)].y;
427 } else {
428 /* Unlike trains, road vehicles do not have their offsets moved to the centre. */
429 switch (this->direction) {
430 /* Shorten southern corner of the bounding box according the vehicle length. */
431 case Direction::NE:
432 this->bounds.origin.x = -3;
433 this->bounds.extent.x = this->gcache.cached_veh_length;
434 this->bounds.offset.x = 1;
435 break;
436
437 case Direction::NW:
438 this->bounds.origin.y = -3;
439 this->bounds.extent.y = this->gcache.cached_veh_length;
440 this->bounds.offset.y = 1;
441 break;
442
443 /* Move northern corner of the bounding box down according to vehicle length. */
444 case Direction::SW:
445 this->bounds.origin.x = -3 + (VEHICLE_LENGTH - this->gcache.cached_veh_length);
446 this->bounds.extent.x = this->gcache.cached_veh_length;
447 this->bounds.offset.x = 1 - (VEHICLE_LENGTH - this->gcache.cached_veh_length);
448 break;
449
450 case Direction::SE:
451 this->bounds.origin.y = -3 + (VEHICLE_LENGTH - this->gcache.cached_veh_length);
452 this->bounds.extent.y = this->gcache.cached_veh_length;
453 this->bounds.offset.y = 1 - (VEHICLE_LENGTH - this->gcache.cached_veh_length);
454 break;
455
456 default:
457 NOT_REACHED();
458 }
459 }
460}
461
467{
468 int max_speed = this->gcache.cached_max_track_speed;
469
470 /* Limit speed to 50% while reversing, 75% in curves. */
471 for (const RoadVehicle *u = this; u != nullptr; u = u->Next()) {
472 if (_settings_game.vehicle.roadveh_acceleration_model == AM_REALISTIC) {
474 max_speed = this->gcache.cached_max_track_speed / 2;
475 break;
476 } else if (!IsDiagonalDirection(u->direction)) {
477 max_speed = this->gcache.cached_max_track_speed * 3 / 4;
478 }
479 }
480
481 /* Vehicle is on the middle part of a bridge. */
482 if (u->state == RVSB_WORMHOLE && !u->vehstatus.Test(VehState::Hidden)) {
483 max_speed = std::min(max_speed, GetBridgeSpec(GetBridgeType(u->tile))->speed * 2);
484 }
485 }
486
487 return std::min(max_speed, this->current_order.GetMaxSpeed() * 2);
488}
489
495{
496 RoadVehicle *first = v->First();
497 Vehicle *u = v;
498 for (; v->Next() != nullptr; v = v->Next()) u = v;
499 u->SetNext(nullptr);
500 v->last_station_visited = first->last_station_visited; // for PreDestructor
501
502 /* Only leave the road stop when we're really gone. */
503 if (IsInsideMM(v->state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END)) RoadStop::GetByTile(v->tile, GetRoadStopType(v->tile))->Leave(v);
504
505 delete v;
506}
507
508static void RoadVehSetRandomDirection(RoadVehicle *v)
509{
510 static const DirDiff delta[] = {
512 };
513
514 do {
515 uint32_t r = Random();
516
517 v->direction = ChangeDir(v->direction, delta[r & 3]);
518 v->UpdateViewport(true, true);
519 } while ((v = v->Next()) != nullptr);
520}
521
528{
529 v->crashed_ctr++;
530 if (v->crashed_ctr == 2) {
532 } else if (v->crashed_ctr <= 45) {
533 if ((v->tick_counter & 7) == 0) RoadVehSetRandomDirection(v);
534 } else if (v->crashed_ctr >= 2220 && !(v->tick_counter & 0x1F)) {
535 bool ret = v->Next() != nullptr;
537 return ret;
538 }
539
540 return true;
541}
542
543uint RoadVehicle::Crash(bool flooded)
544{
545 uint victims = this->GroundVehicleBase::Crash(flooded);
546 if (this->IsFrontEngine()) {
547 victims += 1; // driver
548
549 /* If we're in a drive through road stop we ought to leave it */
550 if (IsInsideMM(this->state, RVSB_IN_DT_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END)) {
551 RoadStop::GetByTile(this->tile, GetRoadStopType(this->tile))->Leave(this);
552 }
553 }
554 this->crashed_ctr = flooded ? 2000 : 1; // max 2220, disappear pretty fast when flooded
555 return victims;
556}
557
558static void RoadVehCrash(RoadVehicle *v)
559{
560 uint victims = v->Crash();
561
562 AI::NewEvent(v->owner, new ScriptEventVehicleCrashed(v->index, v->tile, ScriptEventVehicleCrashed::CRASH_RV_LEVEL_CROSSING, victims, v->owner));
563 Game::NewEvent(new ScriptEventVehicleCrashed(v->index, v->tile, ScriptEventVehicleCrashed::CRASH_RV_LEVEL_CROSSING, victims, v->owner));
564
565 EncodedString headline = (victims == 1)
566 ? GetEncodedString(STR_NEWS_ROAD_VEHICLE_CRASH_DRIVER)
567 : GetEncodedString(STR_NEWS_ROAD_VEHICLE_CRASH, victims);
569
570 AddTileNewsItem(std::move(headline), newstype, v->tile);
571
572 ModifyStationRatingAround(v->tile, v->owner, -160, 22);
573 if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_12_EXPLOSION, v);
574}
575
576static bool RoadVehCheckTrainCrash(RoadVehicle *v)
577{
578 for (RoadVehicle *u = v; u != nullptr; u = u->Next()) {
579 if (u->state == RVSB_WORMHOLE) continue;
580
581 TileIndex tile = u->tile;
582
583 if (!IsLevelCrossingTile(tile)) continue;
584
585 if (HasVehicleNearTileXY(v->x_pos, v->y_pos, 4, [&u](const Vehicle *t) {
586 return t->type == VehicleType::Train && abs(t->z_pos - u->z_pos) <= 6;
587 })) {
588 RoadVehCrash(v);
589 return true;
590 }
591 }
592
593 return false;
594}
595
597{
598 if (station == this->last_station_visited) this->last_station_visited = StationID::Invalid();
599
600 const Station *st = Station::Get(station);
601 if (!CanVehicleUseStation(this, st)) {
602 /* There is no stop left at the station, so don't even TRY to go there */
604 return TileIndex{};
605 }
606
607 return st->xy;
608}
609
610static void StartRoadVehSound(const RoadVehicle *v)
611{
612 if (!PlayVehicleSound(v, VSE_START)) {
613 SoundID s = RoadVehInfo(v->engine_type)->sfx;
614 if (s == SND_19_DEPARTURE_OLD_RV_1 && (v->tick_counter & 3) == 0) {
616 }
617 SndPlayVehicleFx(s, v);
618 }
619}
620
622 int x;
623 int y;
624 const Vehicle *veh;
625 Vehicle *best;
626 uint best_diff;
627 Direction dir;
628};
629
630static void FindClosestBlockingRoadVeh(Vehicle *v, RoadVehFindData *rvf)
631{
632 static constexpr DirectionIndexArray<int8_t> dist_x{-4, -8, -4, -1, 4, 8, 4, 1};
633 static constexpr DirectionIndexArray<int8_t> dist_y{-4, -1, 4, 8, 4, 1, -4, -8};
634
635 int x_diff = v->x_pos - rvf->x;
636 int y_diff = v->y_pos - rvf->y;
637
638 /* Not a close Road vehicle when it's not a road vehicle, in the depot, or ourself. */
639 if (v->type != VehicleType::Road || v->IsInDepot() || rvf->veh->First() == v->First()) return;
640
641 /* Not close when at a different height or when going in a different direction. */
642 if (abs(v->z_pos - rvf->veh->z_pos) >= 6 || v->direction != rvf->dir) return;
643
644 /* We 'return' the closest vehicle, in distance and then VehicleID as tie-breaker. */
645 uint diff = abs(x_diff) + abs(y_diff);
646 if (diff > rvf->best_diff || (diff == rvf->best_diff && v->index > rvf->best->index)) return;
647
648 auto IsCloseOnAxis = [](int dist, int diff) {
649 if (dist < 0) return diff > dist && diff <= 0;
650 return diff < dist && diff >= 0;
651 };
652
653 if (IsCloseOnAxis(dist_x[v->direction], x_diff) && IsCloseOnAxis(dist_y[v->direction], y_diff)) {
654 rvf->best = v;
655 rvf->best_diff = diff;
656 }
657}
658
659static RoadVehicle *RoadVehFindCloseTo(RoadVehicle *v, int x, int y, Direction dir, bool update_blocked_ctr = true)
660{
661 RoadVehFindData rvf;
662 RoadVehicle *front = v->First();
663
664 if (front->reverse_ctr != 0) return nullptr;
665
666 rvf.x = x;
667 rvf.y = y;
668 rvf.dir = dir;
669 rvf.veh = v;
670 rvf.best_diff = UINT_MAX;
671
672 if (front->state == RVSB_WORMHOLE) {
673 for (Vehicle *u : VehiclesOnTile(v->tile)) {
674 FindClosestBlockingRoadVeh(u, &rvf);
675 }
677 FindClosestBlockingRoadVeh(u, &rvf);
678 }
679 } else {
680 for (Vehicle *u : VehiclesNearTileXY(x, y, 8)) {
681 FindClosestBlockingRoadVeh(u, &rvf);
682 }
683 }
684
685 /* This code protects a roadvehicle from being blocked for ever
686 * If more than 1480 / 74 days a road vehicle is blocked, it will
687 * drive just through it. The ultimate backup-code of TTD.
688 * It can be disabled. */
689 if (rvf.best_diff == UINT_MAX) {
690 front->blocked_ctr = 0;
691 return nullptr;
692 }
693
694 if (update_blocked_ctr && ++front->blocked_ctr > 1480) return nullptr;
695
696 return RoadVehicle::From(rvf.best);
697}
698
704static void RoadVehArrivesAt(const RoadVehicle *v, Station *st)
705{
706 if (v->IsBus()) {
707 /* Check if station was ever visited before */
708 if (!(st->had_vehicle_of_type & HVOT_BUS)) {
709 st->had_vehicle_of_type |= HVOT_BUS;
711 GetEncodedString(RoadTypeIsRoad(v->roadtype) ? STR_NEWS_FIRST_BUS_ARRIVAL : STR_NEWS_FIRST_PASSENGER_TRAM_ARRIVAL, st->index),
713 v->index,
714 st->index
715 );
716 AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
717 Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
718 }
719 } else {
720 /* Check if station was ever visited before */
721 if (!(st->had_vehicle_of_type & HVOT_TRUCK)) {
722 st->had_vehicle_of_type |= HVOT_TRUCK;
724 GetEncodedString(RoadTypeIsRoad(v->roadtype) ? STR_NEWS_FIRST_TRUCK_ARRIVAL : STR_NEWS_FIRST_CARGO_TRAM_ARRIVAL, st->index),
726 v->index,
727 st->index
728 );
729 AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
730 Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
731 }
732 }
733}
734
743{
744 switch (_settings_game.vehicle.roadveh_acceleration_model) {
745 default: NOT_REACHED();
746 case AM_ORIGINAL:
747 return this->DoUpdateSpeed(this->overtaking != 0 ? 512 : 256, 0, this->GetCurrentMaxSpeed());
748
749 case AM_REALISTIC:
750 return this->DoUpdateSpeed(this->GetAcceleration() + (this->overtaking != 0 ? 256 : 0), this->GetAccelerationStatus() == AS_BRAKE ? 0 : 4, this->GetCurrentMaxSpeed());
751 }
752}
753
754static Direction RoadVehGetNewDirection(const RoadVehicle *v, int x, int y)
755{
756 static const Direction _roadveh_new_dir[] = {
760 };
761
762 x = x - v->x_pos + 1;
763 y = y - v->y_pos + 1;
764
765 if ((uint)x > 2 || (uint)y > 2) return v->direction;
766 return _roadveh_new_dir[y * 4 + x];
767}
768
769static Direction RoadVehGetSlidingDirection(const RoadVehicle *v, int x, int y)
770{
771 Direction new_dir = RoadVehGetNewDirection(v, x, y);
772 Direction old_dir = v->direction;
773
774 if (new_dir == old_dir) return old_dir;
775 DirDiff dirdiff = DirDifference(new_dir, old_dir);
776 return ChangeDir(old_dir, LimitDirDiff(dirdiff));
777}
778
780 const RoadVehicle *u;
781 const RoadVehicle *v;
782 TileIndex tile;
783 Trackdir trackdir;
784};
785
793{
794 if (!HasTileAnyRoadType(od->tile, od->v->compatible_roadtypes)) return true;
795 TrackStatus ts = GetTileTrackStatus(od->tile, TRANSPORT_ROAD, GetRoadTramType(od->v->roadtype));
797
798 /* Track does not continue along overtaking direction || track has junction || levelcrossing is barred */
799 if (!HasBit(ts.trackdirs, od->trackdir) || (trackbits & ~TRACK_BIT_CROSS) || (ts.signals != TRACKDIR_BIT_NONE)) return true;
800
801 /* Are there more vehicles on the tile except the two vehicles involved in overtaking */
802 return HasVehicleOnTile(od->tile, [&](const Vehicle *v) {
803 return v->type == VehicleType::Road && v->First() == v && v != od->u && v != od->v;
804 });
805}
806
807static void RoadVehCheckOvertake(RoadVehicle *v, RoadVehicle *u)
808{
809 OvertakeData od;
810
811 od.v = v;
812 od.u = u;
813
814 /* Trams can't overtake other trams */
815 if (RoadTypeIsTram(v->roadtype)) return;
816
817 /* Don't overtake in stations */
819
820 /* For now, articulated road vehicles can't overtake anything. */
821 if (v->HasArticulatedPart()) return;
822
823 /* Vehicles are not driving in same direction || direction is not a diagonal direction */
824 if (v->direction != u->direction || !IsDiagonalDirection(v->direction)) return;
825
826 /* Check if vehicle is in a road stop, depot, tunnel or bridge or not on a straight road */
828
829 /* Can't overtake a vehicle that is moving faster than us. If the vehicle in front is
830 * accelerating, take the maximum speed for the comparison, else the current speed.
831 * Original acceleration always accelerates, so always use the maximum speed. */
832 int u_speed = (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL || u->GetAcceleration() > 0) ? u->GetCurrentMaxSpeed() : u->cur_speed;
833 if (u_speed >= v->GetCurrentMaxSpeed() &&
835 u->cur_speed != 0) {
836 return;
837 }
838
840
841 /* Are the current and the next tile suitable for overtaking?
842 * - Does the track continue along od.trackdir
843 * - No junctions
844 * - No barred levelcrossing
845 * - No other vehicles in the way
846 */
847 od.tile = v->tile;
848 if (CheckRoadBlockedForOvertaking(&od)) return;
849
850 od.tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
851 if (CheckRoadBlockedForOvertaking(&od)) return;
852
853 /* When the vehicle in front of us is stopped we may only take
854 * half the time to pass it than when the vehicle is moving. */
855 v->overtaking_ctr = (od.u->cur_speed == 0 || od.u->vehstatus.Test(VehState::Stopped)) ? RV_OVERTAKE_TIMEOUT / 2 : 0;
857}
858
859static void RoadZPosAffectSpeed(RoadVehicle *v, int old_z)
860{
861 if (old_z == v->z_pos || _settings_game.vehicle.roadveh_acceleration_model != AM_ORIGINAL) return;
862
863 if (old_z < v->z_pos) {
864 v->cur_speed = v->cur_speed * 232 / 256; // slow down by ~10%
865 } else {
866 uint16_t spd = v->cur_speed + 2;
867 if (spd <= v->gcache.cached_max_track_speed) v->cur_speed = spd;
868 }
869}
870
871static int PickRandomBit(uint bits)
872{
873 uint i;
874 uint num = RandomRange(CountBits(bits));
875
876 for (i = 0; !(bits & 1) || (int)--num >= 0; bits >>= 1, i++) {}
877 return i;
878}
879
889{
890 bool path_found = true;
891
892 TrackStatus ts = GetTileTrackStatus(tile, TRANSPORT_ROAD, GetRoadTramType(v->roadtype));
893
894 /* Replaces the given track with INVALID_TRACK when there is red signal for that track. */
895 auto FilterRedSignal = [&ts](auto track) {
896 if (HasBit(ts.signals, track)) return INVALID_TRACKDIR;
897 return static_cast<Trackdir>(track);
898 };
899
900 if (IsTileType(tile, TileType::Road)) {
901 if (IsRoadDepot(tile) && (!IsTileOwner(tile, v->owner) || GetRoadDepotDirection(tile) == enterdir)) {
902 /* Road depot owned by another company or with the wrong orientation */
904 }
905 } else if (IsTileType(tile, TileType::Station) && IsBayRoadStopTile(tile)) {
906 /* Standard road stop (drive-through stops are treated as normal road) */
907
908 if (!IsTileOwner(tile, v->owner) || GetBayRoadStopDir(tile) == enterdir || v->HasArticulatedPart()) {
909 /* different station owner or wrong orientation or the vehicle has articulated parts */
911 } else {
912 /* Our station */
914
915 if (GetRoadStopType(tile) != rstype) {
916 /* Wrong station type */
918 } else {
919 /* Proper station type, check if there is free loading bay */
920 if (!_settings_game.pf.roadveh_queue && IsBayRoadStopTile(tile) &&
921 !RoadStop::GetByTile(tile, rstype)->HasFreeBay()) {
922 /* Station is full and RV queuing is off */
924 }
925 }
926 }
927 }
928 /* The above lookups should be moved to GetTileTrackStatus in the
929 * future, but that requires more changes to the pathfinder and other
930 * stuff, probably even more arguments to GTTS.
931 */
932
933 /* Remove tracks unreachable from the enter dir */
934 ts.trackdirs &= DiagdirReachesTrackdirs(enterdir);
935 if (ts.trackdirs == TRACKDIR_BIT_NONE) {
936 /* If vehicle expected a path, it no longer exists, so invalidate it. */
937 if (!v->path.empty()) v->path.clear();
938 /* No reachable tracks, so we'll reverse */
939 return FilterRedSignal(_road_reverse_table[enterdir]);
940 }
941
942 if (v->reverse_ctr != 0) {
943 bool reverse = true;
944 if (RoadTypeIsTram(v->roadtype)) {
945 /* Trams may only reverse on a tile if it contains at least the straight
946 * trackbits or when it is a valid turning tile (i.e. one roadbit) */
948 RoadBits straight = AxisToRoadBits(DiagDirToAxis(enterdir));
949 reverse = rb.All(straight) ||
950 rb == DiagDirToRoadBits(enterdir);
951 }
952 if (reverse) {
953 v->reverse_ctr = 0;
954 if (v->tile != tile) {
955 return FilterRedSignal(_road_reverse_table[enterdir]);
956 }
957 }
958 }
959
960 if (v->dest_tile == INVALID_TILE) {
961 /* We've got no destination, pick a random track */
962 return FilterRedSignal(PickRandomBit(ts.trackdirs));
963 }
964
965 /* Only one track to choose between? */
967 if (!v->path.empty() && v->path.back().tile == tile) {
968 /* Vehicle expected a choice here, invalidate its path. */
969 v->path.clear();
970 }
971 return FilterRedSignal(FindFirstBit(ts.trackdirs));
972 }
973
974 /* Attempt to follow cached path. */
975 if (!v->path.empty()) {
976 if (v->path.back().tile != tile) {
977 /* Vehicle didn't expect a choice here, invalidate its path. */
978 v->path.clear();
979 } else {
980 Trackdir trackdir = v->path.back().trackdir;
981
982 if (HasBit(ts.trackdirs, trackdir)) {
983 v->path.pop_back();
984 return FilterRedSignal(trackdir);
985 }
986
987 /* Vehicle expected a choice which is no longer available. */
988 v->path.clear();
989 }
990 }
991
992 Trackdir best_track = YapfRoadVehicleChooseTrack(v, tile, enterdir, ts.trackdirs, path_found, v->path);
993
994 v->HandlePathfindingResult(path_found);
995
996 return FilterRedSignal(best_track);
997}
998
1000 uint8_t x, y;
1001};
1002
1003#include "table/roadveh_movement.h"
1004
1005bool RoadVehLeaveDepot(RoadVehicle *v, bool first)
1006{
1007 /* Don't leave unless v and following wagons are in the depot. */
1008 for (const RoadVehicle *u = v; u != nullptr; u = u->Next()) {
1009 if (u->state != RVSB_IN_DEPOT || u->tile != v->tile) return false;
1010 }
1011
1013 v->direction = DiagDirToDir(dir);
1014
1015 Trackdir tdir = DiagDirToDiagTrackdir(dir);
1016 const RoadDriveEntry *rdp = _road_drive_data[GetRoadTramType(v->roadtype)][(_settings_game.vehicle.road_side << RVS_DRIVE_SIDE) + tdir];
1017
1018 int x = TileX(v->tile) * TILE_SIZE + (rdp[RVC_DEPOT_START_FRAME].x & 0xF);
1019 int y = TileY(v->tile) * TILE_SIZE + (rdp[RVC_DEPOT_START_FRAME].y & 0xF);
1020
1021 if (first) {
1022 /* We are leaving a depot, but have to go to the exact same one; re-enter */
1023 if (v->current_order.IsType(OT_GOTO_DEPOT) && v->tile == v->dest_tile) {
1025 return true;
1026 }
1027
1028 if (RoadVehFindCloseTo(v, x, y, v->direction, false) != nullptr) return true;
1029
1032
1033 StartRoadVehSound(v);
1034
1035 /* Vehicle is about to leave a depot */
1036 v->cur_speed = 0;
1037 }
1038
1040 v->state = tdir;
1041 v->frame = RVC_DEPOT_START_FRAME;
1042
1043 v->x_pos = x;
1044 v->y_pos = y;
1045 v->UpdatePosition();
1046 v->UpdateInclination(true, true);
1047
1048 InvalidateWindowData(WindowClass::VehicleDepot, v->tile);
1049
1050 return true;
1051}
1052
1053static Trackdir FollowPreviousRoadVehicle(const RoadVehicle *v, const RoadVehicle *prev, TileIndex tile, DiagDirection entry_dir, bool already_reversed)
1054{
1055 if (prev->tile == v->tile && !already_reversed) {
1056 /* If the previous vehicle is on the same tile as this vehicle is
1057 * then it must have reversed. */
1058 return _road_reverse_table[entry_dir];
1059 }
1060
1061 uint8_t prev_state = prev->state;
1062 Trackdir dir;
1063
1064 if (prev_state == RVSB_WORMHOLE || prev_state == RVSB_IN_DEPOT) {
1066
1068 diag_dir = GetTunnelBridgeDirection(tile);
1069 } else if (IsRoadDepotTile(tile)) {
1070 diag_dir = ReverseDiagDir(GetRoadDepotDirection(tile));
1071 }
1072
1073 if (diag_dir == DiagDirection::Invalid) return INVALID_TRACKDIR;
1074 dir = DiagDirToDiagTrackdir(diag_dir);
1075 } else {
1076 if (already_reversed && (prev->tile != tile || (prev_state < TRACKDIR_END && IsReversingRoadTrackdir(static_cast<Trackdir>(prev_state))))) {
1077 /*
1078 * The vehicle has reversed, but did not go straight back.
1079 * It immediately turn onto another tile. This means that
1080 * the roadstate of the previous vehicle cannot be used
1081 * as the direction we have to go with this vehicle.
1082 *
1083 * Next table is build in the following way:
1084 * - first row for when the vehicle in front went to the northern or
1085 * western tile, second for southern and eastern.
1086 * - columns represent the entry direction.
1087 * - cell values are determined by the Trackdir one has to take from
1088 * the entry dir (column) to the tile in north or south by only
1089 * going over the trackdirs used for turning 90 degrees, i.e.
1090 * TRACKDIR_{UPPER,RIGHT,LOWER,LEFT}_{N,E,S,W}.
1091 */
1092 bool north;
1093 if (prev->tile != tile) {
1094 north = prev->tile < tile;
1095 } else {
1096 north = (prev_state == TRACKDIR_RVREV_NW || prev_state == TRACKDIR_RVREV_NE);
1097 }
1098 static const DiagDirectionIndexArray<Trackdir> reversed_turn_lookup[2] = {
1101 dir = reversed_turn_lookup[north ? 0 : 1][ReverseDiagDir(entry_dir)];
1102 } else if (HasBit(prev_state, RVS_IN_DT_ROAD_STOP)) {
1103 dir = (Trackdir)(prev_state & RVSB_ROAD_STOP_TRACKDIR_MASK);
1104 } else if (prev_state < TRACKDIR_END) {
1105 dir = (Trackdir)prev_state;
1106 } else {
1107 return INVALID_TRACKDIR;
1108 }
1109 }
1110
1111 /* Do some sanity checking. */
1112 static const RoadBits required_roadbits[] = {
1113 ROAD_X,
1114 ROAD_Y,
1119 ROAD_X,
1120 ROAD_Y,
1121 };
1122 RoadBits required = required_roadbits[dir & 0x07];
1123
1124 if (!required.Any(GetAnyRoadBits(tile, GetRoadTramType(v->roadtype), true))) {
1125 dir = INVALID_TRACKDIR;
1126 }
1127
1128 return dir;
1129}
1130
1139static bool CanBuildTramTrackOnTile(CompanyID c, TileIndex t, RoadType rt, RoadBits r)
1140{
1141 /* The 'current' company is not necessarily the owner of the vehicle. */
1142 AutoRestoreBackup cur_company(_current_company, c);
1143 return Command<Commands::BuildRoad>::Do(DoCommandFlag::NoWater, t, r, rt, {}, TownID::Invalid()).Succeeded();
1144}
1145
1146bool IndividualRoadVehicleController(RoadVehicle *v, const RoadVehicle *prev)
1147{
1148 if (v->overtaking != 0) {
1150 /* Force us to be not overtaking! */
1151 v->overtaking = 0;
1152 } else if (++v->overtaking_ctr >= RV_OVERTAKE_TIMEOUT) {
1153 /* If overtaking just aborts at a random moment, we can have a out-of-bound problem,
1154 * if the vehicle started a corner. To protect that, only allow an abort of
1155 * overtake if we are on straight roads */
1157 v->overtaking = 0;
1158 }
1159 }
1160 }
1161
1162 /* If this vehicle is in a depot and we've reached this point it must be
1163 * one of the articulated parts. It will stay in the depot until activated
1164 * by the previous vehicle in the chain when it gets to the right place. */
1165 if (v->IsInDepot()) return true;
1166
1167 if (v->state == RVSB_WORMHOLE) {
1168 /* Vehicle is entering a depot or is on a bridge or in a tunnel */
1170
1171 if (v->IsFrontEngine()) {
1172 const Vehicle *u = RoadVehFindCloseTo(v, gp.x, gp.y, v->direction);
1173 if (u != nullptr) {
1174 v->cur_speed = u->First()->cur_speed;
1175 return false;
1176 }
1177 }
1178
1180 /* Vehicle has just entered a bridge or tunnel */
1181 v->x_pos = gp.x;
1182 v->y_pos = gp.y;
1183 v->UpdatePosition();
1184 v->UpdateInclination(true, true);
1185 return true;
1186 }
1187
1188 v->x_pos = gp.x;
1189 v->y_pos = gp.y;
1190 v->UpdatePosition();
1191 if (!v->vehstatus.Test(VehState::Hidden)) v->Vehicle::UpdateViewport(true);
1192 return true;
1193 }
1194
1195 /* Get move position data for next frame.
1196 * For a drive-through road stop use 'straight road' move data.
1197 * In this case v->state is masked to give the road stop entry direction. */
1198 RoadDriveEntry rd = _road_drive_data[GetRoadTramType(v->roadtype)][(
1200 (_settings_game.vehicle.road_side << RVS_DRIVE_SIDE)) ^ v->overtaking][v->frame + 1];
1201
1202 if (rd.x & RDE_NEXT_TILE) {
1203 DiagDirection diagdir = static_cast<DiagDirection>(rd.x & 3);
1204 TileIndex tile = v->tile + TileOffsByDiagDir(diagdir);
1205 Trackdir dir;
1206
1207 if (v->IsFrontEngine()) {
1208 /* If this is the front engine, look for the right path. */
1210 dir = RoadFindPathToDest(v, tile, diagdir);
1211 } else {
1212 dir = _road_reverse_table[diagdir];
1213 }
1214 } else {
1215 dir = FollowPreviousRoadVehicle(v, prev, tile, diagdir, false);
1216 }
1217
1218 if (dir == INVALID_TRACKDIR) {
1219 if (!v->IsFrontEngine()) FatalError("Disconnecting road vehicle.");
1220 v->cur_speed = 0;
1221 return false;
1222 }
1223
1224again:
1225 uint start_frame = RVC_DEFAULT_START_FRAME;
1226 if (IsReversingRoadTrackdir(dir)) {
1227 /* When turning around we can't be overtaking. */
1228 v->overtaking = 0;
1229
1230 /* Turning around */
1231 if (RoadTypeIsTram(v->roadtype)) {
1232 /* Determine the road bits the tram needs to be able to turn around
1233 * using the 'big' corner loop. */
1234 RoadBit needed;
1235 switch (dir) {
1236 default: NOT_REACHED();
1237 case TRACKDIR_RVREV_NE: needed = RoadBit::SW; break;
1238 case TRACKDIR_RVREV_SE: needed = RoadBit::NW; break;
1239 case TRACKDIR_RVREV_SW: needed = RoadBit::NE; break;
1240 case TRACKDIR_RVREV_NW: needed = RoadBit::SE; break;
1241 }
1242 if ((v->Previous() != nullptr && v->Previous()->tile == tile) ||
1243 (v->IsFrontEngine() && IsNormalRoadTile(tile) && !HasRoadWorks(tile) &&
1245 GetRoadBits(tile, RoadTramType::Tram).Test(needed))) {
1246 /*
1247 * Taking the 'big' corner for trams only happens when:
1248 * - The previous vehicle in this (articulated) tram chain is
1249 * already on the 'next' tile, we just follow them regardless of
1250 * anything. When it is NOT on the 'next' tile, the tram started
1251 * doing a reversing turn when the piece of tram track on the next
1252 * tile did not exist yet. Do not use the big tram loop as that is
1253 * going to cause the tram to split up.
1254 * - Or the front of the tram can drive over the next tile.
1255 */
1256 } else if (!v->IsFrontEngine() || !CanBuildTramTrackOnTile(v->owner, tile, v->roadtype, needed) || GetAnyRoadBits(v->tile, RoadTramType::Tram, false).Reset(needed).Any()) {
1257 /*
1258 * Taking the 'small' corner for trams only happens when:
1259 * - We are not the from vehicle of an articulated tram.
1260 * - Or when the company cannot build on the next tile.
1261 *
1262 * The 'small' corner means that the vehicle is on the end of a
1263 * tram track and needs to start turning there. To do this properly
1264 * the tram needs to start at an offset in the tram turning 'code'
1265 * for 'big' corners. It furthermore does not go to the next tile,
1266 * so that needs to be fixed too.
1267 */
1268 tile = v->tile;
1269 start_frame = RVC_TURN_AROUND_START_FRAME_SHORT_TRAM;
1270 } else {
1271 /* The company can build on the next tile, so wait till they do. */
1272 v->cur_speed = 0;
1273 return false;
1274 }
1275 } else if (IsNormalRoadTile(v->tile) && GetDisallowedRoadDirections(v->tile).Any()) {
1276 v->cur_speed = 0;
1277 return false;
1278 } else {
1279 tile = v->tile;
1280 }
1281 }
1282
1283 /* Get position data for first frame on the new tile */
1284 const RoadDriveEntry *rdp = _road_drive_data[GetRoadTramType(v->roadtype)][(dir + (_settings_game.vehicle.road_side << RVS_DRIVE_SIDE)) ^ v->overtaking];
1285
1286 int x = TileX(tile) * TILE_SIZE + rdp[start_frame].x;
1287 int y = TileY(tile) * TILE_SIZE + rdp[start_frame].y;
1288
1289 Direction new_dir = RoadVehGetSlidingDirection(v, x, y);
1290 if (v->IsFrontEngine()) {
1291 const Vehicle *u = RoadVehFindCloseTo(v, x, y, new_dir);
1292 if (u != nullptr) {
1293 v->cur_speed = u->First()->cur_speed;
1294 /* We might be blocked, prevent pathfinding rerun as we already know where we are heading to. */
1295 v->path.emplace_back(dir, tile);
1296 return false;
1297 }
1298 }
1299
1300 auto vets = VehicleEnterTile(v, tile, x, y);
1301 if (vets.Test(VehicleEnterTileState::CannotEnter)) {
1302 if (!IsTileType(tile, TileType::TunnelBridge)) {
1303 v->cur_speed = 0;
1304 return false;
1305 }
1306 /* Try an about turn to re-enter the previous tile */
1307 dir = _road_reverse_table[static_cast<DiagDirection>(rd.x & 3)];
1308 goto again;
1309 }
1310
1311 if (IsInsideMM(v->state, RVSB_IN_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) && IsTileType(v->tile, TileType::Station)) {
1312 if (IsReversingRoadTrackdir(dir) && IsInsideMM(v->state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END)) {
1313 /* New direction is trying to turn vehicle around.
1314 * We can't turn at the exit of a road stop so wait.*/
1315 v->cur_speed = 0;
1316 return false;
1317 }
1318
1319 /* If we are a drive through road stop and the next tile is of
1320 * the same road stop and the next tile isn't this one (i.e. we
1321 * are not reversing), then keep the reservation and state.
1322 * This way we will not be shortly unregister from the road
1323 * stop. It also makes it possible to load when on the edge of
1324 * two road stops; otherwise you could get vehicles that should
1325 * be loading but are not actually loading. */
1326 if (IsDriveThroughStopTile(v->tile) &&
1328 v->tile != tile) {
1329 /* So, keep 'our' state */
1330 dir = (Trackdir)v->state;
1331 } else if (IsStationRoadStop(v->tile)) {
1332 /* We're not continuing our drive through road stop, so leave. */
1334 }
1335 }
1336
1338 TileIndex old_tile = v->tile;
1339
1340 v->tile = tile;
1341 v->state = (uint8_t)dir;
1342 v->frame = start_frame;
1343 RoadTramType rtt = GetRoadTramType(v->roadtype);
1344 if (GetRoadType(old_tile, rtt) != GetRoadType(tile, rtt)) {
1345 if (v->IsFrontEngine()) {
1347 }
1348 v->First()->CargoChanged();
1349 }
1350 }
1351 if (new_dir != v->direction) {
1352 v->direction = new_dir;
1353 if (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL) v->cur_speed -= v->cur_speed >> 2;
1354 }
1355 v->x_pos = x;
1356 v->y_pos = y;
1357 v->UpdatePosition();
1358 RoadZPosAffectSpeed(v, v->UpdateInclination(true, true));
1359 return true;
1360 }
1361
1362 if (rd.x & RDE_TURNED) {
1363 /* Vehicle has finished turning around, it will now head back onto the same tile */
1364 Trackdir dir;
1365 uint turn_around_start_frame = RVC_TURN_AROUND_START_FRAME;
1366
1367 if (RoadTypeIsTram(v->roadtype) && !IsRoadDepotTile(v->tile) && GetAnyRoadBits(v->tile, RoadTramType::Tram, true).Count() == 1) {
1368 /*
1369 * The tram is turning around with one tram 'roadbit'. This means that
1370 * it is using the 'big' corner 'drive data'. However, to support the
1371 * trams to take a small corner, there is a 'turned' marker in the middle
1372 * of the turning 'drive data'. When the tram took the long corner, we
1373 * will still use the 'big' corner drive data, but we advance it one
1374 * frame. We furthermore set the driving direction so the turning is
1375 * going to be properly shown.
1376 */
1377 turn_around_start_frame = RVC_START_FRAME_AFTER_LONG_TRAM;
1378 switch (static_cast<DiagDirection>(rd.x & 0x3)) {
1379 default: NOT_REACHED();
1380 case DiagDirection::NW: dir = TRACKDIR_RVREV_SE; break;
1381 case DiagDirection::NE: dir = TRACKDIR_RVREV_SW; break;
1382 case DiagDirection::SE: dir = TRACKDIR_RVREV_NW; break;
1383 case DiagDirection::SW: dir = TRACKDIR_RVREV_NE; break;
1384 }
1385 } else {
1386 if (v->IsFrontEngine()) {
1387 /* If this is the front engine, look for the right path. */
1388 dir = RoadFindPathToDest(v, v->tile, (DiagDirection)(rd.x & 3));
1389 } else {
1390 dir = FollowPreviousRoadVehicle(v, prev, v->tile, (DiagDirection)(rd.x & 3), true);
1391 }
1392 }
1393
1394 if (dir == INVALID_TRACKDIR) {
1395 v->cur_speed = 0;
1396 return false;
1397 }
1398
1399 const RoadDriveEntry *rdp = _road_drive_data[GetRoadTramType(v->roadtype)][(_settings_game.vehicle.road_side << RVS_DRIVE_SIDE) + dir];
1400
1401 int x = TileX(v->tile) * TILE_SIZE + rdp[turn_around_start_frame].x;
1402 int y = TileY(v->tile) * TILE_SIZE + rdp[turn_around_start_frame].y;
1403
1404 Direction new_dir = RoadVehGetSlidingDirection(v, x, y);
1405 if (v->IsFrontEngine()) {
1406 const Vehicle *u = RoadVehFindCloseTo(v, x, y, new_dir);
1407 if (u != nullptr) {
1408 v->cur_speed = u->First()->cur_speed;
1409 /* We might be blocked, prevent pathfinding rerun as we already know where we are heading to. */
1410 v->path.emplace_back(dir, v->tile);
1411 return false;
1412 }
1413 }
1414
1415 auto vets = VehicleEnterTile(v, v->tile, x, y);
1416 if (vets.Test(VehicleEnterTileState::CannotEnter)) {
1417 v->cur_speed = 0;
1418 return false;
1419 }
1420
1421 v->state = dir;
1422 v->frame = turn_around_start_frame;
1423
1424 if (new_dir != v->direction) {
1425 v->direction = new_dir;
1426 if (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL) v->cur_speed -= v->cur_speed >> 2;
1427 }
1428
1429 v->x_pos = x;
1430 v->y_pos = y;
1431 v->UpdatePosition();
1432 RoadZPosAffectSpeed(v, v->UpdateInclination(true, true));
1433 return true;
1434 }
1435
1436 /* This vehicle is not in a wormhole and it hasn't entered a new tile. If
1437 * it's on a depot tile, check if it's time to activate the next vehicle in
1438 * the chain yet. */
1439 if (v->Next() != nullptr && IsRoadDepotTile(v->tile)) {
1440 if (v->frame == v->gcache.cached_veh_length + RVC_DEPOT_START_FRAME) {
1441 RoadVehLeaveDepot(v->Next(), false);
1442 }
1443 }
1444
1445 /* Calculate new position for the vehicle */
1446 int x = (v->x_pos & ~15) + (rd.x & 15);
1447 int y = (v->y_pos & ~15) + (rd.y & 15);
1448
1449 Direction new_dir = RoadVehGetSlidingDirection(v, x, y);
1450
1451 if (v->IsFrontEngine() && !IsInsideMM(v->state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END)) {
1452 /* Vehicle is not in a road stop.
1453 * Check for another vehicle to overtake */
1454 RoadVehicle *u = RoadVehFindCloseTo(v, x, y, new_dir);
1455
1456 if (u != nullptr) {
1457 u = u->First();
1458 /* There is a vehicle in front overtake it if possible */
1459 if (v->overtaking == 0) RoadVehCheckOvertake(v, u);
1460 if (v->overtaking == 0) v->cur_speed = u->cur_speed;
1461
1462 /* In case an RV is stopped in a road stop, why not try to load? */
1463 if (v->cur_speed == 0 && IsInsideMM(v->state, RVSB_IN_DT_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) &&
1465 v->owner == GetTileOwner(v->tile) && !v->current_order.IsType(OT_LEAVESTATION) &&
1468 v->last_station_visited = st->index;
1469 RoadVehArrivesAt(v, st);
1470 v->BeginLoading();
1472 TriggerRoadStopAnimation(st, v->tile, StationAnimationTrigger::VehicleArrives);
1473 }
1474 return false;
1475 }
1476 }
1477
1478 Direction old_dir = v->direction;
1479 if (new_dir != old_dir) {
1480 v->direction = new_dir;
1481 if (_settings_game.vehicle.roadveh_acceleration_model == AM_ORIGINAL) v->cur_speed -= v->cur_speed >> 2;
1482
1483 /* Delay the vehicle in curves by making it require one additional frame per turning direction (two in total).
1484 * A vehicle has to spend at least 9 frames on a tile, so the following articulated part can follow.
1485 * (The following part may only be one tile behind, and the front part is moved before the following ones.)
1486 * The short (inner) curve has 8 frames, this elongates it to 10. */
1487 v->UpdateViewport(true, true);
1488 return true;
1489 }
1490
1491 /* If the vehicle is in a normal road stop and the frame equals the stop frame OR
1492 * if the vehicle is in a drive-through road stop and this is the destination station
1493 * and it's the correct type of stop (bus or truck) and the frame equals the stop frame...
1494 * (the station test and stop type test ensure that other vehicles, using the road stop as
1495 * a through route, do not stop) */
1496 if (v->IsFrontEngine() && ((IsInsideMM(v->state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END) &&
1497 _road_stop_stop_frame[v->state - RVSB_IN_ROAD_STOP + (_settings_game.vehicle.road_side << RVS_DRIVE_SIDE)] == v->frame) ||
1498 (IsInsideMM(v->state, RVSB_IN_DT_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) &&
1500 v->owner == GetTileOwner(v->tile) &&
1502 v->frame == RVC_DRIVE_THROUGH_STOP_FRAME))) {
1503
1506
1507 /* Vehicle is at the stop position (at a bay) in a road stop.
1508 * Note, if vehicle is loading/unloading it has already been handled,
1509 * so if we get here the vehicle has just arrived or is just ready to leave. */
1510 if (!HasBit(v->state, RVS_ENTERED_STOP)) {
1511 /* Vehicle has arrived at a bay in a road stop */
1512
1513 if (IsDriveThroughStopTile(v->tile)) {
1514 TileIndex next_tile = TileAddByDir(v->tile, v->direction);
1515
1516 /* Check if next inline bay is free and has compatible road. */
1518 v->frame++;
1519 v->x_pos = x;
1520 v->y_pos = y;
1521 v->UpdatePosition();
1522 RoadZPosAffectSpeed(v, v->UpdateInclination(true, false));
1523 return true;
1524 }
1525 }
1526
1527 rs->SetEntranceBusy(false);
1529
1530 v->last_station_visited = st->index;
1531
1532 if (IsDriveThroughStopTile(v->tile) || (v->current_order.IsType(OT_GOTO_STATION) && v->current_order.GetDestination() == st->index)) {
1533 RoadVehArrivesAt(v, st);
1534 v->BeginLoading();
1536 TriggerRoadStopAnimation(st, v->tile, StationAnimationTrigger::VehicleArrives);
1537 return false;
1538 }
1539 } else {
1540 /* Vehicle is ready to leave a bay in a road stop */
1541 if (rs->IsEntranceBusy()) {
1542 /* Road stop entrance is busy, so wait as there is nowhere else to go */
1543 v->cur_speed = 0;
1544 return false;
1545 }
1546 if (v->current_order.IsType(OT_LEAVESTATION)) v->current_order.Free();
1547 }
1548
1549 if (IsBayRoadStopTile(v->tile)) rs->SetEntranceBusy(true);
1550
1551 StartRoadVehSound(v);
1552 SetWindowWidgetDirty(WindowClass::VehicleView, v->index, WID_VV_START_STOP);
1553 }
1554
1555 /* Check tile position conditions - i.e. stop position in depot,
1556 * entry onto bridge or into tunnel */
1557 auto vets = VehicleEnterTile(v, v->tile, x, y);
1558 if (vets.Test(VehicleEnterTileState::CannotEnter)) {
1559 v->cur_speed = 0;
1560 return false;
1561 }
1562
1563 if (v->current_order.IsType(OT_LEAVESTATION) && IsDriveThroughStopTile(v->tile)) {
1564 v->current_order.Free();
1565 }
1566
1567 /* Move to next frame unless vehicle arrived at a stop position
1568 * in a depot or entered a tunnel/bridge */
1569 if (!vets.Test(VehicleEnterTileState::EnteredWormhole)) v->frame++;
1570 v->x_pos = x;
1571 v->y_pos = y;
1572 v->UpdatePosition();
1573 RoadZPosAffectSpeed(v, v->UpdateInclination(false, true));
1574 return true;
1575}
1576
1577static bool RoadVehController(RoadVehicle *v)
1578{
1579 /* decrease counters */
1580 v->current_order_time++;
1581 if (v->reverse_ctr != 0) v->reverse_ctr--;
1582
1583 /* handle crashed */
1584 if (v->vehstatus.Test(VehState::Crashed) || RoadVehCheckTrainCrash(v)) {
1585 return RoadVehIsCrashed(v);
1586 }
1587
1588 /* road vehicle has broken down? */
1589 if (v->HandleBreakdown()) return true;
1591 v->SetLastSpeed();
1592 return true;
1593 }
1594
1595 ProcessOrders(v);
1596 v->HandleLoading();
1597
1598 if (v->current_order.IsType(OT_LOADING)) return true;
1599
1600 if (v->IsInDepot()) {
1601 /* Check if we should wait here for unbunching. */
1602 if (v->IsWaitingForUnbunching()) return true;
1603 if (RoadVehLeaveDepot(v, true)) return true;
1604 }
1605
1606 v->ShowVisualEffect();
1607
1608 /* Check how far the vehicle needs to proceed */
1609 int j = v->UpdateSpeed();
1610
1611 int adv_spd = v->GetAdvanceDistance();
1612 bool blocked = false;
1613 while (j >= adv_spd) {
1614 j -= adv_spd;
1615
1616 RoadVehicle *u = v;
1617 for (RoadVehicle *prev = nullptr; u != nullptr; prev = u, u = u->Next()) {
1618 if (!IndividualRoadVehicleController(u, prev)) {
1619 blocked = true;
1620 break;
1621 }
1622 }
1623 if (blocked) break;
1624
1625 /* Determine distance to next map position */
1626 adv_spd = v->GetAdvanceDistance();
1627
1628 /* Test for a collision, but only if another movement will occur. */
1629 if (j >= adv_spd && RoadVehCheckTrainCrash(v)) break;
1630 }
1631
1632 v->SetLastSpeed();
1633
1634 for (RoadVehicle *u = v; u != nullptr; u = u->Next()) {
1635 if (u->vehstatus.Test(VehState::Hidden)) continue;
1636
1637 u->UpdateViewport(false, false);
1638 }
1639
1640 /* If movement is blocked, set 'progress' to its maximum, so the roadvehicle does
1641 * not accelerate again before it can actually move. I.e. make sure it tries to advance again
1642 * on next tick to discover whether it is still blocked. */
1643 if (v->progress == 0) v->progress = blocked ? adv_spd - 1 : j;
1644
1645 return true;
1646}
1647
1649{
1650 const Engine *e = this->GetEngine();
1651 if (e->VehInfo<RoadVehicleInfo>().running_cost_class == Price::Invalid) return 0;
1652
1653 uint cost_factor = GetVehicleProperty(this, PROP_ROADVEH_RUNNING_COST_FACTOR, e->VehInfo<RoadVehicleInfo>().running_cost);
1654 if (cost_factor == 0) return 0;
1655
1656 return GetPrice(e->VehInfo<RoadVehicleInfo>().running_cost_class, cost_factor, e->GetGRF());
1657}
1658
1660{
1662
1663 this->tick_counter++;
1664
1665 if (this->IsFrontEngine()) {
1666 if (!this->vehstatus.Test(VehState::Stopped)) this->running_ticks++;
1667 return RoadVehController(this);
1668 }
1669
1670 return true;
1671}
1672
1674{
1675 if (tile == this->dest_tile) return;
1676 this->path.clear();
1677 this->dest_tile = tile;
1678}
1679
1680static void CheckIfRoadVehNeedsService(RoadVehicle *v)
1681{
1682 /* If we already got a slot at a stop, use that FIRST, and go to a depot later */
1683 if (Company::Get(v->owner)->settings.vehicle.servint_roadveh == 0 || !v->NeedsAutomaticServicing()) return;
1684 if (v->IsChainInDepot()) {
1686 return;
1687 }
1688
1689 uint max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty;
1690
1691 FindDepotData rfdd = FindClosestRoadDepot(v, max_penalty);
1692 /* Only go to the depot if it is not too far out of our way. */
1693 if (rfdd.best_length == UINT_MAX || rfdd.best_length > max_penalty) {
1694 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
1695 /* If we were already heading for a depot but it has
1696 * suddenly moved farther away, we continue our normal
1697 * schedule? */
1699 SetWindowWidgetDirty(WindowClass::VehicleView, v->index, WID_VV_START_STOP);
1700 }
1701 return;
1702 }
1703
1704 DepotID depot = GetDepotIndex(rfdd.tile);
1705
1706 if (v->current_order.IsType(OT_GOTO_DEPOT) &&
1708 !Chance16(1, 20)) {
1709 return;
1710 }
1711
1714 v->SetDestTile(rfdd.tile);
1715 SetWindowWidgetDirty(WindowClass::VehicleView, v->index, WID_VV_START_STOP);
1716}
1717
1720{
1721 if (!this->IsFrontEngine()) return;
1722 AgeVehicle(this);
1723}
1724
1727{
1728 if (!this->IsFrontEngine()) return;
1729 EconomyAgeVehicle(this);
1730
1731 if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
1732 if (this->blocked_ctr == 0) CheckVehicleBreakdown(this);
1733
1734 CheckIfRoadVehNeedsService(this);
1735
1736 CheckOrders(this);
1737
1738 if (this->running_ticks == 0) return;
1739
1741
1742 this->profit_this_year -= cost.GetCost();
1743 this->running_ticks = 0;
1744
1746
1747 SetWindowDirty(WindowClass::VehicleDetails, this->index);
1748 SetWindowClassesDirty(WindowClass::RoadVehicleList);
1749}
1750
1752{
1753 if (this->vehstatus.Test(VehState::Crashed)) return INVALID_TRACKDIR;
1754
1755 if (this->IsInDepot()) {
1756 /* We'll assume the road vehicle is facing outwards */
1757 return DiagDirToDiagTrackdir(GetRoadDepotDirection(this->tile));
1758 }
1759
1760 if (IsBayRoadStopTile(this->tile)) {
1761 /* We'll assume the road vehicle is facing outwards */
1762 return DiagDirToDiagTrackdir(GetBayRoadStopDir(this->tile)); // Road vehicle in a station
1763 }
1764
1765 /* Drive through road stops / wormholes (tunnels) */
1767
1768 /* If vehicle's state is a valid track direction (vehicle is not turning around) return it,
1769 * otherwise transform it into a valid track direction */
1770 return (Trackdir)((IsReversingRoadTrackdir((Trackdir)this->state)) ? (this->state - 6) : this->state);
1771}
1772
1774{
1775 uint16_t weight = CargoSpec::Get(this->cargo_type)->WeightOfNUnits(this->GetEngine()->DetermineCapacity(this));
1776
1777 /* Vehicle weight is not added for articulated parts. */
1778 if (!this->IsArticulatedPart()) {
1779 /* Road vehicle weight is in units of 1/4 t. */
1780 weight += GetVehicleProperty(this, PROP_ROADVEH_WEIGHT, RoadVehInfo(this->engine_type)->weight) / 4;
1781 }
1782
1783 return weight;
1784}
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.
Class for backupping variables and making sure they are restored later.
@ BuiltAsPrototype
Vehicle is a prototype (accepted as exclusive preview).
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 uint CountBits(T value)
Counts the number of set bits in a variable.
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T KillFirstBit(T value)
Clear the first bit in an integer.
const BridgeSpec * GetBridgeSpec(BridgeType i)
Get the specification of a bridge type.
Definition bridge.h:60
BridgeType GetBridgeType(Tile t)
Determines the type of bridge on a tile.
Definition bridge_map.h:56
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:110
@ Passengers
Passengers.
Definition cargotype.h:51
bool IsCargoInClass(CargoType cargo, CargoClasses cc)
Does cargo c have cargo class cc?
Definition cargotype.h:236
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
Definition ai_core.cpp:221
constexpr bool All(const Timpl &other) const
Test if all of the values are set.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Reset()
Reset all bits.
constexpr bool Any(const Timpl &other) const
Test if any of the given values are set.
Common return value for all commands.
Money GetCost() const
The costs as made up to this moment.
bool Failed() const
Did this command fail?
Container for an encoded string, created by GetEncodedString.
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.
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.
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).
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.
@ NoWater
don't allow building on water
@ Execute
execute the given command
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.
Map related accessors for depots.
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.
Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
bool IsDiagonalDirection(Direction dir)
Checks if a given Direction is diagonal.
Axis DiagDirToAxis(DiagDirection d)
Convert a DiagDirection to the axis.
DirDiff LimitDirDiff(DirDiff d)
Limit a direction difference to up to 45 degrees.
DiagDirection DirToDiagDir(Direction dir)
Convert a Direction to a DiagDirection.
DirDiff
Enumeration for the difference between two directions.
@ Left45
Angle of 45 degrees left.
@ Same
Both directions faces to the same direction.
@ Right45
Angle of 45 degrees right.
EnumIndexArray< T, DiagDirection, DiagDirection::End > DiagDirectionIndexArray
Array with DiagDirection as index.
Direction
Defines the 8 directions on the map.
@ Invalid
Flag for an invalid direction.
@ SW
Southwest.
@ NW
Northwest.
@ NE
Northeast.
@ SE
Southeast.
EnumIndexArray< T, Direction, Direction::End > DirectionIndexArray
Array with Direction as index.
DiagDirection
Enumeration for diagonal directions.
@ Invalid
Flag for an invalid DiagDirection.
@ SW
Southwest.
@ NW
Northwest.
@ NE
Northeast, upper right on your monitor.
@ SE
Southeast.
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition economy.cpp:938
@ RoadVehRun
Running costs road 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_LARGE
Various explosions.
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.
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
Error reporting related functions.
fluid_settings_t * settings
FluidSynth settings handle.
Types for recording game performance data.
@ PFE_GL_ROADVEHS
Time spend processing road vehicles.
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.
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.
TileIndex TileAddByDir(TileIndex tile, Direction dir)
Adds a Direction to a tile.
Definition map_func.h:603
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
constexpr bool IsInsideMM(const size_t x, const size_t min, const size_t max) noexcept
Checks if a value is in an interval.
constexpr T abs(const T a)
Returns the absolute value of (scalar) variable.
Definition math_func.hpp:23
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
Base for the NewGRF implementation.
@ CBID_VEHICLE_LENGTH
Vehicle length, returns the amount of 1/8's the vehicle is shorter for trains and RVs.
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.
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.
@ PROP_ROADVEH_CARGO_AGE_PERIOD
Number of ticks before carried cargo is aged.
@ PROP_ROADVEH_WEIGHT
Weight in 1/4 t.
@ PROP_ROADVEH_RUNNING_COST_FACTOR
Yearly runningcost.
@ PROP_ROADVEH_SHORTEN_FACTOR
Shorter vehicles.
@ PROP_ROADVEH_SPEED
Max. speed: 1 unit = 1/0.8 mph = 2 km-ish/h.
void TriggerRoadStopRandomisation(BaseStation *st, TileIndex tile, StationRandomTrigger trigger, CargoType cargo_type)
Trigger road stop randomisation.
NewGRF definitions and structures for road stops.
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.
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
NewsType
Type of news.
Definition news_type.h:29
@ ArrivalCompany
First vehicle arrived for company.
Definition news_type.h:30
@ AccidentOther
An accident or disaster has occurred.
Definition news_type.h:33
@ ArrivalOther
First vehicle arrived for competitor.
Definition news_type.h:31
@ Accident
An accident or disaster has occurred.
Definition news_type.h:32
bool ProcessOrders(Vehicle *v)
Handle the orders of a vehicle and determine the next place to go to if needed.
void CheckOrders(const Vehicle *v)
Check the orders of a vehicle, to see if there are invalid orders and stuff.
@ NonStop
The vehicle will not stop at any stations it passes except the destination, aka non-stop.
Definition order_type.h:88
@ Service
This depot order is because of the servicing limit.
Definition order_type.h:108
Pseudo random number generator.
uint32_t RandomRange(uint32_t limit, const std::source_location location=std::source_location::current())
Pick a random number between 0 and limit - 1, inclusive.
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.
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition road.h:215
Road related functions.
RoadBits AxisToRoadBits(Axis a)
Create the road-part which belongs to the given Axis.
Definition road_func.h:93
RoadBits DiagDirToRoadBits(DiagDirection d)
Create the road-part which belongs to the given DiagDirection.
Definition road_func.h:78
RoadBits GetAnyRoadBits(Tile tile, RoadTramType rtt, bool straight_tunnel_bridge_entrance)
Returns the RoadBits on an arbitrary tile Special behaviour:
Definition road_map.cpp:54
bool IsLevelCrossingTile(Tile t)
Return whether a tile is a level crossing tile.
Definition road_map.h:79
RoadBits GetRoadBits(Tile t, RoadTramType rtt)
Get the present road bits for a specific road type.
Definition road_map.h:112
static bool IsRoadDepotTile(Tile t)
Return whether a tile is a road depot tile.
Definition road_map.h:100
bool HasTileAnyRoadType(Tile t, RoadTypes rts)
Check if a tile has one of the specified road types.
Definition road_map.h:232
DisallowedRoadDirections GetDisallowedRoadDirections(Tile t)
Gets the disallowed directions.
Definition road_map.h:311
DiagDirection GetRoadDepotDirection(Tile t)
Get the direction of the exit of a road depot.
Definition road_map.h:576
static bool IsRoadDepot(Tile t)
Return whether a tile is a road depot.
Definition road_map.h:90
RoadType GetRoadType(Tile t, RoadTramType rtt)
Get the road type for the given RoadTramType.
Definition road_map.h:175
static bool IsNormalRoadTile(Tile t)
Return whether a tile is a normal road tile.
Definition road_map.h:58
bool HasRoadWorks(Tile t)
Check if a tile has road works.
Definition road_map.h:519
static constexpr RoadBits ROAD_X
Full road along the x-axis (south-west + north-east).
Definition road_type.h:66
EnumBitSet< RoadBit, uint8_t > RoadBits
Bitset of RoadBit elements.
Definition road_type.h:64
RoadBit
Enumeration for the road parts on a tile.
Definition road_type.h:56
@ SW
South-west part.
Definition road_type.h:58
@ NW
North-west part.
Definition road_type.h:57
@ NE
North-east part.
Definition road_type.h:60
@ SE
South-east part.
Definition road_type.h:59
static constexpr RoadBits ROAD_Y
Full road along the y-axis (north-west + south-east).
Definition road_type.h:67
RoadType
The different roadtypes we support.
Definition road_type.h:23
RoadTramType
The different types of road type.
Definition road_type.h:37
@ Tram
Tram type.
Definition road_type.h:39
Base class for roadstops.
Road vehicle states.
@ RVSB_IN_DT_ROAD_STOP
The vehicle is in a drive-through road stop.
Definition roadveh.h:51
@ RVS_ENTERED_STOP
Only set when a vehicle has entered the stop.
Definition roadveh.h:43
@ RVSB_IN_ROAD_STOP
The vehicle is in a road stop.
Definition roadveh.h:49
@ RVSB_ROAD_STOP_TRACKDIR_MASK
Only bits 0 and 3 are used to encode the trackdir for road stops.
Definition roadveh.h:57
@ RVS_IN_DT_ROAD_STOP
The vehicle is in a drive-through road stop.
Definition roadveh.h:46
@ RVSB_TRACKDIR_MASK
The mask used to extract track dirs.
Definition roadveh.h:56
@ RVSB_DRIVE_SIDE
The vehicle is at the opposite side of the road.
Definition roadveh.h:54
@ RVSB_IN_DEPOT
The vehicle is in a depot.
Definition roadveh.h:38
@ RVSB_WORMHOLE
The vehicle is in a tunnel and/or bridge.
Definition roadveh.h:39
@ RVS_DRIVE_SIDE
Only used when retrieving move data.
Definition roadveh.h:44
static const uint RDE_TURNED
We just finished turning.
Definition roadveh.h:63
static const uint RVC_DRIVE_THROUGH_STOP_FRAME
Stop frame for a vehicle in a drive-through stop.
Definition roadveh.h:82
static const uint8_t RV_OVERTAKE_TIMEOUT
The number of ticks a vehicle has for overtaking.
Definition roadveh.h:86
static const uint RDE_NEXT_TILE
We should enter the next tile.
Definition roadveh.h:62
bool IsValidImageIndex< VehicleType::Road >(uint8_t image_index)
Helper to check whether an image index is valid for a particular vehicle.
static constexpr DiagDirectionIndexArray< Trackdir > _road_reverse_table
Track direction to use when reversing for each diagonal direction.
static bool RoadVehIsCrashed(RoadVehicle *v)
Road vehicle chain has crashed.
static uint GetRoadVehLength(const RoadVehicle *v)
Get length of a road vehicle.
static void RoadVehArrivesAt(const RoadVehicle *v, Station *st)
A road vehicle arrives at a station.
static bool CheckRoadBlockedForOvertaking(OvertakeData *od)
Check if overtaking is possible on a piece of track.
void RoadVehUpdateCache(RoadVehicle *v, bool same_length)
Update the cache of a road vehicle.
static bool CanBuildTramTrackOnTile(CompanyID c, TileIndex t, RoadType rt, RoadBits r)
Can a tram track build without destruction on the given tile?
void GetRoadVehSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
Get the size of the sprite of a road vehicle sprite heading west (used for lists).
CommandCost CmdBuildRoadVehicle(DoCommandFlags flags, TileIndex tile, const Engine *e, Vehicle **ret)
Build a road vehicle.
CommandCost CmdTurnRoadVeh(DoCommandFlags flags, VehicleID veh_id)
Turn a roadvehicle around.
void DrawRoadVehEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
Draw a road vehicle engine.
static Trackdir RoadFindPathToDest(RoadVehicle *v, TileIndex tile, DiagDirection enterdir)
Returns direction to for a road vehicle to take or INVALID_TRACKDIR if the direction is currently blo...
static void DeleteLastRoadVeh(RoadVehicle *v)
Delete last vehicle of a chain road vehicles.
Command definitions related to road vehicles.
Data about how a road vehicle must drive on a tile.
static const EnumIndexArray< const RoadDriveEntry *const *, RoadTramType, RoadTramType::End > _road_drive_data
Road drive data for all RoadTramTypes.
const uint8_t _road_stop_stop_frame[]
Table of road stop stop frames, when to stop at a road stop.
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
Functions related to sound.
@ SND_19_DEPARTURE_OLD_RV_1
23 == 0x17 Station departure: truck and old bus (1) (non-toyland)
Definition sound_type.h:71
@ SND_12_EXPLOSION
16 == 0x10 Destruction, crashes, disasters, ...
Definition sound_type.h:64
@ SND_1A_DEPARTURE_OLD_RV_2
24 == 0x18 Station departure: truck and old bus (2) (random variation of SND_19_DEPARTURE_OLD_RV_1) (...
Definition sound_type.h:72
static const PaletteID PALETTE_CRASH
Recolour sprite greying of crashed vehicles.
Definition sprites.h:1619
Base classes/functions for stations.
void ModifyStationRatingAround(TileIndex tile, Owner owner, int amount, uint radius)
Forcibly modify station ratings near a given tile.
bool IsBayRoadStopTile(Tile t)
Is tile t a bay (non-drive through) road stop station?
bool IsDriveThroughStopTile(Tile t)
Is tile t a drive through road stop station or waypoint?
StationID GetStationIndex(Tile t)
Get StationID from a tile.
Definition station_map.h:28
bool IsStationRoadStop(Tile t)
Is the station at t a road station?
DiagDirection GetBayRoadStopDir(Tile t)
Gets the direction the bay road stop entrance points towards.
RoadStopType GetRoadStopType(Tile t)
Get the road stop type of this tile.
Definition station_map.h:56
RoadStopType
Types of RoadStops.
@ Bus
A standard stop for buses.
@ Truck
A standard stop for trucks.
@ HVOT_TRUCK
Station has seen a truck.
@ HVOT_BUS
Station has seen a bus.
@ 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.
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...
TimerGameTick::Ticks current_order_time
How many ticks have passed since this order started.
void ResetDepotUnbunching()
Resets all the data used for depot unbunching.
TileIndex xy
Base tile of the station.
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
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
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.
Helper container to find a depot.
uint best_length
The distance towards the depot in penalty, or UINT_MAX if not found.
TileIndex tile
The tile of the depot.
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
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.
int UpdateInclination(bool new_tile, bool update_delta)
Checks if the vehicle is in a slope and sets the required flags in that case.
uint Crash(bool flooded) override
Common code executed for crashed ground vehicles.
uint DoUpdateSpeed(uint accel, int min_speed, int max_speed)
void SetLastSpeed()
Update the GUI variant of the current speed of the vehicle.
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
void MakeDummy()
Makes this order a Dummy order.
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
static Engine * Get(auto index)
static T * Create(Targs &&... args)
static Vehicle * GetIfValid(auto index)
int Width() const
Get width of Rect.
int Height() const
Get height of Rect.
A Stop for a Road Vehicle.
void SetEntranceBusy(bool busy)
Makes an entrance occupied or free.
void Leave(RoadVehicle *rv)
Leave the road stop.
Definition roadstop.cpp:203
bool IsEntranceBusy() const
Checks whether the entrance of the road stop is occupied by a vehicle.
static bool IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next)
Checks whether the 'next' tile is still part of the road same drive through stop 'rs' in the same dir...
Definition roadstop.cpp:292
static RoadStop * GetByTile(TileIndex tile, RoadStopType type)
Find a roadstop at given tile.
Definition roadstop.cpp:253
Information about a road vehicle.
Buses, trucks and trams belong to this class.
Definition roadveh.h:105
uint Crash(bool flooded=false) override
Common code executed for crashed ground vehicles.
TileIndex GetOrderStationLocation(StationID station) override
Determine the location for the station where the vehicle goes to next.
void OnNewEconomyDay() override
Economy day handler.
uint8_t state
Definition roadveh.h:107
void SetDestTile(TileIndex tile) override
Set the destination of this vehicle.
int GetDisplayImageWidth(Point *offset=nullptr) const
Get the width of a road vehicle image in the GUI.
Money GetRunningCost() const override
Gets the running cost of a vehicle.
bool IsPrimaryVehicle() const override
Whether this is the primary vehicle in the chain.
Definition roadveh.h:128
uint16_t GetMaxWeight() const override
Calculates the weight value that this vehicle will have when fully loaded with its current cargo.
RoadTypes compatible_roadtypes
NOSAVE: Roadtypes this consist is powered on.
Definition roadveh.h:117
AccelStatus GetAccelerationStatus() const
Checks the current acceleration status of this vehicle.
Definition roadveh.h:229
void UpdateDeltaXY() override
Updates the x and y offsets and the size of the sprite used for this vehicle.
uint16_t crashed_ctr
Animation counter when the vehicle has crashed.
Definition roadveh.h:112
bool IsBus() const
Check whether a roadvehicle is a bus.
uint8_t overtaking_ctr
The length of the current overtake attempt.
Definition roadveh.h:111
void OnNewCalendarDay() override
Calender day handler.
bool IsInDepot() const override
Check whether the vehicle is in the depot.
Definition roadveh.h:134
void GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const override
Gets the sprite to show for the given direction.
RoadVehPathCache path
Cached path.
Definition roadveh.h:106
Trackdir GetVehicleTrackdir() const override
Returns the Trackdir on which the vehicle is currently located.
int GetCurrentMaxSpeed() const override
Calculates the maximum speed of the vehicle under its current conditions.
RoadType roadtype
NOSAVE: Roadtype of this vehicle.
Definition roadveh.h:115
uint8_t overtaking
Set to RVSB_DRIVE_SIDE when overtaking, otherwise 0.
Definition roadveh.h:110
int UpdateSpeed()
This function looks at the vehicle and updates its speed (cur_speed and subspeed) variables.
bool Tick() override
Calls the tick handler of the vehicle.
ClosestDepot FindClosestDepot() override
Find the closest depot for this vehicle and tell us the location, DestinationID and whether we should...
void MarkDirty() override
Marks the vehicles to be redrawn and updates cached variables.
static Station * Get(auto index)
T * Next() const
Get next vehicle in the chain.
T * Previous() const
Get previous vehicle in the chain.
static RoadVehicle * From(Vehicle *v)
T * First() const
Get the first vehicle in the chain.
void UpdateViewport(bool force_update, bool update_delta)
Update vehicle sprite- and position caches.
Station data structure.
Track status of a tile.
Definition track_type.h:132
TrackdirBits signals
Red signals on the tile.
Definition track_type.h:134
TrackdirBits trackdirs
Trackdirs present on the tile.
Definition track_type.h:133
uint16_t cached_max_speed
Maximum speed of the consist (minimum of the max speed of all vehicles in the consist).
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
void IncrementRealOrderIndex()
Advanced cur_real_order_index to the next real order, keeps care of the wrap-around and invalidates t...
VehicleCargoList cargo
The cargo this vehicle is carrying.
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:2453
Money profit_this_year
Profit this year << 8, low 8 bits are fract.
bool HasArticulatedPart() const
Check if an engine has an articulated part.
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.
VehStates vehstatus
Status.
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:2533
CargoType cargo_type
type of cargo this vehicle is carrying
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
int32_t y_pos
y coordinate.
int32_t x_pos
x coordinate.
VehicleCache vcache
Cache of often used vehicle values.
SpriteBounds bounds
Bounding box of vehicle.
void BeginLoading()
Prepare everything to begin the loading when arriving at a station.
Definition vehicle.cpp:2227
uint8_t spritenum
currently displayed sprite index 0xfd == custom sprite, 0xfe == custom second head sprite 0xff == res...
uint16_t cur_speed
current speed
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:2580
void SetNext(Vehicle *next)
Set the next vehicle of this vehicle.
Definition vehicle.cpp:2987
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.
uint8_t tick_counter
Increased by one for each tick.
virtual bool IsInDepot() const
Check whether the vehicle is in the depot.
TileIndex tile
Current tile index.
TileIndex dest_tile
Heading for this tile.
void UpdatePosition()
Update the position of the vehicle.
Definition vehicle.cpp:1699
StationID last_station_visited
The last station we stopped at.
void InvalidateNewGRFCacheOfChain()
Invalidates cached NewGRF variables of all vehicles in the chain (after the current vehicle).
void ShowVisualEffect() const
Draw visual effects (smoke and/or sparks) for a vehicle chain.
Definition vehicle.cpp:2836
Owner owner
Which company owns the vehicle?
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.
@ 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:1863
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
@ Road
A tile with road and/or tram tracks.
Definition tile_type.h:51
Definition of the game-calendar-timer.
Definition of the game-economy-timer.
bool IsReversingRoadTrackdir(Trackdir dir)
Checks whether the trackdir means that we are reversing.
Definition track_func.h:631
TrackdirBits DiagdirReachesTrackdirs(DiagDirection diagdir)
Returns all trackdirs that can be reached when entering a tile from a given (diagonal) direction.
Definition track_func.h:513
bool IsStraightRoadTrackdir(Trackdir dir)
Checks whether the given trackdir is a straight road.
Definition track_func.h:642
Trackdir DiagDirToDiagTrackdir(DiagDirection diagdir)
Maps a (4-way) direction to the diagonal trackdir that runs in that direction.
Definition track_func.h:495
TrackBits TrackdirBitsToTrackBits(TrackdirBits bits)
Discards all directional information from a TrackdirBits value.
Definition track_func.h:308
TrackBits
Bitfield corresponding to Track.
Definition track_type.h:42
@ TRACK_BIT_CROSS
X-Y-axis cross.
Definition track_type.h:50
Trackdir
Enumeration for tracks and directions.
Definition track_type.h:73
@ TRACKDIR_RVREV_NE
(Road vehicle) reverse direction north-east
Definition track_type.h:81
@ TRACKDIR_LOWER_E
Lower track and direction to east.
Definition track_type.h:78
@ TRACKDIR_RIGHT_N
Right track and direction to north.
Definition track_type.h:88
@ INVALID_TRACKDIR
Flag for an invalid trackdir.
Definition track_type.h:92
@ TRACKDIR_UPPER_E
Upper track and direction to east.
Definition track_type.h:77
@ TRACKDIR_LEFT_S
Left track and direction to south.
Definition track_type.h:79
@ TRACKDIR_UPPER_W
Upper track and direction to west.
Definition track_type.h:85
@ TRACKDIR_RVREV_SE
(Road vehicle) reverse direction south-east
Definition track_type.h:82
@ TRACKDIR_LOWER_W
Lower track and direction to west.
Definition track_type.h:86
@ TRACKDIR_END
Used for iterations.
Definition track_type.h:91
@ TRACKDIR_RIGHT_S
Right track and direction to south.
Definition track_type.h:80
@ TRACKDIR_RVREV_NW
(Road vehicle) reverse direction north-west
Definition track_type.h:90
@ TRACKDIR_RVREV_SW
(Road vehicle) reverse direction south-west
Definition track_type.h:89
@ TRACKDIR_LEFT_N
Left track and direction to north.
Definition track_type.h:87
@ TRACKDIR_BIT_NONE
No track build.
Definition track_type.h:112
@ TRANSPORT_ROAD
Transport by road vehicle.
Functions that have tunnels and bridges in common.
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 VehicleEnterDepot(Vehicle *v)
Vehicle entirely entered the depot, update its status, orders, vehicle windows, service it,...
Definition vehicle.cpp:1562
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
bool CanVehicleUseStation(EngineID engine_type, const Station *st)
Can this station be used by the given engine type?
Definition vehicle.cpp:3114
void AgeVehicle(Vehicle *v)
Update age of a vehicle.
Definition vehicle.cpp:1452
@ Crashed
Vehicle is crashed.
@ Hidden
Vehicle is not visible.
@ DefaultPalette
Use default vehicle palette.
@ Stopped
Vehicle is stopped by the player.
Functions related to vehicles.
bool HasVehicleNearTileXY(int32_t x, int32_t y, uint max_dist, UnaryPred &&predicate)
Loop over vehicles near a given world coordinate, and check whether a predicate is true for any of th...
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.
EngineImageType
Visualisation contexts of vehicles and engines.
PoolID< uint32_t, struct VehicleIDTag, 0xFF000, 0xFFFFF > VehicleID
The type all our vehicle IDs have.
@ Road
Road vehicle type.
static const uint VEHICLE_LENGTH
The length of a vehicle in tile units.
@ WID_VV_START_STOP
Start or stop this vehicle, and show information about the current state.
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
Entry point for OpenTTD to YAPF.
Trackdir YapfRoadVehicleChooseTrack(const RoadVehicle *v, TileIndex tile, DiagDirection enterdir, TrackdirBits trackdirs, bool &path_found, RoadVehPathCache &path_cache)
Finds the best path for given road vehicle using YAPF.
FindDepotData YapfRoadVehicleFindNearestDepot(const RoadVehicle *v, int max_penalty)
Used when user sends road vehicle to the nearest depot or if road vehicle needs servicing using 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