OpenTTD
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 "company_func.h"
14 #include "command_func.h"
15 #include "news_func.h"
16 #include "aircraft.h"
17 #include "newgrf.h"
18 #include "newgrf_engine.h"
19 #include "strings_func.h"
20 #include "core/random_func.hpp"
21 #include "window_func.h"
22 #include "date_func.h"
23 #include "autoreplace_gui.h"
24 #include "string_func.h"
25 #include "ai/ai.hpp"
26 #include "core/pool_func.hpp"
27 #include "engine_gui.h"
28 #include "engine_func.h"
29 #include "engine_base.h"
30 #include "company_base.h"
31 #include "vehicle_func.h"
32 #include "articulated_vehicles.h"
33 #include "error.h"
34 
35 #include "table/strings.h"
36 #include "table/engines.h"
37 
38 #include "safeguards.h"
39 
40 EnginePool _engine_pool("Engine");
42 
43 EngineOverrideManager _engine_mngr;
44 
50 
52 const uint8 _engine_counts[4] = {
53  lengthof(_orig_rail_vehicle_info),
54  lengthof(_orig_road_vehicle_info),
55  lengthof(_orig_ship_vehicle_info),
56  lengthof(_orig_aircraft_vehicle_info),
57 };
58 
60 const uint8 _engine_offsets[4] = {
61  0,
62  lengthof(_orig_rail_vehicle_info),
63  lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info),
64  lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info),
65 };
66 
67 assert_compile(lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info) + lengthof(_orig_aircraft_vehicle_info) == lengthof(_orig_engine_info));
68 
70 
71 Engine::Engine() :
72  name(nullptr),
73  overrides_count(0),
74  overrides(nullptr)
75 {
76 }
77 
78 Engine::Engine(VehicleType type, EngineID base)
79 {
80  this->type = type;
81  this->grf_prop.local_id = base;
82  this->list_position = base;
84 
85  /* Check if this base engine is within the original engine data range */
86  if (base >= _engine_counts[type]) {
87  /* Set model life to maximum to make wagons available */
88  this->info.base_life = 0xFF;
89  /* Set road vehicle tractive effort to the default value */
90  if (type == VEH_ROAD) this->u.road.tractive_effort = 0x4C;
91  /* Aircraft must have CT_INVALID as default, as there is no property */
92  if (type == VEH_AIRCRAFT) this->info.cargo_type = CT_INVALID;
93  /* Set visual effect to the default value */
94  switch (type) {
95  case VEH_TRAIN: this->u.rail.visual_effect = VE_DEFAULT; break;
96  case VEH_ROAD: this->u.road.visual_effect = VE_DEFAULT; break;
97  case VEH_SHIP: this->u.ship.visual_effect = VE_DEFAULT; break;
98  default: break; // The aircraft, disasters and especially visual effects have no NewGRF configured visual effects
99  }
100  /* Set cargo aging period to the default value. */
101  this->info.cargo_age_period = CARGO_AGING_TICKS;
102  return;
103  }
104 
105  /* Copy the original engine info for this slot */
106  this->info = _orig_engine_info[_engine_offsets[type] + base];
107 
108  /* Copy the original engine data for this slot */
109  switch (type) {
110  default: NOT_REACHED();
111 
112  case VEH_TRAIN:
113  this->u.rail = _orig_rail_vehicle_info[base];
114  this->original_image_index = this->u.rail.image_index;
115  this->info.string_id = STR_VEHICLE_NAME_TRAIN_ENGINE_RAIL_KIRBY_PAUL_TANK_STEAM + base;
116 
117  /* Set the default model life of original wagons to "infinite" */
118  if (this->u.rail.railveh_type == RAILVEH_WAGON) this->info.base_life = 0xFF;
119 
120  break;
121 
122  case VEH_ROAD:
123  this->u.road = _orig_road_vehicle_info[base];
124  this->original_image_index = this->u.road.image_index;
125  this->info.string_id = STR_VEHICLE_NAME_ROAD_VEHICLE_MPS_REGAL_BUS + base;
126  break;
127 
128  case VEH_SHIP:
129  this->u.ship = _orig_ship_vehicle_info[base];
130  this->original_image_index = this->u.ship.image_index;
131  this->info.string_id = STR_VEHICLE_NAME_SHIP_MPS_OIL_TANKER + base;
132  break;
133 
134  case VEH_AIRCRAFT:
135  this->u.air = _orig_aircraft_vehicle_info[base];
136  this->original_image_index = this->u.air.image_index;
137  this->info.string_id = STR_VEHICLE_NAME_AIRCRAFT_SAMPSON_U52 + base;
138  break;
139  }
140 }
141 
142 Engine::~Engine()
143 {
144  UnloadWagonOverrides(this);
145  free(this->name);
146 }
147 
152 bool Engine::IsEnabled() const
153 {
154  return this->info.string_id != STR_NEWGRF_INVALID_ENGINE && HasBit(this->info.climates, _settings_game.game_creation.landscape);
155 }
156 
162 uint32 Engine::GetGRFID() const
163 {
164  const GRFFile *file = this->GetGRF();
165  return file == nullptr ? 0 : file->grfid;
166 }
167 
174 {
175  /* For engines that can appear in a consist (i.e. rail vehicles and (articulated) road vehicles), a capacity
176  * of zero is a special case, to define the vehicle to not carry anything. The default cargotype is still used
177  * for livery selection etc.
178  * Note: Only the property is tested. A capacity callback returning 0 does not have the same effect.
179  */
180  switch (this->type) {
181  case VEH_TRAIN:
182  if (this->u.rail.capacity == 0) return false;
183  break;
184 
185  case VEH_ROAD:
186  if (this->u.road.capacity == 0) return false;
187  break;
188 
189  case VEH_SHIP:
190  case VEH_AIRCRAFT:
191  break;
192 
193  default: NOT_REACHED();
194  }
195  return this->GetDefaultCargoType() != CT_INVALID;
196 }
197 
198 
206 uint Engine::DetermineCapacity(const Vehicle *v, uint16 *mail_capacity) const
207 {
208  assert(v == nullptr || this->index == v->engine_type);
209  if (mail_capacity != nullptr) *mail_capacity = 0;
210 
211  if (!this->CanCarryCargo()) return 0;
212 
213  bool new_multipliers = HasBit(this->info.misc_flags, EF_NO_DEFAULT_CARGO_MULTIPLIER);
214  CargoID default_cargo = this->GetDefaultCargoType();
215  CargoID cargo_type = (v != nullptr) ? v->cargo_type : default_cargo;
216 
217  if (mail_capacity != nullptr && this->type == VEH_AIRCRAFT && IsCargoInClass(cargo_type, CC_PASSENGERS)) {
218  *mail_capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity, v);
219  }
220 
221  /* Check the refit capacity callback if we are not in the default configuration, or if we are using the new multiplier algorithm. */
223  (new_multipliers || default_cargo != cargo_type || (v != nullptr && v->cargo_subtype != 0))) {
224  uint16 callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, this->index, v);
225  if (callback != CALLBACK_FAILED) return callback;
226  }
227 
228  /* Get capacity according to property resp. CB */
229  uint capacity;
230  uint extra_mail_cap = 0;
231  switch (this->type) {
232  case VEH_TRAIN:
233  capacity = GetEngineProperty(this->index, PROP_TRAIN_CARGO_CAPACITY, this->u.rail.capacity, v);
234 
235  /* In purchase list add the capacity of the second head. Always use the plain property for this. */
236  if (v == nullptr && this->u.rail.railveh_type == RAILVEH_MULTIHEAD) capacity += this->u.rail.capacity;
237  break;
238 
239  case VEH_ROAD:
240  capacity = GetEngineProperty(this->index, PROP_ROADVEH_CARGO_CAPACITY, this->u.road.capacity, v);
241  break;
242 
243  case VEH_SHIP:
244  capacity = GetEngineProperty(this->index, PROP_SHIP_CARGO_CAPACITY, this->u.ship.capacity, v);
245  break;
246 
247  case VEH_AIRCRAFT:
248  capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_PASSENGER_CAPACITY, this->u.air.passenger_capacity, v);
249  if (!IsCargoInClass(cargo_type, CC_PASSENGERS)) {
250  extra_mail_cap = GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity, v);
251  }
252  if (!new_multipliers && cargo_type == CT_MAIL) return capacity + extra_mail_cap;
253  default_cargo = CT_PASSENGERS; // Always use 'passengers' wrt. cargo multipliers
254  break;
255 
256  default: NOT_REACHED();
257  }
258 
259  if (!new_multipliers) {
260  /* Use the passenger multiplier for mail as well */
261  capacity += extra_mail_cap;
262  extra_mail_cap = 0;
263  }
264 
265  /* Apply multipliers depending on cargo- and vehicletype. */
266  if (new_multipliers || (this->type != VEH_SHIP && default_cargo != cargo_type)) {
267  uint16 default_multiplier = new_multipliers ? 0x100 : CargoSpec::Get(default_cargo)->multiplier;
268  uint16 cargo_multiplier = CargoSpec::Get(cargo_type)->multiplier;
269  capacity *= cargo_multiplier;
270  if (extra_mail_cap > 0) {
271  uint mail_multiplier = CargoSpec::Get(CT_MAIL)->multiplier;
272  capacity += (default_multiplier * extra_mail_cap * cargo_multiplier + mail_multiplier / 2) / mail_multiplier;
273  }
274  capacity = (capacity + default_multiplier / 2) / default_multiplier;
275  }
276 
277  return capacity;
278 }
279 
285 {
286  Price base_price;
287  uint cost_factor;
288  switch (this->type) {
289  case VEH_ROAD:
290  base_price = this->u.road.running_cost_class;
291  if (base_price == INVALID_PRICE) return 0;
292  cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_RUNNING_COST_FACTOR, this->u.road.running_cost);
293  break;
294 
295  case VEH_TRAIN:
296  base_price = this->u.rail.running_cost_class;
297  if (base_price == INVALID_PRICE) return 0;
298  cost_factor = GetEngineProperty(this->index, PROP_TRAIN_RUNNING_COST_FACTOR, this->u.rail.running_cost);
299  break;
300 
301  case VEH_SHIP:
302  base_price = PR_RUNNING_SHIP;
303  cost_factor = GetEngineProperty(this->index, PROP_SHIP_RUNNING_COST_FACTOR, this->u.ship.running_cost);
304  break;
305 
306  case VEH_AIRCRAFT:
307  base_price = PR_RUNNING_AIRCRAFT;
308  cost_factor = GetEngineProperty(this->index, PROP_AIRCRAFT_RUNNING_COST_FACTOR, this->u.air.running_cost);
309  break;
310 
311  default: NOT_REACHED();
312  }
313 
314  return GetPrice(base_price, cost_factor, this->GetGRF(), -8);
315 }
316 
322 {
323  Price base_price;
324  uint cost_factor;
325  switch (this->type) {
326  case VEH_ROAD:
327  base_price = PR_BUILD_VEHICLE_ROAD;
328  cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_COST_FACTOR, this->u.road.cost_factor);
329  break;
330 
331  case VEH_TRAIN:
332  if (this->u.rail.railveh_type == RAILVEH_WAGON) {
333  base_price = PR_BUILD_VEHICLE_WAGON;
334  cost_factor = GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor);
335  } else {
336  base_price = PR_BUILD_VEHICLE_TRAIN;
337  cost_factor = GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor);
338  }
339  break;
340 
341  case VEH_SHIP:
342  base_price = PR_BUILD_VEHICLE_SHIP;
343  cost_factor = GetEngineProperty(this->index, PROP_SHIP_COST_FACTOR, this->u.ship.cost_factor);
344  break;
345 
346  case VEH_AIRCRAFT:
347  base_price = PR_BUILD_VEHICLE_AIRCRAFT;
348  cost_factor = GetEngineProperty(this->index, PROP_AIRCRAFT_COST_FACTOR, this->u.air.cost_factor);
349  break;
350 
351  default: NOT_REACHED();
352  }
353 
354  return GetPrice(base_price, cost_factor, this->GetGRF(), -8);
355 }
356 
362 {
363  switch (this->type) {
364  case VEH_TRAIN:
365  return GetEngineProperty(this->index, PROP_TRAIN_SPEED, this->u.rail.max_speed);
366 
367  case VEH_ROAD: {
368  uint max_speed = GetEngineProperty(this->index, PROP_ROADVEH_SPEED, 0);
369  return (max_speed != 0) ? max_speed * 2 : this->u.road.max_speed / 2;
370  }
371 
372  case VEH_SHIP:
373  return GetEngineProperty(this->index, PROP_SHIP_SPEED, this->u.ship.max_speed) / 2;
374 
375  case VEH_AIRCRAFT: {
376  uint max_speed = GetEngineProperty(this->index, PROP_AIRCRAFT_SPEED, 0);
377  if (max_speed != 0) {
378  return (max_speed * 128) / 10;
379  }
380  return this->u.air.max_speed;
381  }
382 
383  default: NOT_REACHED();
384  }
385 }
386 
393 uint Engine::GetPower() const
394 {
395  /* Only trains and road vehicles have 'power'. */
396  switch (this->type) {
397  case VEH_TRAIN:
398  return GetEngineProperty(this->index, PROP_TRAIN_POWER, this->u.rail.power);
399  case VEH_ROAD:
400  return GetEngineProperty(this->index, PROP_ROADVEH_POWER, this->u.road.power) * 10;
401 
402  default: NOT_REACHED();
403  }
404 }
405 
412 {
413  /* Only trains and road vehicles have 'weight'. */
414  switch (this->type) {
415  case VEH_TRAIN:
416  return GetEngineProperty(this->index, PROP_TRAIN_WEIGHT, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0);
417  case VEH_ROAD:
418  return GetEngineProperty(this->index, PROP_ROADVEH_WEIGHT, this->u.road.weight) / 4;
419 
420  default: NOT_REACHED();
421  }
422 }
423 
430 {
431  /* Only trains and road vehicles have 'tractive effort'. */
432  switch (this->type) {
433  case VEH_TRAIN:
434  return (GROUND_ACCELERATION * this->GetDisplayWeight() * GetEngineProperty(this->index, PROP_TRAIN_TRACTIVE_EFFORT, this->u.rail.tractive_effort)) / 256 / 1000;
435  case VEH_ROAD:
436  return (GROUND_ACCELERATION * this->GetDisplayWeight() * GetEngineProperty(this->index, PROP_ROADVEH_TRACTIVE_EFFORT, this->u.road.tractive_effort)) / 256 / 1000;
437 
438  default: NOT_REACHED();
439  }
440 }
441 
447 {
448  /* Assume leap years; this gives the player a bit more than the given amount of years, but never less. */
450 }
451 
456 uint16 Engine::GetRange() const
457 {
458  switch (this->type) {
459  case VEH_AIRCRAFT:
460  return GetEngineProperty(this->index, PROP_AIRCRAFT_RANGE, this->u.air.max_range);
461 
462  default: NOT_REACHED();
463  }
464 }
465 
471 {
472  switch (this->type) {
473  case VEH_AIRCRAFT:
474  switch (this->u.air.subtype) {
475  case AIR_HELI: return STR_LIVERY_HELICOPTER;
476  case AIR_CTOL: return STR_LIVERY_SMALL_PLANE;
477  case AIR_CTOL | AIR_FAST: return STR_LIVERY_LARGE_PLANE;
478  default: NOT_REACHED();
479  }
480 
481  default: NOT_REACHED();
482  }
483 }
484 
489 {
490  this->clear();
492  for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++) {
493  /*C++17: EngineIDMapping &eid = */ this->emplace_back();
494  EngineIDMapping &eid = this->back();
495  eid.type = type;
496  eid.grfid = INVALID_GRFID;
497  eid.internal_id = internal_id;
498  eid.substitute_id = internal_id;
499  }
500  }
501 }
502 
512 EngineID EngineOverrideManager::GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
513 {
514  EngineID index = 0;
515  for (const EngineIDMapping &eid : *this) {
516  if (eid.type == type && eid.grfid == grfid && eid.internal_id == grf_local_id) {
517  return index;
518  }
519  index++;
520  }
521  return INVALID_ENGINE;
522 }
523 
530 {
531  const Vehicle *v;
532  FOR_ALL_VEHICLES(v) {
533  if (IsCompanyBuildableVehicleType(v)) return false;
534  }
535 
536  /* Reset the engines, they will get new EngineIDs */
537  _engine_mngr.ResetToDefaultMapping();
539 
540  return true;
541 }
542 
547 {
549  _engine_pool.CleanPool();
550 
551  assert(_engine_mngr.size() >= _engine_mngr.NUM_DEFAULT_ENGINES);
552  uint index = 0;
553  for (const EngineIDMapping &eid : _engine_mngr) {
554  /* Assert is safe; there won't be more than 256 original vehicles
555  * in any case, and we just cleaned the pool. */
556  assert(Engine::CanAllocateItem());
557  const Engine *e = new Engine(eid.type, eid.internal_id);
558  assert(e->index == index);
559  index++;
560  }
561 }
562 
563 void ShowEnginePreviewWindow(EngineID engine);
564 
570 static bool IsWagon(EngineID index)
571 {
572  const Engine *e = Engine::Get(index);
573  return e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON;
574 }
575 
581 {
582  uint age = e->age;
583 
584  /* Check for early retirement */
585  if (e->company_avail != 0 && !_settings_game.vehicle.never_expire_vehicles && e->info.base_life != 0xFF) {
586  int retire_early = e->info.retire_early;
587  uint retire_early_max_age = max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
588  if (retire_early != 0 && age >= retire_early_max_age) {
589  /* Early retirement is enabled and we're past the date... */
590  e->company_avail = 0;
592  }
593  }
594 
595  if (age < e->duration_phase_1) {
596  uint start = e->reliability_start;
597  e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
598  } else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _settings_game.vehicle.never_expire_vehicles || e->info.base_life == 0xFF) {
599  /* We are at the peak of this engines life. It will have max reliability.
600  * This is also true if the engines never expire. They will not go bad over time */
602  } else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
603  uint max = e->reliability_max;
604  e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
605  } else {
606  /* time's up for this engine.
607  * We will now completely retire this design */
608  e->company_avail = 0;
610  /* Kick this engine out of the lists */
612  }
613  SetWindowClassesDirty(WC_BUILD_VEHICLE); // Update to show the new reliability
615 }
616 
619 {
620  /* Determine last engine aging year, default to 2050 as previously. */
622 
623  const Engine *e;
624  FOR_ALL_ENGINES(e) {
625  const EngineInfo *ei = &e->info;
626 
627  /* Exclude certain engines */
629  if (e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON) continue;
630 
631  /* Base year ending date on half the model life */
632  YearMonthDay ymd;
633  ConvertDateToYMD(ei->base_intro + (ei->lifelength * DAYS_IN_LEAP_YEAR) / 2, &ymd);
634 
636  }
637 }
638 
644 void StartupOneEngine(Engine *e, Date aging_date)
645 {
646  const EngineInfo *ei = &e->info;
647 
648  e->age = 0;
649  e->flags = 0;
650  e->company_avail = 0;
651  e->company_hidden = 0;
652 
653  /* Don't randomise the start-date in the first two years after gamestart to ensure availability
654  * of engines in early starting games.
655  * Note: TTDP uses fixed 1922 */
656  SavedRandomSeeds saved_seeds;
657  SaveRandomSeeds(&saved_seeds);
659  ei->base_intro ^
660  e->type ^
661  e->GetGRFID());
662  uint32 r = Random();
663 
665  if (e->intro_date <= _date) {
666  e->age = (aging_date - e->intro_date) >> 5;
667  e->company_avail = (CompanyMask)-1;
668  e->flags |= ENGINE_AVAILABLE;
669  }
670 
671  e->reliability_start = GB(r, 16, 14) + 0x7AE0;
672  r = Random();
673  e->reliability_max = GB(r, 0, 14) + 0xBFFF;
674  e->reliability_final = GB(r, 16, 14) + 0x3FFF;
675 
676  r = Random();
677  e->duration_phase_1 = GB(r, 0, 5) + 7;
678  e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
679  e->duration_phase_3 = GB(r, 9, 7) + 120;
680 
681  e->reliability_spd_dec = ei->decay_speed << 2;
682 
683  RestoreRandomSeeds(saved_seeds);
685 
686  /* prevent certain engines from ever appearing. */
688  e->flags |= ENGINE_AVAILABLE;
689  e->company_avail = 0;
690  }
691 }
692 
698 {
699  Engine *e;
700  /* Aging of vehicles stops, so account for that when starting late */
701  const Date aging_date = min(_date, ConvertYMDToDate(_year_engine_aging_stops, 0, 1));
702 
703  FOR_ALL_ENGINES(e) {
704  StartupOneEngine(e, aging_date);
705  }
706 
707  /* Update the bitmasks for the vehicle lists */
708  Company *c;
709  FOR_ALL_COMPANIES(c) {
712  }
713 
714  /* Invalidate any open purchase lists */
716 }
717 
723 static void AcceptEnginePreview(EngineID eid, CompanyID company)
724 {
725  Engine *e = Engine::Get(eid);
726  Company *c = Company::Get(company);
727 
728  SetBit(e->company_avail, company);
729  if (e->type == VEH_TRAIN) {
730  assert(e->u.rail.railtype < RAILTYPE_END);
732  } else if (e->type == VEH_ROAD) {
733  assert(e->u.road.roadtype < ROADTYPE_END);
735  }
736 
738  e->preview_asked = (CompanyMask)-1;
739  if (company == _local_company) {
741  }
742 
743  /* Update the toolbar. */
746 
747  /* Notify preview window, that it might want to close.
748  * Note: We cannot directly close the window.
749  * In singleplayer this function is called from the preview window, so
750  * we have to use the GUI-scope scheduling of InvalidateWindowData.
751  */
753 }
754 
761 {
762  CompanyID best_company = INVALID_COMPANY;
763 
764  /* For trains the cargomask has no useful meaning, since you can attach other wagons */
765  CargoTypes cargomask = e->type != VEH_TRAIN ? GetUnionOfArticulatedRefitMasks(e->index, true) : ALL_CARGOTYPES;
766 
767  int32 best_hist = -1;
768  const Company *c;
769  FOR_ALL_COMPANIES(c) {
770  if (c->block_preview == 0 && !HasBit(e->preview_asked, c->index) &&
771  c->old_economy[0].performance_history > best_hist) {
772 
773  /* Check whether the company uses similar vehicles */
774  Vehicle *v;
775  FOR_ALL_VEHICLES(v) {
776  if (v->owner != c->index || v->type != e->type) continue;
777  if (!v->GetEngine()->CanCarryCargo() || !HasBit(cargomask, v->cargo_type)) continue;
778 
779  best_hist = c->old_economy[0].performance_history;
780  best_company = c->index;
781  break;
782  }
783  }
784  }
785 
786  return best_company;
787 }
788 
797 {
798  switch (type) {
803 
804  default: NOT_REACHED();
805  }
806 }
807 
810 {
811  Company *c;
812  FOR_ALL_COMPANIES(c) {
815  }
816 
817  if (_cur_year >= _year_engine_aging_stops) return;
818 
819  Engine *e;
820  FOR_ALL_ENGINES(e) {
821  EngineID i = e->index;
822  if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
823  if (e->preview_company != INVALID_COMPANY) {
824  if (!--e->preview_wait) {
827  }
828  } else if (CountBits(e->preview_asked) < MAX_COMPANIES) {
830 
831  if (e->preview_company == INVALID_COMPANY) {
832  e->preview_asked = (CompanyMask)-1;
833  continue;
834  }
835 
837  e->preview_wait = 20;
838  /* AIs are intentionally not skipped for preview even if they cannot build a certain
839  * vehicle type. This is done to not give poor performing human companies an "unfair"
840  * boost that they wouldn't have gotten against other human companies. The check on
841  * the line below is just to make AIs not notice that they have a preview if they
842  * cannot build the vehicle. */
843  if (!IsVehicleTypeDisabled(e->type, true)) AI::NewEvent(e->preview_company, new ScriptEventEnginePreview(i));
844  if (IsInteractiveCompany(e->preview_company)) ShowEnginePreviewWindow(i);
845  }
846  }
847  }
848 }
849 
855 {
856  Engine *e;
857  FOR_ALL_ENGINES(e) {
858  SB(e->company_hidden, cid, 1, 0);
859  }
860 }
861 
871 CommandCost CmdSetVehicleVisibility(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
872 {
873  Engine *e = Engine::GetIfValid(GB(p2, 0, 31));
874  if (e == nullptr || _current_company >= MAX_COMPANIES) return CMD_ERROR;
875  if (!IsEngineBuildable(e->index, e->type, _current_company)) return CMD_ERROR;
876 
877  if ((flags & DC_EXEC) != 0) {
878  SB(e->company_hidden, _current_company, 1, GB(p2, 31, 1));
880  }
881 
882  return CommandCost();
883 }
884 
895 CommandCost CmdWantEnginePreview(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
896 {
897  Engine *e = Engine::GetIfValid(p1);
898  if (e == nullptr || !(e->flags & ENGINE_EXCLUSIVE_PREVIEW) || e->preview_company != _current_company) return CMD_ERROR;
899 
900  if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_company);
901 
902  return CommandCost();
903 }
904 
911 {
912  Vehicle *v;
913  Company *c;
914  EngineID index = e->index;
915 
916  /* In case the company didn't build the vehicle during the intro period,
917  * prevent that company from getting future intro periods for a while. */
918  if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
919  FOR_ALL_COMPANIES(c) {
920  uint block_preview = c->block_preview;
921 
922  if (!HasBit(e->company_avail, c->index)) continue;
923 
924  /* We assume the user did NOT build it.. prove me wrong ;) */
925  c->block_preview = 20;
926 
927  FOR_ALL_VEHICLES(v) {
928  if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
930  if (v->owner == c->index && v->engine_type == index) {
931  /* The user did prove me wrong, so restore old value */
932  c->block_preview = block_preview;
933  break;
934  }
935  }
936  }
937  }
938  }
939 
942 
943  /* Now available for all companies */
944  e->company_avail = (CompanyMask)-1;
945 
946  /* Do not introduce new rail wagons */
947  if (IsWagon(index)) return;
948 
949  if (e->type == VEH_TRAIN) {
950  /* maybe make another rail type available */
951  RailType railtype = e->u.rail.railtype;
952  assert(railtype < RAILTYPE_END);
953  FOR_ALL_COMPANIES(c) c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes | GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes, _date);
954  } else if (e->type == VEH_ROAD) {
955  /* maybe make another road type available */
956  assert(e->u.road.roadtype < ROADTYPE_END);
958  }
959 
960  /* Only broadcast event if AIs are able to build this vehicle type. */
961  if (!IsVehicleTypeDisabled(e->type, true)) AI::BroadcastNewEvent(new ScriptEventEngineAvailable(index));
962 
963  /* Only provide the "New Vehicle available" news paper entry, if engine can be built. */
964  if (!IsVehicleTypeDisabled(e->type, false)) {
965  SetDParam(0, GetEngineCategoryName(index));
966  SetDParam(1, index);
967  AddNewsItem(STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NT_NEW_VEHICLES, NF_VEHICLE, NR_ENGINE, index);
968  }
969 
970  /* Update the toolbar. */
973 
974  /* Close pending preview windows */
976 }
977 
980 {
982  Engine *e;
983  FOR_ALL_ENGINES(e) {
984  /* Age the vehicle */
985  if ((e->flags & ENGINE_AVAILABLE) && e->age != MAX_DAY) {
986  e->age++;
988  }
989 
990  /* Do not introduce invalid engines */
991  if (!e->IsEnabled()) continue;
992 
993  if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + DAYS_IN_YEAR)) {
994  /* Introduce it to all companies */
996  } else if (!(e->flags & (ENGINE_AVAILABLE | ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
997  /* Introduction date has passed...
998  * Check if it is allowed to build this vehicle type at all
999  * based on the current game settings. If not, it does not
1000  * make sense to show the preview dialog to any company. */
1001  if (IsVehicleTypeDisabled(e->type, false)) continue;
1002 
1003  /* Do not introduce new rail wagons */
1004  if (IsWagon(e->index)) continue;
1005 
1006  /* Show preview dialog to one of the companies. */
1009  e->preview_asked = 0;
1010  }
1011  }
1012 
1013  InvalidateWindowClassesData(WC_BUILD_VEHICLE); // rebuild the purchase list (esp. when sorted by reliability)
1014  }
1015 }
1016 
1022 static bool IsUniqueEngineName(const char *name)
1023 {
1024  const Engine *e;
1025 
1026  FOR_ALL_ENGINES(e) {
1027  if (e->name != nullptr && strcmp(e->name, name) == 0) return false;
1028  }
1029 
1030  return true;
1031 }
1032 
1042 CommandCost CmdRenameEngine(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
1043 {
1044  Engine *e = Engine::GetIfValid(p1);
1045  if (e == nullptr) return CMD_ERROR;
1046 
1047  bool reset = StrEmpty(text);
1048 
1049  if (!reset) {
1051  if (!IsUniqueEngineName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
1052  }
1053 
1054  if (flags & DC_EXEC) {
1055  free(e->name);
1056 
1057  if (reset) {
1058  e->name = nullptr;
1059  } else {
1060  e->name = stredup(text);
1061  }
1062 
1064  }
1065 
1066  return CommandCost();
1067 }
1068 
1069 
1079 {
1080  const Engine *e = Engine::GetIfValid(engine);
1081 
1082  /* check if it's an engine that is in the engine array */
1083  if (e == nullptr) return false;
1084 
1085  /* check if it's an engine of specified type */
1086  if (e->type != type) return false;
1087 
1088  /* check if it's available ... */
1089  if (company == OWNER_DEITY) {
1090  /* ... for any company (preview does not count) */
1091  if (!(e->flags & ENGINE_AVAILABLE) || e->company_avail == 0) return false;
1092  } else {
1093  /* ... for this company */
1094  if (!HasBit(e->company_avail, company)) return false;
1095  }
1096 
1097  if (!e->IsEnabled()) return false;
1098 
1099  if (type == VEH_TRAIN && company != OWNER_DEITY) {
1100  /* Check if the rail type is available to this company */
1101  const Company *c = Company::Get(company);
1102  if (((GetRailTypeInfo(e->u.rail.railtype))->compatible_railtypes & c->avail_railtypes) == 0) return false;
1103  }
1104  if (type == VEH_ROAD && company != OWNER_DEITY) {
1105  /* Check if the road type is available to this company */
1106  const Company *c = Company::Get(company);
1107  if ((GetRoadTypeInfo(e->u.road.roadtype)->powered_roadtypes & c->avail_roadtypes) == ROADTYPES_NONE) return false;
1108  }
1109 
1110  return true;
1111 }
1112 
1120 {
1121  const Engine *e = Engine::GetIfValid(engine);
1122 
1123  /* check if it's an engine that is in the engine array */
1124  if (e == nullptr) return false;
1125 
1126  if (!e->CanCarryCargo()) return false;
1127 
1128  const EngineInfo *ei = &e->info;
1129  if (ei->refit_mask == 0) return false;
1130 
1131  /* Are there suffixes?
1132  * Note: This does not mean the suffixes are actually available for every consist at any time. */
1133  if (HasBit(ei->callback_mask, CBM_VEHICLE_CARGO_SUFFIX)) return true;
1134 
1135  /* Is there any cargo except the default cargo? */
1136  CargoID default_cargo = e->GetDefaultCargoType();
1137  CargoTypes default_cargo_mask = 0;
1138  SetBit(default_cargo_mask, default_cargo);
1139  return default_cargo != CT_INVALID && ei->refit_mask != default_cargo_mask;
1140 }
1141 
1146 {
1147  const Engine *e;
1148  Date min_date = INT32_MAX;
1149 
1150  FOR_ALL_ENGINES(e) {
1151  if (!e->IsEnabled()) continue;
1152 
1153  /* We have an available engine... yay! */
1154  if ((e->flags & ENGINE_AVAILABLE) != 0 && e->company_avail != 0) return;
1155 
1156  /* Okay, try to find the earliest date. */
1157  min_date = min(min_date, e->info.base_intro);
1158  }
1159 
1160  if (min_date < INT32_MAX) {
1161  SetDParam(0, min_date);
1162  ShowErrorMessage(STR_ERROR_NO_VEHICLES_AVAILABLE_YET, STR_ERROR_NO_VEHICLES_AVAILABLE_YET_EXPLANATION, WL_WARNING);
1163  } else {
1164  ShowErrorMessage(STR_ERROR_NO_VEHICLES_AVAILABLE_AT_ALL, STR_ERROR_NO_VEHICLES_AVAILABLE_AT_ALL_EXPLANATION, WL_WARNING);
1165  }
1166 }
AISettings ai
what may the AI do?
bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
Check if an engine is buildable.
Definition: engine.cpp:1078
Functions related to OTTD&#39;s strings.
Owner
Enum for all companies/owners.
Definition: company_type.h:20
VehicleSettings vehicle
options for vehicles
This vehicle is in the exclusive preview stage, either being used or being offered to a company...
Definition: engine_type.h:171
uint16 reliability_start
Initial reliability of the engine.
Definition: engine_base.h:29
uint16 reliability
Current reliability of the engine.
Definition: engine_base.h:27
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:81
Definition of stuff that is very close to a company, like the company struct itself.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:257
Functions for NewGRF engines.
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:306
static const int DAYS_IN_YEAR
days per year
Definition: date_type.h:31
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
void CheckEngines()
Check for engines that have an appropriate availability.
Definition: engine.cpp:1145
StringID GetEngineCategoryName(EngineID engine)
Return the category of an engine.
Definition: engine_gui.cpp:40
void EnginesDailyLoop()
Daily check to offer an exclusive engine preview to the companies.
Definition: engine.cpp:809
void UnloadWagonOverrides(Engine *e)
Unload all wagon override sprite groups.
byte landscape
the landscape we&#39;re currently in
Aircraft range.
Functions related to the autoreplace GUIs.
static bool IsUniqueEngineName(const char *name)
Is name still free as name for an engine?
Definition: engine.cpp:1022
uint32 grfid
The GRF ID of the file the entity belongs to.
Definition: engine_base.h:149
bool IsEnabled() const
Checks whether the engine is a valid (non-articulated part of an) engine.
Definition: engine.cpp:152
StringID GetAircraftTypeText() const
Get the name of the aircraft type for display purposes.
Definition: engine.cpp:470
static CompanyID GetPreviewCompany(Engine *e)
Get the best company for an engine preview.
Definition: engine.cpp:760
Train vehicle type.
Definition: vehicle_type.h:26
Max. speed: 1 unit = 1/1.6 mph = 1 km-ish/h.
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
int32 performance_history
Company score (scale 0-1000)
Definition: company_base.h:27
Functions related to dates.
Power in hp (if dualheaded: sum of both vehicles)
Conventional Take Off and Landing, i.e. planes.
Definition: engine_type.h:94
const uint8 _engine_offsets[4]
Offset of the first engine of each vehicle type in original engine data.
Definition: engine.cpp:60
Stores the state of all random number generators.
Definition: random_func.hpp:35
uint16 GetRange() const
Get the range of an aircraft type.
Definition: engine.cpp:456
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:25
Ship vehicle type.
Definition: vehicle_type.h:28
Replace vehicle window; Window numbers:
Definition: window_type.h:213
GRFFilePropsBase< NUM_CARGO+2 > grf_prop
Properties related the the grf file.
Definition: engine_base.h:60
Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:25
static const int GROUND_ACCELERATION
Acceleration due to gravity, 9.8 m/s^2.
Definition: vehicle_type.h:20
RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces)
Get the road types the given company can build.
Definition: road.cpp:190
Used for iterations.
Definition: road_type.h:31
VehicleType
Available vehicle types.
Definition: vehicle_type.h:23
byte block_preview
Number of quarters that the company is not allowed to get new exclusive engine previews (see Companie...
Definition: company_base.h:73
Yearly runningcost (if dualheaded: sum of both vehicles)
static void RestoreRandomSeeds(const SavedRandomSeeds &storage)
Restores previously saved seeds.
Definition: random_func.hpp:54
void StartupOneEngine(Engine *e, Date aging_date)
Start/initialise one engine.
Definition: engine.cpp:644
void SetRandomSeed(uint32 seed)
(Re)set the state of the random number generators.
Definition: random_func.cpp:67
Transport over water.
Functions related to vehicles.
CargoTypes GetUnionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
Ors the refit_masks of all articulated parts.
static bool IsWagon(EngineID index)
Determine whether an engine type is a wagon (and not a loco).
Definition: engine.cpp:570
Price
Enumeration of all base prices for use with Prices.
Definition: economy_type.h:67
CompanyMask company_hidden
Bit for each company whether the engine is normally hidden in the build gui for that company...
Definition: engine_base.h:40
Build vehicle; Window numbers:
Definition: window_type.h:378
Vehicle data structure.
Definition: vehicle_base.h:212
UnitID max_aircraft
max planes in game per company
New vehicle has become available.
Definition: news_type.h:35
Purchase cost (if dualheaded: sum of both vehicles)
static Year _year_engine_aging_stops
Year that engine aging stops.
Definition: engine.cpp:49
static const uint NUM_DEFAULT_ENGINES
Number of default entries.
Definition: engine_base.h:160
static void BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company=MAX_COMPANIES)
Broadcast a new event to all active AIs.
Definition: ai_core.cpp:263
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
void ShowErrorMessage(StringID summary_msg, StringID detailed_msg, WarningLevel wl, int x=0, int y=0, const GRFFile *textref_stack_grffile=nullptr, uint textref_stack_size=0, const uint32 *textref_stack=nullptr)
Display an error message in a window.
Definition: error_gui.cpp:382
EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
Looks up an EngineID in the EngineOverrideManager.
Definition: engine.cpp:512
Cargo capacity after refit.
Base for aircraft.
Tractive effort coefficient in 1/256.
Common return value for all commands.
Definition: command_type.h:25
int32 Year
Type for the year, note: 0 based, i.e. starts at the year 0.
Definition: date_type.h:20
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
static void NewVehicleAvailable(Engine *e)
An engine has become available for general use.
Definition: engine.cpp:910
byte flags
Flags of the engine.
Definition: engine_base.h:35
void ClearEnginesHiddenFlagOfCompany(CompanyID cid)
Clear the &#39;hidden&#39; flag for all engines of a new company.
Definition: engine.cpp:854
bool never_expire_vehicles
never expire vehicles
CommandCost CmdSetVehicleVisibility(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Set the visibility of an engine.
Definition: engine.cpp:871
Year _cur_year
Current year, starting at 0.
Definition: date.cpp:26
static Aircraft * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
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
Date base_intro
Basic date of engine introduction (without random parts).
Definition: engine_type.h:135
bool IsNormalAircraft() const
Check if the aircraft type is a normal flying device; eg not a rotor or a shadow. ...
Definition: aircraft.h:123
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition: economy.cpp:966
Year lifelength
Lifetime of a single vehicle.
Definition: engine_type.h:136
static T SB(T &x, const uint8 s, const uint8 n, const U d)
Set n bits in x starting at bit s to d.
uint16 duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:33
uint32 GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition: engine.cpp:162
RoadType roadtype
Road type.
Definition: engine_type.h:127
Pseudo random number generator.
uint16 multiplier
Capacity multiplier for vehicles. (8 fractional bits)
Definition: cargotype.h:63
bool IsEngineRefittable(EngineID engine)
Check if an engine is refittable.
Definition: engine.cpp:1119
Invalid cargo type.
Definition: cargo_type.h:70
bool ai_disable_veh_train
disable types for AI
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition: window.cpp:3318
static const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:226
Functions related to low-level strings.
Some methods of Pool are placed here in order to reduce compilation time and binary size...
uint16 reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:28
Money GetCost() const
Return how much a new engine costs.
Definition: engine.cpp:321
Engine preview window; Window numbers:
Definition: window_type.h:585
Information about a vehicle.
Definition: engine_type.h:134
uint16 internal_id
The internal ID within the GRF file.
Definition: engine_base.h:150
Other information.
Definition: error.h:24
Functions related to errors.
RoadTypes powered_roadtypes
bitmask to the OTHER roadtypes on which a vehicle of THIS roadtype generates power ...
Definition: road.h:121
byte cargo_subtype
Used for livery refits (NewGRF variations)
Definition: vehicle_base.h:306
void SetupEngines()
Initialise the engine pool with the data from the original vehicles.
Definition: engine.cpp:546
void SetYearEngineAgingStops()
Compute the value for _year_engine_aging_stops.
Definition: engine.cpp:618
RoadTypes AddDateIntroducedRoadTypes(RoadTypes current, Date date)
Add the road types that are to be introduced at the given date.
Definition: road.cpp:157
int8 retire_early
Number of years early to retire vehicle.
Definition: engine_type.h:146
UnitID max_roadveh
max trucks in game per company
Functions related to engines.
CommandCost CmdWantEnginePreview(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Accept an engine prototype.
Definition: engine.cpp:895
DoCommandFlag
List of flags for a command.
Definition: command_type.h:344
uint16 duration_phase_3
Third reliability phase on months, decaying to reliability_final.
Definition: engine_base.h:34
byte callback_mask
Bitmask of vehicle callbacks that have to be called.
Definition: engine_type.h:145
simple wagon, not motorized
Definition: engine_type.h:31
Stores the mapping of EngineID to the internal id of newgrfs.
Definition: engine_base.h:159
Capacity (if dualheaded: for each single vehicle)
Definition of base types and functions in a cross-platform compatible way.
Show suffix after cargo name.
Weight in 1/4 t.
uint16 duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max...
Definition: engine_base.h:32
Data structure to convert between Date and triplet (year, month, and day).
Definition: date_type.h:103
A number of safeguards to prevent using unsafe methods.
static bool IsVehicleTypeDisabled(VehicleType type, bool ai)
Checks if a vehicle type is disabled for all/ai companies.
Definition: engine.cpp:796
RailTypes introduces_railtypes
Bitmask of which other railtypes are introduced when this railtype is introduced. ...
Definition: rail.h:265
Max. speed: 1 unit = 1/0.8 mph = 2 km-ish/h.
Engine GUI functions, used by build_vehicle_gui and autoreplace_gui
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:42
CargoID cargo_type
type of cargo this vehicle is carrying
Definition: vehicle_base.h:305
static const int CARGO_AGING_TICKS
cycle duration for aging cargo
Definition: date_type.h:37
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:39
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:138
uint16 reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:30
static void CalcEngineReliability(Engine *e)
Update Engine::reliability and (if needed) update the engine GUIs.
Definition: engine.cpp:580
byte misc_flags
Miscellaneous flags.
Definition: engine_type.h:144
Year year
Year (0...)
Definition: date_type.h:104
Power in 10 HP.
Owner owner
Which company owns the vehicle?
Definition: vehicle_base.h:273
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
uint16 reliability_final
Final reliability of the engine.
Definition: engine_base.h:31
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
bool ai_disable_veh_ship
disable types for AI
static bool IsCargoInClass(CargoID c, CargoClass cc)
Does cargo c have cargo class cc?
Definition: cargotype.h:150
char * name
Custom name of engine.
Definition: engine_base.h:24
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
const uint8 _engine_counts[4]
Number of engines of each vehicle type in original engine data.
Definition: engine.cpp:52
static void AcceptEnginePreview(EngineID eid, CompanyID company)
Company company accepts engine eid for preview.
Definition: engine.cpp:723
void DeleteWindowByClass(WindowClass cls)
Delete all windows of a given class.
Definition: window.cpp:1159
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:35
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition: engine.cpp:361
Base class for all pools.
Definition: pool_type.hpp:83
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
Definition: ai_core.cpp:238
Build toolbar; Window numbers:
Definition: window_type.h:68
VehicleType type
The engine type.
Definition: engine_base.h:151
void StartupEngines()
Start/initialise all our engines.
Definition: engine.cpp:697
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:226
void DeleteWindowById(WindowClass cls, WindowNumber number, bool force)
Delete a window by its class and window number (if it is open).
Definition: window.cpp:1146
static bool IsInteractiveCompany(CompanyID company)
Is the user representing company?
Definition: company_func.h:55
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:37
uint GetPower() const
Returns the power of the engine for display and sorting purposes.
Definition: engine.cpp:393
execute the given command
Definition: command_type.h:346
UnitID max_ships
max ships in game per company
static const EngineID INVALID_ENGINE
Constant denoting an invalid engine.
Definition: engine_type.h:176
byte preview_wait
Daily countdown timer for timeout of offering the engine to the preview_company company.
Definition: engine_base.h:38
Functions related to companies.
An invalid company.
Definition: company_type.h:32
Functions related to articulated vehicles.
Base class for engines.
RailType
Enumeration for all possible railtypes.
Definition: rail_type.h:29
uint32 generation_seed
noise seed for world generation
void EnginesMonthlyLoop()
Monthly update of the availability, reliability, and preview offers of the engines.
Definition: engine.cpp:979
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition: engine_base.h:140
static bool ResetToCurrentNewGRFConfig()
Tries to reset the engine mapping to match the current NewGRF configuration.
Definition: engine.cpp:529
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:59
Refit capacity, the passed vehicle needs to have its ->cargo_type set to the cargo we are refitting t...
size_t Utf8StringLength(const char *s)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Definition: string.cpp:312
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
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
Max. speed: 1 unit = 1/3.2 mph = 0.5 km-ish/h.
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition: engine_base.h:36
indicates a combination of two locomotives
Definition: engine_type.h:30
RoadTypes avail_roadtypes
Road types available to this company.
Definition: company_base.h:123
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
CommandCost CmdRenameEngine(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
Rename an engine.
Definition: engine.cpp:1042
StringID string_id
Default name of engine.
Definition: engine_type.h:147
byte extend_vehicle_life
extend vehicle life by this many years
CompanyEconomyEntry old_economy[MAX_HISTORY_QUARTERS]
Economic data of the company of the last MAX_HISTORY_QUARTERS quarters.
Definition: company_base.h:99
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:54
bool ai_disable_veh_aircraft
disable types for AI
Maximum number of companies.
Definition: company_type.h:25
#define MAX_DAY
The number of days till the last day.
Definition: date_type.h:97
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() ...
Definition: pool_type.hpp:216
Transport by road vehicle.
Default value to indicate that visual effect should be based on engine class.
Definition: vehicle_base.h:94
static uint CountBits(T value)
Counts the number of set bits in a variable.
uint8 substitute_id
The (original) entity ID to use if this GRF is not available (currently not used) ...
Definition: engine_base.h:152
Functions related to commands.
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
uint16 local_id
id defined by the grf file for this entity
UnitID max_trains
max trains in game per company
Max. speed: 1 unit = 8 mph = 12.8 km-ish/h.
Reference engine.
Definition: news_type.h:58
uint16 GetVehicleCallback(CallbackID callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v)
Evaluate a newgrf callback for vehicles.
int32 Date
The type to store our dates in.
Definition: date_type.h:16
Tractive effort coefficient in 1/256.
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 original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition: engine_base.h:41
void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type)
When an engine is made buildable or is removed from being buildable, add/remove it from the build/aut...
Money GetRunningCost() const
Return how much the running costs of this engine are.
Definition: engine.cpp:284
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:288
Used for iterations.
Definition: rail_type.h:35
Passengers.
Definition: cargotype.h:41
RailTypes avail_railtypes
Rail types available to this company.
Definition: company_base.h:122
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Base functions for all AIs.
#define FOR_ALL_VEHICLES(var)
Iterate over all vehicles.
Definition: vehicle_base.h:987
static void SaveRandomSeeds(SavedRandomSeeds *storage)
Saves the current seeds.
Definition: random_func.hpp:44
void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1=NR_NONE, uint32 ref1=UINT32_MAX, NewsReferenceType reftype2=NR_NONE, uint32 ref2=UINT32_MAX, void *free_data=nullptr)
Add a new newsitem to be shown.
Definition: news_gui.cpp:698
GameCreationSettings game_creation
settings used during the creation of a game (map)
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
uint16 cargo_age_period
Number of ticks before carried cargo is aged.
Definition: engine_type.h:148
Weight in t (if dualheaded: for each single vehicle)
Year base_life
Basic duration of engine availability (without random parts). 0xFF means infinite life...
Definition: engine_type.h:137
Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
Definition: date.cpp:149
static const uint MAX_LENGTH_ENGINE_NAME_CHARS
The maximum length of an engine name in characters including &#39;\0&#39;.
Definition: engine_type.h:174
RailTypes GetCompanyRailtypes(CompanyID company, bool introduces)
Get the rail types the given company can build.
Definition: rail.cpp:268
This vehicle is available to everyone.
Definition: engine_type.h:170
Window functions not directly related to making/drawing windows.
void ReloadNewGRFData()
Reload all NewGRF files during a running game.
Definition: afterload.cpp:3234
RoadTypes introduces_roadtypes
Bitmask of which other roadtypes are introduced when this roadtype is introduced. ...
Definition: road.h:176
uint GetDisplayWeight() const
Returns the weight of the engine for display purposes.
Definition: engine.cpp:411
Use the new capacity algorithm. The default cargotype of the vehicle does not affect capacity multipl...
Definition: engine_type.h:161
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition: window.cpp:3227
uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition: engine.cpp:206
Functions related to news.
bool ai_disable_veh_roadveh
disable types for AI
byte climates
Climates supported by the engine.
Definition: engine_type.h:140
Date _date
Current date in days (day counter)
Definition: date.cpp:28
Vehicle news item. (new engine available)
Definition: news_type.h:81
void ConvertDateToYMD(Date date, YearMonthDay *ymd)
Converts a Date to a Year, Month & Day.
Definition: date.cpp:94
uint GetDisplayMaxTractiveEffort() const
Returns the tractive effort of the engine for display purposes.
Definition: engine.cpp:429
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
The object is owned by a superuser / goal script.
Definition: company_type.h:29
static bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Definition: vehicle_func.h:91
RailTypes AddDateIntroducedRailTypes(RailTypes current, Date date)
Add the rail types that are to be introduced at the given date.
Definition: rail.cpp:235
Year starting_year
starting date
Road vehicle type.
Definition: vehicle_type.h:27
Date GetLifeLengthInDays() const
Returns the vehicle&#39;s (not model&#39;s!) life length in days.
Definition: engine.cpp:446
No roadtypes.
Definition: road_type.h:42
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
Definition: window.cpp:3300
static const int DAYS_IN_LEAP_YEAR
sometimes, you need one day more...
Definition: date_type.h:32
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1459
virtual void CleanPool()
Virtual method that deletes all items in the pool.
void ResetToDefaultMapping()
Initializes the EngineOverrideManager with the default engines.
Definition: engine.cpp:488
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:107
This file contains all the data for vehicles.
static void SetDParam(uint n, uint64 v)
Set a string parameter v at index n in the global string parameter array.
Definition: strings_func.h:201
Base for the NewGRF implementation.