00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "company_func.h"
00014 #include "train.h"
00015 #include "command_func.h"
00016 #include "engine_func.h"
00017 #include "vehicle_func.h"
00018 #include "autoreplace_func.h"
00019 #include "autoreplace_gui.h"
00020 #include "articulated_vehicles.h"
00021 #include "core/random_func.hpp"
00022
00023 #include "table/strings.h"
00024
00025 extern void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index);
00026 extern void ChangeVehicleNews(VehicleID from_index, VehicleID to_index);
00027 extern void ChangeVehicleViewWindow(VehicleID from_index, VehicleID to_index);
00028
00036 static bool EnginesHaveCargoInCommon(EngineID engine_a, EngineID engine_b)
00037 {
00038 uint32 available_cargoes_a = GetUnionOfArticulatedRefitMasks(engine_a, true);
00039 uint32 available_cargoes_b = GetUnionOfArticulatedRefitMasks(engine_b, true);
00040 return (available_cargoes_a == 0 || available_cargoes_b == 0 || (available_cargoes_a & available_cargoes_b) != 0);
00041 }
00042
00050 bool CheckAutoreplaceValidity(EngineID from, EngineID to, CompanyID company)
00051 {
00052 assert(Engine::IsValidID(from) && Engine::IsValidID(to));
00053
00054
00055 if (from == to) return false;
00056
00057 const Engine *e_from = Engine::Get(from);
00058 const Engine *e_to = Engine::Get(to);
00059 VehicleType type = e_from->type;
00060
00061
00062 if (!IsEngineBuildable(to, type, company)) return false;
00063
00064 switch (type) {
00065 case VEH_TRAIN: {
00066
00067 if ((GetRailTypeInfo(e_from->u.rail.railtype)->compatible_railtypes & GetRailTypeInfo(e_to->u.rail.railtype)->compatible_railtypes) == 0) return false;
00068
00069
00070 if ((e_from->u.rail.railveh_type == RAILVEH_WAGON) != (e_to->u.rail.railveh_type == RAILVEH_WAGON)) return false;
00071 break;
00072 }
00073
00074 case VEH_ROAD:
00075
00076 if (HasBit(e_from->info.misc_flags, EF_ROAD_TRAM) != HasBit(e_to->info.misc_flags, EF_ROAD_TRAM)) return false;
00077 break;
00078
00079 case VEH_AIRCRAFT:
00080
00081 if ((e_from->u.air.subtype & AIR_CTOL) != (e_to->u.air.subtype & AIR_CTOL)) return false;
00082 break;
00083
00084 default: break;
00085 }
00086
00087
00088 return EnginesHaveCargoInCommon(from, to);
00089 }
00090
00095 void CheckCargoCapacity(Vehicle *v)
00096 {
00097 assert(v == NULL || v->First() == v);
00098
00099 for (Vehicle *src = v; src != NULL; src = src->Next()) {
00100
00101 if (src->cargo.Count() <= src->cargo_cap) continue;
00102
00103
00104 uint to_spread = src->cargo.Count() - src->cargo_cap;
00105 for (Vehicle *dest = v; dest != NULL && to_spread != 0; dest = dest->Next()) {
00106 if (dest->cargo.Count() >= dest->cargo_cap || dest->cargo_type != src->cargo_type) continue;
00107
00108 uint amount = min(to_spread, dest->cargo_cap - dest->cargo.Count());
00109 src->cargo.MoveTo(&dest->cargo, amount, VehicleCargoList::MTA_UNLOAD, NULL);
00110 to_spread -= amount;
00111 }
00112
00113
00114 src->cargo.Truncate(src->cargo_cap);
00115 }
00116 }
00117
00124 static void TransferCargo(Vehicle *old_veh, Vehicle *new_head, bool part_of_chain)
00125 {
00126 assert(!part_of_chain || new_head->IsPrimaryVehicle());
00127
00128 for (Vehicle *src = old_veh; src != NULL; src = src->Next()) {
00129 if (!part_of_chain && src->type == VEH_TRAIN && src != old_veh && src != Train::From(old_veh)->other_multiheaded_part && !src->IsArticulatedPart()) {
00130
00131 src = src->GetLastEnginePart();
00132 continue;
00133 }
00134 if (src->cargo_type >= NUM_CARGO || src->cargo.Count() == 0) continue;
00135
00136
00137 for (Vehicle *dest = new_head; dest != NULL && src->cargo.Count() > 0; dest = dest->Next()) {
00138 if (!part_of_chain && dest->type == VEH_TRAIN && dest != new_head && dest != Train::From(new_head)->other_multiheaded_part && !dest->IsArticulatedPart()) {
00139
00140 dest = dest->GetLastEnginePart();
00141 continue;
00142 }
00143 if (dest->cargo_type != src->cargo_type) continue;
00144
00145 uint amount = min(src->cargo.Count(), dest->cargo_cap - dest->cargo.Count());
00146 if (amount <= 0) continue;
00147
00148 src->cargo.MoveTo(&dest->cargo, amount, VehicleCargoList::MTA_UNLOAD, NULL);
00149 }
00150 }
00151
00152
00153 if (part_of_chain && new_head->type == VEH_TRAIN) Train::From(new_head)->ConsistChanged(true);
00154 }
00155
00162 static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, EngineID engine_type)
00163 {
00164
00165 uint32 union_refit_mask_a = GetUnionOfArticulatedRefitMasks(v->engine_type, false);
00166 uint32 union_refit_mask_b = GetUnionOfArticulatedRefitMasks(engine_type, false);
00167
00168 const Order *o;
00169 const Vehicle *u = (v->type == VEH_TRAIN) ? v->First() : v;
00170 FOR_VEHICLE_ORDERS(u, o) {
00171 if (!o->IsRefit() || o->IsAutoRefit()) continue;
00172 CargoID cargo_type = o->GetRefitCargo();
00173
00174 if (!HasBit(union_refit_mask_a, cargo_type)) continue;
00175 if (!HasBit(union_refit_mask_b, cargo_type)) return false;
00176 }
00177
00178 return true;
00179 }
00180
00190 static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type, bool part_of_chain)
00191 {
00192 uint32 available_cargo_types, union_mask;
00193 GetArticulatedRefitMasks(engine_type, true, &union_mask, &available_cargo_types);
00194
00195 if (union_mask == 0) return CT_NO_REFIT;
00196
00197 CargoID cargo_type;
00198 if (IsArticulatedVehicleCarryingDifferentCargoes(v, &cargo_type)) return CT_INVALID;
00199
00200 if (cargo_type == CT_INVALID) {
00201 if (v->type != VEH_TRAIN) return CT_NO_REFIT;
00202
00203 if (!part_of_chain) return CT_NO_REFIT;
00204
00205
00206
00207
00208 for (v = v->First(); v != NULL; v = v->Next()) {
00209 if (!v->GetEngine()->CanCarryCargo()) continue;
00210
00211 if (HasBit(available_cargo_types, v->cargo_type)) return v->cargo_type;
00212 }
00213
00214 return CT_NO_REFIT;
00215 } else {
00216 if (!HasBit(available_cargo_types, cargo_type)) return CT_INVALID;
00217
00218 if (part_of_chain && !VerifyAutoreplaceRefitForOrders(v, engine_type)) return CT_INVALID;
00219
00220 return cargo_type;
00221 }
00222 }
00223
00232 static CommandCost GetNewEngineType(const Vehicle *v, const Company *c, bool always_replace, EngineID &e)
00233 {
00234 assert(v->type != VEH_TRAIN || !v->IsArticulatedPart());
00235
00236 e = INVALID_ENGINE;
00237
00238 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
00239
00240 return CommandCost();
00241 }
00242
00243 bool replace_when_old;
00244 e = EngineReplacementForCompany(c, v->engine_type, v->group_id, &replace_when_old);
00245 if (!always_replace && replace_when_old && !v->NeedsAutorenewing(c, false)) e = INVALID_ENGINE;
00246
00247
00248 if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_company)) {
00249 return CommandCost();
00250 }
00251
00252
00253 if (v->NeedsAutorenewing(c)) e = v->engine_type;
00254
00255
00256 if (e == INVALID_ENGINE || IsEngineBuildable(e, v->type, _current_company)) return CommandCost();
00257
00258
00259 return CommandCost(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + v->type);
00260 }
00261
00270 static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehicle, bool part_of_chain)
00271 {
00272 *new_vehicle = NULL;
00273
00274
00275 const Company *c = Company::Get(_current_company);
00276 EngineID e;
00277 CommandCost cost = GetNewEngineType(old_veh, c, true, e);
00278 if (cost.Failed()) return cost;
00279 if (e == INVALID_ENGINE) return CommandCost();
00280
00281
00282 CargoID refit_cargo = GetNewCargoTypeForReplace(old_veh, e, part_of_chain);
00283 if (refit_cargo == CT_INVALID) return CommandCost();
00284
00285
00286 cost = DoCommand(old_veh->tile, e, 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_veh));
00287 if (cost.Failed()) return cost;
00288
00289 Vehicle *new_veh = Vehicle::Get(_new_vehicle_id);
00290 *new_vehicle = new_veh;
00291
00292
00293 if (refit_cargo != CT_NO_REFIT) {
00294 byte subtype = GetBestFittingSubType(old_veh, new_veh, refit_cargo);
00295
00296 cost.AddCost(DoCommand(0, new_veh->index, refit_cargo | (subtype << 8), DC_EXEC, GetCmdRefitVeh(new_veh)));
00297 assert(cost.Succeeded());
00298 }
00299
00300
00301 if (new_veh->type == VEH_TRAIN && HasBit(Train::From(old_veh)->flags, VRF_REVERSE_DIRECTION)) {
00302 DoCommand(0, new_veh->index, true, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
00303 }
00304
00305 return cost;
00306 }
00307
00314 static inline CommandCost CmdStartStopVehicle(const Vehicle *v, bool evaluate_callback)
00315 {
00316 return DoCommand(0, v->index, evaluate_callback ? 1 : 0, DC_EXEC | DC_AUTOREPLACE, CMD_START_STOP_VEHICLE);
00317 }
00318
00327 static inline CommandCost CmdMoveVehicle(const Vehicle *v, const Vehicle *after, DoCommandFlag flags, bool whole_chain)
00328 {
00329 return DoCommand(0, v->index | (whole_chain ? 1 : 0) << 20, after != NULL ? after->index : INVALID_VEHICLE, flags | DC_NO_CARGO_CAP_CHECK, CMD_MOVE_RAIL_VEHICLE);
00330 }
00331
00338 static CommandCost CopyHeadSpecificThings(Vehicle *old_head, Vehicle *new_head, DoCommandFlag flags)
00339 {
00340 CommandCost cost = CommandCost();
00341
00342
00343 if (cost.Succeeded() && old_head != new_head) cost.AddCost(DoCommand(0, new_head->index | CO_SHARE << 30, old_head->index, DC_EXEC, CMD_CLONE_ORDER));
00344
00345
00346 if (cost.Succeeded() && old_head != new_head) cost.AddCost(DoCommand(0, old_head->group_id, new_head->index, DC_EXEC, CMD_ADD_VEHICLE_GROUP));
00347
00348
00349 if (cost.Succeeded()) {
00350
00351 assert((new_head->vehstatus & VS_STOPPED) != 0);
00352 cost.AddCost(CmdStartStopVehicle(new_head, true));
00353
00354
00355 if (cost.Succeeded()) cost.AddCost(CmdStartStopVehicle(new_head, false));
00356 }
00357
00358
00359 if (cost.Succeeded() && old_head != new_head && (flags & DC_EXEC) != 0) {
00360
00361 new_head->CopyVehicleConfigAndStatistics(old_head);
00362
00363
00364 ChangeVehicleViewports(old_head->index, new_head->index);
00365 ChangeVehicleViewWindow(old_head->index, new_head->index);
00366 ChangeVehicleNews(old_head->index, new_head->index);
00367 }
00368
00369 return cost;
00370 }
00371
00379 static CommandCost ReplaceFreeUnit(Vehicle **single_unit, DoCommandFlag flags, bool *nothing_to_do)
00380 {
00381 Train *old_v = Train::From(*single_unit);
00382 assert(!old_v->IsArticulatedPart() && !old_v->IsRearDualheaded());
00383
00384 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00385
00386
00387 Vehicle *new_v = NULL;
00388 cost.AddCost(BuildReplacementVehicle(old_v, &new_v, false));
00389
00390
00391 if (cost.Succeeded() && new_v != NULL) {
00392 *nothing_to_do = false;
00393
00394 if ((flags & DC_EXEC) != 0) {
00395
00396 CmdMoveVehicle(new_v, old_v, DC_EXEC, false);
00397
00398
00399
00400
00401
00402
00403
00404 TransferCargo(old_v, new_v, false);
00405
00406 *single_unit = new_v;
00407 }
00408
00409
00410 cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v)));
00411
00412
00413 if ((flags & DC_EXEC) == 0) {
00414 DoCommand(0, new_v->index, 0, DC_EXEC, GetCmdSellVeh(new_v));
00415 }
00416 }
00417
00418 return cost;
00419 }
00420
00429 static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon_removal, bool *nothing_to_do)
00430 {
00431 Vehicle *old_head = *chain;
00432 assert(old_head->IsPrimaryVehicle());
00433
00434 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00435
00436 if (old_head->type == VEH_TRAIN) {
00437
00438 uint16 old_total_length = CeilDiv(Train::From(old_head)->gcache.cached_total_length, TILE_SIZE) * TILE_SIZE;
00439
00440 int num_units = 0;
00441 for (Train *w = Train::From(old_head); w != NULL; w = w->GetNextUnit()) num_units++;
00442
00443 Train **old_vehs = CallocT<Train *>(num_units);
00444 Train **new_vehs = CallocT<Train *>(num_units);
00445 Money *new_costs = MallocT<Money>(num_units);
00446
00447
00448
00449 int i;
00450 Train *w;
00451 for (w = Train::From(old_head), i = 0; w != NULL; w = w->GetNextUnit(), i++) {
00452 assert(i < num_units);
00453 old_vehs[i] = w;
00454
00455 CommandCost ret = BuildReplacementVehicle(old_vehs[i], (Vehicle**)&new_vehs[i], true);
00456 cost.AddCost(ret);
00457 if (cost.Failed()) break;
00458
00459 new_costs[i] = ret.GetCost();
00460 if (new_vehs[i] != NULL) *nothing_to_do = false;
00461 }
00462 Train *new_head = (new_vehs[0] != NULL ? new_vehs[0] : old_vehs[0]);
00463
00464
00465 if (cost.Succeeded()) {
00466
00467 Train *second = Train::From(old_head)->GetNextUnit();
00468 if (second != NULL) cost.AddCost(CmdMoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true));
00469
00470 assert(Train::From(new_head)->GetNextUnit() == NULL);
00471
00472
00473
00474
00475
00476 Train *last_engine = NULL;
00477 if (cost.Succeeded()) {
00478 for (int i = num_units - 1; i > 0; i--) {
00479 Train *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
00480
00481 if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) continue;
00482
00483 if (new_vehs[i] != NULL) {
00484
00485
00486
00487 CmdMoveVehicle(old_vehs[i], NULL, DC_EXEC | DC_AUTOREPLACE, false);
00488 }
00489
00490 if (last_engine == NULL) last_engine = append;
00491 cost.AddCost(CmdMoveVehicle(append, new_head, DC_EXEC, false));
00492 if (cost.Failed()) break;
00493 }
00494 if (last_engine == NULL) last_engine = new_head;
00495 }
00496
00497
00498 if (cost.Succeeded() && wagon_removal && new_head->gcache.cached_total_length > old_total_length) cost = CommandCost(STR_ERROR_TRAIN_TOO_LONG_AFTER_REPLACEMENT);
00499
00500
00501
00502
00503 if (cost.Succeeded()) {
00504 for (int i = num_units - 1; i > 0; i--) {
00505 assert(last_engine != NULL);
00506 Vehicle *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
00507
00508 if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) {
00509
00510 CommandCost res = CmdMoveVehicle(append, last_engine, DC_EXEC, false);
00511
00512
00513
00514
00515 if (wagon_removal && (res.Failed() ? res.GetErrorMessage() == STR_ERROR_TRAIN_TOO_LONG : new_head->gcache.cached_total_length > old_total_length)) {
00516 CmdMoveVehicle(append, NULL, DC_EXEC | DC_AUTOREPLACE, false);
00517 break;
00518 }
00519
00520 cost.AddCost(res);
00521 if (cost.Failed()) break;
00522 } else {
00523
00524 assert(append == last_engine);
00525 last_engine = last_engine->GetPrevUnit();
00526 }
00527 }
00528 }
00529
00530
00531 if (cost.Succeeded() && wagon_removal) {
00532 assert(new_head->gcache.cached_total_length <= _settings_game.vehicle.max_train_length * TILE_SIZE);
00533 for (int i = 1; i < num_units; i++) {
00534 Vehicle *wagon = new_vehs[i];
00535 if (wagon == NULL) continue;
00536 if (wagon->First() == new_head) break;
00537
00538 assert(RailVehInfo(wagon->engine_type)->railveh_type == RAILVEH_WAGON);
00539
00540
00541 CommandCost ret = DoCommand(0, wagon->index, 0, DC_EXEC, GetCmdSellVeh(wagon));
00542 assert(ret.Succeeded());
00543 new_vehs[i] = NULL;
00544
00545
00546
00547 cost.AddCost(-new_costs[i]);
00548 }
00549 }
00550
00551
00552 if (cost.Succeeded()) cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags));
00553
00554 if (cost.Succeeded()) {
00555
00556 if ((flags & DC_EXEC) != 0 && new_head != old_head) {
00557 *chain = new_head;
00558 }
00559
00560
00561 for (int i = 0; i < num_units; i++) {
00562 Vehicle *w = old_vehs[i];
00563
00564
00565 if (w->First() == new_head) continue;
00566
00567 if ((flags & DC_EXEC) != 0) TransferCargo(w, new_head, true);
00568
00569
00570
00571
00572 cost.AddCost(DoCommand(0, w->index, 0, flags | DC_AUTOREPLACE, GetCmdSellVeh(w)));
00573 if ((flags & DC_EXEC) != 0) {
00574 old_vehs[i] = NULL;
00575 if (i == 0) old_head = NULL;
00576 }
00577 }
00578
00579 if ((flags & DC_EXEC) != 0) CheckCargoCapacity(new_head);
00580 }
00581
00582
00583
00584
00585 if ((flags & DC_EXEC) == 0) {
00586
00587 Train *second = Train::From(old_head)->GetNextUnit();
00588 if (second != NULL) CmdMoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true);
00589
00590 assert(Train::From(old_head)->GetNextUnit() == NULL);
00591
00592 for (int i = num_units - 1; i > 0; i--) {
00593 CommandCost ret = CmdMoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false);
00594 assert(ret.Succeeded());
00595 }
00596 }
00597 }
00598
00599
00600 if ((flags & DC_EXEC) == 0) {
00601 for (int i = num_units - 1; i >= 0; i--) {
00602 if (new_vehs[i] != NULL) {
00603 DoCommand(0, new_vehs[i]->index, 0, DC_EXEC, GetCmdSellVeh(new_vehs[i]));
00604 new_vehs[i] = NULL;
00605 }
00606 }
00607 }
00608
00609 free(old_vehs);
00610 free(new_vehs);
00611 free(new_costs);
00612 } else {
00613
00614 Vehicle *new_head = NULL;
00615 cost.AddCost(BuildReplacementVehicle(old_head, &new_head, true));
00616
00617
00618 if (cost.Succeeded() && new_head != NULL) {
00619 *nothing_to_do = false;
00620
00621
00622 cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags));
00623
00624 if (cost.Succeeded()) {
00625
00626 if ((flags & DC_EXEC) != 0) {
00627 TransferCargo(old_head, new_head, true);
00628 *chain = new_head;
00629 }
00630
00631
00632 cost.AddCost(DoCommand(0, old_head->index, 0, flags, GetCmdSellVeh(old_head)));
00633 }
00634
00635
00636 if ((flags & DC_EXEC) == 0) {
00637 DoCommand(0, new_head->index, 0, DC_EXEC, GetCmdSellVeh(new_head));
00638 }
00639 }
00640 }
00641
00642 return cost;
00643 }
00644
00655 CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00656 {
00657 Vehicle *v = Vehicle::GetIfValid(p1);
00658 if (v == NULL) return CMD_ERROR;
00659
00660 CommandCost ret = CheckOwnership(v->owner);
00661 if (ret.Failed()) return ret;
00662
00663 if (!v->IsChainInDepot()) return CMD_ERROR;
00664 if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
00665
00666 bool free_wagon = false;
00667 if (v->type == VEH_TRAIN) {
00668 Train *t = Train::From(v);
00669 if (t->IsArticulatedPart() || t->IsRearDualheaded()) return CMD_ERROR;
00670 free_wagon = !t->IsFrontEngine();
00671 if (free_wagon && t->First()->IsFrontEngine()) return CMD_ERROR;
00672 } else {
00673 if (!v->IsPrimaryVehicle()) return CMD_ERROR;
00674 }
00675
00676 const Company *c = Company::Get(_current_company);
00677 bool wagon_removal = c->settings.renew_keep_length;
00678
00679
00680 Vehicle *w = v;
00681 bool any_replacements = false;
00682 while (w != NULL) {
00683 EngineID e;
00684 CommandCost cost = GetNewEngineType(w, c, false, e);
00685 if (cost.Failed()) return cost;
00686 any_replacements |= (e != INVALID_ENGINE);
00687 w = (!free_wagon && w->type == VEH_TRAIN ? Train::From(w)->GetNextUnit() : NULL);
00688 }
00689
00690 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00691 bool nothing_to_do = true;
00692
00693 if (any_replacements) {
00694 bool was_stopped = free_wagon || ((v->vehstatus & VS_STOPPED) != 0);
00695
00696
00697 if (!was_stopped) cost.AddCost(CmdStartStopVehicle(v, true));
00698 if (cost.Failed()) return cost;
00699
00700 assert(free_wagon || v->IsStoppedInDepot());
00701
00702
00703
00704
00705 SavedRandomSeeds saved_seeds;
00706 SaveRandomSeeds(&saved_seeds);
00707 if (free_wagon) {
00708 cost.AddCost(ReplaceFreeUnit(&v, flags & ~DC_EXEC, ¬hing_to_do));
00709 } else {
00710 cost.AddCost(ReplaceChain(&v, flags & ~DC_EXEC, wagon_removal, ¬hing_to_do));
00711 }
00712 RestoreRandomSeeds(saved_seeds);
00713
00714 if (cost.Succeeded() && (flags & DC_EXEC) != 0) {
00715 CommandCost ret;
00716 if (free_wagon) {
00717 ret = ReplaceFreeUnit(&v, flags, ¬hing_to_do);
00718 } else {
00719 ret = ReplaceChain(&v, flags, wagon_removal, ¬hing_to_do);
00720 }
00721 assert(ret.Succeeded() && ret.GetCost() == cost.GetCost());
00722 }
00723
00724
00725 if (!was_stopped) cost.AddCost(CmdStartStopVehicle(v, false));
00726 }
00727
00728 if (cost.Succeeded() && nothing_to_do) cost = CommandCost(STR_ERROR_AUTOREPLACE_NOTHING_TO_DO);
00729 return cost;
00730 }
00731
00745 CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00746 {
00747 Company *c = Company::GetIfValid(_current_company);
00748 if (c == NULL) return CMD_ERROR;
00749
00750 EngineID old_engine_type = GB(p2, 0, 16);
00751 EngineID new_engine_type = GB(p2, 16, 16);
00752 GroupID id_g = GB(p1, 16, 16);
00753 CommandCost cost;
00754
00755 if (Group::IsValidID(id_g) ? Group::Get(id_g)->owner != _current_company : !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
00756 if (!Engine::IsValidID(old_engine_type)) return CMD_ERROR;
00757
00758 if (new_engine_type != INVALID_ENGINE) {
00759 if (!Engine::IsValidID(new_engine_type)) return CMD_ERROR;
00760 if (!CheckAutoreplaceValidity(old_engine_type, new_engine_type, _current_company)) return CMD_ERROR;
00761
00762 cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, HasBit(p1, 0), flags);
00763 } else {
00764 cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags);
00765 }
00766
00767 if (flags & DC_EXEC) {
00768 GroupStatistics::UpdateAutoreplace(_current_company);
00769 if (IsLocalCompany()) SetWindowDirty(WC_REPLACE_VEHICLE, Engine::Get(old_engine_type)->type);
00770 }
00771 if ((flags & DC_EXEC) && IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
00772
00773 return cost;
00774 }
00775