tron@2186: /* $Id$ */ tron@2186: bjarni@10262: /** @file autoreplace_cmd.cpp Deals with autoreplace execution but not the setup */ bjarni@10262: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@3959: #include "roadveh.h" tron@3961: #include "ship.h" rubidium@9259: #include "news_func.h" rubidium@8750: #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@6439: #include "cargotype.h" rubidium@7139: #include "group.h" rubidium@8610: #include "strings_func.h" rubidium@8612: #include "command_func.h" rubidium@8640: #include "vehicle_func.h" rubidium@8627: #include "functions.h" rubidium@8707: #include "variables.h" rubidium@8708: #include "autoreplace_func.h" bjarni@8859: #include "articulated_vehicles.h" bjarni@2244: rubidium@8760: #include "table/strings.h" rubidium@8760: bjarni@9124: 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@7506: if (dest->cargo.Count() == dest->cargo_cap) rubidium@4434: continue; // the destination vehicle is already full bjarni@2599: rubidium@7506: uint units_moved = min(source->cargo.Count(), dest->cargo_cap - dest->cargo.Count()); rubidium@7506: 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@7988: } while (source->cargo.Count() > 0 && (dest = dest->Next()) != NULL); bjarni@2599: dest = v; rubidium@7988: } while ((source = source->Next()) != NULL); rubidium@5944: rubidium@5944: /* rubidium@5944: * The of the train will be incorrect at this moment. This is due rubidium@5944: * to the fact that removing the old wagon updates the weight of rubidium@5944: * the complete train, which is without the weight of cargo we just rubidium@5944: * moved back into some (of the) new wagon(s). rubidium@5944: */ rubidium@7993: if (dest->type == VEH_TRAIN) TrainConsistChanged(dest->First()); bjarni@2599: } bjarni@2599: bjarni@4741: static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, const EngineID engine_type) bjarni@4741: { bjarni@4741: const Order *o; bjarni@4741: const Vehicle *u; bjarni@4741: rubidium@6585: if (v->type == VEH_TRAIN) { rubidium@7993: u = v->First(); bjarni@4741: } else { bjarni@4741: u = v; bjarni@4741: } bjarni@4741: bjarni@4741: FOR_VEHICLE_ORDERS(u, o) { rubidium@9334: if (!o->IsRefit()) continue; rubidium@9334: if (!CanRefitTo(v->engine_type, o->GetRefitCargo())) continue; rubidium@9334: if (!CanRefitTo(engine_type, o->GetRefitCargo())) 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: { bjarni@7001: CargoID new_cargo_type = GetEngineCargoType(engine_type); bjarni@4554: bjarni@7001: if (new_cargo_type == CT_INVALID) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity bjarni@4554: bjarni@8858: if (v->cargo_cap != 0 && (v->cargo_type == new_cargo_type || CanRefitTo(engine_type, v->cargo_type))) { bjarni@4741: if (VerifyAutoreplaceRefitForOrders(v, engine_type)) { rubidium@5838: return v->cargo_type == new_cargo_type ? (CargoID)CT_NO_REFIT : v->cargo_type; bjarni@4741: } else { bjarni@4741: return CT_INVALID; bjarni@4741: } bjarni@4741: } rubidium@6585: if (v->type != VEH_TRAIN) return CT_INVALID; // We can't refit the vehicle to carry the cargo we want bjarni@4554: bjarni@4554: /* Below this line it's safe to assume that the vehicle in question is a train */ bjarni@4554: bjarni@4554: if (v->cargo_cap != 0) return CT_INVALID; // trying to replace a vehicle with cargo capacity into another one with incompatible cargo type bjarni@4554: bjarni@4554: /* the old engine didn't have cargo capacity, but the new one does bjarni@4554: * now we will figure out what cargo the train is carrying and refit to fit this */ rubidium@7993: v = v->First(); bjarni@4554: do { bjarni@4554: if (v->cargo_cap == 0) continue; bjarni@4554: /* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */ bjarni@4554: if (v->cargo_type == new_cargo_type) return CT_NO_REFIT; bjarni@4554: if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type; rubidium@7988: } while ((v = v->Next()) != NULL); bjarni@4554: return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one bjarni@4554: } bjarni@4554: bjarni@10260: /** 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@10260: * @param p The vehicle owner (faster than refinding the pointer) bjarni@10260: * @param new_engine_type The EngineID to replace to tron@2639: * @return value is cost of the replacement or CMD_ERROR bjarni@2552: */ smatz@11133: static CommandCost ReplaceVehicle(Vehicle **w, uint32 flags, Money total_cost, const Player *p, EngineID new_engine_type) bjarni@2552: { rubidium@7439: CommandCost cost; rubidium@7439: 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@8754: 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@7446: if (replacement_cargo_type == CT_INVALID) return CommandCost(); bjarni@4554: bjarni@6043: 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@11133: cost = DoCommand(old_v->tile, new_engine_type, 0, flags | DC_AUTOREPLACE, GetCmdBuildVeh(old_v)); bjarni@4728: if (CmdFailed(cost)) { rubidium@7446: /* Take back the money we just gave the player */ rubidium@7446: sell_value.MultiplyCost(-1); rubidium@7446: SubtractMoneyFromPlayer(sell_value); bjarni@4728: return cost; bjarni@4728: } bjarni@2552: bjarni@7242: if (replacement_cargo_type != CT_NO_REFIT) { bjarni@7242: /* add refit cost */ rubidium@7439: CommandCost refit_cost = GetRefitCost(new_engine_type); bjarni@8859: if (old_v->type == VEH_TRAIN && RailVehInfo(new_engine_type)->railveh_type == RAILVEH_MULTIHEAD) { bjarni@8859: /* Since it's a dualheaded engine we have to pay once more because the rear end is being refitted too. */ bjarni@8859: refit_cost.AddCost(refit_cost); bjarni@8859: } rubidium@7446: cost.AddCost(refit_cost); bjarni@7242: } 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@6043: 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@8424: 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@8427: SetBit(new_v->u.rail.flags, VRF_REVERSE_DIRECTION); bjarni@3896: } bjarni@2552: rubidium@6585: 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@7231: /* Get the vehicle in front of the one we move out */ rubidium@7993: Vehicle *front = old_v->Previous(); bjarni@9232: if (front == NULL) { bjarni@9232: /* It would appear that we have the front wagon of a row of wagons without engines */ bjarni@9232: Vehicle *next = old_v->Next(); bjarni@9232: if (next != NULL) { bjarni@9232: /* Move the chain to the new front wagon */ bjarni@9232: DoCommand(0, (new_v->index << 16) | next->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); bjarni@9232: } bjarni@9232: } else { bjarni@9232: /* 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@9232: if (IsRearDualheaded(front)) front = front->Previous(); bjarni@9232: /* Now we move the old one out of the train */ bjarni@9232: DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); bjarni@9232: /* Add the new vehicle */ bjarni@10372: CommandCost tmp_move = DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); bjarni@10372: if (CmdFailed(tmp_move)) { bjarni@10372: cost.AddCost(tmp_move); bjarni@10372: DoCommand(0, new_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN)); bjarni@10372: } bjarni@9232: } bjarni@2552: } else { bjarni@2552: // copy/clone the orders belugas@8965: 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@10602: 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@7143: 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@7047: new_v->dest_tile = old_v->dest_tile; bjarni@2552: bjarni@2552: new_v->current_order = old_v->current_order; rubidium@6585: 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@10372: CommandCost tmp_move = DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); bjarni@10372: if (CmdFailed(tmp_move)) { bjarni@10372: cost.AddCost(tmp_move); bjarni@10372: DoCommand(0, temp_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN)); bjarni@10372: } bjarni@2681: } bjarni@2552: } bjarni@2552: } bjarni@10459: if (CmdSucceeded(cost)) { bjarni@10459: /* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */ bjarni@10459: MoveVehicleCargo(new_v->type == VEH_TRAIN ? new_v->First() : new_v, old_v); peter1138@2716: bjarni@10459: /* Get the name of the old vehicle if it has a custom name. */ bjarni@10459: if (old_v->name != NULL) vehicle_name = strdup(old_v->name); bjarni@10459: } bjarni@4554: } else { // flags & DC_EXEC not set rubidium@7446: CommandCost tmp_move; bjarni@8020: bjarni@8020: if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v)) { bjarni@8023: Vehicle *next_veh = GetNextUnit(old_v); // don't try to move the rear multiheaded engine or articulated parts bjarni@8020: if (next_veh != NULL) { bjarni@8020: /* Verify that the wagons can be placed on the engine in question. bjarni@8020: * This is done by building an engine, test if the wagons can be added and then sell the test engine. */ smatz@11133: DoCommand(old_v->tile, new_engine_type, 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_v)); bjarni@8020: Vehicle *temp = GetVehicle(_new_vehicle_id); bjarni@8020: tmp_move = DoCommand(0, (temp->index << 16) | next_veh->index, 1, 0, CMD_MOVE_RAIL_VEHICLE); bjarni@8020: DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v)); bjarni@8020: } bjarni@7296: } bjarni@7296: 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@7448: if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) { rubidium@7446: /* Pay back the loan */ rubidium@7446: sell_value.MultiplyCost(-1); rubidium@7446: 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@7446: sell_value.MultiplyCost(-1); rubidium@7446: 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@7446: cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v))); bjarni@2552: bjarni@10459: if (CmdFailed(cost)) return cost; bjarni@10459: 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@8754: 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@8754: free(vehicle_name); peter1138@2716: } peter1138@2716: bjarni@2552: return cost; bjarni@2552: } bjarni@2552: bjarni@10265: /** Removes wagons from a train until it get a certain length bjarni@10265: * @param v The vehicle bjarni@10265: * @param old_total_length The wanted max length bjarni@10265: * @return The profit from selling the wagons bjarni@10265: */ bjarni@10265: static CommandCost WagonRemoval(Vehicle *v, uint16 old_total_length) bjarni@10265: { bjarni@10265: if (v->type != VEH_TRAIN) return CommandCost(); bjarni@10265: Vehicle *front = v; bjarni@10265: bjarni@10265: CommandCost cost = CommandCost(); bjarni@10265: bjarni@10265: while (front->u.rail.cached_total_length > old_total_length) { bjarni@10265: /* the train is too long. We will remove cars one by one from the start of the train until it's short enough */ bjarni@10265: while (v != NULL && RailVehInfo(v->engine_type)->railveh_type != RAILVEH_WAGON) { bjarni@10265: /* We move backwards in the train until we find a wagon */ bjarni@10265: v = GetNextVehicle(v); bjarni@10265: } bjarni@10265: bjarni@10265: if (v == NULL) { bjarni@10265: /* We sold all the wagons and the train is still not short enough */ bjarni@10265: SetDParam(0, front->unitnumber); rubidium@10556: AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NS_ADVICE, front->index, 0); bjarni@10265: return cost; bjarni@10265: } bjarni@10265: bjarni@10265: /* We found a wagon we can sell */ bjarni@10265: Vehicle *temp = v; bjarni@10265: v = GetNextVehicle(v); bjarni@10265: DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); // remove the wagon from the train bjarni@10265: MoveVehicleCargo(front, temp); // move the cargo back on the train bjarni@10265: cost.AddCost(DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON)); // sell the wagon bjarni@10265: } bjarni@10265: return cost; bjarni@10265: } bjarni@10265: bjarni@10260: /** Get the EngineID of the replacement for a vehicle bjarni@10260: * @param v The vehicle to find a replacement for bjarni@10260: * @param p The vehicle's owner (it's faster to forward the pointer than refinding it) bjarni@10260: * @return the EngineID of the replacement. INVALID_ENGINE if no buildable replacement is found bjarni@10260: */ bjarni@10260: static EngineID GetNewEngineType(const Vehicle *v, const Player *p) bjarni@10260: { bjarni@10260: if (v->type == VEH_TRAIN && IsRearDualheaded(v)) { bjarni@10260: /* we build the rear ends of multiheaded trains with the front ones */ bjarni@10260: return INVALID_ENGINE; bjarni@10260: } bjarni@10260: bjarni@10260: EngineID e = EngineReplacementForPlayer(p, v->engine_type, v->group_id); bjarni@10260: bjarni@10260: if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_player)) { bjarni@10260: return e; bjarni@10260: } bjarni@10260: bjarni@10260: if (v->NeedsAutorenewing(p) && // replace if engine is too old bjarni@10260: IsEngineBuildable(v->engine_type, v->type, _current_player)) { // engine can still be build bjarni@10260: return v->engine_type; bjarni@10260: } bjarni@10260: bjarni@10260: return INVALID_ENGINE; bjarni@10260: } bjarni@10260: 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@10259: * @param flags bjarni@10259: * @param display_costs If set, a cost animation is shown (only if DC_EXEC is set) bjarni@10259: * This bool also takes autorenew money into consideration bjarni@10259: * @return the costs, the success bool and sometimes an error message tron@2639: */ bjarni@10259: CommandCost MaybeReplaceVehicle(Vehicle *v, uint32 flags, bool display_costs) bjarni@2552: { bjarni@2552: Vehicle *w; bjarni@10372: Player *p = GetPlayer(v->owner); bjarni@10259: CommandCost cost; bjarni@10259: bool stopped = false; bjarni@10372: BackuppedVehicle backup(true); bjarni@10259: bjarni@10259: /* We only want "real" vehicle types. */ bjarni@10259: assert(IsPlayerBuildableVehicleType(v)); bjarni@10259: bjarni@10259: /* Ensure that this bool is cleared. */ bjarni@10259: assert(!v->leave_depot_instantly); bjarni@10259: bjarni@10259: /* We can't sell if the current player don't own the vehicle. */ bjarni@10259: assert(v->owner == _current_player); bjarni@10259: bjarni@10259: if (!v->IsInDepot()) { bjarni@10259: /* The vehicle should be inside the depot */ bjarni@10259: switch (v->type) { bjarni@10259: default: NOT_REACHED(); bjarni@10259: case VEH_TRAIN: return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED); break; bjarni@10259: case VEH_ROAD: return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE); break; bjarni@10259: case VEH_SHIP: return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); break; bjarni@10259: case VEH_AIRCRAFT: return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED); break; bjarni@10259: } bjarni@10259: } 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@6585: 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@10259: if (!(v->vehstatus & VS_STOPPED)) { bjarni@10259: /* The vehicle is moving so we better stop it before we might alter consist or sell it */ bjarni@10259: v->vehstatus |= VS_STOPPED; bjarni@10259: /* Remember that we stopped the vehicle */ bjarni@10259: stopped = true; bjarni@10259: } bjarni@2590: bjarni@10259: { rubidium@8726: cost = CommandCost(EXPENSES_NEW_VEHICLES); bjarni@2552: w = v; bjarni@2552: do { bjarni@10260: EngineID new_engine = GetNewEngineType(w, p); bjarni@10260: if (new_engine == INVALID_ENGINE) continue; bjarni@2552: bjarni@10372: if (!backup.ContainsBackup()) { bjarni@10372: /* We are going to try to replace a vehicle but we don't have any backup so we will make one. */ bjarni@10372: backup.Backup(v, p); bjarni@10372: } bjarni@10460: /* Now replace the vehicle. bjarni@10460: * First we need to cache if it's the front vehicle as we need to update the v pointer if it is. bjarni@10460: * If the replacement fails then we can't trust the data in the vehicle hence the reason to cache the result. */ bjarni@10460: bool IsFront = w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE; bjarni@10460: bjarni@10372: cost.AddCost(ReplaceVehicle(&w, DC_EXEC, cost.GetCost(), p, new_engine)); bjarni@7296: bjarni@10460: 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@10459: } while (CmdSucceeded(cost) && w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL); bjarni@2552: bjarni@10459: if (CmdSucceeded(cost) && v->type == VEH_TRAIN && p->renew_keep_length) { bjarni@10372: /* Remove wagons until the wanted length is reached */ bjarni@10372: cost.AddCost(WagonRemoval(v, old_total_length)); bjarni@10372: } bjarni@10372: bjarni@10259: if (flags & DC_QUERY_COST || cost.GetCost() == 0) { bjarni@10259: /* We didn't do anything during the replace so we will just exit here */ bjarni@10398: v = backup.Restore(v, p); bjarni@10259: if (stopped) v->vehstatus &= ~VS_STOPPED; bjarni@10259: return cost; bjarni@10259: } bjarni@10259: bjarni@10372: if (display_costs) { bjarni@10259: /* We want to ensure that we will not get below p->engine_renew_money. bjarni@10259: * We will not actually pay this amount. It's for display and checks only. */ bjarni@10372: CommandCost tmp = cost; bjarni@10372: tmp.AddCost((Money)p->engine_renew_money); bjarni@10372: if (CmdSucceeded(tmp) && GetAvailableMoneyForCommand() < tmp.GetCost()) { bjarni@10259: /* We don't have enough money so we will set cost to failed */ bjarni@10372: cost.AddCost((Money)p->engine_renew_money); bjarni@10259: cost.AddCost(CMD_ERROR); bjarni@10259: } bjarni@10259: } bjarni@10259: bjarni@10259: if (display_costs && CmdFailed(cost)) { bjarni@10259: if (GetAvailableMoneyForCommand() < cost.GetCost() && IsLocalPlayer()) { bjarni@2552: StringID message; bjarni@2552: SetDParam(0, v->unitnumber); bjarni@2552: switch (v->type) { rubidium@6585: case VEH_TRAIN: message = STR_TRAIN_AUTORENEW_FAILED; break; rubidium@6585: case VEH_ROAD: message = STR_ROADVEHICLE_AUTORENEW_FAILED; break; rubidium@6585: case VEH_SHIP: message = STR_SHIP_AUTORENEW_FAILED; break; rubidium@6585: 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@10556: AddNewsItem(message, NS_ADVICE, v->index, 0); bjarni@2552: } bjarni@2617: } bjarni@2617: } bjarni@2617: bjarni@10372: if (display_costs && IsLocalPlayer() && (flags & DC_EXEC) && CmdSucceeded(cost)) { bjarni@10372: ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost()); bjarni@10372: } bjarni@10259: bjarni@10372: if (!(flags & DC_EXEC) || CmdFailed(cost)) { bjarni@10398: v = backup.Restore(v, p); bjarni@10259: } bjarni@10259: bjarni@10259: /* Start the vehicle if we stopped it earlier */ tron@2639: if (stopped) v->vehstatus &= ~VS_STOPPED; bjarni@10259: bjarni@4662: return cost; bjarni@2552: }