# engine.cpp

Go to the documentation of this file.

    00001 /* $Id: engine.cpp 15434 2009-02-09 21:20:05Z 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
    00033 Year _year_engine_aging_stops;
    00034
    00036 const uint8 _engine_counts[4] = {
    00037   lengthof(_orig_rail_vehicle_info),
    00038   lengthof(_orig_road_vehicle_info),
    00039   lengthof(_orig_ship_vehicle_info),
    00040   lengthof(_orig_aircraft_vehicle_info),
    00041 };
    00042
    00044 const uint8 _engine_offsets[4] = {
    00045   0,
    00046   lengthof(_orig_rail_vehicle_info),
    00047   lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info),
    00048   lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info),
    00049 };
    00050
    00051 Engine::Engine() :
    00052   name(NULL),
    00053   overrides_count(0),
    00054   overrides(NULL)
    00055 {
    00056 }
    00057
    00058 Engine::Engine(VehicleType type, EngineID base)
    00059 {
    00060   this->type = type;
    00061   this->internal_id = base;
    00062   this->list_position = base;
    00063
    00064   /* Check if this base engine is within the original engine data range */
    00065   if (base >= _engine_counts[type]) {
    00066     /* Mark engine as valid anyway */
    00067     this->info.climates = 0x80;
    00068     /* Set model life to maximum to make wagons available */
    00069     this->info.base_life = 0xFF;
    00070     return;
    00071   }
    00072
    00073   /* Copy the original engine info for this slot */
    00074   this->info = _orig_engine_info[_engine_offsets[type] + base];
    00075
    00076   /* Copy the original engine data for this slot */
    00077   switch (type) {
    00078     default: NOT_REACHED();
    00079
    00080     case VEH_TRAIN:
    00081       this->u.rail = _orig_rail_vehicle_info[base];
    00082       this->image_index = this->u.rail.image_index;
    00083       this->info.string_id = STR_8000_KIRBY_PAUL_TANK_STEAM + base;
    00084
    00085       /* Set the default model life of original wagons to "infinite" */
    00086       if (this->u.rail.railveh_type == RAILVEH_WAGON) this->info.base_life = 0xFF;
    00087
    00088       break;
    00089
    00090     case VEH_ROAD:
    00091       this->u.road = _orig_road_vehicle_info[base];
    00092       this->image_index = this->u.road.image_index;
    00093       this->info.string_id = STR_8074_MPS_REGAL_BUS + base;
    00094       break;
    00095
    00096     case VEH_SHIP:
    00097       this->u.ship = _orig_ship_vehicle_info[base];
    00098       this->image_index = this->u.ship.image_index;
    00099       this->info.string_id = STR_80CC_MPS_OIL_TANKER + base;
    00100       break;
    00101
    00102     case VEH_AIRCRAFT:
    00103       this->u.air = _orig_aircraft_vehicle_info[base];
    00104       this->image_index = this->u.air.image_index;
    00105       this->info.string_id = STR_80D7_SAMPSON_U52 + base;
    00106       break;
    00107   }
    00108 }
    00109
    00110 Engine::~Engine()
    00111 {
    00112   UnloadWagonOverrides(this);
    00113   free(this->name);
    00114 }
    00115
    00116 Money Engine::GetRunningCost() const
    00117 {
    00118   switch (this->type) {
    00119     case VEH_ROAD:
    00120       return this->u.road.running_cost * GetPriceByIndex(this->u.road.running_cost_class) >> 8;
    00121
    00122     case VEH_TRAIN:
    00123       return GetEngineProperty(this->index, 0x0D, this->u.rail.running_cost) * GetPriceByIndex(this->u.rail.running_cost_class) >> 8;
    00124
    00125     case VEH_SHIP:
    00126       return GetEngineProperty(this->index, 0x0F, this->u.ship.running_cost) * _price.ship_running >> 8;
    00127
    00128     case VEH_AIRCRAFT:
    00129       return GetEngineProperty(this->index, 0x0E, this->u.air.running_cost) * _price.aircraft_running >> 8;
    00130
    00131     default: NOT_REACHED();
    00132   }
    00133 }
    00134
    00135 Money Engine::GetCost() const
    00136 {
    00137   switch (this->type) {
    00138     case VEH_ROAD:
    00139       return GetEngineProperty(this->index, 0x11, this->u.road.cost_factor) * (_price.roadveh_base >> 3) >> 5;
    00140
    00141     case VEH_TRAIN:
    00142       if (this->u.rail.railveh_type == RAILVEH_WAGON) {
    00143         return (GetEngineProperty(this->index, 0x17, this->u.rail.cost_factor) * _price.build_railwagon) >> 8;
    00144       } else {
    00145         return GetEngineProperty(this->index, 0x17, this->u.rail.cost_factor) * (_price.build_railvehicle >> 3) >> 5;
    00146       }
    00147     case VEH_SHIP:
    00148       return GetEngineProperty(this->index, 0x0A, this->u.ship.cost_factor) * (_price.ship_base >> 3) >> 5;
    00149
    00150     case VEH_AIRCRAFT:
    00151       return GetEngineProperty(this->index, 0x0B, this->u.air.cost_factor) * (_price.aircraft_base >> 3) >> 5;
    00152
    00153     default: NOT_REACHED();
    00154   }
    00155 }
    00156
    00161 uint Engine::GetDisplayMaxSpeed() const
    00162 {
    00163   switch (this->type) {
    00164     case VEH_TRAIN:
    00165       return GetEngineProperty(this->index, 0x09, this->u.rail.max_speed);
    00166
    00167     case VEH_ROAD:
    00168       return this->u.road.max_speed / 2;
    00169
    00170     case VEH_SHIP:
    00171       return GetEngineProperty(this->index, 0x0B, this->u.ship.max_speed) / 2;
    00172
    00173     case VEH_AIRCRAFT:
    00174       return this->u.air.max_speed;
    00175
    00176     default: NOT_REACHED();
    00177   }
    00178 }
    00179
    00180 uint Engine::GetPower() const
    00181 {
    00182   /* Currently only trains have 'power' */
    00183   switch (this->type) {
    00184     case VEH_TRAIN:
    00185       return GetEngineProperty(this->index, 0x0B, this->u.rail.power);
    00186
    00187     default: NOT_REACHED();
    00188   }
    00189 }
    00190
    00196 uint Engine::GetDisplayWeight() const
    00197 {
    00198   /* Currently only trains have 'weight' */
    00199   switch (this->type) {
    00200     case VEH_TRAIN:
    00201       return GetEngineProperty(this->index, 0x16, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0);
    00202
    00203     default: NOT_REACHED();
    00204   }
    00205 }
    00206
    00209 void SetCachedEngineCounts()
    00210 {
    00211   uint engines = GetEnginePoolSize();
    00212
    00213   /* Set up the engine count for all companies */
    00214   Company *c;
    00215   FOR_ALL_COMPANIES(c) {
    00216     free(c->num_engines);
    00217     c->num_engines = CallocT<EngineID>(engines);
    00218   }
    00219
    00220   /* Recalculate */
    00221   Group *g;
    00222   FOR_ALL_GROUPS(g) {
    00223     free(g->num_engines);
    00224     g->num_engines = CallocT<EngineID>(engines);
    00225   }
    00226
    00227   const Vehicle *v;
    00228   FOR_ALL_VEHICLES(v) {
    00229     if (!IsEngineCountable(v)) continue;
    00230
    00231     assert(v->engine_type < engines);
    00232
    00233     GetCompany(v->owner)->num_engines[v->engine_type]++;
    00234
    00235     if (v->group_id == DEFAULT_GROUP) continue;
    00236
    00237     g = GetGroup(v->group_id);
    00238     assert(v->type == g->vehicle_type);
    00239     assert(v->owner == g->owner);
    00240
    00241     g->num_engines[v->engine_type]++;
    00242   }
    00243 }
    00244
    00245 void SetupEngines()
    00246 {
    00247   _Engine_pool.CleanPool();
    00248   _Engine_pool.AddBlockToPool();
    00249
    00250   for (uint i = 0; i < lengthof(_orig_rail_vehicle_info); i++) new Engine(VEH_TRAIN, i);
    00251   for (uint i = 0; i < lengthof(_orig_road_vehicle_info); i++) new Engine(VEH_ROAD, i);
    00252   for (uint i = 0; i < lengthof(_orig_ship_vehicle_info); i++) new Engine(VEH_SHIP, i);
    00253   for (uint i = 0; i < lengthof(_orig_aircraft_vehicle_info); i++) new Engine(VEH_AIRCRAFT, i);
    00254 }
    00255
    00256
    00257 void ShowEnginePreviewWindow(EngineID engine);
    00258
    00259 /* Determine if an engine type is a wagon (and not a loco) */
    00260 static bool IsWagon(EngineID index)
    00261 {
    00262   const Engine *e = GetEngine(index);
    00263   return e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON;
    00264 }
    00265
    00266 static void CalcEngineReliability(Engine *e)
    00267 {
    00268   uint age = e->age;
    00269
    00270   /* Check for early retirement */
    00271   if (e->company_avail != 0 && !_settings_game.vehicle.never_expire_vehicles && e->info.base_life != 0xFF) {
    00272     int retire_early = e->info.retire_early;
    00273     uint retire_early_max_age = max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
    00274     if (retire_early != 0 && age >= retire_early_max_age) {
    00275       /* Early retirement is enabled and we're past the date... */
    00276       e->company_avail = 0;
    00277       AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
    00278     }
    00279   }
    00280
    00281   if (age < e->duration_phase_1) {
    00282     uint start = e->reliability_start;
    00283     e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
    00284   } else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _settings_game.vehicle.never_expire_vehicles || e->info.base_life == 0xFF) {
    00285     /* We are at the peak of this engines life. It will have max reliability.
    00286      * This is also true if the engines never expire. They will not go bad over time */
    00287     e->reliability = e->reliability_max;
    00288   } else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
    00289     uint max = e->reliability_max;
    00290     e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
    00291   } else {
    00292     /* time's up for this engine.
    00293      * We will now completely retire this design */
    00294     e->company_avail = 0;
    00295     e->reliability = e->reliability_final;
    00296     /* Kick this engine out of the lists */
    00297     AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
    00298   }
    00299   InvalidateWindowClasses(WC_BUILD_VEHICLE); // Update to show the new reliability
    00300   InvalidateWindowClasses(WC_REPLACE_VEHICLE);
    00301 }
    00302
    00303 void SetYearEngineAgingStops()
    00304 {
    00305   /* Determine last engine aging year, default to 2050 as previously. */
    00306   _year_engine_aging_stops = 2050;
    00307
    00308   const Engine *e;
    00309   FOR_ALL_ENGINES(e) {
    00310     const EngineInfo *ei = &e->info;
    00311
    00312     /* Exclude certain engines */
    00313     if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) continue;
    00314     if (e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON) continue;
    00315
    00316     /* Base year ending date on half the model life */
    00317     YearMonthDay ymd;
    00318     ConvertDateToYMD(ei->base_intro + (ei->lifelength * DAYS_IN_LEAP_YEAR) / 2, &ymd);
    00319
    00320     _year_engine_aging_stops = max(_year_engine_aging_stops, ymd.year);
    00321   }
    00322 }
    00323
    00324 void StartupOneEngine(Engine *e, Date aging_date)
    00325 {
    00326   const EngineInfo *ei = &e->info;
    00327   uint32 r;
    00328
    00329   e->age = 0;
    00330   e->flags = 0;
    00331   e->company_avail = 0;
    00332
    00333   /* The magic value of 729 days below comes from the NewGRF spec. If the
    00334    * base intro date is before 1922 then the random number of days is not
    00335    * added. */
    00336   r = Random();
    00337   e->intro_date = ei->base_intro <= ConvertYMDToDate(1922, 0, 1) ? ei->base_intro : (Date)GB(r, 0, 9) + ei->base_intro;
    00338   if (e->intro_date <= _date) {
    00339     e->age = (aging_date - e->intro_date) >> 5;
    00340     e->company_avail = (CompanyMask)-1;
    00341     e->flags |= ENGINE_AVAILABLE;
    00342   }
    00343
    00344   e->reliability_start = GB(r, 16, 14) + 0x7AE0;
    00345   r = Random();
    00346   e->reliability_max   = GB(r,  0, 14) + 0xBFFF;
    00347   e->reliability_final = GB(r, 16, 14) + 0x3FFF;
    00348
    00349   r = Random();
    00350   e->duration_phase_1 = GB(r, 0, 5) + 7;
    00351   e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
    00352   e->duration_phase_3 = GB(r, 9, 7) + 120;
    00353
    00354   e->reliability_spd_dec = ei->decay_speed << 2;
    00355
    00356   CalcEngineReliability(e);
    00357
    00358   e->lifelength = ei->lifelength + _settings_game.vehicle.extend_vehicle_life;
    00359
    00360   /* prevent certain engines from ever appearing. */
    00361   if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) {
    00362     e->flags |= ENGINE_AVAILABLE;
    00363     e->company_avail = 0;
    00364   }
    00365 }
    00366
    00367 void StartupEngines()
    00368 {
    00369   Engine *e;
    00370   /* Aging of vehicles stops, so account for that when starting late */
    00371   const Date aging_date = min(_date, ConvertYMDToDate(_year_engine_aging_stops, 0, 1));
    00372
    00373   FOR_ALL_ENGINES(e) {
    00374     StartupOneEngine(e, aging_date);
    00375   }
    00376
    00377   /* Update the bitmasks for the vehicle lists */
    00378   Company *c;
    00379   FOR_ALL_COMPANIES(c) {
    00380     c->avail_railtypes = GetCompanyRailtypes(c->index);
    00381     c->avail_roadtypes = GetCompanyRoadtypes(c->index);
    00382   }
    00383 }
    00384
    00385 static void AcceptEnginePreview(EngineID eid, CompanyID company)
    00386 {
    00387   Engine *e = GetEngine(eid);
    00388   Company *c = GetCompany(company);
    00389
    00390   SetBit(e->company_avail, company);
    00391   if (e->type == VEH_TRAIN) {
    00392     const RailVehicleInfo *rvi = RailVehInfo(eid);
    00393
    00394     assert(rvi->railtype < RAILTYPE_END);
    00395     SetBit(c->avail_railtypes, rvi->railtype);
    00396   } else if (e->type == VEH_ROAD) {
    00397     SetBit(c->avail_roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
    00398   }
    00399
    00400   e->preview_company_rank = 0xFF;
    00401   if (company == _local_company) {
    00402     AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
    00403   }
    00404 }
    00405
    00406 static CompanyID GetBestCompany(uint8 pp)
    00407 {
    00408   const Company *c;
    00409   int32 best_hist;
    00410   CompanyID best_company;
    00411   CompanyMask mask = 0;
    00412
    00413   do {
    00414     best_hist = -1;
    00415     best_company = INVALID_COMPANY;
    00416     FOR_ALL_COMPANIES(c) {
    00417       if (c->block_preview == 0 && !HasBit(mask, c->index) &&
    00418           c->old_economy[0].performance_history > best_hist) {
    00419         best_hist = c->old_economy[0].performance_history;
    00420         best_company = c->index;
    00421       }
    00422     }
    00423
    00424     if (best_company == INVALID_COMPANY) return INVALID_COMPANY;
    00425
    00426     SetBit(mask, best_company);
    00427   } while (--pp != 0);
    00428
    00429   return best_company;
    00430 }
    00431
    00432 void EnginesDailyLoop()
    00433 {
    00434   if (_cur_year >= _year_engine_aging_stops) return;
    00435
    00436   Engine *e;
    00437   FOR_ALL_ENGINES(e) {
    00438     EngineID i = e->index;
    00439     if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
    00440       if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
    00441         if (e->preview_company_rank != 0xFF && !--e->preview_wait) {
    00442           e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
    00443           DeleteWindowById(WC_ENGINE_PREVIEW, i);
    00444           e->preview_company_rank++;
    00445         }
    00446       } else if (e->preview_company_rank != 0xFF) {
    00447         CompanyID best_company = GetBestCompany(e->preview_company_rank);
    00448
    00449         if (best_company == INVALID_COMPANY) {
    00450           e->preview_company_rank = 0xFF;
    00451           continue;
    00452         }
    00453
    00454         e->flags |= ENGINE_OFFER_WINDOW_OPEN;
    00455         e->preview_wait = 20;
    00456         AI::NewEvent(best_company, new AIEventEnginePreview(i));
    00457         if (IsInteractiveCompany(best_company)) ShowEnginePreviewWindow(i);
    00458       }
    00459     }
    00460   }
    00461 }
    00462
    00470 CommandCost CmdWantEnginePreview(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00471 {
    00472   Engine *e;
    00473
    00474   if (!IsEngineIndex(p1)) return CMD_ERROR;
    00475   e = GetEngine(p1);
    00476   if (GetBestCompany(e->preview_company_rank) != _current_company) return CMD_ERROR;
    00477
    00478   if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_company);
    00479
    00480   return CommandCost();
    00481 }
    00482
    00483 StringID GetEngineCategoryName(EngineID engine);
    00484
    00485 static void NewVehicleAvailable(Engine *e)
    00486 {
    00487   Vehicle *v;
    00488   Company *c;
    00489   EngineID index = e->index;
    00490
    00491   /* In case the company didn't build the vehicle during the intro period,
    00492    * prevent that company from getting future intro periods for a while. */
    00493   if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
    00494     FOR_ALL_COMPANIES(c) {
    00495       uint block_preview = c->block_preview;
    00496
    00497       if (!HasBit(e->company_avail, c->index)) continue;
    00498
    00499       /* We assume the user did NOT build it.. prove me wrong ;) */
    00500       c->block_preview = 20;
    00501
    00502       FOR_ALL_VEHICLES(v) {
    00503         if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
    00504             (v->type == VEH_AIRCRAFT && IsNormalAircraft(v))) {
    00505           if (v->owner == c->index && v->engine_type == index) {
    00506             /* The user did prove me wrong, so restore old value */
    00507             c->block_preview = block_preview;
    00508             break;
    00509           }
    00510         }
    00511       }
    00512     }
    00513   }
    00514
    00515   e->flags = (e->flags & ~ENGINE_EXCLUSIVE_PREVIEW) | ENGINE_AVAILABLE;
    00516   AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
    00517
    00518   /* Now available for all companies */
    00519   e->company_avail = (CompanyMask)-1;
    00520
    00521   /* Do not introduce new rail wagons */
    00522   if (IsWagon(index)) return;
    00523
    00524   if (e->type == VEH_TRAIN) {
    00525     /* maybe make another rail type available */
    00526     RailType railtype = e->u.rail.railtype;
    00527     assert(railtype < RAILTYPE_END);
    00528     FOR_ALL_COMPANIES(c) SetBit(c->avail_railtypes, railtype);
    00529   } else if (e->type == VEH_ROAD) {
    00530     /* maybe make another road type available */
    00531     FOR_ALL_COMPANIES(c) SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
    00532   }
    00533
    00534   AI::BroadcastNewEvent(new AIEventEngineAvailable(index));
    00535
    00536   SetDParam(0, GetEngineCategoryName(index));
    00537   SetDParam(1, index);
    00538   AddNewsItem(STR_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NS_NEW_VEHICLES, index, 0);
    00539 }
    00540
    00541 void EnginesMonthlyLoop()
    00542 {
    00543   if (_cur_year < _year_engine_aging_stops) {
    00544     Engine *e;
    00545     FOR_ALL_ENGINES(e) {
    00546       /* Age the vehicle */
    00547       if (e->flags & ENGINE_AVAILABLE && e->age != 0xFFFF) {
    00548         e->age++;
    00549         CalcEngineReliability(e);
    00550       }
    00551
    00552       if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + DAYS_IN_YEAR)) {
    00553         /* Introduce it to all companies */
    00554         NewVehicleAvailable(e);
    00555       } else if (!(e->flags & (ENGINE_AVAILABLE|ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
    00556         /* Introduction date has passed.. show introducing dialog to one companies. */
    00557         e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
    00558
    00559         /* Do not introduce new rail wagons */
    00560         if (!IsWagon(e->index))
    00561           e->preview_company_rank = 1; // Give to the company with the highest rating.
    00562       }
    00563     }
    00564   }
    00565 }
    00566
    00567 static bool IsUniqueEngineName(const char *name)
    00568 {
    00569   const Engine *e;
    00570
    00571   FOR_ALL_ENGINES(e) {
    00572     if (e->name != NULL && strcmp(e->name, name) == 0) return false;
    00573   }
    00574
    00575   return true;
    00576 }
    00577
    00584 CommandCost CmdRenameEngine(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00585 {
    00586   if (!IsEngineIndex(p1)) return CMD_ERROR;
    00587
    00588   bool reset = StrEmpty(text);
    00589
    00590   if (!reset) {
    00591     if (strlen(text) >= MAX_LENGTH_ENGINE_NAME_BYTES) return CMD_ERROR;
    00592     if (!IsUniqueEngineName(text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
    00593   }
    00594
    00595   if (flags & DC_EXEC) {
    00596     Engine *e = GetEngine(p1);
    00597     free(e->name);
    00598
    00599     if (reset) {
    00600       e->name = NULL;
    00601     } else {
    00602       e->name = strdup(text);
    00603     }
    00604
    00605     MarkWholeScreenDirty();
    00606   }
    00607
    00608   return CommandCost();
    00609 }
    00610
    00611
    00619 bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
    00620 {
    00621   /* check if it's an engine that is in the engine array */
    00622   if (!IsEngineIndex(engine)) return false;
    00623
    00624   const Engine *e = GetEngine(engine);
    00625
    00626   /* check if it's an engine of specified type */
    00627   if (e->type != type) return false;
    00628
    00629   /* check if it's available */
    00630   if (!HasBit(e->company_avail, company)) return false;
    00631
    00632   if (type == VEH_TRAIN) {
    00633     /* Check if the rail type is available to this company */
    00634     const Company *c = GetCompany(company);
    00635     if (!HasBit(c->avail_railtypes, RailVehInfo(engine)->railtype)) return false;
    00636   }
    00637
    00638   return true;
    00639 }
    00640
    00646 bool IsEngineRefittable(EngineID engine)
    00647 {
    00648   /* check if it's an engine that is in the engine array */
    00649   if (!IsEngineIndex(engine)) return false;
    00650
    00651   const Engine *e = GetEngine(engine);
    00652
    00653   if (e->type == VEH_SHIP && !e->u.ship.refittable) return false;
    00654
    00655   const EngineInfo *ei = &e->info;
    00656   if (ei->refit_mask == 0) return false;
    00657
    00658   /* Are there suffixes?
    00659    * Note: This does not mean the suffixes are actually available for every consist at any time. */
    00660   if (HasBit(ei->callbackmask, CBM_VEHICLE_CARGO_SUFFIX)) return true;
    00661
    00662   /* Is there any cargo except the default cargo? */
    00663   CargoID default_cargo = GetEngineCargoType(engine);
    00664   return default_cargo != CT_INVALID && ei->refit_mask != 1U << default_cargo;
    00665 }
    00666
    00671 CargoID GetEngineCargoType(EngineID engine)
    00672 {
    00673   assert(IsEngineIndex(engine));
    00674
    00675   switch (GetEngine(engine)->type) {
    00676     case VEH_TRAIN:
    00677       if (RailVehInfo(engine)->capacity == 0) return CT_INVALID;
    00678       return RailVehInfo(engine)->cargo_type;
    00679
    00680     case VEH_ROAD:
    00681       if (RoadVehInfo(engine)->capacity == 0) return CT_INVALID;
    00682       return RoadVehInfo(engine)->cargo_type;
    00683
    00684     case VEH_SHIP:
    00685       if (ShipVehInfo(engine)->capacity == 0) return CT_INVALID;
    00686       return ShipVehInfo(engine)->cargo_type;
    00687
    00688     case VEH_AIRCRAFT:
    00689       /* all aircraft starts as passenger planes with cargo capacity */
    00690       return CT_PASSENGERS;
    00691
    00692     default: NOT_REACHED(); return CT_INVALID;
    00693   }
    00694 }