engine.cpp

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

Generated on Mon Dec 14 20:59:58 2009 for OpenTTD by  doxygen 1.5.6