# engine.cpp

Go to the documentation of this file.

    00001 /* $Id: engine.cpp 15645 2009-03-08 16:51:08Z frosch $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "debug.h"
    00007 #include "company_func.h"
    00008 #include "command_func.h"
    00009 #include "news_func.h"
    00010 #include "variables.h"
    00011 #include "aircraft.h"
    00012 #include "newgrf_engine.h"
    00013 #include "group.h"
    00014 #include "strings_func.h"
    00015 #include "gfx_func.h"
    00016 #include "core/random_func.hpp"
    00017 #include "window_func.h"
    00018 #include "date_func.h"
    00019 #include "autoreplace_gui.h"
    00020 #include "string_func.h"
    00021 #include "oldpool_func.h"
    00022 #include "ai/ai.hpp"
    00023 #include "vehicle_func.h"
    00024 #include "settings_type.h"
    00025
    00026 #include "table/strings.h"
    00027 #include "table/engines.h"
    00028
    00029 DEFINE_OLD_POOL_GENERIC(Engine, Engine)
    00030
    00031 EngineOverrideManager _engine_mngr;
    00032
    00035 Year _year_engine_aging_stops;
    00036
    00038 const uint8 _engine_counts[4] = {
    00039   lengthof(_orig_rail_vehicle_info),
    00040   lengthof(_orig_road_vehicle_info),
    00041   lengthof(_orig_ship_vehicle_info),
    00042   lengthof(_orig_aircraft_vehicle_info),
    00043 };
    00044
    00046 const uint8 _engine_offsets[4] = {
    00047   0,
    00048   lengthof(_orig_rail_vehicle_info),
    00049   lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info),
    00050   lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info),
    00051 };
    00052
    00053 const uint EngineOverrideManager::NUM_DEFAULT_ENGINES = _engine_counts[VEH_TRAIN] + _engine_counts[VEH_ROAD] + _engine_counts[VEH_SHIP] + _engine_counts[VEH_AIRCRAFT];
    00054
    00055 Engine::Engine() :
    00056   name(NULL),
    00057   overrides_count(0),
    00058   overrides(NULL)
    00059 {
    00060 }
    00061
    00062 Engine::Engine(VehicleType type, EngineID base)
    00063 {
    00064   this->type = type;
    00065   this->internal_id = base;
    00066   this->list_position = base;
    00067
    00068   /* Check if this base engine is within the original engine data range */
    00069   if (base >= _engine_counts[type]) {
    00070     /* Mark engine as valid anyway */
    00071     this->info.climates = 0x80;
    00072     /* Set model life to maximum to make wagons available */
    00073     this->info.base_life = 0xFF;
    00074     return;
    00075   }
    00076
    00077   /* Copy the original engine info for this slot */
    00078   this->info = _orig_engine_info[_engine_offsets[type] + base];
    00079
    00080   /* Copy the original engine data for this slot */
    00081   switch (type) {
    00082     default: NOT_REACHED();
    00083
    00084     case VEH_TRAIN:
    00085       this->u.rail = _orig_rail_vehicle_info[base];
    00086       this->image_index = this->u.rail.image_index;
    00087       this->info.string_id = STR_8000_KIRBY_PAUL_TANK_STEAM + base;
    00088
    00089       /* Set the default model life of original wagons to "infinite" */
    00090       if (this->u.rail.railveh_type == RAILVEH_WAGON) this->info.base_life = 0xFF;
    00091
    00092       break;
    00093
    00094     case VEH_ROAD:
    00095       this->u.road = _orig_road_vehicle_info[base];
    00096       this->image_index = this->u.road.image_index;
    00097       this->info.string_id = STR_8074_MPS_REGAL_BUS + base;
    00098       break;
    00099
    00100     case VEH_SHIP:
    00101       this->u.ship = _orig_ship_vehicle_info[base];
    00102       this->image_index = this->u.ship.image_index;
    00103       this->info.string_id = STR_80CC_MPS_OIL_TANKER + base;
    00104       break;
    00105
    00106     case VEH_AIRCRAFT:
    00107       this->u.air = _orig_aircraft_vehicle_info[base];
    00108       this->image_index = this->u.air.image_index;
    00109       this->info.string_id = STR_80D7_SAMPSON_U52 + base;
    00110       break;
    00111   }
    00112 }
    00113
    00114 Engine::~Engine()
    00115 {
    00116   UnloadWagonOverrides(this);
    00117   free(this->name);
    00118 }
    00119
    00131 CargoID Engine::GetDefaultCargoType() const
    00132 {
    00133   switch (this->type) {
    00134     case VEH_TRAIN:
    00135       return this->u.rail.cargo_type;
    00136
    00137     case VEH_ROAD:
    00138       return this->u.road.cargo_type;
    00139
    00140     case VEH_SHIP:
    00141       return this->u.ship.cargo_type;
    00142
    00143     case VEH_AIRCRAFT:
    00144       return FindFirstRefittableCargo(this->index);
    00145
    00146     default: NOT_REACHED();
    00147   }
    00148 }
    00149
    00155 bool Engine::CanCarryCargo() const
    00156 {
    00157   /* For engines that can appear in a consist (i.e. rail vehicles and (articulated) road vehicles), a capacity
    00158    * of zero is a special case, to define the vehicle to not carry anything. The default cargotype is still used
    00159    * for livery selection etc.
    00160    * Note: Only the property is tested. A capacity callback returning 0 does not have the same effect.
    00161    */
    00162   switch (this->type) {
    00163     case VEH_TRAIN:
    00164       if (this->u.rail.capacity == 0) return false;
    00165       break;
    00166
    00167     case VEH_ROAD:
    00168       if (this->u.road.capacity == 0) return false;
    00169       break;
    00170
    00171     case VEH_SHIP:
    00172     case VEH_AIRCRAFT:
    00173       break;
    00174
    00175     default: NOT_REACHED();
    00176   }
    00177   return this->GetDefaultCargoType() != CT_INVALID;
    00178 }
    00179
    00180 Money Engine::GetRunningCost() const
    00181 {
    00182   switch (this->type) {
    00183     case VEH_ROAD:
    00184       return this->u.road.running_cost * GetPriceByIndex(this->u.road.running_cost_class) >> 8;
    00185
    00186     case VEH_TRAIN:
    00187       return GetEngineProperty(this->index, 0x0D, this->u.rail.running_cost) * GetPriceByIndex(this->u.rail.running_cost_class) >> 8;
    00188
    00189     case VEH_SHIP:
    00190       return GetEngineProperty(this->index, 0x0F, this->u.ship.running_cost) * _price.ship_running >> 8;
    00191
    00192     case VEH_AIRCRAFT:
    00193       return GetEngineProperty(this->index, 0x0E, this->u.air.running_cost) * _price.aircraft_running >> 8;
    00194
    00195     default: NOT_REACHED();
    00196   }
    00197 }
    00198
    00199 Money Engine::GetCost() const
    00200 {
    00201   switch (this->type) {
    00202     case VEH_ROAD:
    00203       return GetEngineProperty(this->index, 0x11, this->u.road.cost_factor) * (_price.roadveh_base >> 3) >> 5;
    00204
    00205     case VEH_TRAIN:
    00206       if (this->u.rail.railveh_type == RAILVEH_WAGON) {
    00207         return (GetEngineProperty(this->index, 0x17, this->u.rail.cost_factor) * _price.build_railwagon) >> 8;
    00208       } else {
    00209         return GetEngineProperty(this->index, 0x17, this->u.rail.cost_factor) * (_price.build_railvehicle >> 3) >> 5;
    00210       }
    00211     case VEH_SHIP:
    00212       return GetEngineProperty(this->index, 0x0A, this->u.ship.cost_factor) * (_price.ship_base >> 3) >> 5;
    00213
    00214     case VEH_AIRCRAFT:
    00215       return GetEngineProperty(this->index, 0x0B, this->u.air.cost_factor) * (_price.aircraft_base >> 3) >> 5;
    00216
    00217     default: NOT_REACHED();
    00218   }
    00219 }
    00220
    00225 uint Engine::GetDisplayMaxSpeed() const
    00226 {
    00227   switch (this->type) {
    00228     case VEH_TRAIN:
    00229       return GetEngineProperty(this->index, 0x09, this->u.rail.max_speed);
    00230
    00231     case VEH_ROAD:
    00232       return this->u.road.max_speed / 2;
    00233
    00234     case VEH_SHIP:
    00235       return GetEngineProperty(this->index, 0x0B, this->u.ship.max_speed) / 2;
    00236
    00237     case VEH_AIRCRAFT:
    00238       return this->u.air.max_speed;
    00239
    00240     default: NOT_REACHED();
    00241   }
    00242 }
    00243
    00244 uint Engine::GetPower() const
    00245 {
    00246   /* Currently only trains have 'power' */
    00247   switch (this->type) {
    00248     case VEH_TRAIN:
    00249       return GetEngineProperty(this->index, 0x0B, this->u.rail.power);
    00250
    00251     default: NOT_REACHED();
    00252   }
    00253 }
    00254
    00260 uint Engine::GetDisplayWeight() const
    00261 {
    00262   /* Currently only trains have 'weight' */
    00263   switch (this->type) {
    00264     case VEH_TRAIN:
    00265       return GetEngineProperty(this->index, 0x16, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0);
    00266
    00267     default: NOT_REACHED();
    00268   }
    00269 }
    00270
    00276 uint Engine::GetDisplayMaxTractiveEffort() const
    00277 {
    00278   /* Currently only trains have 'tractive effort' */
    00279   switch (this->type) {
    00280     case VEH_TRAIN:
    00281       return (10 * this->GetDisplayWeight() * GetEngineProperty(this->index, 0x1F, this->u.rail.tractive_effort)) / 256;
    00282
    00283     default: NOT_REACHED();
    00284   }
    00285 }
    00286
    00290 void EngineOverrideManager::ResetToDefaultMapping()
    00291 {
    00292   this->Clear();
    00293   for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) {
    00294     for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++) {
    00295       EngineIDMapping *eid = this->Append();
    00296       eid->type            = type;
    00297       eid->grfid           = INVALID_GRFID;
    00298       eid->internal_id     = internal_id;
    00299       eid->substitute_id   = internal_id;
    00300     }
    00301   }
    00302 }
    00303
    00313 EngineID EngineOverrideManager::GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
    00314 {
    00315   const EngineIDMapping *end = this->End();
    00316   EngineID index = 0;
    00317   for (const EngineIDMapping *eid = this->Begin(); eid != end; eid++, index++) {
    00318     if (eid->type == type && eid->grfid == grfid && eid->internal_id == grf_local_id) {
    00319       return index;
    00320     }
    00321   }
    00322   return INVALID_ENGINE;
    00323 }
    00324
    00327 void SetCachedEngineCounts()
    00328 {
    00329   uint engines = GetEnginePoolSize();
    00330
    00331   /* Set up the engine count for all companies */
    00332   Company *c;
    00333   FOR_ALL_COMPANIES(c) {
    00334     free(c->num_engines);
    00335     c->num_engines = CallocT<EngineID>(engines);
    00336   }
    00337
    00338   /* Recalculate */
    00339   Group *g;
    00340   FOR_ALL_GROUPS(g) {
    00341     free(g->num_engines);
    00342     g->num_engines = CallocT<EngineID>(engines);
    00343   }
    00344
    00345   const Vehicle *v;
    00346   FOR_ALL_VEHICLES(v) {
    00347     if (!IsEngineCountable(v)) continue;
    00348
    00349     assert(v->engine_type < engines);
    00350
    00351     GetCompany(v->owner)->num_engines[v->engine_type]++;
    00352
    00353     if (v->group_id == DEFAULT_GROUP) continue;
    00354
    00355     g = GetGroup(v->group_id);
    00356     assert(v->type == g->vehicle_type);
    00357     assert(v->owner == g->owner);
    00358
    00359     g->num_engines[v->engine_type]++;
    00360   }
    00361 }
    00362
    00363 void SetupEngines()
    00364 {
    00365   _Engine_pool.CleanPool();
    00366   _Engine_pool.AddBlockToPool();
    00367
    00368   assert(_engine_mngr.Length() >= _engine_mngr.NUM_DEFAULT_ENGINES);
    00369   const EngineIDMapping *end = _engine_mngr.End();
    00370   uint index = 0;
    00371   for (const EngineIDMapping *eid = _engine_mngr.Begin(); eid != end; eid++, index++) {
    00372     const Engine *e = new Engine(eid->type, eid->internal_id);
    00373     assert(e->index == index);
    00374   }
    00375 }
    00376
    00377
    00378 void ShowEnginePreviewWindow(EngineID engine);
    00379
    00380 /* Determine if an engine type is a wagon (and not a loco) */
    00381 static bool IsWagon(EngineID index)
    00382 {
    00383   const Engine *e = GetEngine(index);
    00384   return e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON;
    00385 }
    00386
    00387 static void CalcEngineReliability(Engine *e)
    00388 {
    00389   uint age = e->age;
    00390
    00391   /* Check for early retirement */
    00392   if (e->company_avail != 0 && !_settings_game.vehicle.never_expire_vehicles && e->info.base_life != 0xFF) {
    00393     int retire_early = e->info.retire_early;
    00394     uint retire_early_max_age = max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
    00395     if (retire_early != 0 && age >= retire_early_max_age) {
    00396       /* Early retirement is enabled and we're past the date... */
    00397       e->company_avail = 0;
    00398       AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
    00399     }
    00400   }
    00401
    00402   if (age < e->duration_phase_1) {
    00403     uint start = e->reliability_start;
    00404     e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
    00405   } else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _settings_game.vehicle.never_expire_vehicles || e->info.base_life == 0xFF) {
    00406     /* We are at the peak of this engines life. It will have max reliability.
    00407      * This is also true if the engines never expire. They will not go bad over time */
    00408     e->reliability = e->reliability_max;
    00409   } else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
    00410     uint max = e->reliability_max;
    00411     e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
    00412   } else {
    00413     /* time's up for this engine.
    00414      * We will now completely retire this design */
    00415     e->company_avail = 0;
    00416     e->reliability = e->reliability_final;
    00417     /* Kick this engine out of the lists */
    00418     AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
    00419   }
    00420   InvalidateWindowClasses(WC_BUILD_VEHICLE); // Update to show the new reliability
    00421   InvalidateWindowClasses(WC_REPLACE_VEHICLE);
    00422 }
    00423
    00424 void SetYearEngineAgingStops()
    00425 {
    00426   /* Determine last engine aging year, default to 2050 as previously. */
    00427   _year_engine_aging_stops = 2050;
    00428
    00429   const Engine *e;
    00430   FOR_ALL_ENGINES(e) {
    00431     const EngineInfo *ei = &e->info;
    00432
    00433     /* Exclude certain engines */
    00434     if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) continue;
    00435     if (e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON) continue;
    00436
    00437     /* Base year ending date on half the model life */
    00438     YearMonthDay ymd;
    00439     ConvertDateToYMD(ei->base_intro + (ei->lifelength * DAYS_IN_LEAP_YEAR) / 2, &ymd);
    00440
    00441     _year_engine_aging_stops = max(_year_engine_aging_stops, ymd.year);
    00442   }
    00443 }
    00444
    00445 void StartupOneEngine(Engine *e, Date aging_date)
    00446 {
    00447   const EngineInfo *ei = &e->info;
    00448   uint32 r;
    00449
    00450   e->age = 0;
    00451   e->flags = 0;
    00452   e->company_avail = 0;
    00453
    00454   /* The magic value of 729 days below comes from the NewGRF spec. If the
    00455    * base intro date is before 1922 then the random number of days is not
    00456    * added. */
    00457   r = Random();
    00458   e->intro_date = ei->base_intro <= ConvertYMDToDate(1922, 0, 1) ? ei->base_intro : (Date)GB(r, 0, 9) + ei->base_intro;
    00459   if (e->intro_date <= _date) {
    00460     e->age = (aging_date - e->intro_date) >> 5;
    00461     e->company_avail = (CompanyMask)-1;
    00462     e->flags |= ENGINE_AVAILABLE;
    00463   }
    00464
    00465   e->reliability_start = GB(r, 16, 14) + 0x7AE0;
    00466   r = Random();
    00467   e->reliability_max   = GB(r,  0, 14) + 0xBFFF;
    00468   e->reliability_final = GB(r, 16, 14) + 0x3FFF;
    00469
    00470   r = Random();
    00471   e->duration_phase_1 = GB(r, 0, 5) + 7;
    00472   e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
    00473   e->duration_phase_3 = GB(r, 9, 7) + 120;
    00474
    00475   e->reliability_spd_dec = ei->decay_speed << 2;
    00476
    00477   CalcEngineReliability(e);
    00478
    00479   e->lifelength = ei->lifelength + _settings_game.vehicle.extend_vehicle_life;
    00480
    00481   /* prevent certain engines from ever appearing. */
    00482   if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) {
    00483     e->flags |= ENGINE_AVAILABLE;
    00484     e->company_avail = 0;
    00485   }
    00486 }
    00487
    00488 void StartupEngines()
    00489 {
    00490   Engine *e;
    00491   /* Aging of vehicles stops, so account for that when starting late */
    00492   const Date aging_date = min(_date, ConvertYMDToDate(_year_engine_aging_stops, 0, 1));
    00493
    00494   FOR_ALL_ENGINES(e) {
    00495     StartupOneEngine(e, aging_date);
    00496   }
    00497
    00498   /* Update the bitmasks for the vehicle lists */
    00499   Company *c;
    00500   FOR_ALL_COMPANIES(c) {
    00501     c->avail_railtypes = GetCompanyRailtypes(c->index);
    00502     c->avail_roadtypes = GetCompanyRoadtypes(c->index);
    00503   }
    00504 }
    00505
    00506 static void AcceptEnginePreview(EngineID eid, CompanyID company)
    00507 {
    00508   Engine *e = GetEngine(eid);
    00509   Company *c = GetCompany(company);
    00510
    00511   SetBit(e->company_avail, company);
    00512   if (e->type == VEH_TRAIN) {
    00513     const RailVehicleInfo *rvi = RailVehInfo(eid);
    00514
    00515     assert(rvi->railtype < RAILTYPE_END);
    00516     SetBit(c->avail_railtypes, rvi->railtype);
    00517   } else if (e->type == VEH_ROAD) {
    00518     SetBit(c->avail_roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
    00519   }
    00520
    00521   e->preview_company_rank = 0xFF;
    00522   if (company == _local_company) {
    00523     AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
    00524   }
    00525 }
    00526
    00527 static CompanyID GetBestCompany(uint8 pp)
    00528 {
    00529   const Company *c;
    00530   int32 best_hist;
    00531   CompanyID best_company;
    00532   CompanyMask mask = 0;
    00533
    00534   do {
    00535     best_hist = -1;
    00536     best_company = INVALID_COMPANY;
    00537     FOR_ALL_COMPANIES(c) {
    00538       if (c->block_preview == 0 && !HasBit(mask, c->index) &&
    00539           c->old_economy[0].performance_history > best_hist) {
    00540         best_hist = c->old_economy[0].performance_history;
    00541         best_company = c->index;
    00542       }
    00543     }
    00544
    00545     if (best_company == INVALID_COMPANY) return INVALID_COMPANY;
    00546
    00547     SetBit(mask, best_company);
    00548   } while (--pp != 0);
    00549
    00550   return best_company;
    00551 }
    00552
    00553 void EnginesDailyLoop()
    00554 {
    00555   if (_cur_year >= _year_engine_aging_stops) return;
    00556
    00557   Engine *e;
    00558   FOR_ALL_ENGINES(e) {
    00559     EngineID i = e->index;
    00560     if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
    00561       if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
    00562         if (e->preview_company_rank != 0xFF && !--e->preview_wait) {
    00563           e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
    00564           DeleteWindowById(WC_ENGINE_PREVIEW, i);
    00565           e->preview_company_rank++;
    00566         }
    00567       } else if (e->preview_company_rank != 0xFF) {
    00568         CompanyID best_company = GetBestCompany(e->preview_company_rank);
    00569
    00570         if (best_company == INVALID_COMPANY) {
    00571           e->preview_company_rank = 0xFF;
    00572           continue;
    00573         }
    00574
    00575         e->flags |= ENGINE_OFFER_WINDOW_OPEN;
    00576         e->preview_wait = 20;
    00577         AI::NewEvent(best_company, new AIEventEnginePreview(i));
    00578         if (IsInteractiveCompany(best_company)) ShowEnginePreviewWindow(i);
    00579       }
    00580     }
    00581   }
    00582 }
    00583
    00591 CommandCost CmdWantEnginePreview(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00592 {
    00593   Engine *e;
    00594
    00595   if (!IsEngineIndex(p1)) return CMD_ERROR;
    00596   e = GetEngine(p1);
    00597   if (GetBestCompany(e->preview_company_rank) != _current_company) return CMD_ERROR;
    00598
    00599   if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_company);
    00600
    00601   return CommandCost();
    00602 }
    00603
    00604 StringID GetEngineCategoryName(EngineID engine);
    00605
    00606 static void NewVehicleAvailable(Engine *e)
    00607 {
    00608   Vehicle *v;
    00609   Company *c;
    00610   EngineID index = e->index;
    00611
    00612   /* In case the company didn't build the vehicle during the intro period,
    00613    * prevent that company from getting future intro periods for a while. */
    00614   if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
    00615     FOR_ALL_COMPANIES(c) {
    00616       uint block_preview = c->block_preview;
    00617
    00618       if (!HasBit(e->company_avail, c->index)) continue;
    00619
    00620       /* We assume the user did NOT build it.. prove me wrong ;) */
    00621       c->block_preview = 20;
    00622
    00623       FOR_ALL_VEHICLES(v) {
    00624         if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
    00625             (v->type == VEH_AIRCRAFT && IsNormalAircraft(v))) {
    00626           if (v->owner == c->index && v->engine_type == index) {
    00627             /* The user did prove me wrong, so restore old value */
    00628             c->block_preview = block_preview;
    00629             break;
    00630           }
    00631         }
    00632       }
    00633     }
    00634   }
    00635
    00636   e->flags = (e->flags & ~ENGINE_EXCLUSIVE_PREVIEW) | ENGINE_AVAILABLE;
    00637   AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
    00638
    00639   /* Now available for all companies */
    00640   e->company_avail = (CompanyMask)-1;
    00641
    00642   /* Do not introduce new rail wagons */
    00643   if (IsWagon(index)) return;
    00644
    00645   if (e->type == VEH_TRAIN) {
    00646     /* maybe make another rail type available */
    00647     RailType railtype = e->u.rail.railtype;
    00648     assert(railtype < RAILTYPE_END);
    00649     FOR_ALL_COMPANIES(c) SetBit(c->avail_railtypes, railtype);
    00650   } else if (e->type == VEH_ROAD) {
    00651     /* maybe make another road type available */
    00652     FOR_ALL_COMPANIES(c) SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
    00653   }
    00654
    00655   AI::BroadcastNewEvent(new AIEventEngineAvailable(index));
    00656
    00657   SetDParam(0, GetEngineCategoryName(index));
    00658   SetDParam(1, index);
    00659   AddNewsItem(STR_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NS_NEW_VEHICLES, index, 0);
    00660 }
    00661
    00662 void EnginesMonthlyLoop()
    00663 {
    00664   if (_cur_year < _year_engine_aging_stops) {
    00665     Engine *e;
    00666     FOR_ALL_ENGINES(e) {
    00667       /* Age the vehicle */
    00668       if (e->flags & ENGINE_AVAILABLE && e->age != 0xFFFF) {
    00669         e->age++;
    00670         CalcEngineReliability(e);
    00671       }
    00672
    00673       if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + DAYS_IN_YEAR)) {
    00674         /* Introduce it to all companies */
    00675         NewVehicleAvailable(e);
    00676       } else if (!(e->flags & (ENGINE_AVAILABLE|ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
    00677         /* Introduction date has passed.. show introducing dialog to one companies. */
    00678         e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
    00679
    00680         /* Do not introduce new rail wagons */
    00681         if (!IsWagon(e->index))
    00682           e->preview_company_rank = 1; // Give to the company with the highest rating.
    00683       }
    00684     }
    00685   }
    00686 }
    00687
    00688 static bool IsUniqueEngineName(const char *name)
    00689 {
    00690   const Engine *e;
    00691
    00692   FOR_ALL_ENGINES(e) {
    00693     if (e->name != NULL && strcmp(e->name, name) == 0) return false;
    00694   }
    00695
    00696   return true;
    00697 }
    00698
    00705 CommandCost CmdRenameEngine(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00706 {
    00707   if (!IsEngineIndex(p1)) return CMD_ERROR;
    00708
    00709   bool reset = StrEmpty(text);
    00710
    00711   if (!reset) {
    00712     if (strlen(text) >= MAX_LENGTH_ENGINE_NAME_BYTES) return CMD_ERROR;
    00713     if (!IsUniqueEngineName(text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
    00714   }
    00715
    00716   if (flags & DC_EXEC) {
    00717     Engine *e = GetEngine(p1);
    00718     free(e->name);
    00719
    00720     if (reset) {
    00721       e->name = NULL;
    00722     } else {
    00723       e->name = strdup(text);
    00724     }
    00725
    00726     MarkWholeScreenDirty();
    00727   }
    00728
    00729   return CommandCost();
    00730 }
    00731
    00732
    00740 bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
    00741 {
    00742   /* check if it's an engine that is in the engine array */
    00743   if (!IsEngineIndex(engine)) return false;
    00744
    00745   const Engine *e = GetEngine(engine);
    00746
    00747   /* check if it's an engine of specified type */
    00748   if (e->type != type) return false;
    00749
    00750   /* check if it's available */
    00751   if (!HasBit(e->company_avail, company)) return false;
    00752
    00753   if (type == VEH_TRAIN) {
    00754     /* Check if the rail type is available to this company */
    00755     const Company *c = GetCompany(company);
    00756     if (!HasBit(c->avail_railtypes, RailVehInfo(engine)->railtype)) return false;
    00757   }
    00758
    00759   return true;
    00760 }
    00761
    00768 bool IsEngineRefittable(EngineID engine)
    00769 {
    00770   /* check if it's an engine that is in the engine array */
    00771   if (!IsEngineIndex(engine)) return false;
    00772
    00773   const Engine *e = GetEngine(engine);
    00774
    00775   if (e->type == VEH_SHIP && !e->u.ship.refittable) return false;
    00776
    00777   if (!e->CanCarryCargo()) return false;
    00778
    00779   const EngineInfo *ei = &e->info;
    00780   if (ei->refit_mask == 0) return false;
    00781
    00782   /* Are there suffixes?
    00783    * Note: This does not mean the suffixes are actually available for every consist at any time. */
    00784   if (HasBit(ei->callbackmask, CBM_VEHICLE_CARGO_SUFFIX)) return true;
    00785
    00786   /* Is there any cargo except the default cargo? */
    00787   CargoID default_cargo = e->GetDefaultCargoType();
    00788   return default_cargo != CT_INVALID && ei->refit_mask != 1U << default_cargo;
    00789 }