OpenTTD Source  13.0-beta2
engine.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "stdafx.h"
11 #include "company_func.h"
12 #include "command_func.h"
13 #include "news_func.h"
14 #include "aircraft.h"
15 #include "newgrf.h"
16 #include "newgrf_engine.h"
17 #include "strings_func.h"
18 #include "core/random_func.hpp"
19 #include "window_func.h"
20 #include "date_func.h"
21 #include "autoreplace_gui.h"
22 #include "string_func.h"
23 #include "ai/ai.hpp"
24 #include "core/pool_func.hpp"
25 #include "engine_gui.h"
26 #include "engine_func.h"
27 #include "engine_base.h"
28 #include "company_base.h"
29 #include "vehicle_func.h"
30 #include "articulated_vehicles.h"
31 #include "error.h"
32 #include "engine_base.h"
33 
34 #include "table/strings.h"
35 #include "table/engines.h"
36 
37 #include "safeguards.h"
38 
39 EnginePool _engine_pool("Engine");
41 
42 EngineOverrideManager _engine_mngr;
43 
49 
51 const uint8 _engine_counts[4] = {
52  lengthof(_orig_rail_vehicle_info),
53  lengthof(_orig_road_vehicle_info),
54  lengthof(_orig_ship_vehicle_info),
55  lengthof(_orig_aircraft_vehicle_info),
56 };
57 
59 const uint8 _engine_offsets[4] = {
60  0,
61  lengthof(_orig_rail_vehicle_info),
62  lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info),
63  lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info),
64 };
65 
66 static_assert(lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info) + lengthof(_orig_aircraft_vehicle_info) == lengthof(_orig_engine_info));
67 
69 
70 Engine::Engine(VehicleType type, EngineID base)
71 {
72  this->type = type;
73  this->grf_prop.local_id = base;
74  this->list_position = base;
76 
77  /* Check if this base engine is within the original engine data range */
78  if (base >= _engine_counts[type]) {
79  /* 'power' defaults to zero, so we also have to default to 'wagon' */
80  if (type == VEH_TRAIN) this->u.rail.railveh_type = RAILVEH_WAGON;
81  /* Set model life to maximum to make wagons available */
82  this->info.base_life = 0xFF;
83  /* Set road vehicle tractive effort to the default value */
84  if (type == VEH_ROAD) this->u.road.tractive_effort = 0x4C;
85  /* Aircraft must have CT_INVALID as default, as there is no property */
86  if (type == VEH_AIRCRAFT) this->info.cargo_type = CT_INVALID;
87  /* Set visual effect to the default value */
88  switch (type) {
89  case VEH_TRAIN: this->u.rail.visual_effect = VE_DEFAULT; break;
90  case VEH_ROAD: this->u.road.visual_effect = VE_DEFAULT; break;
91  case VEH_SHIP: this->u.ship.visual_effect = VE_DEFAULT; break;
92  default: break; // The aircraft, disasters and especially visual effects have no NewGRF configured visual effects
93  }
94  /* Set cargo aging period to the default value. */
96  return;
97  }
98 
99  /* Copy the original engine info for this slot */
100  this->info = _orig_engine_info[_engine_offsets[type] + base];
101 
102  /* Copy the original engine data for this slot */
103  switch (type) {
104  default: NOT_REACHED();
105 
106  case VEH_TRAIN:
107  this->u.rail = _orig_rail_vehicle_info[base];
108  this->original_image_index = this->u.rail.image_index;
109  this->info.string_id = STR_VEHICLE_NAME_TRAIN_ENGINE_RAIL_KIRBY_PAUL_TANK_STEAM + base;
110 
111  /* Set the default model life of original wagons to "infinite" */
112  if (this->u.rail.railveh_type == RAILVEH_WAGON) this->info.base_life = 0xFF;
113 
114  break;
115 
116  case VEH_ROAD:
117  this->u.road = _orig_road_vehicle_info[base];
118  this->original_image_index = this->u.road.image_index;
119  this->info.string_id = STR_VEHICLE_NAME_ROAD_VEHICLE_MPS_REGAL_BUS + base;
120  break;
121 
122  case VEH_SHIP:
123  this->u.ship = _orig_ship_vehicle_info[base];
124  this->original_image_index = this->u.ship.image_index;
125  this->info.string_id = STR_VEHICLE_NAME_SHIP_MPS_OIL_TANKER + base;
126  break;
127 
128  case VEH_AIRCRAFT:
129  this->u.air = _orig_aircraft_vehicle_info[base];
130  this->original_image_index = this->u.air.image_index;
131  this->info.string_id = STR_VEHICLE_NAME_AIRCRAFT_SAMPSON_U52 + base;
132  break;
133  }
134 }
135 
140 bool Engine::IsEnabled() const
141 {
142  return this->info.string_id != STR_NEWGRF_INVALID_ENGINE && HasBit(this->info.climates, _settings_game.game_creation.landscape);
143 }
144 
150 uint32 Engine::GetGRFID() const
151 {
152  const GRFFile *file = this->GetGRF();
153  return file == nullptr ? 0 : file->grfid;
154 }
155 
162 {
163  /* For engines that can appear in a consist (i.e. rail vehicles and (articulated) road vehicles), a capacity
164  * of zero is a special case, to define the vehicle to not carry anything. The default cargotype is still used
165  * for livery selection etc.
166  * Note: Only the property is tested. A capacity callback returning 0 does not have the same effect.
167  */
168  switch (this->type) {
169  case VEH_TRAIN:
170  if (this->u.rail.capacity == 0) return false;
171  break;
172 
173  case VEH_ROAD:
174  if (this->u.road.capacity == 0) return false;
175  break;
176 
177  case VEH_SHIP:
178  case VEH_AIRCRAFT:
179  break;
180 
181  default: NOT_REACHED();
182  }
183  return this->GetDefaultCargoType() != CT_INVALID;
184 }
185 
186 
194 uint Engine::DetermineCapacity(const Vehicle *v, uint16 *mail_capacity) const
195 {
196  assert(v == nullptr || this->index == v->engine_type);
197  if (mail_capacity != nullptr) *mail_capacity = 0;
198 
199  if (!this->CanCarryCargo()) return 0;
200 
201  bool new_multipliers = HasBit(this->info.misc_flags, EF_NO_DEFAULT_CARGO_MULTIPLIER);
202  CargoID default_cargo = this->GetDefaultCargoType();
203  CargoID cargo_type = (v != nullptr) ? v->cargo_type : default_cargo;
204 
205  if (mail_capacity != nullptr && this->type == VEH_AIRCRAFT && IsCargoInClass(cargo_type, CC_PASSENGERS)) {
206  *mail_capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity, v);
207  }
208 
209  /* Check the refit capacity callback if we are not in the default configuration, or if we are using the new multiplier algorithm. */
211  (new_multipliers || default_cargo != cargo_type || (v != nullptr && v->cargo_subtype != 0))) {
212  uint16 callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, this->index, v);
213  if (callback != CALLBACK_FAILED) return callback;
214  }
215 
216  /* Get capacity according to property resp. CB */
217  uint capacity;
218  uint extra_mail_cap = 0;
219  switch (this->type) {
220  case VEH_TRAIN:
221  capacity = GetEngineProperty(this->index, PROP_TRAIN_CARGO_CAPACITY, this->u.rail.capacity, v);
222 
223  /* In purchase list add the capacity of the second head. Always use the plain property for this. */
224  if (v == nullptr && this->u.rail.railveh_type == RAILVEH_MULTIHEAD) capacity += this->u.rail.capacity;
225  break;
226 
227  case VEH_ROAD:
228  capacity = GetEngineProperty(this->index, PROP_ROADVEH_CARGO_CAPACITY, this->u.road.capacity, v);
229  break;
230 
231  case VEH_SHIP:
232  capacity = GetEngineProperty(this->index, PROP_SHIP_CARGO_CAPACITY, this->u.ship.capacity, v);
233  break;
234 
235  case VEH_AIRCRAFT:
236  capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_PASSENGER_CAPACITY, this->u.air.passenger_capacity, v);
237  if (!IsCargoInClass(cargo_type, CC_PASSENGERS)) {
238  extra_mail_cap = GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity, v);
239  }
240  if (!new_multipliers && cargo_type == CT_MAIL) return capacity + extra_mail_cap;
241  default_cargo = CT_PASSENGERS; // Always use 'passengers' wrt. cargo multipliers
242  break;
243 
244  default: NOT_REACHED();
245  }
246 
247  if (!new_multipliers) {
248  /* Use the passenger multiplier for mail as well */
249  capacity += extra_mail_cap;
250  extra_mail_cap = 0;
251  }
252 
253  /* Apply multipliers depending on cargo- and vehicletype. */
254  if (new_multipliers || (this->type != VEH_SHIP && default_cargo != cargo_type)) {
255  uint16 default_multiplier = new_multipliers ? 0x100 : CargoSpec::Get(default_cargo)->multiplier;
256  uint16 cargo_multiplier = CargoSpec::Get(cargo_type)->multiplier;
257  capacity *= cargo_multiplier;
258  if (extra_mail_cap > 0) {
259  uint mail_multiplier = CargoSpec::Get(CT_MAIL)->multiplier;
260  capacity += (default_multiplier * extra_mail_cap * cargo_multiplier + mail_multiplier / 2) / mail_multiplier;
261  }
262  capacity = (capacity + default_multiplier / 2) / default_multiplier;
263  }
264 
265  return capacity;
266 }
267 
273 {
274  Price base_price;
275  uint cost_factor;
276  switch (this->type) {
277  case VEH_ROAD:
278  base_price = this->u.road.running_cost_class;
279  if (base_price == INVALID_PRICE) return 0;
280  cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_RUNNING_COST_FACTOR, this->u.road.running_cost);
281  break;
282 
283  case VEH_TRAIN:
284  base_price = this->u.rail.running_cost_class;
285  if (base_price == INVALID_PRICE) return 0;
286  cost_factor = GetEngineProperty(this->index, PROP_TRAIN_RUNNING_COST_FACTOR, this->u.rail.running_cost);
287  break;
288 
289  case VEH_SHIP:
290  base_price = PR_RUNNING_SHIP;
291  cost_factor = GetEngineProperty(this->index, PROP_SHIP_RUNNING_COST_FACTOR, this->u.ship.running_cost);
292  break;
293 
294  case VEH_AIRCRAFT:
295  base_price = PR_RUNNING_AIRCRAFT;
296  cost_factor = GetEngineProperty(this->index, PROP_AIRCRAFT_RUNNING_COST_FACTOR, this->u.air.running_cost);
297  break;
298 
299  default: NOT_REACHED();
300  }
301 
302  return GetPrice(base_price, cost_factor, this->GetGRF(), -8);
303 }
304 
310 {
311  Price base_price;
312  uint cost_factor;
313  switch (this->type) {
314  case VEH_ROAD:
315  base_price = PR_BUILD_VEHICLE_ROAD;
316  cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_COST_FACTOR, this->u.road.cost_factor);
317  break;
318 
319  case VEH_TRAIN:
320  if (this->u.rail.railveh_type == RAILVEH_WAGON) {
321  base_price = PR_BUILD_VEHICLE_WAGON;
322  cost_factor = GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor);
323  } else {
324  base_price = PR_BUILD_VEHICLE_TRAIN;
325  cost_factor = GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor);
326  }
327  break;
328 
329  case VEH_SHIP:
330  base_price = PR_BUILD_VEHICLE_SHIP;
331  cost_factor = GetEngineProperty(this->index, PROP_SHIP_COST_FACTOR, this->u.ship.cost_factor);
332  break;
333 
334  case VEH_AIRCRAFT:
335  base_price = PR_BUILD_VEHICLE_AIRCRAFT;
336  cost_factor = GetEngineProperty(this->index, PROP_AIRCRAFT_COST_FACTOR, this->u.air.cost_factor);
337  break;
338 
339  default: NOT_REACHED();
340  }
341 
342  return GetPrice(base_price, cost_factor, this->GetGRF(), -8);
343 }
344 
350 {
351  switch (this->type) {
352  case VEH_TRAIN:
353  return GetEngineProperty(this->index, PROP_TRAIN_SPEED, this->u.rail.max_speed);
354 
355  case VEH_ROAD: {
356  uint max_speed = GetEngineProperty(this->index, PROP_ROADVEH_SPEED, 0);
357  return (max_speed != 0) ? max_speed * 2 : this->u.road.max_speed / 2;
358  }
359 
360  case VEH_SHIP:
361  return GetEngineProperty(this->index, PROP_SHIP_SPEED, this->u.ship.max_speed) / 2;
362 
363  case VEH_AIRCRAFT: {
364  uint max_speed = GetEngineProperty(this->index, PROP_AIRCRAFT_SPEED, 0);
365  if (max_speed != 0) {
366  return (max_speed * 128) / 10;
367  }
368  return this->u.air.max_speed;
369  }
370 
371  default: NOT_REACHED();
372  }
373 }
374 
381 uint Engine::GetPower() const
382 {
383  /* Only trains and road vehicles have 'power'. */
384  switch (this->type) {
385  case VEH_TRAIN:
386  return GetEngineProperty(this->index, PROP_TRAIN_POWER, this->u.rail.power);
387  case VEH_ROAD:
388  return GetEngineProperty(this->index, PROP_ROADVEH_POWER, this->u.road.power) * 10;
389 
390  default: NOT_REACHED();
391  }
392 }
393 
400 {
401  /* Only trains and road vehicles have 'weight'. */
402  switch (this->type) {
403  case VEH_TRAIN:
404  return GetEngineProperty(this->index, PROP_TRAIN_WEIGHT, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0);
405  case VEH_ROAD:
406  return GetEngineProperty(this->index, PROP_ROADVEH_WEIGHT, this->u.road.weight) / 4;
407 
408  default: NOT_REACHED();
409  }
410 }
411 
418 {
419  /* Only trains and road vehicles have 'tractive effort'. */
420  switch (this->type) {
421  case VEH_TRAIN:
422  return (GROUND_ACCELERATION * this->GetDisplayWeight() * GetEngineProperty(this->index, PROP_TRAIN_TRACTIVE_EFFORT, this->u.rail.tractive_effort)) / 256 / 1000;
423  case VEH_ROAD:
424  return (GROUND_ACCELERATION * this->GetDisplayWeight() * GetEngineProperty(this->index, PROP_ROADVEH_TRACTIVE_EFFORT, this->u.road.tractive_effort)) / 256 / 1000;
425 
426  default: NOT_REACHED();
427  }
428 }
429 
435 {
436  /* Assume leap years; this gives the player a bit more than the given amount of years, but never less. */
438 }
439 
444 uint16 Engine::GetRange() const
445 {
446  switch (this->type) {
447  case VEH_AIRCRAFT:
448  return GetEngineProperty(this->index, PROP_AIRCRAFT_RANGE, this->u.air.max_range);
449 
450  default: NOT_REACHED();
451  }
452 }
453 
459 {
460  switch (this->type) {
461  case VEH_AIRCRAFT:
462  switch (this->u.air.subtype) {
463  case AIR_HELI: return STR_LIVERY_HELICOPTER;
464  case AIR_CTOL: return STR_LIVERY_SMALL_PLANE;
465  case AIR_CTOL | AIR_FAST: return STR_LIVERY_LARGE_PLANE;
466  default: NOT_REACHED();
467  }
468 
469  default: NOT_REACHED();
470  }
471 }
472 
477 {
478  this->clear();
479  for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) {
480  for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++) {
481  EngineIDMapping &eid = this->emplace_back();
482  eid.type = type;
483  eid.grfid = INVALID_GRFID;
484  eid.internal_id = internal_id;
485  eid.substitute_id = internal_id;
486  }
487  }
488 }
489 
499 EngineID EngineOverrideManager::GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
500 {
501  EngineID index = 0;
502  for (const EngineIDMapping &eid : *this) {
503  if (eid.type == type && eid.grfid == grfid && eid.internal_id == grf_local_id) {
504  return index;
505  }
506  index++;
507  }
508  return INVALID_ENGINE;
509 }
510 
517 {
518  for (const Vehicle *v : Vehicle::Iterate()) {
519  if (IsCompanyBuildableVehicleType(v)) return false;
520  }
521 
522  /* Reset the engines, they will get new EngineIDs */
523  _engine_mngr.ResetToDefaultMapping();
525 
526  return true;
527 }
528 
533 {
535  _engine_pool.CleanPool();
536 
537  assert(_engine_mngr.size() >= _engine_mngr.NUM_DEFAULT_ENGINES);
538  uint index = 0;
539  for (const EngineIDMapping &eid : _engine_mngr) {
540  /* Assert is safe; there won't be more than 256 original vehicles
541  * in any case, and we just cleaned the pool. */
542  assert(Engine::CanAllocateItem());
543  [[maybe_unused]] const Engine *e = new Engine(eid.type, eid.internal_id);
544  assert(e->index == index);
545  index++;
546  }
547 }
548 
549 void ShowEnginePreviewWindow(EngineID engine);
550 
556 static bool IsWagon(EngineID index)
557 {
558  const Engine *e = Engine::Get(index);
559  return e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON;
560 }
561 
567 {
568  uint age = e->age;
569 
570  /* Check for early retirement */
571  if (e->company_avail != 0 && !_settings_game.vehicle.never_expire_vehicles && e->info.base_life != 0xFF) {
572  int retire_early = e->info.retire_early;
573  uint retire_early_max_age = std::max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
574  if (retire_early != 0 && age >= retire_early_max_age) {
575  /* Early retirement is enabled and we're past the date... */
576  e->company_avail = 0;
578  }
579  }
580 
581  if (age < e->duration_phase_1) {
582  uint start = e->reliability_start;
583  e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
584  } else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _settings_game.vehicle.never_expire_vehicles || e->info.base_life == 0xFF) {
585  /* We are at the peak of this engines life. It will have max reliability.
586  * This is also true if the engines never expire. They will not go bad over time */
588  } else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
589  uint max = e->reliability_max;
590  e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
591  } else {
592  /* time's up for this engine.
593  * We will now completely retire this design */
594  e->company_avail = 0;
596  /* Kick this engine out of the lists */
598  }
599  SetWindowClassesDirty(WC_BUILD_VEHICLE); // Update to show the new reliability
601 }
602 
605 {
606  /* Determine last engine aging year, default to 2050 as previously. */
608 
609  for (const Engine *e : Engine::Iterate()) {
610  const EngineInfo *ei = &e->info;
611 
612  /* Exclude certain engines */
614  if (e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON) continue;
615 
616  /* Base year ending date on half the model life */
617  YearMonthDay ymd;
618  ConvertDateToYMD(ei->base_intro + (ei->lifelength * DAYS_IN_LEAP_YEAR) / 2, &ymd);
619 
621  }
622 }
623 
629 void StartupOneEngine(Engine *e, Date aging_date)
630 {
631  const EngineInfo *ei = &e->info;
632 
633  e->age = 0;
634  e->flags = 0;
635  e->company_avail = 0;
636  e->company_hidden = 0;
637 
638  /* Vehicles with the same base_intro date shall be introduced at the same time.
639  * Make sure they use the same randomisation of the date. */
640  SavedRandomSeeds saved_seeds;
641  SaveRandomSeeds(&saved_seeds);
643  ei->base_intro ^
644  e->type ^
645  e->GetGRFID());
646  uint32 r = Random();
647 
648  /* Don't randomise the start-date in the first two years after gamestart to ensure availability
649  * of engines in early starting games.
650  * Note: TTDP uses fixed 1922 */
652  if (e->intro_date <= _date) {
653  e->age = (aging_date - e->intro_date) >> 5;
654  e->company_avail = (CompanyMask)-1;
655  e->flags |= ENGINE_AVAILABLE;
656  }
657 
658  RestoreRandomSeeds(saved_seeds);
659 
660  r = Random();
661  e->reliability_start = GB(r, 16, 14) + 0x7AE0;
662  e->reliability_max = GB(r, 0, 14) + 0xBFFF;
663 
664  r = Random();
665  e->reliability_final = GB(r, 16, 14) + 0x3FFF;
666  e->duration_phase_1 = GB(r, 0, 5) + 7;
667  e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
668  e->duration_phase_3 = GB(r, 9, 7) + 120;
669 
670  e->reliability_spd_dec = ei->decay_speed << 2;
671 
673 
674  /* prevent certain engines from ever appearing. */
676  e->flags |= ENGINE_AVAILABLE;
677  e->company_avail = 0;
678  }
679 }
680 
686 {
687  /* Aging of vehicles stops, so account for that when starting late */
688  const Date aging_date = std::min(_date, ConvertYMDToDate(_year_engine_aging_stops, 0, 1));
689 
690  for (Engine *e : Engine::Iterate()) {
691  StartupOneEngine(e, aging_date);
692  }
693 
694  /* Update the bitmasks for the vehicle lists */
695  for (Company *c : Company::Iterate()) {
696  c->avail_railtypes = GetCompanyRailtypes(c->index);
697  c->avail_roadtypes = GetCompanyRoadTypes(c->index);
698  }
699 
700  /* Invalidate any open purchase lists */
702 }
703 
709 static void EnableEngineForCompany(EngineID eid, CompanyID company)
710 {
711  Engine *e = Engine::Get(eid);
712  Company *c = Company::Get(company);
713 
714  SetBit(e->company_avail, company);
715  if (e->type == VEH_TRAIN) {
717  } else if (e->type == VEH_ROAD) {
719  }
720 
721  if (company == _local_company) {
723 
724  /* Update the toolbar. */
729  }
730 }
731 
737 static void DisableEngineForCompany(EngineID eid, CompanyID company)
738 {
739  Engine *e = Engine::Get(eid);
740  Company *c = Company::Get(company);
741 
742  ClrBit(e->company_avail, company);
743  if (e->type == VEH_TRAIN) {
745  } else if (e->type == VEH_ROAD) {
747  }
748 
749  if (company == _local_company) {
751  }
752 }
753 
759 static void AcceptEnginePreview(EngineID eid, CompanyID company)
760 {
761  Engine *e = Engine::Get(eid);
762 
764  e->preview_asked = (CompanyMask)-1;
765 
766  EnableEngineForCompany(eid, company);
767 
768  /* Notify preview window, that it might want to close.
769  * Note: We cannot directly close the window.
770  * In singleplayer this function is called from the preview window, so
771  * we have to use the GUI-scope scheduling of InvalidateWindowData.
772  */
774 }
775 
782 {
783  CompanyID best_company = INVALID_COMPANY;
784 
785  /* For trains the cargomask has no useful meaning, since you can attach other wagons */
786  CargoTypes cargomask = e->type != VEH_TRAIN ? GetUnionOfArticulatedRefitMasks(e->index, true) : ALL_CARGOTYPES;
787 
788  int32 best_hist = -1;
789  for (const Company *c : Company::Iterate()) {
790  if (c->block_preview == 0 && !HasBit(e->preview_asked, c->index) &&
791  c->old_economy[0].performance_history > best_hist) {
792 
793  /* Check whether the company uses similar vehicles */
794  for (const Vehicle *v : Vehicle::Iterate()) {
795  if (v->owner != c->index || v->type != e->type) continue;
796  if (!v->GetEngine()->CanCarryCargo() || !HasBit(cargomask, v->cargo_type)) continue;
797 
798  best_hist = c->old_economy[0].performance_history;
799  best_company = c->index;
800  break;
801  }
802  }
803  }
804 
805  return best_company;
806 }
807 
815 static bool IsVehicleTypeDisabled(VehicleType type, bool ai)
816 {
817  switch (type) {
822 
823  default: NOT_REACHED();
824  }
825 }
826 
829 {
830  for (Company *c : Company::Iterate()) {
831  c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes, _date);
832  c->avail_roadtypes = AddDateIntroducedRoadTypes(c->avail_roadtypes, _date);
833  }
834 
835  if (_cur_year >= _year_engine_aging_stops) return;
836 
837  for (Engine *e : Engine::Iterate()) {
838  EngineID i = e->index;
839  if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
840  if (e->preview_company != INVALID_COMPANY) {
841  if (!--e->preview_wait) {
843  e->preview_company = INVALID_COMPANY;
844  }
845  } else if (CountBits(e->preview_asked) < MAX_COMPANIES) {
846  e->preview_company = GetPreviewCompany(e);
847 
848  if (e->preview_company == INVALID_COMPANY) {
849  e->preview_asked = (CompanyMask)-1;
850  continue;
851  }
852 
853  SetBit(e->preview_asked, e->preview_company);
854  e->preview_wait = 20;
855  /* AIs are intentionally not skipped for preview even if they cannot build a certain
856  * vehicle type. This is done to not give poor performing human companies an "unfair"
857  * boost that they wouldn't have gotten against other human companies. The check on
858  * the line below is just to make AIs not notice that they have a preview if they
859  * cannot build the vehicle. */
860  if (!IsVehicleTypeDisabled(e->type, true)) AI::NewEvent(e->preview_company, new ScriptEventEnginePreview(i));
861  if (IsInteractiveCompany(e->preview_company)) ShowEnginePreviewWindow(i);
862  }
863  }
864  }
865 }
866 
872 {
873  for (Engine *e : Engine::Iterate()) {
874  SB(e->company_hidden, cid, 1, 0);
875  }
876 }
877 
886 {
887  Engine *e = Engine::GetIfValid(engine_id);
888  if (e == nullptr || _current_company >= MAX_COMPANIES) return CMD_ERROR;
889  if (!IsEngineBuildable(e->index, e->type, _current_company)) return CMD_ERROR;
890 
891  if ((flags & DC_EXEC) != 0) {
892  SB(e->company_hidden, _current_company, 1, hide ? 1 : 0);
894  }
895 
896  return CommandCost();
897 }
898 
907 {
908  Engine *e = Engine::GetIfValid(engine_id);
909  if (e == nullptr || !(e->flags & ENGINE_EXCLUSIVE_PREVIEW) || e->preview_company != _current_company) return CMD_ERROR;
910 
911  if (flags & DC_EXEC) AcceptEnginePreview(engine_id, _current_company);
912 
913  return CommandCost();
914 }
915 
924 CommandCost CmdEngineCtrl(DoCommandFlag flags, EngineID engine_id, CompanyID company_id, bool allow)
925 {
926  if (_current_company != OWNER_DEITY) return CMD_ERROR;
927 
928  if (!Engine::IsValidID(engine_id) || !Company::IsValidID(company_id)) return CMD_ERROR;
929 
930  if (flags & DC_EXEC) {
931  if (allow) {
932  EnableEngineForCompany(engine_id, company_id);
933  } else {
934  DisableEngineForCompany(engine_id, company_id);
935  }
936  }
937 
938  return CommandCost();
939 }
940 
947 {
948  EngineID index = e->index;
949 
950  /* In case the company didn't build the vehicle during the intro period,
951  * prevent that company from getting future intro periods for a while. */
952  if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
953  for (Company *c : Company::Iterate()) {
954  uint block_preview = c->block_preview;
955 
956  if (!HasBit(e->company_avail, c->index)) continue;
957 
958  /* We assume the user did NOT build it.. prove me wrong ;) */
959  c->block_preview = 20;
960 
961  for (const Vehicle *v : Vehicle::Iterate()) {
962  if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
963  (v->type == VEH_AIRCRAFT && Aircraft::From(v)->IsNormalAircraft())) {
964  if (v->owner == c->index && v->engine_type == index) {
965  /* The user did prove me wrong, so restore old value */
966  c->block_preview = block_preview;
967  break;
968  }
969  }
970  }
971  }
972  }
973 
976 
977  /* Now available for all companies */
978  e->company_avail = (CompanyMask)-1;
979 
980  /* Do not introduce new rail wagons */
981  if (IsWagon(index)) return;
982 
983  if (e->type == VEH_TRAIN) {
984  /* maybe make another rail type available */
985  assert(e->u.rail.railtype < RAILTYPE_END);
986  for (Company *c : Company::Iterate()) c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes | GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes, _date);
987  } else if (e->type == VEH_ROAD) {
988  /* maybe make another road type available */
989  assert(e->u.road.roadtype < ROADTYPE_END);
990  for (Company* c : Company::Iterate()) c->avail_roadtypes = AddDateIntroducedRoadTypes(c->avail_roadtypes | GetRoadTypeInfo(e->u.road.roadtype)->introduces_roadtypes, _date);
991  }
992 
993  /* Only broadcast event if AIs are able to build this vehicle type. */
994  if (!IsVehicleTypeDisabled(e->type, true)) AI::BroadcastNewEvent(new ScriptEventEngineAvailable(index));
995 
996  /* Only provide the "New Vehicle available" news paper entry, if engine can be built. */
997  if (!IsVehicleTypeDisabled(e->type, false)) {
998  SetDParam(0, GetEngineCategoryName(index));
999  SetDParam(1, index);
1000  AddNewsItem(STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NT_NEW_VEHICLES, NF_VEHICLE, NR_ENGINE, index);
1001  }
1002 
1003  /* Update the toolbar. */
1007 
1008  /* Close pending preview windows */
1010 }
1011 
1014 {
1016  for (Engine *e : Engine::Iterate()) {
1017  /* Age the vehicle */
1018  if ((e->flags & ENGINE_AVAILABLE) && e->age != MAX_DAY) {
1019  e->age++;
1021  }
1022 
1023  /* Do not introduce invalid engines */
1024  if (!e->IsEnabled()) continue;
1025 
1026  if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + DAYS_IN_YEAR)) {
1027  /* Introduce it to all companies */
1029  } else if (!(e->flags & (ENGINE_AVAILABLE | ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
1030  /* Introduction date has passed...
1031  * Check if it is allowed to build this vehicle type at all
1032  * based on the current game settings. If not, it does not
1033  * make sense to show the preview dialog to any company. */
1034  if (IsVehicleTypeDisabled(e->type, false)) continue;
1035 
1036  /* Do not introduce new rail wagons */
1037  if (IsWagon(e->index)) continue;
1038 
1039  /* Show preview dialog to one of the companies. */
1040  e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
1041  e->preview_company = INVALID_COMPANY;
1042  e->preview_asked = 0;
1043  }
1044  }
1045 
1046  InvalidateWindowClassesData(WC_BUILD_VEHICLE); // rebuild the purchase list (esp. when sorted by reliability)
1047  }
1048 }
1049 
1055 static bool IsUniqueEngineName(const std::string &name)
1056 {
1057  for (const Engine *e : Engine::Iterate()) {
1058  if (!e->name.empty() && e->name == name) return false;
1059  }
1060 
1061  return true;
1062 }
1063 
1071 CommandCost CmdRenameEngine(DoCommandFlag flags, EngineID engine_id, const std::string &text)
1072 {
1073  Engine *e = Engine::GetIfValid(engine_id);
1074  if (e == nullptr) return CMD_ERROR;
1075 
1076  bool reset = text.empty();
1077 
1078  if (!reset) {
1080  if (!IsUniqueEngineName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
1081  }
1082 
1083  if (flags & DC_EXEC) {
1084  if (reset) {
1085  e->name.clear();
1086  } else {
1087  e->name = text;
1088  }
1089 
1091  }
1092 
1093  return CommandCost();
1094 }
1095 
1096 
1106 {
1107  const Engine *e = Engine::GetIfValid(engine);
1108 
1109  /* check if it's an engine that is in the engine array */
1110  if (e == nullptr) return false;
1111 
1112  /* check if it's an engine of specified type */
1113  if (e->type != type) return false;
1114 
1115  /* check if it's available ... */
1116  if (company == OWNER_DEITY) {
1117  /* ... for any company (preview does not count) */
1118  if (!(e->flags & ENGINE_AVAILABLE) || e->company_avail == 0) return false;
1119  } else {
1120  /* ... for this company */
1121  if (!HasBit(e->company_avail, company)) return false;
1122  }
1123 
1124  if (!e->IsEnabled()) return false;
1125 
1126  if (type == VEH_TRAIN && company != OWNER_DEITY) {
1127  /* Check if the rail type is available to this company */
1128  const Company *c = Company::Get(company);
1129  if (((GetRailTypeInfo(e->u.rail.railtype))->compatible_railtypes & c->avail_railtypes) == 0) return false;
1130  }
1131  if (type == VEH_ROAD && company != OWNER_DEITY) {
1132  /* Check if the road type is available to this company */
1133  const Company *c = Company::Get(company);
1134  if ((GetRoadTypeInfo(e->u.road.roadtype)->powered_roadtypes & c->avail_roadtypes) == ROADTYPES_NONE) return false;
1135  }
1136 
1137  return true;
1138 }
1139 
1147 {
1148  const Engine *e = Engine::GetIfValid(engine);
1149 
1150  /* check if it's an engine that is in the engine array */
1151  if (e == nullptr) return false;
1152 
1153  if (!e->CanCarryCargo()) return false;
1154 
1155  const EngineInfo *ei = &e->info;
1156  if (ei->refit_mask == 0) return false;
1157 
1158  /* Are there suffixes?
1159  * Note: This does not mean the suffixes are actually available for every consist at any time. */
1160  if (HasBit(ei->callback_mask, CBM_VEHICLE_CARGO_SUFFIX)) return true;
1161 
1162  /* Is there any cargo except the default cargo? */
1163  CargoID default_cargo = e->GetDefaultCargoType();
1164  CargoTypes default_cargo_mask = 0;
1165  SetBit(default_cargo_mask, default_cargo);
1166  return default_cargo != CT_INVALID && ei->refit_mask != default_cargo_mask;
1167 }
1168 
1173 {
1174  Date min_date = INT32_MAX;
1175 
1176  for (const Engine *e : Engine::Iterate()) {
1177  if (!e->IsEnabled()) continue;
1178 
1179  /* We have an available engine... yay! */
1180  if ((e->flags & ENGINE_AVAILABLE) != 0 && e->company_avail != 0) return;
1181 
1182  /* Okay, try to find the earliest date. */
1183  min_date = std::min(min_date, e->info.base_intro);
1184  }
1185 
1186  if (min_date < INT32_MAX) {
1187  SetDParam(0, min_date);
1188  ShowErrorMessage(STR_ERROR_NO_VEHICLES_AVAILABLE_YET, STR_ERROR_NO_VEHICLES_AVAILABLE_YET_EXPLANATION, WL_WARNING);
1189  } else {
1190  ShowErrorMessage(STR_ERROR_NO_VEHICLES_AVAILABLE_AT_ALL, STR_ERROR_NO_VEHICLES_AVAILABLE_AT_ALL_EXPLANATION, WL_WARNING);
1191  }
1192 }
VEH_AIRCRAFT
@ VEH_AIRCRAFT
Aircraft vehicle type.
Definition: vehicle_type.h:27
AddDateIntroducedRailTypes
RailTypes AddDateIntroducedRailTypes(RailTypes current, Date date)
Add the rail types that are to be introduced at the given date.
Definition: rail.cpp:218
INVALID_ENGINE
static const EngineID INVALID_ENGINE
Constant denoting an invalid engine.
Definition: engine_type.h:175
EngineInfo::base_life
Year base_life
Basic duration of engine availability (without random parts). 0xFF means infinite life.
Definition: engine_type.h:136
IsCompanyBuildableVehicleType
static bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
Definition: vehicle_func.h:89
VehicleSettings::max_aircraft
UnitID max_aircraft
max planes in game per company
Definition: settings_type.h:494
Engine::GetGRFID
uint32 GetGRFID() const
Retrieve the GRF ID of the NewGRF the engine is tied to.
Definition: engine.cpp:150
InvalidateWindowData
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:3241
CheckEngines
void CheckEngines()
Check for engines that have an appropriate availability.
Definition: engine.cpp:1172
PROP_TRAIN_SPEED
@ PROP_TRAIN_SPEED
Max. speed: 1 unit = 1/1.6 mph = 1 km-ish/h.
Definition: newgrf_properties.h:21
GameCreationSettings::generation_seed
uint32 generation_seed
noise seed for world generation
Definition: settings_type.h:310
ROADTYPE_END
@ ROADTYPE_END
Used for iterations.
Definition: road_type.h:26
EngineOverrideManager::ResetToCurrentNewGRFConfig
static bool ResetToCurrentNewGRFConfig()
Tries to reset the engine mapping to match the current NewGRF configuration.
Definition: engine.cpp:516
Pool::PoolItem<&_engine_pool >::Get
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:337
WC_BUILD_TOOLBAR
@ WC_BUILD_TOOLBAR
Build toolbar; Window numbers:
Definition: window_type.h:66
EngineOverrideManager::ResetToDefaultMapping
void ResetToDefaultMapping()
Initializes the EngineOverrideManager with the default engines.
Definition: engine.cpp:476
PROP_TRAIN_CARGO_CAPACITY
@ PROP_TRAIN_CARGO_CAPACITY
Capacity (if dualheaded: for each single vehicle)
Definition: newgrf_properties.h:24
AISettings::ai_disable_veh_roadveh
bool ai_disable_veh_roadveh
disable types for AI
Definition: settings_type.h:369
PROP_ROADVEH_TRACTIVE_EFFORT
@ PROP_ROADVEH_TRACTIVE_EFFORT
Tractive effort coefficient in 1/256.
Definition: newgrf_properties.h:39
ReloadNewGRFData
void ReloadNewGRFData()
Reload all NewGRF files during a running game.
Definition: afterload.cpp:3234
EngineIDMapping::grfid
uint32 grfid
The GRF ID of the file the entity belongs to.
Definition: engine_base.h:168
Engine::reliability_max
uint16 reliability_max
Maximal reliability of the engine.
Definition: engine_base.h:34
GetPrice
Money GetPrice(Price index, uint cost_factor, const GRFFile *grf_file, int shift)
Determine a certain price.
Definition: economy.cpp:961
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
Engine::duration_phase_3
uint16 duration_phase_3
Third reliability phase in months, decaying to reliability_final.
Definition: engine_base.h:38
PROP_ROADVEH_COST_FACTOR
@ PROP_ROADVEH_COST_FACTOR
Purchase cost.
Definition: newgrf_properties.h:35
command_func.h
Pool::PoolItem<&_engine_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:348
CMD_ERROR
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
Definition: command_func.h:28
Company::avail_railtypes
RailTypes avail_railtypes
Rail types available to this company.
Definition: company_base.h:121
WL_WARNING
@ WL_WARNING
Other information.
Definition: error.h:23
PROP_TRAIN_RUNNING_COST_FACTOR
@ PROP_TRAIN_RUNNING_COST_FACTOR
Yearly runningcost (if dualheaded: sum of both vehicles)
Definition: newgrf_properties.h:23
GameCreationSettings::landscape
byte landscape
the landscape we're currently in
Definition: settings_type.h:326
Engine::reliability_spd_dec
uint16 reliability_spd_dec
Speed of reliability decay between services (per day).
Definition: engine_base.h:32
RoadTypeInfo::introduces_roadtypes
RoadTypes introduces_roadtypes
Bitmask of which other roadtypes are introduced when this roadtype is introduced.
Definition: road.h:174
Company::avail_roadtypes
RoadTypes avail_roadtypes
Road types available to this company.
Definition: company_base.h:122
_engine_counts
const uint8 _engine_counts[4]
Number of engines of each vehicle type in original engine data.
Definition: engine.cpp:51
company_base.h
IsWagon
static bool IsWagon(EngineID index)
Determine whether an engine type is a wagon (and not a loco).
Definition: engine.cpp:556
GetPreviewCompany
static CompanyID GetPreviewCompany(Engine *e)
Get the best company for an engine preview.
Definition: engine.cpp:781
_cur_year
Year _cur_year
Current year, starting at 0.
Definition: date.cpp:26
PROP_TRAIN_TRACTIVE_EFFORT
@ PROP_TRAIN_TRACTIVE_EFFORT
Tractive effort coefficient in 1/256.
Definition: newgrf_properties.h:27
EnginesDailyLoop
void EnginesDailyLoop()
Daily check to offer an exclusive engine preview to the companies.
Definition: engine.cpp:828
Pool::CleanPool
virtual void CleanPool()
Virtual method that deletes all items in the pool.
_year_engine_aging_stops
static Year _year_engine_aging_stops
Year that engine aging stops.
Definition: engine.cpp:48
Price
Price
Enumeration of all base prices for use with Prices.
Definition: economy_type.h:74
PROP_AIRCRAFT_MAIL_CAPACITY
@ PROP_AIRCRAFT_MAIL_CAPACITY
Mail Capacity.
Definition: newgrf_properties.h:53
Engine::company_avail
CompanyMask company_avail
Bit for each company whether the engine is available for that company.
Definition: engine_base.h:43
WC_ENGINE_PREVIEW
@ WC_ENGINE_PREVIEW
Engine preview window; Window numbers:
Definition: window_type.h:582
Engine::GetDisplayMaxTractiveEffort
uint GetDisplayMaxTractiveEffort() const
Returns the tractive effort of the engine for display purposes.
Definition: engine.cpp:417
VE_DEFAULT
@ VE_DEFAULT
Default value to indicate that visual effect should be based on engine class.
Definition: vehicle_base.h:96
SavedRandomSeeds
Stores the state of all random number generators.
Definition: random_func.hpp:33
Pool::PoolItem<&_engine_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:235
CargoSpec::Get
static CargoSpec * Get(size_t index)
Retrieve cargo details for the given cargo ID.
Definition: cargotype.h:118
PROP_SHIP_CARGO_CAPACITY
@ PROP_SHIP_CARGO_CAPACITY
Capacity.
Definition: newgrf_properties.h:45
Engine::duration_phase_1
uint16 duration_phase_1
First reliability phase in months, increasing reliability from reliability_start to reliability_max.
Definition: engine_base.h:36
PROP_AIRCRAFT_RUNNING_COST_FACTOR
@ PROP_AIRCRAFT_RUNNING_COST_FACTOR
Yearly runningcost.
Definition: newgrf_properties.h:51
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
EnableEngineForCompany
static void EnableEngineForCompany(EngineID eid, CompanyID company)
Allows engine eid to be used by a company company.
Definition: engine.cpp:709
SaveRandomSeeds
static void SaveRandomSeeds(SavedRandomSeeds *storage)
Saves the current seeds.
Definition: random_func.hpp:42
Engine::GetRunningCost
Money GetRunningCost() const
Return how much the running costs of this engine are.
Definition: engine.cpp:272
AddNewsItem
void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1=NR_NONE, uint32 ref1=UINT32_MAX, NewsReferenceType reftype2=NR_NONE, uint32 ref2=UINT32_MAX, const NewsAllocatedData *data=nullptr)
Add a new newsitem to be shown.
Definition: news_gui.cpp:827
ClrBit
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
Definition: bitmath_func.hpp:151
Engine::reliability_start
uint16 reliability_start
Initial reliability of the engine.
Definition: engine_base.h:33
GetEngineCategoryName
StringID GetEngineCategoryName(EngineID engine)
Return the category of an engine.
Definition: engine_gui.cpp:39
Year
int32 Year
Type for the year, note: 0 based, i.e. starts at the year 0.
Definition: date_type.h:18
autoreplace_gui.h
aircraft.h
SetupEngines
void SetupEngines()
Initialise the engine pool with the data from the original vehicles.
Definition: engine.cpp:532
AddDateIntroducedRoadTypes
RoadTypes AddDateIntroducedRoadTypes(RoadTypes current, Date date)
Add the road types that are to be introduced at the given date.
Definition: road.cpp:155
TRANSPORT_WATER
@ TRANSPORT_WATER
Transport over water.
Definition: transport_type.h:29
Engine::preview_company
CompanyID preview_company
Company which is currently being offered a preview INVALID_COMPANY means no company.
Definition: engine_base.h:41
GetCompanyRoadTypes
RoadTypes GetCompanyRoadTypes(CompanyID company, bool introduces)
Get the road types the given company can build.
Definition: road.cpp:188
StartupOneEngine
void StartupOneEngine(Engine *e, Date aging_date)
Start/initialise one engine.
Definition: engine.cpp:629
Engine::GetLifeLengthInDays
Date GetLifeLengthInDays() const
Returns the vehicle's (not model's!) life length in days.
Definition: engine.cpp:434
EngineInfo
Information about a vehicle.
Definition: engine_type.h:133
EngineInfo::base_intro
Date base_intro
Basic date of engine introduction (without random parts).
Definition: engine_type.h:134
CBM_VEHICLE_CARGO_SUFFIX
@ CBM_VEHICLE_CARGO_SUFFIX
Show suffix after cargo name.
Definition: newgrf_callbacks.h:294
Engine
Definition: engine_base.h:27
MAX_DAY
#define MAX_DAY
The number of days till the last day.
Definition: date_type.h:98
VEH_ROAD
@ VEH_ROAD
Road vehicle type.
Definition: vehicle_type.h:25
CC_PASSENGERS
@ CC_PASSENGERS
Passengers.
Definition: cargotype.h:41
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:224
EngineIDMapping::internal_id
uint16 internal_id
The internal ID within the GRF file.
Definition: engine_base.h:169
Engine::GetDefaultCargoType
CargoID GetDefaultCargoType() const
Determines the default cargo type of an engine.
Definition: engine_base.h:83
Engine::company_hidden
CompanyMask company_hidden
Bit for each company whether the engine is normally hidden in the build gui for that company.
Definition: engine_base.h:44
RoadVehicleInfo::roadtype
RoadType roadtype
Road type.
Definition: engine_type.h:126
StartupEngines
void StartupEngines()
Start/initialise all our engines.
Definition: engine.cpp:685
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
GetVehicleCallback
uint16 GetVehicleCallback(CallbackID callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v)
Evaluate a newgrf callback for vehicles.
Definition: newgrf_engine.cpp:1160
DC_EXEC
@ DC_EXEC
execute the given command
Definition: command_type.h:356
DisableEngineForCompany
static void DisableEngineForCompany(EngineID eid, CompanyID company)
Forbids engine eid to be used by a company company.
Definition: engine.cpp:737
EngineOverrideManager::GetID
EngineID GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
Looks up an EngineID in the EngineOverrideManager.
Definition: engine.cpp:499
SetDParam
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:196
DoCommandFlag
DoCommandFlag
List of flags for a command.
Definition: command_type.h:354
RestoreRandomSeeds
static void RestoreRandomSeeds(const SavedRandomSeeds &storage)
Restores previously saved seeds.
Definition: random_func.hpp:52
ShowErrorMessage
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:377
CountBits
static uint CountBits(T value)
Counts the number of set bits in a variable.
Definition: bitmath_func.hpp:251
VehicleSettings::extend_vehicle_life
byte extend_vehicle_life
extend vehicle life by this many years
Definition: settings_type.h:500
GameSettings::game_creation
GameCreationSettings game_creation
settings used during the creation of a game (map)
Definition: settings_type.h:584
CmdWantEnginePreview
CommandCost CmdWantEnginePreview(DoCommandFlag flags, EngineID engine_id)
Accept an engine prototype.
Definition: engine.cpp:906
NR_ENGINE
@ NR_ENGINE
Reference engine.
Definition: news_type.h:57
ai.hpp
EngineInfo::lifelength
Year lifelength
Lifetime of a single vehicle.
Definition: engine_type.h:135
Engine::GetGRF
const GRFFile * GetGRF() const
Retrieve the NewGRF the engine is tied to.
Definition: engine_base.h:142
IsUniqueEngineName
static bool IsUniqueEngineName(const std::string &name)
Is name still free as name for an engine?
Definition: engine.cpp:1055
GetRailTypeInfo
static const RailtypeInfo * GetRailTypeInfo(RailType railtype)
Returns a pointer to the Railtype information for a given railtype.
Definition: rail.h:304
ENGINE_EXCLUSIVE_PREVIEW
@ ENGINE_EXCLUSIVE_PREVIEW
This vehicle is in the exclusive preview stage, either being used or being offered to a company.
Definition: engine_type.h:170
Engine::GetDisplayMaxSpeed
uint GetDisplayMaxSpeed() const
Returns max speed of the engine for display purposes.
Definition: engine.cpp:349
Utf8StringLength
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:435
PROP_ROADVEH_SPEED
@ PROP_ROADVEH_SPEED
Max. speed: 1 unit = 1/0.8 mph = 2 km-ish/h.
Definition: newgrf_properties.h:38
IsVehicleTypeDisabled
static bool IsVehicleTypeDisabled(VehicleType type, bool ai)
Checks if a vehicle type is disabled for all/ai companies.
Definition: engine.cpp:815
EngineID
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
RoadTypeInfo::powered_roadtypes
RoadTypes powered_roadtypes
bitmask to the OTHER roadtypes on which a vehicle of THIS roadtype generates power
Definition: road.h:119
NT_NEW_VEHICLES
@ NT_NEW_VEHICLES
New vehicle has become available.
Definition: news_type.h:34
return_cmd_error
#define return_cmd_error(errcode)
Returns from a function with a specific StringID as error.
Definition: command_func.h:38
AISettings::ai_disable_veh_train
bool ai_disable_veh_train
disable types for AI
Definition: settings_type.h:368
AI::NewEvent
static void NewEvent(CompanyID company, ScriptEvent *event)
Queue a new event for an AI.
Definition: ai_core.cpp:234
CommandCost
Common return value for all commands.
Definition: command_type.h:24
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
GetUnionOfArticulatedRefitMasks
CargoTypes GetUnionOfArticulatedRefitMasks(EngineID engine, bool include_initial_cargo_type)
Ors the refit_masks of all articulated parts.
Definition: articulated_vehicles.cpp:255
newgrf_engine.h
EngineInfo::callback_mask
byte callback_mask
Bitmask of vehicle callbacks that have to be called.
Definition: engine_type.h:144
VehicleSettings::max_ships
UnitID max_ships
max ships in game per company
Definition: settings_type.h:495
CARGO_AGING_TICKS
static const int CARGO_AGING_TICKS
cycle duration for aging cargo
Definition: date_type.h:36
Vehicle::engine_type
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:302
SB
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.
Definition: bitmath_func.hpp:58
ConvertYMDToDate
Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
Definition: date.cpp:149
GROUND_ACCELERATION
static const int GROUND_ACCELERATION
Acceleration due to gravity, 9.8 m/s^2.
Definition: vehicle_type.h:18
MAX_LENGTH_ENGINE_NAME_CHARS
static const uint MAX_LENGTH_ENGINE_NAME_CHARS
The maximum length of an engine name in characters including '\0'.
Definition: engine_type.h:173
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:14
Engine::preview_asked
CompanyMask preview_asked
Bit for each company which has already been offered a preview.
Definition: engine_base.h:40
WC_REPLACE_VEHICLE
@ WC_REPLACE_VEHICLE
Replace vehicle window; Window numbers:
Definition: window_type.h:211
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:54
Engine::GetDisplayWeight
uint GetDisplayWeight() const
Returns the weight of the engine for display purposes.
Definition: engine.cpp:399
ConvertDateToYMD
void ConvertDateToYMD(Date date, YearMonthDay *ymd)
Converts a Date to a Year, Month & Day.
Definition: date.cpp:94
MAX_COMPANIES
@ MAX_COMPANIES
Maximum number of companies.
Definition: company_type.h:23
_local_company
CompanyID _local_company
Company controlled by the human player at this client. Can also be COMPANY_SPECTATOR.
Definition: company_cmd.cpp:46
AI::BroadcastNewEvent
static void BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company=MAX_COMPANIES)
Broadcast a new event to all active AIs.
Definition: ai_core.cpp:259
safeguards.h
IsInteractiveCompany
static bool IsInteractiveCompany(CompanyID company)
Is the user representing company?
Definition: company_func.h:53
CalcEngineReliability
static void CalcEngineReliability(Engine *e)
Update Engine::reliability and (if needed) update the engine GUIs.
Definition: engine.cpp:566
EnginesMonthlyLoop
void EnginesMonthlyLoop()
Monthly update of the availability, reliability, and preview offers of the engines.
Definition: engine.cpp:1013
error.h
EngineIDMapping
Definition: engine_base.h:167
EngineInfo::retire_early
int8 retire_early
Number of years early to retire vehicle.
Definition: engine_type.h:145
DAYS_IN_LEAP_YEAR
static const int DAYS_IN_LEAP_YEAR
sometimes, you need one day more...
Definition: date_type.h:30
EngineIDMapping::substitute_id
uint8 substitute_id
The (original) entity ID to use if this GRF is not available (currently not used)
Definition: engine_base.h:171
Engine::GetCost
Money GetCost() const
Return how much a new engine costs.
Definition: engine.cpp:309
date_func.h
stdafx.h
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
PROP_AIRCRAFT_COST_FACTOR
@ PROP_AIRCRAFT_COST_FACTOR
Purchase cost.
Definition: newgrf_properties.h:49
ENGINE_AVAILABLE
@ ENGINE_AVAILABLE
This vehicle is available to everyone.
Definition: engine_type.h:169
EngineInfo::misc_flags
byte misc_flags
Miscellaneous flags.
Definition: engine_type.h:143
PROP_SHIP_RUNNING_COST_FACTOR
@ PROP_SHIP_RUNNING_COST_FACTOR
Yearly runningcost.
Definition: newgrf_properties.h:46
RAILVEH_WAGON
@ RAILVEH_WAGON
simple wagon, not motorized
Definition: engine_type.h:29
CBM_VEHICLE_REFIT_CAPACITY
@ CBM_VEHICLE_REFIT_CAPACITY
Cargo capacity after refit.
Definition: newgrf_callbacks.h:292
Engine::reliability_final
uint16 reliability_final
Final reliability of the engine.
Definition: engine_base.h:35
_engine_offsets
const uint8 _engine_offsets[4]
Offset of the first engine of each vehicle type in original engine data.
Definition: engine.cpp:59
GameSettings::ai
AISettings ai
what may the AI do?
Definition: settings_type.h:586
YearMonthDay::year
Year year
Year (0...)
Definition: date_type.h:105
string_func.h
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition: newgrf_callbacks.h:404
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
AISettings::ai_disable_veh_ship
bool ai_disable_veh_ship
disable types for AI
Definition: settings_type.h:371
_current_company
CompanyID _current_company
Company currently doing an action.
Definition: company_cmd.cpp:47
vehicle_func.h
PROP_TRAIN_WEIGHT
@ PROP_TRAIN_WEIGHT
Weight in t (if dualheaded: for each single vehicle)
Definition: newgrf_properties.h:25
Engine::name
std::string name
Custom name of engine.
Definition: engine_base.h:28
Pool::PoolItem<&_vehicle_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
engine_gui.h
strings_func.h
Pool
Base class for all pools.
Definition: pool_type.hpp:81
Engine::CanCarryCargo
bool CanCarryCargo() const
Determines whether an engine can carry something.
Definition: engine.cpp:161
CmdSetVehicleVisibility
CommandCost CmdSetVehicleVisibility(DoCommandFlag flags, EngineID engine_id, bool hide)
Set the visibility of an engine.
Definition: engine.cpp:885
VehicleSettings::max_trains
UnitID max_trains
max trains in game per company
Definition: settings_type.h:492
SetRandomSeed
void SetRandomSeed(uint32 seed)
(Re)set the state of the random number generators.
Definition: random_func.cpp:65
WC_BUILD_VEHICLE
@ WC_BUILD_VEHICLE
Build vehicle; Window numbers:
Definition: window_type.h:376
SpecializedVehicle< Aircraft, VEH_AIRCRAFT >::From
static Aircraft * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition: vehicle_base.h:1183
Engine::GetPower
uint GetPower() const
Returns the power of the engine for display and sorting purposes.
Definition: engine.cpp:381
RAILTYPE_END
@ RAILTYPE_END
Used for iterations.
Definition: rail_type.h:33
EF_NO_DEFAULT_CARGO_MULTIPLIER
@ EF_NO_DEFAULT_CARGO_MULTIPLIER
Use the new capacity algorithm. The default cargotype of the vehicle does not affect capacity multipl...
Definition: engine_type.h:160
CloseWindowByClass
void CloseWindowByClass(WindowClass cls)
Close all windows of a given class.
Definition: window.cpp:1188
InvalidateWindowClassesData
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:3258
Aircraft::IsNormalAircraft
bool IsNormalAircraft() const
Check if the aircraft type is a normal flying device; eg not a rotor or a shadow.
Definition: aircraft.h:121
CmdRenameEngine
CommandCost CmdRenameEngine(DoCommandFlag flags, EngineID engine_id, const std::string &text)
Rename an engine.
Definition: engine.cpp:1071
EngineOverrideManager
Stores the mapping of EngineID to the internal id of newgrfs.
Definition: engine_base.h:178
RAILVEH_MULTIHEAD
@ RAILVEH_MULTIHEAD
indicates a combination of two locomotives
Definition: engine_type.h:28
GetCompanyRailtypes
RailTypes GetCompanyRailtypes(CompanyID company, bool introduces)
Get the rail types the given company can build.
Definition: rail.cpp:251
AISettings::ai_disable_veh_aircraft
bool ai_disable_veh_aircraft
disable types for AI
Definition: settings_type.h:370
newgrf.h
Pool::PoolItem<&_engine_pool >::CanAllocateItem
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
Definition: pool_type.hpp:307
DAYS_IN_YEAR
static const int DAYS_IN_YEAR
days per year
Definition: date_type.h:29
GetRoadTypeInfo
static const RoadTypeInfo * GetRoadTypeInfo(RoadType roadtype)
Returns a pointer to the Roadtype information for a given roadtype.
Definition: road.h:224
TRANSPORT_AIR
@ TRANSPORT_AIR
Transport through air.
Definition: transport_type.h:30
NewVehicleAvailable
static void NewVehicleAvailable(Engine *e)
An engine has become available for general use.
Definition: engine.cpp:946
PROP_ROADVEH_CARGO_CAPACITY
@ PROP_ROADVEH_CARGO_CAPACITY
Capacity.
Definition: newgrf_properties.h:34
OWNER_DEITY
@ OWNER_DEITY
The object is owned by a superuser / goal script.
Definition: company_type.h:27
GameCreationSettings::starting_year
Year starting_year
starting date
Definition: settings_type.h:311
company_func.h
VehicleSettings::max_roadveh
UnitID max_roadveh
max trucks in game per company
Definition: settings_type.h:493
Engine::original_image_index
uint8 original_image_index
Original vehicle image index, thus the image index of the overridden vehicle.
Definition: engine_base.h:45
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
CargoSpec::multiplier
uint16 multiplier
Capacity multiplier for vehicles. (8 fractional bits)
Definition: cargotype.h:63
AIR_CTOL
@ AIR_CTOL
Conventional Take Off and Landing, i.e. planes.
Definition: engine_type.h:93
window_func.h
NF_VEHICLE
@ NF_VEHICLE
Vehicle news item. (new engine available)
Definition: news_type.h:80
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:121
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:386
YearMonthDay
Data structure to convert between Date and triplet (year, month, and day).
Definition: date_type.h:104
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1752
CBID_VEHICLE_REFIT_CAPACITY
@ CBID_VEHICLE_REFIT_CAPACITY
Refit capacity, the passed vehicle needs to have its ->cargo_type set to the cargo we are refitting t...
Definition: newgrf_callbacks.h:48
random_func.hpp
Engine::DetermineCapacity
uint DetermineCapacity(const Vehicle *v, uint16 *mail_capacity=nullptr) const
Determines capacity of a given vehicle from scratch.
Definition: engine.cpp:194
OverflowSafeInt< int64 >
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
CloseWindowById
void CloseWindowById(WindowClass cls, WindowNumber number, bool force)
Close a window by its class and window number (if it is open).
Definition: window.cpp:1176
VehicleSettings::never_expire_vehicles
bool never_expire_vehicles
never expire vehicles
Definition: settings_type.h:499
Engine::IsEnabled
bool IsEnabled() const
Checks whether the engine is a valid (non-articulated part of an) engine.
Definition: engine.cpp:140
CmdEngineCtrl
CommandCost CmdEngineCtrl(DoCommandFlag flags, EngineID engine_id, CompanyID company_id, bool allow)
Allow or forbid a specific company to use an engine.
Definition: engine.cpp:924
INVALID_COMPANY
@ INVALID_COMPANY
An invalid company.
Definition: company_type.h:30
PROP_SHIP_COST_FACTOR
@ PROP_SHIP_COST_FACTOR
Purchase cost.
Definition: newgrf_properties.h:43
engine_base.h
TRANSPORT_ROAD
@ TRANSPORT_ROAD
Transport by road vehicle.
Definition: transport_type.h:28
Vehicle::cargo_type
CargoID cargo_type
type of cargo this vehicle is carrying
Definition: vehicle_base.h:320
Engine::grf_prop
GRFFilePropsBase< NUM_CARGO+2 > grf_prop
Properties related the the grf file.
Definition: engine_base.h:64
GRFFilePropsBase::local_id
uint16 local_id
id defined by the grf file for this entity
Definition: newgrf_commons.h:319
articulated_vehicles.h
GameSettings::vehicle
VehicleSettings vehicle
options for vehicles
Definition: settings_type.h:592
Vehicle::cargo_subtype
byte cargo_subtype
Used for livery refits (NewGRF variations)
Definition: vehicle_base.h:321
EngineInfo::climates
byte climates
Climates supported by the engine.
Definition: engine_type.h:139
PROP_SHIP_SPEED
@ PROP_SHIP_SPEED
Max. speed: 1 unit = 1/3.2 mph = 0.5 km-ish/h.
Definition: newgrf_properties.h:44
PROP_AIRCRAFT_PASSENGER_CAPACITY
@ PROP_AIRCRAFT_PASSENGER_CAPACITY
Passenger Capacity.
Definition: newgrf_properties.h:52
IsEngineRefittable
bool IsEngineRefittable(EngineID engine)
Check if an engine is refittable.
Definition: engine.cpp:1146
PROP_ROADVEH_POWER
@ PROP_ROADVEH_POWER
Power in 10 HP.
Definition: newgrf_properties.h:36
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
Pool::PoolItem<&_engine_pool >::IsValidID
static bool IsValidID(size_t index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
Definition: pool_type.hpp:326
PROP_TRAIN_COST_FACTOR
@ PROP_TRAIN_COST_FACTOR
Purchase cost (if dualheaded: sum of both vehicles)
Definition: newgrf_properties.h:26
PROP_AIRCRAFT_SPEED
@ PROP_AIRCRAFT_SPEED
Max. speed: 1 unit = 8 mph = 12.8 km-ish/h.
Definition: newgrf_properties.h:50
engines.h
Engine::GetRange
uint16 GetRange() const
Get the range of an aircraft type.
Definition: engine.cpp:444
EngineIDMapping::type
VehicleType type
The engine type.
Definition: engine_base.h:170
Engine::flags
byte flags
Flags of the engine.
Definition: engine_base.h:39
pool_func.hpp
EngineInfo::string_id
StringID string_id
Default name of engine.
Definition: engine_type.h:146
ClearEnginesHiddenFlagOfCompany
void ClearEnginesHiddenFlagOfCompany(CompanyID cid)
Clear the 'hidden' flag for all engines of a new company.
Definition: engine.cpp:871
CT_INVALID
@ CT_INVALID
Invalid cargo type.
Definition: cargo_type.h:69
IsCargoInClass
static bool IsCargoInClass(CargoID c, CargoClass cc)
Does cargo c have cargo class cc?
Definition: cargotype.h:200
PROP_TRAIN_POWER
@ PROP_TRAIN_POWER
Power in hp (if dualheaded: sum of both vehicles)
Definition: newgrf_properties.h:22
AddRemoveEngineFromAutoreplaceAndBuildWindows
void AddRemoveEngineFromAutoreplaceAndBuildWindows(VehicleType type)
When an engine is made buildable or is removed from being buildable, add/remove it from the build/aut...
Definition: autoreplace_gui.cpp:66
VEH_SHIP
@ VEH_SHIP
Ship vehicle type.
Definition: vehicle_type.h:26
ROADTYPES_NONE
@ ROADTYPES_NONE
No roadtypes.
Definition: road_type.h:37
Company
Definition: company_base.h:117
PROP_ROADVEH_RUNNING_COST_FACTOR
@ PROP_ROADVEH_RUNNING_COST_FACTOR
Yearly runningcost.
Definition: newgrf_properties.h:33
WC_MAIN_TOOLBAR
@ WC_MAIN_TOOLBAR
Main toolbar (the long bar at the top); Window numbers:
Definition: window_type.h:51
SetWindowClassesDirty
void SetWindowClassesDirty(WindowClass cls)
Mark all windows of a particular class as dirty (in need of repainting)
Definition: window.cpp:3169
PROP_AIRCRAFT_RANGE
@ PROP_AIRCRAFT_RANGE
Aircraft range.
Definition: newgrf_properties.h:55
EngineInfo::cargo_age_period
uint16 cargo_age_period
Number of ticks before carried cargo is aged.
Definition: engine_type.h:147
SetYearEngineAgingStops
void SetYearEngineAgingStops()
Compute the value for _year_engine_aging_stops.
Definition: engine.cpp:604
IsEngineBuildable
bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
Check if an engine is buildable.
Definition: engine.cpp:1105
Engine::intro_date
Date intro_date
Date of introduction of the engine.
Definition: engine_base.h:29
Engine::GetAircraftTypeText
StringID GetAircraftTypeText() const
Get the name of the aircraft type for display purposes.
Definition: engine.cpp:458
EngineOverrideManager::NUM_DEFAULT_ENGINES
static const uint NUM_DEFAULT_ENGINES
Number of default entries.
Definition: engine_base.h:179
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
Engine::reliability
uint16 reliability
Current reliability of the engine.
Definition: engine_base.h:31
Engine::type
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Definition: engine_base.h:46
AcceptEnginePreview
static void AcceptEnginePreview(EngineID eid, CompanyID company)
Company company accepts engine eid for preview.
Definition: engine.cpp:759
Engine::duration_phase_2
uint16 duration_phase_2
Second reliability phase in months, keeping reliability_max.
Definition: engine_base.h:37
PROP_ROADVEH_WEIGHT
@ PROP_ROADVEH_WEIGHT
Weight in 1/4 t.
Definition: newgrf_properties.h:37
engine_func.h
news_func.h
RailtypeInfo::introduces_railtypes
RailTypes introduces_railtypes
Bitmask of which other railtypes are introduced when this railtype is introduced.
Definition: rail.h:263