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 "strings_func.h"
00020 #include "core/random_func.hpp"
00021 #include "window_func.h"
00022 #include "date_func.h"
00023 #include "autoreplace_gui.h"
00024 #include "string_func.h"
00025 #include "ai/ai.hpp"
00026 #include "core/pool_func.hpp"
00027 #include "engine_gui.h"
00028 #include "engine_func.h"
00029 #include "engine_base.h"
00030 #include "company_base.h"
00031 #include "vehicle_func.h"
00032
00033 #include "table/strings.h"
00034 #include "table/engines.h"
00035
00036 EnginePool _engine_pool("Engine");
00037 INSTANTIATE_POOL_METHODS(Engine)
00038
00039 EngineOverrideManager _engine_mngr;
00040
00045 static Year _year_engine_aging_stops;
00046
00050 static uint16 _introduced_railtypes;
00051
00053 const uint8 _engine_counts[4] = {
00054 lengthof(_orig_rail_vehicle_info),
00055 lengthof(_orig_road_vehicle_info),
00056 lengthof(_orig_ship_vehicle_info),
00057 lengthof(_orig_aircraft_vehicle_info),
00058 };
00059
00061 const uint8 _engine_offsets[4] = {
00062 0,
00063 lengthof(_orig_rail_vehicle_info),
00064 lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info),
00065 lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info),
00066 };
00067
00068 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));
00069
00070 const uint EngineOverrideManager::NUM_DEFAULT_ENGINES = _engine_counts[VEH_TRAIN] + _engine_counts[VEH_ROAD] + _engine_counts[VEH_SHIP] + _engine_counts[VEH_AIRCRAFT];
00071
00072 Engine::Engine() :
00073 name(NULL),
00074 overrides_count(0),
00075 overrides(NULL)
00076 {
00077 }
00078
00079 Engine::Engine(VehicleType type, EngineID base)
00080 {
00081 this->type = type;
00082 this->grf_prop.local_id = base;
00083 this->list_position = base;
00084
00085
00086 if (base >= _engine_counts[type]) {
00087
00088 this->info.base_life = 0xFF;
00089
00090 if (type == VEH_ROAD) this->u.road.tractive_effort = 0x4C;
00091
00092 if (type == VEH_AIRCRAFT) this->info.cargo_type = CT_INVALID;
00093
00094 switch (type) {
00095 case VEH_TRAIN: this->u.rail.visual_effect = VE_DEFAULT; break;
00096 case VEH_ROAD: this->u.road.visual_effect = VE_DEFAULT; break;
00097 case VEH_SHIP: this->u.ship.visual_effect = VE_DEFAULT; break;
00098 default: break;
00099 }
00100
00101 this->info.cargo_age_period = CARGO_AGING_TICKS;
00102 return;
00103 }
00104
00105
00106 this->info = _orig_engine_info[_engine_offsets[type] + base];
00107
00108
00109 switch (type) {
00110 default: NOT_REACHED();
00111
00112 case VEH_TRAIN:
00113 this->u.rail = _orig_rail_vehicle_info[base];
00114 this->original_image_index = this->u.rail.image_index;
00115 this->info.string_id = STR_VEHICLE_NAME_TRAIN_ENGINE_RAIL_KIRBY_PAUL_TANK_STEAM + base;
00116
00117
00118 if (this->u.rail.railveh_type == RAILVEH_WAGON) this->info.base_life = 0xFF;
00119
00120 break;
00121
00122 case VEH_ROAD:
00123 this->u.road = _orig_road_vehicle_info[base];
00124 this->original_image_index = this->u.road.image_index;
00125 this->info.string_id = STR_VEHICLE_NAME_ROAD_VEHICLE_MPS_REGAL_BUS + base;
00126 break;
00127
00128 case VEH_SHIP:
00129 this->u.ship = _orig_ship_vehicle_info[base];
00130 this->original_image_index = this->u.ship.image_index;
00131 this->info.string_id = STR_VEHICLE_NAME_SHIP_MPS_OIL_TANKER + base;
00132 break;
00133
00134 case VEH_AIRCRAFT:
00135 this->u.air = _orig_aircraft_vehicle_info[base];
00136 this->original_image_index = this->u.air.image_index;
00137 this->info.string_id = STR_VEHICLE_NAME_AIRCRAFT_SAMPSON_U52 + base;
00138 break;
00139 }
00140 }
00141
00142 Engine::~Engine()
00143 {
00144 UnloadWagonOverrides(this);
00145 free(this->name);
00146 }
00147
00152 bool Engine::IsEnabled() const
00153 {
00154 return this->info.string_id != STR_NEWGRF_INVALID_ENGINE;
00155 }
00156
00162 uint32 Engine::GetGRFID() const
00163 {
00164 const GRFFile *file = this->GetGRF();
00165 return file == NULL ? 0 : file->grfid;
00166 }
00167
00173 bool Engine::CanCarryCargo() const
00174 {
00175
00176
00177
00178
00179
00180 switch (this->type) {
00181 case VEH_TRAIN:
00182 if (this->u.rail.capacity == 0) return false;
00183 break;
00184
00185 case VEH_ROAD:
00186 if (this->u.road.capacity == 0) return false;
00187 break;
00188
00189 case VEH_SHIP:
00190 case VEH_AIRCRAFT:
00191 break;
00192
00193 default: NOT_REACHED();
00194 }
00195 return this->GetDefaultCargoType() != CT_INVALID;
00196 }
00197
00198
00206 uint Engine::DetermineCapacity(const Vehicle *v, uint16 *mail_capacity) const
00207 {
00208 assert(v == NULL || this->index == v->engine_type);
00209 if (mail_capacity != NULL) *mail_capacity = 0;
00210
00211 if (!this->CanCarryCargo()) return 0;
00212
00213 CargoID default_cargo = this->GetDefaultCargoType();
00214 CargoID cargo_type = (v != NULL) ? v->cargo_type : default_cargo;
00215
00216 if (mail_capacity != NULL && this->type == VEH_AIRCRAFT && IsCargoInClass(cargo_type, CC_PASSENGERS)) {
00217 *mail_capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity, v);
00218 }
00219
00220
00221
00222 if (HasBit(this->info.callback_mask, CBM_VEHICLE_REFIT_CAPACITY) &&
00223 (default_cargo != cargo_type || (v != NULL && v->cargo_subtype != 0))) {
00224 uint16 callback = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, this->index, v);
00225 if (callback != CALLBACK_FAILED) return callback;
00226 }
00227
00228
00229 uint capacity;
00230 switch (this->type) {
00231 case VEH_TRAIN:
00232 capacity = GetEngineProperty(this->index, PROP_TRAIN_CARGO_CAPACITY, this->u.rail.capacity, v);
00233
00234
00235 if (v == NULL && this->u.rail.railveh_type == RAILVEH_MULTIHEAD) capacity += this->u.rail.capacity;
00236 break;
00237
00238 case VEH_ROAD:
00239 capacity = GetEngineProperty(this->index, PROP_ROADVEH_CARGO_CAPACITY, this->u.road.capacity, v);
00240 break;
00241
00242 case VEH_SHIP:
00243 capacity = GetEngineProperty(this->index, PROP_SHIP_CARGO_CAPACITY, this->u.ship.capacity, v);
00244 break;
00245
00246 case VEH_AIRCRAFT:
00247 capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_PASSENGER_CAPACITY, this->u.air.passenger_capacity, v);
00248 if (!IsCargoInClass(cargo_type, CC_PASSENGERS)) {
00249 capacity += GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity, v);
00250 }
00251 if (cargo_type == CT_MAIL) return capacity;
00252 default_cargo = CT_PASSENGERS;
00253 break;
00254
00255 default: NOT_REACHED();
00256 }
00257
00258
00259
00260 if (this->type != VEH_SHIP && default_cargo != cargo_type) {
00261 switch (default_cargo) {
00262 case CT_PASSENGERS: break;
00263 case CT_MAIL:
00264 case CT_GOODS: capacity *= 2; break;
00265 default: capacity *= 4; break;
00266 }
00267 switch (cargo_type) {
00268 case CT_PASSENGERS: break;
00269 case CT_MAIL:
00270 case CT_GOODS: capacity /= 2; break;
00271 default: capacity /= 4; break;
00272 }
00273 }
00274
00275 return capacity;
00276 }
00277
00282 Money Engine::GetRunningCost() const
00283 {
00284 Price base_price;
00285 uint cost_factor;
00286 switch (this->type) {
00287 case VEH_ROAD:
00288 base_price = this->u.road.running_cost_class;
00289 if (base_price == INVALID_PRICE) return 0;
00290 cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_RUNNING_COST_FACTOR, this->u.road.running_cost);
00291 break;
00292
00293 case VEH_TRAIN:
00294 base_price = this->u.rail.running_cost_class;
00295 if (base_price == INVALID_PRICE) return 0;
00296 cost_factor = GetEngineProperty(this->index, PROP_TRAIN_RUNNING_COST_FACTOR, this->u.rail.running_cost);
00297 break;
00298
00299 case VEH_SHIP:
00300 base_price = PR_RUNNING_SHIP;
00301 cost_factor = GetEngineProperty(this->index, PROP_SHIP_RUNNING_COST_FACTOR, this->u.ship.running_cost);
00302 break;
00303
00304 case VEH_AIRCRAFT:
00305 base_price = PR_RUNNING_AIRCRAFT;
00306 cost_factor = GetEngineProperty(this->index, PROP_AIRCRAFT_RUNNING_COST_FACTOR, this->u.air.running_cost);
00307 break;
00308
00309 default: NOT_REACHED();
00310 }
00311
00312 return GetPrice(base_price, cost_factor, this->GetGRF(), -8);
00313 }
00314
00319 Money Engine::GetCost() const
00320 {
00321 Price base_price;
00322 uint cost_factor;
00323 switch (this->type) {
00324 case VEH_ROAD:
00325 base_price = PR_BUILD_VEHICLE_ROAD;
00326 cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_COST_FACTOR, this->u.road.cost_factor);
00327 break;
00328
00329 case VEH_TRAIN:
00330 if (this->u.rail.railveh_type == RAILVEH_WAGON) {
00331 base_price = PR_BUILD_VEHICLE_WAGON;
00332 cost_factor = GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor);
00333 } else {
00334 base_price = PR_BUILD_VEHICLE_TRAIN;
00335 cost_factor = GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor);
00336 }
00337 break;
00338
00339 case VEH_SHIP:
00340 base_price = PR_BUILD_VEHICLE_SHIP;
00341 cost_factor = GetEngineProperty(this->index, PROP_SHIP_COST_FACTOR, this->u.ship.cost_factor);
00342 break;
00343
00344 case VEH_AIRCRAFT:
00345 base_price = PR_BUILD_VEHICLE_AIRCRAFT;
00346 cost_factor = GetEngineProperty(this->index, PROP_AIRCRAFT_COST_FACTOR, this->u.air.cost_factor);
00347 break;
00348
00349 default: NOT_REACHED();
00350 }
00351
00352 return GetPrice(base_price, cost_factor, this->GetGRF(), -8);
00353 }
00354
00359 uint Engine::GetDisplayMaxSpeed() const
00360 {
00361 switch (this->type) {
00362 case VEH_TRAIN:
00363 return GetEngineProperty(this->index, PROP_TRAIN_SPEED, this->u.rail.max_speed);
00364
00365 case VEH_ROAD: {
00366 uint max_speed = GetEngineProperty(this->index, PROP_ROADVEH_SPEED, 0);
00367 return (max_speed != 0) ? max_speed * 2 : this->u.road.max_speed / 2;
00368 }
00369
00370 case VEH_SHIP:
00371 return GetEngineProperty(this->index, PROP_SHIP_SPEED, this->u.ship.max_speed) / 2;
00372
00373 case VEH_AIRCRAFT: {
00374 uint max_speed = GetEngineProperty(this->index, PROP_AIRCRAFT_SPEED, 0);
00375 if (max_speed != 0) {
00376 return (max_speed * 128) / 10;
00377 }
00378 return this->u.air.max_speed;
00379 }
00380
00381 default: NOT_REACHED();
00382 }
00383 }
00384
00391 uint Engine::GetPower() const
00392 {
00393
00394 switch (this->type) {
00395 case VEH_TRAIN:
00396 return GetEngineProperty(this->index, PROP_TRAIN_POWER, this->u.rail.power);
00397 case VEH_ROAD:
00398 return GetEngineProperty(this->index, PROP_ROADVEH_POWER, this->u.road.power) * 10;
00399
00400 default: NOT_REACHED();
00401 }
00402 }
00403
00409 uint Engine::GetDisplayWeight() const
00410 {
00411
00412 switch (this->type) {
00413 case VEH_TRAIN:
00414 return GetEngineProperty(this->index, PROP_TRAIN_WEIGHT, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0);
00415 case VEH_ROAD:
00416 return GetEngineProperty(this->index, PROP_ROADVEH_WEIGHT, this->u.road.weight) / 4;
00417
00418 default: NOT_REACHED();
00419 }
00420 }
00421
00427 uint Engine::GetDisplayMaxTractiveEffort() const
00428 {
00429
00430 switch (this->type) {
00431 case VEH_TRAIN:
00432 return (10 * this->GetDisplayWeight() * GetEngineProperty(this->index, PROP_TRAIN_TRACTIVE_EFFORT, this->u.rail.tractive_effort)) / 256;
00433 case VEH_ROAD:
00434 return (10 * this->GetDisplayWeight() * GetEngineProperty(this->index, PROP_ROADVEH_TRACTIVE_EFFORT, this->u.road.tractive_effort)) / 256;
00435
00436 default: NOT_REACHED();
00437 }
00438 }
00439
00444 Date Engine::GetLifeLengthInDays() const
00445 {
00446
00447 return (this->info.lifelength + _settings_game.vehicle.extend_vehicle_life) * DAYS_IN_LEAP_YEAR;
00448 }
00449
00454 uint16 Engine::GetRange() const
00455 {
00456 switch (this->type) {
00457 case VEH_AIRCRAFT:
00458 return GetEngineProperty(this->index, PROP_AIRCRAFT_RANGE, this->u.air.max_range);
00459
00460 default: NOT_REACHED();
00461 }
00462 }
00463
00467 void EngineOverrideManager::ResetToDefaultMapping()
00468 {
00469 this->Clear();
00470 for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) {
00471 for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++) {
00472 EngineIDMapping *eid = this->Append();
00473 eid->type = type;
00474 eid->grfid = INVALID_GRFID;
00475 eid->internal_id = internal_id;
00476 eid->substitute_id = internal_id;
00477 }
00478 }
00479 }
00480
00490 EngineID EngineOverrideManager::GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
00491 {
00492 const EngineIDMapping *end = this->End();
00493 EngineID index = 0;
00494 for (const EngineIDMapping *eid = this->Begin(); eid != end; eid++, index++) {
00495 if (eid->type == type && eid->grfid == grfid && eid->internal_id == grf_local_id) {
00496 return index;
00497 }
00498 }
00499 return INVALID_ENGINE;
00500 }
00501
00507 bool EngineOverrideManager::ResetToCurrentNewGRFConfig()
00508 {
00509 const Vehicle *v;
00510 FOR_ALL_VEHICLES(v) {
00511 if (IsCompanyBuildableVehicleType(v)) return false;
00512 }
00513
00514
00515 _engine_mngr.ResetToDefaultMapping();
00516 ReloadNewGRFData();
00517
00518 return true;
00519 }
00520
00524 void SetupEngines()
00525 {
00526 DeleteWindowByClass(WC_ENGINE_PREVIEW);
00527 _engine_pool.CleanPool();
00528
00529 assert(_engine_mngr.Length() >= _engine_mngr.NUM_DEFAULT_ENGINES);
00530 const EngineIDMapping *end = _engine_mngr.End();
00531 uint index = 0;
00532 for (const EngineIDMapping *eid = _engine_mngr.Begin(); eid != end; eid++, index++) {
00533
00534
00535 assert(Engine::CanAllocateItem());
00536 const Engine *e = new Engine(eid->type, eid->internal_id);
00537 assert(e->index == index);
00538 }
00539
00540 _introduced_railtypes = 0;
00541 }
00542
00546 static void CheckRailIntroduction()
00547 {
00548
00549 if (_introduced_railtypes == UINT16_MAX || Company::GetPoolSize() == 0) return;
00550
00551
00552 RailTypes rts = (RailTypes)UINT16_MAX;
00553
00554
00555 Company *c;
00556 FOR_ALL_COMPANIES(c) {
00557 c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes, _date);
00558 rts &= c->avail_railtypes;
00559 }
00560
00561 _introduced_railtypes |= rts;
00562 }
00563
00564 void ShowEnginePreviewWindow(EngineID engine);
00565
00571 static bool IsWagon(EngineID index)
00572 {
00573 const Engine *e = Engine::Get(index);
00574 return e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON;
00575 }
00576
00581 static void CalcEngineReliability(Engine *e)
00582 {
00583 uint age = e->age;
00584
00585
00586 if (e->company_avail != 0 && !_settings_game.vehicle.never_expire_vehicles && e->info.base_life != 0xFF) {
00587 int retire_early = e->info.retire_early;
00588 uint retire_early_max_age = max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
00589 if (retire_early != 0 && age >= retire_early_max_age) {
00590
00591 e->company_avail = 0;
00592 AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00593 }
00594 }
00595
00596 if (age < e->duration_phase_1) {
00597 uint start = e->reliability_start;
00598 e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
00599 } else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _settings_game.vehicle.never_expire_vehicles || e->info.base_life == 0xFF) {
00600
00601
00602 e->reliability = e->reliability_max;
00603 } else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
00604 uint max = e->reliability_max;
00605 e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
00606 } else {
00607
00608
00609 e->company_avail = 0;
00610 e->reliability = e->reliability_final;
00611
00612 AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00613 }
00614 SetWindowClassesDirty(WC_BUILD_VEHICLE);
00615 SetWindowClassesDirty(WC_REPLACE_VEHICLE);
00616 }
00617
00619 void SetYearEngineAgingStops()
00620 {
00621
00622 _year_engine_aging_stops = 2050;
00623
00624 const Engine *e;
00625 FOR_ALL_ENGINES(e) {
00626 const EngineInfo *ei = &e->info;
00627
00628
00629 if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) continue;
00630 if (e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON) continue;
00631
00632
00633 YearMonthDay ymd;
00634 ConvertDateToYMD(ei->base_intro + (ei->lifelength * DAYS_IN_LEAP_YEAR) / 2, &ymd);
00635
00636 _year_engine_aging_stops = max(_year_engine_aging_stops, ymd.year);
00637 }
00638 }
00639
00645 void StartupOneEngine(Engine *e, Date aging_date)
00646 {
00647 const EngineInfo *ei = &e->info;
00648
00649 e->age = 0;
00650 e->flags = 0;
00651 e->company_avail = 0;
00652
00653
00654
00655
00656 uint32 r = Random();
00657 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;
00658 if (e->intro_date <= _date) {
00659 e->age = (aging_date - e->intro_date) >> 5;
00660 e->company_avail = (CompanyMask)-1;
00661 e->flags |= ENGINE_AVAILABLE;
00662 }
00663
00664 e->reliability_start = GB(r, 16, 14) + 0x7AE0;
00665 r = Random();
00666 e->reliability_max = GB(r, 0, 14) + 0xBFFF;
00667 e->reliability_final = GB(r, 16, 14) + 0x3FFF;
00668
00669 r = Random();
00670 e->duration_phase_1 = GB(r, 0, 5) + 7;
00671 e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
00672 e->duration_phase_3 = GB(r, 9, 7) + 120;
00673
00674 e->reliability_spd_dec = ei->decay_speed << 2;
00675
00676 CalcEngineReliability(e);
00677
00678
00679 if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) {
00680 e->flags |= ENGINE_AVAILABLE;
00681 e->company_avail = 0;
00682 }
00683 }
00684
00689 void StartupEngines()
00690 {
00691 Engine *e;
00692
00693 const Date aging_date = min(_date, ConvertYMDToDate(_year_engine_aging_stops, 0, 1));
00694
00695 FOR_ALL_ENGINES(e) {
00696 StartupOneEngine(e, aging_date);
00697 }
00698
00699
00700 Company *c;
00701 FOR_ALL_COMPANIES(c) {
00702 c->avail_railtypes = GetCompanyRailtypes(c->index);
00703 c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00704 }
00705
00706
00707
00708
00709
00710 for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
00711 const RailtypeInfo *rti = GetRailTypeInfo(rt);
00712 if (rti->label != 0 && IsInsideMM(rti->introduction_date, 0, MAX_DAY)) continue;
00713
00714 SetBit(_introduced_railtypes, rt);
00715 }
00716
00717 CheckRailIntroduction();
00718
00719
00720 InvalidateWindowClassesData(WC_BUILD_VEHICLE);
00721 }
00722
00728 static void AcceptEnginePreview(EngineID eid, CompanyID company)
00729 {
00730 Engine *e = Engine::Get(eid);
00731 Company *c = Company::Get(company);
00732
00733 SetBit(e->company_avail, company);
00734 if (e->type == VEH_TRAIN) {
00735 assert(e->u.rail.railtype < RAILTYPE_END);
00736 c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes | GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes, _date);
00737 } else if (e->type == VEH_ROAD) {
00738 SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00739 }
00740
00741 e->preview_company_rank = 0xFF;
00742 if (company == _local_company) {
00743 AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00744 }
00745
00746
00747 if (e->type == VEH_ROAD) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_ROAD);
00748 if (e->type == VEH_SHIP) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_WATER);
00749 }
00750
00756 static CompanyID GetBestCompany(uint8 pp)
00757 {
00758 CompanyID best_company;
00759 CompanyMask mask = 0;
00760
00761 do {
00762 int32 best_hist = -1;
00763 best_company = INVALID_COMPANY;
00764
00765 const Company *c;
00766 FOR_ALL_COMPANIES(c) {
00767 if (c->block_preview == 0 && !HasBit(mask, c->index) &&
00768 c->old_economy[0].performance_history > best_hist) {
00769 best_hist = c->old_economy[0].performance_history;
00770 best_company = c->index;
00771 }
00772 }
00773
00774 if (best_company == INVALID_COMPANY) return INVALID_COMPANY;
00775
00776 SetBit(mask, best_company);
00777 } while (--pp != 0);
00778
00779 return best_company;
00780 }
00781
00783 void EnginesDailyLoop()
00784 {
00785 CheckRailIntroduction();
00786
00787 if (_cur_year >= _year_engine_aging_stops) return;
00788
00789 Engine *e;
00790 FOR_ALL_ENGINES(e) {
00791 EngineID i = e->index;
00792 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
00793 if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
00794 if (e->preview_company_rank != 0xFF && !--e->preview_wait) {
00795 e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
00796 DeleteWindowById(WC_ENGINE_PREVIEW, i);
00797 e->preview_company_rank++;
00798 }
00799 } else if (e->preview_company_rank != 0xFF) {
00800 CompanyID best_company = GetBestCompany(e->preview_company_rank);
00801
00802 if (best_company == INVALID_COMPANY) {
00803 e->preview_company_rank = 0xFF;
00804 continue;
00805 }
00806
00807 e->flags |= ENGINE_OFFER_WINDOW_OPEN;
00808 e->preview_wait = 20;
00809 AI::NewEvent(best_company, new ScriptEventEnginePreview(i));
00810 if (IsInteractiveCompany(best_company)) ShowEnginePreviewWindow(i);
00811 }
00812 }
00813 }
00814 }
00815
00826 CommandCost CmdWantEnginePreview(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00827 {
00828 Engine *e = Engine::GetIfValid(p1);
00829 if (e == NULL || GetBestCompany(e->preview_company_rank) != _current_company) return CMD_ERROR;
00830
00831 if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_company);
00832
00833 return CommandCost();
00834 }
00835
00841 static void NewVehicleAvailable(Engine *e)
00842 {
00843 Vehicle *v;
00844 Company *c;
00845 EngineID index = e->index;
00846
00847
00848
00849 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
00850 FOR_ALL_COMPANIES(c) {
00851 uint block_preview = c->block_preview;
00852
00853 if (!HasBit(e->company_avail, c->index)) continue;
00854
00855
00856 c->block_preview = 20;
00857
00858 FOR_ALL_VEHICLES(v) {
00859 if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
00860 (v->type == VEH_AIRCRAFT && Aircraft::From(v)->IsNormalAircraft())) {
00861 if (v->owner == c->index && v->engine_type == index) {
00862
00863 c->block_preview = block_preview;
00864 break;
00865 }
00866 }
00867 }
00868 }
00869 }
00870
00871 e->flags = (e->flags & ~ENGINE_EXCLUSIVE_PREVIEW) | ENGINE_AVAILABLE;
00872 AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00873
00874
00875 e->company_avail = (CompanyMask)-1;
00876
00877
00878 if (IsWagon(index)) return;
00879
00880 if (e->type == VEH_TRAIN) {
00881
00882 RailType railtype = e->u.rail.railtype;
00883 assert(railtype < RAILTYPE_END);
00884 FOR_ALL_COMPANIES(c) c->avail_railtypes = AddDateIntroducedRailTypes(c->avail_railtypes | GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes, _date);
00885 } else if (e->type == VEH_ROAD) {
00886
00887 FOR_ALL_COMPANIES(c) SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00888 }
00889
00890 AI::BroadcastNewEvent(new ScriptEventEngineAvailable(index));
00891
00892 SetDParam(0, GetEngineCategoryName(index));
00893 SetDParam(1, index);
00894 AddNewsItem(STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NS_NEW_VEHICLES, NR_ENGINE, index);
00895
00896
00897 if (e->type == VEH_ROAD) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_ROAD);
00898 if (e->type == VEH_SHIP) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_WATER);
00899 }
00900
00901 void EnginesMonthlyLoop()
00902 {
00903 if (_cur_year < _year_engine_aging_stops) {
00904 Engine *e;
00905 FOR_ALL_ENGINES(e) {
00906
00907 if ((e->flags & ENGINE_AVAILABLE) && e->age != MAX_DAY) {
00908 e->age++;
00909 CalcEngineReliability(e);
00910 }
00911
00912
00913 if (!e->IsEnabled()) continue;
00914
00915 if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + DAYS_IN_YEAR)) {
00916
00917 NewVehicleAvailable(e);
00918 } else if (!(e->flags & (ENGINE_AVAILABLE | ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
00919
00920 e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
00921
00922
00923 if (!IsWagon(e->index)) {
00924 e->preview_company_rank = 1;
00925 }
00926 }
00927 }
00928 }
00929 }
00930
00931 static bool IsUniqueEngineName(const char *name)
00932 {
00933 const Engine *e;
00934
00935 FOR_ALL_ENGINES(e) {
00936 if (e->name != NULL && strcmp(e->name, name) == 0) return false;
00937 }
00938
00939 return true;
00940 }
00941
00951 CommandCost CmdRenameEngine(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00952 {
00953 Engine *e = Engine::GetIfValid(p1);
00954 if (e == NULL) return CMD_ERROR;
00955
00956 bool reset = StrEmpty(text);
00957
00958 if (!reset) {
00959 if (Utf8StringLength(text) >= MAX_LENGTH_ENGINE_NAME_CHARS) return CMD_ERROR;
00960 if (!IsUniqueEngineName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
00961 }
00962
00963 if (flags & DC_EXEC) {
00964 free(e->name);
00965
00966 if (reset) {
00967 e->name = NULL;
00968 } else {
00969 e->name = strdup(text);
00970 }
00971
00972 MarkWholeScreenDirty();
00973 }
00974
00975 return CommandCost();
00976 }
00977
00978
00987 bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
00988 {
00989 const Engine *e = Engine::GetIfValid(engine);
00990
00991
00992 if (e == NULL) return false;
00993
00994
00995 if (e->type != type) return false;
00996
00997
00998 if (company != OWNER_DEITY && !HasBit(e->company_avail, company)) return false;
00999
01000 if (!e->IsEnabled()) return false;
01001
01002 if (type == VEH_TRAIN && company != OWNER_DEITY) {
01003
01004 const Company *c = Company::Get(company);
01005 if (((GetRailTypeInfo(e->u.rail.railtype))->compatible_railtypes & c->avail_railtypes) == 0) return false;
01006 }
01007
01008 return true;
01009 }
01010
01017 bool IsEngineRefittable(EngineID engine)
01018 {
01019 const Engine *e = Engine::GetIfValid(engine);
01020
01021
01022 if (e == NULL) return false;
01023
01024 if (!e->CanCarryCargo()) return false;
01025
01026 const EngineInfo *ei = &e->info;
01027 if (ei->refit_mask == 0) return false;
01028
01029
01030
01031 if (HasBit(ei->callback_mask, CBM_VEHICLE_CARGO_SUFFIX)) return true;
01032
01033
01034 CargoID default_cargo = e->GetDefaultCargoType();
01035 return default_cargo != CT_INVALID && ei->refit_mask != 1U << default_cargo;
01036 }