autoreplace_cmd.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
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_cargoes_a = GetUnionOfArticulatedRefitMasks(engine_a, true);
00040   uint32 available_cargoes_b = GetUnionOfArticulatedRefitMasks(engine_b, true);
00041   return (available_cargoes_a == 0 || available_cargoes_b == 0 || (available_cargoes_a & available_cargoes_b) != 0);
00042 }
00043 
00051 bool CheckAutoreplaceValidity(EngineID from, EngineID to, CompanyID company)
00052 {
00053   assert(Engine::IsValidID(from) && Engine::IsValidID(to));
00054 
00055   /* we can't replace an engine into itself (that would be autorenew) */
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   /* check that the new vehicle type is available to the company and its type is the same as the original one */
00063   if (!IsEngineBuildable(to, type, company)) return false;
00064 
00065   switch (type) {
00066     case VEH_TRAIN: {
00067       /* make sure the railtypes are compatible */
00068       if ((GetRailTypeInfo(e_from->u.rail.railtype)->compatible_railtypes & GetRailTypeInfo(e_to->u.rail.railtype)->compatible_railtypes) == 0) return false;
00069 
00070       /* make sure we do not replace wagons with engines or vise versa */
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       /* make sure that we do not replace a tram with a normal road vehicles or vise versa */
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       /* make sure that we do not replace a plane with a helicopter or vise versa */
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   /* the engines needs to be able to carry the same cargo */
00089   return EnginesHaveCargoInCommon(from, to);
00090 }
00091 
00096 void CheckCargoCapacity(Vehicle *v)
00097 {
00098   assert(v == NULL || v->First() == v);
00099 
00100   for (Vehicle *src = v; src != NULL; src = src->Next()) {
00101     /* Do we need to more cargo away? */
00102     if (src->cargo.Count() <= src->cargo_cap) continue;
00103 
00104     /* We need to move a particular amount. Try that on the other vehicles. */
00105     uint to_spread = src->cargo.Count() - src->cargo_cap;
00106     for (Vehicle *dest = v; dest != NULL && to_spread != 0; dest = dest->Next()) {
00107       if (dest->cargo.Count() >= dest->cargo_cap || dest->cargo_type != src->cargo_type) continue;
00108 
00109       uint amount = min(to_spread, dest->cargo_cap - dest->cargo.Count());
00110       src->cargo.MoveTo(&dest->cargo, amount);
00111       to_spread -= amount;
00112     }
00113 
00114     /* Any left-overs will be thrown away, but not their feeder share. */
00115     src->cargo.Truncate(src->cargo_cap);
00116   }
00117 }
00118 
00125 static void TransferCargo(Vehicle *old_veh, Vehicle *new_head, bool part_of_chain)
00126 {
00127   assert(!part_of_chain || new_head->IsPrimaryVehicle());
00128   /* Loop through source parts */
00129   for (Vehicle *src = old_veh; src != NULL; src = src->Next()) {
00130     if (!part_of_chain && src->type == VEH_TRAIN && src != old_veh && src != Train::From(old_veh)->other_multiheaded_part && !src->IsArticulatedPart()) {
00131       /* Skip vehicles, which do not belong to old_veh */
00132       src = src->GetLastEnginePart();
00133       continue;
00134     }
00135     if (src->cargo_type >= NUM_CARGO || src->cargo.Count() == 0) continue;
00136 
00137     /* Find free space in the new chain */
00138     for (Vehicle *dest = new_head; dest != NULL && src->cargo.Count() > 0; dest = dest->Next()) {
00139       if (!part_of_chain && dest->type == VEH_TRAIN && dest != new_head && dest != Train::From(new_head)->other_multiheaded_part && !dest->IsArticulatedPart()) {
00140         /* Skip vehicles, which do not belong to new_head */
00141         dest = dest->GetLastEnginePart();
00142         continue;
00143       }
00144       if (dest->cargo_type != src->cargo_type) continue;
00145 
00146       uint amount = min(src->cargo.Count(), dest->cargo_cap - dest->cargo.Count());
00147       if (amount <= 0) continue;
00148 
00149       src->cargo.MoveTo(&dest->cargo, amount);
00150     }
00151   }
00152 
00153   /* Update train weight etc., the old vehicle will be sold anyway */
00154   if (part_of_chain && new_head->type == VEH_TRAIN) Train::From(new_head)->ConsistChanged(true);
00155 }
00156 
00163 static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, EngineID engine_type)
00164 {
00165 
00166   uint32 union_refit_mask_a = GetUnionOfArticulatedRefitMasks(v->engine_type, false);
00167   uint32 union_refit_mask_b = GetUnionOfArticulatedRefitMasks(engine_type, false);
00168 
00169   const Order *o;
00170   const Vehicle *u = (v->type == VEH_TRAIN) ? v->First() : v;
00171   FOR_VEHICLE_ORDERS(u, o) {
00172     if (!o->IsRefit() || o->IsAutoRefit()) continue;
00173     CargoID cargo_type = o->GetRefitCargo();
00174 
00175     if (!HasBit(union_refit_mask_a, cargo_type)) continue;
00176     if (!HasBit(union_refit_mask_b, cargo_type)) return false;
00177   }
00178 
00179   return true;
00180 }
00181 
00191 static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type, bool part_of_chain)
00192 {
00193   uint32 available_cargo_types, union_mask;
00194   GetArticulatedRefitMasks(engine_type, true, &union_mask, &available_cargo_types);
00195 
00196   if (union_mask == 0) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity
00197 
00198   CargoID cargo_type;
00199   if (IsArticulatedVehicleCarryingDifferentCargoes(v, &cargo_type)) return CT_INVALID; // We cannot refit to mixed cargoes in an automated way
00200 
00201   if (cargo_type == CT_INVALID) {
00202     if (v->type != VEH_TRAIN) return CT_NO_REFIT; // If the vehicle does not carry anything at all, every replacement is fine.
00203 
00204     if (!part_of_chain) return CT_NO_REFIT;
00205 
00206     /* the old engine didn't have cargo capacity, but the new one does
00207      * now we will figure out what cargo the train is carrying and refit to fit this */
00208 
00209     for (v = v->First(); v != NULL; v = v->Next()) {
00210       if (v->cargo_cap == 0) continue;
00211       /* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
00212       if (HasBit(available_cargo_types, v->cargo_type)) return v->cargo_type;
00213     }
00214 
00215     return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one
00216   } else {
00217     if (!HasBit(available_cargo_types, cargo_type)) return CT_INVALID; // We can't refit the vehicle to carry the cargo we want
00218 
00219     if (part_of_chain && !VerifyAutoreplaceRefitForOrders(v, engine_type)) return CT_INVALID; // Some refit orders lose their effect
00220 
00221     return cargo_type;
00222   }
00223 }
00224 
00232 static CommandCost GetNewEngineType(const Vehicle *v, const Company *c, 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     /* we build the rear ends of multiheaded trains with the front ones */
00240     return CommandCost();
00241   }
00242 
00243   e = EngineReplacementForCompany(c, v->engine_type, v->group_id);
00244 
00245   /* Autoreplace, if engine is available */
00246   if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_company)) {
00247     return CommandCost();
00248   }
00249 
00250   /* Autorenew if needed */
00251   if (v->NeedsAutorenewing(c)) e = v->engine_type;
00252 
00253   /* Nothing to do or all is fine? */
00254   if (e == INVALID_ENGINE || IsEngineBuildable(e, v->type, _current_company)) return CommandCost();
00255 
00256   /* The engine we need is not available. Report error to user */
00257   return CommandCost(STR_ERROR_RAIL_VEHICLE_NOT_AVAILABLE + v->type);
00258 }
00259 
00268 static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehicle, bool part_of_chain)
00269 {
00270   *new_vehicle = NULL;
00271 
00272   /* Shall the vehicle be replaced? */
00273   const Company *c = Company::Get(_current_company);
00274   EngineID e;
00275   CommandCost cost = GetNewEngineType(old_veh, c, e);
00276   if (cost.Failed()) return cost;
00277   if (e == INVALID_ENGINE) return CommandCost(); // neither autoreplace is set, nor autorenew is triggered
00278 
00279   /* Does it need to be refitted */
00280   CargoID refit_cargo = GetNewCargoTypeForReplace(old_veh, e, part_of_chain);
00281   if (refit_cargo == CT_INVALID) return CommandCost(); // incompatible cargoes
00282 
00283   /* Build the new vehicle */
00284   cost = DoCommand(old_veh->tile, e, 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_veh));
00285   if (cost.Failed()) return cost;
00286 
00287   Vehicle *new_veh = Vehicle::Get(_new_vehicle_id);
00288   *new_vehicle = new_veh;
00289 
00290   /* Refit the vehicle if needed */
00291   if (refit_cargo != CT_NO_REFIT) {
00292     byte subtype = GetBestFittingSubType(old_veh, new_veh, refit_cargo);
00293 
00294     cost.AddCost(DoCommand(0, new_veh->index, refit_cargo | (subtype << 8), DC_EXEC, GetCmdRefitVeh(new_veh)));
00295     assert(cost.Succeeded()); // This should be ensured by GetNewCargoTypeForReplace()
00296   }
00297 
00298   /* Try to reverse the vehicle, but do not care if it fails as the new type might not be reversible */
00299   if (new_veh->type == VEH_TRAIN && HasBit(Train::From(old_veh)->flags, VRF_REVERSE_DIRECTION)) {
00300     DoCommand(0, new_veh->index, true, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
00301   }
00302 
00303   return cost;
00304 }
00305 
00312 static inline CommandCost CmdStartStopVehicle(const Vehicle *v, bool evaluate_callback)
00313 {
00314   return DoCommand(0, v->index, evaluate_callback ? 1 : 0, DC_EXEC | DC_AUTOREPLACE, CMD_START_STOP_VEHICLE);
00315 }
00316 
00325 static inline CommandCost CmdMoveVehicle(const Vehicle *v, const Vehicle *after, DoCommandFlag flags, bool whole_chain)
00326 {
00327   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);
00328 }
00329 
00336 static CommandCost CopyHeadSpecificThings(Vehicle *old_head, Vehicle *new_head, DoCommandFlag flags)
00337 {
00338   CommandCost cost = CommandCost();
00339 
00340   /* Share orders */
00341   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));
00342 
00343   /* Copy group membership */
00344   if (cost.Succeeded() && old_head != new_head) cost.AddCost(DoCommand(0, old_head->group_id, new_head->index, DC_EXEC, CMD_ADD_VEHICLE_GROUP));
00345 
00346   /* Perform start/stop check whether the new vehicle suits newgrf restrictions etc. */
00347   if (cost.Succeeded()) {
00348     /* Start the vehicle, might be denied by certain things */
00349     assert((new_head->vehstatus & VS_STOPPED) != 0);
00350     cost.AddCost(CmdStartStopVehicle(new_head, true));
00351 
00352     /* Stop the vehicle again, but do not care about evil newgrfs allowing starting but not stopping :p */
00353     if (cost.Succeeded()) cost.AddCost(CmdStartStopVehicle(new_head, false));
00354   }
00355 
00356   /* Last do those things which do never fail (resp. we do not care about), but which are not undo-able */
00357   if (cost.Succeeded() && old_head != new_head && (flags & DC_EXEC) != 0) {
00358     /* Copy vehicle name */
00359     if (old_head->name != NULL) {
00360       DoCommand(0, new_head->index, 0, DC_EXEC | DC_AUTOREPLACE, CMD_RENAME_VEHICLE, old_head->name);
00361     }
00362 
00363     /* Copy other things which cannot be copied by a command and which shall not stay resetted from the build vehicle command */
00364     new_head->CopyVehicleConfigAndStatistics(old_head);
00365 
00366     /* Switch vehicle windows/news to the new vehicle, so they are not closed/deleted when the old vehicle is sold */
00367     ChangeVehicleViewports(old_head->index, new_head->index);
00368     ChangeVehicleViewWindow(old_head->index, new_head->index);
00369     ChangeVehicleNews(old_head->index, new_head->index);
00370   }
00371 
00372   return cost;
00373 }
00374 
00382 static CommandCost ReplaceFreeUnit(Vehicle **single_unit, DoCommandFlag flags, bool *nothing_to_do)
00383 {
00384   Train *old_v = Train::From(*single_unit);
00385   assert(!old_v->IsArticulatedPart() && !old_v->IsRearDualheaded());
00386 
00387   CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00388 
00389   /* Build and refit replacement vehicle */
00390   Vehicle *new_v = NULL;
00391   cost.AddCost(BuildReplacementVehicle(old_v, &new_v, false));
00392 
00393   /* Was a new vehicle constructed? */
00394   if (cost.Succeeded() && new_v != NULL) {
00395     *nothing_to_do = false;
00396 
00397     if ((flags & DC_EXEC) != 0) {
00398       /* Move the new vehicle behind the old */
00399       CmdMoveVehicle(new_v, old_v, DC_EXEC, false);
00400 
00401       /* Take over cargo
00402        * Note: We do only transfer cargo from the old to the new vehicle.
00403        *       I.e. we do not transfer remaining cargo to other vehicles.
00404        *       Else you would also need to consider moving cargo to other free chains,
00405        *       or doing the same in ReplaceChain(), which would be quite troublesome.
00406        */
00407       TransferCargo(old_v, new_v, false);
00408 
00409       *single_unit = new_v;
00410     }
00411 
00412     /* Sell the old vehicle */
00413     cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v)));
00414 
00415     /* If we are not in DC_EXEC undo everything */
00416     if ((flags & DC_EXEC) == 0) {
00417       DoCommand(0, new_v->index, 0, DC_EXEC, GetCmdSellVeh(new_v));
00418     }
00419   }
00420 
00421   return cost;
00422 }
00423 
00432 static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon_removal, bool *nothing_to_do)
00433 {
00434   Vehicle *old_head = *chain;
00435   assert(old_head->IsPrimaryVehicle());
00436 
00437   CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00438 
00439   if (old_head->type == VEH_TRAIN) {
00440     /* Store the length of the old vehicle chain, rounded up to whole tiles */
00441     uint16 old_total_length = CeilDiv(Train::From(old_head)->gcache.cached_total_length, TILE_SIZE) * TILE_SIZE;
00442 
00443     int num_units = 0; 
00444     for (Train *w = Train::From(old_head); w != NULL; w = w->GetNextUnit()) num_units++;
00445 
00446     Train **old_vehs = CallocT<Train *>(num_units); 
00447     Train **new_vehs = CallocT<Train *>(num_units); 
00448     Money *new_costs = MallocT<Money>(num_units);   
00449 
00450     /* Collect vehicles and build replacements
00451      * Note: The replacement vehicles can only successfully build as long as the old vehicles are still in their chain */
00452     int i;
00453     Train *w;
00454     for (w = Train::From(old_head), i = 0; w != NULL; w = w->GetNextUnit(), i++) {
00455       assert(i < num_units);
00456       old_vehs[i] = w;
00457 
00458       CommandCost ret = BuildReplacementVehicle(old_vehs[i], (Vehicle**)&new_vehs[i], true);
00459       cost.AddCost(ret);
00460       if (cost.Failed()) break;
00461 
00462       new_costs[i] = ret.GetCost();
00463       if (new_vehs[i] != NULL) *nothing_to_do = false;
00464     }
00465     Train *new_head = (new_vehs[0] != NULL ? new_vehs[0] : old_vehs[0]);
00466 
00467     /* Note: When autoreplace has already failed here, old_vehs[] is not completely initialized. But it is also not needed. */
00468     if (cost.Succeeded()) {
00469       /* Separate the head, so we can start constructing the new chain */
00470       Train *second = Train::From(old_head)->GetNextUnit();
00471       if (second != NULL) cost.AddCost(CmdMoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true));
00472 
00473       assert(Train::From(new_head)->GetNextUnit() == NULL);
00474 
00475       /* Append engines to the new chain
00476        * We do this from back to front, so that the head of the temporary vehicle chain does not change all the time.
00477        * That way we also have less trouble when exceeding the unitnumber limit.
00478        * OTOH the vehicle attach callback is more expensive this way :s */
00479       Train *last_engine = NULL; 
00480       if (cost.Succeeded()) {
00481         for (int i = num_units - 1; i > 0; i--) {
00482           Train *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
00483 
00484           if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) continue;
00485 
00486           if (new_vehs[i] != NULL) {
00487             /* Move the old engine to a separate row with DC_AUTOREPLACE. Else
00488              * moving the wagon in front may fail later due to unitnumber limit.
00489              * (We have to attach wagons without DC_AUTOREPLACE.) */
00490             CmdMoveVehicle(old_vehs[i], NULL, DC_EXEC | DC_AUTOREPLACE, false);
00491           }
00492 
00493           if (last_engine == NULL) last_engine = append;
00494           cost.AddCost(CmdMoveVehicle(append, new_head, DC_EXEC, false));
00495           if (cost.Failed()) break;
00496         }
00497         if (last_engine == NULL) last_engine = new_head;
00498       }
00499 
00500       /* When wagon removal is enabled and the new engines without any wagons are already longer than the old, we have to fail */
00501       if (cost.Succeeded() && wagon_removal && new_head->gcache.cached_total_length > old_total_length) cost = CommandCost(STR_ERROR_TRAIN_TOO_LONG_AFTER_REPLACEMENT);
00502 
00503       /* Append/insert wagons into the new vehicle chain
00504        * We do this from back to front, so we can stop when wagon removal or maximum train length (i.e. from mammoth-train setting) is triggered.
00505        */
00506       if (cost.Succeeded()) {
00507         for (int i = num_units - 1; i > 0; i--) {
00508           assert(last_engine != NULL);
00509           Vehicle *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
00510 
00511           if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) {
00512             /* Insert wagon after 'last_engine' */
00513             CommandCost res = CmdMoveVehicle(append, last_engine, DC_EXEC, false);
00514 
00515             /* When we allow removal of wagons, either the move failing due
00516              * to the train becoming too long, or the train becoming longer
00517              * would move the vehicle to the empty vehicle chain. */
00518             if (wagon_removal && (res.Failed() ? res.GetErrorMessage() == STR_ERROR_TRAIN_TOO_LONG : new_head->gcache.cached_total_length > old_total_length)) {
00519               CmdMoveVehicle(append, NULL, DC_EXEC | DC_AUTOREPLACE, false);
00520               break;
00521             }
00522 
00523             cost.AddCost(res);
00524             if (cost.Failed()) break;
00525           } else {
00526             /* We have reached 'last_engine', continue with the next engine towards the front */
00527             assert(append == last_engine);
00528             last_engine = last_engine->GetPrevUnit();
00529           }
00530         }
00531       }
00532 
00533       /* Sell superfluous new vehicles that could not be inserted. */
00534       if (cost.Succeeded() && wagon_removal) {
00535         assert(new_head->gcache.cached_total_length <= _settings_game.vehicle.max_train_length * TILE_SIZE);
00536         for (int i = 1; i < num_units; i++) {
00537           Vehicle *wagon = new_vehs[i];
00538           if (wagon == NULL) continue;
00539           if (wagon->First() == new_head) break;
00540 
00541           assert(RailVehInfo(wagon->engine_type)->railveh_type == RAILVEH_WAGON);
00542 
00543           /* Sell wagon */
00544           CommandCost ret = DoCommand(0, wagon->index, 0, DC_EXEC, GetCmdSellVeh(wagon));
00545           assert(ret.Succeeded());
00546           new_vehs[i] = NULL;
00547 
00548           /* Revert the money subtraction when the vehicle was built.
00549            * This value is different from the sell value, esp. because of refitting */
00550           cost.AddCost(-new_costs[i]);
00551         }
00552       }
00553 
00554       /* The new vehicle chain is constructed, now take over orders and everything... */
00555       if (cost.Succeeded()) cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags));
00556 
00557       if (cost.Succeeded()) {
00558         /* Success ! */
00559         if ((flags & DC_EXEC) != 0 && new_head != old_head) {
00560           *chain = new_head;
00561         }
00562 
00563         /* Transfer cargo of old vehicles and sell them */
00564         for (int i = 0; i < num_units; i++) {
00565           Vehicle *w = old_vehs[i];
00566           /* Is the vehicle again part of the new chain?
00567            * Note: We cannot test 'new_vehs[i] != NULL' as wagon removal might cause to remove both */
00568           if (w->First() == new_head) continue;
00569 
00570           if ((flags & DC_EXEC) != 0) TransferCargo(w, new_head, true);
00571 
00572           /* Sell the vehicle.
00573            * Note: This might temporarly construct new trains, so use DC_AUTOREPLACE to prevent
00574            *       it from failing due to engine limits. */
00575           cost.AddCost(DoCommand(0, w->index, 0, flags | DC_AUTOREPLACE, GetCmdSellVeh(w)));
00576           if ((flags & DC_EXEC) != 0) {
00577             old_vehs[i] = NULL;
00578             if (i == 0) old_head = NULL;
00579           }
00580         }
00581 
00582         if ((flags & DC_EXEC) != 0) CheckCargoCapacity(new_head);
00583       }
00584 
00585       /* If we are not in DC_EXEC undo everything, i.e. rearrange old vehicles.
00586        * We do this from back to front, so that the head of the temporary vehicle chain does not change all the time.
00587        * Note: The vehicle attach callback is disabled here :) */
00588       if ((flags & DC_EXEC) == 0) {
00589         /* Separate the head, so we can reattach the old vehicles */
00590         Train *second = Train::From(old_head)->GetNextUnit();
00591         if (second != NULL) CmdMoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true);
00592 
00593         assert(Train::From(old_head)->GetNextUnit() == NULL);
00594 
00595         for (int i = num_units - 1; i > 0; i--) {
00596           CommandCost ret = CmdMoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false);
00597           assert(ret.Succeeded());
00598         }
00599       }
00600     }
00601 
00602     /* Finally undo buying of new vehicles */
00603     if ((flags & DC_EXEC) == 0) {
00604       for (int i = num_units - 1; i >= 0; i--) {
00605         if (new_vehs[i] != NULL) {
00606           DoCommand(0, new_vehs[i]->index, 0, DC_EXEC, GetCmdSellVeh(new_vehs[i]));
00607           new_vehs[i] = NULL;
00608         }
00609       }
00610     }
00611 
00612     free(old_vehs);
00613     free(new_vehs);
00614     free(new_costs);
00615   } else {
00616     /* Build and refit replacement vehicle */
00617     Vehicle *new_head = NULL;
00618     cost.AddCost(BuildReplacementVehicle(old_head, &new_head, true));
00619 
00620     /* Was a new vehicle constructed? */
00621     if (cost.Succeeded() && new_head != NULL) {
00622       *nothing_to_do = false;
00623 
00624       /* The new vehicle is constructed, now take over orders and everything... */
00625       cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags));
00626 
00627       if (cost.Succeeded()) {
00628         /* The new vehicle is constructed, now take over cargo */
00629         if ((flags & DC_EXEC) != 0) {
00630           TransferCargo(old_head, new_head, true);
00631           *chain = new_head;
00632         }
00633 
00634         /* Sell the old vehicle */
00635         cost.AddCost(DoCommand(0, old_head->index, 0, flags, GetCmdSellVeh(old_head)));
00636       }
00637 
00638       /* If we are not in DC_EXEC undo everything */
00639       if ((flags & DC_EXEC) == 0) {
00640         DoCommand(0, new_head->index, 0, DC_EXEC, GetCmdSellVeh(new_head));
00641       }
00642     }
00643   }
00644 
00645   return cost;
00646 }
00647 
00658 CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00659 {
00660   Vehicle *v = Vehicle::GetIfValid(p1);
00661   if (v == NULL) return CMD_ERROR;
00662 
00663   CommandCost ret = CheckOwnership(v->owner);
00664   if (ret.Failed()) return ret;
00665 
00666   if (!v->IsInDepot()) return CMD_ERROR;
00667   if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
00668 
00669   bool free_wagon = false;
00670   if (v->type == VEH_TRAIN) {
00671     Train *t = Train::From(v);
00672     if (t->IsArticulatedPart() || t->IsRearDualheaded()) return CMD_ERROR;
00673     free_wagon = !t->IsFrontEngine();
00674     if (free_wagon && t->First()->IsFrontEngine()) return CMD_ERROR;
00675   } else {
00676     if (!v->IsPrimaryVehicle()) return CMD_ERROR;
00677   }
00678 
00679   const Company *c = Company::Get(_current_company);
00680   bool wagon_removal = c->settings.renew_keep_length;
00681 
00682   /* Test whether any replacement is set, before issuing a whole lot of commands that would end in nothing changed */
00683   Vehicle *w = v;
00684   bool any_replacements = false;
00685   while (w != NULL) {
00686     EngineID e;
00687     CommandCost cost = GetNewEngineType(w, c, e);
00688     if (cost.Failed()) return cost;
00689     any_replacements |= (e != INVALID_ENGINE);
00690     w = (!free_wagon && w->type == VEH_TRAIN ? Train::From(w)->GetNextUnit() : NULL);
00691   }
00692 
00693   CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
00694   bool nothing_to_do = true;
00695 
00696   if (any_replacements) {
00697     bool was_stopped = free_wagon || ((v->vehstatus & VS_STOPPED) != 0);
00698 
00699     /* Stop the vehicle */
00700     if (!was_stopped) cost.AddCost(CmdStartStopVehicle(v, true));
00701     if (cost.Failed()) return cost;
00702 
00703     assert(v->IsStoppedInDepot());
00704 
00705     /* We have to construct the new vehicle chain to test whether it is valid.
00706      * Vehicle construction needs random bits, so we have to save the random seeds
00707      * to prevent desyncs and to replay newgrf callbacks during DC_EXEC */
00708     SavedRandomSeeds saved_seeds;
00709     SaveRandomSeeds(&saved_seeds);
00710     if (free_wagon) {
00711       cost.AddCost(ReplaceFreeUnit(&v, flags & ~DC_EXEC, &nothing_to_do));
00712     } else {
00713       cost.AddCost(ReplaceChain(&v, flags & ~DC_EXEC, wagon_removal, &nothing_to_do));
00714     }
00715     RestoreRandomSeeds(saved_seeds);
00716 
00717     if (cost.Succeeded() && (flags & DC_EXEC) != 0) {
00718       CommandCost ret;
00719       if (free_wagon) {
00720         ret = ReplaceFreeUnit(&v, flags, &nothing_to_do);
00721       } else {
00722         ret = ReplaceChain(&v, flags, wagon_removal, &nothing_to_do);
00723       }
00724       assert(ret.Succeeded() && ret.GetCost() == cost.GetCost());
00725     }
00726 
00727     /* Restart the vehicle */
00728     if (!was_stopped) cost.AddCost(CmdStartStopVehicle(v, false));
00729   }
00730 
00731   if (cost.Succeeded() && nothing_to_do) cost = CommandCost(STR_ERROR_AUTOREPLACE_NOTHING_TO_DO);
00732   return cost;
00733 }
00734 
00747 CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00748 {
00749   Company *c = Company::GetIfValid(_current_company);
00750   if (c == NULL) return CMD_ERROR;
00751 
00752   EngineID old_engine_type = GB(p2, 0, 16);
00753   EngineID new_engine_type = GB(p2, 16, 16);
00754   GroupID id_g = GB(p1, 16, 16);
00755   CommandCost cost;
00756 
00757   if (Group::IsValidID(id_g) ? Group::Get(id_g)->owner != _current_company : !IsAllGroupID(id_g) && !IsDefaultGroupID(id_g)) return CMD_ERROR;
00758   if (!Engine::IsValidID(old_engine_type)) return CMD_ERROR;
00759 
00760   if (new_engine_type != INVALID_ENGINE) {
00761     if (!Engine::IsValidID(new_engine_type)) return CMD_ERROR;
00762     if (!CheckAutoreplaceValidity(old_engine_type, new_engine_type, _current_company)) return CMD_ERROR;
00763 
00764     cost = AddEngineReplacementForCompany(c, old_engine_type, new_engine_type, id_g, flags);
00765   } else {
00766     cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags);
00767   }
00768 
00769   if (flags & DC_EXEC) GroupStatistics::UpdateAutoreplace(_current_company);
00770   if ((flags & DC_EXEC) && IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
00771 
00772   return cost;
00773 }
00774