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 "group.h"
00021 #include "articulated_vehicles.h"
00022 #include "core/random_func.hpp"
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
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 assert(Engine::IsValidID(from) && Engine::IsValidID(to));
00054
00055
00056 if (from == to) return false;
00057
00058 const Engine *e_from = Engine::Get(from);
00059 const Engine *e_to = Engine::Get(to);
00060 VehicleType type = e_from->type;
00061
00062
00063 if (!IsEngineBuildable(to, type, company)) return false;
00064
00065 switch (type) {
00066 case VEH_TRAIN: {
00067
00068 if ((GetRailTypeInfo(e_from->u.rail.railtype)->compatible_railtypes & GetRailTypeInfo(e_to->u.rail.railtype)->compatible_railtypes) == 0) return false;
00069
00070
00071 if ((e_from->u.rail.railveh_type == RAILVEH_WAGON) != (e_to->u.rail.railveh_type == RAILVEH_WAGON)) return false;
00072 break;
00073 }
00074
00075 case VEH_ROAD:
00076
00077 if (HasBit(e_from->info.misc_flags, EF_ROAD_TRAM) != HasBit(e_to->info.misc_flags, EF_ROAD_TRAM)) return false;
00078 break;
00079
00080 case VEH_AIRCRAFT:
00081
00082 if ((e_from->u.air.subtype & AIR_CTOL) != (e_to->u.air.subtype & AIR_CTOL)) return false;
00083 break;
00084
00085 default: break;
00086 }
00087
00088
00089 return EnginesHaveCargoInCommon(from, to);
00090 }
00091
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 && !src->IsArticulatedPart()) {
00104
00105 src = 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 && !dest->IsArticulatedPart()) {
00113
00114 dest = 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);
00123 }
00124 }
00125
00126
00127 if (part_of_chain && new_head->type == VEH_TRAIN) Train::From(new_head)->ConsistChanged(true);
00128 }
00129
00136 static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, EngineID engine_type)
00137 {
00138
00139 uint32 union_refit_mask_a = GetUnionOfArticulatedRefitMasks(v->engine_type, false);
00140 uint32 union_refit_mask_b = GetUnionOfArticulatedRefitMasks(engine_type, false);
00141
00142 const Order *o;
00143 const Vehicle *u = (v->type == VEH_TRAIN) ? v->First() : v;
00144 FOR_VEHICLE_ORDERS(u, o) {
00145 if (!o->IsRefit()) continue;
00146 CargoID cargo_type = o->GetRefitCargo();
00147
00148 if (!HasBit(union_refit_mask_a, cargo_type)) continue;
00149 if (!HasBit(union_refit_mask_b, cargo_type)) return false;
00150 }
00151
00152 return true;
00153 }
00154
00164 static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type, bool part_of_chain)
00165 {
00166 uint32 available_cargo_types, union_mask;
00167 GetArticulatedRefitMasks(engine_type, true, &union_mask, &available_cargo_types);
00168
00169 if (union_mask == 0) return CT_NO_REFIT;
00170
00171 CargoID cargo_type;
00172 if (IsArticulatedVehicleCarryingDifferentCargos(v, &cargo_type)) return CT_INVALID;
00173
00174 if (cargo_type == CT_INVALID) {
00175 if (v->type != VEH_TRAIN) return CT_NO_REFIT;
00176
00177 if (!part_of_chain) return CT_NO_REFIT;
00178
00179
00180
00181
00182 for (v = v->First(); v != NULL; v = v->Next()) {
00183 if (v->cargo_cap == 0) continue;
00184
00185 if (HasBit(available_cargo_types, v->cargo_type)) {
00186
00187 CargoArray default_capacity = GetCapacityOfArticulatedParts(engine_type);
00188 for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
00189 if (cid != v->cargo_type && default_capacity[cid] > 0) return v->cargo_type;
00190 }
00191
00192 return CT_NO_REFIT;
00193 }
00194 }
00195
00196 return CT_NO_REFIT;
00197 } else {
00198 if (!HasBit(available_cargo_types, cargo_type)) return CT_INVALID;
00199
00200 if (part_of_chain && !VerifyAutoreplaceRefitForOrders(v, engine_type)) return CT_INVALID;
00201
00202
00203 CargoArray default_capacity = GetCapacityOfArticulatedParts(engine_type);
00204 for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
00205 if (cid != cargo_type && default_capacity[cid] > 0) return cargo_type;
00206 }
00207
00208 return CT_NO_REFIT;
00209 }
00210 }
00211
00218 static EngineID GetNewEngineType(const Vehicle *v, const Company *c)
00219 {
00220 assert(v->type != VEH_TRAIN || !v->IsArticulatedPart());
00221
00222 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
00223
00224 return INVALID_ENGINE;
00225 }
00226
00227 EngineID e = EngineReplacementForCompany(c, v->engine_type, v->group_id);
00228
00229 if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_company)) {
00230 return e;
00231 }
00232
00233 if (v->NeedsAutorenewing(c) &&
00234 IsEngineBuildable(v->engine_type, v->type, _current_company)) {
00235 return v->engine_type;
00236 }
00237
00238 return INVALID_ENGINE;
00239 }
00240
00249 static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehicle, bool part_of_chain)
00250 {
00251 *new_vehicle = NULL;
00252
00253
00254 const Company *c = Company::Get(_current_company);
00255 EngineID e = GetNewEngineType(old_veh, c);
00256 if (e == INVALID_ENGINE) return CommandCost();
00257
00258
00259 CargoID refit_cargo = GetNewCargoTypeForReplace(old_veh, e, part_of_chain);
00260 if (refit_cargo == CT_INVALID) return CommandCost();
00261
00262
00263 CommandCost cost = DoCommand(old_veh->tile, e, 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_veh));
00264 if (cost.Failed()) return cost;
00265
00266 Vehicle *new_veh = Vehicle::Get(_new_vehicle_id);
00267 *new_vehicle = new_veh;
00268
00269
00270 byte subtype = GetBestFittingSubType(old_veh, new_veh);
00271
00272
00273 if (subtype != 0 && refit_cargo == CT_NO_REFIT) refit_cargo = old_veh->cargo_type;
00274
00275 if (refit_cargo != CT_NO_REFIT) {
00276 cost.AddCost(DoCommand(0, new_veh->index, refit_cargo | (subtype << 8), DC_EXEC, GetCmdRefitVeh(new_veh)));
00277 assert(cost.Succeeded());
00278 }
00279
00280
00281 if (new_veh->type == VEH_TRAIN && HasBit(Train::From(old_veh)->flags, VRF_REVERSE_DIRECTION)) {
00282 DoCommand(0, new_veh->index, true, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
00283 }
00284
00285 return cost;
00286 }
00287
00294 static inline CommandCost CmdStartStopVehicle(const Vehicle *v, bool evaluate_callback)
00295 {
00296 return DoCommand(0, v->index, evaluate_callback ? 1 : 0, DC_EXEC | DC_AUTOREPLACE, CMD_START_STOP_VEHICLE);
00297 }
00298
00307 static inline CommandCost CmdMoveVehicle(const Vehicle *v, const Vehicle *after, DoCommandFlag flags, bool whole_chain)
00308 {
00309 return DoCommand(0, v->index | (whole_chain ? 1 : 0) << 20, after != NULL ? after->index : INVALID_VEHICLE, flags, CMD_MOVE_RAIL_VEHICLE);
00310 }
00311
00318 static CommandCost CopyHeadSpecificThings(Vehicle *old_head, Vehicle *new_head, DoCommandFlag flags)
00319 {
00320 CommandCost cost = CommandCost();
00321
00322
00323 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));
00324
00325
00326 if (cost.Succeeded() && old_head != new_head) cost.AddCost(DoCommand(0, old_head->group_id, new_head->index, DC_EXEC, CMD_ADD_VEHICLE_GROUP));
00327
00328
00329 if (cost.Succeeded()) {
00330
00331 assert((new_head->vehstatus & VS_STOPPED) != 0);
00332 cost.AddCost(CmdStartStopVehicle(new_head, true));
00333
00334
00335 if (cost.Succeeded()) cost.AddCost(CmdStartStopVehicle(new_head, false));
00336 }
00337
00338
00339 if (cost.Succeeded() && old_head != new_head && (flags & DC_EXEC) != 0) {
00340
00341 if (old_head->name != NULL) {
00342 DoCommand(0, new_head->index, 0, DC_EXEC | DC_AUTOREPLACE, CMD_RENAME_VEHICLE, old_head->name);
00343 }
00344
00345
00346 new_head->CopyVehicleConfigAndStatistics(old_head);
00347
00348
00349 ChangeVehicleViewports(old_head->index, new_head->index);
00350 ChangeVehicleViewWindow(old_head->index, new_head->index);
00351 ChangeVehicleNews(old_head->index, new_head->index);
00352 }
00353
00354 return cost;
00355 }
00356
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 CmdMoveVehicle(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
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 = CeilDiv(Train::From(old_head)->gcache.cached_total_length, 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(CmdMoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true));
00454
00455 assert(Train::From(new_head)->GetNextUnit() == NULL);
00456
00457
00458
00459
00460
00461 Train *last_engine = NULL;
00462 if (cost.Succeeded()) {
00463 for (int i = num_units - 1; i > 0; i--) {
00464 Train *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
00465
00466 if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) continue;
00467
00468 if (new_vehs[i] != NULL) {
00469
00470
00471
00472 CmdMoveVehicle(old_vehs[i], NULL, DC_EXEC | DC_AUTOREPLACE, false);
00473 }
00474
00475 if (last_engine == NULL) last_engine = append;
00476 cost.AddCost(CmdMoveVehicle(append, new_head, DC_EXEC, false));
00477 if (cost.Failed()) break;
00478 }
00479 if (last_engine == NULL) last_engine = new_head;
00480 }
00481
00482
00483 if (cost.Succeeded() && wagon_removal && new_head->gcache.cached_total_length > old_total_length) cost = CommandCost(STR_ERROR_TRAIN_TOO_LONG_AFTER_REPLACEMENT);
00484
00485
00486
00487
00488 if (cost.Succeeded()) {
00489 for (int i = num_units - 1; i > 0; i--) {
00490 assert(last_engine != NULL);
00491 Vehicle *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
00492
00493 if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) {
00494
00495 CommandCost res = CmdMoveVehicle(append, last_engine, DC_EXEC, false);
00496
00497
00498
00499
00500 if (wagon_removal && (res.Failed() ? res.GetErrorMessage() == STR_ERROR_TRAIN_TOO_LONG : new_head->gcache.cached_total_length > old_total_length)) {
00501 CmdMoveVehicle(append, NULL, DC_EXEC | DC_AUTOREPLACE, false);
00502 break;
00503 }
00504
00505 cost.AddCost(res);
00506 if (cost.Failed()) break;
00507 } else {
00508
00509 assert(append == last_engine);
00510 last_engine = last_engine->GetPrevUnit();
00511 }
00512 }
00513 }
00514
00515
00516 if (cost.Succeeded() && wagon_removal) {
00517 assert(new_head->gcache.cached_total_length <= _settings_game.vehicle.max_train_length * TILE_SIZE);
00518 for (int i = 1; i < num_units; i++) {
00519 Vehicle *wagon = new_vehs[i];
00520 if (wagon == NULL) continue;
00521 if (wagon->First() == new_head) break;
00522
00523 assert(RailVehInfo(wagon->engine_type)->railveh_type == RAILVEH_WAGON);
00524
00525
00526 CommandCost ret = DoCommand(0, wagon->index, 0, DC_EXEC, GetCmdSellVeh(wagon));
00527 assert(ret.Succeeded());
00528 new_vehs[i] = NULL;
00529
00530
00531
00532 cost.AddCost(-new_costs[i]);
00533 }
00534 }
00535
00536
00537 if (cost.Succeeded()) cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags));
00538
00539 if (cost.Succeeded()) {
00540
00541 if ((flags & DC_EXEC) != 0 && new_head != old_head) {
00542 *chain = new_head;
00543 }
00544
00545
00546 for (int i = 0; i < num_units; i++) {
00547 Vehicle *w = old_vehs[i];
00548
00549
00550 if (w->First() == new_head) continue;
00551
00552 if ((flags & DC_EXEC) != 0) TransferCargo(w, new_head, true);
00553
00554
00555
00556
00557 cost.AddCost(DoCommand(0, w->index, 0, flags | DC_AUTOREPLACE, GetCmdSellVeh(w)));
00558 if ((flags & DC_EXEC) != 0) {
00559 old_vehs[i] = NULL;
00560 if (i == 0) old_head = NULL;
00561 }
00562 }
00563 }
00564
00565
00566
00567
00568 if ((flags & DC_EXEC) == 0) {
00569
00570 Train *second = Train::From(old_head)->GetNextUnit();
00571 if (second != NULL) CmdMoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true);
00572
00573 assert(Train::From(old_head)->GetNextUnit() == NULL);
00574
00575 for (int i = num_units - 1; i > 0; i--) {
00576 CommandCost ret = CmdMoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false);
00577 assert(ret.Succeeded());
00578 }
00579 }
00580 }
00581
00582
00583 if ((flags & DC_EXEC) == 0) {
00584 for (int i = num_units - 1; i >= 0; i--) {
00585 if (new_vehs[i] != NULL) {
00586 DoCommand(0, new_vehs[i]->index, 0, DC_EXEC, GetCmdSellVeh(new_vehs[i]));
00587 new_vehs[i] = NULL;
00588 }
00589 }
00590 }
00591
00592 free(old_vehs);
00593 free(new_vehs);
00594 free(new_costs);
00595 } else {
00596
00597 Vehicle *new_head = NULL;
00598 cost.AddCost(BuildReplacementVehicle(old_head, &new_head, true));
00599
00600
00601 if (cost.Succeeded() && new_head != NULL) {
00602 *nothing_to_do = false;
00603
00604
00605 cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags));
00606
00607 if (cost.Succeeded()) {
00608
00609 if ((flags & DC_EXEC) != 0) {
00610 TransferCargo(old_head, new_head, true);
00611 *chain = new_head;
00612 }
00613
00614
00615 cost.AddCost(DoCommand(0, old_head->index, 0, flags, GetCmdSellVeh(old_head)));
00616 }
00617
00618
00619 if ((flags & DC_EXEC) == 0) {
00620 DoCommand(0, new_head->index, 0, DC_EXEC, GetCmdSellVeh(new_head));
00621 }
00622 }
00623 }
00624
00625 return cost;
00626 }
00627
00638 CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00639 {
00640 Vehicle *v = Vehicle::GetIfValid(p1);
00641 if (v == NULL) return CMD_ERROR;
00642
00643 CommandCost ret = CheckOwnership(v->owner);
00644 if (ret.Failed()) return ret;
00645
00646 if (!v->IsInDepot()) return CMD_ERROR;
00647 if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
00648
00649 bool free_wagon = false;
00650 if (v->type == VEH_TRAIN) {
00651 Train *t = Train::From(v);
00652 if (t->IsArticulatedPart() || t->IsRearDualheaded()) return CMD_ERROR;
00653 free_wagon = !t->IsFrontEngine();
00654 if (free_wagon && t->First()->IsFrontEngine()) return CMD_ERROR;
00655 } else {
00656 if (!v->IsPrimaryVehicle()) return CMD_ERROR;
00657 }
00658
00659 const Company *c = Company::Get(_current_company);
00660 bool wagon_removal = c->settings.renew_keep_length;
00661
00662
00663 Vehicle *w = v;
00664 bool any_replacements = false;
00665 while (w != NULL && !any_replacements) {
00666 any_replacements = (GetNewEngineType(w, c) != INVALID_ENGINE);
00667 w = (!free_wagon && w->type == VEH_TRAIN ? Train::From(w)->GetNextUnit() : NULL);
00668 }
00669
00670 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00671 bool nothing_to_do = true;
00672
00673 if (any_replacements) {
00674 bool was_stopped = free_wagon || ((v->vehstatus & VS_STOPPED) != 0);
00675
00676
00677 if (!was_stopped) cost.AddCost(CmdStartStopVehicle(v, true));
00678 if (cost.Failed()) return cost;
00679
00680 assert(v->IsStoppedInDepot());
00681
00682
00683
00684
00685 SavedRandomSeeds saved_seeds;
00686 SaveRandomSeeds(&saved_seeds);
00687 if (free_wagon) {
00688 cost.AddCost(ReplaceFreeUnit(&v, flags & ~DC_EXEC, ¬hing_to_do));
00689 } else {
00690 cost.AddCost(ReplaceChain(&v, flags & ~DC_EXEC, wagon_removal, ¬hing_to_do));
00691 }
00692 RestoreRandomSeeds(saved_seeds);
00693
00694 if (cost.Succeeded() && (flags & DC_EXEC) != 0) {
00695 CommandCost ret;
00696 if (free_wagon) {
00697 ret = ReplaceFreeUnit(&v, flags, ¬hing_to_do);
00698 } else {
00699 ret = ReplaceChain(&v, flags, wagon_removal, ¬hing_to_do);
00700 }
00701 assert(ret.Succeeded() && ret.GetCost() == cost.GetCost());
00702 }
00703
00704
00705 if (!was_stopped) cost.AddCost(CmdStartStopVehicle(v, false));
00706 }
00707
00708 if (cost.Succeeded() && nothing_to_do) cost = CommandCost(STR_ERROR_AUTOREPLACE_NOTHING_TO_DO);
00709 return cost;
00710 }
00711
00724 CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00725 {
00726 Company *c = Company::GetIfValid(_current_company);
00727 if (c == NULL) return CMD_ERROR;
00728
00729 EngineID old_engine_type = GB(p2, 0, 16);
00730 EngineID new_engine_type = GB(p2, 16, 16);
00731 GroupID id_g = GB(p1, 16, 16);
00732 CommandCost cost;
00733
00734 if (!Group::IsValidID(id_g) && !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
00735 if (!Engine::IsValidID(old_engine_type)) return CMD_ERROR;
00736
00737 if (new_engine_type != INVALID_ENGINE) {
00738 if (!Engine::IsValidID(new_engine_type)) return CMD_ERROR;
00739 if (!CheckAutoreplaceValidity(old_engine_type, new_engine_type, _current_company)) return CMD_ERROR;
00740
00741 cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, flags);
00742 } else {
00743 cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags);
00744 }
00745
00746 if ((flags & DC_EXEC) && IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
00747
00748 return cost;
00749 }
00750