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 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
00097 static void TransferCargo(Vehicle *old_veh, Vehicle *new_head, bool part_of_chain)
00098 {
00099 assert(!part_of_chain || new_head->IsPrimaryVehicle());
00100
00101 for (Vehicle *src = old_veh; src != NULL; src = src->Next()) {
00102 if (!part_of_chain && src->type == VEH_TRAIN && src != old_veh && src != Train::From(old_veh)->other_multiheaded_part && !Train::From(src)->IsArticulatedPart()) {
00103
00104 src = Train::From(src)->GetLastEnginePart();
00105 continue;
00106 }
00107 if (src->cargo_type >= NUM_CARGO || src->cargo.Count() == 0) continue;
00108
00109
00110 for (Vehicle *dest = new_head; dest != NULL && src->cargo.Count() > 0; dest = dest->Next()) {
00111 if (!part_of_chain && dest->type == VEH_TRAIN && dest != new_head && dest != Train::From(new_head)->other_multiheaded_part && !Train::From(dest)->IsArticulatedPart()) {
00112
00113 dest = Train::From(dest)->GetLastEnginePart();
00114 continue;
00115 }
00116 if (dest->cargo_type != src->cargo_type) continue;
00117
00118 uint amount = min(src->cargo.Count(), dest->cargo_cap - dest->cargo.Count());
00119 if (amount <= 0) continue;
00120
00121 src->cargo.MoveTo(&dest->cargo, amount, VehicleCargoList::MTA_UNLOAD, NULL);
00122 }
00123 }
00124
00125
00126 if (part_of_chain && new_head->type == VEH_TRAIN) Train::From(new_head)->ConsistChanged(true);
00127 }
00128
00135 static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, EngineID engine_type)
00136 {
00137 const Order *o;
00138 const Vehicle *u;
00139
00140 uint32 union_refit_mask_a = GetUnionOfArticulatedRefitMasks(v->engine_type, false);
00141 uint32 union_refit_mask_b = GetUnionOfArticulatedRefitMasks(engine_type, false);
00142
00143 if (v->type == VEH_TRAIN) {
00144 u = v->First();
00145 } else {
00146 u = v;
00147 }
00148
00149 FOR_VEHICLE_ORDERS(u, o) {
00150 if (!o->IsRefit()) continue;
00151 CargoID cargo_type = o->GetRefitCargo();
00152
00153 if (!HasBit(union_refit_mask_a, cargo_type)) continue;
00154 if (!HasBit(union_refit_mask_b, cargo_type)) return false;
00155 }
00156
00157 return true;
00158 }
00159
00169 static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type, bool part_of_chain)
00170 {
00171 CargoID cargo_type;
00172 uint32 available_cargo_types, union_mask;
00173 GetArticulatedRefitMasks(engine_type, true, &union_mask, &available_cargo_types);
00174
00175 if (union_mask == 0) return CT_NO_REFIT;
00176
00177 if (IsArticulatedVehicleCarryingDifferentCargos(v, &cargo_type)) return CT_INVALID;
00178
00179 if (cargo_type == CT_INVALID) {
00180 if (v->type != VEH_TRAIN) return CT_NO_REFIT;
00181
00182 if (!part_of_chain) return CT_NO_REFIT;
00183
00184
00185
00186
00187 for (v = v->First(); v != NULL; v = v->Next()) {
00188 if (v->cargo_cap == 0) continue;
00189
00190 if (HasBit(available_cargo_types, v->cargo_type)) {
00191
00192 CargoArray default_capacity = GetCapacityOfArticulatedParts(engine_type);
00193 for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
00194 if (cid != v->cargo_type && default_capacity[cid] > 0) return v->cargo_type;
00195 }
00196
00197 return CT_NO_REFIT;
00198 }
00199 }
00200
00201 return CT_NO_REFIT;
00202 } else {
00203 if (!HasBit(available_cargo_types, cargo_type)) return CT_INVALID;
00204
00205 if (part_of_chain && !VerifyAutoreplaceRefitForOrders(v, engine_type)) return CT_INVALID;
00206
00207
00208 CargoArray default_capacity = GetCapacityOfArticulatedParts(engine_type);
00209 for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
00210 if (cid != cargo_type && default_capacity[cid] > 0) return cargo_type;
00211 }
00212
00213 return CT_NO_REFIT;
00214 }
00215 }
00216
00222 static EngineID GetNewEngineType(const Vehicle *v, const Company *c)
00223 {
00224 assert(v->type != VEH_TRAIN || !Train::From(v)->IsArticulatedPart());
00225
00226 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) {
00227
00228 return INVALID_ENGINE;
00229 }
00230
00231 EngineID e = EngineReplacementForCompany(c, v->engine_type, v->group_id);
00232
00233 if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_company)) {
00234 return e;
00235 }
00236
00237 if (v->NeedsAutorenewing(c) &&
00238 IsEngineBuildable(v->engine_type, v->type, _current_company)) {
00239 return v->engine_type;
00240 }
00241
00242 return INVALID_ENGINE;
00243 }
00244
00252 static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehicle, bool part_of_chain)
00253 {
00254 *new_vehicle = NULL;
00255
00256
00257 const Company *c = Company::Get(_current_company);
00258 EngineID e = GetNewEngineType(old_veh, c);
00259 if (e == INVALID_ENGINE) return CommandCost();
00260
00261
00262 CargoID refit_cargo = GetNewCargoTypeForReplace(old_veh, e, part_of_chain);
00263 if (refit_cargo == CT_INVALID) return CommandCost();
00264
00265
00266 CommandCost cost = DoCommand(old_veh->tile, e, 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_veh));
00267 if (cost.Failed()) return cost;
00268
00269 Vehicle *new_veh = Vehicle::Get(_new_vehicle_id);
00270 *new_vehicle = new_veh;
00271
00272
00273 byte subtype = GetBestFittingSubType(old_veh, new_veh);
00274
00275
00276 if (subtype != 0 && refit_cargo == CT_NO_REFIT) refit_cargo = old_veh->cargo_type;
00277
00278 if (refit_cargo != CT_NO_REFIT) {
00279 cost.AddCost(DoCommand(0, new_veh->index, refit_cargo | (subtype << 8), DC_EXEC, GetCmdRefitVeh(new_veh)));
00280 assert(cost.Succeeded());
00281 }
00282
00283
00284 if (new_veh->type == VEH_TRAIN && HasBit(Train::From(old_veh)->flags, VRF_REVERSE_DIRECTION)) {
00285 DoCommand(0, new_veh->index, true, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
00286 }
00287
00288 return cost;
00289 }
00290
00296 static inline CommandCost StartStopVehicle(const Vehicle *v, bool evaluate_callback)
00297 {
00298 return DoCommand(0, v->index, evaluate_callback ? 1 : 0, DC_EXEC | DC_AUTOREPLACE, CMD_START_STOP_VEHICLE);
00299 }
00300
00308 static inline CommandCost MoveVehicle(const Vehicle *v, const Vehicle *after, DoCommandFlag flags, bool whole_chain)
00309 {
00310 return DoCommand(0, v->index | (after != NULL ? after->index : INVALID_VEHICLE) << 16, whole_chain ? 1 : 0, flags, CMD_MOVE_RAIL_VEHICLE);
00311 }
00312
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, (old_head->index << 16) | new_head->index, CO_SHARE, 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(StartStopVehicle(new_head, true));
00333
00334
00335 if (cost.Succeeded()) cost.AddCost(StartStopVehicle(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
00363 static CommandCost ReplaceFreeUnit(Vehicle **single_unit, DoCommandFlag flags, bool *nothing_to_do)
00364 {
00365 Train *old_v = Train::From(*single_unit);
00366 assert(!old_v->IsArticulatedPart() && !old_v->IsRearDualheaded());
00367
00368 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00369
00370
00371 Vehicle *new_v = NULL;
00372 cost.AddCost(BuildReplacementVehicle(old_v, &new_v, false));
00373
00374
00375 if (cost.Succeeded() && new_v != NULL) {
00376 *nothing_to_do = false;
00377
00378 if ((flags & DC_EXEC) != 0) {
00379
00380 MoveVehicle(new_v, old_v, DC_EXEC, false);
00381
00382
00383
00384
00385
00386
00387
00388 TransferCargo(old_v, new_v, false);
00389
00390 *single_unit = new_v;
00391 }
00392
00393
00394 cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v)));
00395
00396
00397 if ((flags & DC_EXEC) == 0) {
00398 DoCommand(0, new_v->index, 0, DC_EXEC, GetCmdSellVeh(new_v));
00399 }
00400 }
00401
00402 return cost;
00403 }
00404
00412 static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon_removal, bool *nothing_to_do)
00413 {
00414 Vehicle *old_head = *chain;
00415 assert(old_head->IsPrimaryVehicle());
00416
00417 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00418
00419 if (old_head->type == VEH_TRAIN) {
00420
00421 uint16 old_total_length = (Train::From(old_head)->tcache.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE;
00422
00423 int num_units = 0;
00424 for (Train *w = Train::From(old_head); w != NULL; w = w->GetNextUnit()) num_units++;
00425
00426 Train **old_vehs = CallocT<Train *>(num_units);
00427 Train **new_vehs = CallocT<Train *>(num_units);
00428 Money *new_costs = MallocT<Money>(num_units);
00429
00430
00431
00432 int i;
00433 Train *w;
00434 for (w = Train::From(old_head), i = 0; w != NULL; w = w->GetNextUnit(), i++) {
00435 assert(i < num_units);
00436 old_vehs[i] = w;
00437
00438 CommandCost ret = BuildReplacementVehicle(old_vehs[i], (Vehicle**)&new_vehs[i], true);
00439 cost.AddCost(ret);
00440 if (cost.Failed()) break;
00441
00442 new_costs[i] = ret.GetCost();
00443 if (new_vehs[i] != NULL) *nothing_to_do = false;
00444 }
00445 Train *new_head = (new_vehs[0] != NULL ? new_vehs[0] : old_vehs[0]);
00446
00447
00448 if (cost.Succeeded()) {
00449
00450 Train *second = Train::From(old_head)->GetNextUnit();
00451 if (second != NULL) cost.AddCost(MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true));
00452
00453 assert(Train::From(new_head)->GetNextUnit() == NULL);
00454
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 (new_vehs[i] != NULL) {
00467
00468
00469
00470 MoveVehicle(old_vehs[i], NULL, DC_EXEC | DC_AUTOREPLACE, false);
00471 }
00472
00473 if (last_engine == NULL) last_engine = append;
00474 cost.AddCost(MoveVehicle(append, new_head, DC_EXEC, false));
00475 if (cost.Failed()) break;
00476 }
00477 if (last_engine == NULL) last_engine = new_head;
00478 }
00479
00480
00481 if (cost.Succeeded() && wagon_removal && new_head->tcache.cached_total_length > old_total_length) cost = CommandCost(STR_ERROR_TRAIN_TOO_LONG_AFTER_REPLACEMENT);
00482
00483
00484
00485
00486 if (cost.Succeeded()) {
00487 for (int i = num_units - 1; i > 0; i--) {
00488 assert(last_engine != NULL);
00489 Vehicle *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
00490
00491 if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) {
00492
00493 CommandCost res = MoveVehicle(append, last_engine, DC_EXEC, false);
00494
00495 if (res.Succeeded() && wagon_removal && new_head->tcache.cached_total_length > old_total_length) {
00496 MoveVehicle(append, NULL, DC_EXEC | DC_AUTOREPLACE, false);
00497 break;
00498 }
00499
00500 cost.AddCost(res);
00501 if (cost.Failed()) break;
00502 } else {
00503
00504 assert(append == last_engine);
00505 last_engine = last_engine->GetPrevUnit();
00506 }
00507 }
00508 }
00509
00510
00511 if (cost.Succeeded() && wagon_removal) {
00512 for (int i = 1; i < num_units; i++) {
00513 Vehicle *wagon = new_vehs[i];
00514 if (wagon == NULL) continue;
00515 if (wagon->First() == new_head) break;
00516
00517 assert(RailVehInfo(wagon->engine_type)->railveh_type == RAILVEH_WAGON);
00518
00519
00520 CommandCost ret = DoCommand(0, wagon->index, 0, DC_EXEC, GetCmdSellVeh(wagon));
00521 assert(ret.Succeeded());
00522 new_vehs[i] = NULL;
00523
00524
00525
00526 cost.AddCost(-new_costs[i]);
00527 }
00528 }
00529
00530
00531 if (cost.Succeeded()) cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags));
00532
00533 if (cost.Succeeded()) {
00534
00535 if ((flags & DC_EXEC) != 0 && new_head != old_head) {
00536 *chain = new_head;
00537 }
00538
00539
00540 for (int i = 0; i < num_units; i++) {
00541 Vehicle *w = old_vehs[i];
00542
00543
00544 if (w->First() == new_head) continue;
00545
00546 if ((flags & DC_EXEC) != 0) TransferCargo(w, new_head, true);
00547
00548
00549
00550
00551 cost.AddCost(DoCommand(0, w->index, 0, flags | DC_AUTOREPLACE, GetCmdSellVeh(w)));
00552 if ((flags & DC_EXEC) != 0) {
00553 old_vehs[i] = NULL;
00554 if (i == 0) old_head = NULL;
00555 }
00556 }
00557 }
00558
00559
00560
00561
00562 if ((flags & DC_EXEC) == 0) {
00563
00564 Train *second = Train::From(old_head)->GetNextUnit();
00565 if (second != NULL) MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true);
00566
00567 assert(Train::From(old_head)->GetNextUnit() == NULL);
00568
00569 for (int i = num_units - 1; i > 0; i--) {
00570 CommandCost ret = MoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false);
00571 assert(ret.Succeeded());
00572 }
00573 }
00574 }
00575
00576
00577 if ((flags & DC_EXEC) == 0) {
00578 for (int i = num_units - 1; i >= 0; i--) {
00579 if (new_vehs[i] != NULL) {
00580 DoCommand(0, new_vehs[i]->index, 0, DC_EXEC, GetCmdSellVeh(new_vehs[i]));
00581 new_vehs[i] = NULL;
00582 }
00583 }
00584 }
00585
00586 free(old_vehs);
00587 free(new_vehs);
00588 free(new_costs);
00589 } else {
00590
00591 Vehicle *new_head = NULL;
00592 cost.AddCost(BuildReplacementVehicle(old_head, &new_head, true));
00593
00594
00595 if (cost.Succeeded() && new_head != NULL) {
00596 *nothing_to_do = false;
00597
00598
00599 cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags));
00600
00601 if (cost.Succeeded()) {
00602
00603 if ((flags & DC_EXEC) != 0) {
00604 TransferCargo(old_head, new_head, true);
00605 *chain = new_head;
00606 }
00607
00608
00609 cost.AddCost(DoCommand(0, old_head->index, 0, flags, GetCmdSellVeh(old_head)));
00610 }
00611
00612
00613 if ((flags & DC_EXEC) == 0) {
00614 DoCommand(0, new_head->index, 0, DC_EXEC, GetCmdSellVeh(new_head));
00615 }
00616 }
00617 }
00618
00619 return cost;
00620 }
00621
00631 CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00632 {
00633 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00634 bool nothing_to_do = true;
00635
00636 Vehicle *v = Vehicle::GetIfValid(p1);
00637 if (v == NULL) return CMD_ERROR;
00638
00639 if (!CheckOwnership(v->owner)) return CMD_ERROR;
00640 if (!v->IsInDepot()) return CMD_ERROR;
00641 if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
00642
00643 bool free_wagon = false;
00644 if (v->type == VEH_TRAIN) {
00645 Train *t = Train::From(v);
00646 if (t->IsArticulatedPart() || t->IsRearDualheaded()) return CMD_ERROR;
00647 free_wagon = !t->IsFrontEngine();
00648 if (free_wagon && t->First()->IsFrontEngine()) return CMD_ERROR;
00649 } else {
00650 if (!v->IsPrimaryVehicle()) return CMD_ERROR;
00651 }
00652
00653 const Company *c = Company::Get(_current_company);
00654 bool wagon_removal = c->settings.renew_keep_length;
00655
00656
00657 Vehicle *w = v;
00658 bool any_replacements = false;
00659 while (w != NULL && !any_replacements) {
00660 any_replacements = (GetNewEngineType(w, c) != INVALID_ENGINE);
00661 w = (!free_wagon && w->type == VEH_TRAIN ? Train::From(w)->GetNextUnit() : NULL);
00662 }
00663
00664 if (any_replacements) {
00665 bool was_stopped = free_wagon || ((v->vehstatus & VS_STOPPED) != 0);
00666
00667
00668 if (!was_stopped) cost.AddCost(StartStopVehicle(v, true));
00669 if (cost.Failed()) return cost;
00670
00671 assert(v->IsStoppedInDepot());
00672
00673
00674
00675
00676 SavedRandomSeeds saved_seeds;
00677 SaveRandomSeeds(&saved_seeds);
00678 if (free_wagon) {
00679 cost.AddCost(ReplaceFreeUnit(&v, flags & ~DC_EXEC, ¬hing_to_do));
00680 } else {
00681 cost.AddCost(ReplaceChain(&v, flags & ~DC_EXEC, wagon_removal, ¬hing_to_do));
00682 }
00683 RestoreRandomSeeds(saved_seeds);
00684
00685 if (cost.Succeeded() && (flags & DC_EXEC) != 0) {
00686 CommandCost ret;
00687 if (free_wagon) {
00688 ret = ReplaceFreeUnit(&v, flags, ¬hing_to_do);
00689 } else {
00690 ret = ReplaceChain(&v, flags, wagon_removal, ¬hing_to_do);
00691 }
00692 assert(ret.Succeeded() && ret.GetCost() == cost.GetCost());
00693 }
00694
00695
00696 if (!was_stopped) cost.AddCost(StartStopVehicle(v, false));
00697 }
00698
00699 if (cost.Succeeded() && nothing_to_do) cost = CommandCost(STR_ERROR_AUTOREPLACE_NOTHING_TO_DO);
00700 return cost;
00701 }