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
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
00048 const uint8 _engine_counts[4] = {
00049 lengthof(_orig_rail_vehicle_info),
00050 lengthof(_orig_road_vehicle_info),
00051 lengthof(_orig_ship_vehicle_info),
00052 lengthof(_orig_aircraft_vehicle_info),
00053 };
00054
00056 const uint8 _engine_offsets[4] = {
00057 0,
00058 lengthof(_orig_rail_vehicle_info),
00059 lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info),
00060 lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info),
00061 };
00062
00063 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));
00064
00065 const uint EngineOverrideManager::NUM_DEFAULT_ENGINES = _engine_counts[VEH_TRAIN] + _engine_counts[VEH_ROAD] + _engine_counts[VEH_SHIP] + _engine_counts[VEH_AIRCRAFT];
00066
00067 Engine::Engine() :
00068 name(NULL),
00069 overrides_count(0),
00070 overrides(NULL)
00071 {
00072 }
00073
00074 Engine::Engine(VehicleType type, EngineID base)
00075 {
00076 this->type = type;
00077 this->grf_prop.local_id = base;
00078 this->list_position = base;
00079
00080
00081 if (base >= _engine_counts[type]) {
00082
00083 this->info.climates = 0x80;
00084
00085 this->info.base_life = 0xFF;
00086
00087 if (type == VEH_ROAD) this->u.road.tractive_effort = 0x4C;
00088
00089 switch (type) {
00090 case VEH_TRAIN: this->u.rail.visual_effect = VE_DEFAULT; break;
00091 case VEH_ROAD: this->u.road.visual_effect = VE_DEFAULT; break;
00092 case VEH_SHIP: this->u.ship.visual_effect = VE_DEFAULT; break;
00093 default: break;
00094 }
00095 return;
00096 }
00097
00098
00099 this->info = _orig_engine_info[_engine_offsets[type] + base];
00100
00101
00102 switch (type) {
00103 default: NOT_REACHED();
00104
00105 case VEH_TRAIN:
00106 this->u.rail = _orig_rail_vehicle_info[base];
00107 this->original_image_index = this->u.rail.image_index;
00108 this->info.string_id = STR_VEHICLE_NAME_TRAIN_ENGINE_RAIL_KIRBY_PAUL_TANK_STEAM + base;
00109
00110
00111 if (this->u.rail.railveh_type == RAILVEH_WAGON) this->info.base_life = 0xFF;
00112
00113 break;
00114
00115 case VEH_ROAD:
00116 this->u.road = _orig_road_vehicle_info[base];
00117 this->original_image_index = this->u.road.image_index;
00118 this->info.string_id = STR_VEHICLE_NAME_ROAD_VEHICLE_MPS_REGAL_BUS + base;
00119 break;
00120
00121 case VEH_SHIP:
00122 this->u.ship = _orig_ship_vehicle_info[base];
00123 this->original_image_index = this->u.ship.image_index;
00124 this->info.string_id = STR_VEHICLE_NAME_SHIP_MPS_OIL_TANKER + base;
00125 break;
00126
00127 case VEH_AIRCRAFT:
00128 this->u.air = _orig_aircraft_vehicle_info[base];
00129 this->original_image_index = this->u.air.image_index;
00130 this->info.string_id = STR_VEHICLE_NAME_AIRCRAFT_SAMPSON_U52 + base;
00131 break;
00132 }
00133 }
00134
00135 Engine::~Engine()
00136 {
00137 UnloadWagonOverrides(this);
00138 free(this->name);
00139 }
00140
00146 bool Engine::CanCarryCargo() const
00147 {
00148
00149
00150
00151
00152
00153 switch (this->type) {
00154 case VEH_TRAIN:
00155 if (this->u.rail.capacity == 0) return false;
00156 break;
00157
00158 case VEH_ROAD:
00159 if (this->u.road.capacity == 0) return false;
00160 break;
00161
00162 case VEH_SHIP:
00163 case VEH_AIRCRAFT:
00164 break;
00165
00166 default: NOT_REACHED();
00167 }
00168 return this->GetDefaultCargoType() != CT_INVALID;
00169 }
00170
00183 uint Engine::GetDisplayDefaultCapacity(uint16 *mail_capacity) const
00184 {
00185 if (mail_capacity != NULL) *mail_capacity = 0;
00186 if (!this->CanCarryCargo()) return 0;
00187 switch (type) {
00188 case VEH_TRAIN:
00189 return GetEngineProperty(this->index, PROP_TRAIN_CARGO_CAPACITY, this->u.rail.capacity) + (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? this->u.rail.capacity : 0);
00190
00191 case VEH_ROAD:
00192 return GetEngineProperty(this->index, PROP_ROADVEH_CARGO_CAPACITY, this->u.road.capacity);
00193
00194 case VEH_SHIP:
00195 return GetEngineProperty(this->index, PROP_SHIP_CARGO_CAPACITY, this->u.ship.capacity);
00196
00197 case VEH_AIRCRAFT: {
00198 uint capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_PASSENGER_CAPACITY, this->u.air.passenger_capacity);
00199 CargoID cargo = this->GetDefaultCargoType();
00200 if (IsCargoInClass(cargo, CC_PASSENGERS)) {
00201 if (mail_capacity != NULL) *mail_capacity = GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity);
00202 } else {
00203 capacity += GetEngineProperty(this->index, PROP_AIRCRAFT_MAIL_CAPACITY, this->u.air.mail_capacity);
00204 }
00205 switch (cargo) {
00206 case CT_PASSENGERS:
00207 case CT_MAIL: return capacity;
00208 case CT_GOODS: return capacity / 2;
00209 default: return capacity / 4;
00210 }
00211 }
00212
00213 default: NOT_REACHED();
00214 }
00215 }
00216
00221 Money Engine::GetRunningCost() const
00222 {
00223 Price base_price;
00224 uint cost_factor;
00225 switch (this->type) {
00226 case VEH_ROAD:
00227 base_price = this->u.road.running_cost_class;
00228 if (base_price == INVALID_PRICE) return 0;
00229 cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_RUNNING_COST_FACTOR, this->u.road.running_cost);
00230 break;
00231
00232 case VEH_TRAIN:
00233 base_price = this->u.rail.running_cost_class;
00234 if (base_price == INVALID_PRICE) return 0;
00235 cost_factor = GetEngineProperty(this->index, PROP_TRAIN_RUNNING_COST_FACTOR, this->u.rail.running_cost);
00236 break;
00237
00238 case VEH_SHIP:
00239 base_price = PR_RUNNING_SHIP;
00240 cost_factor = GetEngineProperty(this->index, PROP_SHIP_RUNNING_COST_FACTOR, this->u.ship.running_cost);
00241 break;
00242
00243 case VEH_AIRCRAFT:
00244 base_price = PR_RUNNING_AIRCRAFT;
00245 cost_factor = GetEngineProperty(this->index, PROP_AIRCRAFT_RUNNING_COST_FACTOR, this->u.air.running_cost);
00246 break;
00247
00248 default: NOT_REACHED();
00249 }
00250
00251 return GetPrice(base_price, cost_factor, this->grf_prop.grffile, -8);
00252 }
00253
00258 Money Engine::GetCost() const
00259 {
00260 Price base_price;
00261 uint cost_factor;
00262 switch (this->type) {
00263 case VEH_ROAD:
00264 base_price = PR_BUILD_VEHICLE_ROAD;
00265 cost_factor = GetEngineProperty(this->index, PROP_ROADVEH_COST_FACTOR, this->u.road.cost_factor);
00266 break;
00267
00268 case VEH_TRAIN:
00269 if (this->u.rail.railveh_type == RAILVEH_WAGON) {
00270 base_price = PR_BUILD_VEHICLE_WAGON;
00271 cost_factor = GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor);
00272 } else {
00273 base_price = PR_BUILD_VEHICLE_TRAIN;
00274 cost_factor = GetEngineProperty(this->index, PROP_TRAIN_COST_FACTOR, this->u.rail.cost_factor);
00275 }
00276 break;
00277
00278 case VEH_SHIP:
00279 base_price = PR_BUILD_VEHICLE_SHIP;
00280 cost_factor = GetEngineProperty(this->index, PROP_SHIP_COST_FACTOR, this->u.ship.cost_factor);
00281 break;
00282
00283 case VEH_AIRCRAFT:
00284 base_price = PR_BUILD_VEHICLE_AIRCRAFT;
00285 cost_factor = GetEngineProperty(this->index, PROP_AIRCRAFT_COST_FACTOR, this->u.air.cost_factor);
00286 break;
00287
00288 default: NOT_REACHED();
00289 }
00290
00291 return GetPrice(base_price, cost_factor, this->grf_prop.grffile, -8);
00292 }
00293
00298 uint Engine::GetDisplayMaxSpeed() const
00299 {
00300 switch (this->type) {
00301 case VEH_TRAIN:
00302 return GetEngineProperty(this->index, PROP_TRAIN_SPEED, this->u.rail.max_speed);
00303
00304 case VEH_ROAD: {
00305 uint max_speed = GetEngineProperty(this->index, PROP_ROADVEH_SPEED, 0);
00306 return (max_speed != 0) ? max_speed * 2 : this->u.road.max_speed / 2;
00307 }
00308
00309 case VEH_SHIP:
00310 return GetEngineProperty(this->index, PROP_SHIP_SPEED, this->u.ship.max_speed) / 2;
00311
00312 case VEH_AIRCRAFT: {
00313 uint max_speed = GetEngineProperty(this->index, PROP_AIRCRAFT_SPEED, 0);
00314 if (max_speed != 0) {
00315 return (max_speed * 128) / 10;
00316 }
00317 return this->u.air.max_speed;
00318 }
00319
00320 default: NOT_REACHED();
00321 }
00322 }
00323
00330 uint Engine::GetPower() const
00331 {
00332
00333 switch (this->type) {
00334 case VEH_TRAIN:
00335 return GetEngineProperty(this->index, PROP_TRAIN_POWER, this->u.rail.power);
00336 case VEH_ROAD:
00337 return GetEngineProperty(this->index, PROP_ROADVEH_POWER, this->u.road.power) * 10;
00338
00339 default: NOT_REACHED();
00340 }
00341 }
00342
00348 uint Engine::GetDisplayWeight() const
00349 {
00350
00351 switch (this->type) {
00352 case VEH_TRAIN:
00353 return GetEngineProperty(this->index, PROP_TRAIN_WEIGHT, this->u.rail.weight) << (this->u.rail.railveh_type == RAILVEH_MULTIHEAD ? 1 : 0);
00354 case VEH_ROAD:
00355 return GetEngineProperty(this->index, PROP_ROADVEH_WEIGHT, this->u.road.weight) / 4;
00356
00357 default: NOT_REACHED();
00358 }
00359 }
00360
00366 uint Engine::GetDisplayMaxTractiveEffort() const
00367 {
00368
00369 switch (this->type) {
00370 case VEH_TRAIN:
00371 return (10 * this->GetDisplayWeight() * GetEngineProperty(this->index, PROP_TRAIN_TRACTIVE_EFFORT, this->u.rail.tractive_effort)) / 256;
00372 case VEH_ROAD:
00373 return (10 * this->GetDisplayWeight() * GetEngineProperty(this->index, PROP_ROADVEH_TRACTIVE_EFFORT, this->u.road.tractive_effort)) / 256;
00374
00375 default: NOT_REACHED();
00376 }
00377 }
00378
00383 Date Engine::GetLifeLengthInDays() const
00384 {
00385
00386 return (this->info.lifelength + _settings_game.vehicle.extend_vehicle_life) * DAYS_IN_LEAP_YEAR;
00387 }
00388
00392 void EngineOverrideManager::ResetToDefaultMapping()
00393 {
00394 this->Clear();
00395 for (VehicleType type = VEH_TRAIN; type <= VEH_AIRCRAFT; type++) {
00396 for (uint internal_id = 0; internal_id < _engine_counts[type]; internal_id++) {
00397 EngineIDMapping *eid = this->Append();
00398 eid->type = type;
00399 eid->grfid = INVALID_GRFID;
00400 eid->internal_id = internal_id;
00401 eid->substitute_id = internal_id;
00402 }
00403 }
00404 }
00405
00415 EngineID EngineOverrideManager::GetID(VehicleType type, uint16 grf_local_id, uint32 grfid)
00416 {
00417 const EngineIDMapping *end = this->End();
00418 EngineID index = 0;
00419 for (const EngineIDMapping *eid = this->Begin(); eid != end; eid++, index++) {
00420 if (eid->type == type && eid->grfid == grfid && eid->internal_id == grf_local_id) {
00421 return index;
00422 }
00423 }
00424 return INVALID_ENGINE;
00425 }
00426
00430 void SetCachedEngineCounts()
00431 {
00432 size_t engines = Engine::GetPoolSize();
00433
00434
00435 Company *c;
00436 FOR_ALL_COMPANIES(c) {
00437 free(c->num_engines);
00438 c->num_engines = CallocT<EngineID>(engines);
00439 }
00440
00441
00442 Group *g;
00443 FOR_ALL_GROUPS(g) {
00444 free(g->num_engines);
00445 g->num_engines = CallocT<EngineID>(engines);
00446 }
00447
00448 const Vehicle *v;
00449 FOR_ALL_VEHICLES(v) {
00450 if (!v->IsEngineCountable()) continue;
00451
00452 assert(v->engine_type < engines);
00453
00454 Company::Get(v->owner)->num_engines[v->engine_type]++;
00455
00456 if (v->group_id == DEFAULT_GROUP) continue;
00457
00458 g = Group::Get(v->group_id);
00459 assert(v->type == g->vehicle_type);
00460 assert(v->owner == g->owner);
00461
00462 g->num_engines[v->engine_type]++;
00463 }
00464 }
00465
00466 void SetupEngines()
00467 {
00468 _engine_pool.CleanPool();
00469
00470 assert(_engine_mngr.Length() >= _engine_mngr.NUM_DEFAULT_ENGINES);
00471 const EngineIDMapping *end = _engine_mngr.End();
00472 uint index = 0;
00473 for (const EngineIDMapping *eid = _engine_mngr.Begin(); eid != end; eid++, index++) {
00474 const Engine *e = new Engine(eid->type, eid->internal_id);
00475 assert(e->index == index);
00476 }
00477 }
00478
00479
00480 void ShowEnginePreviewWindow(EngineID engine);
00481
00482
00483 static bool IsWagon(EngineID index)
00484 {
00485 const Engine *e = Engine::Get(index);
00486 return e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON;
00487 }
00488
00489 static void CalcEngineReliability(Engine *e)
00490 {
00491 uint age = e->age;
00492
00493
00494 if (e->company_avail != 0 && !_settings_game.vehicle.never_expire_vehicles && e->info.base_life != 0xFF) {
00495 int retire_early = e->info.retire_early;
00496 uint retire_early_max_age = max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
00497 if (retire_early != 0 && age >= retire_early_max_age) {
00498
00499 e->company_avail = 0;
00500 AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00501 }
00502 }
00503
00504 if (age < e->duration_phase_1) {
00505 uint start = e->reliability_start;
00506 e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
00507 } else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _settings_game.vehicle.never_expire_vehicles || e->info.base_life == 0xFF) {
00508
00509
00510 e->reliability = e->reliability_max;
00511 } else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
00512 uint max = e->reliability_max;
00513 e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
00514 } else {
00515
00516
00517 e->company_avail = 0;
00518 e->reliability = e->reliability_final;
00519
00520 AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00521 }
00522 SetWindowClassesDirty(WC_BUILD_VEHICLE);
00523 SetWindowClassesDirty(WC_REPLACE_VEHICLE);
00524 }
00525
00527 void SetYearEngineAgingStops()
00528 {
00529
00530 _year_engine_aging_stops = 2050;
00531
00532 const Engine *e;
00533 FOR_ALL_ENGINES(e) {
00534 const EngineInfo *ei = &e->info;
00535
00536
00537 if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) continue;
00538 if (e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON) continue;
00539
00540
00541 YearMonthDay ymd;
00542 ConvertDateToYMD(ei->base_intro + (ei->lifelength * DAYS_IN_LEAP_YEAR) / 2, &ymd);
00543
00544 _year_engine_aging_stops = max(_year_engine_aging_stops, ymd.year);
00545 }
00546 }
00547
00548 void StartupOneEngine(Engine *e, Date aging_date)
00549 {
00550 const EngineInfo *ei = &e->info;
00551
00552 e->age = 0;
00553 e->flags = 0;
00554 e->company_avail = 0;
00555
00556
00557
00558
00559 uint32 r = Random();
00560 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;
00561 if (e->intro_date <= _date) {
00562 e->age = (aging_date - e->intro_date) >> 5;
00563 e->company_avail = (CompanyMask)-1;
00564 e->flags |= ENGINE_AVAILABLE;
00565 }
00566
00567 e->reliability_start = GB(r, 16, 14) + 0x7AE0;
00568 r = Random();
00569 e->reliability_max = GB(r, 0, 14) + 0xBFFF;
00570 e->reliability_final = GB(r, 16, 14) + 0x3FFF;
00571
00572 r = Random();
00573 e->duration_phase_1 = GB(r, 0, 5) + 7;
00574 e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
00575 e->duration_phase_3 = GB(r, 9, 7) + 120;
00576
00577 e->reliability_spd_dec = ei->decay_speed << 2;
00578
00579 CalcEngineReliability(e);
00580
00581
00582 if (!HasBit(ei->climates, _settings_game.game_creation.landscape)) {
00583 e->flags |= ENGINE_AVAILABLE;
00584 e->company_avail = 0;
00585 }
00586 }
00587
00588 void StartupEngines()
00589 {
00590 Engine *e;
00591
00592 const Date aging_date = min(_date, ConvertYMDToDate(_year_engine_aging_stops, 0, 1));
00593
00594 FOR_ALL_ENGINES(e) {
00595 StartupOneEngine(e, aging_date);
00596 }
00597
00598
00599 Company *c;
00600 FOR_ALL_COMPANIES(c) {
00601 c->avail_railtypes = GetCompanyRailtypes(c->index);
00602 c->avail_roadtypes = GetCompanyRoadtypes(c->index);
00603 }
00604
00605
00606 InvalidateWindowClassesData(WC_BUILD_VEHICLE);
00607 }
00608
00609 static void AcceptEnginePreview(EngineID eid, CompanyID company)
00610 {
00611 Engine *e = Engine::Get(eid);
00612 Company *c = Company::Get(company);
00613
00614 SetBit(e->company_avail, company);
00615 if (e->type == VEH_TRAIN) {
00616 assert(e->u.rail.railtype < RAILTYPE_END);
00617 SetBit(c->avail_railtypes, e->u.rail.railtype);
00618 } else if (e->type == VEH_ROAD) {
00619 SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00620 }
00621
00622 e->preview_company_rank = 0xFF;
00623 if (company == _local_company) {
00624 AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00625 }
00626
00627
00628 if (e->type == VEH_ROAD) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_ROAD);
00629 if (e->type == VEH_SHIP) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_WATER);
00630 }
00631
00637 static CompanyID GetBestCompany(uint8 pp)
00638 {
00639 CompanyID best_company;
00640 CompanyMask mask = 0;
00641
00642 do {
00643 int32 best_hist = -1;
00644 best_company = INVALID_COMPANY;
00645
00646 const Company *c;
00647 FOR_ALL_COMPANIES(c) {
00648 if (c->block_preview == 0 && !HasBit(mask, c->index) &&
00649 c->old_economy[0].performance_history > best_hist) {
00650 best_hist = c->old_economy[0].performance_history;
00651 best_company = c->index;
00652 }
00653 }
00654
00655 if (best_company == INVALID_COMPANY) return INVALID_COMPANY;
00656
00657 SetBit(mask, best_company);
00658 } while (--pp != 0);
00659
00660 return best_company;
00661 }
00662
00664 void EnginesDailyLoop()
00665 {
00666 if (_cur_year >= _year_engine_aging_stops) return;
00667
00668 Engine *e;
00669 FOR_ALL_ENGINES(e) {
00670 EngineID i = e->index;
00671 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
00672 if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
00673 if (e->preview_company_rank != 0xFF && !--e->preview_wait) {
00674 e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
00675 DeleteWindowById(WC_ENGINE_PREVIEW, i);
00676 e->preview_company_rank++;
00677 }
00678 } else if (e->preview_company_rank != 0xFF) {
00679 CompanyID best_company = GetBestCompany(e->preview_company_rank);
00680
00681 if (best_company == INVALID_COMPANY) {
00682 e->preview_company_rank = 0xFF;
00683 continue;
00684 }
00685
00686 e->flags |= ENGINE_OFFER_WINDOW_OPEN;
00687 e->preview_wait = 20;
00688 AI::NewEvent(best_company, new AIEventEnginePreview(i));
00689 if (IsInteractiveCompany(best_company)) ShowEnginePreviewWindow(i);
00690 }
00691 }
00692 }
00693 }
00694
00705 CommandCost CmdWantEnginePreview(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00706 {
00707 Engine *e = Engine::GetIfValid(p1);
00708 if (e == NULL || GetBestCompany(e->preview_company_rank) != _current_company) return CMD_ERROR;
00709
00710 if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_company);
00711
00712 return CommandCost();
00713 }
00714
00720 static void NewVehicleAvailable(Engine *e)
00721 {
00722 Vehicle *v;
00723 Company *c;
00724 EngineID index = e->index;
00725
00726
00727
00728 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
00729 FOR_ALL_COMPANIES(c) {
00730 uint block_preview = c->block_preview;
00731
00732 if (!HasBit(e->company_avail, c->index)) continue;
00733
00734
00735 c->block_preview = 20;
00736
00737 FOR_ALL_VEHICLES(v) {
00738 if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
00739 (v->type == VEH_AIRCRAFT && Aircraft::From(v)->IsNormalAircraft())) {
00740 if (v->owner == c->index && v->engine_type == index) {
00741
00742 c->block_preview = block_preview;
00743 break;
00744 }
00745 }
00746 }
00747 }
00748 }
00749
00750 e->flags = (e->flags & ~ENGINE_EXCLUSIVE_PREVIEW) | ENGINE_AVAILABLE;
00751 AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
00752
00753
00754 e->company_avail = (CompanyMask)-1;
00755
00756
00757 if (IsWagon(index)) return;
00758
00759 if (e->type == VEH_TRAIN) {
00760
00761 RailType railtype = e->u.rail.railtype;
00762 assert(railtype < RAILTYPE_END);
00763 FOR_ALL_COMPANIES(c) SetBit(c->avail_railtypes, railtype);
00764 } else if (e->type == VEH_ROAD) {
00765
00766 FOR_ALL_COMPANIES(c) SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
00767 }
00768
00769 AI::BroadcastNewEvent(new AIEventEngineAvailable(index));
00770
00771 SetDParam(0, GetEngineCategoryName(index));
00772 SetDParam(1, index);
00773 AddNewsItem(STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NS_NEW_VEHICLES, NR_ENGINE, index);
00774
00775
00776 if (e->type == VEH_ROAD) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_ROAD);
00777 if (e->type == VEH_SHIP) InvalidateWindowData(WC_BUILD_TOOLBAR, TRANSPORT_WATER);
00778 }
00779
00780 void EnginesMonthlyLoop()
00781 {
00782 if (_cur_year < _year_engine_aging_stops) {
00783 Engine *e;
00784 FOR_ALL_ENGINES(e) {
00785
00786 if ((e->flags & ENGINE_AVAILABLE) && e->age != MAX_DAY) {
00787 e->age++;
00788 CalcEngineReliability(e);
00789 }
00790
00791 if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + DAYS_IN_YEAR)) {
00792
00793 NewVehicleAvailable(e);
00794 } else if (!(e->flags & (ENGINE_AVAILABLE | ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
00795
00796 e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
00797
00798
00799 if (!IsWagon(e->index)) {
00800 e->preview_company_rank = 1;
00801 }
00802 }
00803 }
00804 }
00805 }
00806
00807 static bool IsUniqueEngineName(const char *name)
00808 {
00809 const Engine *e;
00810
00811 FOR_ALL_ENGINES(e) {
00812 if (e->name != NULL && strcmp(e->name, name) == 0) return false;
00813 }
00814
00815 return true;
00816 }
00817
00827 CommandCost CmdRenameEngine(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00828 {
00829 Engine *e = Engine::GetIfValid(p1);
00830 if (e == NULL) return CMD_ERROR;
00831
00832 bool reset = StrEmpty(text);
00833
00834 if (!reset) {
00835 if (Utf8StringLength(text) >= MAX_LENGTH_ENGINE_NAME_CHARS) return CMD_ERROR;
00836 if (!IsUniqueEngineName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
00837 }
00838
00839 if (flags & DC_EXEC) {
00840 free(e->name);
00841
00842 if (reset) {
00843 e->name = NULL;
00844 } else {
00845 e->name = strdup(text);
00846 }
00847
00848 MarkWholeScreenDirty();
00849 }
00850
00851 return CommandCost();
00852 }
00853
00854
00863 bool IsEngineBuildable(EngineID engine, VehicleType type, CompanyID company)
00864 {
00865 const Engine *e = Engine::GetIfValid(engine);
00866
00867
00868 if (e == NULL) return false;
00869
00870
00871 if (e->type != type) return false;
00872
00873
00874 if (!HasBit(e->company_avail, company)) return false;
00875
00876 if (e->info.string_id == STR_NEWGRF_INVALID_ENGINE) return false;
00877
00878 if (type == VEH_TRAIN) {
00879
00880 const Company *c = Company::Get(company);
00881 if (((GetRailTypeInfo(e->u.rail.railtype))->compatible_railtypes & c->avail_railtypes) == 0) return false;
00882 }
00883
00884 return true;
00885 }
00886
00893 bool IsEngineRefittable(EngineID engine)
00894 {
00895 const Engine *e = Engine::GetIfValid(engine);
00896
00897
00898 if (e == NULL) return false;
00899
00900 if (!e->CanCarryCargo()) return false;
00901
00902 const EngineInfo *ei = &e->info;
00903 if (ei->refit_mask == 0) return false;
00904
00905
00906
00907 if (HasBit(ei->callback_mask, CBM_VEHICLE_CARGO_SUFFIX)) return true;
00908
00909
00910 CargoID default_cargo = e->GetDefaultCargoType();
00911 return default_cargo != CT_INVALID && ei->refit_mask != 1U << default_cargo;
00912 }