tron@2186: /* $Id$ */ tron@2186: bjarni@8999: /** @file autoreplace_cmd.cpp Deals with autoreplace execution but not the setup */ bjarni@8999: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" rubidium@8254: #include "player_func.h" celestar@1601: #include "debug.h" matthijs@1752: #include "vehicle_gui.h" bjarni@2676: #include "train.h" frosch@9936: #include "rail.h" rubidium@8116: #include "command_func.h" frosch@9936: #include "engine_base.h" frosch@9936: #include "engine_func.h" rubidium@8144: #include "vehicle_func.h" rubidium@8131: #include "functions.h" rubidium@8212: #include "autoreplace_func.h" bjarni@8363: #include "articulated_vehicles.h" rubidium@10122: #include "core/alloc_func.hpp" bjarni@2244: rubidium@8264: #include "table/strings.h" rubidium@8264: frosch@9889: /** Figure out if two engines got at least one type of cargo in common (refitting if needed) frosch@9889: * @param engine_a one of the EngineIDs frosch@9889: * @param engine_b the other EngineID frosch@9889: * @param type the type of the engines frosch@9889: * @return true if they can both carry the same type of cargo (or at least one of them got no capacity at all) frosch@9889: */ frosch@9889: static bool EnginesGotCargoInCommon(EngineID engine_a, EngineID engine_b, VehicleType type) frosch@9889: { frosch@9889: uint32 available_cargos_a = GetUnionOfArticulatedRefitMasks(engine_a, type, true); frosch@9889: uint32 available_cargos_b = GetUnionOfArticulatedRefitMasks(engine_b, type, true); frosch@9889: return (available_cargos_a == 0 || available_cargos_b == 0 || (available_cargos_a & available_cargos_b) != 0); frosch@9889: } frosch@9889: frosch@9889: /** frosch@9889: * Checks some basic properties whether autoreplace is allowed frosch@9889: * @param from Origin engine frosch@9889: * @param to Destination engine frosch@9889: * @param player Player to check for frosch@9889: * @return true if autoreplace is allowed frosch@9889: */ frosch@9889: bool CheckAutoreplaceValidity(EngineID from, EngineID to, PlayerID player) frosch@9889: { frosch@9889: /* First we make sure that it's a valid type the user requested frosch@9889: * check that it's an engine that is in the engine array */ frosch@9889: if (!IsEngineIndex(from) || !IsEngineIndex(to)) return false; frosch@9889: frosch@9889: /* we can't replace an engine into itself (that would be autorenew) */ frosch@9889: if (from == to) return false; frosch@9889: frosch@9889: VehicleType type = GetEngine(from)->type; frosch@9889: frosch@9889: /* check that the new vehicle type is available to the player and its type is the same as the original one */ frosch@9889: if (!IsEngineBuildable(to, type, player)) return false; frosch@9889: frosch@9889: switch (type) { frosch@9889: case VEH_TRAIN: { frosch@9889: const RailVehicleInfo *rvi_from = RailVehInfo(from); frosch@9889: const RailVehicleInfo *rvi_to = RailVehInfo(to); frosch@9889: frosch@9889: /* make sure the railtypes are compatible */ frosch@9889: if ((GetRailTypeInfo(rvi_from->railtype)->compatible_railtypes & GetRailTypeInfo(rvi_to->railtype)->compatible_railtypes) == 0) return false; frosch@9889: frosch@9889: /* make sure we do not replace wagons with engines or vise versa */ frosch@9889: if ((rvi_from->railveh_type == RAILVEH_WAGON) != (rvi_to->railveh_type == RAILVEH_WAGON)) return false; frosch@9889: break; frosch@9889: } frosch@9889: frosch@9889: case VEH_ROAD: frosch@9889: /* make sure that we do not replace a tram with a normal road vehicles or vise versa */ frosch@9889: if (HasBit(EngInfo(from)->misc_flags, EF_ROAD_TRAM) != HasBit(EngInfo(to)->misc_flags, EF_ROAD_TRAM)) return false; frosch@9889: break; frosch@9889: frosch@9889: case VEH_AIRCRAFT: frosch@9889: /* make sure that we do not replace a plane with a helicopter or vise versa */ frosch@9889: if ((AircraftVehInfo(from)->subtype & AIR_CTOL) != (AircraftVehInfo(to)->subtype & AIR_CTOL)) return false; frosch@9889: break; frosch@9889: frosch@9889: default: break; frosch@9889: } frosch@9889: frosch@9889: /* the engines needs to be able to carry the same cargo */ frosch@9889: return EnginesGotCargoInCommon(from, to, type); frosch@9889: } bjarni@8628: frosch@9928: /** Transfer cargo from a single (articulated )old vehicle to the new vehicle chain frosch@9928: * @param old_veh Old vehicle that will be sold frosch@9928: * @param new_head Head of the completely constructed new vehicle chain frosch@9928: */ frosch@9928: static void TransferCargo(Vehicle *old_veh, Vehicle *new_head) frosch@9928: { frosch@9928: /* Loop through source parts */ frosch@9928: for (Vehicle *src = old_veh; src != NULL; src = src->Next()) { frosch@9928: if (src->cargo_type >= NUM_CARGO || src->cargo.Count() == 0) continue; frosch@9928: frosch@9928: /* Find free space in the new chain */ frosch@9928: for (Vehicle *dest = new_head; dest != NULL && src->cargo.Count() > 0; dest = dest->Next()) { frosch@9928: if (dest->cargo_type != src->cargo_type) continue; frosch@9928: frosch@9928: uint amount = min(src->cargo.Count(), dest->cargo_cap - dest->cargo.Count()); frosch@9928: if (amount <= 0) continue; frosch@9928: frosch@9928: src->cargo.MoveTo(&dest->cargo, amount); frosch@9928: } frosch@9928: } frosch@9928: frosch@9928: /* Update train weight etc., the old vehicle will be sold anyway */ frosch@9928: if (new_head->type == VEH_TRAIN) TrainConsistChanged(new_head, true); frosch@9928: } frosch@9928: frosch@9725: /** frosch@9725: * Tests whether refit orders that applied to v will also apply to the new vehicle type frosch@9725: * @param v The vehicle to be replaced frosch@9725: * @param engine_type The type we want to replace with frosch@9725: * @return true iff all refit orders stay valid frosch@9725: */ frosch@9725: static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, EngineID engine_type) bjarni@4741: { bjarni@4741: const Order *o; bjarni@4741: const Vehicle *u; bjarni@4741: frosch@9725: uint32 union_refit_mask_a = GetUnionOfArticulatedRefitMasks(v->engine_type, v->type, false); frosch@9725: uint32 union_refit_mask_b = GetUnionOfArticulatedRefitMasks(engine_type, v->type, false); frosch@9725: rubidium@6259: if (v->type == VEH_TRAIN) { rubidium@7497: u = v->First(); bjarni@4741: } else { bjarni@4741: u = v; bjarni@4741: } bjarni@4741: bjarni@4741: FOR_VEHICLE_ORDERS(u, o) { rubidium@8838: if (!o->IsRefit()) continue; frosch@9725: CargoID cargo_type = o->GetRefitCargo(); frosch@9725: frosch@9725: if (!HasBit(union_refit_mask_a, cargo_type)) continue; frosch@9725: if (!HasBit(union_refit_mask_b, cargo_type)) return false; bjarni@4741: } bjarni@4741: bjarni@4741: return true; bjarni@4741: } bjarni@4741: bjarni@4554: /** bjarni@4554: * Function to find what type of cargo to refit to when autoreplacing bjarni@4554: * @param *v Original vehicle, that is being replaced bjarni@4554: * @param engine_type The EngineID of the vehicle that is being replaced to bjarni@4554: * @return The cargo type to replace to bjarni@4554: * CT_NO_REFIT is returned if no refit is needed bjarni@4554: * CT_INVALID is returned when both old and new vehicle got cargo capacity and refitting the new one to the old one's cargo type isn't possible bjarni@4554: */ bjarni@4554: static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type) bjarni@4554: { frosch@9725: CargoID cargo_type; bjarni@4554: frosch@9725: if (GetUnionOfArticulatedRefitMasks(engine_type, v->type, true) == 0) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity bjarni@4554: frosch@9725: if (IsArticulatedVehicleCarryingDifferentCargos(v, &cargo_type)) return CT_INVALID; // We cannot refit to mixed cargos in an automated way frosch@9725: frosch@9725: uint32 available_cargo_types = GetIntersectionOfArticulatedRefitMasks(engine_type, v->type, true); frosch@9725: frosch@9725: if (cargo_type == CT_INVALID) { frosch@9725: if (v->type != VEH_TRAIN) return CT_NO_REFIT; // If the vehicle does not carry anything at all, every replacement is fine. frosch@9725: frosch@9725: /* the old engine didn't have cargo capacity, but the new one does frosch@9725: * now we will figure out what cargo the train is carrying and refit to fit this */ frosch@9725: frosch@9725: for (v = v->First(); v != NULL; v = v->Next()) { frosch@9725: if (v->cargo_cap == 0) continue; frosch@9725: /* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */ frosch@9725: if (HasBit(available_cargo_types, v->cargo_type)) { frosch@9725: /* Do we have to refit the vehicle, or is it already carrying the right cargo? */ frosch@9725: uint16 *default_capacity = GetCapacityOfArticulatedParts(engine_type, v->type); frosch@9725: for (CargoID cid = 0; cid < NUM_CARGO; cid++) { frosch@9883: if (cid != v->cargo_type && default_capacity[cid] > 0) return v->cargo_type; frosch@9725: } frosch@9725: frosch@9725: return CT_NO_REFIT; frosch@9725: } frosch@9725: } frosch@9725: frosch@9725: return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one frosch@9725: } else { frosch@9725: if (!HasBit(available_cargo_types, cargo_type)) return CT_INVALID; // We can't refit the vehicle to carry the cargo we want frosch@9725: smatz@10015: if (!VerifyAutoreplaceRefitForOrders(v, engine_type)) return CT_INVALID; // Some refit orders lose their effect frosch@9725: frosch@9725: /* Do we have to refit the vehicle, or is it already carrying the right cargo? */ frosch@9725: uint16 *default_capacity = GetCapacityOfArticulatedParts(engine_type, v->type); frosch@9725: for (CargoID cid = 0; cid < NUM_CARGO; cid++) { frosch@9725: if (cid != cargo_type && default_capacity[cid] > 0) return cargo_type; frosch@9725: } frosch@9725: frosch@9725: return CT_NO_REFIT; frosch@9725: } bjarni@4554: } bjarni@4554: bjarni@8997: /** Get the EngineID of the replacement for a vehicle bjarni@8997: * @param v The vehicle to find a replacement for bjarni@8997: * @param p The vehicle's owner (it's faster to forward the pointer than refinding it) bjarni@8997: * @return the EngineID of the replacement. INVALID_ENGINE if no buildable replacement is found bjarni@8997: */ bjarni@8997: static EngineID GetNewEngineType(const Vehicle *v, const Player *p) bjarni@8997: { frosch@9725: assert(v->type != VEH_TRAIN || !IsArticulatedPart(v)); frosch@9725: bjarni@8997: if (v->type == VEH_TRAIN && IsRearDualheaded(v)) { bjarni@8997: /* we build the rear ends of multiheaded trains with the front ones */ bjarni@8997: return INVALID_ENGINE; bjarni@8997: } bjarni@8997: bjarni@8997: EngineID e = EngineReplacementForPlayer(p, v->engine_type, v->group_id); bjarni@8997: bjarni@8997: if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_player)) { bjarni@8997: return e; bjarni@8997: } bjarni@8997: bjarni@8997: if (v->NeedsAutorenewing(p) && // replace if engine is too old bjarni@8997: IsEngineBuildable(v->engine_type, v->type, _current_player)) { // engine can still be build bjarni@8997: return v->engine_type; bjarni@8997: } bjarni@8997: bjarni@8997: return INVALID_ENGINE; bjarni@8997: } bjarni@8997: frosch@9928: /** Builds and refits a replacement vehicle frosch@9928: * Important: The old vehicle is still in the original vehicle chain (used for determining the cargo when the old vehicle did not carry anything, but the new one does) frosch@9928: * @param old_veh A single (articulated/multiheaded) vehicle that shall be replaced. frosch@9928: * @param new_vehicle Returns the newly build and refittet vehicle frosch@9928: * @return cost or error frosch@9928: */ frosch@9928: static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehicle) frosch@9928: { frosch@9928: *new_vehicle = NULL; frosch@9928: frosch@9928: /* Shall the vehicle be replaced? */ frosch@9928: const Player *p = GetPlayer(_current_player); frosch@9928: EngineID e = GetNewEngineType(old_veh, p); frosch@9928: if (e == INVALID_ENGINE) return CommandCost(); // neither autoreplace is set, nor autorenew is triggered frosch@9928: frosch@9928: /* Does it need to be refitted */ frosch@9928: CargoID refit_cargo = GetNewCargoTypeForReplace(old_veh, e); frosch@9928: if (refit_cargo == CT_INVALID) return CommandCost(); // incompatible cargos frosch@9928: frosch@9928: /* Build the new vehicle */ frosch@9928: CommandCost cost = DoCommand(old_veh->tile, e, 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_veh)); frosch@9928: if (cost.Failed()) return cost; frosch@9928: frosch@9928: Vehicle *new_veh = GetVehicle(_new_vehicle_id); frosch@9928: *new_vehicle = new_veh; frosch@9928: frosch@9928: /* Refit the vehicle if needed */ frosch@9928: if (refit_cargo != CT_NO_REFIT) { frosch@9928: cost.AddCost(DoCommand(0, new_veh->index, refit_cargo, DC_EXEC, GetCmdRefitVeh(new_veh))); frosch@9928: assert(cost.Succeeded()); // This should be ensured by GetNewCargoTypeForReplace() frosch@9928: } frosch@9928: frosch@9928: /* Try to reverse the vehicle, but do not care if it fails as the new type might not be reversible */ frosch@9928: if (new_veh->type == VEH_TRAIN && HasBit(old_veh->u.rail.flags, VRF_REVERSE_DIRECTION)) { frosch@9928: DoCommand(0, new_veh->index, true, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION); frosch@9928: } frosch@9928: frosch@9928: return cost; frosch@9928: } frosch@9928: frosch@9928: /** Issue a start/stop command frosch@9928: * @param v a vehicle frosch@9928: * @param evaluate_callback shall the start/stop callback be evaluated? frosch@9928: * @return success or error frosch@9928: */ frosch@9928: static inline CommandCost StartStopVehicle(const Vehicle *v, bool evaluate_callback) frosch@9928: { frosch@9928: return DoCommand(0, v->index, evaluate_callback ? 1 : 0, DC_EXEC | DC_AUTOREPLACE, CMD_START_STOP_VEHICLE); frosch@9928: } frosch@9928: frosch@9928: /** Issue a train vehicle move command frosch@9928: * @param v The vehicle to move frosch@9928: * @param after The vehicle to insert 'v' after, or NULL to start new chain frosch@9928: * @param whole_chain move all vehicles following 'v' (true), or only 'v' (false) frosch@9928: * @return success or error frosch@9928: */ frosch@9928: static inline CommandCost MoveVehicle(const Vehicle *v, const Vehicle *after, uint32 flags, bool whole_chain) frosch@9928: { frosch@9928: return DoCommand(0, v->index | (after != NULL ? after->index : INVALID_VEHICLE) << 16, whole_chain ? 1 : 0, flags, CMD_MOVE_RAIL_VEHICLE); frosch@9928: } frosch@9928: frosch@9928: /** Copy head specific things to the new vehicle chain after it was successfully constructed frosch@9928: * @param old_head The old front vehicle (no wagons attached anymore) frosch@9928: * @param new_head The new head of the completely replaced vehicle chain frosch@9928: * @param flags the command flags to use frosch@9928: */ frosch@9928: static CommandCost CopyHeadSpecificThings(Vehicle *old_head, Vehicle *new_head, uint32 flags) frosch@9928: { frosch@9928: CommandCost cost = CommandCost(); frosch@9928: frosch@9928: /* Share orders */ frosch@9928: 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)); frosch@9928: frosch@9928: /* Copy group membership */ frosch@9928: if (cost.Succeeded() && old_head != new_head) cost.AddCost(DoCommand(0, old_head->group_id, new_head->index, DC_EXEC, CMD_ADD_VEHICLE_GROUP)); frosch@9928: frosch@9928: /* Perform start/stop check whether the new vehicle suits newgrf restrictions etc. */ frosch@9928: if (cost.Succeeded()) { frosch@9928: /* Start the vehicle, might be denied by certain things */ frosch@9928: assert((new_head->vehstatus & VS_STOPPED) != 0); frosch@9928: cost.AddCost(StartStopVehicle(new_head, true)); frosch@9928: frosch@9928: /* Stop the vehicle again, but do not care about evil newgrfs allowing starting but not stopping :p */ frosch@9928: if (cost.Succeeded()) cost.AddCost(StartStopVehicle(new_head, false)); frosch@9928: } frosch@9928: frosch@9928: /* Last do those things which do never fail (resp. we do not care about), but which are not undo-able */ frosch@9928: if (cost.Succeeded() && old_head != new_head && (flags & DC_EXEC) != 0) { frosch@9928: /* Copy vehicle name */ frosch@9928: if (old_head->name != NULL) { frosch@9928: _cmd_text = old_head->name; smatz@10151: DoCommand(0, new_head->index, 0, DC_EXEC | DC_AUTOREPLACE, CMD_RENAME_VEHICLE); frosch@9928: _cmd_text = NULL; frosch@9928: } frosch@9928: frosch@9928: /* Copy other things which cannot be copied by a command and which shall not stay resetted from the build vehicle command */ frosch@9928: new_head->CopyVehicleConfigAndStatistics(old_head); frosch@9928: frosch@9928: /* Switch vehicle windows to the new vehicle, so they are not closed when the old vehicle is sold */ frosch@9928: ChangeVehicleViewWindow(old_head->index, new_head->index); frosch@9928: } frosch@9928: frosch@9928: return cost; frosch@9928: } frosch@9928: frosch@9928: /** Replace a whole vehicle chain frosch@9928: * @param chain vehicle chain to let autoreplace/renew operator on frosch@9928: * @param flags command flags frosch@9928: * @param wagon_removal remove wagons when the resulting chain occupies more tiles than the old did frosch@9928: * @param nothing_to_do is set to 'false' when something was done (only valid when not failed) frosch@9928: * @return cost or error frosch@9928: */ frosch@9928: static CommandCost ReplaceChain(Vehicle **chain, uint32 flags, bool wagon_removal, bool *nothing_to_do) frosch@9928: { frosch@9928: Vehicle *old_head = *chain; frosch@9928: frosch@9928: CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0); frosch@9928: frosch@9928: if (old_head->type == VEH_TRAIN) { frosch@9928: /* Store the length of the old vehicle chain, rounded up to whole tiles */ frosch@9928: uint16 old_total_length = (old_head->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE; frosch@9928: frosch@9928: int num_units = 0; ///< Number of units in the chain frosch@9928: for (Vehicle *w = old_head; w != NULL; w = GetNextUnit(w)) num_units++; frosch@9928: frosch@9928: Vehicle **old_vehs = CallocT(num_units); ///< Will store vehicles of the old chain in their order frosch@9928: Vehicle **new_vehs = CallocT(num_units); ///< New vehicles corresponding to old_vehs or NULL if no replacement frosch@9928: Money *new_costs = MallocT(num_units); ///< Costs for buying and refitting the new vehicles frosch@9928: frosch@9928: /* Collect vehicles and build replacements frosch@9928: * Note: The replacement vehicles can only successfully build as long as the old vehicles are still in their chain */ frosch@9928: int i; frosch@9928: Vehicle *w; frosch@9928: for (w = old_head, i = 0; w != NULL; w = GetNextUnit(w), i++) { frosch@9928: assert(i < num_units); frosch@9928: old_vehs[i] = w; frosch@9928: frosch@9928: CommandCost ret = BuildReplacementVehicle(old_vehs[i], &new_vehs[i]); frosch@9928: cost.AddCost(ret); frosch@9928: if (cost.Failed()) break; frosch@9928: frosch@9928: new_costs[i] = ret.GetCost(); frosch@9928: if (new_vehs[i] != NULL) *nothing_to_do = false; frosch@9928: } frosch@9928: Vehicle *new_head = (new_vehs[0] != NULL ? new_vehs[0] : old_vehs[0]); frosch@9928: frosch@9968: /* Note: When autoreplace has already failed here, old_vehs[] is not completely initialized. But it is also not needed. */ frosch@9928: if (cost.Succeeded()) { frosch@9968: /* Separate the head, so we can start constructing the new chain */ frosch@9928: Vehicle *second = GetNextUnit(old_head); frosch@9928: if (second != NULL) cost.AddCost(MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true)); frosch@9928: frosch@9928: assert(GetNextUnit(new_head) == NULL); frosch@9928: frosch@9969: /* Append engines to the new chain frosch@9969: * We do this from back to front, so that the head of the temporary vehicle chain does not change all the time. frosch@9969: * OTOH the vehicle attach callback is more expensive this way :s */ frosch@9969: Vehicle *last_engine = NULL; ///< Shall store the last engine unit after this step frosch@9969: if (cost.Succeeded()) { frosch@9969: for (int i = num_units - 1; i > 0; i--) { frosch@9969: Vehicle *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]); frosch@9928: frosch@9969: if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) continue; frosch@9928: frosch@9969: if (last_engine == NULL) last_engine = append; frosch@9969: cost.AddCost(MoveVehicle(append, new_head, DC_EXEC, false)); frosch@9969: if (cost.Failed()) break; frosch@9969: } frosch@9969: if (last_engine == NULL) last_engine = new_head; frosch@9969: } frosch@9928: frosch@9969: /* When wagon removal is enabled and the new engines without any wagons are already longer than the old, we have to fail */ frosch@9969: if (cost.Succeeded() && wagon_removal && new_head->u.rail.cached_total_length > old_total_length) cost = CommandCost(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT); frosch@9928: frosch@9969: /* Append/insert wagons into the new vehicle chain frosch@9969: * 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. frosch@9969: */ frosch@9969: if (cost.Succeeded()) { frosch@9969: for (int i = num_units - 1; i > 0; i--) { frosch@9969: assert(last_engine != NULL); frosch@9969: Vehicle *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]); frosch@9969: frosch@9969: if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) { frosch@9969: /* Insert wagon after 'last_engine' */ frosch@9969: CommandCost res = MoveVehicle(append, last_engine, DC_EXEC, false); frosch@9969: frosch@9969: if (res.Succeeded() && wagon_removal && new_head->u.rail.cached_total_length > old_total_length) { frosch@9969: MoveVehicle(append, NULL, DC_EXEC | DC_AUTOREPLACE, false); frosch@9969: break; frosch@9969: } frosch@9969: frosch@9969: cost.AddCost(res); frosch@9969: if (cost.Failed()) break; frosch@9969: } else { frosch@9969: /* We have reached 'last_engine', continue with the next engine towards the front */ frosch@9969: assert(append == last_engine); frosch@9969: last_engine = GetPrevUnit(last_engine); frosch@9969: } frosch@9928: } frosch@9928: } frosch@9928: frosch@9969: /* Sell superfluous new vehicles that could not be inserted. */ frosch@9969: if (cost.Succeeded() && wagon_removal) { frosch@9969: for (int i = 1; i < num_units; i++) { frosch@9969: Vehicle *wagon = new_vehs[i]; frosch@9969: if (wagon == NULL) continue; frosch@9969: if (wagon->First() == new_head) break; frosch@9928: frosch@9969: assert(RailVehInfo(wagon->engine_type)->railveh_type == RAILVEH_WAGON); frosch@9928: frosch@9969: /* Sell wagon */ frosch@9969: CommandCost ret = DoCommand(0, wagon->index, 0, DC_EXEC, GetCmdSellVeh(wagon)); frosch@9969: assert(ret.Succeeded()); frosch@9969: new_vehs[i] = NULL; frosch@9928: frosch@9969: /* Revert the money subtraction when the vehicle was built. frosch@9969: * This value is different from the sell value, esp. because of refitting */ frosch@9969: cost.AddCost(-new_costs[i]); frosch@9969: } frosch@9928: } frosch@9928: frosch@9969: /* The new vehicle chain is constructed, now take over orders and everything... */ frosch@9969: if (cost.Succeeded()) cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags)); frosch@9928: frosch@9969: if (cost.Succeeded()) { frosch@9969: /* Success ! */ frosch@9969: if ((flags & DC_EXEC) != 0 && new_head != old_head) { frosch@9969: *chain = new_head; frosch@9969: } frosch@9928: frosch@9969: /* Transfer cargo of old vehicles and sell them*/ frosch@9969: for (int i = 0; i < num_units; i++) { frosch@9969: Vehicle *w = old_vehs[i]; frosch@9969: /* Is the vehicle again part of the new chain? frosch@9969: * Note: We cannot test 'new_vehs[i] != NULL' as wagon removal might cause to remove both */ frosch@9969: if (w->First() == new_head) continue; frosch@9969: frosch@9969: if ((flags & DC_EXEC) != 0) TransferCargo(w, new_head); frosch@9969: frosch@9969: cost.AddCost(DoCommand(0, w->index, 0, flags, GetCmdSellVeh(w))); frosch@9969: if ((flags & DC_EXEC) != 0) { frosch@9969: old_vehs[i] = NULL; frosch@9969: if (i == 0) old_head = NULL; frosch@9969: } frosch@9928: } frosch@9928: } frosch@9928: frosch@9969: /* If we are not in DC_EXEC undo everything, i.e. rearrange old vehicles. frosch@9969: * We do this from back to front, so that the head of the temporary vehicle chain does not change all the time. frosch@9969: * Note: The vehicle attach callback is disabled here :) */ frosch@9969: if ((flags & DC_EXEC) == 0) { frosch@9969: /* Separate the head, so we can reattach the old vehicles */ frosch@9969: Vehicle *second = GetNextUnit(old_head); frosch@9969: if (second != NULL) MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true); frosch@9928: frosch@9969: assert(GetNextUnit(old_head) == NULL); frosch@9928: frosch@9969: for (int i = num_units - 1; i > 0; i--) { frosch@9969: CommandCost ret = MoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false); frosch@9969: assert(ret.Succeeded()); frosch@9969: } frosch@9968: } frosch@9968: } frosch@9928: frosch@9968: /* Finally undo buying of new vehicles */ frosch@9968: if ((flags & DC_EXEC) == 0) { frosch@9928: for (int i = num_units - 1; i >= 0; i--) { frosch@9928: if (new_vehs[i] != NULL) { frosch@9928: DoCommand(0, new_vehs[i]->index, 0, DC_EXEC, GetCmdSellVeh(new_vehs[i])); frosch@9928: new_vehs[i] = NULL; frosch@9928: } frosch@9928: } frosch@9928: } frosch@9928: frosch@9928: free(old_vehs); frosch@9928: free(new_vehs); frosch@9928: free(new_costs); frosch@9928: } else { frosch@9928: /* Build and refit replacement vehicle */ frosch@9928: Vehicle *new_head = NULL; frosch@9928: cost.AddCost(BuildReplacementVehicle(old_head, &new_head)); frosch@9928: frosch@9928: /* Was a new vehicle constructed? */ frosch@9928: if (cost.Succeeded() && new_head != NULL) { frosch@9928: *nothing_to_do = false; frosch@9928: frosch@9928: /* The new vehicle is constructed, now take over orders and everything... */ frosch@9928: cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags)); frosch@9928: frosch@9928: if (cost.Succeeded()) { frosch@9928: /* The new vehicle is constructed, now take over cargo */ frosch@9928: if ((flags & DC_EXEC) != 0) { frosch@9928: TransferCargo(old_head, new_head); frosch@9928: *chain = new_head; frosch@9928: } frosch@9928: frosch@9928: /* Sell the old vehicle */ frosch@9928: cost.AddCost(DoCommand(0, old_head->index, 0, flags, GetCmdSellVeh(old_head))); frosch@9928: } frosch@9928: frosch@9928: /* If we are not in DC_EXEC undo everything */ frosch@9928: if ((flags & DC_EXEC) == 0) { frosch@9928: DoCommand(0, new_head->index, 0, DC_EXEC, GetCmdSellVeh(new_head)); frosch@9928: } frosch@9928: } frosch@9928: } frosch@9928: frosch@9928: return cost; frosch@9928: } frosch@9928: frosch@9928: /** Autoreplace a vehicles frosch@9928: * @param tile not used frosch@9928: * @param flags type of operation frosch@9928: * @param p1 Index of vehicle frosch@9928: * @param p2 not used frosch@9928: */ frosch@9928: CommandCost CmdAutoreplaceVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) frosch@9928: { frosch@9928: CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0); frosch@9928: bool nothing_to_do = true; frosch@9928: frosch@9928: if (!IsValidVehicleID(p1)) return CMD_ERROR; frosch@9928: Vehicle *v = GetVehicle(p1); frosch@9928: if (!CheckOwnership(v->owner)) return CMD_ERROR; frosch@9928: if (!v->IsInDepot()) return CMD_ERROR; frosch@9928: if (HASBITS(v->vehstatus, VS_CRASHED)) return CMD_ERROR; frosch@9928: frosch@9928: const Player *p = GetPlayer(_current_player); frosch@9928: bool wagon_removal = p->renew_keep_length; frosch@9928: frosch@9928: /* Test whether any replacement is set, before issuing a whole lot of commands that would end in nothing changed */ frosch@9928: Vehicle *w = v; frosch@9928: bool any_replacements = false; frosch@9928: while (w != NULL && !any_replacements) { frosch@9928: any_replacements = (GetNewEngineType(w, p) != INVALID_ENGINE); frosch@9928: w = (w->type == VEH_TRAIN ? GetNextUnit(w) : NULL); frosch@9928: } frosch@9928: frosch@9928: if (any_replacements) { frosch@9928: bool was_stopped = (v->vehstatus & VS_STOPPED) != 0; frosch@9928: frosch@9928: /* Stop the vehicle */ frosch@9928: if (!was_stopped) cost.AddCost(StartStopVehicle(v, true)); frosch@9928: if (cost.Failed()) return cost; frosch@9928: frosch@9928: assert(v->IsStoppedInDepot()); frosch@9928: frosch@9928: /* We have to construct the new vehicle chain to test whether it is valid. frosch@9928: * Vehicle construction needs random bits, so we have to save the random seeds frosch@9928: * to prevent desyncs and to replay newgrf callbacks during DC_EXEC */ frosch@9928: SavedRandomSeeds saved_seeds; frosch@9928: SaveRandomSeeds(&saved_seeds); frosch@9928: cost.AddCost(ReplaceChain(&v, flags & ~DC_EXEC, wagon_removal, ¬hing_to_do)); frosch@9928: RestoreRandomSeeds(saved_seeds); frosch@9928: frosch@9928: if (cost.Succeeded() && (flags & DC_EXEC) != 0) { frosch@9928: CommandCost ret = ReplaceChain(&v, flags, wagon_removal, ¬hing_to_do); frosch@9928: assert(ret.Succeeded() && ret.GetCost() == cost.GetCost()); frosch@9928: } frosch@9928: frosch@9928: /* Restart the vehicle */ frosch@9928: if (!was_stopped) cost.AddCost(StartStopVehicle(v, false)); frosch@9928: } frosch@9928: frosch@9928: if (cost.Succeeded() && nothing_to_do) cost = CommandCost(STR_AUTOREPLACE_NOTHING_TO_DO); frosch@9928: return cost; frosch@9928: }