engine.cpp

Go to the documentation of this file.
00001 /* $Id: engine.cpp 14828 2009-01-04 15:32:25Z smatz $ */
00002 
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "debug.h"
00008 #include "company_base.h"
00009 #include "company_func.h"
00010 #include "command_func.h"
00011 #include "news_func.h"
00012 #include "variables.h"
00013 #include "train.h"
00014 #include "aircraft.h"
00015 #include "newgrf_cargo.h"
00016 #include "newgrf_engine.h"
00017 #include "group.h"
00018 #include "strings_func.h"
00019 #include "gfx_func.h"
00020 #include "functions.h"
00021 #include "window_func.h"
00022 #include "date_func.h"
00023 #include "autoreplace_gui.h"
00024 #include "string_func.h"
00025 #include "settings_type.h"
00026 #include "oldpool_func.h"
00027 #include "core/alloc_func.hpp"
00028 #include "vehicle_func.h"
00029 
00030 #include "table/strings.h"
00031 #include "table/engines.h"
00032 
00033 DEFINE_OLD_POOL_GENERIC(Engine, Engine)
00034 
00035 enum {
00036   YEAR_ENGINE_AGING_STOPS = 2050,
00037 };
00038 
00039 
00041 const uint8 _engine_counts[4] = {
00042   lengthof(_orig_rail_vehicle_info),
00043   lengthof(_orig_road_vehicle_info),
00044   lengthof(_orig_ship_vehicle_info),
00045   lengthof(_orig_aircraft_vehicle_info),
00046 };
00047 
00049 const uint8 _engine_offsets[4] = {
00050   0,
00051   lengthof(_orig_rail_vehicle_info),
00052   lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info),
00053   lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info),
00054 };
00055 
00056 Engine::Engine() :
00057   name(NULL),
00058   overrides_count(0),
00059   overrides(NULL)
00060 {
00061 }
00062 
00063 Engine::Engine(VehicleType type, EngineID base)
00064 {
00065   this->type = type;
00066   this->internal_id = base;
00067   this->list_position = base;
00068 
00069   /* Check if this base engine is within the original engine data range */
00070   if (base >= _engine_counts[type]) {
00071     /* Mark engine as valid anyway */
00072     this->info.climates = 0x80;
00073     /* Set model life to maximum to make wagons available */
00074     this->info.base_life = 0xFF;
00075     return;
00076   }
00077 
00078   /* Copy the original engine info for this slot */
00079   this->info = _orig_engine_info[_engine_offsets[type] + base];
00080 
00081   /* Copy the original engine data for this slot */
00082   switch (type) {
00083     default: NOT_REACHED();
00084 
00085     case VEH_TRAIN:
00086       this->u.rail = _orig_rail_vehicle_info[base];
00087       this->image_index = this->u.rail.image_index;
00088       this->info.string_id = STR_8000_KIRBY_PAUL_TANK_STEAM + base;
00089 
00090       /* Set the default model life of original wagons to "infinite" */
00091       if (this->u.rail.railveh_type == RAILVEH_WAGON) this->info.base_life = 0xFF;
00092 
00093       break;
00094 
00095     case VEH_ROAD:
00096       this->u.road = _orig_road_vehicle_info[base];
00097       this->image_index = this->u.road.image_index;
00098       this->info.string_id = STR_8074_MPS_REGAL_BUS + base;
00099       break;
00100 
00101     case VEH_SHIP:
00102       this->u.ship = _orig_ship_vehicle_info[base];
00103       this->image_index = this->u.ship.image_index;
00104       this->info.string_id = STR_80CC_MPS_OIL_TANKER + base;
00105       break;
00106 
00107     case VEH_AIRCRAFT:
00108       this->u.air = _orig_aircraft_vehicle_info[base];
00109       this->image_index = this->u.air.image_index;
00110       this->info.string_id = STR_80D7_SAMPSON_U52 + base;
00111       break;
00112   }
00113 }
00114 
00115 Engine::~Engine()
00116 {
00117   UnloadWagonOverrides(this);
00118   free(this->name);
00119 }
00120 
00123 void SetCachedEngineCounts()
00124 {
00125   uint engines = GetEnginePoolSize();
00126 
00127   /* Set up the engine count for all companies */
00128   Company *c;
00129   FOR_ALL_COMPANIES(c) {
00130     free(c->num_engines);
00131     c->num_engines = CallocT<EngineID>(engines);
00132   }
00133 
00134   /* Recalculate */
00135   Group *g;
00136   FOR_ALL_GROUPS(g) {
00137     free(g->num_engines);
00138     g->num_engines = CallocT<EngineID>(engines);
00139   }
00140 
00141   const Vehicle *v;
00142   FOR_ALL_VEHICLES(v) {
00143     if (!IsEngineCountable(v)) continue;
00144 
00145     assert(v->engine_type < engines);
00146 
00147     GetCompany(v->owner)->num_engines[v->engine_type]++;
00148 
00149     if (v->group_id == DEFAULT_GROUP) continue;
00150 
00151     g = GetGroup(v->group_id);
00152     assert(v->type == g->vehicle_type);
00153     assert(v->owner == g->owner);
00154 
00155     g->num_engines[v->engine_type]++;
00156   }
00157 }
00158 
00159 void SetupEngines()
00160 {
00161   _Engine_pool.CleanPool();
00162   _Engine_pool.AddBlockToPool();
00163 
00164   for (uint i = 0; i < lengthof(_orig_rail_vehicle_info); i++) new Engine(VEH_TRAIN, i);
00165   for (uint i = 0; i < lengthof(_orig_road_vehicle_info); i++) new Engine(VEH_ROAD, i);
00166   for (uint i = 0; i < lengthof(_orig_ship_vehicle_info); i++) new Engine(VEH_SHIP, i);
00167   for (uint i = 0; i < lengthof(_orig_aircraft_vehicle_info); i++) new Engine(VEH_AIRCRAFT, i);
00168 }
00169 
00170 
00171 void ShowEnginePreviewWindow(EngineID engine);
00172 
00173 void DeleteCustomEngineNames()
00174 {
00175   Engine *e;
00176   FOR_ALL_ENGINES(e) {
00177     free(e->name);
00178     e->name = NULL;
00179   }
00180 
00181   _vehicle_design_names &= ~1;
00182 }
00183 
00184 void LoadCustomEngineNames()
00185 {
00186   /* XXX: not done */
00187   DEBUG(misc, 1, "LoadCustomEngineNames: not done");
00188 }
00189 
00190 /* Determine if an engine type is a wagon (and not a loco) */
00191 static bool IsWagon(EngineID index)
00192 {
00193   const Engine *e = GetEngine(index);
00194   return e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON;
00195 }
00196 
00197 static void CalcEngineReliability(Engine *e)
00198 {
00199   uint age = e->age;
00200 
00201   /* Check for early retirement */
00202   if (e->company_avail != 0 && !_settings_game.vehicle.never_expire_vehicles && e->info.base_life != 0xFF) {
00203     int retire_early = e->info.retire_early;
00204     uint retire_early_max_age = max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
00205     if (retire_early != 0 && age >= retire_early_max_age) {
00206       /* Early retirement is enabled and we're past the date... */
00207       e->company_avail = 0;
00208       AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00209     }
00210   }
00211 
00212   if (age < e->duration_phase_1) {
00213     uint start = e->reliability_start;
00214     e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
00215   } else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _settings_game.vehicle.never_expire_vehicles || e->info.base_life == 0xFF) {
00216     /* We are at the peak of this engines life. It will have max reliability.
00217      * This is also true if the engines never expire. They will not go bad over time */
00218     e->reliability = e->reliability_max;
00219   } else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
00220     uint max = e->reliability_max;
00221     e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
00222   } else {
00223     /* time's up for this engine.
00224      * We will now completely retire this design */
00225     e->company_avail = 0;
00226     e->reliability = e->reliability_final;
00227     /* Kick this engine out of the lists */
00228     AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00229   }
00230   InvalidateWindowClasses(WC_BUILD_VEHICLE); // Update to show the new reliability
00231   InvalidateWindowClasses(WC_REPLACE_VEHICLE);
00232 }
00233 
00234 void StartupEngines()
00235 {
00236   Engine *e;
00237   /* Aging of vehicles stops, so account for that when starting late */
00238   const Date aging_date = min(_date, ConvertYMDToDate(YEAR_ENGINE_AGING_STOPS, 0, 1));
00239 
00240   FOR_ALL_ENGINES(e) {
00241     const EngineInfo *ei = &e->info;
00242     uint32 r;
00243 
00244     e->age = 0;
00245     e->flags = 0;
00246     e->company_avail = 0;
00247 
00248     /* The magic value of 729 days below comes from the NewGRF spec. If the
00249      * base intro date is before 1922 then the random number of days is not
00250      * added. */
00251     r = Random();
00252     e->intro_date = ei->base_intro <= ConvertYMDToDate(1922, 0, 1) ? ei->base_intro : (Date)GB(r, 0, 9) + ei->base_intro;
00253     if (e->intro_date <= _date) {
00254       e->age = (aging_date - e->intro_date) >> 5;
00255       e->company_avail = (CompanyMask)-1;
00256       e->flags |= ENGINE_AVAILABLE;
00257     }
00258 
00259     e->reliability_start = GB(r, 16, 14) + 0x7AE0;
00260     r = Random();
00261     e->reliability_max   = GB(r,  0, 14) + 0xBFFF;
00262     e->reliability_final = GB(r, 16, 14) + 0x3FFF;
00263 
00264     r = Random();
00265     e->duration_phase_1 = GB(r, 0, 5) + 7;
00266     e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
00267     e->duration_phase_3 = GB(r, 9, 7) + 120;
00268 
00269     e->reliability_spd_dec = ei->decay_speed << 2;
00270 
00271     CalcEngineReliability(e);
00272 
00273     e->lifelength = ei->lifelength + _settings_game.vehicle.extend_vehicle_life;
00274 
00275     /* prevent certain engines from ever appearing. */
00276     if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) {
00277       e->flags |= ENGINE_AVAILABLE;
00278       e->company_avail = 0;
00279     }
00280   }
00281 
00282   /* Update the bitmasks for the vehicle lists */
00283   Company *c;
00284   FOR_ALL_COMPANIES(c) {
00285     c->avail_railtypes = GetCompanyRailtypes(c->index);
00286     c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00287   }
00288 }
00289 
00290 static void AcceptEnginePreview(EngineID eid, CompanyID company)
00291 {
00292   Engine *e = GetEngine(eid);
00293   Company *c = GetCompany(company);
00294 
00295   SetBit(e->company_avail, company);
00296   if (e->type == VEH_TRAIN) {
00297     const RailVehicleInfo *rvi = RailVehInfo(eid);
00298 
00299     assert(rvi->railtype < RAILTYPE_END);
00300     SetBit(c->avail_railtypes, rvi->railtype);
00301   } else if (e->type == VEH_ROAD) {
00302     SetBit(c->avail_roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00303   }
00304 
00305   e->preview_company_rank = 0xFF;
00306   if (company == _local_company) {
00307     AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00308   }
00309 }
00310 
00311 static CompanyID GetBestCompany(uint8 pp)
00312 {
00313   const Company *c;
00314   int32 best_hist;
00315   CompanyID best_company;
00316   CompanyMask mask = 0;
00317 
00318   do {
00319     best_hist = -1;
00320     best_company = INVALID_COMPANY;
00321     FOR_ALL_COMPANIES(c) {
00322       if (c->block_preview == 0 && !HasBit(mask, c->index) &&
00323           c->old_economy[0].performance_history > best_hist) {
00324         best_hist = c->old_economy[0].performance_history;
00325         best_company = c->index;
00326       }
00327     }
00328 
00329     if (best_company == INVALID_COMPANY) return INVALID_COMPANY;
00330 
00331     SetBit(mask, best_company);
00332   } while (--pp != 0);
00333 
00334   return best_company;
00335 }
00336 
00337 void EnginesDailyLoop()
00338 {
00339   if (_cur_year >= YEAR_ENGINE_AGING_STOPS) return;
00340 
00341   Engine *e;
00342   FOR_ALL_ENGINES(e) {
00343     EngineID i = e->index;
00344     if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
00345       if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
00346         if (e->preview_company_rank != 0xFF && !--e->preview_wait) {
00347           e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
00348           DeleteWindowById(WC_ENGINE_PREVIEW, i);
00349           e->preview_company_rank++;
00350         }
00351       } else if (e->preview_company_rank != 0xFF) {
00352         CompanyID best_company = GetBestCompany(e->preview_company_rank);
00353 
00354         if (best_company == INVALID_COMPANY) {
00355           e->preview_company_rank = 0xFF;
00356           continue;
00357         }
00358 
00359         if (!IsHumanCompany(best_company)) {
00360           /* XXX - TTDBUG: TTD has a bug here ???? */
00361           AcceptEnginePreview(i, best_company);
00362         } else {
00363           e->flags |= ENGINE_OFFER_WINDOW_OPEN;
00364           e->preview_wait = 20;
00365           if (IsInteractiveCompany(best_company)) ShowEnginePreviewWindow(i);
00366         }
00367       }
00368     }
00369   }
00370 }
00371 
00379 CommandCost CmdWantEnginePreview(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, const char *text)
00380 {
00381   Engine *e;
00382 
00383   if (!IsEngineIndex(p1)) return CMD_ERROR;
00384   e = GetEngine(p1);
00385   if (GetBestCompany(e->preview_company_rank) != _current_company) return CMD_ERROR;
00386 
00387   if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_company);
00388 
00389   return CommandCost();
00390 }
00391 
00392 StringID GetEngineCategoryName(EngineID engine);
00393 
00394 static void NewVehicleAvailable(Engine *e)
00395 {
00396   Vehicle *v;
00397   Company *c;
00398   EngineID index = e->index;
00399 
00400   /* In case the company didn't build the vehicle during the intro period,
00401    * prevent that company from getting future intro periods for a while. */
00402   if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
00403     FOR_ALL_COMPANIES(c) {
00404       uint block_preview = c->block_preview;
00405 
00406       if (!HasBit(e->company_avail, c->index)) continue;
00407 
00408       /* We assume the user did NOT build it.. prove me wrong ;) */
00409       c->block_preview = 20;
00410 
00411       FOR_ALL_VEHICLES(v) {
00412         if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
00413             (v->type == VEH_AIRCRAFT && IsNormalAircraft(v))) {
00414           if (v->owner == c->index && v->engine_type == index) {
00415             /* The user did prove me wrong, so restore old value */
00416             c->block_preview = block_preview;
00417             break;
00418           }
00419         }
00420       }
00421     }
00422   }
00423 
00424   e->flags = (e->flags & ~ENGINE_EXCLUSIVE_PREVIEW) | ENGINE_AVAILABLE;
00425   AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00426 
00427   /* Now available for all companies */
00428   e->company_avail = (CompanyMask)-1;
00429 
00430   /* Do not introduce new rail wagons */
00431   if (IsWagon(index)) return;
00432 
00433   if (e->type == VEH_TRAIN) {
00434     /* maybe make another rail type available */
00435     RailType railtype = e->u.rail.railtype;
00436     assert(railtype < RAILTYPE_END);
00437     FOR_ALL_COMPANIES(c) SetBit(c->avail_railtypes, railtype);
00438   } else if (e->type == VEH_ROAD) {
00439     /* maybe make another road type available */
00440     FOR_ALL_COMPANIES(c) SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00441   }
00442 
00443   SetDParam(0, GetEngineCategoryName(index));
00444   SetDParam(1, index);
00445   AddNewsItem(STR_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NS_NEW_VEHICLES, index, 0);
00446 }
00447 
00448 void EnginesMonthlyLoop()
00449 {
00450   if (_cur_year < YEAR_ENGINE_AGING_STOPS) {
00451     Engine *e;
00452     FOR_ALL_ENGINES(e) {
00453       /* Age the vehicle */
00454       if (e->flags & ENGINE_AVAILABLE && e->age != 0xFFFF) {
00455         e->age++;
00456         CalcEngineReliability(e);
00457       }
00458 
00459       if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + 365)) {
00460         /* Introduce it to all companies */
00461         NewVehicleAvailable(e);
00462       } else if (!(e->flags & (ENGINE_AVAILABLE|ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
00463         /* Introduction date has passed.. show introducing dialog to one companies. */
00464         e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
00465 
00466         /* Do not introduce new rail wagons */
00467         if (!IsWagon(e->index))
00468           e->preview_company_rank = 1; // Give to the company with the highest rating.
00469       }
00470     }
00471   }
00472 }
00473 
00474 static bool IsUniqueEngineName(const char *name)
00475 {
00476   char buf[512];
00477 
00478   const Engine *e;
00479   FOR_ALL_ENGINES(e) {
00480     SetDParam(0, e->index);
00481     GetString(buf, STR_ENGINE_NAME, lastof(buf));
00482     if (strcmp(buf, name) == 0) return false;
00483   }
00484 
00485   return true;
00486 }
00487 
00494 CommandCost CmdRenameEngine(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, const char *text)
00495 {
00496   if (!IsEngineIndex(p1)) return CMD_ERROR;
00497 
00498   bool reset = StrEmpty(text);
00499 
00500   if (!reset) {
00501     if (strlen(text) >= MAX_LENGTH_ENGINE_NAME_BYTES) return CMD_ERROR;
00502     if (!IsUniqueEngineName(text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
00503   }
00504 
00505   if (flags & DC_EXEC) {
00506     Engine *e = GetEngine(p1);
00507     free(e->name);
00508 
00509     if (reset) {
00510       e->name = NULL;
00511       /* if we removed the last custom name, disable the 'Save custom names' button */
00512       _vehicle_design_names &= ~1;
00513       FOR_ALL_ENGINES(e) {
00514         if (e->name != NULL) {
00515           _vehicle_design_names |= 1;
00516           break;
00517         }
00518       }
00519     } else {
00520       e->name = strdup(text);
00521       _vehicle_design_names |= 3;
00522     }
00523 
00524     MarkWholeScreenDirty();
00525   }
00526 
00527   return CommandCost();
00528 }
00529 
00530 
00538 bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
00539 {
00540   /* check if it's an engine that is in the engine array */
00541   if (!IsEngineIndex(engine)) return false;
00542 
00543   const Engine *e = GetEngine(engine);
00544 
00545   /* check if it's an engine of specified type */
00546   if (e->type != type) return false;
00547 
00548   /* check if it's available */
00549   if (!HasBit(e->company_avail, company)) return false;
00550 
00551   if (type == VEH_TRAIN) {
00552     /* Check if the rail type is available to this company */
00553     const Company *c = GetCompany(company);
00554     if (!HasBit(c->avail_railtypes, RailVehInfo(engine)->railtype)) return false;
00555   }
00556 
00557   return true;
00558 }
00559 
00565 bool IsEngineRefittable(EngineID engine)
00566 {
00567   /* check if it's an engine that is in the engine array */
00568   if (!IsEngineIndex(engine)) return false;
00569 
00570   const Engine *e = GetEngine(engine);
00571 
00572   if (e->type == VEH_SHIP && !e->u.ship.refittable) return false;
00573 
00574   const EngineInfo *ei = &e->info;
00575   if (ei->refit_mask == 0) return false;
00576 
00577   /* Are there suffixes?
00578    * Note: This does not mean the suffixes are actually available for every consist at any time. */
00579   if (HasBit(ei->callbackmask, CBM_VEHICLE_CARGO_SUFFIX)) return true;
00580 
00581   /* Is there any cargo except the default cargo? */
00582   CargoID default_cargo = GetEngineCargoType(engine);
00583   return default_cargo != CT_INVALID && ei->refit_mask != 1U << default_cargo;
00584 }
00585 
00590 CargoID GetEngineCargoType(EngineID engine)
00591 {
00592   assert(IsEngineIndex(engine));
00593 
00594   switch (GetEngine(engine)->type) {
00595     case VEH_TRAIN:
00596       if (RailVehInfo(engine)->capacity == 0) return CT_INVALID;
00597       return RailVehInfo(engine)->cargo_type;
00598 
00599     case VEH_ROAD:
00600       if (RoadVehInfo(engine)->capacity == 0) return CT_INVALID;
00601       return RoadVehInfo(engine)->cargo_type;
00602 
00603     case VEH_SHIP:
00604       if (ShipVehInfo(engine)->capacity == 0) return CT_INVALID;
00605       return ShipVehInfo(engine)->cargo_type;
00606 
00607     case VEH_AIRCRAFT:
00608       /* all aircraft starts as passenger planes with cargo capacity */
00609       return CT_PASSENGERS;
00610 
00611     default: NOT_REACHED(); return CT_INVALID;
00612   }
00613 }

Generated on Tue Jan 6 19:01:36 2009 for openttd by  doxygen 1.5.6