OpenTTD Source: src/newgrf_engine.cpp Source File

OpenTTD Source 15.3

newgrf_engine.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
10#include "stdafx.h"
11#include "debug.h"
12#include "train.h"
13#include "roadveh.h"
14#include "company_func.h"
15#include "newgrf_badge.h"
16#include "newgrf_cargo.h"
17#include "newgrf_spritegroup.h"
18#include "timer/timer_game_calendar.h"
19#include "vehicle_func.h"
20#include "core/random_func.hpp"
21#include "core/container_func.hpp"
22#include "aircraft.h"
23#include "station_base.h"
24#include "company_base.h"
25#include "newgrf_railtype.h"
26#include "newgrf_roadtype.h"
27#include "ship.h"
28
29#include "safeguards.h"
30
31void SetWagonOverrideSprites(EngineID engine, CargoType cargo, const SpriteGroup *group, std::span<EngineID> engine_ids)
32{
33 Engine *e = Engine::Get(engine);
34
35 assert(cargo < NUM_CARGO + 2); // Include SpriteGroupCargo::SG_DEFAULT and SpriteGroupCargo::SG_PURCHASE pseudo cargoes.
36
37 WagonOverride *wo = &e->overrides.emplace_back();
38 wo->group = group;
39 wo->cargo = cargo;
40 wo->engines.assign(engine_ids.begin(), engine_ids.end());
41}
42
43const SpriteGroup *GetWagonOverrideSpriteSet(EngineID engine, CargoType cargo, EngineID overriding_engine)
44{
45 const Engine *e = Engine::Get(engine);
46
47 for (const WagonOverride &wo : e->overrides) {
48 if (wo.cargo != cargo && wo.cargo != CargoGRFFileProps::SG_DEFAULT) continue;
49 if (std::ranges::find(wo.engines, overriding_engine) != wo.engines.end()) return wo.group;
50 }
51 return nullptr;
52}
53
54void SetCustomEngineSprites(EngineID engine, CargoType cargo, const SpriteGroup *group)
55{
56 Engine *e = Engine::Get(engine);
57
58 if (e->grf_prop.GetSpriteGroup(cargo) != nullptr) {
59 GrfMsg(6, "SetCustomEngineSprites: engine {} cargo {} already has group -- replacing", engine, cargo);
60 }
61 e->grf_prop.SetSpriteGroup(cargo, group);
62}
63
64
71 void SetEngineGRF(EngineID engine, const GRFFile *file)
72{
73 Engine *e = Engine::Get(engine);
74 e->grf_prop.SetGRFFile(file);
75}
76
77
78static int MapOldSubType(const Vehicle *v)
79{
80 switch (v->type) {
81 case VEH_TRAIN:
82 if (Train::From(v)->IsEngine()) return 0;
83 if (Train::From(v)->IsFreeWagon()) return 4;
84 return 2;
85 case VEH_ROAD:
86 case VEH_SHIP: return 0;
87 case VEH_AIRCRAFT:
88 case VEH_DISASTER: return v->subtype;
89 case VEH_EFFECT: return v->subtype << 1;
90 default: NOT_REACHED();
91 }
92}
93
94
95/* TTDP style aircraft movement states for GRF Action 2 Var 0xE2 */
96enum TTDPAircraftMovementStates : uint8_t {
97 AMS_TTDP_HANGAR,
98 AMS_TTDP_TO_HANGAR,
99 AMS_TTDP_TO_PAD1,
100 AMS_TTDP_TO_PAD2,
101 AMS_TTDP_TO_PAD3,
102 AMS_TTDP_TO_ENTRY_2_AND_3,
103 AMS_TTDP_TO_ENTRY_2_AND_3_AND_H,
104 AMS_TTDP_TO_JUNCTION,
105 AMS_TTDP_LEAVE_RUNWAY,
106 AMS_TTDP_TO_INWAY,
107 AMS_TTDP_TO_RUNWAY,
108 AMS_TTDP_TO_OUTWAY,
109 AMS_TTDP_WAITING,
110 AMS_TTDP_TAKEOFF,
111 AMS_TTDP_TO_TAKEOFF,
112 AMS_TTDP_CLIMBING,
113 AMS_TTDP_FLIGHT_APPROACH,
114 AMS_TTDP_UNUSED_0x11,
115 AMS_TTDP_FLIGHT_TO_TOWER,
116 AMS_TTDP_UNUSED_0x13,
117 AMS_TTDP_FLIGHT_FINAL,
118 AMS_TTDP_FLIGHT_DESCENT,
119 AMS_TTDP_BRAKING,
120 AMS_TTDP_HELI_TAKEOFF_AIRPORT,
121 AMS_TTDP_HELI_TO_TAKEOFF_AIRPORT,
122 AMS_TTDP_HELI_LAND_AIRPORT,
123 AMS_TTDP_HELI_TAKEOFF_HELIPORT,
124 AMS_TTDP_HELI_TO_TAKEOFF_HELIPORT,
125 AMS_TTDP_HELI_LAND_HELIPORT,
126};
127
128
133 static uint8_t MapAircraftMovementState(const Aircraft *v)
134{
135 const Station *st = GetTargetAirportIfValid(v);
136 if (st == nullptr) return AMS_TTDP_FLIGHT_TO_TOWER;
137
138 const AirportFTAClass *afc = st->airport.GetFTA();
139 AirportMovingDataFlags amdflag = afc->MovingData(v->pos)->flags;
140
141 switch (v->state) {
142 case HANGAR:
143 /* The international airport is a special case as helicopters can land in
144 * front of the hangar. Helicopters also change their air.state to
145 * AirportMovingDataFlag::HeliLower some time before actually descending. */
146
147 /* This condition only occurs for helicopters, during descent,
148 * to a landing by the hangar of an international airport. */
149 if (amdflag.Test(AirportMovingDataFlag::HeliLower)) return AMS_TTDP_HELI_LAND_AIRPORT;
150
151 /* This condition only occurs for helicopters, before starting descent,
152 * to a landing by the hangar of an international airport. */
153 if (amdflag.Test(AirportMovingDataFlag::SlowTurn)) return AMS_TTDP_FLIGHT_TO_TOWER;
154
155 /* The final two conditions apply to helicopters or aircraft.
156 * Has reached hangar? */
157 if (amdflag.Test(AirportMovingDataFlag::ExactPosition)) return AMS_TTDP_HANGAR;
158
159 /* Still moving towards hangar. */
160 return AMS_TTDP_TO_HANGAR;
161
162 case TERM1:
163 if (amdflag.Test(AirportMovingDataFlag::ExactPosition)) return AMS_TTDP_TO_PAD1;
164 return AMS_TTDP_TO_JUNCTION;
165
166 case TERM2:
167 if (amdflag.Test(AirportMovingDataFlag::ExactPosition)) return AMS_TTDP_TO_PAD2;
168 return AMS_TTDP_TO_ENTRY_2_AND_3_AND_H;
169
170 case TERM3:
171 case TERM4:
172 case TERM5:
173 case TERM6:
174 case TERM7:
175 case TERM8:
176 /* TTDPatch only has 3 terminals, so treat these states the same */
177 if (amdflag.Test(AirportMovingDataFlag::ExactPosition)) return AMS_TTDP_TO_PAD3;
178 return AMS_TTDP_TO_ENTRY_2_AND_3_AND_H;
179
180 case HELIPAD1:
181 case HELIPAD2:
182 case HELIPAD3:
183 /* Will only occur for helicopters.*/
184 if (amdflag.Test(AirportMovingDataFlag::HeliLower)) return AMS_TTDP_HELI_LAND_AIRPORT; // Descending.
185 if (amdflag.Test(AirportMovingDataFlag::SlowTurn)) return AMS_TTDP_FLIGHT_TO_TOWER; // Still hasn't started descent.
186 return AMS_TTDP_TO_JUNCTION; // On the ground.
187
188 case TAKEOFF: // Moving to takeoff position.
189 return AMS_TTDP_TO_OUTWAY;
190
191 case STARTTAKEOFF: // Accelerating down runway.
192 return AMS_TTDP_TAKEOFF;
193
194 case ENDTAKEOFF: // Ascent
195 return AMS_TTDP_CLIMBING;
196
197 case HELITAKEOFF: // Helicopter is moving to take off position.
198 if (afc->delta_z == 0) {
199 return amdflag.Test(AirportMovingDataFlag::HeliRaise) ?
200 AMS_TTDP_HELI_TAKEOFF_AIRPORT : AMS_TTDP_TO_JUNCTION;
201 } else {
202 return AMS_TTDP_HELI_TAKEOFF_HELIPORT;
203 }
204
205 case FLYING:
206 return amdflag.Test(AirportMovingDataFlag::Hold) ? AMS_TTDP_FLIGHT_APPROACH : AMS_TTDP_FLIGHT_TO_TOWER;
207
208 case LANDING: // Descent
209 return AMS_TTDP_FLIGHT_DESCENT;
210
211 case ENDLANDING: // On the runway braking
212 if (amdflag.Test(AirportMovingDataFlag::Brake)) return AMS_TTDP_BRAKING;
213 /* Landed - moving off runway */
214 return AMS_TTDP_TO_INWAY;
215
216 case HELILANDING:
217 case HELIENDLANDING: // Helicoptor is descending.
218 if (amdflag.Test(AirportMovingDataFlag::HeliLower)) {
219 return afc->delta_z == 0 ?
220 AMS_TTDP_HELI_LAND_AIRPORT : AMS_TTDP_HELI_LAND_HELIPORT;
221 } else {
222 return AMS_TTDP_FLIGHT_TO_TOWER;
223 }
224
225 default:
226 return AMS_TTDP_HANGAR;
227 }
228}
229
230
231/* TTDP style aircraft movement action for GRF Action 2 Var 0xE6 */
232enum TTDPAircraftMovementActions : uint8_t {
233 AMA_TTDP_IN_HANGAR,
234 AMA_TTDP_ON_PAD1,
235 AMA_TTDP_ON_PAD2,
236 AMA_TTDP_ON_PAD3,
237 AMA_TTDP_HANGAR_TO_PAD1,
238 AMA_TTDP_HANGAR_TO_PAD2,
239 AMA_TTDP_HANGAR_TO_PAD3,
240 AMA_TTDP_LANDING_TO_PAD1,
241 AMA_TTDP_LANDING_TO_PAD2,
242 AMA_TTDP_LANDING_TO_PAD3,
243 AMA_TTDP_PAD1_TO_HANGAR,
244 AMA_TTDP_PAD2_TO_HANGAR,
245 AMA_TTDP_PAD3_TO_HANGAR,
246 AMA_TTDP_PAD1_TO_TAKEOFF,
247 AMA_TTDP_PAD2_TO_TAKEOFF,
248 AMA_TTDP_PAD3_TO_TAKEOFF,
249 AMA_TTDP_HANGAR_TO_TAKEOFF,
250 AMA_TTDP_LANDING_TO_HANGAR,
251 AMA_TTDP_IN_FLIGHT,
252};
253
254
260 static uint8_t MapAircraftMovementAction(const Aircraft *v)
261{
262 switch (v->state) {
263 case HANGAR:
264 return (v->cur_speed > 0) ? AMA_TTDP_LANDING_TO_HANGAR : AMA_TTDP_IN_HANGAR;
265
266 case TERM1:
267 case HELIPAD1:
268 return (v->current_order.IsType(OT_LOADING)) ? AMA_TTDP_ON_PAD1 : AMA_TTDP_LANDING_TO_PAD1;
269
270 case TERM2:
271 case HELIPAD2:
272 return (v->current_order.IsType(OT_LOADING)) ? AMA_TTDP_ON_PAD2 : AMA_TTDP_LANDING_TO_PAD2;
273
274 case TERM3:
275 case TERM4:
276 case TERM5:
277 case TERM6:
278 case TERM7:
279 case TERM8:
280 case HELIPAD3:
281 return (v->current_order.IsType(OT_LOADING)) ? AMA_TTDP_ON_PAD3 : AMA_TTDP_LANDING_TO_PAD3;
282
283 case TAKEOFF: // Moving to takeoff position
284 case STARTTAKEOFF: // Accelerating down runway
285 case ENDTAKEOFF: // Ascent
286 case HELITAKEOFF:
287 /* @todo Need to find which terminal (or hangar) we've come from. How? */
288 return AMA_TTDP_PAD1_TO_TAKEOFF;
289
290 case FLYING:
291 return AMA_TTDP_IN_FLIGHT;
292
293 case LANDING: // Descent
294 case ENDLANDING: // On the runway braking
295 case HELILANDING:
296 case HELIENDLANDING:
297 /* @todo Need to check terminal we're landing to. Is it known yet? */
298 return (v->current_order.IsType(OT_GOTO_DEPOT)) ?
299 AMA_TTDP_LANDING_TO_HANGAR : AMA_TTDP_LANDING_TO_PAD1;
300
301 default:
302 return AMA_TTDP_IN_HANGAR;
303 }
304}
305
306
307/* virtual */ uint32_t VehicleScopeResolver::GetRandomBits() const
308{
309 return this->v == nullptr ? 0 : this->v->random_bits;
310}
311
312/* virtual */ uint32_t VehicleScopeResolver::GetRandomTriggers() const
313{
314 return this->v == nullptr ? 0 : this->v->waiting_random_triggers.base();
315}
316
317
318/* virtual */ ScopeResolver *VehicleResolverObject::GetScope(VarSpriteGroupScope scope, uint8_t relative)
319{
320 switch (scope) {
321 case VSG_SCOPE_SELF: return &this->self_scope;
322 case VSG_SCOPE_PARENT: return &this->parent_scope;
323 case VSG_SCOPE_RELATIVE: {
324 int32_t count = GB(relative, 0, 4);
325 if (this->self_scope.v != nullptr && (relative != this->cached_relative_count || count == 0)) {
326 /* Note: This caching only works as long as the VSG_SCOPE_RELATIVE cannot be used in
327 * VarAct2 with procedure calls. */
328 if (count == 0) count = this->GetRegister(0x100);
329
330 const Vehicle *v = nullptr;
331 switch (GB(relative, 6, 2)) {
332 default: NOT_REACHED();
333 case 0x00: // count back (away from the engine), starting at this vehicle
334 v = this->self_scope.v;
335 break;
336 case 0x01: // count forward (toward the engine), starting at this vehicle
337 v = this->self_scope.v;
338 count = -count;
339 break;
340 case 0x02: // count back, starting at the engine
341 v = this->parent_scope.v;
342 break;
343 case 0x03: { // count back, starting at the first vehicle in this chain of vehicles with the same ID, as for vehicle variable 41
344 const Vehicle *self = this->self_scope.v;
345 for (const Vehicle *u = self->First(); u != self; u = u->Next()) {
346 if (u->engine_type != self->engine_type) {
347 v = nullptr;
348 } else {
349 if (v == nullptr) v = u;
350 }
351 }
352 if (v == nullptr) v = self;
353 break;
354 }
355 }
356 this->relative_scope.SetVehicle(v->Move(count));
357 }
358 return &this->relative_scope;
359 }
360 default: return ResolverObject::GetScope(scope, relative);
361 }
362}
363
373 static const Livery *LiveryHelper(EngineID engine, const Vehicle *v)
374{
375 const Livery *l;
376
377 if (v == nullptr) {
378 if (!Company::IsValidID(_current_company)) return nullptr;
379 l = GetEngineLivery(engine, _current_company, EngineID::Invalid(), nullptr, LIT_ALL);
380 } else if (v->IsGroundVehicle()) {
381 l = GetEngineLivery(v->engine_type, v->owner, v->GetGroundVehicleCache()->first_engine, v, LIT_ALL);
382 } else {
383 l = GetEngineLivery(v->engine_type, v->owner, EngineID::Invalid(), v, LIT_ALL);
384 }
385
386 return l;
387}
388
396 static uint32_t PositionHelper(const Vehicle *v, bool consecutive)
397{
398 const Vehicle *u;
399 uint8_t chain_before = 0;
400 uint8_t chain_after = 0;
401
402 for (u = v->First(); u != v; u = u->Next()) {
403 chain_before++;
404 if (consecutive && u->engine_type != v->engine_type) chain_before = 0;
405 }
406
407 while (u->Next() != nullptr && (!consecutive || u->Next()->engine_type == v->engine_type)) {
408 chain_after++;
409 u = u->Next();
410 }
411
412 return chain_before | chain_after << 8 | (chain_before + chain_after + consecutive) << 16;
413}
414
415static uint32_t VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object, uint8_t variable, uint32_t parameter, bool &available)
416{
417 /* Calculated vehicle parameters */
418 switch (variable) {
419 case 0x25: // Get engine GRF ID
420 return v->GetGRFID();
421
422 case 0x40: // Get length of consist
423 if (!HasBit(v->grf_cache.cache_valid, NCVV_POSITION_CONSIST_LENGTH)) {
424 v->grf_cache.position_consist_length = PositionHelper(v, false);
425 SetBit(v->grf_cache.cache_valid, NCVV_POSITION_CONSIST_LENGTH);
426 }
427 return v->grf_cache.position_consist_length;
428
429 case 0x41: // Get length of same consecutive wagons
430 if (!HasBit(v->grf_cache.cache_valid, NCVV_POSITION_SAME_ID_LENGTH)) {
431 v->grf_cache.position_same_id_length = PositionHelper(v, true);
432 SetBit(v->grf_cache.cache_valid, NCVV_POSITION_SAME_ID_LENGTH);
433 }
434 return v->grf_cache.position_same_id_length;
435
436 case 0x42: { // Consist cargo information
437 if (!HasBit(v->grf_cache.cache_valid, NCVV_CONSIST_CARGO_INFORMATION)) {
438 std::array<uint8_t, NUM_CARGO> common_cargoes{};
439 uint8_t cargo_classes = 0;
440 uint8_t user_def_data = 0;
441
442 for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
443 if (v->type == VEH_TRAIN) user_def_data |= Train::From(u)->tcache.user_def_data;
444
445 /* Skip empty engines */
446 if (!u->GetEngine()->CanCarryCargo()) continue;
447
448 cargo_classes |= CargoSpec::Get(u->cargo_type)->classes.base();
449 common_cargoes[u->cargo_type]++;
450 }
451
452 /* Pick the most common cargo type */
453 auto cargo_it = std::max_element(std::begin(common_cargoes), std::end(common_cargoes));
454 /* Return INVALID_CARGO if nothing is carried */
455 CargoType common_cargo_type = (*cargo_it == 0) ? INVALID_CARGO : static_cast<CargoType>(std::distance(std::begin(common_cargoes), cargo_it));
456
457 /* Count subcargo types of common_cargo_type */
458 std::array<uint8_t, UINT8_MAX + 1> common_subtypes{};
459 for (const Vehicle *u = v; u != nullptr; u = u->Next()) {
460 /* Skip empty engines and engines not carrying common_cargo_type */
461 if (u->cargo_type != common_cargo_type || !u->GetEngine()->CanCarryCargo()) continue;
462
463 common_subtypes[u->cargo_subtype]++;
464 }
465
466 /* Pick the most common subcargo type*/
467 auto subtype_it = std::max_element(std::begin(common_subtypes), std::end(common_subtypes));
468 /* Return UINT8_MAX if nothing is carried */
469 uint8_t common_subtype = (*subtype_it == 0) ? UINT8_MAX : static_cast<uint8_t>(std::distance(std::begin(common_subtypes), subtype_it));
470
471 /* Note: We have to store the untranslated cargotype in the cache as the cache can be read by different NewGRFs,
472 * which will need different translations */
473 v->grf_cache.consist_cargo_information = cargo_classes | (common_cargo_type << 8) | (common_subtype << 16) | (user_def_data << 24);
474 SetBit(v->grf_cache.cache_valid, NCVV_CONSIST_CARGO_INFORMATION);
475 }
476
477 /* The cargo translation is specific to the accessing GRF, and thus cannot be cached. */
478 CargoType common_cargo_type = (v->grf_cache.consist_cargo_information >> 8) & 0xFF;
479
480 /* Note:
481 * - Unlike everywhere else the cargo translation table is only used since grf version 8, not 7.
482 * - For translating the cargo type we need to use the GRF which is resolving the variable, which
483 * is object->ro.grffile.
484 * In case of CBID_TRAIN_ALLOW_WAGON_ATTACH this is not the same as v->GetGRF().
485 * - The grffile == nullptr case only happens if this function is called for default vehicles.
486 * And this is only done by CheckCaches().
487 */
488 const GRFFile *grffile = object->ro.grffile;
489 uint8_t common_bitnum = (common_cargo_type == INVALID_CARGO) ? 0xFF :
490 (grffile == nullptr || grffile->grf_version < 8) ? CargoSpec::Get(common_cargo_type)->bitnum : grffile->cargo_map[common_cargo_type];
491
492 return (v->grf_cache.consist_cargo_information & 0xFFFF00FF) | common_bitnum << 8;
493 }
494
495 case 0x43: // Company information
496 if (!HasBit(v->grf_cache.cache_valid, NCVV_COMPANY_INFORMATION)) {
497 v->grf_cache.company_information = GetCompanyInfo(v->owner, LiveryHelper(v->engine_type, v));
498 SetBit(v->grf_cache.cache_valid, NCVV_COMPANY_INFORMATION);
499 }
500 return v->grf_cache.company_information;
501
502 case 0x44: // Aircraft information
503 if (v->type != VEH_AIRCRAFT || !Aircraft::From(v)->IsNormalAircraft()) return UINT_MAX;
504
505 {
506 const Vehicle *w = v->Next();
507 assert(w != nullptr);
508 uint16_t altitude = ClampTo<uint16_t>(v->z_pos - w->z_pos); // Aircraft height - shadow height
509 uint8_t airporttype = ATP_TTDP_LARGE;
510
511 const Station *st = GetTargetAirportIfValid(Aircraft::From(v));
512
513 if (st != nullptr && st->airport.tile != INVALID_TILE) {
514 airporttype = st->airport.GetSpec()->ttd_airport_type;
515 }
516
517 return (ClampTo<uint8_t>(altitude) << 8) | airporttype;
518 }
519
520 case 0x45: { // Curvature info
521 /* Format: xxxTxBxF
522 * F - previous wagon to current wagon, 0 if vehicle is first
523 * B - current wagon to next wagon, 0 if wagon is last
524 * T - previous wagon to next wagon, 0 in an S-bend
525 */
526 if (!v->IsGroundVehicle()) return 0;
527
528 const Vehicle *u_p = v->Previous();
529 const Vehicle *u_n = v->Next();
530 DirDiff f = (u_p == nullptr) ? DIRDIFF_SAME : DirDifference(u_p->direction, v->direction);
531 DirDiff b = (u_n == nullptr) ? DIRDIFF_SAME : DirDifference(v->direction, u_n->direction);
532 DirDiff t = ChangeDirDiff(f, b);
533
534 return ((t > DIRDIFF_REVERSE ? t | 8 : t) << 16) |
535 ((b > DIRDIFF_REVERSE ? b | 8 : b) << 8) |
536 ( f > DIRDIFF_REVERSE ? f | 8 : f);
537 }
538
539 case 0x46: // Motion counter
540 return v->motion_counter;
541
542 case 0x47: { // Vehicle cargo info
543 /* Format: ccccwwtt
544 * tt - the cargo type transported by the vehicle,
545 * translated if a translation table has been installed.
546 * ww - cargo unit weight in 1/16 tons, same as cargo prop. 0F.
547 * cccc - the cargo class value of the cargo transported by the vehicle.
548 */
549 const CargoSpec *cs = CargoSpec::Get(v->cargo_type);
550
551 /* Note:
552 * For translating the cargo type we need to use the GRF which is resolving the variable, which
553 * is object->ro.grffile.
554 * In case of CBID_TRAIN_ALLOW_WAGON_ATTACH this is not the same as v->GetGRF().
555 */
556 return (cs->classes.base() << 16) | (cs->weight << 8) | object->ro.grffile->cargo_map[v->cargo_type];
557 }
558
559 case 0x48: return v->GetEngine()->flags.base(); // Vehicle Type Info
560 case 0x49: return v->build_year.base();
561
562 case 0x4A:
563 switch (v->type) {
564 case VEH_TRAIN: {
565 RailType rt = GetTileRailType(v->tile);
566 const RailTypeInfo *rti = GetRailTypeInfo(rt);
567 return (rti->flags.Test(RailTypeFlag::Catenary) ? 0x200 : 0) |
568 (HasPowerOnRail(Train::From(v)->railtypes, rt) ? 0x100 : 0) |
569 GetReverseRailTypeTranslation(rt, object->ro.grffile);
570 }
571
572 case VEH_ROAD: {
573 RoadType rt = GetRoadType(v->tile, GetRoadTramType(RoadVehicle::From(v)->roadtype));
574 const RoadTypeInfo *rti = GetRoadTypeInfo(rt);
575 return (rti->flags.Test(RoadTypeFlag::Catenary) ? 0x200 : 0) |
576 0x100 |
577 GetReverseRoadTypeTranslation(rt, object->ro.grffile);
578 }
579
580 default:
581 return 0;
582 }
583
584 case 0x4B: // Long date of last service
585 return v->date_of_last_service_newgrf.base();
586
587 case 0x4C: // Current maximum speed in NewGRF units
588 if (!v->IsPrimaryVehicle()) return 0;
589 return v->GetCurrentMaxSpeed();
590
591 case 0x4D: // Position within articulated vehicle
592 if (!HasBit(v->grf_cache.cache_valid, NCVV_POSITION_IN_VEHICLE)) {
593 uint8_t artic_before = 0;
594 for (const Vehicle *u = v; u->IsArticulatedPart(); u = u->Previous()) artic_before++;
595 uint8_t artic_after = 0;
596 for (const Vehicle *u = v; u->HasArticulatedPart(); u = u->Next()) artic_after++;
597 v->grf_cache.position_in_vehicle = artic_before | artic_after << 8;
598 SetBit(v->grf_cache.cache_valid, NCVV_POSITION_IN_VEHICLE);
599 }
600 return v->grf_cache.position_in_vehicle;
601
602 /* Variables which use the parameter */
603 case 0x60: // Count consist's engine ID occurrence
604 if (v->type != VEH_TRAIN) return v->GetEngine()->grf_prop.local_id == parameter ? 1 : 0;
605
606 {
607 uint count = 0;
608 for (; v != nullptr; v = v->Next()) {
609 if (v->GetEngine()->grf_prop.local_id == parameter) count++;
610 }
611 return count;
612 }
613
614 case 0x61: // Get variable of n-th vehicle in chain [signed number relative to vehicle]
615 if (!v->IsGroundVehicle() || parameter == 0x61) {
616 /* Not available */
617 break;
618 }
619
620 /* Only allow callbacks that don't change properties to avoid circular dependencies. */
621 if (object->ro.callback == CBID_NO_CALLBACK || object->ro.callback == CBID_RANDOM_TRIGGER || object->ro.callback == CBID_TRAIN_ALLOW_WAGON_ATTACH ||
622 object->ro.callback == CBID_VEHICLE_START_STOP_CHECK || object->ro.callback == CBID_VEHICLE_32DAY_CALLBACK || object->ro.callback == CBID_VEHICLE_COLOUR_MAPPING ||
623 object->ro.callback == CBID_VEHICLE_SPAWN_VISUAL_EFFECT) {
624 Vehicle *u = v->Move(object->ro.GetRegister(0x10F));
625 if (u == nullptr) return 0; // available, but zero
626
627 if (parameter == 0x5F) {
628 /* This seems to be the only variable that makes sense to access via var 61, but is not handled by VehicleGetVariable */
629 return (u->random_bits << 8) | u->waiting_random_triggers.base();
630 } else {
631 return VehicleGetVariable(u, object, parameter, object->ro.GetRegister(0x10E), available);
632 }
633 }
634 /* Not available */
635 break;
636
637 case 0x62: { // Curvature/position difference for n-th vehicle in chain [signed number relative to vehicle]
638 /* Format: zzyyxxFD
639 * zz - Signed difference of z position between the selected and this vehicle.
640 * yy - Signed difference of y position between the selected and this vehicle.
641 * xx - Signed difference of x position between the selected and this vehicle.
642 * F - Flags, bit 7 corresponds to VehState::Hidden.
643 * D - Dir difference, like in 0x45.
644 */
645 if (!v->IsGroundVehicle()) return 0;
646
647 const Vehicle *u = v->Move((int8_t)parameter);
648 if (u == nullptr) return 0;
649
650 /* Get direction difference. */
651 bool prev = (int8_t)parameter < 0;
652 uint32_t ret = prev ? DirDifference(u->direction, v->direction) : DirDifference(v->direction, u->direction);
653 if (ret > DIRDIFF_REVERSE) ret |= 0x08;
654
655 if (u->vehstatus.Test(VehState::Hidden)) ret |= 0x80;
656
657 /* Get position difference. */
658 ret |= ((prev ? u->x_pos - v->x_pos : v->x_pos - u->x_pos) & 0xFF) << 8;
659 ret |= ((prev ? u->y_pos - v->y_pos : v->y_pos - u->y_pos) & 0xFF) << 16;
660 ret |= ((prev ? u->z_pos - v->z_pos : v->z_pos - u->z_pos) & 0xFF) << 24;
661
662 return ret;
663 }
664
665 case 0x63:
666 /* Tile compatibility wrt. arbitrary track-type
667 * Format:
668 * bit 0: Type 'parameter' is known.
669 * bit 1: Engines with type 'parameter' are compatible with this tile.
670 * bit 2: Engines with type 'parameter' are powered on this tile.
671 * bit 3: This tile has type 'parameter' or it is considered equivalent (alternate labels).
672 */
673 switch (v->type) {
674 case VEH_TRAIN: {
675 RailType param_type = GetRailTypeTranslation(parameter, object->ro.grffile);
676 if (param_type == INVALID_RAILTYPE) return 0x00;
677 RailType tile_type = GetTileRailType(v->tile);
678 if (tile_type == param_type) return 0x0F;
679 return (HasPowerOnRail(param_type, tile_type) ? 0x04 : 0x00) |
680 (IsCompatibleRail(param_type, tile_type) ? 0x02 : 0x00) |
681 0x01;
682 }
683 case VEH_ROAD: {
684 RoadTramType rtt = GetRoadTramType(RoadVehicle::From(v)->roadtype);
685 RoadType param_type = GetRoadTypeTranslation(rtt, parameter, object->ro.grffile);
686 if (param_type == INVALID_ROADTYPE) return 0x00;
687 RoadType tile_type = GetRoadType(v->tile, rtt);
688 if (tile_type == param_type) return 0x0F;
689 return (HasPowerOnRoad(param_type, tile_type) ? 0x06 : 0x00) |
690 0x01;
691 }
692 default: return 0x00;
693 }
694
695 case 0x64: { // Count consist's badge ID occurrence
696 if (v->type != VEH_TRAIN) return GetBadgeVariableResult(*object->ro.grffile, v->GetEngine()->badges, parameter);
697
698 /* Look up badge index. */
699 if (parameter >= std::size(object->ro.grffile->badge_list)) return UINT_MAX;
700 BadgeID index = object->ro.grffile->badge_list[parameter];
701
702 /* Count number of vehicles that contain this badge index. */
703 uint count = 0;
704 for (; v != nullptr; v = v->Next()) {
705 const auto &badges = v->GetEngine()->badges;
706 if (std::ranges::find(badges, index) != std::end(badges)) count++;
707 }
708
709 return count;
710 }
711
712 case 0x65:
713 if (v->type == VEH_TRAIN) {
714 RailType rt = GetRailType(v->tile);
715 return GetBadgeVariableResult(*object->ro.grffile, GetRailTypeInfo(rt)->badges, parameter);
716 }
717 if (v->type == VEH_ROAD) {
718 RoadType rt = GetRoadType(v->tile, GetRoadTramType(RoadVehicle::From(v)->roadtype));
719 return GetBadgeVariableResult(*object->ro.grffile, GetRoadTypeInfo(rt)->badges, parameter);
720 }
721 return UINT_MAX;
722
723 case 0x7A: return GetBadgeVariableResult(*object->ro.grffile, v->GetEngine()->badges, parameter);
724
725 case 0xFE:
726 case 0xFF: {
727 uint16_t modflags = 0;
728
729 if (v->type == VEH_TRAIN) {
730 const Train *t = Train::From(v);
731 bool is_powered_wagon = t->flags.Test(VehicleRailFlag::PoweredWagon);
732 const Train *u = is_powered_wagon ? t->First() : t; // for powered wagons the engine defines the type of engine (i.e. railtype)
733 RailType railtype = GetRailType(v->tile);
734 bool powered = t->IsEngine() || is_powered_wagon;
735 bool has_power = HasPowerOnRail(u->railtypes, railtype);
736
737 if (powered && has_power) SetBit(modflags, 5);
738 if (powered && !has_power) SetBit(modflags, 6);
739 if (t->flags.Test(VehicleRailFlag::Reversed)) SetBit(modflags, 8);
740 }
741 if (v->vehicle_flags.Test(VehicleFlag::CargoUnloading)) SetBit(modflags, 1);
742 if (v->vehicle_flags.Test(VehicleFlag::BuiltAsPrototype)) SetBit(modflags, 10);
743
744 return variable == 0xFE ? modflags : GB(modflags, 8, 8);
745 }
746 }
747
748 /*
749 * General vehicle properties
750 *
751 * Some parts of the TTD Vehicle structure are omitted for various reasons
752 * (see http://marcin.ttdpatch.net/sv1codec/TTD-locations.html#_VehicleArray)
753 */
754 switch (variable - 0x80) {
755 case 0x00: return v->type + 0x10;
756 case 0x01: return MapOldSubType(v);
757 case 0x02: break; // not implemented
758 case 0x03: break; // not implemented
759 case 0x04: return v->index.base();
760 case 0x05: return GB(v->index.base(), 8, 8);
761 case 0x06: break; // not implemented
762 case 0x07: break; // not implemented
763 case 0x08: break; // not implemented
764 case 0x09: break; // not implemented
765 case 0x0A: return v->current_order.MapOldOrder();
766 case 0x0B: return v->current_order.GetDestination().value;
767 case 0x0C: return v->GetNumOrders();
768 case 0x0D: return v->cur_real_order_index;
769 case 0x0E: break; // not implemented
770 case 0x0F: break; // not implemented
771 case 0x10:
772 case 0x11: {
773 uint ticks;
774 if (v->current_order.IsType(OT_LOADING)) {
775 ticks = v->load_unload_ticks;
776 } else {
777 switch (v->type) {
778 case VEH_TRAIN: ticks = Train::From(v)->wait_counter; break;
779 case VEH_AIRCRAFT: ticks = Aircraft::From(v)->turn_counter; break;
780 default: ticks = 0; break;
781 }
782 }
783 return (variable - 0x80) == 0x10 ? ticks : GB(ticks, 8, 8);
784 }
785 case 0x12: return ClampTo<uint16_t>(v->date_of_last_service_newgrf - CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR);
786 case 0x13: return GB(ClampTo<uint16_t>(v->date_of_last_service_newgrf - CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR), 8, 8);
787 case 0x14: return v->GetServiceInterval();
788 case 0x15: return GB(v->GetServiceInterval(), 8, 8);
789 case 0x16: return v->last_station_visited.base();
790 case 0x17: return v->tick_counter;
791 case 0x18:
792 case 0x19: {
793 uint max_speed;
794 switch (v->type) {
795 case VEH_AIRCRAFT:
796 max_speed = Aircraft::From(v)->GetSpeedOldUnits(); // Convert to old units.
797 break;
798
799 default:
800 max_speed = v->vcache.cached_max_speed;
801 break;
802 }
803 return (variable - 0x80) == 0x18 ? max_speed : GB(max_speed, 8, 8);
804 }
805 case 0x1A: return v->x_pos;
806 case 0x1B: return GB(v->x_pos, 8, 8);
807 case 0x1C: return v->y_pos;
808 case 0x1D: return GB(v->y_pos, 8, 8);
809 case 0x1E: return v->z_pos;
810 case 0x1F: return object->rotor_in_gui ? DIR_W : v->direction; // for rotors the spriteset contains animation frames, so NewGRF need a different way to tell the helicopter orientation.
811 case 0x20: break; // not implemented
812 case 0x21: break; // not implemented
813 case 0x22: break; // not implemented
814 case 0x23: break; // not implemented
815 case 0x24: break; // not implemented
816 case 0x25: break; // not implemented
817 case 0x26: break; // not implemented
818 case 0x27: break; // not implemented
819 case 0x28: return 0; // cur_image is a potential desyncer due to Action1 in static NewGRFs.
820 case 0x29: return 0; // cur_image is a potential desyncer due to Action1 in static NewGRFs.
821 case 0x2A: break; // not implemented
822 case 0x2B: break; // not implemented
823 case 0x2C: break; // not implemented
824 case 0x2D: break; // not implemented
825 case 0x2E: break; // not implemented
826 case 0x2F: break; // not implemented
827 case 0x30: break; // not implemented
828 case 0x31: break; // not implemented
829 case 0x32: return v->vehstatus.base();
830 case 0x33: return 0; // non-existent high byte of vehstatus
831 case 0x34: return v->type == VEH_AIRCRAFT ? (v->cur_speed * 10) / 128 : v->cur_speed;
832 case 0x35: return GB(v->type == VEH_AIRCRAFT ? (v->cur_speed * 10) / 128 : v->cur_speed, 8, 8);
833 case 0x36: return v->subspeed;
834 case 0x37: return v->acceleration;
835 case 0x38: break; // not implemented
836 case 0x39: return v->cargo_type;
837 case 0x3A: return v->cargo_cap;
838 case 0x3B: return GB(v->cargo_cap, 8, 8);
839 case 0x3C: return ClampTo<uint16_t>(v->cargo.StoredCount());
840 case 0x3D: return GB(ClampTo<uint16_t>(v->cargo.StoredCount()), 8, 8);
841 case 0x3E: return v->cargo.GetFirstStation().base();
842 case 0x3F: return ClampTo<uint8_t>(v->cargo.PeriodsInTransit());
843 case 0x40: return ClampTo<uint16_t>(v->age);
844 case 0x41: return GB(ClampTo<uint16_t>(v->age), 8, 8);
845 case 0x42: return ClampTo<uint16_t>(v->max_age);
846 case 0x43: return GB(ClampTo<uint16_t>(v->max_age), 8, 8);
847 case 0x44: return (Clamp(v->build_year, CalendarTime::ORIGINAL_BASE_YEAR, CalendarTime::ORIGINAL_MAX_YEAR) - CalendarTime::ORIGINAL_BASE_YEAR).base();
848 case 0x45: return v->unitnumber;
849 case 0x46: return v->GetEngine()->grf_prop.local_id;
850 case 0x47: return GB(v->GetEngine()->grf_prop.local_id, 8, 8);
851 case 0x48:
852 if (v->type != VEH_TRAIN || v->spritenum != CUSTOM_VEHICLE_SPRITENUM) return v->spritenum;
853 return Train::From(v)->flags.Test(VehicleRailFlag::Flipped) ? CUSTOM_VEHICLE_SPRITENUM_REVERSED : CUSTOM_VEHICLE_SPRITENUM;
854
855 case 0x49: return v->day_counter;
856 case 0x4A: return v->breakdowns_since_last_service;
857 case 0x4B: return v->breakdown_ctr;
858 case 0x4C: return v->breakdown_delay;
859 case 0x4D: return v->breakdown_chance;
860 case 0x4E: return v->reliability;
861 case 0x4F: return GB(v->reliability, 8, 8);
862 case 0x50: return v->reliability_spd_dec;
863 case 0x51: return GB(v->reliability_spd_dec, 8, 8);
864 case 0x52: return ClampTo<int32_t>(v->GetDisplayProfitThisYear());
865 case 0x53: return GB(ClampTo<int32_t>(v->GetDisplayProfitThisYear()), 8, 24);
866 case 0x54: return GB(ClampTo<int32_t>(v->GetDisplayProfitThisYear()), 16, 16);
867 case 0x55: return GB(ClampTo<int32_t>(v->GetDisplayProfitThisYear()), 24, 8);
868 case 0x56: return ClampTo<int32_t>(v->GetDisplayProfitLastYear());
869 case 0x57: return GB(ClampTo<int32_t>(v->GetDisplayProfitLastYear()), 8, 24);
870 case 0x58: return GB(ClampTo<int32_t>(v->GetDisplayProfitLastYear()), 16, 16);
871 case 0x59: return GB(ClampTo<int32_t>(v->GetDisplayProfitLastYear()), 24, 8);
872 case 0x5A: return (v->Next() == nullptr ? VehicleID::Invalid() : v->Next()->index).base();
873 case 0x5B: break; // not implemented
874 case 0x5C: return ClampTo<int32_t>(v->value);
875 case 0x5D: return GB(ClampTo<int32_t>(v->value), 8, 24);
876 case 0x5E: return GB(ClampTo<int32_t>(v->value), 16, 16);
877 case 0x5F: return GB(ClampTo<int32_t>(v->value), 24, 8);
878 case 0x60: break; // not implemented
879 case 0x61: break; // not implemented
880 case 0x62: break; // vehicle specific, see below
881 case 0x63: break; // not implemented
882 case 0x64: break; // vehicle specific, see below
883 case 0x65: break; // vehicle specific, see below
884 case 0x66: break; // vehicle specific, see below
885 case 0x67: break; // vehicle specific, see below
886 case 0x68: break; // vehicle specific, see below
887 case 0x69: break; // vehicle specific, see below
888 case 0x6A: break; // not implemented
889 case 0x6B: break; // not implemented
890 case 0x6C: break; // not implemented
891 case 0x6D: break; // not implemented
892 case 0x6E: break; // not implemented
893 case 0x6F: break; // not implemented
894 case 0x70: break; // not implemented
895 case 0x71: break; // not implemented
896 case 0x72: return v->cargo_subtype;
897 case 0x73: break; // vehicle specific, see below
898 case 0x74: break; // vehicle specific, see below
899 case 0x75: break; // vehicle specific, see below
900 case 0x76: break; // vehicle specific, see below
901 case 0x77: break; // vehicle specific, see below
902 case 0x78: break; // not implemented
903 case 0x79: break; // not implemented
904 case 0x7A: return v->random_bits;
905 case 0x7B: return v->waiting_random_triggers.base();
906 case 0x7C: break; // vehicle specific, see below
907 case 0x7D: break; // vehicle specific, see below
908 case 0x7E: break; // not implemented
909 case 0x7F: break; // vehicle specific, see below
910 }
911
912 /* Vehicle specific properties */
913 switch (v->type) {
914 case VEH_TRAIN: {
915 Train *t = Train::From(v);
916 switch (variable - 0x80) {
917 case 0x62: return t->track;
918 case 0x66: return t->railtypes.GetNthSetBit(0).value_or(RailType::INVALID_RAILTYPE);
919 case 0x73: return 0x80 + VEHICLE_LENGTH - t->gcache.cached_veh_length;
920 case 0x74: return t->gcache.cached_power;
921 case 0x75: return GB(t->gcache.cached_power, 8, 24);
922 case 0x76: return GB(t->gcache.cached_power, 16, 16);
923 case 0x77: return GB(t->gcache.cached_power, 24, 8);
924 case 0x7C: return t->First()->index.base();
925 case 0x7D: return GB(t->First()->index.base(), 8, 8);
926 case 0x7F: return 0; // Used for vehicle reversing hack in TTDP
927 }
928 break;
929 }
930
931 case VEH_ROAD: {
932 RoadVehicle *rv = RoadVehicle::From(v);
933 switch (variable - 0x80) {
934 case 0x62: return rv->state;
935 case 0x64: return rv->blocked_ctr;
936 case 0x65: return GB(rv->blocked_ctr, 8, 8);
937 case 0x66: return rv->overtaking;
938 case 0x67: return rv->overtaking_ctr;
939 case 0x68: return rv->crashed_ctr;
940 case 0x69: return GB(rv->crashed_ctr, 8, 8);
941 }
942 break;
943 }
944
945 case VEH_SHIP: {
946 Ship *s = Ship::From(v);
947 switch (variable - 0x80) {
948 case 0x62: return s->state;
949 }
950 break;
951 }
952
953 case VEH_AIRCRAFT: {
954 Aircraft *a = Aircraft::From(v);
955 switch (variable - 0x80) {
956 case 0x62: return MapAircraftMovementState(a); // Current movement state
957 case 0x63: return a->targetairport.base(); // Airport to which the action refers
958 case 0x66: return MapAircraftMovementAction(a); // Current movement action
959 }
960 break;
961 }
962
963 default: break;
964 }
965
966 Debug(grf, 1, "Unhandled vehicle variable 0x{:X}, type 0x{:X}", variable, (uint)v->type);
967
968 available = false;
969 return UINT_MAX;
970}
971
972/* virtual */ uint32_t VehicleScopeResolver::GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const
973{
974 if (this->v == nullptr) {
975 /* Vehicle does not exist, so we're in a purchase list */
976 switch (variable) {
977 case 0x43: return GetCompanyInfo(_current_company, LiveryHelper(this->self_type, nullptr)); // Owner information
978 case 0x46: return 0; // Motion counter
979 case 0x47: { // Vehicle cargo info
980 const Engine *e = Engine::Get(this->self_type);
981 CargoType cargo_type = e->GetDefaultCargoType();
982 if (IsValidCargoType(cargo_type)) {
983 const CargoSpec *cs = CargoSpec::Get(cargo_type);
984 return (cs->classes.base() << 16) | (cs->weight << 8) | this->ro.grffile->cargo_map[cargo_type];
985 } else {
986 return 0x000000FF;
987 }
988 }
989 case 0x48: return Engine::Get(this->self_type)->flags.base(); // Vehicle Type Info
990 case 0x49: return TimerGameCalendar::year.base(); // 'Long' format build year
991 case 0x4B: return TimerGameCalendar::date.base(); // Long date of last service
992
993 case 0x7A: return GetBadgeVariableResult(*this->ro.grffile, Engine::Get(this->self_type)->badges, parameter);
994
995 case 0x92: return ClampTo<uint16_t>(TimerGameCalendar::date - CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR); // Date of last service
996 case 0x93: return GB(ClampTo<uint16_t>(TimerGameCalendar::date - CalendarTime::DAYS_TILL_ORIGINAL_BASE_YEAR), 8, 8);
997 case 0xC4: return (Clamp(TimerGameCalendar::year, CalendarTime::ORIGINAL_BASE_YEAR, CalendarTime::ORIGINAL_MAX_YEAR) - CalendarTime::ORIGINAL_BASE_YEAR).base(); // Build year
998 case 0xC6: return Engine::Get(this->self_type)->grf_prop.local_id;
999 case 0xC7: return GB(Engine::Get(this->self_type)->grf_prop.local_id, 8, 8);
1000 case 0xDA: return VehicleID::Invalid().base(); // Next vehicle
1001 case 0xF2: return 0; // Cargo subtype
1002 }
1003
1004 available = false;
1005 return UINT_MAX;
1006 }
1007
1008 return VehicleGetVariable(const_cast<Vehicle*>(this->v), this, variable, parameter, available);
1009}
1010
1011
1012/* virtual */ const SpriteGroup *VehicleResolverObject::ResolveReal(const RealSpriteGroup &group) const
1013{
1014 const Vehicle *v = this->self_scope.v;
1015
1016 if (v == nullptr) {
1017 if (!group.loading.empty()) return group.loading[0];
1018 if (!group.loaded.empty()) return group.loaded[0];
1019 return nullptr;
1020 }
1021
1022 const Order &order = v->First()->current_order;
1023 bool not_loading = order.GetUnloadType() == OrderUnloadType::NoUnload && order.GetLoadType() == OrderLoadType::NoLoad;
1024 bool in_motion = !order.IsType(OT_LOADING) || not_loading;
1025
1026 uint totalsets = static_cast<uint>(in_motion ? group.loaded.size() : group.loading.size());
1027
1028 if (totalsets == 0) return nullptr;
1029
1030 uint set = (v->cargo.StoredCount() * totalsets) / std::max<uint16_t>(1u, v->cargo_cap);
1031 set = std::min(set, totalsets - 1);
1032
1033 return in_motion ? group.loaded[set] : group.loading[set];
1034}
1035
1036 GrfSpecFeature VehicleResolverObject::GetFeature() const
1037{
1038 switch (Engine::Get(this->self_scope.self_type)->type) {
1039 case VEH_TRAIN: return GSF_TRAINS;
1040 case VEH_ROAD: return GSF_ROADVEHICLES;
1041 case VEH_SHIP: return GSF_SHIPS;
1042 case VEH_AIRCRAFT: return GSF_AIRCRAFT;
1043 default: return GSF_INVALID;
1044 }
1045}
1046
1047uint32_t VehicleResolverObject::GetDebugID() const
1048{
1049 return Engine::Get(this->self_scope.self_type)->grf_prop.local_id;
1050}
1051
1057 static const GRFFile *GetEngineGrfFile(EngineID engine_type)
1058{
1059 const Engine *e = Engine::Get(engine_type);
1060 return (e != nullptr) ? e->GetGRF() : nullptr;
1061}
1062
1073VehicleResolverObject::VehicleResolverObject(EngineID engine_type, const Vehicle *v, WagonOverride wagon_override, bool rotor_in_gui,
1074 CallbackID callback, uint32_t callback_param1, uint32_t callback_param2)
1075 : SpecializedResolverObject<VehicleRandomTriggers>(GetEngineGrfFile(engine_type), callback, callback_param1, callback_param2),
1076 self_scope(*this, engine_type, v, rotor_in_gui),
1077 parent_scope(*this, engine_type, ((v != nullptr) ? v->First() : v), rotor_in_gui),
1078 relative_scope(*this, engine_type, v, rotor_in_gui),
1079 cached_relative_count(0)
1080{
1081 if (wagon_override == WO_SELF) {
1082 this->root_spritegroup = GetWagonOverrideSpriteSet(engine_type, CargoGRFFileProps::SG_DEFAULT, engine_type);
1083 } else {
1084 if (wagon_override != WO_NONE && v != nullptr && v->IsGroundVehicle()) {
1085 assert(v->engine_type == engine_type); // overrides make little sense with fake scopes
1086
1087 /* For trains we always use cached value, except for callbacks because the override spriteset
1088 * to use may be different than the one cached. It happens for callback 0x15 (refit engine),
1089 * as v->cargo_type is temporary changed to the new type */
1090 if (wagon_override == WO_CACHED && v->type == VEH_TRAIN) {
1091 this->root_spritegroup = Train::From(v)->tcache.cached_override;
1092 } else {
1093 this->root_spritegroup = GetWagonOverrideSpriteSet(v->engine_type, v->cargo_type, v->GetGroundVehicleCache()->first_engine);
1094 }
1095 }
1096
1097 if (this->root_spritegroup == nullptr) {
1098 const Engine *e = Engine::Get(engine_type);
1099 CargoType cargo = v != nullptr ? v->cargo_type : CargoGRFFileProps::SG_PURCHASE;
1100 this->root_spritegroup = e->grf_prop.GetFirstSpriteGroupOf({cargo, CargoGRFFileProps::SG_DEFAULT});
1101 }
1102 }
1103}
1104
1105static void GetCustomEngineSprite(EngineID engine, const Vehicle *v, Direction direction, EngineImageType image_type, VehicleSpriteSeq *result)
1106{
1107 VehicleResolverObject object(engine, v, VehicleResolverObject::WO_CACHED, false, CBID_NO_CALLBACK);
1108 result->Clear();
1109
1110 bool sprite_stack = EngInfo(engine)->misc_flags.Test(EngineMiscFlag::SpriteStack);
1111 uint max_stack = sprite_stack ? static_cast<uint>(std::size(result->seq)) : 1;
1112 for (uint stack = 0; stack < max_stack; ++stack) {
1113 object.callback_param1 = image_type | (stack << 8);
1114 const auto *group = object.Resolve<ResultSpriteGroup>();
1115 int32_t reg100 = sprite_stack ? object.GetRegister(0x100) : 0;
1116 if (group != nullptr && group->num_sprites != 0) {
1117 result->seq[result->count].sprite = group->sprite + (direction % group->num_sprites);
1118 result->seq[result->count].pal = GB(reg100, 0, 16); // zero means default recolouring
1119 result->count++;
1120 }
1121 if (!HasBit(reg100, 31)) break;
1122 }
1123}
1124
1125void GetCustomVehicleSprite(const Vehicle *v, Direction direction, EngineImageType image_type, VehicleSpriteSeq *result)
1126{
1127 GetCustomEngineSprite(v->engine_type, v, direction, image_type, result);
1128}
1129
1130void GetCustomVehicleIcon(EngineID engine, Direction direction, EngineImageType image_type, VehicleSpriteSeq *result)
1131{
1132 GetCustomEngineSprite(engine, nullptr, direction, image_type, result);
1133}
1134
1135static void GetRotorOverrideSprite(EngineID engine, const struct Aircraft *v, EngineImageType image_type, VehicleSpriteSeq *result)
1136{
1137 const Engine *e = Engine::Get(engine);
1138
1139 /* Only valid for helicopters */
1140 assert(e->type == VEH_AIRCRAFT);
1141 assert(!(e->VehInfo<AircraftVehicleInfo>().subtype & AIR_CTOL));
1142
1143 /* We differ from TTDPatch by resolving the sprite using the primary vehicle 'v', and not using the rotor vehicle 'v->Next()->Next()'.
1144 * TTDPatch copies some variables between the vehicles each time, to somehow synchronize the rotor vehicle with the primary vehicle.
1145 * We use 'rotor_in_gui' to replicate when the variables differ.
1146 * But some other variables like 'rotor state' and 'rotor speed' are not available in OpenTTD, while they are in TTDPatch. */
1147 bool rotor_in_gui = image_type != EIT_ON_MAP;
1148 VehicleResolverObject object(engine, v, VehicleResolverObject::WO_SELF, rotor_in_gui, CBID_NO_CALLBACK);
1149 result->Clear();
1150 uint rotor_pos = v == nullptr || rotor_in_gui ? 0 : v->Next()->Next()->state;
1151
1152 bool sprite_stack = e->info.misc_flags.Test(EngineMiscFlag::SpriteStack);
1153 uint max_stack = sprite_stack ? static_cast<uint>(std::size(result->seq)) : 1;
1154 for (uint stack = 0; stack < max_stack; ++stack) {
1155 object.callback_param1 = image_type | (stack << 8);
1156 const auto *group = object.Resolve<ResultSpriteGroup>();
1157 int32_t reg100 = sprite_stack ? object.GetRegister(0x100) : 0;
1158 if (group != nullptr && group->num_sprites != 0) {
1159 result->seq[result->count].sprite = group->sprite + (rotor_pos % group->num_sprites);
1160 result->seq[result->count].pal = GB(reg100, 0, 16); // zero means default recolouring
1161 result->count++;
1162 }
1163 if (!HasBit(reg100, 31)) break;
1164 }
1165}
1166
1167void GetCustomRotorSprite(const struct Aircraft *v, EngineImageType image_type, VehicleSpriteSeq *result)
1168{
1169 GetRotorOverrideSprite(v->engine_type, v, image_type, result);
1170}
1171
1172void GetCustomRotorIcon(EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
1173{
1174 GetRotorOverrideSprite(engine, nullptr, image_type, result);
1175}
1176
1182 bool UsesWagonOverride(const Vehicle *v)
1183{
1184 assert(v->type == VEH_TRAIN);
1185 return Train::From(v)->tcache.cached_override != nullptr;
1186}
1187
1198uint16_t GetVehicleCallback(CallbackID callback, uint32_t param1, uint32_t param2, EngineID engine, const Vehicle *v, std::span<int32_t> regs100)
1199{
1200 VehicleResolverObject object(engine, v, VehicleResolverObject::WO_UNCACHED, false, callback, param1, param2);
1201 return object.ResolveCallback(regs100);
1202}
1203
1215uint16_t GetVehicleCallbackParent(CallbackID callback, uint32_t param1, uint32_t param2, EngineID engine, const Vehicle *v, const Vehicle *parent, std::span<int32_t> regs100)
1216{
1217 VehicleResolverObject object(engine, v, VehicleResolverObject::WO_NONE, false, callback, param1, param2);
1218 object.parent_scope.SetVehicle(parent);
1219 return object.ResolveCallback(regs100);
1220}
1221
1222
1223/* Callback 36 handlers */
1224int GetVehicleProperty(const Vehicle *v, PropertyID property, int orig_value, bool is_signed)
1225{
1226 return GetEngineProperty(v->engine_type, property, orig_value, v, is_signed);
1227}
1228
1229
1230int GetEngineProperty(EngineID engine, PropertyID property, int orig_value, const Vehicle *v, bool is_signed)
1231{
1232 uint16_t callback = GetVehicleCallback(CBID_VEHICLE_MODIFY_PROPERTY, property, 0, engine, v);
1233 if (callback != CALLBACK_FAILED) {
1234 if (is_signed) {
1235 /* Sign extend 15 bit integer */
1236 return static_cast<int16_t>(callback << 1) / 2;
1237 } else {
1238 return callback;
1239 }
1240 }
1241
1242 return orig_value;
1243}
1244
1251std::optional<bool> TestVehicleBuildProbability(Vehicle *v, EngineID engine, BuildProbabilityType type)
1252{
1253 uint16_t p = GetVehicleCallback(CBID_VEHICLE_BUILD_PROBABILITY, to_underlying(type), 0, engine, v);
1254 if (p == CALLBACK_FAILED) return std::nullopt;
1255
1256 const uint16_t PROBABILITY_RANGE = 100;
1257 return p + RandomRange(PROBABILITY_RANGE) >= PROBABILITY_RANGE;
1258}
1259
1260static void DoTriggerVehicleRandomisation(Vehicle *v, VehicleRandomTrigger trigger, uint16_t base_random_bits, bool first)
1261{
1262 /* We can't trigger a non-existent vehicle... */
1263 assert(v != nullptr);
1264
1265 VehicleResolverObject object(v->engine_type, v, VehicleResolverObject::WO_CACHED, false, CBID_RANDOM_TRIGGER);
1266 v->waiting_random_triggers.Set(trigger); // store now for var 5F
1267 object.SetWaitingRandomTriggers(v->waiting_random_triggers);
1268
1269 object.ResolveRerandomisation();
1270
1271 /* Store remaining triggers. */
1272 v->waiting_random_triggers.Reset(object.GetUsedRandomTriggers());
1273
1274 /* Rerandomise bits. Scopes other than SELF are invalid for rerandomisation. For bug-to-bug-compatibility with TTDP we ignore the scope. */
1275 uint16_t new_random_bits = Random();
1276 uint32_t reseed = object.GetReseedSum();
1277 v->random_bits &= ~reseed;
1278 v->random_bits |= (first ? new_random_bits : base_random_bits) & reseed;
1279
1280 switch (trigger) {
1281 case VehicleRandomTrigger::NewCargo:
1282 /* All vehicles in chain get ANY_NEW_CARGO trigger now.
1283 * So we call it for the first one and they will recurse.
1284 * Indexing part of vehicle random bits needs to be
1285 * same for all triggered vehicles in the chain (to get
1286 * all the random-cargo wagons carry the same cargo,
1287 * i.e.), so we give them all the NEW_CARGO triggered
1288 * vehicle's portion of random bits. */
1289 assert(first);
1290 DoTriggerVehicleRandomisation(v->First(), VehicleRandomTrigger::AnyNewCargo, new_random_bits, false);
1291 break;
1292
1293 case VehicleRandomTrigger::Depot:
1294 /* We now trigger the next vehicle in chain recursively.
1295 * The random bits portions may be different for each
1296 * vehicle in chain. */
1297 if (v->Next() != nullptr) DoTriggerVehicleRandomisation(v->Next(), trigger, 0, true);
1298 break;
1299
1300 case VehicleRandomTrigger::Empty:
1301 /* We now trigger the next vehicle in chain
1302 * recursively. The random bits portions must be same
1303 * for each vehicle in chain, so we give them all
1304 * first chained vehicle's portion of random bits. */
1305 if (v->Next() != nullptr) DoTriggerVehicleRandomisation(v->Next(), trigger, first ? new_random_bits : base_random_bits, false);
1306 break;
1307
1308 case VehicleRandomTrigger::AnyNewCargo:
1309 /* Now pass the trigger recursively to the next vehicle
1310 * in chain. */
1311 assert(!first);
1312 if (v->Next() != nullptr) DoTriggerVehicleRandomisation(v->Next(), trigger, base_random_bits, false);
1313 break;
1314
1315 case VehicleRandomTrigger::Callback32:
1316 /* Do not do any recursion */
1317 break;
1318 }
1319}
1320
1321void TriggerVehicleRandomisation(Vehicle *v, VehicleRandomTrigger trigger)
1322{
1323 v->InvalidateNewGRFCacheOfChain();
1324 DoTriggerVehicleRandomisation(v, trigger, 0, true);
1325 v->InvalidateNewGRFCacheOfChain();
1326}
1327
1328/* Functions for changing the order of vehicle purchase lists */
1329
1330 struct ListOrderChange {
1331 EngineID engine;
1332 uint16_t target;
1333
1334 ListOrderChange(EngineID engine, uint16_t target) : engine(engine), target(target) {}
1335};
1336
1337static std::vector<ListOrderChange> _list_order_changes;
1338
1345 void AlterVehicleListOrder(EngineID engine, uint16_t target)
1346{
1347 /* Add the list order change to a queue */
1348 _list_order_changes.emplace_back(engine, target);
1349}
1350
1357 static bool EnginePreSort(const EngineID &a, const EngineID &b)
1358{
1359 const Engine &engine_a = *Engine::Get(a);
1360 const Engine &engine_b = *Engine::Get(b);
1361
1362 /* 1. Sort by engine type */
1363 if (engine_a.type != engine_b.type) return static_cast<int>(engine_a.type) < static_cast<int>(engine_b.type);
1364
1365 /* 2. Sort by scope-GRFID */
1366 if (engine_a.grf_prop.grfid != engine_b.grf_prop.grfid) return engine_a.grf_prop.grfid < engine_b.grf_prop.grfid;
1367
1368 /* 3. Sort by local ID */
1369 return static_cast<int>(engine_a.grf_prop.local_id) < static_cast<int>(engine_b.grf_prop.local_id);
1370}
1371
1375 void CommitVehicleListOrderChanges()
1376{
1377 /* Build a list of EngineIDs. EngineIDs are sequential from 0 up to the number of pool items with no gaps. */
1378 std::vector<EngineID> ordering(Engine::GetNumItems());
1379 std::iota(std::begin(ordering), std::end(ordering), EngineID::Begin());
1380
1381 /* Pre-sort engines by scope-grfid and local index */
1382 std::ranges::sort(ordering, EnginePreSort);
1383
1384 /* Apply Insertion-Sort operations */
1385 for (const ListOrderChange &loc : _list_order_changes) {
1386 EngineID source = loc.engine;
1387
1388 Engine *engine_source = Engine::Get(source);
1389 if (engine_source->grf_prop.local_id == loc.target) continue;
1390
1391 EngineID target = _engine_mngr.GetID(engine_source->type, loc.target, engine_source->grf_prop.grfid);
1392 if (target == EngineID::Invalid()) continue;
1393
1394 auto it_source = std::ranges::find(ordering, source);
1395 auto it_target = std::ranges::find(ordering, target);
1396
1397 assert(it_source != std::end(ordering) && it_target != std::end(ordering));
1398 assert(it_source != it_target);
1399
1400 /* Move just this item to before the target. */
1401 Slide(it_source, std::next(it_source), it_target);
1402 }
1403
1404 /* Store final sort-order */
1405 for (uint16_t index = 0; const EngineID &eid : ordering) {
1406 Engine::Get(eid)->list_position = index;
1407 ++index;
1408 }
1409
1410 /* Clear out the queue */
1411 _list_order_changes.clear();
1412 _list_order_changes.shrink_to_fit();
1413}
1414
1419 void FillNewGRFVehicleCache(const Vehicle *v)
1420{
1421 VehicleResolverObject ro(v->engine_type, v, VehicleResolverObject::WO_NONE);
1422
1423 /* These variables we have to check; these are the ones with a cache. */
1424 static const int cache_entries[][2] = {
1425 { 0x40, NCVV_POSITION_CONSIST_LENGTH },
1426 { 0x41, NCVV_POSITION_SAME_ID_LENGTH },
1427 { 0x42, NCVV_CONSIST_CARGO_INFORMATION },
1428 { 0x43, NCVV_COMPANY_INFORMATION },
1429 { 0x4D, NCVV_POSITION_IN_VEHICLE },
1430 };
1431 static_assert(NCVV_END == lengthof(cache_entries));
1432
1433 /* Resolve all the variables, so their caches are set. */
1434 for (const auto &cache_entry : cache_entries) {
1435 /* Only resolve when the cache isn't valid. */
1436 if (HasBit(v->grf_cache.cache_valid, cache_entry[1])) continue;
1437 bool stub;
1438 ro.GetScope(VSG_SCOPE_SELF)->GetVariable(cache_entry[0], 0, stub);
1439 }
1440
1441 /* Make sure really all bits are set. */
1442 assert(v->grf_cache.cache_valid == (1 << NCVV_END) - 1);
1443}
aircraft.h
Base for aircraft.
GetTargetAirportIfValid
Station * GetTargetAirportIfValid(const Aircraft *v)
Returns aircraft's target station if v->target_airport is a valid station with airport.
Definition aircraft_cmd.cpp:2163
AirportMovingDataFlag::HeliRaise
@ HeliRaise
Helicopter take-off.
AirportMovingDataFlag::HeliLower
@ HeliLower
Helicopter landing.
AirportMovingDataFlag::SlowTurn
@ SlowTurn
Turn slowly (mostly used in the air).
AirportMovingDataFlag::Brake
@ Brake
Taxiing at the airport.
AirportMovingDataFlag::Hold
@ Hold
Holding pattern movement (above the airport).
AirportMovingDataFlag::ExactPosition
@ ExactPosition
Go exactly to the destination coordinates.
HELITAKEOFF
@ HELITAKEOFF
Helicopter wants to leave the airport.
Definition airport.h:76
TERM4
@ TERM4
Heading for terminal 4.
Definition airport.h:68
STARTTAKEOFF
@ STARTTAKEOFF
Airplane has arrived at a runway for take-off.
Definition airport.h:74
HELIPAD2
@ HELIPAD2
Heading for helipad 2.
Definition airport.h:72
ENDTAKEOFF
@ ENDTAKEOFF
Airplane has reached end-point of the take-off runway.
Definition airport.h:75
TERM5
@ TERM5
Heading for terminal 5.
Definition airport.h:69
TERM6
@ TERM6
Heading for terminal 6.
Definition airport.h:70
TERM3
@ TERM3
Heading for terminal 3.
Definition airport.h:67
TERM8
@ TERM8
Heading for terminal 8.
Definition airport.h:83
HELIPAD3
@ HELIPAD3
Heading for helipad 3.
Definition airport.h:84
HELIPAD1
@ HELIPAD1
Heading for helipad 1.
Definition airport.h:71
TERM2
@ TERM2
Heading for terminal 2.
Definition airport.h:66
HANGAR
@ HANGAR
Heading for hangar.
Definition airport.h:64
FLYING
@ FLYING
Vehicle is flying in the air.
Definition airport.h:77
TAKEOFF
@ TAKEOFF
Airplane wants to leave the airport.
Definition airport.h:73
HELILANDING
@ HELILANDING
Helicopter wants to land.
Definition airport.h:80
ENDLANDING
@ ENDLANDING
Airplane wants to finish landing.
Definition airport.h:79
HELIENDLANDING
@ HELIENDLANDING
Helicopter wants to finish landing.
Definition airport.h:81
TERM1
@ TERM1
Heading for terminal 1.
Definition airport.h:65
LANDING
@ LANDING
Airplane wants to land.
Definition airport.h:78
TERM7
@ TERM7
Heading for terminal 7.
Definition airport.h:82
VehicleFlag::BuiltAsPrototype
@ BuiltAsPrototype
Vehicle is a prototype (accepted as exclusive preview).
VehicleFlag::CargoUnloading
@ CargoUnloading
Vehicle is unloading cargo.
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition bitmath_func.hpp:121
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
CargoType
uint8_t CargoType
Cargo slots to indicate a cargo type within a game.
Definition cargo_type.h:21
IsValidCargoType
bool IsValidCargoType(CargoType cargo)
Test whether cargo type is not INVALID_CARGO.
Definition cargo_type.h:104
NUM_CARGO
static const CargoType NUM_CARGO
Maximum number of cargo types in a game.
Definition cargo_type.h:73
BaseBitSet::Test
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
Definition base_bitset_type.hpp:141
BaseBitSet::base
constexpr Tstorage base() const noexcept
Retrieve the raw value behind this bit set.
Definition base_bitset_type.hpp:219
BaseBitSet::Reset
constexpr Timpl & Reset()
Reset all bits.
Definition base_bitset_type.hpp:83
BaseBitSet::Set
constexpr Timpl & Set()
Set all bits.
Definition base_bitset_type.hpp:40
BaseBitSet::GetNthSetBit
std::optional< Tvalue_type > GetNthSetBit(uint n) const
Get the value of the Nth set bit.
Definition base_bitset_type.hpp:247
CargoList::PeriodsInTransit
uint PeriodsInTransit() const
Returns average number of cargo aging periods in transit for a cargo entity.
Definition cargopacket.h:329
Engine
Definition engine_base.h:38
Engine::GetGRF
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition engine_base.h:166
Engine::grf_prop
CargoGRFFileProps grf_prop
Link to NewGRF.
Definition engine_base.h:71
Engine::flags
EngineFlags flags
Flags of the engine.
Definition engine_base.h:57
Engine::type
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition engine_base.h:62
Engine::GetDefaultCargoType
CargoType GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition engine_base.h:95
EnumBitSet< AirportMovingDataFlag, uint16_t >
RailTypeInfo
This struct contains all the info that is needed to draw and construct tracks.
Definition rail.h:115
RailTypeInfo::flags
RailTypeFlags flags
Bit mask of rail type flags.
Definition rail.h:200
RoadTypeInfo
Definition road.h:52
RoadTypeInfo::flags
RoadTypeFlags flags
Bit mask of road type flags.
Definition road.h:101
TimerGameCalendar::date
static Date date
Current date in days (day counter).
Definition timer_game_calendar.h:33
TimerGameCalendar::year
static Year year
Current year, starting at 0.
Definition timer_game_calendar.h:31
TimerGameConst< struct Calendar >::ORIGINAL_MAX_YEAR
static constexpr TimerGame< struct Calendar >::Year ORIGINAL_MAX_YEAR
The maximum year of the original TTD.
Definition timer_game_common.h:167
TimerGameConst< struct Calendar >::ORIGINAL_BASE_YEAR
static constexpr TimerGame< struct Calendar >::Year ORIGINAL_BASE_YEAR
The minimum starting year/base year of the original TTD.
Definition timer_game_common.h:163
TimerGameConst< struct Calendar >::DAYS_TILL_ORIGINAL_BASE_YEAR
static constexpr TimerGame< struct Calendar >::Date DAYS_TILL_ORIGINAL_BASE_YEAR
The date of the first day of the original base year.
Definition timer_game_common.h:184
VehicleCargoList::GetFirstStation
StationID GetFirstStation() const
Returns the first station of the first cargo packet in this list.
Definition cargopacket.h:396
VehicleCargoList::StoredCount
uint StoredCount() const
Returns sum of cargo on board the vehicle (ie not only reserved).
Definition cargopacket.h:425
company_base.h
Definition of stuff that is very close to a company, like the company struct itself.
_current_company
CompanyID _current_company
Company currently doing an action.
Definition company_cmd.cpp:56
company_func.h
Functions related to companies.
container_func.hpp
Some simple functions to help with accessing containers.
Slide
auto Slide(TIter first, TIter last, TIter position) -> std::pair< TIter, TIter >
Move elements between first and last to a new position, rotating elements in between as necessary.
Definition container_func.hpp:57
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
DirDifference
DirDiff DirDifference(Direction d0, Direction d1)
Calculate the difference between two directions.
Definition direction_func.h:68
ChangeDirDiff
DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
Applies two differences together.
Definition direction_func.h:88
DirDiff
DirDiff
Enumeration for the difference between two directions.
Definition direction_type.h:63
DIRDIFF_REVERSE
@ DIRDIFF_REVERSE
One direction is the opposite of the other one.
Definition direction_type.h:67
DIRDIFF_SAME
@ DIRDIFF_SAME
Both directions faces to the same direction.
Definition direction_type.h:64
Direction
Direction
Defines the 8 directions on the map.
Definition direction_type.h:24
DIR_W
@ DIR_W
West.
Definition direction_type.h:32
AIR_CTOL
@ AIR_CTOL
Conventional Take Off and Landing, i.e. planes.
Definition engine_type.h:127
EngineMiscFlag::SpriteStack
@ SpriteStack
Draw vehicle by stacking multiple sprites.
to_underlying
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition enum_type.hpp:17
BorderFlag::Random
@ Random
Randomise borders.
LIT_ALL
static const uint8_t LIT_ALL
Show the liveries of all companies.
Definition livery.h:19
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition math_func.hpp:79
GrfSpecFeature
GrfSpecFeature
Definition newgrf.h:68
GSF_INVALID
@ GSF_INVALID
An invalid spec feature.
Definition newgrf.h:99
ATP_TTDP_LARGE
@ ATP_TTDP_LARGE
Same as AT_LARGE.
Definition newgrf_airport.h:89
GetBadgeVariableResult
uint32_t GetBadgeVariableResult(const GRFFile &grffile, std::span< const BadgeID > badges, uint32_t parameter)
Test for a matching badge in a list of badges, returning the number of matching bits.
Definition newgrf_badge.cpp:225
newgrf_badge.h
Functions related to NewGRF badges.
CallbackID
CallbackID
List of implemented NewGRF callbacks.
Definition newgrf_callbacks.h:22
CBID_VEHICLE_BUILD_PROBABILITY
@ CBID_VEHICLE_BUILD_PROBABILITY
Called to determine probability during build.
Definition newgrf_callbacks.h:289
CBID_VEHICLE_SPAWN_VISUAL_EFFECT
@ CBID_VEHICLE_SPAWN_VISUAL_EFFECT
Called to spawn visual effects for vehicles.
Definition newgrf_callbacks.h:283
CBID_VEHICLE_COLOUR_MAPPING
@ CBID_VEHICLE_COLOUR_MAPPING
Called to determine if a specific colour map should be used for a vehicle instead of the default live...
Definition newgrf_callbacks.h:128
CBID_VEHICLE_32DAY_CALLBACK
@ CBID_VEHICLE_32DAY_CALLBACK
Called for every vehicle every 32 days (not all on same date though).
Definition newgrf_callbacks.h:146
CBID_NO_CALLBACK
@ CBID_NO_CALLBACK
Set when using the callback resolve system, but not to resolve a callback.
Definition newgrf_callbacks.h:24
CBID_VEHICLE_START_STOP_CHECK
@ CBID_VEHICLE_START_STOP_CHECK
Called when the company (or AI) tries to start or stop a vehicle.
Definition newgrf_callbacks.h:143
CBID_RANDOM_TRIGGER
@ CBID_RANDOM_TRIGGER
Set when calling a randomizing trigger (almost undocumented).
Definition newgrf_callbacks.h:27
CBID_TRAIN_ALLOW_WAGON_ATTACH
@ CBID_TRAIN_ALLOW_WAGON_ATTACH
Determine whether a wagon can be attached to an already existing train.
Definition newgrf_callbacks.h:74
CBID_VEHICLE_MODIFY_PROPERTY
@ CBID_VEHICLE_MODIFY_PROPERTY
Called to modify various vehicle properties.
Definition newgrf_callbacks.h:161
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition newgrf_callbacks.h:439
newgrf_cargo.h
Cargo support for NewGRFs.
GetCompanyInfo
uint32_t GetCompanyInfo(CompanyID owner, const Livery *l)
Returns company information like in vehicle var 43 or station var 43.
Definition newgrf_commons.cpp:457
SetEngineGRF
void SetEngineGRF(EngineID engine, const GRFFile *file)
Tie a GRFFile entry to an engine, to allow us to retrieve GRF parameters etc during a game.
Definition newgrf_engine.cpp:71
PositionHelper
static uint32_t PositionHelper(const Vehicle *v, bool consecutive)
Helper to get the position of a vehicle within a chain of vehicles.
Definition newgrf_engine.cpp:396
EnginePreSort
static bool EnginePreSort(const EngineID &a, const EngineID &b)
Comparator function to sort engines via scope-GRFID and local ID.
Definition newgrf_engine.cpp:1357
FillNewGRFVehicleCache
void FillNewGRFVehicleCache(const Vehicle *v)
Fill the grf_cache of the given vehicle.
Definition newgrf_engine.cpp:1419
GetVehicleCallbackParent
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.
Definition newgrf_engine.cpp:1215
TestVehicleBuildProbability
std::optional< bool > TestVehicleBuildProbability(Vehicle *v, EngineID engine, BuildProbabilityType type)
Test for vehicle build probability type.
Definition newgrf_engine.cpp:1251
UsesWagonOverride
bool UsesWagonOverride(const Vehicle *v)
Check if a wagon is currently using a wagon override.
Definition newgrf_engine.cpp:1182
LiveryHelper
static const Livery * LiveryHelper(EngineID engine, const Vehicle *v)
Determines the livery of an engine.
Definition newgrf_engine.cpp:373
MapAircraftMovementState
static uint8_t MapAircraftMovementState(const Aircraft *v)
Map OTTD aircraft movement states to TTDPatch style movement states (VarAction 2 Variable 0xE2)
Definition newgrf_engine.cpp:133
GetVehicleCallback
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.
Definition newgrf_engine.cpp:1198
MapAircraftMovementAction
static uint8_t MapAircraftMovementAction(const Aircraft *v)
Map OTTD aircraft movement states to TTDPatch style movement actions (VarAction 2 Variable 0xE6) This...
Definition newgrf_engine.cpp:260
CommitVehicleListOrderChanges
void CommitVehicleListOrderChanges()
Determine default engine sorting and execute recorded ListOrderChanges from AlterVehicleListOrder.
Definition newgrf_engine.cpp:1375
GetEngineGrfFile
static const GRFFile * GetEngineGrfFile(EngineID engine_type)
Get the grf file associated with an engine type.
Definition newgrf_engine.cpp:1057
AlterVehicleListOrder
void AlterVehicleListOrder(EngineID engine, uint16_t target)
Record a vehicle ListOrderChange.
Definition newgrf_engine.cpp:1345
PropertyID
PropertyID
List of NewGRF properties used in Action 0 or Callback 0x36 (CBID_VEHICLE_MODIFY_PROPERTY).
Definition newgrf_properties.h:18
GetReverseRailTypeTranslation
uint8_t GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
Perform a reverse railtype lookup to get the GRF internal ID.
Definition newgrf_railtype.cpp:173
GetRailTypeTranslation
RailType GetRailTypeTranslation(uint8_t railtype, const GRFFile *grffile)
Translate an index to the GRF-local railtype-translation table into a RailType.
Definition newgrf_railtype.cpp:151
newgrf_railtype.h
NewGRF handling of rail types.
GetReverseRoadTypeTranslation
uint8_t GetReverseRoadTypeTranslation(RoadType roadtype, const GRFFile *grffile)
Perform a reverse roadtype lookup to get the GRF internal ID.
Definition newgrf_roadtype.cpp:201
GetRoadTypeTranslation
RoadType GetRoadTypeTranslation(RoadTramType rtt, uint8_t tracktype, const GRFFile *grffile)
Translate an index to the GRF-local road/tramtype-translation table into a RoadType.
Definition newgrf_roadtype.cpp:171
newgrf_roadtype.h
NewGRF handling of road types.
newgrf_spritegroup.h
Action 2 handling.
VarSpriteGroupScope
VarSpriteGroupScope
Definition newgrf_spritegroup.h:82
VSG_SCOPE_SELF
@ VSG_SCOPE_SELF
Resolved object itself.
Definition newgrf_spritegroup.h:85
VSG_SCOPE_PARENT
@ VSG_SCOPE_PARENT
Related object of the resolved one.
Definition newgrf_spritegroup.h:86
VSG_SCOPE_RELATIVE
@ VSG_SCOPE_RELATIVE
Relative position (vehicles only)
Definition newgrf_spritegroup.h:87
OrderUnloadType::NoUnload
@ NoUnload
Totally no unloading will be done.
OrderLoadType::NoLoad
@ NoLoad
Do not load anything.
GetTileRailType
RailType GetTileRailType(Tile tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition rail.cpp:37
IsCompatibleRail
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:351
HasPowerOnRail
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:376
GetRailTypeInfo
const RailTypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition rail.h:300
RailTypeFlag::Catenary
@ Catenary
Bit number for drawing a catenary.
GetRailType
RailType GetRailType(Tile t)
Gets the rail type of the given tile.
Definition rail_map.h:115
RailType
RailType
Enumeration for all possible railtypes.
Definition rail_type.h:25
INVALID_RAILTYPE
@ INVALID_RAILTYPE
Flag for invalid railtype.
Definition rail_type.h:32
random_func.hpp
Pseudo random number generator.
RandomRange
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.
Definition random_func.hpp:88
HasPowerOnRoad
bool HasPowerOnRoad(RoadType enginetype, RoadType tiletype)
Checks if an engine of the given RoadType got power on a tile with a given RoadType.
Definition road.h:230
RoadTypeFlag::Catenary
@ Catenary
Bit number for adding catenary.
GetRoadTypeInfo
const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition road.h:215
RoadTramType
RoadTramType
The different types of road type.
Definition road_type.h:37
RoadType
RoadType
The different roadtypes we support.
Definition road_type.h:23
INVALID_ROADTYPE
@ INVALID_ROADTYPE
flag for invalid roadtype
Definition road_type.h:28
roadveh.h
Road vehicle states.
safeguards.h
A number of safeguards to prevent using unsafe methods.
ship.h
Base for ships.
station_base.h
Base classes/functions for stations.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
lengthof
#define lengthof(array)
Return the length of an fixed size array.
Definition stdafx.h:271
AircraftVehicleInfo
Information about a aircraft vehicle.
Definition engine_type.h:132
AircraftVehicleInfo::subtype
uint8_t subtype
Type of aircraft.
Definition engine_type.h:136
Aircraft
Aircraft, helicopters, rotors and their shadows belong to this class.
Definition aircraft.h:72
Aircraft::pos
uint8_t pos
Next desired position of the aircraft.
Definition aircraft.h:74
Aircraft::state
uint8_t state
State of the airport.
Definition aircraft.h:77
Aircraft::turn_counter
uint8_t turn_counter
Ticks between each turn to prevent > 45 degree turns.
Definition aircraft.h:80
Aircraft::IsNormalAircraft
bool IsNormalAircraft() const
Check if the aircraft type is a normal flying device; eg not a rotor or a shadow.
Definition aircraft.h:121
Aircraft::targetairport
StationID targetairport
Airport to go to next.
Definition aircraft.h:76
AirportFTAClass
Finite sTate mAchine (FTA) of an airport.
Definition airport.h:159
AirportFTAClass::MovingData
const AirportMovingData * MovingData(uint8_t position) const
Get movement data at a position.
Definition airport.h:184
AirportFTAClass::delta_z
uint8_t delta_z
Z adjustment for helicopter pads.
Definition airport.h:197
AirportMovingData::flags
AirportMovingDataFlags flags
special flags when moving towards the destination.
Definition airport.h:139
AirportSpec::ttd_airport_type
TTDPAirportType ttd_airport_type
ttdpatch airport type (Small/Large/Helipad/Oilrig)
Definition newgrf_airport.h:120
Airport::GetFTA
const AirportFTAClass * GetFTA() const
Get the finite-state machine for this airport or the finite-state machine for the dummy airport in ca...
Definition station_base.h:398
Airport::GetSpec
const AirportSpec * GetSpec() const
Get the AirportSpec that from the airport type of this airport.
Definition station_base.h:386
BaseConsist::cur_real_order_index
VehicleOrderID cur_real_order_index
The index to the current real (non-implicit) order.
Definition base_consist.h:47
BaseConsist::vehicle_flags
VehicleFlags vehicle_flags
Used for gradual loading and other miscellaneous things (.
Definition base_consist.h:50
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition vehicle_type.h:51
CargoGRFFileProps::SG_PURCHASE
static constexpr CargoType SG_PURCHASE
Used in purchase lists before an item exists.
Definition newgrf_commons.h:457
CargoGRFFileProps::SG_DEFAULT
static constexpr CargoType SG_DEFAULT
Default type used when no more-specific cargo matches.
Definition newgrf_commons.h:456
CargoSpec
Specification of a cargo type.
Definition cargotype.h:74
CargoSpec::classes
CargoClasses classes
Classes of this cargo type.
Definition cargotype.h:81
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo type.
Definition cargotype.h:137
CargoSpec::weight
uint8_t weight
Weight of a single unit of this cargo type in 1/16 ton (62.5 kg).
Definition cargotype.h:79
EngineInfo::misc_flags
EngineMiscFlags misc_flags
Miscellaneous flags.
Definition engine_type.h:209
EngineOverrideManager::GetID
EngineID GetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid)
Looks up an EngineID in the EngineOverrideManager.
Definition engine.cpp:529
GRFFilePropsBase::local_id
uint16_t local_id
id defined by the grf file for this entity
Definition newgrf_commons.h:316
GRFFilePropsBase::grfid
uint32_t grfid
grfid that introduced this entity.
Definition newgrf_commons.h:317
GRFFilePropsBase::SetGRFFile
void SetGRFFile(const struct GRFFile *grffile)
Set the NewGRF file, and its grfid, associated with grf props.
Definition newgrf_commons.cpp:735
GRFFile
Dynamic data of a loaded NewGRF.
Definition newgrf.h:114
GRFFile::cargo_map
std::array< uint8_t, NUM_CARGO > cargo_map
Inverse cargo translation table (CargoType -> local ID)
Definition newgrf.h:136
GRFFile::badge_list
std::vector< BadgeID > badge_list
Badge translation table (local index -> global index)
Definition newgrf.h:138
GroundVehicleCache::first_engine
EngineID first_engine
Cached EngineID of the front vehicle. EngineID::Invalid() for the front vehicle itself.
Definition ground_vehicle.hpp:44
GroundVehicleCache::cached_power
uint32_t cached_power
Total power of the consist (valid only for the first engine).
Definition ground_vehicle.hpp:39
GroundVehicleCache::cached_veh_length
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...
Definition ground_vehicle.hpp:45
GroundVehicle::gcache
GroundVehicleCache gcache
Cache of often calculated values.
Definition ground_vehicle.hpp:83
GroundVehicle::IsEngine
bool IsEngine() const
Check if a vehicle is an engine (can be first in a consist).
Definition ground_vehicle.hpp:318
ListOrderChange
Definition newgrf_engine.cpp:1330
ListOrderChange::target
uint16_t target
GRF-local ID.
Definition newgrf_engine.cpp:1332
ListOrderChange::engine
EngineID engine
Engine ID.
Definition newgrf_engine.cpp:1331
Livery
Information about a particular livery.
Definition livery.h:79
NewGRFCache::position_in_vehicle
uint32_t position_in_vehicle
Cache for NewGRF var 4D.
Definition vehicle_base.h:61
NewGRFCache::position_consist_length
uint32_t position_consist_length
Cache for NewGRF var 40.
Definition vehicle_base.h:57
NewGRFCache::consist_cargo_information
uint32_t consist_cargo_information
Cache for NewGRF var 42. (Note: The cargotype is untranslated in the cache because the accessing GRF ...
Definition vehicle_base.h:59
NewGRFCache::cache_valid
uint8_t cache_valid
Bitset that indicates which cache values are valid.
Definition vehicle_base.h:62
NewGRFCache::company_information
uint32_t company_information
Cache for NewGRF var 43.
Definition vehicle_base.h:60
NewGRFCache::position_same_id_length
uint32_t position_same_id_length
Cache for NewGRF var 41.
Definition vehicle_base.h:58
Order
Definition order_base.h:33
Order::MapOldOrder
uint16_t MapOldOrder() const
Pack this order into a 16 bits integer as close to the TTD representation as possible.
Definition order_cmd.cpp:180
Order::GetDestination
DestinationID GetDestination() const
Gets the destination of this order.
Definition order_base.h:99
Order::IsType
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition order_base.h:66
Order::GetUnloadType
OrderUnloadType GetUnloadType() const
How must the consist be unloaded?
Definition order_base.h:144
Order::GetLoadType
OrderLoadType GetLoadType() const
How must the consist be loaded?
Definition order_base.h:142
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition tilearea_type.h:19
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF >
Pool::PoolItem<&_engine_pool >::Get
static Titem * Get(auto index)
Returns Titem with given index.
Definition pool_type.hpp:387
Pool::PoolItem<&_engine_pool >::GetNumItems
static size_t GetNumItems()
Returns number of valid items in the pool.
Definition pool_type.hpp:417
Pool::PoolItem::index
Tindex index
Index of this pool item.
Definition pool_type.hpp:286
Pool::PoolItem<&_company_pool >::IsValidID
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition pool_type.hpp:376
RealSpriteGroup
Definition newgrf_spritegroup.h:64
RealSpriteGroup::loaded
std::vector< const SpriteGroup * > loaded
List of loaded groups (can be SpriteIDs or Callback results)
Definition newgrf_spritegroup.h:74
RealSpriteGroup::loading
std::vector< const SpriteGroup * > loading
List of loading groups (can be SpriteIDs or Callback results)
Definition newgrf_spritegroup.h:75
ResolverObject::grffile
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
Definition newgrf_spritegroup.h:352
ResolverObject::GetRegister
int32_t GetRegister(uint i) const
Gets the value of a so-called newgrf "register".
Definition newgrf_spritegroup.h:324
ResolverObject::callback
CallbackID callback
Callback being resolved.
Definition newgrf_spritegroup.h:340
ResolverObject::root_spritegroup
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
Definition newgrf_spritegroup.h:353
ResolverObject::GetScope
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0)
Get a resolver for the scope.
Definition newgrf_spritegroup.cpp:133
ResultSpriteGroup
Definition newgrf_spritegroup.h:217
RoadVehicle
Buses, trucks and trams belong to this class.
Definition roadveh.h:98
RoadVehicle::state
uint8_t state
Definition roadveh.h:100
RoadVehicle::crashed_ctr
uint16_t crashed_ctr
Animation counter when the vehicle has crashed.
Definition roadveh.h:105
RoadVehicle::overtaking_ctr
uint8_t overtaking_ctr
The length of the current overtake attempt.
Definition roadveh.h:104
RoadVehicle::overtaking
uint8_t overtaking
Set to RVSB_DRIVE_SIDE when overtaking, otherwise 0.
Definition roadveh.h:103
ScopeResolver
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope).
Definition newgrf_spritegroup.h:269
ScopeResolver::GetVariable
virtual uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const
Get a variable value.
Definition newgrf_spritegroup.cpp:104
ScopeResolver::ro
ResolverObject & ro
Surrounding resolver object.
Definition newgrf_spritegroup.h:270
Ship
All ships have this type.
Definition ship.h:32
Ship::state
TrackBits state
The "track" the ship is following.
Definition ship.h:34
SpecializedResolverObject
Specialization of ResolverObject with type-safe access to RandomTriggers.
Definition newgrf_spritegroup.h:450
SpecializedVehicle::Next
T * Next() const
Get next vehicle in the chain.
Definition vehicle_base.h:1049
SpecializedVehicle::From
static T * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition vehicle_base.h:1134
SpecializedVehicle::First
T * First() const
Get the first vehicle in the chain.
Definition vehicle_base.h:1031
SpriteGroup
Definition newgrf_spritegroup.h:47
SpriteGroup::Resolve
virtual ResolverResult Resolve(ResolverObject &object) const =0
Base sprite group resolver.
Station
Station data structure.
Definition station_base.h:520
Station::airport
Airport airport
Tile area the airport covers.
Definition station_base.h:534
StrongType::Typedef< uint32_t, struct BadgeIDTag, StrongType::Compare >
Train
'Train' is either a loco or a wagon.
Definition train.h:91
VariableGRFFileProps::GetFirstSpriteGroupOf
const struct SpriteGroup * GetFirstSpriteGroupOf(std::initializer_list< Tkey > indices) const
Get the first existing SpriteGroup from a list of options.
Definition newgrf_commons.h:427
VariableGRFFileProps::GetSpriteGroup
const SpriteGroup * GetSpriteGroup(Tkey index) const
Get the SpriteGroup at the specified index.
Definition newgrf_commons.h:415
VariableGRFFileProps::SetSpriteGroup
void SetSpriteGroup(Tkey index, const SpriteGroup *spritegroup)
Set the SpriteGroup at the specified index.
Definition newgrf_commons.h:441
VehicleCache::cached_max_speed
uint16_t cached_max_speed
Maximum speed of the consist (minimum of the max speed of all vehicles in the consist).
Definition vehicle_base.h:93
VehicleResolverObject
Resolver for a vehicle (chain)
Definition newgrf_engine.h:47
VehicleResolverObject::self_scope
VehicleScopeResolver self_scope
Scope resolver for the indicated vehicle.
Definition newgrf_engine.h:56
VehicleResolverObject::GetScope
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, uint8_t relative=0) override
Get a resolver for the scope.
Definition newgrf_engine.cpp:318
VehicleResolverObject::WO_NONE
@ WO_NONE
Resolve no wagon overrides.
Definition newgrf_engine.h:50
VehicleResolverObject::WO_SELF
@ WO_SELF
Resolve self-override (helicopter rotors and such).
Definition newgrf_engine.h:53
VehicleResolverObject::WO_UNCACHED
@ WO_UNCACHED
Resolve wagon overrides.
Definition newgrf_engine.h:51
VehicleResolverObject::WO_CACHED
@ WO_CACHED
Resolve wagon overrides using TrainCache::cached_override.
Definition newgrf_engine.h:52
VehicleResolverObject::parent_scope
VehicleScopeResolver parent_scope
Scope resolver for its parent vehicle.
Definition newgrf_engine.h:57
VehicleResolverObject::GetFeature
GrfSpecFeature GetFeature() const override
Get the feature number being resolved for.
Definition newgrf_engine.cpp:1036
VehicleResolverObject::VehicleResolverObject
VehicleResolverObject(EngineID engine_type, const Vehicle *v, WagonOverride wagon_override, bool rotor_in_gui=false, CallbackID callback=CBID_NO_CALLBACK, uint32_t callback_param1=0, uint32_t callback_param2=0)
Resolver of a vehicle (chain).
Definition newgrf_engine.cpp:1073
VehicleResolverObject::ResolveReal
const SpriteGroup * ResolveReal(const RealSpriteGroup &group) const override
Get the real sprites of the grf.
Definition newgrf_engine.cpp:1012
VehicleResolverObject::relative_scope
VehicleScopeResolver relative_scope
Scope resolver for an other vehicle in the chain.
Definition newgrf_engine.h:59
VehicleResolverObject::GetDebugID
uint32_t GetDebugID() const override
Get an identifier for the item being resolved.
Definition newgrf_engine.cpp:1047
VehicleScopeResolver
Resolver for a vehicle scope.
Definition newgrf_engine.h:22
VehicleScopeResolver::GetRandomTriggers
uint32_t GetRandomTriggers() const override
Get the triggers.
Definition newgrf_engine.cpp:312
VehicleScopeResolver::v
const struct Vehicle * v
The vehicle being resolved.
Definition newgrf_engine.h:23
VehicleScopeResolver::self_type
EngineID self_type
Type of the vehicle.
Definition newgrf_engine.h:24
VehicleScopeResolver::GetVariable
uint32_t GetVariable(uint8_t variable, uint32_t parameter, bool &available) const override
Get a variable value.
Definition newgrf_engine.cpp:972
VehicleScopeResolver::GetRandomBits
uint32_t GetRandomBits() const override
Get a few random bits.
Definition newgrf_engine.cpp:307
VehicleSpriteSeq
Sprite sequence for a vehicle part.
Definition vehicle_base.h:102
VehicleSpriteSeq::Clear
void Clear()
Clear all information.
Definition vehicle_base.h:122
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205
Vehicle::GetDisplayProfitThisYear
Money GetDisplayProfitThisYear() const
Gets the profit vehicle had this year.
Definition vehicle_base.h:572
Vehicle::engine_type
EngineID engine_type
The type of engine used for this vehicle.
Definition vehicle_base.h:278
Vehicle::z_pos
int32_t z_pos
z coordinate.
Definition vehicle_base.h:267
Vehicle::direction
Direction direction
facing
Definition vehicle_base.h:268
Vehicle::GetEngine
const Engine * GetEngine() const
Retrieves the engine of the vehicle.
Definition vehicle.cpp:718
Vehicle::cargo
VehicleCargoList cargo
The cargo this vehicle is carrying.
Definition vehicle_base.h:296
Vehicle::cargo_cap
uint16_t cargo_cap
total capacity
Definition vehicle_base.h:299
Vehicle::GetNumOrders
VehicleOrderID GetNumOrders() const
Get the number of orders this vehicle has.
Definition vehicle_base.h:705
Vehicle::subtype
uint8_t subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
Definition vehicle_base.h:310
Vehicle::random_bits
uint16_t random_bits
Bits used for randomized variational spritegroups.
Definition vehicle_base.h:290
Vehicle::day_counter
uint8_t day_counter
Increased by one for each day.
Definition vehicle_base.h:304
Vehicle::HasArticulatedPart
bool HasArticulatedPart() const
Check if an engine has an articulated part.
Definition vehicle_base.h:931
Vehicle::breakdown_ctr
uint8_t breakdown_ctr
Counter for managing breakdown events.
Definition vehicle_base.h:260
Vehicle::breakdown_delay
uint8_t breakdown_delay
Counter for managing breakdown length.
Definition vehicle_base.h:261
Vehicle::IsGroundVehicle
bool IsGroundVehicle() const
Check if the vehicle is a ground vehicle.
Definition vehicle_base.h:470
Vehicle::vehstatus
VehStates vehstatus
Status.
Definition vehicle_base.h:309
Vehicle::date_of_last_service_newgrf
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...
Definition vehicle_base.h:257
Vehicle::subspeed
uint8_t subspeed
fractional speed
Definition vehicle_base.h:284
Vehicle::IsArticulatedPart
bool IsArticulatedPart() const
Check if the vehicle is an articulated part of an engine.
Definition vehicle_base.h:922
Vehicle::Move
Vehicle * Move(int n)
Get the vehicle at offset n of this vehicle chain.
Definition vehicle_base.h:629
Vehicle::cargo_type
CargoType cargo_type
type of cargo this vehicle is carrying
Definition vehicle_base.h:297
Vehicle::acceleration
uint8_t acceleration
used by train & aircraft
Definition vehicle_base.h:285
Vehicle::First
Vehicle * First() const
Get the first vehicle of this vehicle chain.
Definition vehicle_base.h:600
Vehicle::current_order
Order current_order
The current order (+ status, like: loading)
Definition vehicle_base.h:311
Vehicle::Next
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition vehicle_base.h:587
Vehicle::y_pos
int32_t y_pos
y coordinate.
Definition vehicle_base.h:266
Vehicle::x_pos
int32_t x_pos
x coordinate.
Definition vehicle_base.h:265
Vehicle::value
Money value
Value of the vehicle.
Definition vehicle_base.h:236
Vehicle::waiting_random_triggers
VehicleRandomTriggers waiting_random_triggers
Triggers to be yet matched before rerandomizing the random bits.
Definition vehicle_base.h:289
Vehicle::vcache
VehicleCache vcache
Cache of often used vehicle values.
Definition vehicle_base.h:319
Vehicle::motion_counter
uint32_t motion_counter
counter to occasionally play a vehicle sound.
Definition vehicle_base.h:286
Vehicle::grf_cache
NewGRFCache grf_cache
Cache of often used calculated NewGRF values.
Definition vehicle_base.h:318
Vehicle::GetGRFID
uint32_t GetGRFID() const
Retrieve the GRF ID of the NewGRF the vehicle is tied to.
Definition vehicle.cpp:738
Vehicle::GetGroundVehicleCache
GroundVehicleCache * GetGroundVehicleCache()
Access the ground vehicle cache of the vehicle.
Definition vehicle.cpp:3143
Vehicle::GetCurrentMaxSpeed
virtual int GetCurrentMaxSpeed() const
Calculates the maximum speed of the vehicle under its current conditions.
Definition vehicle_base.h:491
Vehicle::IsPrimaryVehicle
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition vehicle_base.h:432
Vehicle::load_unload_ticks
uint16_t load_unload_ticks
Ticks to wait before starting next cycle.
Definition vehicle_base.h:307
Vehicle::GetDisplayProfitLastYear
Money GetDisplayProfitLastYear() const
Gets the profit vehicle had last year.
Definition vehicle_base.h:578
Vehicle::spritenum
uint8_t spritenum
currently displayed sprite index 0xfd == custom sprite, 0xfe == custom second head sprite 0xff == res...
Definition vehicle_base.h:276
Vehicle::cur_speed
uint16_t cur_speed
current speed
Definition vehicle_base.h:283
Vehicle::cargo_subtype
uint8_t cargo_subtype
Used for livery refits (NewGRF variations)
Definition vehicle_base.h:298
Vehicle::age
TimerGameCalendar::Date age
Age in calendar days.
Definition vehicle_base.h:253
Vehicle::breakdowns_since_last_service
uint8_t breakdowns_since_last_service
Counter for the amount of breakdowns.
Definition vehicle_base.h:262
Vehicle::max_age
TimerGameCalendar::Date max_age
Maximum age.
Definition vehicle_base.h:255
Vehicle::reliability
uint16_t reliability
Reliability.
Definition vehicle_base.h:258
Vehicle::Previous
Vehicle * Previous() const
Get the previous vehicle of this vehicle.
Definition vehicle_base.h:594
Vehicle::reliability_spd_dec
uint16_t reliability_spd_dec
Reliability decrease speed.
Definition vehicle_base.h:259
Vehicle::tick_counter
uint8_t tick_counter
Increased by one for each tick.
Definition vehicle_base.h:305
Vehicle::tile
TileIndex tile
Current tile index.
Definition vehicle_base.h:225
Vehicle::last_station_visited
StationID last_station_visited
The last station we stopped at.
Definition vehicle_base.h:292
Vehicle::InvalidateNewGRFCacheOfChain
void InvalidateNewGRFCacheOfChain()
Invalidates cached NewGRF variables of all vehicles in the chain (after the current vehicle)
Definition vehicle_base.h:459
Vehicle::breakdown_chance
uint8_t breakdown_chance
Current chance of breakdowns.
Definition vehicle_base.h:263
Vehicle::build_year
TimerGameCalendar::Year build_year
Year the vehicle has been built.
Definition vehicle_base.h:252
Vehicle::owner
Owner owner
Which company owns the vehicle?
Definition vehicle_base.h:270
Vehicle::unitnumber
UnitID unitnumber
unit number, for display purposes only
Definition vehicle_base.h:281
WagonOverride
Definition engine_base.h:20
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95
timer_game_calendar.h
Definition of the game-calendar-timer.
train.h
Base for the train class.
Reversed
@ Reversed
Used for vehicle var 0xFE bit 8 (toggled each time the train is reversed, accurate for first vehicle ...
Definition train.h:31
Flipped
@ Flipped
Reverse the visible direction of the vehicle.
Definition train.h:28
PoweredWagon
@ PoweredWagon
Wagon is powered.
Definition train.h:27
GetEngineLivery
const Livery * GetEngineLivery(EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v, uint8_t livery_setting)
Determines the livery for a vehicle.
Definition vehicle.cpp:2045
VehState::Hidden
@ Hidden
Vehicle is not visible.
NCVV_COMPANY_INFORMATION
@ NCVV_COMPANY_INFORMATION
This bit will be set if the NewGRF var 43 currently stored is valid.
Definition vehicle_base.h:49
NCVV_CONSIST_CARGO_INFORMATION
@ NCVV_CONSIST_CARGO_INFORMATION
This bit will be set if the NewGRF var 42 currently stored is valid.
Definition vehicle_base.h:48
NCVV_POSITION_CONSIST_LENGTH
@ NCVV_POSITION_CONSIST_LENGTH
This bit will be set if the NewGRF var 40 currently stored is valid.
Definition vehicle_base.h:46
NCVV_POSITION_SAME_ID_LENGTH
@ NCVV_POSITION_SAME_ID_LENGTH
This bit will be set if the NewGRF var 41 currently stored is valid.
Definition vehicle_base.h:47
NCVV_POSITION_IN_VEHICLE
@ NCVV_POSITION_IN_VEHICLE
This bit will be set if the NewGRF var 4D currently stored is valid.
Definition vehicle_base.h:50
NCVV_END
@ NCVV_END
End of the bits.
Definition vehicle_base.h:51
vehicle_func.h
Functions related to vehicles.
CUSTOM_VEHICLE_SPRITENUM
@ CUSTOM_VEHICLE_SPRITENUM
Vehicle sprite from NewGRF.
Definition vehicle_func.h:28
CUSTOM_VEHICLE_SPRITENUM_REVERSED
@ CUSTOM_VEHICLE_SPRITENUM_REVERSED
Vehicle sprite from NewGRF with reverse driving direction (from articulation callback)
Definition vehicle_func.h:29
EngineImageType
EngineImageType
Visualisation contexts of vehicles and engines.
Definition vehicle_type.h:74
EIT_ON_MAP
@ EIT_ON_MAP
Vehicle drawn in viewport.
Definition vehicle_type.h:75
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition vehicle_type.h:26
VEH_DISASTER
@ VEH_DISASTER
Disaster vehicle type.
Definition vehicle_type.h:33
VEH_AIRCRAFT
@ VEH_AIRCRAFT
Aircraft vehicle type.
Definition vehicle_type.h:28
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition vehicle_type.h:27
VEH_EFFECT
@ VEH_EFFECT
Effect vehicle type (smoke, explosions, sparks, bubbles)
Definition vehicle_type.h:32
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition vehicle_type.h:25
VehicleRandomTrigger
VehicleRandomTrigger
Randomisation triggers for vehicles.
Definition vehicle_type.h:84
VehicleRandomTrigger::Callback32
@ Callback32
All vehicles in consist: 32 day callback requested rerandomisation.
VehicleRandomTrigger::NewCargo
@ NewCargo
Affected vehicle only: Vehicle is loaded with cargo, after it was empty.
VehicleRandomTrigger::Depot
@ Depot
Front vehicle only: Consist arrived in depot.
VehicleRandomTrigger::AnyNewCargo
@ AnyNewCargo
All vehicles in consist: Any vehicle in the consist received new cargo.
VehicleRandomTrigger::Empty
@ Empty
Front vehicle only: Entire consist is empty.
VEHICLE_LENGTH
static const uint VEHICLE_LENGTH
The length of a vehicle in tile units.
Definition vehicle_type.h:65