OpenTTD
newgrf_engine.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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  */
9 
12 #include "stdafx.h"
13 #include "debug.h"
14 #include "train.h"
15 #include "roadveh.h"
16 #include "company_func.h"
17 #include "newgrf_cargo.h"
18 #include "newgrf_spritegroup.h"
19 #include "date_func.h"
20 #include "vehicle_func.h"
21 #include "core/random_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 
31 struct WagonOverride {
32  EngineID *train_id;
33  uint trains;
34  CargoID cargo;
35  const SpriteGroup *group;
36 };
37 
38 void SetWagonOverrideSprites(EngineID engine, CargoID cargo, const SpriteGroup *group, EngineID *train_id, uint trains)
39 {
40  Engine *e = Engine::Get(engine);
41  WagonOverride *wo;
42 
43  assert(cargo < NUM_CARGO + 2); // Include CT_DEFAULT and CT_PURCHASE pseudo cargoes.
44 
45  e->overrides_count++;
46  e->overrides = ReallocT(e->overrides, e->overrides_count);
47 
48  wo = &e->overrides[e->overrides_count - 1];
49  wo->group = group;
50  wo->cargo = cargo;
51  wo->trains = trains;
52  wo->train_id = MallocT<EngineID>(trains);
53  memcpy(wo->train_id, train_id, trains * sizeof *train_id);
54 }
55 
56 const SpriteGroup *GetWagonOverrideSpriteSet(EngineID engine, CargoID cargo, EngineID overriding_engine)
57 {
58  const Engine *e = Engine::Get(engine);
59 
60  for (uint i = 0; i < e->overrides_count; i++) {
61  const WagonOverride *wo = &e->overrides[i];
62 
63  if (wo->cargo != cargo && wo->cargo != CT_DEFAULT) continue;
64 
65  for (uint j = 0; j < wo->trains; j++) {
66  if (wo->train_id[j] == overriding_engine) return wo->group;
67  }
68  }
69  return nullptr;
70 }
71 
76 {
77  for (uint i = 0; i < e->overrides_count; i++) {
78  WagonOverride *wo = &e->overrides[i];
79  free(wo->train_id);
80  }
81  free(e->overrides);
82  e->overrides_count = 0;
83  e->overrides = nullptr;
84 }
85 
86 
87 void SetCustomEngineSprites(EngineID engine, byte cargo, const SpriteGroup *group)
88 {
89  Engine *e = Engine::Get(engine);
90  assert(cargo < lengthof(e->grf_prop.spritegroup));
91 
92  if (e->grf_prop.spritegroup[cargo] != nullptr) {
93  grfmsg(6, "SetCustomEngineSprites: engine %d cargo %d already has group -- replacing", engine, cargo);
94  }
95  e->grf_prop.spritegroup[cargo] = group;
96 }
97 
98 
105 void SetEngineGRF(EngineID engine, const GRFFile *file)
106 {
107  Engine *e = Engine::Get(engine);
108  e->grf_prop.grffile = file;
109 }
110 
111 
112 static int MapOldSubType(const Vehicle *v)
113 {
114  switch (v->type) {
115  case VEH_TRAIN:
116  if (Train::From(v)->IsEngine()) return 0;
117  if (Train::From(v)->IsFreeWagon()) return 4;
118  return 2;
119  case VEH_ROAD:
120  case VEH_SHIP: return 0;
121  case VEH_AIRCRAFT:
122  case VEH_DISASTER: return v->subtype;
123  case VEH_EFFECT: return v->subtype << 1;
124  default: NOT_REACHED();
125  }
126 }
127 
128 
129 /* TTDP style aircraft movement states for GRF Action 2 Var 0xE2 */
130 enum TTDPAircraftMovementStates {
131  AMS_TTDP_HANGAR,
132  AMS_TTDP_TO_HANGAR,
133  AMS_TTDP_TO_PAD1,
134  AMS_TTDP_TO_PAD2,
135  AMS_TTDP_TO_PAD3,
136  AMS_TTDP_TO_ENTRY_2_AND_3,
137  AMS_TTDP_TO_ENTRY_2_AND_3_AND_H,
138  AMS_TTDP_TO_JUNCTION,
139  AMS_TTDP_LEAVE_RUNWAY,
140  AMS_TTDP_TO_INWAY,
141  AMS_TTDP_TO_RUNWAY,
142  AMS_TTDP_TO_OUTWAY,
143  AMS_TTDP_WAITING,
144  AMS_TTDP_TAKEOFF,
145  AMS_TTDP_TO_TAKEOFF,
146  AMS_TTDP_CLIMBING,
147  AMS_TTDP_FLIGHT_APPROACH,
148  AMS_TTDP_UNUSED_0x11,
149  AMS_TTDP_FLIGHT_TO_TOWER,
150  AMS_TTDP_UNUSED_0x13,
151  AMS_TTDP_FLIGHT_FINAL,
152  AMS_TTDP_FLIGHT_DESCENT,
153  AMS_TTDP_BRAKING,
154  AMS_TTDP_HELI_TAKEOFF_AIRPORT,
155  AMS_TTDP_HELI_TO_TAKEOFF_AIRPORT,
156  AMS_TTDP_HELI_LAND_AIRPORT,
157  AMS_TTDP_HELI_TAKEOFF_HELIPORT,
158  AMS_TTDP_HELI_TO_TAKEOFF_HELIPORT,
159  AMS_TTDP_HELI_LAND_HELIPORT,
160 };
161 
162 
167 static byte MapAircraftMovementState(const Aircraft *v)
168 {
169  const Station *st = GetTargetAirportIfValid(v);
170  if (st == nullptr) return AMS_TTDP_FLIGHT_TO_TOWER;
171 
172  const AirportFTAClass *afc = st->airport.GetFTA();
173  uint16 amdflag = afc->MovingData(v->pos)->flag;
174 
175  switch (v->state) {
176  case HANGAR:
177  /* The international airport is a special case as helicopters can land in
178  * front of the hangar. Helicopters also change their air.state to
179  * AMED_HELI_LOWER some time before actually descending. */
180 
181  /* This condition only occurs for helicopters, during descent,
182  * to a landing by the hangar of an international airport. */
183  if (amdflag & AMED_HELI_LOWER) return AMS_TTDP_HELI_LAND_AIRPORT;
184 
185  /* This condition only occurs for helicopters, before starting descent,
186  * to a landing by the hangar of an international airport. */
187  if (amdflag & AMED_SLOWTURN) return AMS_TTDP_FLIGHT_TO_TOWER;
188 
189  /* The final two conditions apply to helicopters or aircraft.
190  * Has reached hangar? */
191  if (amdflag & AMED_EXACTPOS) return AMS_TTDP_HANGAR;
192 
193  /* Still moving towards hangar. */
194  return AMS_TTDP_TO_HANGAR;
195 
196  case TERM1:
197  if (amdflag & AMED_EXACTPOS) return AMS_TTDP_TO_PAD1;
198  return AMS_TTDP_TO_JUNCTION;
199 
200  case TERM2:
201  if (amdflag & AMED_EXACTPOS) return AMS_TTDP_TO_PAD2;
202  return AMS_TTDP_TO_ENTRY_2_AND_3_AND_H;
203 
204  case TERM3:
205  case TERM4:
206  case TERM5:
207  case TERM6:
208  case TERM7:
209  case TERM8:
210  /* TTDPatch only has 3 terminals, so treat these states the same */
211  if (amdflag & AMED_EXACTPOS) return AMS_TTDP_TO_PAD3;
212  return AMS_TTDP_TO_ENTRY_2_AND_3_AND_H;
213 
214  case HELIPAD1:
215  case HELIPAD2:
216  case HELIPAD3:
217  /* Will only occur for helicopters.*/
218  if (amdflag & AMED_HELI_LOWER) return AMS_TTDP_HELI_LAND_AIRPORT; // Descending.
219  if (amdflag & AMED_SLOWTURN) return AMS_TTDP_FLIGHT_TO_TOWER; // Still hasn't started descent.
220  return AMS_TTDP_TO_JUNCTION; // On the ground.
221 
222  case TAKEOFF: // Moving to takeoff position.
223  return AMS_TTDP_TO_OUTWAY;
224 
225  case STARTTAKEOFF: // Accelerating down runway.
226  return AMS_TTDP_TAKEOFF;
227 
228  case ENDTAKEOFF: // Ascent
229  return AMS_TTDP_CLIMBING;
230 
231  case HELITAKEOFF: // Helicopter is moving to take off position.
232  if (afc->delta_z == 0) {
233  return amdflag & AMED_HELI_RAISE ?
234  AMS_TTDP_HELI_TAKEOFF_AIRPORT : AMS_TTDP_TO_JUNCTION;
235  } else {
236  return AMS_TTDP_HELI_TAKEOFF_HELIPORT;
237  }
238 
239  case FLYING:
240  return amdflag & AMED_HOLD ? AMS_TTDP_FLIGHT_APPROACH : AMS_TTDP_FLIGHT_TO_TOWER;
241 
242  case LANDING: // Descent
243  return AMS_TTDP_FLIGHT_DESCENT;
244 
245  case ENDLANDING: // On the runway braking
246  if (amdflag & AMED_BRAKE) return AMS_TTDP_BRAKING;
247  /* Landed - moving off runway */
248  return AMS_TTDP_TO_INWAY;
249 
250  case HELILANDING:
251  case HELIENDLANDING: // Helicoptor is descending.
252  if (amdflag & AMED_HELI_LOWER) {
253  return afc->delta_z == 0 ?
254  AMS_TTDP_HELI_LAND_AIRPORT : AMS_TTDP_HELI_LAND_HELIPORT;
255  } else {
256  return AMS_TTDP_FLIGHT_TO_TOWER;
257  }
258 
259  default:
260  return AMS_TTDP_HANGAR;
261  }
262 }
263 
264 
265 /* TTDP style aircraft movement action for GRF Action 2 Var 0xE6 */
266 enum TTDPAircraftMovementActions {
267  AMA_TTDP_IN_HANGAR,
268  AMA_TTDP_ON_PAD1,
269  AMA_TTDP_ON_PAD2,
270  AMA_TTDP_ON_PAD3,
271  AMA_TTDP_HANGAR_TO_PAD1,
272  AMA_TTDP_HANGAR_TO_PAD2,
273  AMA_TTDP_HANGAR_TO_PAD3,
274  AMA_TTDP_LANDING_TO_PAD1,
275  AMA_TTDP_LANDING_TO_PAD2,
276  AMA_TTDP_LANDING_TO_PAD3,
277  AMA_TTDP_PAD1_TO_HANGAR,
278  AMA_TTDP_PAD2_TO_HANGAR,
279  AMA_TTDP_PAD3_TO_HANGAR,
280  AMA_TTDP_PAD1_TO_TAKEOFF,
281  AMA_TTDP_PAD2_TO_TAKEOFF,
282  AMA_TTDP_PAD3_TO_TAKEOFF,
283  AMA_TTDP_HANGAR_TO_TAKOFF,
284  AMA_TTDP_LANDING_TO_HANGAR,
285  AMA_TTDP_IN_FLIGHT,
286 };
287 
288 
294 static byte MapAircraftMovementAction(const Aircraft *v)
295 {
296  switch (v->state) {
297  case HANGAR:
298  return (v->cur_speed > 0) ? AMA_TTDP_LANDING_TO_HANGAR : AMA_TTDP_IN_HANGAR;
299 
300  case TERM1:
301  case HELIPAD1:
302  return (v->current_order.IsType(OT_LOADING)) ? AMA_TTDP_ON_PAD1 : AMA_TTDP_LANDING_TO_PAD1;
303 
304  case TERM2:
305  case HELIPAD2:
306  return (v->current_order.IsType(OT_LOADING)) ? AMA_TTDP_ON_PAD2 : AMA_TTDP_LANDING_TO_PAD2;
307 
308  case TERM3:
309  case TERM4:
310  case TERM5:
311  case TERM6:
312  case TERM7:
313  case TERM8:
314  case HELIPAD3:
315  return (v->current_order.IsType(OT_LOADING)) ? AMA_TTDP_ON_PAD3 : AMA_TTDP_LANDING_TO_PAD3;
316 
317  case TAKEOFF: // Moving to takeoff position
318  case STARTTAKEOFF: // Accelerating down runway
319  case ENDTAKEOFF: // Ascent
320  case HELITAKEOFF:
321  /* @todo Need to find which terminal (or hangar) we've come from. How? */
322  return AMA_TTDP_PAD1_TO_TAKEOFF;
323 
324  case FLYING:
325  return AMA_TTDP_IN_FLIGHT;
326 
327  case LANDING: // Descent
328  case ENDLANDING: // On the runway braking
329  case HELILANDING:
330  case HELIENDLANDING:
331  /* @todo Need to check terminal we're landing to. Is it known yet? */
332  return (v->current_order.IsType(OT_GOTO_DEPOT)) ?
333  AMA_TTDP_LANDING_TO_HANGAR : AMA_TTDP_LANDING_TO_PAD1;
334 
335  default:
336  return AMA_TTDP_IN_HANGAR;
337  }
338 }
339 
340 
341 /* virtual */ uint32 VehicleScopeResolver::GetRandomBits() const
342 {
343  return this->v == nullptr ? 0 : this->v->random_bits;
344 }
345 
346 /* virtual */ uint32 VehicleScopeResolver::GetTriggers() const
347 {
348  return this->v == nullptr ? 0 : this->v->waiting_triggers;
349 }
350 
351 
353 {
354  switch (scope) {
355  case VSG_SCOPE_SELF: return &this->self_scope;
356  case VSG_SCOPE_PARENT: return &this->parent_scope;
357  case VSG_SCOPE_RELATIVE: {
358  int32 count = GB(relative, 0, 4);
359  if (this->self_scope.v != nullptr && (relative != this->cached_relative_count || count == 0)) {
360  /* Note: This caching only works as long as the VSG_SCOPE_RELATIVE cannot be used in
361  * VarAct2 with procedure calls. */
362  if (count == 0) count = GetRegister(0x100);
363 
364  const Vehicle *v = nullptr;
365  switch (GB(relative, 6, 2)) {
366  default: NOT_REACHED();
367  case 0x00: // count back (away from the engine), starting at this vehicle
368  v = this->self_scope.v;
369  break;
370  case 0x01: // count forward (toward the engine), starting at this vehicle
371  v = this->self_scope.v;
372  count = -count;
373  break;
374  case 0x02: // count back, starting at the engine
375  v = this->parent_scope.v;
376  break;
377  case 0x03: { // count back, starting at the first vehicle in this chain of vehicles with the same ID, as for vehicle variable 41
378  const Vehicle *self = this->self_scope.v;
379  for (const Vehicle *u = self->First(); u != self; u = u->Next()) {
380  if (u->engine_type != self->engine_type) {
381  v = nullptr;
382  } else {
383  if (v == nullptr) v = u;
384  }
385  }
386  if (v == nullptr) v = self;
387  break;
388  }
389  }
390  this->relative_scope.SetVehicle(v->Move(count));
391  }
392  return &this->relative_scope;
393  }
394  default: return ResolverObject::GetScope(scope, relative);
395  }
396 }
397 
407 static const Livery *LiveryHelper(EngineID engine, const Vehicle *v)
408 {
409  const Livery *l;
410 
411  if (v == nullptr) {
412  if (!Company::IsValidID(_current_company)) return nullptr;
413  l = GetEngineLivery(engine, _current_company, INVALID_ENGINE, nullptr, LIT_ALL);
414  } else if (v->IsGroundVehicle()) {
416  } else {
418  }
419 
420  return l;
421 }
422 
430 static uint32 PositionHelper(const Vehicle *v, bool consecutive)
431 {
432  const Vehicle *u;
433  byte chain_before = 0;
434  byte chain_after = 0;
435 
436  for (u = v->First(); u != v; u = u->Next()) {
437  chain_before++;
438  if (consecutive && u->engine_type != v->engine_type) chain_before = 0;
439  }
440 
441  while (u->Next() != nullptr && (!consecutive || u->Next()->engine_type == v->engine_type)) {
442  chain_after++;
443  u = u->Next();
444  }
445 
446  return chain_before | chain_after << 8 | (chain_before + chain_after + consecutive) << 16;
447 }
448 
449 static uint32 VehicleGetVariable(Vehicle *v, const VehicleScopeResolver *object, byte variable, uint32 parameter, bool *available)
450 {
451  /* Calculated vehicle parameters */
452  switch (variable) {
453  case 0x25: // Get engine GRF ID
454  return v->GetGRFID();
455 
456  case 0x40: // Get length of consist
460  }
462 
463  case 0x41: // Get length of same consecutive wagons
467  }
469 
470  case 0x42: { // Consist cargo information
472  const Vehicle *u;
473  byte cargo_classes = 0;
474  uint8 common_cargoes[NUM_CARGO];
475  uint8 common_subtypes[256];
476  byte user_def_data = 0;
477  CargoID common_cargo_type = CT_INVALID;
478  uint8 common_subtype = 0xFF; // Return 0xFF if nothing is carried
479 
480  /* Reset our arrays */
481  memset(common_cargoes, 0, sizeof(common_cargoes));
482  memset(common_subtypes, 0, sizeof(common_subtypes));
483 
484  for (u = v; u != nullptr; u = u->Next()) {
485  if (v->type == VEH_TRAIN) user_def_data |= Train::From(u)->tcache.user_def_data;
486 
487  /* Skip empty engines */
488  if (!u->GetEngine()->CanCarryCargo()) continue;
489 
490  cargo_classes |= CargoSpec::Get(u->cargo_type)->classes;
491  common_cargoes[u->cargo_type]++;
492  }
493 
494  /* Pick the most common cargo type */
495  uint common_cargo_best_amount = 0;
496  for (CargoID cargo = 0; cargo < NUM_CARGO; cargo++) {
497  if (common_cargoes[cargo] > common_cargo_best_amount) {
498  common_cargo_best_amount = common_cargoes[cargo];
499  common_cargo_type = cargo;
500  }
501  }
502 
503  /* Count subcargo types of common_cargo_type */
504  for (u = v; u != nullptr; u = u->Next()) {
505  /* Skip empty engines and engines not carrying common_cargo_type */
506  if (u->cargo_type != common_cargo_type || !u->GetEngine()->CanCarryCargo()) continue;
507 
508  common_subtypes[u->cargo_subtype]++;
509  }
510 
511  /* Pick the most common subcargo type*/
512  uint common_subtype_best_amount = 0;
513  for (uint i = 0; i < lengthof(common_subtypes); i++) {
514  if (common_subtypes[i] > common_subtype_best_amount) {
515  common_subtype_best_amount = common_subtypes[i];
516  common_subtype = i;
517  }
518  }
519 
520  /* Note: We have to store the untranslated cargotype in the cache as the cache can be read by different NewGRFs,
521  * which will need different translations */
522  v->grf_cache.consist_cargo_information = cargo_classes | (common_cargo_type << 8) | (common_subtype << 16) | (user_def_data << 24);
524  }
525 
526  /* The cargo translation is specific to the accessing GRF, and thus cannot be cached. */
527  CargoID common_cargo_type = (v->grf_cache.consist_cargo_information >> 8) & 0xFF;
528 
529  /* Note:
530  * - Unlike everywhere else the cargo translation table is only used since grf version 8, not 7.
531  * - For translating the cargo type we need to use the GRF which is resolving the variable, which
532  * is object->ro.grffile.
533  * In case of CBID_TRAIN_ALLOW_WAGON_ATTACH this is not the same as v->GetGRF().
534  * - The grffile == nullptr case only happens if this function is called for default vehicles.
535  * And this is only done by CheckCaches().
536  */
537  const GRFFile *grffile = object->ro.grffile;
538  uint8 common_bitnum = (common_cargo_type == CT_INVALID) ? 0xFF :
539  (grffile == nullptr || grffile->grf_version < 8) ? CargoSpec::Get(common_cargo_type)->bitnum : grffile->cargo_map[common_cargo_type];
540 
541  return (v->grf_cache.consist_cargo_information & 0xFFFF00FF) | common_bitnum << 8;
542  }
543 
544  case 0x43: // Company information
548  }
549  return v->grf_cache.company_information;
550 
551  case 0x44: // Aircraft information
552  if (v->type != VEH_AIRCRAFT || !Aircraft::From(v)->IsNormalAircraft()) return UINT_MAX;
553 
554  {
555  const Vehicle *w = v->Next();
556  uint16 altitude = ClampToU16(v->z_pos - w->z_pos); // Aircraft height - shadow height
557  byte airporttype = ATP_TTDP_LARGE;
558 
560 
561  if (st != nullptr && st->airport.tile != INVALID_TILE) {
562  airporttype = st->airport.GetSpec()->ttd_airport_type;
563  }
564 
565  return (Clamp(altitude, 0, 0xFF) << 8) | airporttype;
566  }
567 
568  case 0x45: { // Curvature info
569  /* Format: xxxTxBxF
570  * F - previous wagon to current wagon, 0 if vehicle is first
571  * B - current wagon to next wagon, 0 if wagon is last
572  * T - previous wagon to next wagon, 0 in an S-bend
573  */
574  if (!v->IsGroundVehicle()) return 0;
575 
576  const Vehicle *u_p = v->Previous();
577  const Vehicle *u_n = v->Next();
578  DirDiff f = (u_p == nullptr) ? DIRDIFF_SAME : DirDifference(u_p->direction, v->direction);
579  DirDiff b = (u_n == nullptr) ? DIRDIFF_SAME : DirDifference(v->direction, u_n->direction);
580  DirDiff t = ChangeDirDiff(f, b);
581 
582  return ((t > DIRDIFF_REVERSE ? t | 8 : t) << 16) |
583  ((b > DIRDIFF_REVERSE ? b | 8 : b) << 8) |
584  ( f > DIRDIFF_REVERSE ? f | 8 : f);
585  }
586 
587  case 0x46: // Motion counter
588  return v->motion_counter;
589 
590  case 0x47: { // Vehicle cargo info
591  /* Format: ccccwwtt
592  * tt - the cargo type transported by the vehicle,
593  * translated if a translation table has been installed.
594  * ww - cargo unit weight in 1/16 tons, same as cargo prop. 0F.
595  * cccc - the cargo class value of the cargo transported by the vehicle.
596  */
597  const CargoSpec *cs = CargoSpec::Get(v->cargo_type);
598 
599  /* Note:
600  * For translating the cargo type we need to use the GRF which is resolving the variable, which
601  * is object->ro.grffile.
602  * In case of CBID_TRAIN_ALLOW_WAGON_ATTACH this is not the same as v->GetGRF().
603  */
604  return (cs->classes << 16) | (cs->weight << 8) | object->ro.grffile->cargo_map[v->cargo_type];
605  }
606 
607  case 0x48: return v->GetEngine()->flags; // Vehicle Type Info
608  case 0x49: return v->build_year;
609 
610  case 0x4A:
611  switch (v->type) {
612  case VEH_TRAIN: {
613  RailType rt = GetTileRailType(v->tile);
614  return (HasPowerOnRail(Train::From(v)->railtype, rt) ? 0x100 : 0) | GetReverseRailTypeTranslation(rt, object->ro.grffile);
615  }
616 
617  case VEH_ROAD: {
618  RoadType rt = GetRoadType(v->tile, GetRoadTramType(RoadVehicle::From(v)->roadtype));
619  return 0x100 | GetReverseRoadTypeTranslation(rt, object->ro.grffile);
620  }
621 
622  default:
623  return 0;
624  }
625 
626  case 0x4B: // Long date of last service
627  return v->date_of_last_service;
628 
629  case 0x4C: // Current maximum speed in NewGRF units
630  if (!v->IsPrimaryVehicle()) return 0;
631  return v->GetCurrentMaxSpeed();
632 
633  case 0x4D: // Position within articulated vehicle
635  byte artic_before = 0;
636  for (const Vehicle *u = v; u->IsArticulatedPart(); u = u->Previous()) artic_before++;
637  byte artic_after = 0;
638  for (const Vehicle *u = v; u->HasArticulatedPart(); u = u->Next()) artic_after++;
639  v->grf_cache.position_in_vehicle = artic_before | artic_after << 8;
641  }
642  return v->grf_cache.position_in_vehicle;
643 
644  /* Variables which use the parameter */
645  case 0x60: // Count consist's engine ID occurrence
646  if (v->type != VEH_TRAIN) return v->GetEngine()->grf_prop.local_id == parameter ? 1 : 0;
647 
648  {
649  uint count = 0;
650  for (; v != nullptr; v = v->Next()) {
651  if (v->GetEngine()->grf_prop.local_id == parameter) count++;
652  }
653  return count;
654  }
655 
656  case 0x61: // Get variable of n-th vehicle in chain [signed number relative to vehicle]
657  if (!v->IsGroundVehicle() || parameter == 0x61) {
658  /* Not available */
659  break;
660  }
661 
662  /* Only allow callbacks that don't change properties to avoid circular dependencies. */
666  Vehicle *u = v->Move((int32)GetRegister(0x10F));
667  if (u == nullptr) return 0; // available, but zero
668 
669  if (parameter == 0x5F) {
670  /* This seems to be the only variable that makes sense to access via var 61, but is not handled by VehicleGetVariable */
671  return (u->random_bits << 8) | u->waiting_triggers;
672  } else {
673  return VehicleGetVariable(u, object, parameter, GetRegister(0x10E), available);
674  }
675  }
676  /* Not available */
677  break;
678 
679  case 0x62: { // Curvature/position difference for n-th vehicle in chain [signed number relative to vehicle]
680  /* Format: zzyyxxFD
681  * zz - Signed difference of z position between the selected and this vehicle.
682  * yy - Signed difference of y position between the selected and this vehicle.
683  * xx - Signed difference of x position between the selected and this vehicle.
684  * F - Flags, bit 7 corresponds to VS_HIDDEN.
685  * D - Dir difference, like in 0x45.
686  */
687  if (!v->IsGroundVehicle()) return 0;
688 
689  const Vehicle *u = v->Move((int8)parameter);
690  if (u == nullptr) return 0;
691 
692  /* Get direction difference. */
693  bool prev = (int8)parameter < 0;
694  uint32 ret = prev ? DirDifference(u->direction, v->direction) : DirDifference(v->direction, u->direction);
695  if (ret > DIRDIFF_REVERSE) ret |= 0x08;
696 
697  if (u->vehstatus & VS_HIDDEN) ret |= 0x80;
698 
699  /* Get position difference. */
700  ret |= ((prev ? u->x_pos - v->x_pos : v->x_pos - u->x_pos) & 0xFF) << 8;
701  ret |= ((prev ? u->y_pos - v->y_pos : v->y_pos - u->y_pos) & 0xFF) << 16;
702  ret |= ((prev ? u->z_pos - v->z_pos : v->z_pos - u->z_pos) & 0xFF) << 24;
703 
704  return ret;
705  }
706 
707  case 0xFE:
708  case 0xFF: {
709  uint16 modflags = 0;
710 
711  if (v->type == VEH_TRAIN) {
712  const Train *t = Train::From(v);
713  bool is_powered_wagon = HasBit(t->flags, VRF_POWEREDWAGON);
714  const Train *u = is_powered_wagon ? t->First() : t; // for powered wagons the engine defines the type of engine (i.e. railtype)
715  RailType railtype = GetRailType(v->tile);
716  bool powered = t->IsEngine() || is_powered_wagon;
717  bool has_power = HasPowerOnRail(u->railtype, railtype);
718 
719  if (powered && has_power) SetBit(modflags, 5);
720  if (powered && !has_power) SetBit(modflags, 6);
721  if (HasBit(t->flags, VRF_TOGGLE_REVERSE)) SetBit(modflags, 8);
722  }
723  if (HasBit(v->vehicle_flags, VF_CARGO_UNLOADING)) SetBit(modflags, 1);
724  if (HasBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE)) SetBit(modflags, 10);
725 
726  return variable == 0xFE ? modflags : GB(modflags, 8, 8);
727  }
728  }
729 
730  /* General vehicle properties */
731  switch (variable - 0x80) {
732  case 0x00: return v->type + 0x10;
733  case 0x01: return MapOldSubType(v);
734  case 0x04: return v->index;
735  case 0x05: return GB(v->index, 8, 8);
736  case 0x0A: return v->current_order.MapOldOrder();
737  case 0x0B: return v->current_order.GetDestination();
738  case 0x0C: return v->GetNumOrders();
739  case 0x0D: return v->cur_real_order_index;
740  case 0x10:
741  case 0x11: {
742  uint ticks;
743  if (v->current_order.IsType(OT_LOADING)) {
744  ticks = v->load_unload_ticks;
745  } else {
746  switch (v->type) {
747  case VEH_TRAIN: ticks = Train::From(v)->wait_counter; break;
748  case VEH_AIRCRAFT: ticks = Aircraft::From(v)->turn_counter; break;
749  default: ticks = 0; break;
750  }
751  }
752  return (variable - 0x80) == 0x10 ? ticks : GB(ticks, 8, 8);
753  }
754  case 0x12: return Clamp(v->date_of_last_service - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 0xFFFF);
755  case 0x13: return GB(Clamp(v->date_of_last_service - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 0xFFFF), 8, 8);
756  case 0x14: return v->GetServiceInterval();
757  case 0x15: return GB(v->GetServiceInterval(), 8, 8);
758  case 0x16: return v->last_station_visited;
759  case 0x17: return v->tick_counter;
760  case 0x18:
761  case 0x19: {
762  uint max_speed;
763  switch (v->type) {
764  case VEH_AIRCRAFT:
765  max_speed = Aircraft::From(v)->GetSpeedOldUnits(); // Convert to old units.
766  break;
767 
768  default:
769  max_speed = v->vcache.cached_max_speed;
770  break;
771  }
772  return (variable - 0x80) == 0x18 ? max_speed : GB(max_speed, 8, 8);
773  }
774  case 0x1A: return v->x_pos;
775  case 0x1B: return GB(v->x_pos, 8, 8);
776  case 0x1C: return v->y_pos;
777  case 0x1D: return GB(v->y_pos, 8, 8);
778  case 0x1E: return v->z_pos;
779  case 0x1F: return object->info_view ? DIR_W : v->direction;
780  case 0x28: return 0; // cur_image is a potential desyncer due to Action1 in static NewGRFs.
781  case 0x29: return 0; // cur_image is a potential desyncer due to Action1 in static NewGRFs.
782  case 0x32: return v->vehstatus;
783  case 0x33: return 0; // non-existent high byte of vehstatus
784  case 0x34: return v->type == VEH_AIRCRAFT ? (v->cur_speed * 10) / 128 : v->cur_speed;
785  case 0x35: return GB(v->type == VEH_AIRCRAFT ? (v->cur_speed * 10) / 128 : v->cur_speed, 8, 8);
786  case 0x36: return v->subspeed;
787  case 0x37: return v->acceleration;
788  case 0x39: return v->cargo_type;
789  case 0x3A: return v->cargo_cap;
790  case 0x3B: return GB(v->cargo_cap, 8, 8);
791  case 0x3C: return ClampToU16(v->cargo.StoredCount());
792  case 0x3D: return GB(ClampToU16(v->cargo.StoredCount()), 8, 8);
793  case 0x3E: return v->cargo.Source();
794  case 0x3F: return ClampU(v->cargo.DaysInTransit(), 0, 0xFF);
795  case 0x40: return ClampToU16(v->age);
796  case 0x41: return GB(ClampToU16(v->age), 8, 8);
797  case 0x42: return ClampToU16(v->max_age);
798  case 0x43: return GB(ClampToU16(v->max_age), 8, 8);
800  case 0x45: return v->unitnumber;
801  case 0x46: return v->GetEngine()->grf_prop.local_id;
802  case 0x47: return GB(v->GetEngine()->grf_prop.local_id, 8, 8);
803  case 0x48:
804  if (v->type != VEH_TRAIN || v->spritenum != 0xFD) return v->spritenum;
805  return HasBit(Train::From(v)->flags, VRF_REVERSE_DIRECTION) ? 0xFE : 0xFD;
806 
807  case 0x49: return v->day_counter;
808  case 0x4A: return v->breakdowns_since_last_service;
809  case 0x4B: return v->breakdown_ctr;
810  case 0x4C: return v->breakdown_delay;
811  case 0x4D: return v->breakdown_chance;
812  case 0x4E: return v->reliability;
813  case 0x4F: return GB(v->reliability, 8, 8);
814  case 0x50: return v->reliability_spd_dec;
815  case 0x51: return GB(v->reliability_spd_dec, 8, 8);
816  case 0x52: return ClampToI32(v->GetDisplayProfitThisYear());
817  case 0x53: return GB(ClampToI32(v->GetDisplayProfitThisYear()), 8, 24);
818  case 0x54: return GB(ClampToI32(v->GetDisplayProfitThisYear()), 16, 16);
819  case 0x55: return GB(ClampToI32(v->GetDisplayProfitThisYear()), 24, 8);
820  case 0x56: return ClampToI32(v->GetDisplayProfitLastYear());
821  case 0x57: return GB(ClampToI32(v->GetDisplayProfitLastYear()), 8, 24);
822  case 0x58: return GB(ClampToI32(v->GetDisplayProfitLastYear()), 16, 16);
823  case 0x59: return GB(ClampToI32(v->GetDisplayProfitLastYear()), 24, 8);
824  case 0x5A: return v->Next() == nullptr ? INVALID_VEHICLE : v->Next()->index;
825  case 0x5C: return ClampToI32(v->value);
826  case 0x5D: return GB(ClampToI32(v->value), 8, 24);
827  case 0x5E: return GB(ClampToI32(v->value), 16, 16);
828  case 0x5F: return GB(ClampToI32(v->value), 24, 8);
829  case 0x72: return v->cargo_subtype;
830  case 0x7A: return v->random_bits;
831  case 0x7B: return v->waiting_triggers;
832  }
833 
834  /* Vehicle specific properties */
835  switch (v->type) {
836  case VEH_TRAIN: {
837  Train *t = Train::From(v);
838  switch (variable - 0x80) {
839  case 0x62: return t->track;
840  case 0x66: return t->railtype;
841  case 0x73: return 0x80 + VEHICLE_LENGTH - t->gcache.cached_veh_length;
842  case 0x74: return t->gcache.cached_power;
843  case 0x75: return GB(t->gcache.cached_power, 8, 24);
844  case 0x76: return GB(t->gcache.cached_power, 16, 16);
845  case 0x77: return GB(t->gcache.cached_power, 24, 8);
846  case 0x7C: return t->First()->index;
847  case 0x7D: return GB(t->First()->index, 8, 8);
848  case 0x7F: return 0; // Used for vehicle reversing hack in TTDP
849  }
850  break;
851  }
852 
853  case VEH_ROAD: {
855  switch (variable - 0x80) {
856  case 0x62: return rv->state;
857  case 0x64: return rv->blocked_ctr;
858  case 0x65: return GB(rv->blocked_ctr, 8, 8);
859  case 0x66: return rv->overtaking;
860  case 0x67: return rv->overtaking_ctr;
861  case 0x68: return rv->crashed_ctr;
862  case 0x69: return GB(rv->crashed_ctr, 8, 8);
863  }
864  break;
865  }
866 
867  case VEH_SHIP: {
868  Ship *s = Ship::From(v);
869  switch (variable - 0x80) {
870  case 0x62: return s->state;
871  }
872  break;
873  }
874 
875  case VEH_AIRCRAFT: {
876  Aircraft *a = Aircraft::From(v);
877  switch (variable - 0x80) {
878  case 0x62: return MapAircraftMovementState(a); // Current movement state
879  case 0x63: return a->targetairport; // Airport to which the action refers
880  case 0x66: return MapAircraftMovementAction(a); // Current movement action
881  }
882  break;
883  }
884 
885  default: break;
886  }
887 
888  DEBUG(grf, 1, "Unhandled vehicle variable 0x%X, type 0x%X", variable, (uint)v->type);
889 
890  *available = false;
891  return UINT_MAX;
892 }
893 
894 /* virtual */ uint32 VehicleScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
895 {
896  if (this->v == nullptr) {
897  /* Vehicle does not exist, so we're in a purchase list */
898  switch (variable) {
899  case 0x43: return GetCompanyInfo(_current_company, LiveryHelper(this->self_type, nullptr)); // Owner information
900  case 0x46: return 0; // Motion counter
901  case 0x47: { // Vehicle cargo info
902  const Engine *e = Engine::Get(this->self_type);
903  CargoID cargo_type = e->GetDefaultCargoType();
904  if (cargo_type != CT_INVALID) {
905  const CargoSpec *cs = CargoSpec::Get(cargo_type);
906  return (cs->classes << 16) | (cs->weight << 8) | this->ro.grffile->cargo_map[cargo_type];
907  } else {
908  return 0x000000FF;
909  }
910  }
911  case 0x48: return Engine::Get(this->self_type)->flags; // Vehicle Type Info
912  case 0x49: return _cur_year; // 'Long' format build year
913  case 0x4B: return _date; // Long date of last service
914  case 0x92: return Clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 0xFFFF); // Date of last service
915  case 0x93: return GB(Clamp(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 0xFFFF), 8, 8);
916  case 0xC4: return Clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR; // Build year
917  case 0xDA: return INVALID_VEHICLE; // Next vehicle
918  case 0xF2: return 0; // Cargo subtype
919  }
920 
921  *available = false;
922  return UINT_MAX;
923  }
924 
925  return VehicleGetVariable(const_cast<Vehicle*>(this->v), this, variable, parameter, available);
926 }
927 
928 
929 /* virtual */ const SpriteGroup *VehicleResolverObject::ResolveReal(const RealSpriteGroup *group) const
930 {
931  const Vehicle *v = this->self_scope.v;
932 
933  if (v == nullptr) {
934  if (group->num_loading > 0) return group->loading[0];
935  if (group->num_loaded > 0) return group->loaded[0];
936  return nullptr;
937  }
938 
939  bool in_motion = !v->First()->current_order.IsType(OT_LOADING);
940 
941  uint totalsets = in_motion ? group->num_loaded : group->num_loading;
942 
943  if (totalsets == 0) return nullptr;
944 
945  uint set = (v->cargo.StoredCount() * totalsets) / max((uint16)1, v->cargo_cap);
946  set = min(set, totalsets - 1);
947 
948  return in_motion ? group->loaded[set] : group->loading[set];
949 }
950 
956 static const GRFFile *GetEngineGrfFile(EngineID engine_type)
957 {
958  const Engine *e = Engine::Get(engine_type);
959  return (e != nullptr) ? e->GetGRF() : nullptr;
960 }
961 
972 VehicleResolverObject::VehicleResolverObject(EngineID engine_type, const Vehicle *v, WagonOverride wagon_override, bool info_view,
973  CallbackID callback, uint32 callback_param1, uint32 callback_param2)
974  : ResolverObject(GetEngineGrfFile(engine_type), callback, callback_param1, callback_param2),
975  self_scope(*this, engine_type, v, info_view),
976  parent_scope(*this, engine_type, ((v != nullptr) ? v->First() : v), info_view),
977  relative_scope(*this, engine_type, v, info_view),
978  cached_relative_count(0)
979 {
980  if (wagon_override == WO_SELF) {
981  this->root_spritegroup = GetWagonOverrideSpriteSet(engine_type, CT_DEFAULT, engine_type);
982  } else {
983  if (wagon_override != WO_NONE && v != nullptr && v->IsGroundVehicle()) {
984  assert(v->engine_type == engine_type); // overrides make little sense with fake scopes
985 
986  /* For trains we always use cached value, except for callbacks because the override spriteset
987  * to use may be different than the one cached. It happens for callback 0x15 (refit engine),
988  * as v->cargo_type is temporary changed to the new type */
989  if (wagon_override == WO_CACHED && v->type == VEH_TRAIN) {
990  this->root_spritegroup = Train::From(v)->tcache.cached_override;
991  } else {
992  this->root_spritegroup = GetWagonOverrideSpriteSet(v->engine_type, v->cargo_type, v->GetGroundVehicleCache()->first_engine);
993  }
994  }
995 
996  if (this->root_spritegroup == nullptr) {
997  const Engine *e = Engine::Get(engine_type);
998  CargoID cargo = v != nullptr ? v->cargo_type : CT_PURCHASE;
999  assert(cargo < lengthof(e->grf_prop.spritegroup));
1000  this->root_spritegroup = e->grf_prop.spritegroup[cargo] != nullptr ? e->grf_prop.spritegroup[cargo] : e->grf_prop.spritegroup[CT_DEFAULT];
1001  }
1002  }
1003 }
1004 
1005 
1006 
1007 void GetCustomEngineSprite(EngineID engine, const Vehicle *v, Direction direction, EngineImageType image_type, VehicleSpriteSeq *result)
1008 {
1010  result->Clear();
1011 
1012  bool sprite_stack = HasBit(EngInfo(engine)->misc_flags, EF_SPRITE_STACK);
1013  uint max_stack = sprite_stack ? lengthof(result->seq) : 1;
1014  for (uint stack = 0; stack < max_stack; ++stack) {
1015  object.ResetState();
1016  object.callback_param1 = image_type | (stack << 8);
1017  const SpriteGroup *group = object.Resolve();
1018  uint32 reg100 = sprite_stack ? GetRegister(0x100) : 0;
1019  if (group != nullptr && group->GetNumResults() != 0) {
1020  result->seq[result->count].sprite = group->GetResult() + (direction % group->GetNumResults());
1021  result->seq[result->count].pal = GB(reg100, 0, 16); // zero means default recolouring
1022  result->count++;
1023  }
1024  if (!HasBit(reg100, 31)) break;
1025  }
1026 }
1027 
1028 
1029 void GetRotorOverrideSprite(EngineID engine, const struct Aircraft *v, bool info_view, EngineImageType image_type, VehicleSpriteSeq *result)
1030 {
1031  const Engine *e = Engine::Get(engine);
1032 
1033  /* Only valid for helicopters */
1034  assert(e->type == VEH_AIRCRAFT);
1035  assert(!(e->u.air.subtype & AIR_CTOL));
1036 
1038  result->Clear();
1039  uint rotor_pos = v == nullptr || info_view ? 0 : v->Next()->Next()->state;
1040 
1041  bool sprite_stack = HasBit(e->info.misc_flags, EF_SPRITE_STACK);
1042  uint max_stack = sprite_stack ? lengthof(result->seq) : 1;
1043  for (uint stack = 0; stack < max_stack; ++stack) {
1044  object.ResetState();
1045  object.callback_param1 = image_type | (stack << 8);
1046  const SpriteGroup *group = object.Resolve();
1047  uint32 reg100 = sprite_stack ? GetRegister(0x100) : 0;
1048  if (group != nullptr && group->GetNumResults() != 0) {
1049  result->seq[result->count].sprite = group->GetResult() + (rotor_pos % group->GetNumResults());
1050  result->seq[result->count].pal = GB(reg100, 0, 16); // zero means default recolouring
1051  result->count++;
1052  }
1053  if (!HasBit(reg100, 31)) break;
1054  }
1055 }
1056 
1057 
1064 {
1065  assert(v->type == VEH_TRAIN);
1066  return Train::From(v)->tcache.cached_override != nullptr;
1067 }
1068 
1078 uint16 GetVehicleCallback(CallbackID callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v)
1079 {
1080  VehicleResolverObject object(engine, v, VehicleResolverObject::WO_UNCACHED, false, callback, param1, param2);
1081  return object.ResolveCallback();
1082 }
1083 
1094 uint16 GetVehicleCallbackParent(CallbackID callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v, const Vehicle *parent)
1095 {
1096  VehicleResolverObject object(engine, v, VehicleResolverObject::WO_NONE, false, callback, param1, param2);
1097  object.parent_scope.SetVehicle(parent);
1098  return object.ResolveCallback();
1099 }
1100 
1101 
1102 /* Callback 36 handlers */
1103 uint GetVehicleProperty(const Vehicle *v, PropertyID property, uint orig_value)
1104 {
1105  return GetEngineProperty(v->engine_type, property, orig_value, v);
1106 }
1107 
1108 
1109 uint GetEngineProperty(EngineID engine, PropertyID property, uint orig_value, const Vehicle *v)
1110 {
1111  uint16 callback = GetVehicleCallback(CBID_VEHICLE_MODIFY_PROPERTY, property, 0, engine, v);
1112  if (callback != CALLBACK_FAILED) return callback;
1113 
1114  return orig_value;
1115 }
1116 
1117 
1118 static void DoTriggerVehicle(Vehicle *v, VehicleTrigger trigger, byte base_random_bits, bool first)
1119 {
1120  /* We can't trigger a non-existent vehicle... */
1121  assert(v != nullptr);
1122 
1124  object.waiting_triggers = v->waiting_triggers | trigger;
1125  v->waiting_triggers = object.waiting_triggers; // store now for var 5F
1126 
1127  const SpriteGroup *group = object.Resolve();
1128  if (group == nullptr) return;
1129 
1130  /* Store remaining triggers. */
1131  v->waiting_triggers = object.GetRemainingTriggers();
1132 
1133  /* Rerandomise bits. Scopes other than SELF are invalid for rerandomisation. For bug-to-bug-compatibility with TTDP we ignore the scope. */
1134  byte new_random_bits = Random();
1135  uint32 reseed = object.GetReseedSum();
1136  v->random_bits &= ~reseed;
1137  v->random_bits |= (first ? new_random_bits : base_random_bits) & reseed;
1138 
1139  switch (trigger) {
1140  case VEHICLE_TRIGGER_NEW_CARGO:
1141  /* All vehicles in chain get ANY_NEW_CARGO trigger now.
1142  * So we call it for the first one and they will recurse.
1143  * Indexing part of vehicle random bits needs to be
1144  * same for all triggered vehicles in the chain (to get
1145  * all the random-cargo wagons carry the same cargo,
1146  * i.e.), so we give them all the NEW_CARGO triggered
1147  * vehicle's portion of random bits. */
1148  assert(first);
1149  DoTriggerVehicle(v->First(), VEHICLE_TRIGGER_ANY_NEW_CARGO, new_random_bits, false);
1150  break;
1151 
1152  case VEHICLE_TRIGGER_DEPOT:
1153  /* We now trigger the next vehicle in chain recursively.
1154  * The random bits portions may be different for each
1155  * vehicle in chain. */
1156  if (v->Next() != nullptr) DoTriggerVehicle(v->Next(), trigger, 0, true);
1157  break;
1158 
1159  case VEHICLE_TRIGGER_EMPTY:
1160  /* We now trigger the next vehicle in chain
1161  * recursively. The random bits portions must be same
1162  * for each vehicle in chain, so we give them all
1163  * first chained vehicle's portion of random bits. */
1164  if (v->Next() != nullptr) DoTriggerVehicle(v->Next(), trigger, first ? new_random_bits : base_random_bits, false);
1165  break;
1166 
1167  case VEHICLE_TRIGGER_ANY_NEW_CARGO:
1168  /* Now pass the trigger recursively to the next vehicle
1169  * in chain. */
1170  assert(!first);
1171  if (v->Next() != nullptr) DoTriggerVehicle(v->Next(), VEHICLE_TRIGGER_ANY_NEW_CARGO, base_random_bits, false);
1172  break;
1173 
1174  case VEHICLE_TRIGGER_CALLBACK_32:
1175  /* Do not do any recursion */
1176  break;
1177  }
1178 }
1179 
1180 void TriggerVehicle(Vehicle *v, VehicleTrigger trigger)
1181 {
1182  if (trigger == VEHICLE_TRIGGER_DEPOT) {
1183  /* store that the vehicle entered a depot this tick */
1185  }
1186 
1188  DoTriggerVehicle(v, trigger, 0, true);
1190 }
1191 
1192 /* Functions for changing the order of vehicle purchase lists */
1193 
1195  EngineID engine;
1196  uint target;
1197 };
1198 
1199 static std::vector<ListOrderChange> _list_order_changes;
1200 
1207 void AlterVehicleListOrder(EngineID engine, uint target)
1208 {
1209  /* Add the list order change to a queue */
1210  _list_order_changes.push_back({engine, target});
1211 }
1212 
1219 static bool EnginePreSort(const EngineID &a, const EngineID &b)
1220 {
1221  const EngineIDMapping &id_a = _engine_mngr.at(a);
1222  const EngineIDMapping &id_b = _engine_mngr.at(b);
1223 
1224  /* 1. Sort by engine type */
1225  if (id_a.type != id_b.type) return (int)id_a.type < (int)id_b.type;
1226 
1227  /* 2. Sort by scope-GRFID */
1228  if (id_a.grfid != id_b.grfid) return id_a.grfid < id_b.grfid;
1229 
1230  /* 3. Sort by local ID */
1231  return (int)id_a.internal_id < (int)id_b.internal_id;
1232 }
1233 
1238 {
1239  /* Pre-sort engines by scope-grfid and local index */
1240  std::vector<EngineID> ordering;
1241  Engine *e;
1242  FOR_ALL_ENGINES(e) {
1243  ordering.push_back(e->index);
1244  }
1245  std::sort(ordering.begin(), ordering.end(), EnginePreSort);
1246 
1247  /* Apply Insertion-Sort operations */
1248  for (const ListOrderChange &it : _list_order_changes) {
1249  EngineID source = it.engine;
1250  uint local_target = it.target;
1251 
1252  const EngineIDMapping *id_source = _engine_mngr.data() + source;
1253  if (id_source->internal_id == local_target) continue;
1254 
1255  EngineID target = _engine_mngr.GetID(id_source->type, local_target, id_source->grfid);
1256  if (target == INVALID_ENGINE) continue;
1257 
1258  int source_index = find_index(ordering, source);
1259  int target_index = find_index(ordering, target);
1260 
1261  assert(source_index >= 0 && target_index >= 0);
1262  assert(source_index != target_index);
1263 
1264  EngineID *list = ordering.data();
1265  if (source_index < target_index) {
1266  --target_index;
1267  for (int i = source_index; i < target_index; ++i) list[i] = list[i + 1];
1268  list[target_index] = source;
1269  } else {
1270  for (int i = source_index; i > target_index; --i) list[i] = list[i - 1];
1271  list[target_index] = source;
1272  }
1273  }
1274 
1275  /* Store final sort-order */
1276  uint index = 0;
1277  for (const EngineID &eid : ordering) {
1278  Engine::Get(eid)->list_position = index;
1279  ++index;
1280  }
1281 
1282  /* Clear out the queue */
1283  _list_order_changes.clear();
1284  _list_order_changes.shrink_to_fit();
1285 }
1286 
1292 {
1294 
1295  /* These variables we have to check; these are the ones with a cache. */
1296  static const int cache_entries[][2] = {
1297  { 0x40, NCVV_POSITION_CONSIST_LENGTH },
1298  { 0x41, NCVV_POSITION_SAME_ID_LENGTH },
1300  { 0x43, NCVV_COMPANY_INFORMATION },
1301  { 0x4D, NCVV_POSITION_IN_VEHICLE },
1302  };
1303  assert_compile(NCVV_END == lengthof(cache_entries));
1304 
1305  /* Resolve all the variables, so their caches are set. */
1306  for (size_t i = 0; i < lengthof(cache_entries); i++) {
1307  /* Only resolve when the cache isn't valid. */
1308  if (HasBit(v->grf_cache.cache_valid, cache_entries[i][1])) continue;
1309  bool stub;
1310  ro.GetScope(VSG_SCOPE_SELF)->GetVariable(cache_entries[i][0], 0, &stub);
1311  }
1312 
1313  /* Make sure really all bits are set. */
1314  assert(v->grf_cache.cache_valid == (1 << NCVV_END) - 1);
1315 }
StationID Source() const
Returns source of the first cargo packet in this list.
Definition: cargopacket.h:337
Road vehicle states.
static 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:334
TTDPAirportType ttd_airport_type
ttdpatch airport type (Small/Large/Helipad/Oilrig)
Interface to query and set values specific to a single VarSpriteGroupScope (action 2 scope)...
Date max_age
Maximum age.
Definition: vehicle_base.h:259
This bit will be set if the NewGRF var 41 currently stored is valid.
Definition: vehicle_base.h:59
Vehicle * Previous() const
Get the previous vehicle of this vehicle.
Definition: vehicle_base.h:588
Airplane wants to leave the airport.
Definition: airport.h:73
VehicleCargoList cargo
The cargo this vehicle is carrying.
Definition: vehicle_base.h:309
byte state
Definition: roadveh.h:111
Resolver for a vehicle scope.
Definition: newgrf_engine.h:24
Heading for hangar.
Definition: airport.h:64
Definition of stuff that is very close to a company, like the company struct itself.
uint32 motion_counter
counter to occasionally play a vehicle sound.
Definition: vehicle_base.h:296
Heading for terminal 1.
Definition: airport.h:65
Money value
Value of the vehicle.
Definition: vehicle_base.h:241
Airplane has reached end-point of the take-off runway.
Definition: airport.h:75
NewGRF handling of rail types.
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Finite sTate mAchine (FTA) of an airport.
Definition: airport.h:145
Heading for helipad 2.
Definition: airport.h:72
This bit will be set if the NewGRF var 40 currently stored is valid.
Definition: vehicle_base.h:58
Direction direction
facing
Definition: vehicle_base.h:271
const AirportSpec * GetSpec() const
Get the AirportSpec that from the airport type of this airport.
Definition: station_base.h:322
uint8 weight
Weight of a single unit of this cargo type in 1/16 ton (62.5 kg).
Definition: cargotype.h:62
bool UsesWagonOverride(const Vehicle *v)
Check if a wagon is currently using a wagon override.
StationID targetairport
Airport to go to next.
Definition: aircraft.h:80
void AlterVehicleListOrder(EngineID engine, uint target)
Record a vehicle ListOrderChange.
void UnloadWagonOverrides(Engine *e)
Unload all wagon override sprite groups.
Money GetDisplayProfitThisYear() const
Gets the profit vehicle had this year.
Definition: vehicle_base.h:566
uint target
local ID
uint32 grfid
The GRF ID of the file the entity belongs to.
Definition: engine_base.h:149
ResolverObject & ro
Surrounding resolver object.
uint32 GetTriggers() const override
Get the triggers.
#define DAYS_TILL_ORIGINAL_BASE_YEAR
The offset in days from the &#39;_date == 0&#39; till &#39;ConvertYMDToDate(ORIGINAL_BASE_YEAR, 0, 1)&#39;.
Definition: date_type.h:82
VarSpriteGroupScope
Train vehicle type.
Definition: vehicle_type.h:26
static const GRFFile * GetEngineGrfFile(EngineID engine_type)
Get the grf file associated with an engine type.
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:246
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition: engine.cpp:173
Functions related to dates.
uint8 GetReverseRoadTypeTranslation(RoadType roadtype, const GRFFile *grffile)
Perform a reverse roadtype lookup to get the GRF internal ID.
Conventional Take Off and Landing, i.e. planes.
Definition: engine_type.h:94
Heading for terminal 6.
Definition: airport.h:70
static const Year ORIGINAL_MAX_YEAR
The maximum year of the original TTD.
Definition: date_type.h:55
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:334
byte breakdown_delay
Counter for managing breakdown length.
Definition: vehicle_base.h:264
West.
Helicopter landing.
Definition: airport.h:57
Base for the train class.
uint16 GetVehicleCallbackParent(CallbackID callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v, const Vehicle *parent)
Evaluate a newgrf callback for vehicles with a different vehicle for parent scope.
Functions related to debugging.
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Called to spawn visual effects for vehicles.
byte pos
Next desired position of the aircraft.
Definition: aircraft.h:78
uint16 cur_speed
current speed
Definition: vehicle_base.h:293
Taxiing at the airport.
Definition: airport.h:55
Interface for SpriteGroup-s to access the gamestate.
const SpriteGroup * ResolveReal(const RealSpriteGroup *group) const override
Get the real sprites of the grf.
Ship vehicle type.
Definition: vehicle_type.h:28
GRFFilePropsBase< NUM_CARGO+2 > grf_prop
Properties related the the grf file.
Definition: engine_base.h:60
Maximal number of cargo types in a game.
Definition: cargo_type.h:66
static byte MapAircraftMovementState(const Aircraft *v)
Map OTTD aircraft movement states to TTDPatch style movement states (VarAction 2 Variable 0xE2) ...
byte spritenum
currently displayed sprite index 0xfd == custom sprite, 0xfe == custom second head sprite 0xff == res...
Definition: vehicle_base.h:279
Both directions faces to the same direction.
Specification of a cargo type.
Definition: cargotype.h:57
static const Livery * LiveryHelper(EngineID engine, const Vehicle *v)
Determines the livery of an engine.
uint16 wait_counter
Ticks waiting in front of a signal, ticks being stuck or a counter for forced proceeding through sign...
Definition: train.h:102
Functions related to vehicles.
Aircraft, helicopters, rotors and their shadows belong to this class.
Definition: aircraft.h:76
This bit will be set if the NewGRF var 43 currently stored is valid.
Definition: vehicle_base.h:61
Called when the company (or AI) tries to start or stop a vehicle.
const Livery * GetEngineLivery(EngineID engine_type, CompanyID company, EngineID parent_engine_type, const Vehicle *v, byte livery_setting)
Determines the livery for a vehicle.
Definition: vehicle.cpp:1902
Vehicle data structure.
Definition: vehicle_base.h:212
void Clear()
Clear all information.
Definition: vehicle_base.h:155
uint DaysInTransit() const
Returns average number of days in transit for a cargo entity.
Definition: cargopacket.h:270
Set when using the callback resolve system, but not to resolve a callback.
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
Vehicle is flying in the air.
Definition: airport.h:77
EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
Looks up an EngineID in the EngineOverrideManager.
Definition: engine.cpp:512
void CommitVehicleListOrderChanges()
Deternine default engine sorting and execute recorded ListOrderChanges from AlterVehicleListOrder.
T * First() const
Get the first vehicle in the chain.
Vehicle is unloading cargo.
Definition: vehicle_base.h:45
Base for aircraft.
PropertyID
List of NewGRF properties used in Action 0 or Callback 0x36 (CBID_VEHICLE_MODIFY_PROPERTY).
virtual ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, byte relative=0)
Get a resolver for the scope.
StationID last_station_visited
The last station we stopped at.
Definition: vehicle_base.h:302
uint16 reliability_spd_dec
Reliability decrease speed.
Definition: vehicle_base.h:262
uint32 position_consist_length
Cache for NewGRF var 40.
Definition: vehicle_base.h:69
byte user_def_data
Cached property 0x25. Can be set by Callback 0x36.
Definition: train.h:78
Determine whether a wagon can be attached to an already existing train.
Relative position (vehicles only)
byte num_loaded
Number of loaded groups.
uint32 cached_power
Total power of the consist (valid only for the first engine).
uint32 reseed[VSG_END]
Collects bits to rerandomise while triggering triggers.
static const VehicleID INVALID_VEHICLE
Constant representing a non-existing vehicle.
Definition: vehicle_type.h:57
RoadType
The different roadtypes we support.
Definition: road_type.h:27
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
uint16 MapOldOrder() const
Pack this order into a 16 bits integer as close to the TTD representation as possible.
Definition: order_cmd.cpp:209
uint16 classes
Classes of this cargo type.
Definition: cargotype.h:80
byte vehstatus
Status.
Definition: vehicle_base.h:317
EngineImageType
Visualisation contexts of vehicles and engines.
Definition: vehicle_type.h:87
byte flags
Flags of the engine.
Definition: engine_base.h:35
Year _cur_year
Current year, starting at 0.
Definition: date.cpp:26
Helicopter wants to land.
Definition: airport.h:80
uint StoredCount() const
Returns sum of cargo on board the vehicle (ie not only reserved).
Definition: cargopacket.h:366
uint32 GetRandomBits() const override
Get a few random bits.
byte overtaking
Set to RVSB_DRIVE_SIDE when overtaking, otherwise 0.
Definition: roadveh.h:114
static Train * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
uint16 cached_max_speed
Maximum speed of the consist (minimum of the max speed of all vehicles in the consist).
Definition: vehicle_base.h:123
const Engine * GetEngine() const
Retrieves the engine of the vehicle.
Definition: vehicle.cpp:745
CargoID GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition: engine_base.h:81
bool IsNormalAircraft() const
Check if the aircraft type is a normal flying device; eg not a rotor or a shadow. ...
Definition: aircraft.h:123
Go exactly to the destination coordinates.
Definition: airport.h:54
static uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
Definition: math_func.hpp:184
EngineID first_engine
Cached EngineID of the front vehicle. INVALID_ENGINE for the front vehicle itself.
Holding pattern movement (above the airport).
Definition: airport.h:58
Pseudo random number generator.
const SpriteGroup * root_spritegroup
Root SpriteGroup to use for resolving.
byte breakdown_ctr
Counter for managing breakdown events.
Definition: vehicle_base.h:263
Invalid cargo type.
Definition: cargo_type.h:70
Vehicle * Move(int n)
Get the vehicle at offset n of this vehicle chain.
Definition: vehicle_base.h:623
byte subtype
subtype (Filled with values from AircraftSubType/DisasterSubType/EffectVehicleType/GroundVehicleSubty...
Definition: vehicle_base.h:327
Buses, trucks and trams belong to this class.
Definition: roadveh.h:109
uint16 cargo_cap
total capacity
Definition: vehicle_base.h:307
Vehicle is a prototype (accepted as exclusive preview).
Definition: vehicle_base.h:46
Turn slowly (mostly used in the air).
Definition: airport.h:52
byte acceleration
used by train & aircraft
Definition: vehicle_base.h:295
static uint32 GetRegister(uint i)
Gets the value of a so-called newgrf "register".
Resolve no wagon overrides.
Definition: newgrf_engine.h:52
virtual const SpriteGroup * Resolve(ResolverObject &object) const
Base sprite group resolver.
Action 2 handling.
uint16 internal_id
The internal ID within the GRF file.
Definition: engine_base.h:150
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition: vehicle_base.h:433
UnitID unitnumber
unit number, for display purposes only
Definition: vehicle_base.h:291
const SpriteGroup ** loaded
List of loaded groups (can be SpriteIDs or Callback results)
byte cargo_subtype
Used for livery refits (NewGRF variations)
Definition: vehicle_base.h:306
byte subtype
Type of aircraft.
Definition: engine_type.h:103
static byte MapAircraftMovementAction(const Aircraft *v)
Map OTTD aircraft movement states to TTDPatch style movement actions (VarAction 2 Variable 0xE6) This...
Resolve wagon overrides.
Definition: newgrf_engine.h:53
Heading for terminal 7.
Definition: airport.h:82
GroundVehicleCache * GetGroundVehicleCache()
Access the ground vehicle cache of the vehicle.
Definition: vehicle.cpp:2833
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:63
Called to modify various vehicle properties.
Resolver for a vehicle (chain)
Definition: newgrf_engine.h:49
T * Next() const
Get next vehicle in the chain.
Heading for terminal 2.
Definition: airport.h:66
Draw vehicle by stacking multiple sprites.
Definition: engine_type.h:163
Definition of base types and functions in a cross-platform compatible way.
static const uint VEHICLE_LENGTH
The length of a vehicle in tile units.
Definition: vehicle_type.h:78
bool IsArticulatedPart() const
Check if the vehicle is an articulated part of an engine.
Definition: vehicle_base.h:892
A number of safeguards to prevent using unsafe methods.
void InvalidateNewGRFCacheOfChain()
Invalidates cached NewGRF variables of all vehicles in the chain (after the current vehicle) ...
Definition: vehicle_base.h:460
Direction
Defines the 8 directions on the map.
void VehicleEnteredDepotThisTick(Vehicle *v)
Adds a vehicle to the list of vehicles that visited a depot this tick.
Definition: vehicle.cpp:896
byte waiting_triggers
Triggers to be yet matched before rerandomizing the random bits.
Definition: vehicle_base.h:300
DirDiff
Enumeration for the difference between two directions.
Station * GetTargetAirportIfValid(const Aircraft *v)
Returns aircraft&#39;s target station if v->target_airport is a valid station with airport.
uint32 position_same_id_length
Cache for NewGRF var 41.
Definition: vehicle_base.h:70
void FillNewGRFVehicleCache(const Vehicle *v)
Fill the grf_cache of the given vehicle.
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:42
virtual int GetCurrentMaxSpeed() const
Calculates the maximum speed of the vehicle under its current conditions.
Definition: vehicle_base.h:492
CargoID cargo_type
type of cargo this vehicle is carrying
Definition: vehicle_base.h:305
Information about a particular livery.
Definition: livery.h:80
NewGRF handling of road types.
static T * ReallocT(T *t_ptr, size_t num_elements)
Simplified reallocation function that allocates the specified number of elements of the given type...
Definition: alloc_func.hpp:113
uint16 load_unload_ticks
Ticks to wait before starting next cycle.
Definition: vehicle_base.h:325
static const byte LIT_ALL
Show the liveries of all companies.
Definition: livery.h:19
byte misc_flags
Miscellaneous flags.
Definition: engine_type.h:144
TileIndex tile
Current tile index.
Definition: vehicle_base.h:230
VehicleResolverObject(EngineID engine_type, const Vehicle *v, WagonOverride wagon_override, bool info_view=false, CallbackID callback=CBID_NO_CALLBACK, uint32 callback_param1=0, uint32 callback_param2=0)
Resolver of a vehicle (chain).
Disaster vehicle type.
Definition: vehicle_type.h:34
This bit will be set if the NewGRF var 42 currently stored is valid.
Definition: vehicle_base.h:60
bool HasArticulatedPart() const
Check if an engine has an articulated part.
Definition: vehicle_base.h:901
const SpriteGroup ** loading
List of loading groups (can be SpriteIDs or Callback results)
static DirDiff DirDifference(Direction d0, Direction d1)
Calculate the difference between two directions.
Heading for helipad 3.
Definition: airport.h:84
Owner owner
Which company owns the vehicle?
Definition: vehicle_base.h:273
Sprite sequence for a vehicle part.
Definition: vehicle_base.h:130
static bool EnginePreSort(const EngineID &a, const EngineID &b)
Comparator function to sort engines via scope-GRFID and local ID.
Heading for terminal 3.
Definition: airport.h:67
uint8 cargo_map[NUM_CARGO]
Inverse cargo translation table (CargoID -> local ID)
Definition: newgrf.h:129
Called to determine if a specific colour map should be used for a vehicle instead of the default live...
Airplane has arrived at a runway for take-off.
Definition: airport.h:74
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
const AirportMovingData * MovingData(byte position) const
Get movement data at a position.
Definition: airport.h:172
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
Resolved object itself.
byte random_bits
Bits used for determining which randomized variational spritegroups to use when drawing.
Definition: vehicle_base.h:299
byte breakdowns_since_last_service
Counter for the amount of breakdowns.
Definition: vehicle_base.h:265
uint16 reliability
Reliability.
Definition: vehicle_base.h:261
static const Year ORIGINAL_BASE_YEAR
The minimum starting year/base year of the original TTD.
Definition: date_type.h:51
Vehicle * First() const
Get the first vehicle of this vehicle chain.
Definition: vehicle_base.h:594
byte tick_counter
Increased by one for each tick.
Definition: vehicle_base.h:314
uint16 crashed_ctr
Animation counter when the vehicle has crashed.
Definition: roadveh.h:116
const struct SpriteGroup * spritegroup[Tcnt]
pointer to the different sprites of the entity
All ships have this type.
Definition: ship.h:28
Year build_year
Year the vehicle has been built.
Definition: vehicle_base.h:257
byte state
State of the airport.
Definition: aircraft.h:81
virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const
Get a variable value.
byte turn_counter
Ticks between each turn to prevent > 45 degree turns.
Definition: aircraft.h:84
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:37
&#39;Train&#39; is either a loco or a wagon.
Definition: train.h:87
VehicleType type
The engine type.
Definition: engine_base.h:151
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
byte breakdown_chance
Current chance of breakdowns.
Definition: vehicle_base.h:266
This bit will be set if the NewGRF var 4D currently stored is valid.
Definition: vehicle_base.h:62
byte num_loading
Number of loading groups.
Wagon is powered.
Definition: train.h:29
Effect vehicle type (smoke, explosions, sparks, bubbles)
Definition: vehicle_type.h:33
static const EngineID INVALID_ENGINE
Constant denoting an invalid engine.
Definition: engine_type.h:176
const GRFFile * grffile
GRFFile the resolved SpriteGroup belongs to.
static int32 ClampToI32(const int64 a)
Reduce a signed 64-bit int to a signed 32-bit one.
Definition: math_func.hpp:203
Functions related to companies.
uint32 company_information
Cache for NewGRF var 43.
Definition: vehicle_base.h:72
Called for every vehicle every 32 days (not all on same date though).
RailType GetTileRailType(TileIndex tile)
Return the rail type of tile, or INVALID_RAILTYPE if this is no rail tile.
Definition: rail.cpp:157
TrackBits state
The "track" the ship is following.
Definition: ship.h:29
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
bool IsGroundVehicle() const
Check if the vehicle is a ground vehicle.
Definition: vehicle_base.h:471
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition: engine_base.h:140
DestinationID GetDestination() const
Gets the destination of this order.
Definition: order_base.h:96
byte subspeed
fractional speed
Definition: vehicle_base.h:294
Used for vehicle var 0xFE bit 8 (toggled each time the train is reversed, accurate for first vehicle ...
Definition: train.h:33
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:23
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:119
Cargo support for NewGRFs.
Vehicle * Next() const
Get the next vehicle of this vehicle.
Definition: vehicle_base.h:581
Date date_of_last_service
Last date the vehicle had a service at a depot.
Definition: vehicle_base.h:260
Airplane wants to land.
Definition: airport.h:78
uint32 GetVariable(byte variable, uint32 parameter, bool *available) const override
Get a variable value.
uint32 consist_cargo_information
Cache for NewGRF var 42. (Note: The cargotype is untranslated in the cache because the accessing GRF ...
Definition: vehicle_base.h:71
Related object of the resolved one.
Resolve self-override (helicopter rotors and such).
Definition: newgrf_engine.h:55
uint32 position_in_vehicle
Cache for NewGRF var 4D.
Definition: vehicle_base.h:73
void CDECL grfmsg(int severity, const char *str,...)
DEBUG() function dedicated to newGRF debugging messages Function is essentially the same as DEBUG(grf...
Definition: newgrf.cpp:377
CallbackID callback
Callback being resolved.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Heading for terminal 8.
Definition: airport.h:83
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:54
uint32 GetCompanyInfo(CompanyID owner, const Livery *l)
Returns company information like in vehicle var 43 or station var 43.
Reverse the visible direction of the vehicle.
Definition: train.h:30
End of the bits.
Definition: vehicle_base.h:63
uint8 GetReverseRailTypeTranslation(RailType railtype, const GRFFile *grffile)
Perform a reverse railtype lookup to get the GRF internal ID.
int32 z_pos
z coordinate.
Definition: vehicle_base.h:270
Vehicle is not visible.
Definition: vehicle_base.h:32
Same as AT_LARGE.
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:235
uint16 local_id
id defined by the grf file for this entity
Heading for terminal 5.
Definition: airport.h:69
uint32 GetGRFID() const
Retrieve the GRF ID of the NewGRF the vehicle is tied to.
Definition: vehicle.cpp:765
Base for ships.
Heading for terminal 4.
Definition: airport.h:68
ScopeResolver * GetScope(VarSpriteGroupScope scope=VSG_SCOPE_SELF, byte relative=0) override
Get a resolver for the scope.
VehicleOrderID GetNumOrders() const
Get the number of orders this vehicle has.
Definition: vehicle_base.h:687
uint16 GetVehicleCallback(CallbackID callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v)
Evaluate a newgrf callback for vehicles.
Aircraft vehicle type.
Definition: vehicle_type.h:29
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:131
uint8 cached_veh_length
Length of this vehicle in units of 1/VEHICLE_LENGTH of normal length. It is cached because this can b...
Airport airport
Tile area the airport covers.
Definition: station_base.h:466
static DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
Applies two differences together.
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:288
const struct GRFFile * grffile
grf file that introduced this entity
uint8 bitnum
Cargo bit number, is INVALID_CARGO for a non-used spec.
Definition: cargotype.h:58
CallbackID
List of implemented NewGRF callbacks.
Heading for helipad 1.
Definition: airport.h:71
int32 x_pos
x coordinate.
Definition: vehicle_base.h:268
Helicopter wants to finish landing.
Definition: airport.h:81
uint16 vehicle_flags
Used for gradual loading and other miscellaneous things (.
Definition: base_consist.h:32
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Helicopter take-off.
Definition: airport.h:56
Set when calling a randomizing trigger (almost undocumented).
uint8 cache_valid
Bitset that indicates which cache values are valid.
Definition: vehicle_base.h:74
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
uint16 flag
special flags when moving towards the destination.
Definition: airport.h:136
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
int32 y_pos
y coordinate.
Definition: vehicle_base.h:269
One direction is the opposite of the other one.
static uint32 PositionHelper(const Vehicle *v, bool consecutive)
Helper to get the position of a vehicle within a chain of vehicles.
SpriteID sprite
The &#39;real&#39; sprite.
Definition: gfx_type.h:25
Base classes/functions for stations.
VehicleCache vcache
Cache of often used vehicle values.
Definition: vehicle_base.h:330
Date _date
Current date in days (day counter)
Definition: date.cpp:28
Date age
Age in days.
Definition: vehicle_base.h:258
VehicleOrderID cur_real_order_index
The index to the current real (non-implicit) order.
Definition: base_consist.h:29
Airplane wants to finish landing.
Definition: airport.h:79
static uint16 ClampToU16(const uint64 a)
Reduce an unsigned 64-bit int to an unsigned 16-bit one.
Definition: math_func.hpp:215
Station data structure.
Definition: station_base.h:452
NewGRFCache grf_cache
Cache of often used calculated NewGRF values.
Definition: vehicle_base.h:329
Money GetDisplayProfitLastYear() const
Gets the profit vehicle had last year.
Definition: vehicle_base.h:572
Road vehicle type.
Definition: vehicle_type.h:27
int find_index(std::vector< T > const &vec, T const &item)
Helper function to get the index of an item Consider using std::set, std::unordered_set or std::flat_...
byte day_counter
Increased by one for each day.
Definition: vehicle_base.h:313
static RailType GetRailType(TileIndex t)
Gets the rail type of the given tile.
Definition: rail_map.h:117
byte delta_z
Z adjustment for helicopter pads.
Definition: airport.h:185
Order current_order
The current order (+ status, like: loading)
Definition: vehicle_base.h:318
bool IsEngine() const
Check if a vehicle is an engine (can be first in a consist).
GroundVehicleCache gcache
Cache of often calculated values.
byte overtaking_ctr
The length of the current overtake attempt.
Definition: roadveh.h:115
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:107
Helicopter wants to leave the airport.
Definition: airport.h:76
PaletteID pal
The palette (use PAL_NONE) if not needed)
Definition: gfx_type.h:26
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...
Resolve wagon overrides using TrainCache::cached_override.
Definition: newgrf_engine.h:54