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