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" tron@3959: #include "roadveh.h" tron@3961: #include "ship.h" rubidium@8763: #include "news_func.h" rubidium@8254: #include "player_func.h" celestar@1601: #include "debug.h" matthijs@1752: #include "vehicle_gui.h" bjarni@2676: #include "train.h" bjarni@4662: #include "aircraft.h" peter1138@6113: #include "cargotype.h" rubidium@6643: #include "group.h" rubidium@8114: #include "strings_func.h" rubidium@8116: #include "command_func.h" rubidium@8144: #include "vehicle_func.h" rubidium@8131: #include "functions.h" rubidium@8211: #include "variables.h" rubidium@8212: #include "autoreplace_func.h" bjarni@8363: #include "articulated_vehicles.h" bjarni@2244: rubidium@8264: #include "table/strings.h" rubidium@8264: bjarni@8628: bjarni@2599: /* bjarni@2599: * move the cargo from one engine to another if possible bjarni@2599: */ bjarni@2599: static void MoveVehicleCargo(Vehicle *dest, Vehicle *source) bjarni@2599: { bjarni@2599: Vehicle *v = dest; bjarni@2599: bjarni@2599: do { bjarni@2599: do { bjarni@2599: if (source->cargo_type != dest->cargo_type) rubidium@4434: continue; // cargo not compatible bjarni@2599: rubidium@7010: if (dest->cargo.Count() == dest->cargo_cap) rubidium@4434: continue; // the destination vehicle is already full bjarni@2599: rubidium@7010: uint units_moved = min(source->cargo.Count(), dest->cargo_cap - dest->cargo.Count()); rubidium@7010: source->cargo.MoveTo(&dest->cargo, units_moved); bjarni@2599: bjarni@2599: // copy the age of the cargo bjarni@2599: dest->day_counter = source->day_counter; bjarni@2599: dest->tick_counter = source->tick_counter; bjarni@2599: rubidium@7492: } while (source->cargo.Count() > 0 && (dest = dest->Next()) != NULL); bjarni@2599: dest = v; rubidium@7492: } while ((source = source->Next()) != NULL); rubidium@5693: rubidium@5693: /* rubidium@5693: * The of the train will be incorrect at this moment. This is due rubidium@5693: * to the fact that removing the old wagon updates the weight of rubidium@5693: * the complete train, which is without the weight of cargo we just rubidium@5693: * moved back into some (of the) new wagon(s). rubidium@5693: */ smatz@9704: if (dest->type == VEH_TRAIN) TrainConsistChanged(dest->First(), true); bjarni@2599: } bjarni@2599: frosch@9725: 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@9725: if (cid != cargo_type && default_capacity[cid] > 0) return 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: frosch@9725: if (!VerifyAutoreplaceRefitForOrders(v, engine_type)) return CT_INVALID; // Some refit orders loose 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: /** Replaces a vehicle (used to be called autorenew) tron@2639: * This function is only called from MaybeReplaceVehicle() bjarni@2552: * Must be called with _current_player set to the owner of the vehicle bjarni@2552: * @param w Vehicle to replace bjarni@2552: * @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts bjarni@8997: * @param p The vehicle owner (faster than refinding the pointer) bjarni@8997: * @param new_engine_type The EngineID to replace to tron@2639: * @return value is cost of the replacement or CMD_ERROR bjarni@2552: */ smatz@9628: static CommandCost ReplaceVehicle(Vehicle **w, uint32 flags, Money total_cost, const Player *p, EngineID new_engine_type) bjarni@2552: { rubidium@6943: CommandCost cost; rubidium@6943: CommandCost sell_value; bjarni@2599: Vehicle *old_v = *w; bjarni@2552: const UnitID cached_unitnumber = old_v->unitnumber; bjarni@2552: bool new_front = false; bjarni@2552: Vehicle *new_v = NULL; peter1138@8258: char *vehicle_name = NULL; bjarni@4554: CargoID replacement_cargo_type; bjarni@2552: bjarni@4554: replacement_cargo_type = GetNewCargoTypeForReplace(old_v, new_engine_type); bjarni@4554: bjarni@4554: /* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo type */ rubidium@6950: if (replacement_cargo_type == CT_INVALID) return CommandCost(); bjarni@4554: bjarni@5792: sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, GetCmdSellVeh(old_v)); bjarni@4728: bjarni@4728: /* We give the player a loan of the same amount as the sell value. bjarni@4728: * This is needed in case he needs the income from the sale to build the new vehicle. bjarni@4728: * We take it back if building fails or when we really sell the old engine */ bjarni@4728: SubtractMoneyFromPlayer(sell_value); bjarni@4728: smatz@9628: cost = DoCommand(old_v->tile, new_engine_type, 0, flags | DC_AUTOREPLACE, GetCmdBuildVeh(old_v)); bjarni@4728: if (CmdFailed(cost)) { rubidium@6950: /* Take back the money we just gave the player */ rubidium@6950: sell_value.MultiplyCost(-1); rubidium@6950: SubtractMoneyFromPlayer(sell_value); bjarni@4728: return cost; bjarni@4728: } bjarni@2552: bjarni@6746: if (replacement_cargo_type != CT_NO_REFIT) { bjarni@6746: /* add refit cost */ rubidium@6943: CommandCost refit_cost = GetRefitCost(new_engine_type); bjarni@8363: if (old_v->type == VEH_TRAIN && RailVehInfo(new_engine_type)->railveh_type == RAILVEH_MULTIHEAD) { bjarni@8363: /* Since it's a dualheaded engine we have to pay once more because the rear end is being refitted too. */ bjarni@8363: refit_cost.AddCost(refit_cost); bjarni@8363: } rubidium@6950: cost.AddCost(refit_cost); bjarni@6746: } bjarni@4554: bjarni@2552: if (flags & DC_EXEC) { bjarni@2564: new_v = GetVehicle(_new_vehicle_id); rubidium@4434: *w = new_v; //we changed the vehicle, so MaybeReplaceVehicle needs to work on the new one. Now we tell it what the new one is bjarni@2552: bjarni@2552: /* refit if needed */ bjarni@4554: if (replacement_cargo_type != CT_NO_REFIT) { bjarni@5792: if (CmdFailed(DoCommand(0, new_v->index, replacement_cargo_type, DC_EXEC, GetCmdRefitVeh(new_v)))) { bjarni@4613: /* Being here shows a failure, which most likely is in GetNewCargoTypeForReplace() or incorrect estimation costs */ bjarni@4613: error("Autoreplace failed to refit. Replace engine %d to %d and refit to cargo %d", old_v->engine_type, new_v->engine_type, replacement_cargo_type); bjarni@4611: } bjarni@2552: } bjarni@4611: skidd13@7928: if (new_v->type == VEH_TRAIN && HasBit(old_v->u.rail.flags, VRF_REVERSE_DIRECTION) && !IsMultiheaded(new_v) && !(new_v->Next() != NULL && IsArticulatedPart(new_v->Next()))) { bjarni@3896: // we are autorenewing to a single engine, so we will turn it as the old one was turned as well skidd13@7931: SetBit(new_v->u.rail.flags, VRF_REVERSE_DIRECTION); bjarni@3896: } bjarni@2552: rubidium@6259: if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_v)) { bjarni@2552: /* this is a railcar. We need to move the car into the train bjarni@2552: * We add the new engine after the old one instead of replacing it. It will give the same result anyway when we bjarni@2552: * sell the old engine in a moment bjarni@2552: */ bjarni@6735: /* Get the vehicle in front of the one we move out */ rubidium@7497: Vehicle *front = old_v->Previous(); bjarni@8736: if (front == NULL) { bjarni@8736: /* It would appear that we have the front wagon of a row of wagons without engines */ bjarni@8736: Vehicle *next = old_v->Next(); bjarni@8736: if (next != NULL) { bjarni@8736: /* Move the chain to the new front wagon */ bjarni@8736: DoCommand(0, (new_v->index << 16) | next->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); bjarni@8736: } bjarni@8736: } else { bjarni@8736: /* If the vehicle in front is the rear end of a dualheaded engine, then we need to use the one in front of that one */ bjarni@8736: if (IsRearDualheaded(front)) front = front->Previous(); bjarni@8736: /* Now we move the old one out of the train */ bjarni@8736: DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); bjarni@8736: /* Add the new vehicle */ bjarni@9061: CommandCost tmp_move = DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); bjarni@9061: if (CmdFailed(tmp_move)) { bjarni@9061: cost.AddCost(tmp_move); bjarni@9061: DoCommand(0, new_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN)); bjarni@9061: } bjarni@8736: } bjarni@2552: } else { bjarni@2552: // copy/clone the orders belugas@8469: DoCommand(0, (old_v->index << 16) | new_v->index, old_v->IsOrderListShared() ? CO_SHARE : CO_COPY, DC_EXEC, CMD_CLONE_ORDER); bjarni@2552: new_v->cur_order_index = old_v->cur_order_index; rubidium@9280: ChangeVehicleViewWindow(old_v->index, new_v->index); bjarni@2575: new_v->profit_this_year = old_v->profit_this_year; bjarni@2575: new_v->profit_last_year = old_v->profit_last_year; bjarni@3679: new_v->service_interval = old_v->service_interval; rubidium@6647: DoCommand(0, old_v->group_id, new_v->index, flags, CMD_ADD_VEHICLE_GROUP); bjarni@2552: new_front = true; rubidium@4434: new_v->unitnumber = old_v->unitnumber; // use the same unit number bjarni@6551: new_v->dest_tile = old_v->dest_tile; bjarni@2552: bjarni@2552: new_v->current_order = old_v->current_order; rubidium@6259: if (old_v->type == VEH_TRAIN && GetNextVehicle(old_v) != NULL){ bjarni@2842: Vehicle *temp_v = GetNextVehicle(old_v); bjarni@2842: bjarni@2842: // move the entire train to the new engine, excluding the old engine bjarni@2842: if (IsMultiheaded(old_v) && temp_v == old_v->u.rail.other_multiheaded_part) { bjarni@2842: // we got front and rear of a multiheaded engine right after each other. We should work with the next in line instead bjarni@2842: temp_v = GetNextVehicle(temp_v); bjarni@2842: } bjarni@2842: bjarni@2842: if (temp_v != NULL) { bjarni@9061: CommandCost tmp_move = DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); bjarni@9061: if (CmdFailed(tmp_move)) { bjarni@9061: cost.AddCost(tmp_move); bjarni@9061: DoCommand(0, temp_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN)); bjarni@9061: } bjarni@2681: } bjarni@2552: } bjarni@2552: } bjarni@9140: if (CmdSucceeded(cost)) { bjarni@9140: /* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */ bjarni@9140: MoveVehicleCargo(new_v->type == VEH_TRAIN ? new_v->First() : new_v, old_v); peter1138@2716: bjarni@9140: /* Get the name of the old vehicle if it has a custom name. */ bjarni@9140: if (old_v->name != NULL) vehicle_name = strdup(old_v->name); bjarni@9140: } bjarni@4554: } else { // flags & DC_EXEC not set rubidium@6950: CommandCost tmp_move; bjarni@7524: bjarni@7524: if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v)) { bjarni@7527: Vehicle *next_veh = GetNextUnit(old_v); // don't try to move the rear multiheaded engine or articulated parts bjarni@7524: if (next_veh != NULL) { bjarni@7524: /* Verify that the wagons can be placed on the engine in question. bjarni@7524: * This is done by building an engine, test if the wagons can be added and then sell the test engine. */ smatz@9628: DoCommand(old_v->tile, new_engine_type, 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_v)); bjarni@7524: Vehicle *temp = GetVehicle(_new_vehicle_id); bjarni@7524: tmp_move = DoCommand(0, (temp->index << 16) | next_veh->index, 1, 0, CMD_MOVE_RAIL_VEHICLE); bjarni@7524: DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v)); bjarni@7524: } bjarni@6800: } bjarni@6800: bjarni@4554: /* Ensure that the player will not end up having negative money while autoreplacing bjarni@4554: * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */ rubidium@6952: if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) { rubidium@6950: /* Pay back the loan */ rubidium@6950: sell_value.MultiplyCost(-1); rubidium@6950: SubtractMoneyFromPlayer(sell_value); bjarni@4728: return CMD_ERROR; bjarni@4728: } bjarni@2552: } bjarni@2552: bjarni@4728: /* Take back the money we just gave the player just before building the vehicle bjarni@4728: * The player will get the same amount now that the sale actually takes place */ rubidium@6950: sell_value.MultiplyCost(-1); rubidium@6950: SubtractMoneyFromPlayer(sell_value); bjarni@4728: bjarni@4554: /* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */ rubidium@6950: cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v))); bjarni@2552: bjarni@9140: if (CmdFailed(cost)) return cost; bjarni@9140: bjarni@2552: if (new_front) { bjarni@4554: /* now we assign the old unitnumber to the new vehicle */ bjarni@2552: new_v->unitnumber = cached_unitnumber; bjarni@2552: } bjarni@2552: bjarni@4554: /* Transfer the name of the old vehicle */ peter1138@8258: if ((flags & DC_EXEC) && vehicle_name != NULL) { peter1138@2716: _cmd_text = vehicle_name; tron@3491: DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE); peter1138@8258: free(vehicle_name); peter1138@2716: } peter1138@2716: bjarni@2552: return cost; bjarni@2552: } bjarni@2552: bjarni@9002: /** Removes wagons from a train until it get a certain length bjarni@9002: * @param v The vehicle bjarni@9002: * @param old_total_length The wanted max length bjarni@9002: * @return The profit from selling the wagons bjarni@9002: */ bjarni@9002: static CommandCost WagonRemoval(Vehicle *v, uint16 old_total_length) bjarni@9002: { bjarni@9002: if (v->type != VEH_TRAIN) return CommandCost(); bjarni@9002: Vehicle *front = v; bjarni@9002: bjarni@9002: CommandCost cost = CommandCost(); bjarni@9002: bjarni@9002: while (front->u.rail.cached_total_length > old_total_length) { bjarni@9002: /* the train is too long. We will remove cars one by one from the start of the train until it's short enough */ bjarni@9002: while (v != NULL && RailVehInfo(v->engine_type)->railveh_type != RAILVEH_WAGON) { bjarni@9002: /* We move backwards in the train until we find a wagon */ bjarni@9002: v = GetNextVehicle(v); bjarni@9002: } bjarni@9002: bjarni@9002: if (v == NULL) { bjarni@9002: /* We sold all the wagons and the train is still not short enough */ bjarni@9002: SetDParam(0, front->unitnumber); rubidium@9234: AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NS_ADVICE, front->index, 0); bjarni@9002: return cost; bjarni@9002: } bjarni@9002: bjarni@9002: /* We found a wagon we can sell */ bjarni@9002: Vehicle *temp = v; bjarni@9002: v = GetNextVehicle(v); bjarni@9002: DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); // remove the wagon from the train bjarni@9002: MoveVehicleCargo(front, temp); // move the cargo back on the train bjarni@9002: cost.AddCost(DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON)); // sell the wagon bjarni@9002: } bjarni@9002: return cost; bjarni@9002: } bjarni@9002: 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: tron@2639: /** replaces a vehicle if it's set for autoreplace or is too old tron@2639: * (used to be called autorenew) tron@2639: * @param v The vehicle to replace rubidium@4434: * if the vehicle is a train, v needs to be the front engine bjarni@8996: * @param flags bjarni@8996: * @param display_costs If set, a cost animation is shown (only if DC_EXEC is set) bjarni@8996: * This bool also takes autorenew money into consideration bjarni@8996: * @return the costs, the success bool and sometimes an error message tron@2639: */ bjarni@8996: CommandCost MaybeReplaceVehicle(Vehicle *v, uint32 flags, bool display_costs) bjarni@2552: { bjarni@2552: Vehicle *w; bjarni@9061: Player *p = GetPlayer(v->owner); bjarni@8996: CommandCost cost; bjarni@8996: bool stopped = false; bjarni@9061: BackuppedVehicle backup(true); bjarni@8996: bjarni@8996: /* We only want "real" vehicle types. */ bjarni@8996: assert(IsPlayerBuildableVehicleType(v)); bjarni@8996: bjarni@8996: /* Ensure that this bool is cleared. */ bjarni@8996: assert(!v->leave_depot_instantly); bjarni@8996: bjarni@8996: /* We can't sell if the current player don't own the vehicle. */ bjarni@8996: assert(v->owner == _current_player); bjarni@8996: bjarni@8996: if (!v->IsInDepot()) { bjarni@8996: /* The vehicle should be inside the depot */ bjarni@8996: switch (v->type) { bjarni@8996: default: NOT_REACHED(); bjarni@8996: case VEH_TRAIN: return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED); break; bjarni@8996: case VEH_ROAD: return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE); break; bjarni@8996: case VEH_SHIP: return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); break; bjarni@8996: case VEH_AIRCRAFT: return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED); break; bjarni@8996: } bjarni@8996: } tron@2805: tron@2805: /* Remember the length in case we need to trim train later on bjarni@3175: * If it's not a train, the value is unused bjarni@3175: * round up to the length of the tiles used for the train instead of the train length instead bjarni@3175: * Useful when newGRF uses custom length */ rubidium@6259: uint16 old_total_length = (v->type == VEH_TRAIN ? celestar@3422: (v->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE : celestar@3422: -1 celestar@3422: ); bjarni@3175: bjarni@8996: if (!(v->vehstatus & VS_STOPPED)) { bjarni@8996: /* The vehicle is moving so we better stop it before we might alter consist or sell it */ bjarni@8996: v->vehstatus |= VS_STOPPED; bjarni@8996: /* Remember that we stopped the vehicle */ bjarni@8996: stopped = true; bjarni@8996: } bjarni@2590: bjarni@8996: { rubidium@8230: cost = CommandCost(EXPENSES_NEW_VEHICLES); bjarni@2552: w = v; bjarni@2552: do { bjarni@8997: EngineID new_engine = GetNewEngineType(w, p); bjarni@8997: if (new_engine == INVALID_ENGINE) continue; bjarni@2552: bjarni@9061: if (!backup.ContainsBackup()) { bjarni@9061: /* We are going to try to replace a vehicle but we don't have any backup so we will make one. */ bjarni@9061: backup.Backup(v, p); bjarni@9061: } bjarni@9141: /* Now replace the vehicle. bjarni@9141: * First we need to cache if it's the front vehicle as we need to update the v pointer if it is. bjarni@9141: * If the replacement fails then we can't trust the data in the vehicle hence the reason to cache the result. */ bjarni@9141: bool IsFront = w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE; bjarni@9141: bjarni@9061: cost.AddCost(ReplaceVehicle(&w, DC_EXEC, cost.GetCost(), p, new_engine)); bjarni@6800: bjarni@9141: if (IsFront) { tron@2639: /* now we bought a new engine and sold the old one. We need to fix the tron@2639: * pointers in order to avoid pointing to the old one for trains: these tron@2639: * pointers should point to the front engine and not the cars tron@2639: */ bjarni@2552: v = w; bjarni@2552: } bjarni@9140: } while (CmdSucceeded(cost) && w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL); bjarni@2552: bjarni@9140: if (CmdSucceeded(cost) && v->type == VEH_TRAIN && p->renew_keep_length) { bjarni@9061: /* Remove wagons until the wanted length is reached */ bjarni@9061: cost.AddCost(WagonRemoval(v, old_total_length)); bjarni@9061: } bjarni@9061: bjarni@8996: if (flags & DC_QUERY_COST || cost.GetCost() == 0) { bjarni@8996: /* We didn't do anything during the replace so we will just exit here */ bjarni@9081: v = backup.Restore(v, p); bjarni@8996: if (stopped) v->vehstatus &= ~VS_STOPPED; bjarni@8996: return cost; bjarni@8996: } bjarni@8996: bjarni@9061: if (display_costs) { bjarni@8996: /* We want to ensure that we will not get below p->engine_renew_money. bjarni@8996: * We will not actually pay this amount. It's for display and checks only. */ bjarni@9061: CommandCost tmp = cost; bjarni@9061: tmp.AddCost((Money)p->engine_renew_money); bjarni@9061: if (CmdSucceeded(tmp) && GetAvailableMoneyForCommand() < tmp.GetCost()) { bjarni@8996: /* We don't have enough money so we will set cost to failed */ bjarni@9061: cost.AddCost((Money)p->engine_renew_money); bjarni@8996: cost.AddCost(CMD_ERROR); bjarni@8996: } bjarni@8996: } bjarni@8996: bjarni@8996: if (display_costs && CmdFailed(cost)) { bjarni@8996: if (GetAvailableMoneyForCommand() < cost.GetCost() && IsLocalPlayer()) { bjarni@2552: StringID message; bjarni@2552: SetDParam(0, v->unitnumber); bjarni@2552: switch (v->type) { rubidium@6259: case VEH_TRAIN: message = STR_TRAIN_AUTORENEW_FAILED; break; rubidium@6259: case VEH_ROAD: message = STR_ROADVEHICLE_AUTORENEW_FAILED; break; rubidium@6259: case VEH_SHIP: message = STR_SHIP_AUTORENEW_FAILED; break; rubidium@6259: case VEH_AIRCRAFT: message = STR_AIRCRAFT_AUTORENEW_FAILED; break; bjarni@2552: // This should never happen bjarni@2552: default: NOT_REACHED(); message = 0; break; bjarni@2552: } bjarni@2552: rubidium@9234: AddNewsItem(message, NS_ADVICE, v->index, 0); bjarni@2552: } bjarni@2617: } bjarni@2617: } bjarni@2617: bjarni@9061: if (display_costs && IsLocalPlayer() && (flags & DC_EXEC) && CmdSucceeded(cost)) { bjarni@9061: ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost()); bjarni@9061: } bjarni@8996: bjarni@9061: if (!(flags & DC_EXEC) || CmdFailed(cost)) { bjarni@9081: v = backup.Restore(v, p); bjarni@8996: } bjarni@8996: bjarni@8996: /* Start the vehicle if we stopped it earlier */ tron@2639: if (stopped) v->vehstatus &= ~VS_STOPPED; bjarni@8996: bjarni@4662: return cost; bjarni@2552: }