engine.cpp

Go to the documentation of this file.
00001 /* $Id: engine.cpp 15765 2009-03-18 19:50:34Z rubidium $ */
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 
00190 uint Engine::GetDisplayDefaultCapacity() const
00191 {
00192   if (!this->CanCarryCargo()) return 0;
00193   switch (type) {
00194     case VEH_TRAIN:
00195       return GetEngineProperty(this->index, 0x14, this->u.rail.capacity) + (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? this->u.rail.capacity : 0);
00196 
00197     case VEH_ROAD:
00198       return GetEngineProperty(this->index, 0x0F, this->u.road.capacity);
00199 
00200     case VEH_SHIP:
00201       return GetEngineProperty(this->index, 0x0D, this->u.ship.capacity);
00202 
00203     case VEH_AIRCRAFT:
00204       return AircraftDefaultCargoCapacity(this->GetDefaultCargoType(), &this->u.air);
00205 
00206     default: NOT_REACHED();
00207   }
00208 }
00209 
00210 Money Engine::GetRunningCost() const
00211 {
00212   switch (this->type) {
00213     case VEH_ROAD:
00214       return this->u.road.running_cost * GetPriceByIndex(this->u.road.running_cost_class) >> 8;
00215 
00216     case VEH_TRAIN:
00217       return GetEngineProperty(this->index, 0x0D, this->u.rail.running_cost) * GetPriceByIndex(this->u.rail.running_cost_class) >> 8;
00218 
00219     case VEH_SHIP:
00220       return GetEngineProperty(this->index, 0x0F, this->u.ship.running_cost) * _price.ship_running >> 8;
00221 
00222     case VEH_AIRCRAFT:
00223       return GetEngineProperty(this->index, 0x0E, this->u.air.running_cost) * _price.aircraft_running >> 8;
00224 
00225     default: NOT_REACHED();
00226   }
00227 }
00228 
00229 Money Engine::GetCost() const
00230 {
00231   switch (this->type) {
00232     case VEH_ROAD:
00233       return GetEngineProperty(this->index, 0x11, this->u.road.cost_factor) * (_price.roadveh_base >> 3) >> 5;
00234 
00235     case VEH_TRAIN:
00236       if (this->u.rail.railveh_type == RAILVEH_WAGON) {
00237         return (GetEngineProperty(this->index, 0x17, this->u.rail.cost_factor) * _price.build_railwagon) >> 8;
00238       } else {
00239         return GetEngineProperty(this->index, 0x17, this->u.rail.cost_factor) * (_price.build_railvehicle >> 3) >> 5;
00240       }
00241     case VEH_SHIP:
00242       return GetEngineProperty(this->index, 0x0A, this->u.ship.cost_factor) * (_price.ship_base >> 3) >> 5;
00243 
00244     case VEH_AIRCRAFT:
00245       return GetEngineProperty(this->index, 0x0B, this->u.air.cost_factor) * (_price.aircraft_base >> 3) >> 5;
00246 
00247     default: NOT_REACHED();
00248   }
00249 }
00250 
00255 uint Engine::GetDisplayMaxSpeed() const
00256 {
00257   switch (this->type) {
00258     case VEH_TRAIN:
00259       return GetEngineProperty(this->index, 0x09, this->u.rail.max_speed);
00260 
00261     case VEH_ROAD:
00262       return this->u.road.max_speed / 2;
00263 
00264     case VEH_SHIP:
00265       return GetEngineProperty(this->index, 0x0B, this->u.ship.max_speed) / 2;
00266 
00267     case VEH_AIRCRAFT:
00268       return this->u.air.max_speed;
00269 
00270     default: NOT_REACHED();
00271   }
00272 }
00273 
00274 uint Engine::GetPower() const
00275 {
00276   /* Currently only trains have 'power' */
00277   switch (this->type) {
00278     case VEH_TRAIN:
00279       return GetEngineProperty(this->index, 0x0B, this->u.rail.power);
00280 
00281     default: NOT_REACHED();
00282   }
00283 }
00284 
00290 uint Engine::GetDisplayWeight() const
00291 {
00292   /* Currently only trains have 'weight' */
00293   switch (this->type) {
00294     case VEH_TRAIN:
00295       return GetEngineProperty(this->index, 0x16, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0);
00296 
00297     default: NOT_REACHED();
00298   }
00299 }
00300 
00306 uint Engine::GetDisplayMaxTractiveEffort() const
00307 {
00308   /* Currently only trains have 'tractive effort' */
00309   switch (this->type) {
00310     case VEH_TRAIN:
00311       return (10 * this->GetDisplayWeight() * GetEngineProperty(this->index, 0x1F, this->u.rail.tractive_effort)) / 256;
00312 
00313     default: NOT_REACHED();
00314   }
00315 }
00316 
00320 void EngineOverrideManager::ResetToDefaultMapping()
00321 {
00322   this->Clear();
00323   for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) {
00324     for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++) {
00325       EngineIDMapping *eid = this->Append();
00326       eid->type            = type;
00327       eid->grfid           = INVALID_GRFID;
00328       eid->internal_id     = internal_id;
00329       eid->substitute_id   = internal_id;
00330     }
00331   }
00332 }
00333 
00343 EngineID EngineOverrideManager::GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
00344 {
00345   const EngineIDMapping *end = this->End();
00346   EngineID index = 0;
00347   for (const EngineIDMapping *eid = this->Begin(); eid != end; eid++, index++) {
00348     if (eid->type == type && eid->grfid == grfid && eid->internal_id == grf_local_id) {
00349       return index;
00350     }
00351   }
00352   return INVALID_ENGINE;
00353 }
00354 
00357 void SetCachedEngineCounts()
00358 {
00359   uint engines = GetEnginePoolSize();
00360 
00361   /* Set up the engine count for all companies */
00362   Company *c;
00363   FOR_ALL_COMPANIES(c) {
00364     free(c->num_engines);
00365     c->num_engines = CallocT<EngineID>(engines);
00366   }
00367 
00368   /* Recalculate */
00369   Group *g;
00370   FOR_ALL_GROUPS(g) {
00371     free(g->num_engines);
00372     g->num_engines = CallocT<EngineID>(engines);
00373   }
00374 
00375   const Vehicle *v;
00376   FOR_ALL_VEHICLES(v) {
00377     if (!IsEngineCountable(v)) continue;
00378 
00379     assert(v->engine_type < engines);
00380 
00381     GetCompany(v->owner)->num_engines[v->engine_type]++;
00382 
00383     if (v->group_id == DEFAULT_GROUP) continue;
00384 
00385     g = GetGroup(v->group_id);
00386     assert(v->type == g->vehicle_type);
00387     assert(v->owner == g->owner);
00388 
00389     g->num_engines[v->engine_type]++;
00390   }
00391 }
00392 
00393 void SetupEngines()
00394 {
00395   _Engine_pool.CleanPool();
00396   _Engine_pool.AddBlockToPool();
00397 
00398   assert(_engine_mngr.Length() >= _engine_mngr.NUM_DEFAULT_ENGINES);
00399   const EngineIDMapping *end = _engine_mngr.End();
00400   uint index = 0;
00401   for (const EngineIDMapping *eid = _engine_mngr.Begin(); eid != end; eid++, index++) {
00402     const Engine *e = new Engine(eid->type, eid->internal_id);
00403     assert(e->index == index);
00404   }
00405 }
00406 
00407 
00408 void ShowEnginePreviewWindow(EngineID engine);
00409 
00410 /* Determine if an engine type is a wagon (and not a loco) */
00411 static bool IsWagon(EngineID index)
00412 {
00413   const Engine *e = GetEngine(index);
00414   return e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON;
00415 }
00416 
00417 static void CalcEngineReliability(Engine *e)
00418 {
00419   uint age = e->age;
00420 
00421   /* Check for early retirement */
00422   if (e->company_avail != 0 && !_settings_game.vehicle.never_expire_vehicles && e->info.base_life != 0xFF) {
00423     int retire_early = e->info.retire_early;
00424     uint retire_early_max_age = max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
00425     if (retire_early != 0 && age >= retire_early_max_age) {
00426       /* Early retirement is enabled and we're past the date... */
00427       e->company_avail = 0;
00428       AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00429     }
00430   }
00431 
00432   if (age < e->duration_phase_1) {
00433     uint start = e->reliability_start;
00434     e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
00435   } else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _settings_game.vehicle.never_expire_vehicles || e->info.base_life == 0xFF) {
00436     /* We are at the peak of this engines life. It will have max reliability.
00437      * This is also true if the engines never expire. They will not go bad over time */
00438     e->reliability = e->reliability_max;
00439   } else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
00440     uint max = e->reliability_max;
00441     e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
00442   } else {
00443     /* time's up for this engine.
00444      * We will now completely retire this design */
00445     e->company_avail = 0;
00446     e->reliability = e->reliability_final;
00447     /* Kick this engine out of the lists */
00448     AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00449   }
00450   InvalidateWindowClasses(WC_BUILD_VEHICLE); // Update to show the new reliability
00451   InvalidateWindowClasses(WC_REPLACE_VEHICLE);
00452 }
00453 
00454 void SetYearEngineAgingStops()
00455 {
00456   /* Determine last engine aging year, default to 2050 as previously. */
00457   _year_engine_aging_stops = 2050;
00458 
00459   const Engine *e;
00460   FOR_ALL_ENGINES(e) {
00461     const EngineInfo *ei = &e->info;
00462 
00463     /* Exclude certain engines */
00464     if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) continue;
00465     if (e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON) continue;
00466 
00467     /* Base year ending date on half the model life */
00468     YearMonthDay ymd;
00469     ConvertDateToYMD(ei->base_intro + (ei->lifelength * DAYS_IN_LEAP_YEAR) / 2, &ymd);
00470 
00471     _year_engine_aging_stops = max(_year_engine_aging_stops, ymd.year);
00472   }
00473 }
00474 
00475 void StartupOneEngine(Engine *e, Date aging_date)
00476 {
00477   const EngineInfo *ei = &e->info;
00478   uint32 r;
00479 
00480   e->age = 0;
00481   e->flags = 0;
00482   e->company_avail = 0;
00483 
00484   /* The magic value of 729 days below comes from the NewGRF spec. If the
00485    * base intro date is before 1922 then the random number of days is not
00486    * added. */
00487   r = Random();
00488   e->intro_date = ei->base_intro <= ConvertYMDToDate(1922, 0, 1) ? ei->base_intro : (Date)GB(r, 0, 9) + ei->base_intro;
00489   if (e->intro_date <= _date) {
00490     e->age = (aging_date - e->intro_date) >> 5;
00491     e->company_avail = (CompanyMask)-1;
00492     e->flags |= ENGINE_AVAILABLE;
00493   }
00494 
00495   e->reliability_start = GB(r, 16, 14) + 0x7AE0;
00496   r = Random();
00497   e->reliability_max   = GB(r,  0, 14) + 0xBFFF;
00498   e->reliability_final = GB(r, 16, 14) + 0x3FFF;
00499 
00500   r = Random();
00501   e->duration_phase_1 = GB(r, 0, 5) + 7;
00502   e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
00503   e->duration_phase_3 = GB(r, 9, 7) + 120;
00504 
00505   e->reliability_spd_dec = ei->decay_speed << 2;
00506 
00507   CalcEngineReliability(e);
00508 
00509   e->lifelength = ei->lifelength + _settings_game.vehicle.extend_vehicle_life;
00510 
00511   /* prevent certain engines from ever appearing. */
00512   if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) {
00513     e->flags |= ENGINE_AVAILABLE;
00514     e->company_avail = 0;
00515   }
00516 }
00517 
00518 void StartupEngines()
00519 {
00520   Engine *e;
00521   /* Aging of vehicles stops, so account for that when starting late */
00522   const Date aging_date = min(_date, ConvertYMDToDate(_year_engine_aging_stops, 0, 1));
00523 
00524   FOR_ALL_ENGINES(e) {
00525     StartupOneEngine(e, aging_date);
00526   }
00527 
00528   /* Update the bitmasks for the vehicle lists */
00529   Company *c;
00530   FOR_ALL_COMPANIES(c) {
00531     c->avail_railtypes = GetCompanyRailtypes(c->index);
00532     c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00533   }
00534 }
00535 
00536 static void AcceptEnginePreview(EngineID eid, CompanyID company)
00537 {
00538   Engine *e = GetEngine(eid);
00539   Company *c = GetCompany(company);
00540 
00541   SetBit(e->company_avail, company);
00542   if (e->type == VEH_TRAIN) {
00543     const RailVehicleInfo *rvi = RailVehInfo(eid);
00544 
00545     assert(rvi->railtype < RAILTYPE_END);
00546     SetBit(c->avail_railtypes, rvi->railtype);
00547   } else if (e->type == VEH_ROAD) {
00548     SetBit(c->avail_roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00549   }
00550 
00551   e->preview_company_rank = 0xFF;
00552   if (company == _local_company) {
00553     AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00554   }
00555 }
00556 
00557 static CompanyID GetBestCompany(uint8 pp)
00558 {
00559   const Company *c;
00560   int32 best_hist;
00561   CompanyID best_company;
00562   CompanyMask mask = 0;
00563 
00564   do {
00565     best_hist = -1;
00566     best_company = INVALID_COMPANY;
00567     FOR_ALL_COMPANIES(c) {
00568       if (c->block_preview == 0 && !HasBit(mask, c->index) &&
00569           c->old_economy[0].performance_history > best_hist) {
00570         best_hist = c->old_economy[0].performance_history;
00571         best_company = c->index;
00572       }
00573     }
00574 
00575     if (best_company == INVALID_COMPANY) return INVALID_COMPANY;
00576 
00577     SetBit(mask, best_company);
00578   } while (--pp != 0);
00579 
00580   return best_company;
00581 }
00582 
00583 void EnginesDailyLoop()
00584 {
00585   if (_cur_year >= _year_engine_aging_stops) return;
00586 
00587   Engine *e;
00588   FOR_ALL_ENGINES(e) {
00589     EngineID i = e->index;
00590     if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
00591       if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
00592         if (e->preview_company_rank != 0xFF && !--e->preview_wait) {
00593           e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
00594           DeleteWindowById(WC_ENGINE_PREVIEW, i);
00595           e->preview_company_rank++;
00596         }
00597       } else if (e->preview_company_rank != 0xFF) {
00598         CompanyID best_company = GetBestCompany(e->preview_company_rank);
00599 
00600         if (best_company == INVALID_COMPANY) {
00601           e->preview_company_rank = 0xFF;
00602           continue;
00603         }
00604 
00605         e->flags |= ENGINE_OFFER_WINDOW_OPEN;
00606         e->preview_wait = 20;
00607         AI::NewEvent(best_company, new AIEventEnginePreview(i));
00608         if (IsInteractiveCompany(best_company)) ShowEnginePreviewWindow(i);
00609       }
00610     }
00611   }
00612 }
00613 
00621 CommandCost CmdWantEnginePreview(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00622 {
00623   Engine *e;
00624 
00625   if (!IsEngineIndex(p1)) return CMD_ERROR;
00626   e = GetEngine(p1);
00627   if (GetBestCompany(e->preview_company_rank) != _current_company) return CMD_ERROR;
00628 
00629   if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_company);
00630 
00631   return CommandCost();
00632 }
00633 
00634 StringID GetEngineCategoryName(EngineID engine);
00635 
00636 static void NewVehicleAvailable(Engine *e)
00637 {
00638   Vehicle *v;
00639   Company *c;
00640   EngineID index = e->index;
00641 
00642   /* In case the company didn't build the vehicle during the intro period,
00643    * prevent that company from getting future intro periods for a while. */
00644   if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
00645     FOR_ALL_COMPANIES(c) {
00646       uint block_preview = c->block_preview;
00647 
00648       if (!HasBit(e->company_avail, c->index)) continue;
00649 
00650       /* We assume the user did NOT build it.. prove me wrong ;) */
00651       c->block_preview = 20;
00652 
00653       FOR_ALL_VEHICLES(v) {
00654         if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
00655             (v->type == VEH_AIRCRAFT && IsNormalAircraft(v))) {
00656           if (v->owner == c->index && v->engine_type == index) {
00657             /* The user did prove me wrong, so restore old value */
00658             c->block_preview = block_preview;
00659             break;
00660           }
00661         }
00662       }
00663     }
00664   }
00665 
00666   e->flags = (e->flags & ~ENGINE_EXCLUSIVE_PREVIEW) | ENGINE_AVAILABLE;
00667   AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00668 
00669   /* Now available for all companies */
00670   e->company_avail = (CompanyMask)-1;
00671 
00672   /* Do not introduce new rail wagons */
00673   if (IsWagon(index)) return;
00674 
00675   if (e->type == VEH_TRAIN) {
00676     /* maybe make another rail type available */
00677     RailType railtype = e->u.rail.railtype;
00678     assert(railtype < RAILTYPE_END);
00679     FOR_ALL_COMPANIES(c) SetBit(c->avail_railtypes, railtype);
00680   } else if (e->type == VEH_ROAD) {
00681     /* maybe make another road type available */
00682     FOR_ALL_COMPANIES(c) SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00683   }
00684 
00685   AI::BroadcastNewEvent(new AIEventEngineAvailable(index));
00686 
00687   SetDParam(0, GetEngineCategoryName(index));
00688   SetDParam(1, index);
00689   AddNewsItem(STR_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NS_NEW_VEHICLES, index, 0);
00690 }
00691 
00692 void EnginesMonthlyLoop()
00693 {
00694   if (_cur_year < _year_engine_aging_stops) {
00695     Engine *e;
00696     FOR_ALL_ENGINES(e) {
00697       /* Age the vehicle */
00698       if (e->flags & ENGINE_AVAILABLE && e->age != 0xFFFF) {
00699         e->age++;
00700         CalcEngineReliability(e);
00701       }
00702 
00703       if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + DAYS_IN_YEAR)) {
00704         /* Introduce it to all companies */
00705         NewVehicleAvailable(e);
00706       } else if (!(e->flags & (ENGINE_AVAILABLE | ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
00707         /* Introduction date has passed.. show introducing dialog to one companies. */
00708         e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
00709 
00710         /* Do not introduce new rail wagons */
00711         if (!IsWagon(e->index))
00712           e->preview_company_rank = 1; // Give to the company with the highest rating.
00713       }
00714     }
00715   }
00716 }
00717 
00718 static bool IsUniqueEngineName(const char *name)
00719 {
00720   const Engine *e;
00721 
00722   FOR_ALL_ENGINES(e) {
00723     if (e->name != NULL && strcmp(e->name, name) == 0) return false;
00724   }
00725 
00726   return true;
00727 }
00728 
00735 CommandCost CmdRenameEngine(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00736 {
00737   if (!IsEngineIndex(p1)) return CMD_ERROR;
00738 
00739   bool reset = StrEmpty(text);
00740 
00741   if (!reset) {
00742     if (strlen(text) >= MAX_LENGTH_ENGINE_NAME_BYTES) return CMD_ERROR;
00743     if (!IsUniqueEngineName(text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
00744   }
00745 
00746   if (flags & DC_EXEC) {
00747     Engine *e = GetEngine(p1);
00748     free(e->name);
00749 
00750     if (reset) {
00751       e->name = NULL;
00752     } else {
00753       e->name = strdup(text);
00754     }
00755 
00756     MarkWholeScreenDirty();
00757   }
00758 
00759   return CommandCost();
00760 }
00761 
00762 
00770 bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
00771 {
00772   /* check if it's an engine that is in the engine array */
00773   if (!IsEngineIndex(engine)) return false;
00774 
00775   const Engine *e = GetEngine(engine);
00776 
00777   /* check if it's an engine of specified type */
00778   if (e->type != type) return false;
00779 
00780   /* check if it's available */
00781   if (!HasBit(e->company_avail, company)) return false;
00782 
00783   if (type == VEH_TRAIN) {
00784     /* Check if the rail type is available to this company */
00785     const Company *c = GetCompany(company);
00786     if (!HasBit(c->avail_railtypes, RailVehInfo(engine)->railtype)) return false;
00787   }
00788 
00789   return true;
00790 }
00791 
00798 bool IsEngineRefittable(EngineID engine)
00799 {
00800   /* check if it's an engine that is in the engine array */
00801   if (!IsEngineIndex(engine)) return false;
00802 
00803   const Engine *e = GetEngine(engine);
00804 
00805   if (e->type == VEH_SHIP && !e->u.ship.refittable) return false;
00806 
00807   if (!e->CanCarryCargo()) return false;
00808 
00809   const EngineInfo *ei = &e->info;
00810   if (ei->refit_mask == 0) return false;
00811 
00812   /* Are there suffixes?
00813    * Note: This does not mean the suffixes are actually available for every consist at any time. */
00814   if (HasBit(ei->callbackmask, CBM_VEHICLE_CARGO_SUFFIX)) return true;
00815 
00816   /* Is there any cargo except the default cargo? */
00817   CargoID default_cargo = e->GetDefaultCargoType();
00818   return default_cargo != CT_INVALID && ei->refit_mask != 1U << default_cargo;
00819 }

Generated on Mon May 11 15:48:03 2009 for OpenTTD by  doxygen 1.5.6