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 <http://www.gnu.org/licenses/>.
8 /** @file newgrf_engine.cpp NewGRF handling of engines. */
14 #include "company_func.h"
15 #include "newgrf_cargo.h"
16 #include "newgrf_spritegroup.h"
17 #include "date_func.h"
18 #include "vehicle_func.h"
19 #include "core/random_func.hpp"
21 #include "station_base.h"
22 #include "company_base.h"
23 #include "newgrf_railtype.h"
24 #include "newgrf_roadtype.h"
27 #include "safeguards.h"
29 struct WagonOverride
{
33 const SpriteGroup
*group
;
36 void SetWagonOverrideSprites(EngineID engine
, CargoID cargo
, const SpriteGroup
*group
, EngineID
*train_id
, uint trains
)
38 Engine
*e
= Engine::Get(engine
);
41 assert(cargo
< NUM_CARGO
+ 2); // Include CT_DEFAULT and CT_PURCHASE pseudo cargoes.
44 e
->overrides
= ReallocT(e
->overrides
, e
->overrides_count
);
46 wo
= &e
->overrides
[e
->overrides_count
- 1];
50 wo
->train_id
= MallocT
<EngineID
>(trains
);
51 memcpy(wo
->train_id
, train_id
, trains
* sizeof *train_id
);
54 const SpriteGroup
*GetWagonOverrideSpriteSet(EngineID engine
, CargoID cargo
, EngineID overriding_engine
)
56 const Engine
*e
= Engine::Get(engine
);
58 for (uint i
= 0; i
< e
->overrides_count
; i
++) {
59 const WagonOverride
*wo
= &e
->overrides
[i
];
61 if (wo
->cargo
!= cargo
&& wo
->cargo
!= CT_DEFAULT
) continue;
63 for (uint j
= 0; j
< wo
->trains
; j
++) {
64 if (wo
->train_id
[j
] == overriding_engine
) return wo
->group
;
71 * Unload all wagon override sprite groups.
73 void UnloadWagonOverrides(Engine
*e
)
75 for (uint i
= 0; i
< e
->overrides_count
; i
++) {
76 WagonOverride
*wo
= &e
->overrides
[i
];
80 e
->overrides_count
= 0;
81 e
->overrides
= nullptr;
85 void SetCustomEngineSprites(EngineID engine
, byte cargo
, const SpriteGroup
*group
)
87 Engine
*e
= Engine::Get(engine
);
88 assert(cargo
< lengthof(e
->grf_prop
.spritegroup
));
90 if (e
->grf_prop
.spritegroup
[cargo
] != nullptr) {
91 grfmsg(6, "SetCustomEngineSprites: engine %d cargo %d already has group -- replacing", engine
, cargo
);
93 e
->grf_prop
.spritegroup
[cargo
] = group
;
98 * Tie a GRFFile entry to an engine, to allow us to retrieve GRF parameters
100 * @param engine Engine ID to tie the GRFFile to.
101 * @param file Pointer of GRFFile to tie.
103 void SetEngineGRF(EngineID engine
, const GRFFile
*file
)
105 Engine
*e
= Engine::Get(engine
);
106 e
->grf_prop
.grffile
= file
;
110 static int MapOldSubType(const Vehicle
*v
)
114 if (Train::From(v
)->IsEngine()) return 0;
115 if (Train::From(v
)->IsFreeWagon()) return 4;
118 case VEH_SHIP
: return 0;
120 case VEH_DISASTER
: return v
->subtype
;
121 case VEH_EFFECT
: return v
->subtype
<< 1;
122 default: NOT_REACHED();
127 /* TTDP style aircraft movement states for GRF Action 2 Var 0xE2 */
128 enum TTDPAircraftMovementStates
{
134 AMS_TTDP_TO_ENTRY_2_AND_3
,
135 AMS_TTDP_TO_ENTRY_2_AND_3_AND_H
,
136 AMS_TTDP_TO_JUNCTION
,
137 AMS_TTDP_LEAVE_RUNWAY
,
145 AMS_TTDP_FLIGHT_APPROACH
,
146 AMS_TTDP_UNUSED_0x11
,
147 AMS_TTDP_FLIGHT_TO_TOWER
,
148 AMS_TTDP_UNUSED_0x13
,
149 AMS_TTDP_FLIGHT_FINAL
,
150 AMS_TTDP_FLIGHT_DESCENT
,
152 AMS_TTDP_HELI_TAKEOFF_AIRPORT
,
153 AMS_TTDP_HELI_TO_TAKEOFF_AIRPORT
,
154 AMS_TTDP_HELI_LAND_AIRPORT
,
155 AMS_TTDP_HELI_TAKEOFF_HELIPORT
,
156 AMS_TTDP_HELI_TO_TAKEOFF_HELIPORT
,
157 AMS_TTDP_HELI_LAND_HELIPORT
,
162 * Map OTTD aircraft movement states to TTDPatch style movement states
163 * (VarAction 2 Variable 0xE2)
165 static byte
MapAircraftMovementState(const Aircraft
*v
)
167 const Station
*st
= GetTargetAirportIfValid(v
);
168 if (st
== nullptr) return AMS_TTDP_FLIGHT_TO_TOWER
;
170 const AirportFTAClass
*afc
= st
->airport
.GetFTA();
171 uint16 amdflag
= afc
->MovingData(v
->pos
)->flag
;
175 /* The international airport is a special case as helicopters can land in
176 * front of the hangar. Helicopters also change their air.state to
177 * AMED_HELI_LOWER some time before actually descending. */
179 /* This condition only occurs for helicopters, during descent,
180 * to a landing by the hangar of an international airport. */
181 if (amdflag
& AMED_HELI_LOWER
) return AMS_TTDP_HELI_LAND_AIRPORT
;
183 /* This condition only occurs for helicopters, before starting descent,
184 * to a landing by the hangar of an international airport. */
185 if (amdflag
& AMED_SLOWTURN
) return AMS_TTDP_FLIGHT_TO_TOWER
;
187 /* The final two conditions apply to helicopters or aircraft.
188 * Has reached hangar? */
189 if (amdflag
& AMED_EXACTPOS
) return AMS_TTDP_HANGAR
;
191 /* Still moving towards hangar. */
192 return AMS_TTDP_TO_HANGAR
;
195 if (amdflag
& AMED_EXACTPOS
) return AMS_TTDP_TO_PAD1
;
196 return AMS_TTDP_TO_JUNCTION
;
199 if (amdflag
& AMED_EXACTPOS
) return AMS_TTDP_TO_PAD2
;
200 return AMS_TTDP_TO_ENTRY_2_AND_3_AND_H
;
208 /* TTDPatch only has 3 terminals, so treat these states the same */
209 if (amdflag
& AMED_EXACTPOS
) return AMS_TTDP_TO_PAD3
;
210 return AMS_TTDP_TO_ENTRY_2_AND_3_AND_H
;
215 /* Will only occur for helicopters.*/
216 if (amdflag
& AMED_HELI_LOWER
) return AMS_TTDP_HELI_LAND_AIRPORT
; // Descending.
217 if (amdflag
& AMED_SLOWTURN
) return AMS_TTDP_FLIGHT_TO_TOWER
; // Still hasn't started descent.
218 return AMS_TTDP_TO_JUNCTION
; // On the ground.
220 case TAKEOFF
: // Moving to takeoff position.
221 return AMS_TTDP_TO_OUTWAY
;
223 case STARTTAKEOFF
: // Accelerating down runway.
224 return AMS_TTDP_TAKEOFF
;
226 case ENDTAKEOFF
: // Ascent
227 return AMS_TTDP_CLIMBING
;
229 case HELITAKEOFF
: // Helicopter is moving to take off position.
230 if (afc
->delta_z
== 0) {
231 return amdflag
& AMED_HELI_RAISE
?
232 AMS_TTDP_HELI_TAKEOFF_AIRPORT
: AMS_TTDP_TO_JUNCTION
;
234 return AMS_TTDP_HELI_TAKEOFF_HELIPORT
;
238 return amdflag
& AMED_HOLD
? AMS_TTDP_FLIGHT_APPROACH
: AMS_TTDP_FLIGHT_TO_TOWER
;
240 case LANDING
: // Descent
241 return AMS_TTDP_FLIGHT_DESCENT
;
243 case ENDLANDING
: // On the runway braking
244 if (amdflag
& AMED_BRAKE
) return AMS_TTDP_BRAKING
;
245 /* Landed - moving off runway */
246 return AMS_TTDP_TO_INWAY
;
249 case HELIENDLANDING
: // Helicoptor is descending.
250 if (amdflag
& AMED_HELI_LOWER
) {
251 return afc
->delta_z
== 0 ?
252 AMS_TTDP_HELI_LAND_AIRPORT
: AMS_TTDP_HELI_LAND_HELIPORT
;
254 return AMS_TTDP_FLIGHT_TO_TOWER
;
258 return AMS_TTDP_HANGAR
;
263 /* TTDP style aircraft movement action for GRF Action 2 Var 0xE6 */
264 enum TTDPAircraftMovementActions
{
269 AMA_TTDP_HANGAR_TO_PAD1
,
270 AMA_TTDP_HANGAR_TO_PAD2
,
271 AMA_TTDP_HANGAR_TO_PAD3
,
272 AMA_TTDP_LANDING_TO_PAD1
,
273 AMA_TTDP_LANDING_TO_PAD2
,
274 AMA_TTDP_LANDING_TO_PAD3
,
275 AMA_TTDP_PAD1_TO_HANGAR
,
276 AMA_TTDP_PAD2_TO_HANGAR
,
277 AMA_TTDP_PAD3_TO_HANGAR
,
278 AMA_TTDP_PAD1_TO_TAKEOFF
,
279 AMA_TTDP_PAD2_TO_TAKEOFF
,
280 AMA_TTDP_PAD3_TO_TAKEOFF
,
281 AMA_TTDP_HANGAR_TO_TAKOFF
,
282 AMA_TTDP_LANDING_TO_HANGAR
,
288 * Map OTTD aircraft movement states to TTDPatch style movement actions
289 * (VarAction 2 Variable 0xE6)
290 * This is not fully supported yet but it's enough for Planeset.
292 static byte
MapAircraftMovementAction(const Aircraft
*v
)
296 return (v
->cur_speed
> 0) ? AMA_TTDP_LANDING_TO_HANGAR
: AMA_TTDP_IN_HANGAR
;
300 return (v
->current_order
.IsType(OT_LOADING
)) ? AMA_TTDP_ON_PAD1
: AMA_TTDP_LANDING_TO_PAD1
;
304 return (v
->current_order
.IsType(OT_LOADING
)) ? AMA_TTDP_ON_PAD2
: AMA_TTDP_LANDING_TO_PAD2
;
313 return (v
->current_order
.IsType(OT_LOADING
)) ? AMA_TTDP_ON_PAD3
: AMA_TTDP_LANDING_TO_PAD3
;
315 case TAKEOFF
: // Moving to takeoff position
316 case STARTTAKEOFF
: // Accelerating down runway
317 case ENDTAKEOFF
: // Ascent
319 /* @todo Need to find which terminal (or hangar) we've come from. How? */
320 return AMA_TTDP_PAD1_TO_TAKEOFF
;
323 return AMA_TTDP_IN_FLIGHT
;
325 case LANDING
: // Descent
326 case ENDLANDING
: // On the runway braking
329 /* @todo Need to check terminal we're landing to. Is it known yet? */
330 return (v
->current_order
.IsType(OT_GOTO_DEPOT
)) ?
331 AMA_TTDP_LANDING_TO_HANGAR
: AMA_TTDP_LANDING_TO_PAD1
;
334 return AMA_TTDP_IN_HANGAR
;
339 /* virtual */ uint32
VehicleScopeResolver::GetRandomBits() const
341 return this->v
== nullptr ? 0 : this->v
->random_bits
;
344 /* virtual */ uint32
VehicleScopeResolver::GetTriggers() const
346 return this->v
== nullptr ? 0 : this->v
->waiting_triggers
;
350 /* virtual */ ScopeResolver
*VehicleResolverObject::GetScope(VarSpriteGroupScope scope
, byte relative
)
353 case VSG_SCOPE_SELF
: return &this->self_scope
;
354 case VSG_SCOPE_PARENT
: return &this->parent_scope
;
355 case VSG_SCOPE_RELATIVE
: {
356 int32 count
= GB(relative
, 0, 4);
357 if (this->self_scope
.v
!= nullptr && (relative
!= this->cached_relative_count
|| count
== 0)) {
358 /* Note: This caching only works as long as the VSG_SCOPE_RELATIVE cannot be used in
359 * VarAct2 with procedure calls. */
360 if (count
== 0) count
= GetRegister(0x100);
362 const Vehicle
*v
= nullptr;
363 switch (GB(relative
, 6, 2)) {
364 default: NOT_REACHED();
365 case 0x00: // count back (away from the engine), starting at this vehicle
366 v
= this->self_scope
.v
;
368 case 0x01: // count forward (toward the engine), starting at this vehicle
369 v
= this->self_scope
.v
;
372 case 0x02: // count back, starting at the engine
373 v
= this->parent_scope
.v
;
375 case 0x03: { // count back, starting at the first vehicle in this chain of vehicles with the same ID, as for vehicle variable 41
376 const Vehicle
*self
= this->self_scope
.v
;
377 for (const Vehicle
*u
= self
->First(); u
!= self
; u
= u
->Next()) {
378 if (u
->engine_type
!= self
->engine_type
) {
381 if (v
== nullptr) v
= u
;
384 if (v
== nullptr) v
= self
;
388 this->relative_scope
.SetVehicle(v
->Move(count
));
390 return &this->relative_scope
;
392 default: return ResolverObject::GetScope(scope
, relative
);
397 * Determines the livery of an engine.
399 * This always uses dual company colours independent of GUI settings. So it is desync-safe.
401 * @param engine Engine type
402 * @param v Vehicle, nullptr in purchase list.
403 * @return Livery to use
405 static const Livery
*LiveryHelper(EngineID engine
, const Vehicle
*v
)
410 if (!Company::IsValidID(_current_company
)) return nullptr;
411 l
= GetEngineLivery(engine
, _current_company
, INVALID_ENGINE
, nullptr, LIT_ALL
);
412 } else if (v
->IsGroundVehicle()) {
413 l
= GetEngineLivery(v
->engine_type
, v
->owner
, v
->GetGroundVehicleCache()->first_engine
, v
, LIT_ALL
);
415 l
= GetEngineLivery(v
->engine_type
, v
->owner
, INVALID_ENGINE
, v
, LIT_ALL
);
422 * Helper to get the position of a vehicle within a chain of vehicles.
423 * @param v the vehicle to get the position of.
424 * @param consecutive whether to look at the whole chain or the vehicles
425 * with the same 'engine type'.
426 * @return the position in the chain from front and tail and chain length.
428 static uint32
PositionHelper(const Vehicle
*v
, bool consecutive
)
431 byte chain_before
= 0;
432 byte chain_after
= 0;
434 for (u
= v
->First(); u
!= v
; u
= u
->Next()) {
436 if (consecutive
&& u
->engine_type
!= v
->engine_type
) chain_before
= 0;
439 while (u
->Next() != nullptr && (!consecutive
|| u
->Next()->engine_type
== v
->engine_type
)) {
444 return chain_before
| chain_after
<< 8 | (chain_before
+ chain_after
+ consecutive
) << 16;
447 static uint32
VehicleGetVariable(Vehicle
*v
, const VehicleScopeResolver
*object
, byte variable
, uint32 parameter
, bool *available
)
449 /* Calculated vehicle parameters */
451 case 0x25: // Get engine GRF ID
452 return v
->GetGRFID();
454 case 0x40: // Get length of consist
455 if (!HasBit(v
->grf_cache
.cache_valid
, NCVV_POSITION_CONSIST_LENGTH
)) {
456 v
->grf_cache
.position_consist_length
= PositionHelper(v
, false);
457 SetBit(v
->grf_cache
.cache_valid
, NCVV_POSITION_CONSIST_LENGTH
);
459 return v
->grf_cache
.position_consist_length
;
461 case 0x41: // Get length of same consecutive wagons
462 if (!HasBit(v
->grf_cache
.cache_valid
, NCVV_POSITION_SAME_ID_LENGTH
)) {
463 v
->grf_cache
.position_same_id_length
= PositionHelper(v
, true);
464 SetBit(v
->grf_cache
.cache_valid
, NCVV_POSITION_SAME_ID_LENGTH
);
466 return v
->grf_cache
.position_same_id_length
;
468 case 0x42: { // Consist cargo information
469 if (!HasBit(v
->grf_cache
.cache_valid
, NCVV_CONSIST_CARGO_INFORMATION
)) {
471 byte cargo_classes
= 0;
472 uint8 common_cargoes
[NUM_CARGO
];
473 uint8 common_subtypes
[256];
474 byte user_def_data
= 0;
475 CargoID common_cargo_type
= CT_INVALID
;
476 uint8 common_subtype
= 0xFF; // Return 0xFF if nothing is carried
478 /* Reset our arrays */
479 memset(common_cargoes
, 0, sizeof(common_cargoes
));
480 memset(common_subtypes
, 0, sizeof(common_subtypes
));
482 for (u
= v
; u
!= nullptr; u
= u
->Next()) {
483 if (v
->type
== VEH_TRAIN
) user_def_data
|= Train::From(u
)->tcache
.user_def_data
;
485 /* Skip empty engines */
486 if (!u
->GetEngine()->CanCarryCargo()) continue;
488 cargo_classes
|= CargoSpec::Get(u
->cargo_type
)->classes
;
489 common_cargoes
[u
->cargo_type
]++;
492 /* Pick the most common cargo type */
493 uint common_cargo_best_amount
= 0;
494 for (CargoID cargo
= 0; cargo
< NUM_CARGO
; cargo
++) {
495 if (common_cargoes
[cargo
] > common_cargo_best_amount
) {
496 common_cargo_best_amount
= common_cargoes
[cargo
];
497 common_cargo_type
= cargo
;
501 /* Count subcargo types of common_cargo_type */
502 for (u
= v
; u
!= nullptr; u
= u
->Next()) {
503 /* Skip empty engines and engines not carrying common_cargo_type */
504 if (u
->cargo_type
!= common_cargo_type
|| !u
->GetEngine()->CanCarryCargo()) continue;
506 common_subtypes
[u
->cargo_subtype
]++;
509 /* Pick the most common subcargo type*/
510 uint common_subtype_best_amount
= 0;
511 for (uint i
= 0; i
< lengthof(common_subtypes
); i
++) {
512 if (common_subtypes
[i
] > common_subtype_best_amount
) {
513 common_subtype_best_amount
= common_subtypes
[i
];
518 /* Note: We have to store the untranslated cargotype in the cache as the cache can be read by different NewGRFs,
519 * which will need different translations */
520 v
->grf_cache
.consist_cargo_information
= cargo_classes
| (common_cargo_type
<< 8) | (common_subtype
<< 16) | (user_def_data
<< 24);
521 SetBit(v
->grf_cache
.cache_valid
, NCVV_CONSIST_CARGO_INFORMATION
);
524 /* The cargo translation is specific to the accessing GRF, and thus cannot be cached. */
525 CargoID common_cargo_type
= (v
->grf_cache
.consist_cargo_information
>> 8) & 0xFF;
528 * - Unlike everywhere else the cargo translation table is only used since grf version 8, not 7.
529 * - For translating the cargo type we need to use the GRF which is resolving the variable, which
530 * is object->ro.grffile.
531 * In case of CBID_TRAIN_ALLOW_WAGON_ATTACH this is not the same as v->GetGRF().
532 * - The grffile == nullptr case only happens if this function is called for default vehicles.
533 * And this is only done by CheckCaches().
535 const GRFFile
*grffile
= object
->ro
.grffile
;
536 uint8 common_bitnum
= (common_cargo_type
== CT_INVALID
) ? 0xFF :
537 (grffile
== nullptr || grffile
->grf_version
< 8) ? CargoSpec::Get(common_cargo_type
)->bitnum
: grffile
->cargo_map
[common_cargo_type
];
539 return (v
->grf_cache
.consist_cargo_information
& 0xFFFF00FF) | common_bitnum
<< 8;
542 case 0x43: // Company information
543 if (!HasBit(v
->grf_cache
.cache_valid
, NCVV_COMPANY_INFORMATION
)) {
544 v
->grf_cache
.company_information
= GetCompanyInfo(v
->owner
, LiveryHelper(v
->engine_type
, v
));
545 SetBit(v
->grf_cache
.cache_valid
, NCVV_COMPANY_INFORMATION
);
547 return v
->grf_cache
.company_information
;
549 case 0x44: // Aircraft information
550 if (v
->type
!= VEH_AIRCRAFT
|| !Aircraft::From(v
)->IsNormalAircraft()) return UINT_MAX
;
553 const Vehicle
*w
= v
->Next();
554 uint16 altitude
= ClampToU16(v
->z_pos
- w
->z_pos
); // Aircraft height - shadow height
555 byte airporttype
= ATP_TTDP_LARGE
;
557 const Station
*st
= GetTargetAirportIfValid(Aircraft::From(v
));
559 if (st
!= nullptr && st
->airport
.tile
!= INVALID_TILE
) {
560 airporttype
= st
->airport
.GetSpec()->ttd_airport_type
;
563 return (Clamp(altitude
, 0, 0xFF) << 8) | airporttype
;
566 case 0x45: { // Curvature info
568 * F - previous wagon to current wagon, 0 if vehicle is first
569 * B - current wagon to next wagon, 0 if wagon is last
570 * T - previous wagon to next wagon, 0 in an S-bend
572 if (!v
->IsGroundVehicle()) return 0;
574 const Vehicle
*u_p
= v
->Previous();
575 const Vehicle
*u_n
= v
->Next();
576 DirDiff f
= (u_p
== nullptr) ? DIRDIFF_SAME
: DirDifference(u_p
->direction
, v
->direction
);
577 DirDiff b
= (u_n
== nullptr) ? DIRDIFF_SAME
: DirDifference(v
->direction
, u_n
->direction
);
578 DirDiff t
= ChangeDirDiff(f
, b
);
580 return ((t
> DIRDIFF_REVERSE
? t
| 8 : t
) << 16) |
581 ((b
> DIRDIFF_REVERSE
? b
| 8 : b
) << 8) |
582 ( f
> DIRDIFF_REVERSE
? f
| 8 : f
);
585 case 0x46: // Motion counter
586 return v
->motion_counter
;
588 case 0x47: { // Vehicle cargo info
590 * tt - the cargo type transported by the vehicle,
591 * translated if a translation table has been installed.
592 * ww - cargo unit weight in 1/16 tons, same as cargo prop. 0F.
593 * cccc - the cargo class value of the cargo transported by the vehicle.
595 const CargoSpec
*cs
= CargoSpec::Get(v
->cargo_type
);
598 * For translating the cargo type we need to use the GRF which is resolving the variable, which
599 * is object->ro.grffile.
600 * In case of CBID_TRAIN_ALLOW_WAGON_ATTACH this is not the same as v->GetGRF().
602 return (cs
->classes
<< 16) | (cs
->weight
<< 8) | object
->ro
.grffile
->cargo_map
[v
->cargo_type
];
605 case 0x48: return v
->GetEngine()->flags
; // Vehicle Type Info
606 case 0x49: return v
->build_year
;
611 RailType rt
= GetTileRailType(v
->tile
);
612 const RailtypeInfo
*rti
= GetRailTypeInfo(rt
);
613 return ((rti
->flags
& RTFB_CATENARY
) ? 0x200 : 0) |
614 (HasPowerOnRail(Train::From(v
)->railtype
, rt
) ? 0x100 : 0) |
615 GetReverseRailTypeTranslation(rt
, object
->ro
.grffile
);
619 RoadType rt
= GetRoadType(v
->tile
, GetRoadTramType(RoadVehicle::From(v
)->roadtype
));
620 const RoadTypeInfo
*rti
= GetRoadTypeInfo(rt
);
621 return ((rti
->flags
& ROTFB_CATENARY
) ? 0x200 : 0) |
623 GetReverseRoadTypeTranslation(rt
, object
->ro
.grffile
);
630 case 0x4B: // Long date of last service
631 return v
->date_of_last_service
;
633 case 0x4C: // Current maximum speed in NewGRF units
634 if (!v
->IsPrimaryVehicle()) return 0;
635 return v
->GetCurrentMaxSpeed();
637 case 0x4D: // Position within articulated vehicle
638 if (!HasBit(v
->grf_cache
.cache_valid
, NCVV_POSITION_IN_VEHICLE
)) {
639 byte artic_before
= 0;
640 for (const Vehicle
*u
= v
; u
->IsArticulatedPart(); u
= u
->Previous()) artic_before
++;
641 byte artic_after
= 0;
642 for (const Vehicle
*u
= v
; u
->HasArticulatedPart(); u
= u
->Next()) artic_after
++;
643 v
->grf_cache
.position_in_vehicle
= artic_before
| artic_after
<< 8;
644 SetBit(v
->grf_cache
.cache_valid
, NCVV_POSITION_IN_VEHICLE
);
646 return v
->grf_cache
.position_in_vehicle
;
648 /* Variables which use the parameter */
649 case 0x60: // Count consist's engine ID occurrence
650 if (v
->type
!= VEH_TRAIN
) return v
->GetEngine()->grf_prop
.local_id
== parameter
? 1 : 0;
654 for (; v
!= nullptr; v
= v
->Next()) {
655 if (v
->GetEngine()->grf_prop
.local_id
== parameter
) count
++;
660 case 0x61: // Get variable of n-th vehicle in chain [signed number relative to vehicle]
661 if (!v
->IsGroundVehicle() || parameter
== 0x61) {
666 /* Only allow callbacks that don't change properties to avoid circular dependencies. */
667 if (object
->ro
.callback
== CBID_NO_CALLBACK
|| object
->ro
.callback
== CBID_RANDOM_TRIGGER
|| object
->ro
.callback
== CBID_TRAIN_ALLOW_WAGON_ATTACH
||
668 object
->ro
.callback
== CBID_VEHICLE_START_STOP_CHECK
|| object
->ro
.callback
== CBID_VEHICLE_32DAY_CALLBACK
|| object
->ro
.callback
== CBID_VEHICLE_COLOUR_MAPPING
||
669 object
->ro
.callback
== CBID_VEHICLE_SPAWN_VISUAL_EFFECT
) {
670 Vehicle
*u
= v
->Move((int32
)GetRegister(0x10F));
671 if (u
== nullptr) return 0; // available, but zero
673 if (parameter
== 0x5F) {
674 /* This seems to be the only variable that makes sense to access via var 61, but is not handled by VehicleGetVariable */
675 return (u
->random_bits
<< 8) | u
->waiting_triggers
;
677 return VehicleGetVariable(u
, object
, parameter
, GetRegister(0x10E), available
);
683 case 0x62: { // Curvature/position difference for n-th vehicle in chain [signed number relative to vehicle]
685 * zz - Signed difference of z position between the selected and this vehicle.
686 * yy - Signed difference of y position between the selected and this vehicle.
687 * xx - Signed difference of x position between the selected and this vehicle.
688 * F - Flags, bit 7 corresponds to VS_HIDDEN.
689 * D - Dir difference, like in 0x45.
691 if (!v
->IsGroundVehicle()) return 0;
693 const Vehicle
*u
= v
->Move((int8
)parameter
);
694 if (u
== nullptr) return 0;
696 /* Get direction difference. */
697 bool prev
= (int8
)parameter
< 0;
698 uint32 ret
= prev
? DirDifference(u
->direction
, v
->direction
) : DirDifference(v
->direction
, u
->direction
);
699 if (ret
> DIRDIFF_REVERSE
) ret
|= 0x08;
701 if (u
->vehstatus
& VS_HIDDEN
) ret
|= 0x80;
703 /* Get position difference. */
704 ret
|= ((prev
? u
->x_pos
- v
->x_pos
: v
->x_pos
- u
->x_pos
) & 0xFF) << 8;
705 ret
|= ((prev
? u
->y_pos
- v
->y_pos
: v
->y_pos
- u
->y_pos
) & 0xFF) << 16;
706 ret
|= ((prev
? u
->z_pos
- v
->z_pos
: v
->z_pos
- u
->z_pos
) & 0xFF) << 24;
712 /* Tile compatibility wrt. arbitrary track-type
714 * bit 0: Type 'parameter' is known.
715 * bit 1: Engines with type 'parameter' are compatible with this tile.
716 * bit 2: Engines with type 'parameter' are powered on this tile.
717 * bit 3: This tile has type 'parameter' or it is considered equivalent (alternate labels).
721 RailType param_type
= GetRailTypeTranslation(parameter
, object
->ro
.grffile
);
722 if (param_type
== INVALID_RAILTYPE
) return 0x00;
723 RailType tile_type
= GetTileRailType(v
->tile
);
724 if (tile_type
== param_type
) return 0x0F;
725 return (HasPowerOnRail(param_type
, tile_type
) ? 0x04 : 0x00) |
726 (IsCompatibleRail(param_type
, tile_type
) ? 0x02 : 0x00) |
730 RoadTramType rtt
= GetRoadTramType(RoadVehicle::From(v
)->roadtype
);
731 RoadType param_type
= GetRoadTypeTranslation(rtt
, parameter
, object
->ro
.grffile
);
732 if (param_type
== INVALID_ROADTYPE
) return 0x00;
733 RoadType tile_type
= GetRoadType(v
->tile
, rtt
);
734 if (tile_type
== param_type
) return 0x0F;
735 return (HasPowerOnRoad(param_type
, tile_type
) ? 0x06 : 0x00) |
738 default: return 0x00;
745 if (v
->type
== VEH_TRAIN
) {
746 const Train
*t
= Train::From(v
);
747 bool is_powered_wagon
= HasBit(t
->flags
, VRF_POWEREDWAGON
);
748 const Train
*u
= is_powered_wagon
? t
->First() : t
; // for powered wagons the engine defines the type of engine (i.e. railtype)
749 RailType railtype
= GetRailType(v
->tile
);
750 bool powered
= t
->IsEngine() || is_powered_wagon
;
751 bool has_power
= HasPowerOnRail(u
->railtype
, railtype
);
753 if (powered
&& has_power
) SetBit(modflags
, 5);
754 if (powered
&& !has_power
) SetBit(modflags
, 6);
755 if (HasBit(t
->flags
, VRF_TOGGLE_REVERSE
)) SetBit(modflags
, 8);
757 if (HasBit(v
->vehicle_flags
, VF_CARGO_UNLOADING
)) SetBit(modflags
, 1);
758 if (HasBit(v
->vehicle_flags
, VF_BUILT_AS_PROTOTYPE
)) SetBit(modflags
, 10);
760 return variable
== 0xFE ? modflags
: GB(modflags
, 8, 8);
765 * General vehicle properties
767 * Some parts of the TTD Vehicle structure are omitted for various reasons
768 * (see http://marcin.ttdpatch.net/sv1codec/TTD-locations.html#_VehicleArray)
770 switch (variable
- 0x80) {
771 case 0x00: return v
->type
+ 0x10;
772 case 0x01: return MapOldSubType(v
);
773 case 0x02: break; // not implemented
774 case 0x03: break; // not implemented
775 case 0x04: return v
->index
;
776 case 0x05: return GB(v
->index
, 8, 8);
777 case 0x06: break; // not implemented
778 case 0x07: break; // not implemented
779 case 0x08: break; // not implemented
780 case 0x09: break; // not implemented
781 case 0x0A: return v
->current_order
.MapOldOrder();
782 case 0x0B: return v
->current_order
.GetDestination();
783 case 0x0C: return v
->GetNumOrders();
784 case 0x0D: return v
->cur_real_order_index
;
785 case 0x0E: break; // not implemented
786 case 0x0F: break; // not implemented
790 if (v
->current_order
.IsType(OT_LOADING
)) {
791 ticks
= v
->load_unload_ticks
;
794 case VEH_TRAIN
: ticks
= Train::From(v
)->wait_counter
; break;
795 case VEH_AIRCRAFT
: ticks
= Aircraft::From(v
)->turn_counter
; break;
796 default: ticks
= 0; break;
799 return (variable
- 0x80) == 0x10 ? ticks
: GB(ticks
, 8, 8);
801 case 0x12: return Clamp(v
->date_of_last_service
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 0xFFFF);
802 case 0x13: return GB(Clamp(v
->date_of_last_service
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 0xFFFF), 8, 8);
803 case 0x14: return v
->GetServiceInterval();
804 case 0x15: return GB(v
->GetServiceInterval(), 8, 8);
805 case 0x16: return v
->last_station_visited
;
806 case 0x17: return v
->tick_counter
;
812 max_speed
= Aircraft::From(v
)->GetSpeedOldUnits(); // Convert to old units.
816 max_speed
= v
->vcache
.cached_max_speed
;
819 return (variable
- 0x80) == 0x18 ? max_speed
: GB(max_speed
, 8, 8);
821 case 0x1A: return v
->x_pos
;
822 case 0x1B: return GB(v
->x_pos
, 8, 8);
823 case 0x1C: return v
->y_pos
;
824 case 0x1D: return GB(v
->y_pos
, 8, 8);
825 case 0x1E: return v
->z_pos
;
826 case 0x1F: return object
->info_view
? DIR_W
: v
->direction
;
827 case 0x20: break; // not implemented
828 case 0x21: break; // not implemented
829 case 0x22: break; // not implemented
830 case 0x23: break; // not implemented
831 case 0x24: break; // not implemented
832 case 0x25: break; // not implemented
833 case 0x26: break; // not implemented
834 case 0x27: break; // not implemented
835 case 0x28: return 0; // cur_image is a potential desyncer due to Action1 in static NewGRFs.
836 case 0x29: return 0; // cur_image is a potential desyncer due to Action1 in static NewGRFs.
837 case 0x2A: break; // not implemented
838 case 0x2B: break; // not implemented
839 case 0x2C: break; // not implemented
840 case 0x2D: break; // not implemented
841 case 0x2E: break; // not implemented
842 case 0x2F: break; // not implemented
843 case 0x30: break; // not implemented
844 case 0x31: break; // not implemented
845 case 0x32: return v
->vehstatus
;
846 case 0x33: return 0; // non-existent high byte of vehstatus
847 case 0x34: return v
->type
== VEH_AIRCRAFT
? (v
->cur_speed
* 10) / 128 : v
->cur_speed
;
848 case 0x35: return GB(v
->type
== VEH_AIRCRAFT
? (v
->cur_speed
* 10) / 128 : v
->cur_speed
, 8, 8);
849 case 0x36: return v
->subspeed
;
850 case 0x37: return v
->acceleration
;
851 case 0x38: break; // not implemented
852 case 0x39: return v
->cargo_type
;
853 case 0x3A: return v
->cargo_cap
;
854 case 0x3B: return GB(v
->cargo_cap
, 8, 8);
855 case 0x3C: return ClampToU16(v
->cargo
.StoredCount());
856 case 0x3D: return GB(ClampToU16(v
->cargo
.StoredCount()), 8, 8);
857 case 0x3E: return v
->cargo
.Source();
858 case 0x3F: return ClampU(v
->cargo
.DaysInTransit(), 0, 0xFF);
859 case 0x40: return ClampToU16(v
->age
);
860 case 0x41: return GB(ClampToU16(v
->age
), 8, 8);
861 case 0x42: return ClampToU16(v
->max_age
);
862 case 0x43: return GB(ClampToU16(v
->max_age
), 8, 8);
863 case 0x44: return Clamp(v
->build_year
, ORIGINAL_BASE_YEAR
, ORIGINAL_MAX_YEAR
) - ORIGINAL_BASE_YEAR
;
864 case 0x45: return v
->unitnumber
;
865 case 0x46: return v
->GetEngine()->grf_prop
.local_id
;
866 case 0x47: return GB(v
->GetEngine()->grf_prop
.local_id
, 8, 8);
868 if (v
->type
!= VEH_TRAIN
|| v
->spritenum
!= 0xFD) return v
->spritenum
;
869 return HasBit(Train::From(v
)->flags
, VRF_REVERSE_DIRECTION
) ? 0xFE : 0xFD;
871 case 0x49: return v
->day_counter
;
872 case 0x4A: return v
->breakdowns_since_last_service
;
873 case 0x4B: return v
->breakdown_ctr
;
874 case 0x4C: return v
->breakdown_delay
;
875 case 0x4D: return v
->breakdown_chance
;
876 case 0x4E: return v
->reliability
;
877 case 0x4F: return GB(v
->reliability
, 8, 8);
878 case 0x50: return v
->reliability_spd_dec
;
879 case 0x51: return GB(v
->reliability_spd_dec
, 8, 8);
880 case 0x52: return ClampToI32(v
->GetDisplayProfitThisYear());
881 case 0x53: return GB(ClampToI32(v
->GetDisplayProfitThisYear()), 8, 24);
882 case 0x54: return GB(ClampToI32(v
->GetDisplayProfitThisYear()), 16, 16);
883 case 0x55: return GB(ClampToI32(v
->GetDisplayProfitThisYear()), 24, 8);
884 case 0x56: return ClampToI32(v
->GetDisplayProfitLastYear());
885 case 0x57: return GB(ClampToI32(v
->GetDisplayProfitLastYear()), 8, 24);
886 case 0x58: return GB(ClampToI32(v
->GetDisplayProfitLastYear()), 16, 16);
887 case 0x59: return GB(ClampToI32(v
->GetDisplayProfitLastYear()), 24, 8);
888 case 0x5A: return v
->Next() == nullptr ? INVALID_VEHICLE
: v
->Next()->index
;
889 case 0x5B: break; // not implemented
890 case 0x5C: return ClampToI32(v
->value
);
891 case 0x5D: return GB(ClampToI32(v
->value
), 8, 24);
892 case 0x5E: return GB(ClampToI32(v
->value
), 16, 16);
893 case 0x5F: return GB(ClampToI32(v
->value
), 24, 8);
894 case 0x60: break; // not implemented
895 case 0x61: break; // not implemented
896 case 0x62: break; // vehicle specific, see below
897 case 0x63: break; // not implemented
898 case 0x64: break; // vehicle specific, see below
899 case 0x65: break; // vehicle specific, see below
900 case 0x66: break; // vehicle specific, see below
901 case 0x67: break; // vehicle specific, see below
902 case 0x68: break; // vehicle specific, see below
903 case 0x69: break; // vehicle specific, see below
904 case 0x6A: break; // not implemented
905 case 0x6B: break; // not implemented
906 case 0x6C: break; // not implemented
907 case 0x6D: break; // not implemented
908 case 0x6E: break; // not implemented
909 case 0x6F: break; // not implemented
910 case 0x70: break; // not implemented
911 case 0x71: break; // not implemented
912 case 0x72: return v
->cargo_subtype
;
913 case 0x73: break; // vehicle specific, see below
914 case 0x74: break; // vehicle specific, see below
915 case 0x75: break; // vehicle specific, see below
916 case 0x76: break; // vehicle specific, see below
917 case 0x77: break; // vehicle specific, see below
918 case 0x78: break; // not implemented
919 case 0x79: break; // not implemented
920 case 0x7A: return v
->random_bits
;
921 case 0x7B: return v
->waiting_triggers
;
922 case 0x7C: break; // vehicle specific, see below
923 case 0x7D: break; // vehicle specific, see below
924 case 0x7E: break; // not implemented
925 case 0x7F: break; // vehicle specific, see below
928 /* Vehicle specific properties */
931 Train
*t
= Train::From(v
);
932 switch (variable
- 0x80) {
933 case 0x62: return t
->track
;
934 case 0x66: return t
->railtype
;
935 case 0x73: return 0x80 + VEHICLE_LENGTH
- t
->gcache
.cached_veh_length
;
936 case 0x74: return t
->gcache
.cached_power
;
937 case 0x75: return GB(t
->gcache
.cached_power
, 8, 24);
938 case 0x76: return GB(t
->gcache
.cached_power
, 16, 16);
939 case 0x77: return GB(t
->gcache
.cached_power
, 24, 8);
940 case 0x7C: return t
->First()->index
;
941 case 0x7D: return GB(t
->First()->index
, 8, 8);
942 case 0x7F: return 0; // Used for vehicle reversing hack in TTDP
948 RoadVehicle
*rv
= RoadVehicle::From(v
);
949 switch (variable
- 0x80) {
950 case 0x62: return rv
->state
;
951 case 0x64: return rv
->blocked_ctr
;
952 case 0x65: return GB(rv
->blocked_ctr
, 8, 8);
953 case 0x66: return rv
->overtaking
;
954 case 0x67: return rv
->overtaking_ctr
;
955 case 0x68: return rv
->crashed_ctr
;
956 case 0x69: return GB(rv
->crashed_ctr
, 8, 8);
962 Ship
*s
= Ship::From(v
);
963 switch (variable
- 0x80) {
964 case 0x62: return s
->state
;
970 Aircraft
*a
= Aircraft::From(v
);
971 switch (variable
- 0x80) {
972 case 0x62: return MapAircraftMovementState(a
); // Current movement state
973 case 0x63: return a
->targetairport
; // Airport to which the action refers
974 case 0x66: return MapAircraftMovementAction(a
); // Current movement action
982 DEBUG(grf
, 1, "Unhandled vehicle variable 0x%X, type 0x%X", variable
, (uint
)v
->type
);
988 /* virtual */ uint32
VehicleScopeResolver::GetVariable(byte variable
, uint32 parameter
, bool *available
) const
990 if (this->v
== nullptr) {
991 /* Vehicle does not exist, so we're in a purchase list */
993 case 0x43: return GetCompanyInfo(_current_company
, LiveryHelper(this->self_type
, nullptr)); // Owner information
994 case 0x46: return 0; // Motion counter
995 case 0x47: { // Vehicle cargo info
996 const Engine
*e
= Engine::Get(this->self_type
);
997 CargoID cargo_type
= e
->GetDefaultCargoType();
998 if (cargo_type
!= CT_INVALID
) {
999 const CargoSpec
*cs
= CargoSpec::Get(cargo_type
);
1000 return (cs
->classes
<< 16) | (cs
->weight
<< 8) | this->ro
.grffile
->cargo_map
[cargo_type
];
1005 case 0x48: return Engine::Get(this->self_type
)->flags
; // Vehicle Type Info
1006 case 0x49: return _cur_year
; // 'Long' format build year
1007 case 0x4B: return _date
; // Long date of last service
1008 case 0x92: return Clamp(_date
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 0xFFFF); // Date of last service
1009 case 0x93: return GB(Clamp(_date
- DAYS_TILL_ORIGINAL_BASE_YEAR
, 0, 0xFFFF), 8, 8);
1010 case 0xC4: return Clamp(_cur_year
, ORIGINAL_BASE_YEAR
, ORIGINAL_MAX_YEAR
) - ORIGINAL_BASE_YEAR
; // Build year
1011 case 0xDA: return INVALID_VEHICLE
; // Next vehicle
1012 case 0xF2: return 0; // Cargo subtype
1019 return VehicleGetVariable(const_cast<Vehicle
*>(this->v
), this, variable
, parameter
, available
);
1023 /* virtual */ const SpriteGroup
*VehicleResolverObject::ResolveReal(const RealSpriteGroup
*group
) const
1025 const Vehicle
*v
= this->self_scope
.v
;
1028 if (group
->num_loading
> 0) return group
->loading
[0];
1029 if (group
->num_loaded
> 0) return group
->loaded
[0];
1033 bool in_motion
= !v
->First()->current_order
.IsType(OT_LOADING
);
1035 uint totalsets
= in_motion
? group
->num_loaded
: group
->num_loading
;
1037 if (totalsets
== 0) return nullptr;
1039 uint set
= (v
->cargo
.StoredCount() * totalsets
) / std::max
<uint16
>(1u, v
->cargo_cap
);
1040 set
= std::min(set
, totalsets
- 1);
1042 return in_motion
? group
->loaded
[set
] : group
->loading
[set
];
1045 GrfSpecFeature
VehicleResolverObject::GetFeature() const
1047 switch (Engine::Get(this->self_scope
.self_type
)->type
) {
1048 case VEH_TRAIN
: return GSF_TRAINS
;
1049 case VEH_ROAD
: return GSF_ROADVEHICLES
;
1050 case VEH_SHIP
: return GSF_SHIPS
;
1051 case VEH_AIRCRAFT
: return GSF_AIRCRAFT
;
1052 default: return GSF_INVALID
;
1056 uint32
VehicleResolverObject::GetDebugID() const
1058 return Engine::Get(this->self_scope
.self_type
)->grf_prop
.local_id
;
1062 * Get the grf file associated with an engine type.
1063 * @param engine_type Engine to query.
1064 * @return grf file associated with the engine.
1066 static const GRFFile
*GetEngineGrfFile(EngineID engine_type
)
1068 const Engine
*e
= Engine::Get(engine_type
);
1069 return (e
!= nullptr) ? e
->GetGRF() : nullptr;
1073 * Resolver of a vehicle (chain).
1074 * @param engine_type Engine type
1075 * @param v %Vehicle being resolved.
1076 * @param wagon_override Application of wagon overrides.
1077 * @param info_view Indicates if the item is being drawn in an info window.
1078 * @param callback Callback ID.
1079 * @param callback_param1 First parameter (var 10) of the callback.
1080 * @param callback_param2 Second parameter (var 18) of the callback.
1082 VehicleResolverObject::VehicleResolverObject(EngineID engine_type
, const Vehicle
*v
, WagonOverride wagon_override
, bool info_view
,
1083 CallbackID callback
, uint32 callback_param1
, uint32 callback_param2
)
1084 : ResolverObject(GetEngineGrfFile(engine_type
), callback
, callback_param1
, callback_param2
),
1085 self_scope(*this, engine_type
, v
, info_view
),
1086 parent_scope(*this, engine_type
, ((v
!= nullptr) ? v
->First() : v
), info_view
),
1087 relative_scope(*this, engine_type
, v
, info_view
),
1088 cached_relative_count(0)
1090 if (wagon_override
== WO_SELF
) {
1091 this->root_spritegroup
= GetWagonOverrideSpriteSet(engine_type
, CT_DEFAULT
, engine_type
);
1093 if (wagon_override
!= WO_NONE
&& v
!= nullptr && v
->IsGroundVehicle()) {
1094 assert(v
->engine_type
== engine_type
); // overrides make little sense with fake scopes
1096 /* For trains we always use cached value, except for callbacks because the override spriteset
1097 * to use may be different than the one cached. It happens for callback 0x15 (refit engine),
1098 * as v->cargo_type is temporary changed to the new type */
1099 if (wagon_override
== WO_CACHED
&& v
->type
== VEH_TRAIN
) {
1100 this->root_spritegroup
= Train::From(v
)->tcache
.cached_override
;
1102 this->root_spritegroup
= GetWagonOverrideSpriteSet(v
->engine_type
, v
->cargo_type
, v
->GetGroundVehicleCache()->first_engine
);
1106 if (this->root_spritegroup
== nullptr) {
1107 const Engine
*e
= Engine::Get(engine_type
);
1108 CargoID cargo
= v
!= nullptr ? v
->cargo_type
: CT_PURCHASE
;
1109 assert(cargo
< lengthof(e
->grf_prop
.spritegroup
));
1110 this->root_spritegroup
= e
->grf_prop
.spritegroup
[cargo
] != nullptr ? e
->grf_prop
.spritegroup
[cargo
] : e
->grf_prop
.spritegroup
[CT_DEFAULT
];
1117 void GetCustomEngineSprite(EngineID engine
, const Vehicle
*v
, Direction direction
, EngineImageType image_type
, VehicleSpriteSeq
*result
)
1119 VehicleResolverObject
object(engine
, v
, VehicleResolverObject::WO_CACHED
, false, CBID_NO_CALLBACK
);
1122 bool sprite_stack
= HasBit(EngInfo(engine
)->misc_flags
, EF_SPRITE_STACK
);
1123 uint max_stack
= sprite_stack
? lengthof(result
->seq
) : 1;
1124 for (uint stack
= 0; stack
< max_stack
; ++stack
) {
1125 object
.ResetState();
1126 object
.callback_param1
= image_type
| (stack
<< 8);
1127 const SpriteGroup
*group
= object
.Resolve();
1128 uint32 reg100
= sprite_stack
? GetRegister(0x100) : 0;
1129 if (group
!= nullptr && group
->GetNumResults() != 0) {
1130 result
->seq
[result
->count
].sprite
= group
->GetResult() + (direction
% group
->GetNumResults());
1131 result
->seq
[result
->count
].pal
= GB(reg100
, 0, 16); // zero means default recolouring
1134 if (!HasBit(reg100
, 31)) break;
1139 void GetRotorOverrideSprite(EngineID engine
, const struct Aircraft
*v
, bool info_view
, EngineImageType image_type
, VehicleSpriteSeq
*result
)
1141 const Engine
*e
= Engine::Get(engine
);
1143 /* Only valid for helicopters */
1144 assert(e
->type
== VEH_AIRCRAFT
);
1145 assert(!(e
->u
.air
.subtype
& AIR_CTOL
));
1147 VehicleResolverObject
object(engine
, v
, VehicleResolverObject::WO_SELF
, info_view
, CBID_NO_CALLBACK
);
1149 uint rotor_pos
= v
== nullptr || info_view
? 0 : v
->Next()->Next()->state
;
1151 bool sprite_stack
= HasBit(e
->info
.misc_flags
, EF_SPRITE_STACK
);
1152 uint max_stack
= sprite_stack
? lengthof(result
->seq
) : 1;
1153 for (uint stack
= 0; stack
< max_stack
; ++stack
) {
1154 object
.ResetState();
1155 object
.callback_param1
= image_type
| (stack
<< 8);
1156 const SpriteGroup
*group
= object
.Resolve();
1157 uint32 reg100
= sprite_stack
? GetRegister(0x100) : 0;
1158 if (group
!= nullptr && group
->GetNumResults() != 0) {
1159 result
->seq
[result
->count
].sprite
= group
->GetResult() + (rotor_pos
% group
->GetNumResults());
1160 result
->seq
[result
->count
].pal
= GB(reg100
, 0, 16); // zero means default recolouring
1163 if (!HasBit(reg100
, 31)) break;
1169 * Check if a wagon is currently using a wagon override
1170 * @param v The wagon to check
1171 * @return true if it is using an override, false otherwise
1173 bool UsesWagonOverride(const Vehicle
*v
)
1175 assert(v
->type
== VEH_TRAIN
);
1176 return Train::From(v
)->tcache
.cached_override
!= nullptr;
1180 * Evaluate a newgrf callback for vehicles
1181 * @param callback The callback to evaluate
1182 * @param param1 First parameter of the callback
1183 * @param param2 Second parameter of the callback
1184 * @param engine Engine type of the vehicle to evaluate the callback for
1185 * @param v The vehicle to evaluate the callback for, or nullptr if it doesn't exist yet
1186 * @return The value the callback returned, or CALLBACK_FAILED if it failed
1188 uint16
GetVehicleCallback(CallbackID callback
, uint32 param1
, uint32 param2
, EngineID engine
, const Vehicle
*v
)
1190 VehicleResolverObject
object(engine
, v
, VehicleResolverObject::WO_UNCACHED
, false, callback
, param1
, param2
);
1191 return object
.ResolveCallback();
1195 * Evaluate a newgrf callback for vehicles with a different vehicle for parent scope.
1196 * @param callback The callback to evaluate
1197 * @param param1 First parameter of the callback
1198 * @param param2 Second parameter of the callback
1199 * @param engine Engine type of the vehicle to evaluate the callback for
1200 * @param v The vehicle to evaluate the callback for, or nullptr if it doesn't exist yet
1201 * @param parent The vehicle to use for parent scope
1202 * @return The value the callback returned, or CALLBACK_FAILED if it failed
1204 uint16
GetVehicleCallbackParent(CallbackID callback
, uint32 param1
, uint32 param2
, EngineID engine
, const Vehicle
*v
, const Vehicle
*parent
)
1206 VehicleResolverObject
object(engine
, v
, VehicleResolverObject::WO_NONE
, false, callback
, param1
, param2
);
1207 object
.parent_scope
.SetVehicle(parent
);
1208 return object
.ResolveCallback();
1212 /* Callback 36 handlers */
1213 uint
GetVehicleProperty(const Vehicle
*v
, PropertyID property
, uint orig_value
)
1215 return GetEngineProperty(v
->engine_type
, property
, orig_value
, v
);
1219 uint
GetEngineProperty(EngineID engine
, PropertyID property
, uint orig_value
, const Vehicle
*v
)
1221 uint16 callback
= GetVehicleCallback(CBID_VEHICLE_MODIFY_PROPERTY
, property
, 0, engine
, v
);
1222 if (callback
!= CALLBACK_FAILED
) return callback
;
1228 static void DoTriggerVehicle(Vehicle
*v
, VehicleTrigger trigger
, byte base_random_bits
, bool first
)
1230 /* We can't trigger a non-existent vehicle... */
1231 assert(v
!= nullptr);
1233 VehicleResolverObject
object(v
->engine_type
, v
, VehicleResolverObject::WO_CACHED
, false, CBID_RANDOM_TRIGGER
);
1234 object
.waiting_triggers
= v
->waiting_triggers
| trigger
;
1235 v
->waiting_triggers
= object
.waiting_triggers
; // store now for var 5F
1237 const SpriteGroup
*group
= object
.Resolve();
1238 if (group
== nullptr) return;
1240 /* Store remaining triggers. */
1241 v
->waiting_triggers
= object
.GetRemainingTriggers();
1243 /* Rerandomise bits. Scopes other than SELF are invalid for rerandomisation. For bug-to-bug-compatibility with TTDP we ignore the scope. */
1244 byte new_random_bits
= Random();
1245 uint32 reseed
= object
.GetReseedSum();
1246 v
->random_bits
&= ~reseed
;
1247 v
->random_bits
|= (first
? new_random_bits
: base_random_bits
) & reseed
;
1250 case VEHICLE_TRIGGER_NEW_CARGO
:
1251 /* All vehicles in chain get ANY_NEW_CARGO trigger now.
1252 * So we call it for the first one and they will recurse.
1253 * Indexing part of vehicle random bits needs to be
1254 * same for all triggered vehicles in the chain (to get
1255 * all the random-cargo wagons carry the same cargo,
1256 * i.e.), so we give them all the NEW_CARGO triggered
1257 * vehicle's portion of random bits. */
1259 DoTriggerVehicle(v
->First(), VEHICLE_TRIGGER_ANY_NEW_CARGO
, new_random_bits
, false);
1262 case VEHICLE_TRIGGER_DEPOT
:
1263 /* We now trigger the next vehicle in chain recursively.
1264 * The random bits portions may be different for each
1265 * vehicle in chain. */
1266 if (v
->Next() != nullptr) DoTriggerVehicle(v
->Next(), trigger
, 0, true);
1269 case VEHICLE_TRIGGER_EMPTY
:
1270 /* We now trigger the next vehicle in chain
1271 * recursively. The random bits portions must be same
1272 * for each vehicle in chain, so we give them all
1273 * first chained vehicle's portion of random bits. */
1274 if (v
->Next() != nullptr) DoTriggerVehicle(v
->Next(), trigger
, first
? new_random_bits
: base_random_bits
, false);
1277 case VEHICLE_TRIGGER_ANY_NEW_CARGO
:
1278 /* Now pass the trigger recursively to the next vehicle
1281 if (v
->Next() != nullptr) DoTriggerVehicle(v
->Next(), VEHICLE_TRIGGER_ANY_NEW_CARGO
, base_random_bits
, false);
1284 case VEHICLE_TRIGGER_CALLBACK_32
:
1285 /* Do not do any recursion */
1290 void TriggerVehicle(Vehicle
*v
, VehicleTrigger trigger
)
1292 if (trigger
== VEHICLE_TRIGGER_DEPOT
) {
1293 /* store that the vehicle entered a depot this tick */
1294 VehicleEnteredDepotThisTick(v
);
1297 v
->InvalidateNewGRFCacheOfChain();
1298 DoTriggerVehicle(v
, trigger
, 0, true);
1299 v
->InvalidateNewGRFCacheOfChain();
1302 /* Functions for changing the order of vehicle purchase lists */
1304 struct ListOrderChange
{
1306 uint target
; ///< local ID
1309 static std::vector
<ListOrderChange
> _list_order_changes
;
1312 * Record a vehicle ListOrderChange.
1313 * @param engine Engine to move
1314 * @param target Local engine ID to move \a engine in front of
1315 * @note All sorting is done later in CommitVehicleListOrderChanges
1317 void AlterVehicleListOrder(EngineID engine
, uint target
)
1319 /* Add the list order change to a queue */
1320 _list_order_changes
.push_back({engine
, target
});
1324 * Comparator function to sort engines via scope-GRFID and local ID.
1325 * @param a left side
1326 * @param b right side
1327 * @return comparison result
1329 static bool EnginePreSort(const EngineID
&a
, const EngineID
&b
)
1331 const EngineIDMapping
&id_a
= _engine_mngr
.at(a
);
1332 const EngineIDMapping
&id_b
= _engine_mngr
.at(b
);
1334 /* 1. Sort by engine type */
1335 if (id_a
.type
!= id_b
.type
) return (int)id_a
.type
< (int)id_b
.type
;
1337 /* 2. Sort by scope-GRFID */
1338 if (id_a
.grfid
!= id_b
.grfid
) return id_a
.grfid
< id_b
.grfid
;
1340 /* 3. Sort by local ID */
1341 return (int)id_a
.internal_id
< (int)id_b
.internal_id
;
1345 * Deternine default engine sorting and execute recorded ListOrderChanges from AlterVehicleListOrder.
1347 void CommitVehicleListOrderChanges()
1349 /* Pre-sort engines by scope-grfid and local index */
1350 std::vector
<EngineID
> ordering
;
1351 for (const Engine
*e
: Engine::Iterate()) {
1352 ordering
.push_back(e
->index
);
1354 std::sort(ordering
.begin(), ordering
.end(), EnginePreSort
);
1356 /* Apply Insertion-Sort operations */
1357 for (const ListOrderChange
&it
: _list_order_changes
) {
1358 EngineID source
= it
.engine
;
1359 uint local_target
= it
.target
;
1361 const EngineIDMapping
*id_source
= _engine_mngr
.data() + source
;
1362 if (id_source
->internal_id
== local_target
) continue;
1364 EngineID target
= _engine_mngr
.GetID(id_source
->type
, local_target
, id_source
->grfid
);
1365 if (target
== INVALID_ENGINE
) continue;
1367 int source_index
= find_index(ordering
, source
);
1368 int target_index
= find_index(ordering
, target
);
1370 assert(source_index
>= 0 && target_index
>= 0);
1371 assert(source_index
!= target_index
);
1373 EngineID
*list
= ordering
.data();
1374 if (source_index
< target_index
) {
1376 for (int i
= source_index
; i
< target_index
; ++i
) list
[i
] = list
[i
+ 1];
1377 list
[target_index
] = source
;
1379 for (int i
= source_index
; i
> target_index
; --i
) list
[i
] = list
[i
- 1];
1380 list
[target_index
] = source
;
1384 /* Store final sort-order */
1386 for (const EngineID
&eid
: ordering
) {
1387 Engine::Get(eid
)->list_position
= index
;
1391 /* Clear out the queue */
1392 _list_order_changes
.clear();
1393 _list_order_changes
.shrink_to_fit();
1397 * Fill the grf_cache of the given vehicle.
1398 * @param v The vehicle to fill the cache for.
1400 void FillNewGRFVehicleCache(const Vehicle
*v
)
1402 VehicleResolverObject
ro(v
->engine_type
, v
, VehicleResolverObject::WO_NONE
);
1404 /* These variables we have to check; these are the ones with a cache. */
1405 static const int cache_entries
[][2] = {
1406 { 0x40, NCVV_POSITION_CONSIST_LENGTH
},
1407 { 0x41, NCVV_POSITION_SAME_ID_LENGTH
},
1408 { 0x42, NCVV_CONSIST_CARGO_INFORMATION
},
1409 { 0x43, NCVV_COMPANY_INFORMATION
},
1410 { 0x4D, NCVV_POSITION_IN_VEHICLE
},
1412 static_assert(NCVV_END
== lengthof(cache_entries
));
1414 /* Resolve all the variables, so their caches are set. */
1415 for (size_t i
= 0; i
< lengthof(cache_entries
); i
++) {
1416 /* Only resolve when the cache isn't valid. */
1417 if (HasBit(v
->grf_cache
.cache_valid
, cache_entries
[i
][1])) continue;
1419 ro
.GetScope(VSG_SCOPE_SELF
)->GetVariable(cache_entries
[i
][0], 0, &stub
);
1422 /* Make sure really all bits are set. */
1423 assert(v
->grf_cache
.cache_valid
== (1 << NCVV_END
) - 1);