00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "company_func.h"
00014 #include "command_func.h"
00015 #include "news_func.h"
00016 #include "aircraft.h"
00017 #include "newgrf.h"
00018 #include "newgrf_engine.h"
00019 #include "group.h"
00020 #include "strings_func.h"
00021 #include "core/random_func.hpp"
00022 #include "window_func.h"
00023 #include "date_func.h"
00024 #include "autoreplace_gui.h"
00025 #include "string_func.h"
00026 #include "ai/ai.hpp"
00027 #include "core/pool_func.hpp"
00028 #include "engine_gui.h"
00029 #include "engine_func.h"
00030 #include "engine_base.h"
00031 #include "company_base.h"
00032 #include "vehicle_func.h"
00033
00034 #include "table/strings.h"
00035 #include "table/engines.h"
00036
00037 EnginePool _engine_pool("Engine");
00038 INSTANTIATE_POOL_METHODS(Engine)
00039
00040 EngineOverrideManager _engine_mngr;
00041
00046 static Year _year_engine_aging_stops;
00047
00051 static uint16 _introduced_railtypes;
00052
00054 const uint8 _engine_counts[4] = {
00055 lengthof(_orig_rail_vehicle_info),
00056 lengthof(_orig_road_vehicle_info),
00057 lengthof(_orig_ship_vehicle_info),
00058 lengthof(_orig_aircraft_vehicle_info),
00059 };
00060
00062 const uint8 _engine_offsets[4] = {
00063 0,
00064 lengthof(_orig_rail_vehicle_info),
00065 lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info),
00066 lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info),
00067 };
00068
00069 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));
00070
00071 const uint EngineOverrideManager::NUM_DEFAULT_ENGINES = _engine_counts[VEH_TRAIN] + _engine_counts[VEH_ROAD] + _engine_counts[VEH_SHIP] + _engine_counts[VEH_AIRCRAFT];
00072
00073 Engine::Engine() :
00074 name(NULL),
00075 overrides_count(0),
00076 overrides(NULL)
00077 {
00078 }
00079
00080 Engine::Engine(VehicleType type, EngineID base)
00081 {
00082 this->type = type;
00083 this->grf_prop.local_id = base;
00084 this->list_position = base;
00085
00086
00087 if (base >= _engine_counts[type]) {
00088
00089 this->info.climates = 0x80;
00090
00091 this->info.base_life = 0xFF;
00092
00093 if (type == VEH_ROAD) this->u.road.tractive_effort = 0x4C;
00094
00095 switch (type) {
00096 case VEH_TRAIN: this->u.rail.visual_effect = VE_DEFAULT; break;
00097 case VEH_ROAD: this->u.road.visual_effect = VE_DEFAULT; break;
00098 case VEH_SHIP: this->u.ship.visual_effect = VE_DEFAULT; break;
00099 default: break;
00100 }
00101 return;
00102 }
00103
00104
00105 this->info = _orig_engine_info[_engine_offsets[type] + base];
00106
00107
00108 switch (type) {
00109 default: NOT_REACHED();
00110
00111 case VEH_TRAIN:
00112 this->u.rail = _orig_rail_vehicle_info[base];
00113 this->original_image_index = this->u.rail.image_index;
00114 this->info.string_id = STR_VEHICLE_NAME_TRAIN_ENGINE_RAIL_KIRBY_PAUL_TANK_STEAM + base;
00115
00116
00117 if (this->u.rail.railveh_type == RAILVEH_WAGON) this->info.base_life = 0xFF;
00118
00119 break;
00120
00121 case VEH_ROAD:
00122 this->u.road = _orig_road_vehicle_info[base];
00123 this->original_image_index = this->u.road.image_index;
00124 this->info.string_id = STR_VEHICLE_NAME_ROAD_VEHICLE_MPS_REGAL_BUS + base;
00125 break;
00126
00127 case VEH_SHIP:
00128 this->u.ship = _orig_ship_vehicle_info[base];
00129 this->original_image_index = this->u.ship.image_index;
00130 this->info.string_id = STR_VEHICLE_NAME_SHIP_MPS_OIL_TANKER + base;
00131 break;
00132
00133 case VEH_AIRCRAFT:
00134 this->u.air = _orig_aircraft_vehicle_info[base];
00135 this->original_image_index = this->u.air.image_index;
00136 this->info.string_id = STR_VEHICLE_NAME_AIRCRAFT_SAMPSON_U52 + base;
00137 break;
00138 }
00139 }
00140
00141 Engine::~Engine()
00142 {
00143 UnloadWagonOverrides(this);
00144 free(this->name);
00145 }
00146
00151 bool Engine::IsEnabled() const
00152 {
00153 return this->info.string_id != STR_NEWGRF_INVALID_ENGINE;
00154 }
00155
00161 bool Engine::CanCarryCargo() const
00162 {
00163
00164
00165
00166
00167
00168 switch (this->type) {
00169 case VEH_TRAIN:
00170 if (this->u.rail.capacity == 0) return false;
00171 break;
00172
00173 case VEH_ROAD:
00174 if (this->u.road.capacity == 0) return false;
00175 break;
00176
00177 case VEH_SHIP:
00178 case VEH_AIRCRAFT:
00179 break;
00180
00181 default: NOT_REACHED();
00182 }
00183 return this->GetDefaultCargoType() != CT_INVALID;
00184 }
00185
00198 uint Engine::GetDisplayDefaultCapacity(uint16 *mail_capacity) const
00199 {
00200 if (mail_capacity != NULL) *mail_capacity = 0;
00201 if (!this->CanCarryCargo()) return 0;
00202 switch (type) {
00203 case VEH_TRAIN:
00204 return GetEngineProperty(this->index, PROP_TRAIN_CARGO_CAPACITY, this->u.rail.capacity) + (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? this->u.rail.capacity : 0);
00205
00206 case VEH_ROAD:
00207 return GetEngineProperty(this->index, PROP_ROADVEH_CARGO_CAPACITY, this->u.road.capacity);
00208
00209 case VEH_SHIP:
00210 return GetEngineProperty(this->index, PROP_SHIP_CARGO_CAPACITY, this->u.ship.capacity);
00211
00212 case VEH_AIRCRAFT: {
00213 uint capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_PASSENGER_CAPACITY, this->u.air.passenger_capacity);
00214 CargoID cargo = this->GetDefaultCargoType();
00215 if (IsCargoInClass(cargo, CC_PASSENGERS)) {
00216 if (mail_capacity != NULL) *mail_capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity);
00217 } else {
00218 capacity += GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity);
00219 }
00220 switch (cargo) {
00221 case CT_PASSENGERS:
00222 case CT_MAIL: return capacity;
00223 case CT_GOODS: return capacity / 2;
00224 default: return capacity / 4;
00225 }
00226 }
00227
00228 default: NOT_REACHED();
00229 }
00230 }
00231
00236 Money Engine::GetRunningCost() const
00237 {
00238 Price base_price;
00239 uint cost_factor;
00240 switch (this->type) {
00241 case VEH_ROAD:
00242 base_price = this->u.road.running_cost_class;
00243 if (base_price == INVALID_PRICE) return 0;
00244 cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_RUNNING_COST_FACTOR, this->u.road.running_cost);
00245 break;
00246
00247 case VEH_TRAIN:
00248 base_price = this->u.rail.running_cost_class;
00249 if (base_price == INVALID_PRICE) return 0;
00250 cost_factor = GetEngineProperty(this->index, PROP_TRAIN_RUNNING_COST_FACTOR, this->u.rail.running_cost);
00251 break;
00252
00253 case VEH_SHIP:
00254 base_price = PR_RUNNING_SHIP;
00255 cost_factor = GetEngineProperty(this->index, PROP_SHIP_RUNNING_COST_FACTOR, this->u.ship.running_cost);
00256 break;
00257
00258 case VEH_AIRCRAFT:
00259 base_price = PR_RUNNING_AIRCRAFT;
00260 cost_factor = GetEngineProperty(this->index, PROP_AIRCRAFT_RUNNING_COST_FACTOR, this->u.air.running_cost);
00261 break;
00262
00263 default: NOT_REACHED();
00264 }
00265
00266 return GetPrice(base_price, cost_factor, this->grf_prop.grffile, -8);
00267 }
00268
00273 Money Engine::GetCost() const
00274 {
00275 Price base_price;
00276 uint cost_factor;
00277 switch (this->type) {
00278 case VEH_ROAD:
00279 base_price = PR_BUILD_VEHICLE_ROAD;
00280 cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_COST_FACTOR, this->u.road.cost_factor);
00281 break;
00282
00283 case VEH_TRAIN:
00284 if (this->u.rail.railveh_type == RAILVEH_WAGON) {
00285 base_price = PR_BUILD_VEHICLE_WAGON;
00286 cost_factor = GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor);
00287 } else {
00288 base_price = PR_BUILD_VEHICLE_TRAIN;
00289 cost_factor = GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor);
00290 }
00291 break;
00292
00293 case VEH_SHIP:
00294 base_price = PR_BUILD_VEHICLE_SHIP;
00295 cost_factor = GetEngineProperty(this->index, PROP_SHIP_COST_FACTOR, this->u.ship.cost_factor);
00296 break;
00297
00298 case VEH_AIRCRAFT:
00299 base_price = PR_BUILD_VEHICLE_AIRCRAFT;
00300 cost_factor = GetEngineProperty(this->index, PROP_AIRCRAFT_COST_FACTOR, this->u.air.cost_factor);
00301 break;
00302
00303 default: NOT_REACHED();
00304 }
00305
00306 return GetPrice(base_price, cost_factor, this->grf_prop.grffile, -8);
00307 }
00308
00313 uint Engine::GetDisplayMaxSpeed() const
00314 {
00315 switch (this->type) {
00316 case VEH_TRAIN:
00317 return GetEngineProperty(this->index, PROP_TRAIN_SPEED, this->u.rail.max_speed);
00318
00319 case VEH_ROAD: {
00320 uint max_speed = GetEngineProperty(this->index, PROP_ROADVEH_SPEED, 0);
00321 return (max_speed != 0) ? max_speed * 2 : this->u.road.max_speed / 2;
00322 }
00323
00324 case VEH_SHIP:
00325 return GetEngineProperty(this->index, PROP_SHIP_SPEED, this->u.ship.max_speed) / 2;
00326
00327 case VEH_AIRCRAFT: {
00328 uint max_speed = GetEngineProperty(this->index, PROP_AIRCRAFT_SPEED, 0);
00329 if (max_speed != 0) {
00330 return (max_speed * 128) / 10;
00331 }
00332 return this->u.air.max_speed;
00333 }
00334
00335 default: NOT_REACHED();
00336 }
00337 }
00338
00345 uint Engine::GetPower() const
00346 {
00347
00348 switch (this->type) {
00349 case VEH_TRAIN:
00350 return GetEngineProperty(this->index, PROP_TRAIN_POWER, this->u.rail.power);
00351 case VEH_ROAD:
00352 return GetEngineProperty(this->index, PROP_ROADVEH_POWER, this->u.road.power) * 10;
00353
00354 default: NOT_REACHED();
00355 }
00356 }
00357
00363 uint Engine::GetDisplayWeight() const
00364 {
00365
00366 switch (this->type) {
00367 case VEH_TRAIN:
00368 return GetEngineProperty(this->index, PROP_TRAIN_WEIGHT, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0);
00369 case VEH_ROAD:
00370 return GetEngineProperty(this->index, PROP_ROADVEH_WEIGHT, this->u.road.weight) / 4;
00371
00372 default: NOT_REACHED();
00373 }
00374 }
00375
00381 uint Engine::GetDisplayMaxTractiveEffort() const
00382 {
00383
00384 switch (this->type) {
00385 case VEH_TRAIN:
00386 return (10 * this->GetDisplayWeight() * GetEngineProperty(this->index, PROP_TRAIN_TRACTIVE_EFFORT, this->u.rail.tractive_effort)) / 256;
00387 case VEH_ROAD:
00388 return (10 * this->GetDisplayWeight() * GetEngineProperty(this->index, PROP_ROADVEH_TRACTIVE_EFFORT, this->u.road.tractive_effort)) / 256;
00389
00390 default: NOT_REACHED();
00391 }
00392 }
00393
00398 Date Engine::GetLifeLengthInDays() const
00399 {
00400
00401 return (this->info.lifelength + _settings_game.vehicle.extend_vehicle_life) * DAYS_IN_LEAP_YEAR;
00402 }
00403
00407 void EngineOverrideManager::ResetToDefaultMapping()
00408 {
00409 this->Clear();
00410 for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) {
00411 for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++) {
00412 EngineIDMapping *eid = this->Append();
00413 eid->type = type;
00414 eid->grfid = INVALID_GRFID;
00415 eid->internal_id = internal_id;
00416 eid->substitute_id = internal_id;
00417 }
00418 }
00419 }
00420
00430 EngineID EngineOverrideManager::GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
00431 {
00432 const EngineIDMapping *end = this->End();
00433 EngineID index = 0;
00434 for (const EngineIDMapping *eid = this->Begin(); eid != end; eid++, index++) {
00435 if (eid->type == type && eid->grfid == grfid && eid->internal_id == grf_local_id) {
00436 return index;
00437 }
00438 }
00439 return INVALID_ENGINE;
00440 }
00441
00447 bool EngineOverrideManager::ResetToCurrentNewGRFConfig()
00448 {
00449 const Vehicle *v;
00450 FOR_ALL_VEHICLES(v) {
00451 if (IsCompanyBuildableVehicleType(v)) return false;
00452 }
00453
00454
00455 _engine_mngr.ResetToDefaultMapping();
00456 ReloadNewGRFData();
00457
00458 return true;
00459 }
00460
00464 void SetCachedEngineCounts()
00465 {
00466 size_t engines = Engine::GetPoolSize();
00467
00468
00469 Company *c;
00470 FOR_ALL_COMPANIES(c) {
00471 free(c->num_engines);
00472 c->num_engines = CallocT<EngineID>(engines);
00473 }
00474
00475
00476 Group *g;
00477 FOR_ALL_GROUPS(g) {
00478 free(g->num_engines);
00479 g->num_engines = CallocT<EngineID>(engines);
00480 }
00481
00482 const Vehicle *v;
00483 FOR_ALL_VEHICLES(v) {
00484 if (!v->IsEngineCountable()) continue;
00485
00486 assert(v->engine_type < engines);
00487
00488 Company::Get(v->owner)->num_engines[v->engine_type]++;
00489
00490 if (v->group_id == DEFAULT_GROUP) continue;
00491
00492 g = Group::Get(v->group_id);
00493 assert(v->type == g->vehicle_type);
00494 assert(v->owner == g->owner);
00495
00496 g->num_engines[v->engine_type]++;
00497 }
00498 }
00499
00503 void SetupEngines()
00504 {
00505 DeleteWindowByClass(WC_ENGINE_PREVIEW);
00506 _engine_pool.CleanPool();
00507
00508 assert(_engine_mngr.Length() >= _engine_mngr.NUM_DEFAULT_ENGINES);
00509 const EngineIDMapping *end = _engine_mngr.End();
00510 uint index = 0;
00511 for (const EngineIDMapping *eid = _engine_mngr.Begin(); eid != end; eid++, index++) {
00512
00513
00514 assert(Engine::CanAllocateItem());
00515 const Engine *e = new Engine(eid->type, eid->internal_id);
00516 assert(e->index == index);
00517 }
00518
00519 _introduced_railtypes = 0;
00520 }
00521
00525 static void CheckRailIntroduction()
00526 {
00527
00528 if (_introduced_railtypes == UINT16_MAX || Company::GetPoolSize() == 0) return;
00529
00530
00531 RailTypes rts = (RailTypes)UINT16_MAX;
00532
00533
00534 Company *c;
00535 FOR_ALL_COMPANIES(c) {
00536 c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes, _date);
00537 rts &= c->avail_railtypes;
00538 }
00539
00540 _introduced_railtypes |= rts;
00541 }
00542
00543 void ShowEnginePreviewWindow(EngineID engine);
00544
00545
00546 static bool IsWagon(EngineID index)
00547 {
00548 const Engine *e = Engine::Get(index);
00549 return e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON;
00550 }
00551
00552 static void CalcEngineReliability(Engine *e)
00553 {
00554 uint age = e->age;
00555
00556
00557 if (e->company_avail != 0 && !_settings_game.vehicle.never_expire_vehicles && e->info.base_life != 0xFF) {
00558 int retire_early = e->info.retire_early;
00559 uint retire_early_max_age = max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
00560 if (retire_early != 0 && age >= retire_early_max_age) {
00561
00562 e->company_avail = 0;
00563 AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00564 }
00565 }
00566
00567 if (age < e->duration_phase_1) {
00568 uint start = e->reliability_start;
00569 e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
00570 } else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _settings_game.vehicle.never_expire_vehicles || e->info.base_life == 0xFF) {
00571
00572
00573 e->reliability = e->reliability_max;
00574 } else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
00575 uint max = e->reliability_max;
00576 e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
00577 } else {
00578
00579
00580 e->company_avail = 0;
00581 e->reliability = e->reliability_final;
00582
00583 AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00584 }
00585 SetWindowClassesDirty(WC_BUILD_VEHICLE);
00586 SetWindowClassesDirty(WC_REPLACE_VEHICLE);
00587 }
00588
00590 void SetYearEngineAgingStops()
00591 {
00592
00593 _year_engine_aging_stops = 2050;
00594
00595 const Engine *e;
00596 FOR_ALL_ENGINES(e) {
00597 const EngineInfo *ei = &e->info;
00598
00599
00600 if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) continue;
00601 if (e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON) continue;
00602
00603
00604 YearMonthDay ymd;
00605 ConvertDateToYMD(ei->base_intro + (ei->lifelength * DAYS_IN_LEAP_YEAR) / 2, &ymd);
00606
00607 _year_engine_aging_stops = max(_year_engine_aging_stops, ymd.year);
00608 }
00609 }
00610
00611 void StartupOneEngine(Engine *e, Date aging_date)
00612 {
00613 const EngineInfo *ei = &e->info;
00614
00615 e->age = 0;
00616 e->flags = 0;
00617 e->company_avail = 0;
00618
00619
00620
00621
00622 uint32 r = Random();
00623 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;
00624 if (e->intro_date <= _date) {
00625 e->age = (aging_date - e->intro_date) >> 5;
00626 e->company_avail = (CompanyMask)-1;
00627 e->flags |= ENGINE_AVAILABLE;
00628 }
00629
00630 e->reliability_start = GB(r, 16, 14) + 0x7AE0;
00631 r = Random();
00632 e->reliability_max = GB(r, 0, 14) + 0xBFFF;
00633 e->reliability_final = GB(r, 16, 14) + 0x3FFF;
00634
00635 r = Random();
00636 e->duration_phase_1 = GB(r, 0, 5) + 7;
00637 e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
00638 e->duration_phase_3 = GB(r, 9, 7) + 120;
00639
00640 e->reliability_spd_dec = ei->decay_speed << 2;
00641
00642 CalcEngineReliability(e);
00643
00644
00645 if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) {
00646 e->flags |= ENGINE_AVAILABLE;
00647 e->company_avail = 0;
00648 }
00649 }
00650
00651 void StartupEngines()
00652 {
00653 Engine *e;
00654
00655 const Date aging_date = min(_date, ConvertYMDToDate(_year_engine_aging_stops, 0, 1));
00656
00657 FOR_ALL_ENGINES(e) {
00658 StartupOneEngine(e, aging_date);
00659 }
00660
00661
00662 Company *c;
00663 FOR_ALL_COMPANIES(c) {
00664 c->avail_railtypes = GetCompanyRailtypes(c->index);
00665 c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00666 }
00667
00668
00669
00670
00671
00672 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
00673 const RailtypeInfo *rti = GetRailTypeInfo(rt);
00674 if (rti->label != 0 && IsInsideMM(rti->introduction_date, 0, MAX_DAY)) continue;
00675
00676 SetBit(_introduced_railtypes, rt);
00677 }
00678
00679 CheckRailIntroduction();
00680
00681
00682 InvalidateWindowClassesData(WC_BUILD_VEHICLE);
00683 }
00684
00685 static void AcceptEnginePreview(EngineID eid, CompanyID company)
00686 {
00687 Engine *e = Engine::Get(eid);
00688 Company *c = Company::Get(company);
00689
00690 SetBit(e->company_avail, company);
00691 if (e->type == VEH_TRAIN) {
00692 assert(e->u.rail.railtype < RAILTYPE_END);
00693 c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes | GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes, _date);
00694 } else if (e->type == VEH_ROAD) {
00695 SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00696 }
00697
00698 e->preview_company_rank = 0xFF;
00699 if (company == _local_company) {
00700 AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00701 }
00702
00703
00704 if (e->type == VEH_ROAD) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_ROAD);
00705 if (e->type == VEH_SHIP) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_WATER);
00706 }
00707
00713 static CompanyID GetBestCompany(uint8 pp)
00714 {
00715 CompanyID best_company;
00716 CompanyMask mask = 0;
00717
00718 do {
00719 int32 best_hist = -1;
00720 best_company = INVALID_COMPANY;
00721
00722 const Company *c;
00723 FOR_ALL_COMPANIES(c) {
00724 if (c->block_preview == 0 && !HasBit(mask, c->index) &&
00725 c->old_economy[0].performance_history > best_hist) {
00726 best_hist = c->old_economy[0].performance_history;
00727 best_company = c->index;
00728 }
00729 }
00730
00731 if (best_company == INVALID_COMPANY) return INVALID_COMPANY;
00732
00733 SetBit(mask, best_company);
00734 } while (--pp != 0);
00735
00736 return best_company;
00737 }
00738
00740 void EnginesDailyLoop()
00741 {
00742 CheckRailIntroduction();
00743
00744 if (_cur_year >= _year_engine_aging_stops) return;
00745
00746 Engine *e;
00747 FOR_ALL_ENGINES(e) {
00748 EngineID i = e->index;
00749 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
00750 if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
00751 if (e->preview_company_rank != 0xFF && !--e->preview_wait) {
00752 e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
00753 DeleteWindowById(WC_ENGINE_PREVIEW, i);
00754 e->preview_company_rank++;
00755 }
00756 } else if (e->preview_company_rank != 0xFF) {
00757 CompanyID best_company = GetBestCompany(e->preview_company_rank);
00758
00759 if (best_company == INVALID_COMPANY) {
00760 e->preview_company_rank = 0xFF;
00761 continue;
00762 }
00763
00764 e->flags |= ENGINE_OFFER_WINDOW_OPEN;
00765 e->preview_wait = 20;
00766 AI::NewEvent(best_company, new AIEventEnginePreview(i));
00767 if (IsInteractiveCompany(best_company)) ShowEnginePreviewWindow(i);
00768 }
00769 }
00770 }
00771 }
00772
00783 CommandCost CmdWantEnginePreview(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00784 {
00785 Engine *e = Engine::GetIfValid(p1);
00786 if (e == NULL || GetBestCompany(e->preview_company_rank) != _current_company) return CMD_ERROR;
00787
00788 if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_company);
00789
00790 return CommandCost();
00791 }
00792
00798 static void NewVehicleAvailable(Engine *e)
00799 {
00800 Vehicle *v;
00801 Company *c;
00802 EngineID index = e->index;
00803
00804
00805
00806 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
00807 FOR_ALL_COMPANIES(c) {
00808 uint block_preview = c->block_preview;
00809
00810 if (!HasBit(e->company_avail, c->index)) continue;
00811
00812
00813 c->block_preview = 20;
00814
00815 FOR_ALL_VEHICLES(v) {
00816 if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
00817 (v->type == VEH_AIRCRAFT && Aircraft::From(v)->IsNormalAircraft())) {
00818 if (v->owner == c->index && v->engine_type == index) {
00819
00820 c->block_preview = block_preview;
00821 break;
00822 }
00823 }
00824 }
00825 }
00826 }
00827
00828 e->flags = (e->flags & ~ENGINE_EXCLUSIVE_PREVIEW) | ENGINE_AVAILABLE;
00829 AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00830
00831
00832 e->company_avail = (CompanyMask)-1;
00833
00834
00835 if (IsWagon(index)) return;
00836
00837 if (e->type == VEH_TRAIN) {
00838
00839 RailType railtype = e->u.rail.railtype;
00840 assert(railtype < RAILTYPE_END);
00841 FOR_ALL_COMPANIES(c) c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes | GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes, _date);
00842 } else if (e->type == VEH_ROAD) {
00843
00844 FOR_ALL_COMPANIES(c) SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00845 }
00846
00847 AI::BroadcastNewEvent(new AIEventEngineAvailable(index));
00848
00849 SetDParam(0, GetEngineCategoryName(index));
00850 SetDParam(1, index);
00851 AddNewsItem(STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NS_NEW_VEHICLES, NR_ENGINE, index);
00852
00853
00854 if (e->type == VEH_ROAD) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_ROAD);
00855 if (e->type == VEH_SHIP) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_WATER);
00856 }
00857
00858 void EnginesMonthlyLoop()
00859 {
00860 if (_cur_year < _year_engine_aging_stops) {
00861 Engine *e;
00862 FOR_ALL_ENGINES(e) {
00863
00864 if ((e->flags & ENGINE_AVAILABLE) && e->age != MAX_DAY) {
00865 e->age++;
00866 CalcEngineReliability(e);
00867 }
00868
00869
00870 if (!e->IsEnabled()) continue;
00871
00872 if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + DAYS_IN_YEAR)) {
00873
00874 NewVehicleAvailable(e);
00875 } else if (!(e->flags & (ENGINE_AVAILABLE | ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
00876
00877 e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
00878
00879
00880 if (!IsWagon(e->index)) {
00881 e->preview_company_rank = 1;
00882 }
00883 }
00884 }
00885 }
00886 }
00887
00888 static bool IsUniqueEngineName(const char *name)
00889 {
00890 const Engine *e;
00891
00892 FOR_ALL_ENGINES(e) {
00893 if (e->name != NULL && strcmp(e->name, name) == 0) return false;
00894 }
00895
00896 return true;
00897 }
00898
00908 CommandCost CmdRenameEngine(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00909 {
00910 Engine *e = Engine::GetIfValid(p1);
00911 if (e == NULL) return CMD_ERROR;
00912
00913 bool reset = StrEmpty(text);
00914
00915 if (!reset) {
00916 if (Utf8StringLength(text) >= MAX_LENGTH_ENGINE_NAME_CHARS) return CMD_ERROR;
00917 if (!IsUniqueEngineName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
00918 }
00919
00920 if (flags & DC_EXEC) {
00921 free(e->name);
00922
00923 if (reset) {
00924 e->name = NULL;
00925 } else {
00926 e->name = strdup(text);
00927 }
00928
00929 MarkWholeScreenDirty();
00930 }
00931
00932 return CommandCost();
00933 }
00934
00935
00944 bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
00945 {
00946 const Engine *e = Engine::GetIfValid(engine);
00947
00948
00949 if (e == NULL) return false;
00950
00951
00952 if (e->type != type) return false;
00953
00954
00955 if (!HasBit(e->company_avail, company)) return false;
00956
00957 if (!e->IsEnabled()) return false;
00958
00959 if (type == VEH_TRAIN) {
00960
00961 const Company *c = Company::Get(company);
00962 if (((GetRailTypeInfo(e->u.rail.railtype))->compatible_railtypes & c->avail_railtypes) == 0) return false;
00963 }
00964
00965 return true;
00966 }
00967
00974 bool IsEngineRefittable(EngineID engine)
00975 {
00976 const Engine *e = Engine::GetIfValid(engine);
00977
00978
00979 if (e == NULL) return false;
00980
00981 if (!e->CanCarryCargo()) return false;
00982
00983 const EngineInfo *ei = &e->info;
00984 if (ei->refit_mask == 0) return false;
00985
00986
00987
00988 if (HasBit(ei->callback_mask, CBM_VEHICLE_CARGO_SUFFIX)) return true;
00989
00990
00991 CargoID default_cargo = e->GetDefaultCargoType();
00992 return default_cargo != CT_INVALID && ei->refit_mask != 1U << default_cargo;
00993 }