2007-03-08 23:05:05 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-04-19 19:01:57 +00:00
|
|
|
/** @file autoreplace_cmd.cpp Deals with autoreplace execution but not the setup */
|
|
|
|
|
2007-03-08 23:05:05 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "openttd.h"
|
|
|
|
#include "roadveh.h"
|
|
|
|
#include "ship.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2008-01-12 14:10:35 +00:00
|
|
|
#include "player_func.h"
|
2007-03-08 23:05:05 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "vehicle_gui.h"
|
|
|
|
#include "train.h"
|
|
|
|
#include "aircraft.h"
|
|
|
|
#include "cargotype.h"
|
2007-05-19 09:40:18 +00:00
|
|
|
#include "group.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
2008-01-07 00:57:19 +00:00
|
|
|
#include "variables.h"
|
2008-01-07 09:19:53 +00:00
|
|
|
#include "autoreplace_func.h"
|
2008-01-19 23:34:52 +00:00
|
|
|
#include "articulated_vehicles.h"
|
2007-03-08 23:05:05 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2008-08-10 15:02:21 +00:00
|
|
|
/** Figure out if two engines got at least one type of cargo in common (refitting if needed)
|
|
|
|
* @param engine_a one of the EngineIDs
|
|
|
|
* @param engine_b the other EngineID
|
|
|
|
* @param type the type of the engines
|
|
|
|
* @return true if they can both carry the same type of cargo (or at least one of them got no capacity at all)
|
|
|
|
*/
|
|
|
|
static bool EnginesGotCargoInCommon(EngineID engine_a, EngineID engine_b, VehicleType type)
|
|
|
|
{
|
|
|
|
uint32 available_cargos_a = GetUnionOfArticulatedRefitMasks(engine_a, type, true);
|
|
|
|
uint32 available_cargos_b = GetUnionOfArticulatedRefitMasks(engine_b, type, true);
|
|
|
|
return (available_cargos_a == 0 || available_cargos_b == 0 || (available_cargos_a & available_cargos_b) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks some basic properties whether autoreplace is allowed
|
|
|
|
* @param from Origin engine
|
|
|
|
* @param to Destination engine
|
|
|
|
* @param player Player to check for
|
|
|
|
* @return true if autoreplace is allowed
|
|
|
|
*/
|
|
|
|
bool CheckAutoreplaceValidity(EngineID from, EngineID to, PlayerID player)
|
|
|
|
{
|
|
|
|
/* First we make sure that it's a valid type the user requested
|
|
|
|
* check that it's an engine that is in the engine array */
|
|
|
|
if (!IsEngineIndex(from) || !IsEngineIndex(to)) return false;
|
|
|
|
|
|
|
|
/* we can't replace an engine into itself (that would be autorenew) */
|
|
|
|
if (from == to) return false;
|
|
|
|
|
|
|
|
VehicleType type = GetEngine(from)->type;
|
|
|
|
|
|
|
|
/* check that the new vehicle type is available to the player and its type is the same as the original one */
|
|
|
|
if (!IsEngineBuildable(to, type, player)) return false;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case VEH_TRAIN: {
|
|
|
|
const RailVehicleInfo *rvi_from = RailVehInfo(from);
|
|
|
|
const RailVehicleInfo *rvi_to = RailVehInfo(to);
|
|
|
|
|
|
|
|
/* make sure the railtypes are compatible */
|
|
|
|
if ((GetRailTypeInfo(rvi_from->railtype)->compatible_railtypes & GetRailTypeInfo(rvi_to->railtype)->compatible_railtypes) == 0) return false;
|
|
|
|
|
|
|
|
/* make sure we do not replace wagons with engines or vise versa */
|
|
|
|
if ((rvi_from->railveh_type == RAILVEH_WAGON) != (rvi_to->railveh_type == RAILVEH_WAGON)) return false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case VEH_ROAD:
|
|
|
|
/* make sure that we do not replace a tram with a normal road vehicles or vise versa */
|
|
|
|
if (HasBit(EngInfo(from)->misc_flags, EF_ROAD_TRAM) != HasBit(EngInfo(to)->misc_flags, EF_ROAD_TRAM)) return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_AIRCRAFT:
|
|
|
|
/* make sure that we do not replace a plane with a helicopter or vise versa */
|
|
|
|
if ((AircraftVehInfo(from)->subtype & AIR_CTOL) != (AircraftVehInfo(to)->subtype & AIR_CTOL)) return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the engines needs to be able to carry the same cargo */
|
|
|
|
return EnginesGotCargoInCommon(from, to, type);
|
|
|
|
}
|
2008-02-23 22:01:55 +00:00
|
|
|
|
2007-03-08 23:05:05 +00:00
|
|
|
/*
|
|
|
|
* move the cargo from one engine to another if possible
|
|
|
|
*/
|
|
|
|
static void MoveVehicleCargo(Vehicle *dest, Vehicle *source)
|
|
|
|
{
|
|
|
|
Vehicle *v = dest;
|
|
|
|
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
if (source->cargo_type != dest->cargo_type)
|
|
|
|
continue; // cargo not compatible
|
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
if (dest->cargo.Count() == dest->cargo_cap)
|
2007-03-08 23:05:05 +00:00
|
|
|
continue; // the destination vehicle is already full
|
|
|
|
|
2007-06-22 11:58:59 +00:00
|
|
|
uint units_moved = min(source->cargo.Count(), dest->cargo_cap - dest->cargo.Count());
|
|
|
|
source->cargo.MoveTo(&dest->cargo, units_moved);
|
2007-03-08 23:05:05 +00:00
|
|
|
|
|
|
|
// copy the age of the cargo
|
|
|
|
dest->day_counter = source->day_counter;
|
|
|
|
dest->tick_counter = source->tick_counter;
|
|
|
|
|
2007-08-30 13:03:56 +00:00
|
|
|
} while (source->cargo.Count() > 0 && (dest = dest->Next()) != NULL);
|
2007-03-08 23:05:05 +00:00
|
|
|
dest = v;
|
2007-08-30 13:03:56 +00:00
|
|
|
} while ((source = source->Next()) != NULL);
|
2007-03-08 23:05:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The of the train will be incorrect at this moment. This is due
|
|
|
|
* to the fact that removing the old wagon updates the weight of
|
|
|
|
* the complete train, which is without the weight of cargo we just
|
|
|
|
* moved back into some (of the) new wagon(s).
|
|
|
|
*/
|
2008-07-24 15:19:26 +00:00
|
|
|
if (dest->type == VEH_TRAIN) TrainConsistChanged(dest->First(), true);
|
2007-03-08 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
2008-07-27 15:17:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests whether refit orders that applied to v will also apply to the new vehicle type
|
|
|
|
* @param v The vehicle to be replaced
|
|
|
|
* @param engine_type The type we want to replace with
|
|
|
|
* @return true iff all refit orders stay valid
|
|
|
|
*/
|
|
|
|
static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, EngineID engine_type)
|
2007-03-08 23:05:05 +00:00
|
|
|
{
|
|
|
|
const Order *o;
|
|
|
|
const Vehicle *u;
|
|
|
|
|
2008-07-27 15:17:16 +00:00
|
|
|
uint32 union_refit_mask_a = GetUnionOfArticulatedRefitMasks(v->engine_type, v->type, false);
|
|
|
|
uint32 union_refit_mask_b = GetUnionOfArticulatedRefitMasks(engine_type, v->type, false);
|
|
|
|
|
2007-03-08 23:05:05 +00:00
|
|
|
if (v->type == VEH_TRAIN) {
|
2007-08-30 21:11:12 +00:00
|
|
|
u = v->First();
|
2007-03-08 23:05:05 +00:00
|
|
|
} else {
|
|
|
|
u = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
FOR_VEHICLE_ORDERS(u, o) {
|
2008-04-06 07:07:21 +00:00
|
|
|
if (!o->IsRefit()) continue;
|
2008-07-27 15:17:16 +00:00
|
|
|
CargoID cargo_type = o->GetRefitCargo();
|
|
|
|
|
|
|
|
if (!HasBit(union_refit_mask_a, cargo_type)) continue;
|
|
|
|
if (!HasBit(union_refit_mask_b, cargo_type)) return false;
|
2007-03-08 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to find what type of cargo to refit to when autoreplacing
|
|
|
|
* @param *v Original vehicle, that is being replaced
|
|
|
|
* @param engine_type The EngineID of the vehicle that is being replaced to
|
|
|
|
* @return The cargo type to replace to
|
|
|
|
* CT_NO_REFIT is returned if no refit is needed
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type)
|
|
|
|
{
|
2008-07-27 15:17:16 +00:00
|
|
|
CargoID cargo_type;
|
2007-03-08 23:05:05 +00:00
|
|
|
|
2008-07-27 15:17:16 +00:00
|
|
|
if (GetUnionOfArticulatedRefitMasks(engine_type, v->type, true) == 0) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity
|
2007-03-08 23:05:05 +00:00
|
|
|
|
2008-07-27 15:17:16 +00:00
|
|
|
if (IsArticulatedVehicleCarryingDifferentCargos(v, &cargo_type)) return CT_INVALID; // We cannot refit to mixed cargos in an automated way
|
|
|
|
|
|
|
|
uint32 available_cargo_types = GetIntersectionOfArticulatedRefitMasks(engine_type, v->type, true);
|
|
|
|
|
|
|
|
if (cargo_type == CT_INVALID) {
|
|
|
|
if (v->type != VEH_TRAIN) return CT_NO_REFIT; // If the vehicle does not carry anything at all, every replacement is fine.
|
|
|
|
|
|
|
|
/* the old engine didn't have cargo capacity, but the new one does
|
|
|
|
* now we will figure out what cargo the train is carrying and refit to fit this */
|
|
|
|
|
|
|
|
for (v = v->First(); v != NULL; v = v->Next()) {
|
|
|
|
if (v->cargo_cap == 0) continue;
|
|
|
|
/* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
|
|
|
|
if (HasBit(available_cargo_types, v->cargo_type)) {
|
|
|
|
/* Do we have to refit the vehicle, or is it already carrying the right cargo? */
|
|
|
|
uint16 *default_capacity = GetCapacityOfArticulatedParts(engine_type, v->type);
|
|
|
|
for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
|
2008-08-09 16:42:55 +00:00
|
|
|
if (cid != v->cargo_type && default_capacity[cid] > 0) return v->cargo_type;
|
2008-07-27 15:17:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return CT_NO_REFIT;
|
|
|
|
}
|
2007-03-08 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
2008-07-27 15:17:16 +00:00
|
|
|
return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one
|
|
|
|
} else {
|
|
|
|
if (!HasBit(available_cargo_types, cargo_type)) return CT_INVALID; // We can't refit the vehicle to carry the cargo we want
|
|
|
|
|
|
|
|
if (!VerifyAutoreplaceRefitForOrders(v, engine_type)) return CT_INVALID; // Some refit orders loose their effect
|
2007-03-08 23:05:05 +00:00
|
|
|
|
2008-07-27 15:17:16 +00:00
|
|
|
/* Do we have to refit the vehicle, or is it already carrying the right cargo? */
|
|
|
|
uint16 *default_capacity = GetCapacityOfArticulatedParts(engine_type, v->type);
|
|
|
|
for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
|
|
|
|
if (cid != cargo_type && default_capacity[cid] > 0) return cargo_type;
|
|
|
|
}
|
2007-03-08 23:05:05 +00:00
|
|
|
|
2008-07-27 15:17:16 +00:00
|
|
|
return CT_NO_REFIT;
|
|
|
|
}
|
2007-03-08 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
2008-04-19 17:47:56 +00:00
|
|
|
/** Replaces a vehicle (used to be called autorenew)
|
2007-03-08 23:05:05 +00:00
|
|
|
* This function is only called from MaybeReplaceVehicle()
|
|
|
|
* Must be called with _current_player set to the owner of the vehicle
|
|
|
|
* @param w Vehicle to replace
|
|
|
|
* @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts
|
2008-04-19 17:47:56 +00:00
|
|
|
* @param p The vehicle owner (faster than refinding the pointer)
|
|
|
|
* @param new_engine_type The EngineID to replace to
|
2007-03-08 23:05:05 +00:00
|
|
|
* @return value is cost of the replacement or CMD_ERROR
|
|
|
|
*/
|
2008-07-09 20:55:47 +00:00
|
|
|
static CommandCost ReplaceVehicle(Vehicle **w, uint32 flags, Money total_cost, const Player *p, EngineID new_engine_type)
|
2007-03-08 23:05:05 +00:00
|
|
|
{
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost cost;
|
|
|
|
CommandCost sell_value;
|
2007-03-08 23:05:05 +00:00
|
|
|
Vehicle *old_v = *w;
|
|
|
|
const UnitID cached_unitnumber = old_v->unitnumber;
|
|
|
|
bool new_front = false;
|
|
|
|
Vehicle *new_v = NULL;
|
2008-01-12 19:58:06 +00:00
|
|
|
char *vehicle_name = NULL;
|
2007-03-08 23:05:05 +00:00
|
|
|
CargoID replacement_cargo_type;
|
|
|
|
|
|
|
|
replacement_cargo_type = GetNewCargoTypeForReplace(old_v, new_engine_type);
|
|
|
|
|
|
|
|
/* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo type */
|
2007-06-18 19:53:50 +00:00
|
|
|
if (replacement_cargo_type == CT_INVALID) return CommandCost();
|
2007-03-08 23:05:05 +00:00
|
|
|
|
|
|
|
sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, GetCmdSellVeh(old_v));
|
|
|
|
|
|
|
|
/* We give the player a loan of the same amount as the sell value.
|
|
|
|
* This is needed in case he needs the income from the sale to build the new vehicle.
|
|
|
|
* We take it back if building fails or when we really sell the old engine */
|
|
|
|
SubtractMoneyFromPlayer(sell_value);
|
|
|
|
|
2008-07-09 20:55:47 +00:00
|
|
|
cost = DoCommand(old_v->tile, new_engine_type, 0, flags | DC_AUTOREPLACE, GetCmdBuildVeh(old_v));
|
2007-03-08 23:05:05 +00:00
|
|
|
if (CmdFailed(cost)) {
|
2007-06-18 19:53:50 +00:00
|
|
|
/* Take back the money we just gave the player */
|
|
|
|
sell_value.MultiplyCost(-1);
|
|
|
|
SubtractMoneyFromPlayer(sell_value);
|
2007-03-08 23:05:05 +00:00
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2007-05-29 20:06:06 +00:00
|
|
|
if (replacement_cargo_type != CT_NO_REFIT) {
|
|
|
|
/* add refit cost */
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost refit_cost = GetRefitCost(new_engine_type);
|
2008-01-19 23:34:52 +00:00
|
|
|
if (old_v->type == VEH_TRAIN && RailVehInfo(new_engine_type)->railveh_type == RAILVEH_MULTIHEAD) {
|
|
|
|
/* Since it's a dualheaded engine we have to pay once more because the rear end is being refitted too. */
|
|
|
|
refit_cost.AddCost(refit_cost);
|
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
cost.AddCost(refit_cost);
|
2007-05-29 20:06:06 +00:00
|
|
|
}
|
2007-03-08 23:05:05 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
new_v = GetVehicle(_new_vehicle_id);
|
|
|
|
*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
|
|
|
|
|
|
|
|
/* refit if needed */
|
|
|
|
if (replacement_cargo_type != CT_NO_REFIT) {
|
|
|
|
if (CmdFailed(DoCommand(0, new_v->index, replacement_cargo_type, DC_EXEC, GetCmdRefitVeh(new_v)))) {
|
|
|
|
/* Being here shows a failure, which most likely is in GetNewCargoTypeForReplace() or incorrect estimation costs */
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
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()))) {
|
2007-03-08 23:05:05 +00:00
|
|
|
// we are autorenewing to a single engine, so we will turn it as the old one was turned as well
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(new_v->u.rail.flags, VRF_REVERSE_DIRECTION);
|
2007-03-08 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_v)) {
|
|
|
|
/* this is a railcar. We need to move the car into the train
|
|
|
|
* We add the new engine after the old one instead of replacing it. It will give the same result anyway when we
|
|
|
|
* sell the old engine in a moment
|
|
|
|
*/
|
2007-05-28 21:40:40 +00:00
|
|
|
/* Get the vehicle in front of the one we move out */
|
2007-08-30 21:11:12 +00:00
|
|
|
Vehicle *front = old_v->Previous();
|
2008-03-25 21:58:13 +00:00
|
|
|
if (front == NULL) {
|
|
|
|
/* It would appear that we have the front wagon of a row of wagons without engines */
|
|
|
|
Vehicle *next = old_v->Next();
|
|
|
|
if (next != NULL) {
|
|
|
|
/* Move the chain to the new front wagon */
|
|
|
|
DoCommand(0, (new_v->index << 16) | next->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* 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 */
|
|
|
|
if (IsRearDualheaded(front)) front = front->Previous();
|
|
|
|
/* Now we move the old one out of the train */
|
|
|
|
DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
|
|
/* Add the new vehicle */
|
2008-04-27 20:09:29 +00:00
|
|
|
CommandCost tmp_move = DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
|
|
if (CmdFailed(tmp_move)) {
|
|
|
|
cost.AddCost(tmp_move);
|
|
|
|
DoCommand(0, new_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN));
|
|
|
|
}
|
2008-03-25 21:58:13 +00:00
|
|
|
}
|
2007-03-08 23:05:05 +00:00
|
|
|
} else {
|
|
|
|
// copy/clone the orders
|
2008-02-02 02:45:09 +00:00
|
|
|
DoCommand(0, (old_v->index << 16) | new_v->index, old_v->IsOrderListShared() ? CO_SHARE : CO_COPY, DC_EXEC, CMD_CLONE_ORDER);
|
2007-03-08 23:05:05 +00:00
|
|
|
new_v->cur_order_index = old_v->cur_order_index;
|
2008-05-17 20:54:18 +00:00
|
|
|
ChangeVehicleViewWindow(old_v->index, new_v->index);
|
2007-03-08 23:05:05 +00:00
|
|
|
new_v->profit_this_year = old_v->profit_this_year;
|
|
|
|
new_v->profit_last_year = old_v->profit_last_year;
|
|
|
|
new_v->service_interval = old_v->service_interval;
|
2007-05-19 12:29:09 +00:00
|
|
|
DoCommand(0, old_v->group_id, new_v->index, flags, CMD_ADD_VEHICLE_GROUP);
|
2007-03-08 23:05:05 +00:00
|
|
|
new_front = true;
|
|
|
|
new_v->unitnumber = old_v->unitnumber; // use the same unit number
|
2007-04-29 20:16:58 +00:00
|
|
|
new_v->dest_tile = old_v->dest_tile;
|
2007-03-08 23:05:05 +00:00
|
|
|
|
|
|
|
new_v->current_order = old_v->current_order;
|
|
|
|
if (old_v->type == VEH_TRAIN && GetNextVehicle(old_v) != NULL){
|
|
|
|
Vehicle *temp_v = GetNextVehicle(old_v);
|
|
|
|
|
|
|
|
// move the entire train to the new engine, excluding the old engine
|
|
|
|
if (IsMultiheaded(old_v) && temp_v == old_v->u.rail.other_multiheaded_part) {
|
|
|
|
// we got front and rear of a multiheaded engine right after each other. We should work with the next in line instead
|
|
|
|
temp_v = GetNextVehicle(temp_v);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (temp_v != NULL) {
|
2008-04-27 20:09:29 +00:00
|
|
|
CommandCost tmp_move = DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
|
|
|
if (CmdFailed(tmp_move)) {
|
|
|
|
cost.AddCost(tmp_move);
|
|
|
|
DoCommand(0, temp_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN));
|
|
|
|
}
|
2007-03-08 23:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-07 22:08:20 +00:00
|
|
|
if (CmdSucceeded(cost)) {
|
|
|
|
/* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
|
|
|
|
MoveVehicleCargo(new_v->type == VEH_TRAIN ? new_v->First() : new_v, old_v);
|
2007-03-08 23:05:05 +00:00
|
|
|
|
2008-05-07 22:08:20 +00:00
|
|
|
/* Get the name of the old vehicle if it has a custom name. */
|
|
|
|
if (old_v->name != NULL) vehicle_name = strdup(old_v->name);
|
|
|
|
}
|
2007-03-08 23:05:05 +00:00
|
|
|
} else { // flags & DC_EXEC not set
|
2007-06-18 19:53:50 +00:00
|
|
|
CommandCost tmp_move;
|
2007-09-04 19:58:18 +00:00
|
|
|
|
|
|
|
if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v)) {
|
2007-09-05 10:33:42 +00:00
|
|
|
Vehicle *next_veh = GetNextUnit(old_v); // don't try to move the rear multiheaded engine or articulated parts
|
2007-09-04 19:58:18 +00:00
|
|
|
if (next_veh != NULL) {
|
|
|
|
/* Verify that the wagons can be placed on the engine in question.
|
|
|
|
* This is done by building an engine, test if the wagons can be added and then sell the test engine. */
|
2008-07-09 20:55:47 +00:00
|
|
|
DoCommand(old_v->tile, new_engine_type, 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_v));
|
2007-09-04 19:58:18 +00:00
|
|
|
Vehicle *temp = GetVehicle(_new_vehicle_id);
|
|
|
|
tmp_move = DoCommand(0, (temp->index << 16) | next_veh->index, 1, 0, CMD_MOVE_RAIL_VEHICLE);
|
|
|
|
DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v));
|
|
|
|
}
|
2007-06-04 20:46:01 +00:00
|
|
|
}
|
|
|
|
|
2007-03-08 23:05:05 +00:00
|
|
|
/* Ensure that the player will not end up having negative money while autoreplacing
|
|
|
|
* This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */
|
2007-06-18 21:00:14 +00:00
|
|
|
if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) {
|
2007-06-18 19:53:50 +00:00
|
|
|
/* Pay back the loan */
|
|
|
|
sell_value.MultiplyCost(-1);
|
|
|
|
SubtractMoneyFromPlayer(sell_value);
|
2007-03-08 23:05:05 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Take back the money we just gave the player just before building the vehicle
|
|
|
|
* The player will get the same amount now that the sale actually takes place */
|
2007-06-18 19:53:50 +00:00
|
|
|
sell_value.MultiplyCost(-1);
|
|
|
|
SubtractMoneyFromPlayer(sell_value);
|
2007-03-08 23:05:05 +00:00
|
|
|
|
|
|
|
/* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */
|
2007-06-18 19:53:50 +00:00
|
|
|
cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v)));
|
2007-03-08 23:05:05 +00:00
|
|
|
|
2008-05-07 22:08:20 +00:00
|
|
|
if (CmdFailed(cost)) return cost;
|
|
|
|
|
2007-03-08 23:05:05 +00:00
|
|
|
if (new_front) {
|
|
|
|
/* now we assign the old unitnumber to the new vehicle */
|
|
|
|
new_v->unitnumber = cached_unitnumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Transfer the name of the old vehicle */
|
2008-01-12 19:58:06 +00:00
|
|
|
if ((flags & DC_EXEC) && vehicle_name != NULL) {
|
2007-03-08 23:05:05 +00:00
|
|
|
_cmd_text = vehicle_name;
|
|
|
|
DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE);
|
2008-01-12 19:58:06 +00:00
|
|
|
free(vehicle_name);
|
2007-03-08 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2008-04-19 22:51:52 +00:00
|
|
|
/** Removes wagons from a train until it get a certain length
|
|
|
|
* @param v The vehicle
|
|
|
|
* @param old_total_length The wanted max length
|
|
|
|
* @return The profit from selling the wagons
|
|
|
|
*/
|
|
|
|
static CommandCost WagonRemoval(Vehicle *v, uint16 old_total_length)
|
|
|
|
{
|
|
|
|
if (v->type != VEH_TRAIN) return CommandCost();
|
|
|
|
Vehicle *front = v;
|
|
|
|
|
|
|
|
CommandCost cost = CommandCost();
|
|
|
|
|
|
|
|
while (front->u.rail.cached_total_length > old_total_length) {
|
|
|
|
/* the train is too long. We will remove cars one by one from the start of the train until it's short enough */
|
|
|
|
while (v != NULL && RailVehInfo(v->engine_type)->railveh_type != RAILVEH_WAGON) {
|
|
|
|
/* We move backwards in the train until we find a wagon */
|
|
|
|
v = GetNextVehicle(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v == NULL) {
|
|
|
|
/* We sold all the wagons and the train is still not short enough */
|
|
|
|
SetDParam(0, front->unitnumber);
|
2008-05-15 13:39:36 +00:00
|
|
|
AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NS_ADVICE, front->index, 0);
|
2008-04-19 22:51:52 +00:00
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We found a wagon we can sell */
|
|
|
|
Vehicle *temp = v;
|
|
|
|
v = GetNextVehicle(v);
|
|
|
|
DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); // remove the wagon from the train
|
|
|
|
MoveVehicleCargo(front, temp); // move the cargo back on the train
|
|
|
|
cost.AddCost(DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON)); // sell the wagon
|
|
|
|
}
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2008-04-19 17:47:56 +00:00
|
|
|
/** Get the EngineID of the replacement for a vehicle
|
|
|
|
* @param v The vehicle to find a replacement for
|
|
|
|
* @param p The vehicle's owner (it's faster to forward the pointer than refinding it)
|
|
|
|
* @return the EngineID of the replacement. INVALID_ENGINE if no buildable replacement is found
|
|
|
|
*/
|
|
|
|
static EngineID GetNewEngineType(const Vehicle *v, const Player *p)
|
|
|
|
{
|
2008-07-27 15:17:16 +00:00
|
|
|
assert(v->type != VEH_TRAIN || !IsArticulatedPart(v));
|
|
|
|
|
2008-04-19 17:47:56 +00:00
|
|
|
if (v->type == VEH_TRAIN && IsRearDualheaded(v)) {
|
|
|
|
/* we build the rear ends of multiheaded trains with the front ones */
|
|
|
|
return INVALID_ENGINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
EngineID e = EngineReplacementForPlayer(p, v->engine_type, v->group_id);
|
|
|
|
|
|
|
|
if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_player)) {
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->NeedsAutorenewing(p) && // replace if engine is too old
|
|
|
|
IsEngineBuildable(v->engine_type, v->type, _current_player)) { // engine can still be build
|
|
|
|
return v->engine_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
return INVALID_ENGINE;
|
|
|
|
}
|
|
|
|
|
2007-03-08 23:05:05 +00:00
|
|
|
/** replaces a vehicle if it's set for autoreplace or is too old
|
|
|
|
* (used to be called autorenew)
|
|
|
|
* @param v The vehicle to replace
|
|
|
|
* if the vehicle is a train, v needs to be the front engine
|
2008-04-19 15:14:20 +00:00
|
|
|
* @param flags
|
|
|
|
* @param display_costs If set, a cost animation is shown (only if DC_EXEC is set)
|
|
|
|
* This bool also takes autorenew money into consideration
|
|
|
|
* @return the costs, the success bool and sometimes an error message
|
2007-03-08 23:05:05 +00:00
|
|
|
*/
|
2008-04-19 15:14:20 +00:00
|
|
|
CommandCost MaybeReplaceVehicle(Vehicle *v, uint32 flags, bool display_costs)
|
2007-03-08 23:05:05 +00:00
|
|
|
{
|
|
|
|
Vehicle *w;
|
2008-04-27 20:09:29 +00:00
|
|
|
Player *p = GetPlayer(v->owner);
|
2008-04-19 15:14:20 +00:00
|
|
|
CommandCost cost;
|
|
|
|
bool stopped = false;
|
2008-04-27 20:09:29 +00:00
|
|
|
BackuppedVehicle backup(true);
|
2008-04-19 15:14:20 +00:00
|
|
|
|
|
|
|
/* We only want "real" vehicle types. */
|
|
|
|
assert(IsPlayerBuildableVehicleType(v));
|
|
|
|
|
|
|
|
/* Ensure that this bool is cleared. */
|
|
|
|
assert(!v->leave_depot_instantly);
|
|
|
|
|
|
|
|
/* We can't sell if the current player don't own the vehicle. */
|
|
|
|
assert(v->owner == _current_player);
|
|
|
|
|
|
|
|
if (!v->IsInDepot()) {
|
|
|
|
/* The vehicle should be inside the depot */
|
|
|
|
switch (v->type) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
case VEH_TRAIN: return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED); break;
|
|
|
|
case VEH_ROAD: return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE); break;
|
|
|
|
case VEH_SHIP: return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); break;
|
|
|
|
case VEH_AIRCRAFT: return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED); break;
|
|
|
|
}
|
|
|
|
}
|
2007-03-08 23:05:05 +00:00
|
|
|
|
|
|
|
/* Remember the length in case we need to trim train later on
|
|
|
|
* If it's not a train, the value is unused
|
|
|
|
* round up to the length of the tiles used for the train instead of the train length instead
|
|
|
|
* Useful when newGRF uses custom length */
|
|
|
|
uint16 old_total_length = (v->type == VEH_TRAIN ?
|
|
|
|
(v->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE :
|
|
|
|
-1
|
|
|
|
);
|
|
|
|
|
2008-04-19 15:14:20 +00:00
|
|
|
if (!(v->vehstatus & VS_STOPPED)) {
|
|
|
|
/* The vehicle is moving so we better stop it before we might alter consist or sell it */
|
|
|
|
v->vehstatus |= VS_STOPPED;
|
|
|
|
/* Remember that we stopped the vehicle */
|
|
|
|
stopped = true;
|
|
|
|
}
|
2007-03-08 23:05:05 +00:00
|
|
|
|
2008-04-19 15:14:20 +00:00
|
|
|
{
|
2008-01-09 16:55:48 +00:00
|
|
|
cost = CommandCost(EXPENSES_NEW_VEHICLES);
|
2007-03-08 23:05:05 +00:00
|
|
|
w = v;
|
|
|
|
do {
|
2008-04-19 17:47:56 +00:00
|
|
|
EngineID new_engine = GetNewEngineType(w, p);
|
|
|
|
if (new_engine == INVALID_ENGINE) continue;
|
2007-03-08 23:05:05 +00:00
|
|
|
|
2008-04-27 20:09:29 +00:00
|
|
|
if (!backup.ContainsBackup()) {
|
|
|
|
/* We are going to try to replace a vehicle but we don't have any backup so we will make one. */
|
|
|
|
backup.Backup(v, p);
|
|
|
|
}
|
2008-05-07 23:00:11 +00:00
|
|
|
/* Now replace the vehicle.
|
|
|
|
* First we need to cache if it's the front vehicle as we need to update the v pointer if it is.
|
|
|
|
* If the replacement fails then we can't trust the data in the vehicle hence the reason to cache the result. */
|
|
|
|
bool IsFront = w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE;
|
|
|
|
|
2008-04-27 20:09:29 +00:00
|
|
|
cost.AddCost(ReplaceVehicle(&w, DC_EXEC, cost.GetCost(), p, new_engine));
|
2007-06-04 20:46:01 +00:00
|
|
|
|
2008-05-07 23:00:11 +00:00
|
|
|
if (IsFront) {
|
2007-03-08 23:05:05 +00:00
|
|
|
/* now we bought a new engine and sold the old one. We need to fix the
|
|
|
|
* pointers in order to avoid pointing to the old one for trains: these
|
|
|
|
* pointers should point to the front engine and not the cars
|
|
|
|
*/
|
|
|
|
v = w;
|
|
|
|
}
|
2008-05-07 22:08:20 +00:00
|
|
|
} while (CmdSucceeded(cost) && w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL);
|
2007-03-08 23:05:05 +00:00
|
|
|
|
2008-05-07 22:08:20 +00:00
|
|
|
if (CmdSucceeded(cost) && v->type == VEH_TRAIN && p->renew_keep_length) {
|
2008-04-27 20:09:29 +00:00
|
|
|
/* Remove wagons until the wanted length is reached */
|
|
|
|
cost.AddCost(WagonRemoval(v, old_total_length));
|
|
|
|
}
|
|
|
|
|
2008-04-19 15:14:20 +00:00
|
|
|
if (flags & DC_QUERY_COST || cost.GetCost() == 0) {
|
|
|
|
/* We didn't do anything during the replace so we will just exit here */
|
2008-05-04 10:05:35 +00:00
|
|
|
v = backup.Restore(v, p);
|
2008-04-19 15:14:20 +00:00
|
|
|
if (stopped) v->vehstatus &= ~VS_STOPPED;
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2008-04-27 20:09:29 +00:00
|
|
|
if (display_costs) {
|
2008-04-19 15:14:20 +00:00
|
|
|
/* We want to ensure that we will not get below p->engine_renew_money.
|
|
|
|
* We will not actually pay this amount. It's for display and checks only. */
|
2008-04-27 20:09:29 +00:00
|
|
|
CommandCost tmp = cost;
|
|
|
|
tmp.AddCost((Money)p->engine_renew_money);
|
|
|
|
if (CmdSucceeded(tmp) && GetAvailableMoneyForCommand() < tmp.GetCost()) {
|
2008-04-19 15:14:20 +00:00
|
|
|
/* We don't have enough money so we will set cost to failed */
|
2008-04-27 20:09:29 +00:00
|
|
|
cost.AddCost((Money)p->engine_renew_money);
|
2008-04-19 15:14:20 +00:00
|
|
|
cost.AddCost(CMD_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (display_costs && CmdFailed(cost)) {
|
|
|
|
if (GetAvailableMoneyForCommand() < cost.GetCost() && IsLocalPlayer()) {
|
2007-03-08 23:05:05 +00:00
|
|
|
StringID message;
|
|
|
|
SetDParam(0, v->unitnumber);
|
|
|
|
switch (v->type) {
|
|
|
|
case VEH_TRAIN: message = STR_TRAIN_AUTORENEW_FAILED; break;
|
|
|
|
case VEH_ROAD: message = STR_ROADVEHICLE_AUTORENEW_FAILED; break;
|
|
|
|
case VEH_SHIP: message = STR_SHIP_AUTORENEW_FAILED; break;
|
|
|
|
case VEH_AIRCRAFT: message = STR_AIRCRAFT_AUTORENEW_FAILED; break;
|
|
|
|
// This should never happen
|
|
|
|
default: NOT_REACHED(); message = 0; break;
|
|
|
|
}
|
|
|
|
|
2008-05-15 13:39:36 +00:00
|
|
|
AddNewsItem(message, NS_ADVICE, v->index, 0);
|
2007-03-08 23:05:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-27 20:09:29 +00:00
|
|
|
if (display_costs && IsLocalPlayer() && (flags & DC_EXEC) && CmdSucceeded(cost)) {
|
|
|
|
ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost());
|
|
|
|
}
|
2008-04-19 22:51:52 +00:00
|
|
|
|
2008-04-27 20:09:29 +00:00
|
|
|
if (!(flags & DC_EXEC) || CmdFailed(cost)) {
|
2008-05-04 10:05:35 +00:00
|
|
|
v = backup.Restore(v, p);
|
2007-03-08 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
2008-04-19 15:14:20 +00:00
|
|
|
/* Start the vehicle if we stopped it earlier */
|
2007-03-08 23:05:05 +00:00
|
|
|
if (stopped) v->vehstatus &= ~VS_STOPPED;
|
2008-04-19 15:14:20 +00:00
|
|
|
|
2007-03-08 23:05:05 +00:00
|
|
|
return cost;
|
|
|
|
}
|