00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "company_func.h"
00014 #include "train.h"
00015 #include "rail.h"
00016 #include "command_func.h"
00017 #include "engine_base.h"
00018 #include "engine_func.h"
00019 #include "vehicle_func.h"
00020 #include "functions.h"
00021 #include "autoreplace_func.h"
00022 #include "articulated_vehicles.h"
00023 #include "core/random_func.hpp"
00024
00025 #include "table/strings.h"
00026
00027 extern void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index);
00028 extern void ChangeVehicleNews(VehicleID from_index, VehicleID to_index);
00029 extern void ChangeVehicleViewWindow(VehicleID from_index, VehicleID to_index);
00030
00037 static bool EnginesHaveCargoInCommon(EngineID engine_a, EngineID engine_b)
00038 {
00039 uint32 available_cargos_a = GetUnionOfArticulatedRefitMasks(engine_a, true);
00040 uint32 available_cargos_b = GetUnionOfArticulatedRefitMasks(engine_b, true);
00041 return (available_cargos_a == 0 || available_cargos_b == 0 || (available_cargos_a & available_cargos_b) != 0);
00042 }
00043
00051 bool CheckAutoreplaceValidity(EngineID from, EngineID to, CompanyID company)
00052 {
00053
00054
00055 if (!Engine::IsValidID(from) || !Engine::IsValidID(to)) return false;
00056
00057
00058 if (from == to) return false;
00059
00060 const Engine *e_from = Engine::Get(from);
00061 const Engine *e_to = Engine::Get(to);
00062 VehicleType type = e_from->type;
00063
00064
00065 if (!IsEngineBuildable(to, type, company)) return false;
00066
00067 switch (type) {
00068 case VEH_TRAIN: {
00069
00070 if ((GetRailTypeInfo(e_from->u.rail.railtype)->compatible_railtypes & GetRailTypeInfo(e_to->u.rail.railtype)->compatible_railtypes) == 0) return false;
00071
00072
00073 if ((e_from->u.rail.railveh_type == RAILVEH_WAGON) != (e_to->u.rail.railveh_type == RAILVEH_WAGON)) return false;
00074 break;
00075 }
00076
00077 case VEH_ROAD:
00078
00079 if (HasBit(e_from->info.misc_flags, EF_ROAD_TRAM) != HasBit(e_to->info.misc_flags, EF_ROAD_TRAM)) return false;
00080 break;
00081
00082 case VEH_AIRCRAFT:
00083
00084 if ((e_from->u.air.subtype & AIR_CTOL) != (e_to->u.air.subtype & AIR_CTOL)) return false;
00085 break;
00086
00087 default: break;
00088 }
00089
00090
00091 return EnginesHaveCargoInCommon(from, to);
00092 }
00093
00099 static void TransferCargo(Vehicle *old_veh, Vehicle *new_head, bool part_of_chain)
00100 {
00101 assert(!part_of_chain || new_head->IsPrimaryVehicle());
00102
00103 for (Vehicle *src = old_veh; src != NULL; src = src->Next()) {
00104 if (!part_of_chain && src->type == VEH_TRAIN && src != old_veh && src != Train::From(old_veh)->other_multiheaded_part && !Train::From(src)->IsArticulatedPart()) {
00105
00106 src = Train::From(src)->GetLastEnginePart();
00107 continue;
00108 }
00109 if (src->cargo_type >= NUM_CARGO || src->cargo.Count() == 0) continue;
00110
00111
00112 for (Vehicle *dest = new_head; dest != NULL && src->cargo.Count() > 0; dest = dest->Next()) {
00113 if (!part_of_chain && dest->type == VEH_TRAIN && dest != new_head && dest != Train::From(new_head)->other_multiheaded_part && !Train::From(dest)->IsArticulatedPart()) {
00114
00115 dest = Train::From(dest)->GetLastEnginePart();
00116 continue;
00117 }
00118 if (dest->cargo_type != src->cargo_type) continue;
00119
00120 uint amount = min(src->cargo.Count(), dest->cargo_cap - dest->cargo.Count());
00121 if (amount <= 0) continue;
00122
00123 src->cargo.MoveTo(&dest->cargo, amount, VehicleCargoList::MTA_UNLOAD, NULL);
00124 }
00125 }
00126
00127
00128 if (part_of_chain && new_head->type == VEH_TRAIN) Train::From(new_head)->ConsistChanged(true);
00129 }
00130
00137 static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, EngineID engine_type)
00138 {
00139 const Order *o;
00140 const Vehicle *u;
00141
00142 uint32 union_refit_mask_a = GetUnionOfArticulatedRefitMasks(v->engine_type, false);
00143 uint32 union_refit_mask_b = GetUnionOfArticulatedRefitMasks(engine_type, false);
00144
00145 if (v->type == VEH_TRAIN) {
00146 u = v->First();
00147 } else {
00148 u = v;
00149 }
00150
00151 FOR_VEHICLE_ORDERS(u, o) {
00152 if (!o->IsRefit()) continue;
00153 CargoID cargo_type = o->GetRefitCargo();
00154
00155 if (!HasBit(union_refit_mask_a, cargo_type)) continue;
00156 if (!HasBit(union_refit_mask_b, cargo_type)) return false;
00157 }
00158
00159 return true;
00160 }
00161
00171 static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type, bool part_of_chain)
00172 {
00173 CargoID cargo_type;
00174 uint32 available_cargo_types, union_mask;
00175 GetArticulatedRefitMasks(engine_type, true, &union_mask, &available_cargo_types);
00176
00177 if (union_mask == 0) return CT_NO_REFIT;
00178
00179 if (IsArticulatedVehicleCarryingDifferentCargos(v, &cargo_type)) return CT_INVALID;
00180
00181 if (cargo_type == CT_INVALID) {
00182 if (v->type != VEH_TRAIN) return CT_NO_REFIT;
00183
00184 if (!part_of_chain) return CT_NO_REFIT;
00185
00186
00187
00188
00189 for (v = v->First(); v != NULL; v = v->Next()) {
00190 if (v->cargo_cap == 0) continue;
00191
00192 if (HasBit(available_cargo_types, v->cargo_type)) {
00193
00194 CargoArray default_capacity = GetCapacityOfArticulatedParts(engine_type);
00195 for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
00196 if (cid != v->cargo_type && default_capacity[cid] > 0) return v->cargo_type;
00197 }
00198
00199 return CT_NO_REFIT;
00200 }
00201 }
00202
00203 return CT_NO_REFIT;
00204 } else {
00205 if (!HasBit(available_cargo_types, cargo_type)) return CT_INVALID;
00206
00207 if (part_of_chain && !VerifyAutoreplaceRefitForOrders(v, engine_type)) return CT_INVALID;
00208
00209
00210 CargoArray default_capacity = GetCapacityOfArticulatedParts(engine_type);
00211 for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
00212 if (cid != cargo_type && default_capacity[cid] > 0) return cargo_type;
00213 }
00214
00215 return CT_NO_REFIT;
00216 }
00217 }
00218
00224 static EngineID GetNewEngineType(const Vehicle *v, const Company *c)
00225 {
00226 assert(v->type != VEH_TRAIN || !Train::From(v)->IsArticulatedPart());
00227
00228 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
00229
00230 return INVALID_ENGINE;
00231 }
00232
00233 EngineID e = EngineReplacementForCompany(c, v->engine_type, v->group_id);
00234
00235 if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_company)) {
00236 return e;
00237 }
00238
00239 if (v->NeedsAutorenewing(c) &&
00240 IsEngineBuildable(v->engine_type, v->type, _current_company)) {
00241 return v->engine_type;
00242 }
00243
00244 return INVALID_ENGINE;
00245 }
00246
00254 static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehicle, bool part_of_chain)
00255 {
00256 *new_vehicle = NULL;
00257
00258
00259 const Company *c = Company::Get(_current_company);
00260 EngineID e = GetNewEngineType(old_veh, c);
00261 if (e == INVALID_ENGINE) return CommandCost();
00262
00263
00264 CargoID refit_cargo = GetNewCargoTypeForReplace(old_veh, e, part_of_chain);
00265 if (refit_cargo == CT_INVALID) return CommandCost();
00266
00267
00268 CommandCost cost = DoCommand(old_veh->tile, e, 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_veh));
00269 if (cost.Failed()) return cost;
00270
00271 Vehicle *new_veh = Vehicle::Get(_new_vehicle_id);
00272 *new_vehicle = new_veh;
00273
00274
00275 byte subtype = GetBestFittingSubType(old_veh, new_veh);
00276
00277
00278 if (subtype != 0 && refit_cargo == CT_NO_REFIT) refit_cargo = old_veh->cargo_type;
00279
00280 if (refit_cargo != CT_NO_REFIT) {
00281 cost.AddCost(DoCommand(0, new_veh->index, refit_cargo | (subtype << 8), DC_EXEC, GetCmdRefitVeh(new_veh)));
00282 assert(cost.Succeeded());
00283 }
00284
00285
00286 if (new_veh->type == VEH_TRAIN && HasBit(Train::From(old_veh)->flags, VRF_REVERSE_DIRECTION)) {
00287 DoCommand(0, new_veh->index, true, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
00288 }
00289
00290 return cost;
00291 }
00292
00298 static inline CommandCost StartStopVehicle(const Vehicle *v, bool evaluate_callback)
00299 {
00300 return DoCommand(0, v->index, evaluate_callback ? 1 : 0, DC_EXEC | DC_AUTOREPLACE, CMD_START_STOP_VEHICLE);
00301 }
00302
00310 static inline CommandCost MoveVehicle(const Vehicle *v, const Vehicle *after, DoCommandFlag flags, bool whole_chain)
00311 {
00312 return DoCommand(0, v->index | (after != NULL ? after->index : INVALID_VEHICLE) << 16, whole_chain ? 1 : 0, flags, CMD_MOVE_RAIL_VEHICLE);
00313 }
00314
00320 static CommandCost CopyHeadSpecificThings(Vehicle *old_head, Vehicle *new_head, DoCommandFlag flags)
00321 {
00322 CommandCost cost = CommandCost();
00323
00324
00325 if (cost.Succeeded() && old_head != new_head) cost.AddCost(DoCommand(0, (old_head->index << 16) | new_head->index, CO_SHARE, DC_EXEC, CMD_CLONE_ORDER));
00326
00327
00328 if (cost.Succeeded() && old_head != new_head) cost.AddCost(DoCommand(0, old_head->group_id, new_head->index, DC_EXEC, CMD_ADD_VEHICLE_GROUP));
00329
00330
00331 if (cost.Succeeded()) {
00332
00333 assert((new_head->vehstatus & VS_STOPPED) != 0);
00334 cost.AddCost(StartStopVehicle(new_head, true));
00335
00336
00337 if (cost.Succeeded()) cost.AddCost(StartStopVehicle(new_head, false));
00338 }
00339
00340
00341 if (cost.Succeeded() && old_head != new_head && (flags & DC_EXEC) != 0) {
00342
00343 if (old_head->name != NULL) {
00344 DoCommand(0, new_head->index, 0, DC_EXEC | DC_AUTOREPLACE, CMD_RENAME_VEHICLE, old_head->name);
00345 }
00346
00347
00348 new_head->CopyVehicleConfigAndStatistics(old_head);
00349
00350
00351 ChangeVehicleViewports(old_head->index, new_head->index);
00352 ChangeVehicleViewWindow(old_head->index, new_head->index);
00353 ChangeVehicleNews(old_head->index, new_head->index);
00354 }
00355
00356 return cost;
00357 }
00358
00365 static CommandCost ReplaceFreeUnit(Vehicle **single_unit, DoCommandFlag flags, bool *nothing_to_do)
00366 {
00367 Train *old_v = Train::From(*single_unit);
00368 assert(!old_v->IsArticulatedPart() && !old_v->IsRearDualheaded());
00369
00370 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00371
00372
00373 Vehicle *new_v = NULL;
00374 cost.AddCost(BuildReplacementVehicle(old_v, &new_v, false));
00375
00376
00377 if (cost.Succeeded() && new_v != NULL) {
00378 *nothing_to_do = false;
00379
00380 if ((flags & DC_EXEC) != 0) {
00381
00382 MoveVehicle(new_v, old_v, DC_EXEC, false);
00383
00384
00385
00386
00387
00388
00389
00390 TransferCargo(old_v, new_v, false);
00391
00392 *single_unit = new_v;
00393 }
00394
00395
00396 cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v)));
00397
00398
00399 if ((flags & DC_EXEC) == 0) {
00400 DoCommand(0, new_v->index, 0, DC_EXEC, GetCmdSellVeh(new_v));
00401 }
00402 }
00403
00404 return cost;
00405 }
00406
00414 static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon_removal, bool *nothing_to_do)
00415 {
00416 Vehicle *old_head = *chain;
00417 assert(old_head->IsPrimaryVehicle());
00418
00419 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00420
00421 if (old_head->type == VEH_TRAIN) {
00422
00423 uint16 old_total_length = (Train::From(old_head)->tcache.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE;
00424
00425 int num_units = 0;
00426 for (Train *w = Train::From(old_head); w != NULL; w = w->GetNextUnit()) num_units++;
00427
00428 Train **old_vehs = CallocT<Train *>(num_units);
00429 Train **new_vehs = CallocT<Train *>(num_units);
00430 Money *new_costs = MallocT<Money>(num_units);
00431
00432
00433
00434 int i;
00435 Train *w;
00436 for (w = Train::From(old_head), i = 0; w != NULL; w = w->GetNextUnit(), i++) {
00437 assert(i < num_units);
00438 old_vehs[i] = w;
00439
00440 CommandCost ret = BuildReplacementVehicle(old_vehs[i], (Vehicle**)&new_vehs[i], true);
00441 cost.AddCost(ret);
00442 if (cost.Failed()) break;
00443
00444 new_costs[i] = ret.GetCost();
00445 if (new_vehs[i] != NULL) *nothing_to_do = false;
00446 }
00447 Train *new_head = (new_vehs[0] != NULL ? new_vehs[0] : old_vehs[0]);
00448
00449
00450 if (cost.Succeeded()) {
00451
00452 Train *second = Train::From(old_head)->GetNextUnit();
00453 if (second != NULL) cost.AddCost(MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true));
00454
00455 assert(Train::From(new_head)->GetNextUnit() == NULL);
00456
00457
00458
00459
00460 Train *last_engine = NULL;
00461 if (cost.Succeeded()) {
00462 for (int i = num_units - 1; i > 0; i--) {
00463 Train *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
00464
00465 if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) continue;
00466
00467 if (last_engine == NULL) last_engine = append;
00468 cost.AddCost(MoveVehicle(append, new_head, DC_EXEC, false));
00469 if (cost.Failed()) break;
00470 }
00471 if (last_engine == NULL) last_engine = new_head;
00472 }
00473
00474
00475 if (cost.Succeeded() && wagon_removal && new_head->tcache.cached_total_length > old_total_length) cost = CommandCost(STR_ERROR_TRAIN_TOO_LONG_AFTER_REPLACEMENT);
00476
00477
00478
00479
00480 if (cost.Succeeded()) {
00481 for (int i = num_units - 1; i > 0; i--) {
00482 assert(last_engine != NULL);
00483 Vehicle *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
00484
00485 if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) {
00486
00487 CommandCost res = MoveVehicle(append, last_engine, DC_EXEC, false);
00488
00489 if (res.Succeeded() && wagon_removal && new_head->tcache.cached_total_length > old_total_length) {
00490 MoveVehicle(append, NULL, DC_EXEC | DC_AUTOREPLACE, false);
00491 break;
00492 }
00493
00494 cost.AddCost(res);
00495 if (cost.Failed()) break;
00496 } else {
00497
00498 assert(append == last_engine);
00499 last_engine = last_engine->GetPrevUnit();
00500 }
00501 }
00502 }
00503
00504
00505 if (cost.Succeeded() && wagon_removal) {
00506 for (int i = 1; i < num_units; i++) {
00507 Vehicle *wagon = new_vehs[i];
00508 if (wagon == NULL) continue;
00509 if (wagon->First() == new_head) break;
00510
00511 assert(RailVehInfo(wagon->engine_type)->railveh_type == RAILVEH_WAGON);
00512
00513
00514 CommandCost ret = DoCommand(0, wagon->index, 0, DC_EXEC, GetCmdSellVeh(wagon));
00515 assert(ret.Succeeded());
00516 new_vehs[i] = NULL;
00517
00518
00519
00520 cost.AddCost(-new_costs[i]);
00521 }
00522 }
00523
00524
00525 if (cost.Succeeded()) cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags));
00526
00527 if (cost.Succeeded()) {
00528
00529 if ((flags & DC_EXEC) != 0 && new_head != old_head) {
00530 *chain = new_head;
00531 }
00532
00533
00534 for (int i = 0; i < num_units; i++) {
00535 Vehicle *w = old_vehs[i];
00536
00537
00538 if (w->First() == new_head) continue;
00539
00540 if ((flags & DC_EXEC) != 0) TransferCargo(w, new_head, true);
00541
00542
00543
00544
00545 cost.AddCost(DoCommand(0, w->index, 0, flags | DC_AUTOREPLACE, GetCmdSellVeh(w)));
00546 if ((flags & DC_EXEC) != 0) {
00547 old_vehs[i] = NULL;
00548 if (i == 0) old_head = NULL;
00549 }
00550 }
00551 }
00552
00553
00554
00555
00556 if ((flags & DC_EXEC) == 0) {
00557
00558 Train *second = Train::From(old_head)->GetNextUnit();
00559 if (second != NULL) MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true);
00560
00561 assert(Train::From(old_head)->GetNextUnit() == NULL);
00562
00563 for (int i = num_units - 1; i > 0; i--) {
00564 CommandCost ret = MoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false);
00565 assert(ret.Succeeded());
00566 }
00567 }
00568 }
00569
00570
00571 if ((flags & DC_EXEC) == 0) {
00572 for (int i = num_units - 1; i >= 0; i--) {
00573 if (new_vehs[i] != NULL) {
00574 DoCommand(0, new_vehs[i]->index, 0, DC_EXEC, GetCmdSellVeh(new_vehs[i]));
00575 new_vehs[i] = NULL;
00576 }
00577 }
00578 }
00579
00580 free(old_vehs);
00581 free(new_vehs);
00582 free(new_costs);
00583 } else {
00584
00585 Vehicle *new_head = NULL;
00586 cost.AddCost(BuildReplacementVehicle(old_head, &new_head, true));
00587
00588
00589 if (cost.Succeeded() && new_head != NULL) {
00590 *nothing_to_do = false;
00591
00592
00593 cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags));
00594
00595 if (cost.Succeeded()) {
00596
00597 if ((flags & DC_EXEC) != 0) {
00598 TransferCargo(old_head, new_head, true);
00599 *chain = new_head;
00600 }
00601
00602
00603 cost.AddCost(DoCommand(0, old_head->index, 0, flags, GetCmdSellVeh(old_head)));
00604 }
00605
00606
00607 if ((flags & DC_EXEC) == 0) {
00608 DoCommand(0, new_head->index, 0, DC_EXEC, GetCmdSellVeh(new_head));
00609 }
00610 }
00611 }
00612
00613 return cost;
00614 }
00615
00625 CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00626 {
00627 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00628 bool nothing_to_do = true;
00629
00630 Vehicle *v = Vehicle::GetIfValid(p1);
00631 if (v == NULL) return CMD_ERROR;
00632
00633 if (!CheckOwnership(v->owner)) return CMD_ERROR;
00634 if (!v->IsInDepot()) return CMD_ERROR;
00635 if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
00636
00637 bool free_wagon = false;
00638 if (v->type == VEH_TRAIN) {
00639 Train *t = Train::From(v);
00640 if (t->IsArticulatedPart() || t->IsRearDualheaded()) return CMD_ERROR;
00641 free_wagon = !t->IsFrontEngine();
00642 if (free_wagon && t->First()->IsFrontEngine()) return CMD_ERROR;
00643 } else {
00644 if (!v->IsPrimaryVehicle()) return CMD_ERROR;
00645 }
00646
00647 const Company *c = Company::Get(_current_company);
00648 bool wagon_removal = c->settings.renew_keep_length;
00649
00650
00651 Vehicle *w = v;
00652 bool any_replacements = false;
00653 while (w != NULL && !any_replacements) {
00654 any_replacements = (GetNewEngineType(w, c) != INVALID_ENGINE);
00655 w = (!free_wagon && w->type == VEH_TRAIN ? Train::From(w)->GetNextUnit() : NULL);
00656 }
00657
00658 if (any_replacements) {
00659 bool was_stopped = free_wagon || ((v->vehstatus & VS_STOPPED) != 0);
00660
00661
00662 if (!was_stopped) cost.AddCost(StartStopVehicle(v, true));
00663 if (cost.Failed()) return cost;
00664
00665 assert(v->IsStoppedInDepot());
00666
00667
00668
00669
00670 SavedRandomSeeds saved_seeds;
00671 SaveRandomSeeds(&saved_seeds);
00672 if (free_wagon) {
00673 cost.AddCost(ReplaceFreeUnit(&v, flags & ~DC_EXEC, ¬hing_to_do));
00674 } else {
00675 cost.AddCost(ReplaceChain(&v, flags & ~DC_EXEC, wagon_removal, ¬hing_to_do));
00676 }
00677 RestoreRandomSeeds(saved_seeds);
00678
00679 if (cost.Succeeded() && (flags & DC_EXEC) != 0) {
00680 CommandCost ret;
00681 if (free_wagon) {
00682 ret = ReplaceFreeUnit(&v, flags, ¬hing_to_do);
00683 } else {
00684 ret = ReplaceChain(&v, flags, wagon_removal, ¬hing_to_do);
00685 }
00686 assert(ret.Succeeded() && ret.GetCost() == cost.GetCost());
00687 }
00688
00689
00690 if (!was_stopped) cost.AddCost(StartStopVehicle(v, false));
00691 }
00692
00693 if (cost.Succeeded() && nothing_to_do) cost = CommandCost(STR_ERROR_AUTOREPLACE_NOTHING_TO_DO);
00694 return cost;
00695 }