src/autoreplace_cmd.cpp
author truebrain
Thu, 12 Jun 2008 21:07:25 +0000
branchnoai
changeset 10943 5f5a5dd407d8
parent 10645 8cbdb511a674
permissions -rw-r--r--
(svn r13497) [NoAI] -Add [Library]: pathfinder.road, a basic road pathfinder (no bridges/tunnels (yet)) (patch by Yexo, very nice job!)
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
     3
/** @file autoreplace_cmd.cpp Deals with autoreplace execution but not the setup */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1881
diff changeset
     6
#include "openttd.h"
3959
e2bbaa8d4978 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3957
diff changeset
     7
#include "roadveh.h"
3961
e2783f244ac6 (svn r5120) Add IsShipInDepot{Stopped,}() and remove some redundant checks
tron
parents: 3959
diff changeset
     8
#include "ship.h"
9826
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
     9
#include "news_func.h"
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    10
#include "player_func.h"
1601
9fd461d00287 (svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents: 1600
diff changeset
    11
#include "debug.h"
1752
cdbfb2f23e72 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1735
diff changeset
    12
#include "vehicle_gui.h"
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
    13
#include "train.h"
4662
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
    14
#include "aircraft.h"
6439
99a5807852b1 (svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents: 6432
diff changeset
    15
#include "cargotype.h"
9624
b71483f2330f (svn r9915) [NoAI] -Sync with trunk -r9815:9914
glx
parents: 9620
diff changeset
    16
#include "group.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    17
#include "strings_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    18
#include "command_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    19
#include "vehicle_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    20
#include "functions.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    21
#include "variables.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    22
#include "autoreplace_func.h"
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    23
#include "articulated_vehicles.h"
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    24
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    25
#include "table/strings.h"
2244
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
    26
9800
ab08ca2a2018 (svn r12306) [NoAI] -Sync with trunk r12209:12304
glx
parents: 9724
diff changeset
    27
2599
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    28
/*
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    29
 * move the cargo from one engine to another if possible
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    30
 */
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    31
static void MoveVehicleCargo(Vehicle *dest, Vehicle *source)
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    32
{
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    33
	Vehicle *v = dest;
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    34
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    35
	do {
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    36
		do {
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    37
			if (source->cargo_type != dest->cargo_type)
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4404
diff changeset
    38
				continue; // cargo not compatible
2599
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    39
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
    40
			if (dest->cargo.Count() == dest->cargo_cap)
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4404
diff changeset
    41
				continue; // the destination vehicle is already full
2599
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    42
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
    43
			uint units_moved = min(source->cargo.Count(), dest->cargo_cap - dest->cargo.Count());
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
    44
			source->cargo.MoveTo(&dest->cargo, units_moved);
2599
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    45
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    46
			// copy the age of the cargo
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    47
			dest->day_counter  = source->day_counter;
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    48
			dest->tick_counter = source->tick_counter;
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    49
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9686
diff changeset
    50
		} while (source->cargo.Count() > 0 && (dest = dest->Next()) != NULL);
2599
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    51
		dest = v;
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9686
diff changeset
    52
	} while ((source = source->Next()) != NULL);
5944
756660703241 (svn r8157) -Fix (r3136): moving cargo during auto replaces did not update the cached vehicle weight for trains properly. This caused (in network games) the server to have a different cached vehicle weight than newly joined clients would have, which causes desyncs.
rubidium
parents: 5937
diff changeset
    53
756660703241 (svn r8157) -Fix (r3136): moving cargo during auto replaces did not update the cached vehicle weight for trains properly. This caused (in network games) the server to have a different cached vehicle weight than newly joined clients would have, which causes desyncs.
rubidium
parents: 5937
diff changeset
    54
	/*
756660703241 (svn r8157) -Fix (r3136): moving cargo during auto replaces did not update the cached vehicle weight for trains properly. This caused (in network games) the server to have a different cached vehicle weight than newly joined clients would have, which causes desyncs.
rubidium
parents: 5937
diff changeset
    55
	 * The of the train will be incorrect at this moment. This is due
756660703241 (svn r8157) -Fix (r3136): moving cargo during auto replaces did not update the cached vehicle weight for trains properly. This caused (in network games) the server to have a different cached vehicle weight than newly joined clients would have, which causes desyncs.
rubidium
parents: 5937
diff changeset
    56
	 * to the fact that removing the old wagon updates the weight of
756660703241 (svn r8157) -Fix (r3136): moving cargo during auto replaces did not update the cached vehicle weight for trains properly. This caused (in network games) the server to have a different cached vehicle weight than newly joined clients would have, which causes desyncs.
rubidium
parents: 5937
diff changeset
    57
	 * the complete train, which is without the weight of cargo we just
756660703241 (svn r8157) -Fix (r3136): moving cargo during auto replaces did not update the cached vehicle weight for trains properly. This caused (in network games) the server to have a different cached vehicle weight than newly joined clients would have, which causes desyncs.
rubidium
parents: 5937
diff changeset
    58
	 * moved back into some (of the) new wagon(s).
756660703241 (svn r8157) -Fix (r3136): moving cargo during auto replaces did not update the cached vehicle weight for trains properly. This caused (in network games) the server to have a different cached vehicle weight than newly joined clients would have, which causes desyncs.
rubidium
parents: 5937
diff changeset
    59
	 */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9686
diff changeset
    60
	if (dest->type == VEH_TRAIN) TrainConsistChanged(dest->First());
2599
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    61
}
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
    62
4741
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    63
static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, const EngineID engine_type)
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    64
{
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    65
	const Order *o;
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    66
	const Vehicle *u;
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    67
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
    68
	if (v->type == VEH_TRAIN) {
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9686
diff changeset
    69
		u = v->First();
4741
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    70
	} else {
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    71
		u = v;
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    72
	}
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    73
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    74
	FOR_VEHICLE_ORDERS(u, o) {
9869
6404afe43575 (svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents: 9837
diff changeset
    75
		if (!o->IsRefit()) continue;
6404afe43575 (svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents: 9837
diff changeset
    76
		if (!CanRefitTo(v->engine_type, o->GetRefitCargo())) continue;
6404afe43575 (svn r12597) [NoAI] -Sync: with trunk r12501:12596.
rubidium
parents: 9837
diff changeset
    77
		if (!CanRefitTo(engine_type, o->GetRefitCargo())) return false;
4741
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    78
	}
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    79
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    80
	return true;
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    81
}
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    82
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
    83
/**
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
    84
 * Function to find what type of cargo to refit to when autoreplacing
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
    85
 * @param *v Original vehicle, that is being replaced
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
    86
 * @param engine_type The EngineID of the vehicle that is being replaced to
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
    87
 * @return The cargo type to replace to
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
    88
 *    CT_NO_REFIT is returned if no refit is needed
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
    89
 *    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
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
    90
 */
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
    91
static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type)
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
    92
{
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 6590
diff changeset
    93
	CargoID new_cargo_type = GetEngineCargoType(engine_type);
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
    94
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 6590
diff changeset
    95
	if (new_cargo_type == CT_INVALID) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
    96
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    97
	if (v->cargo_cap != 0 && (v->cargo_type == new_cargo_type || CanRefitTo(engine_type, v->cargo_type))) {
4741
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
    98
		if (VerifyAutoreplaceRefitForOrders(v, engine_type)) {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    99
			return v->cargo_type == new_cargo_type ? (CargoID)CT_NO_REFIT : v->cargo_type;
4741
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
   100
		} else {
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
   101
			return CT_INVALID;
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
   102
		}
7e0ebb20950d (svn r6653) -Fix r6624: [autoreplace] autoreplace will no longer replace a vehicle if the old type can refit to the cargo types being used in the refit orders and the new one lacks one or more of those refit capabilities
bjarni
parents: 4739
diff changeset
   103
	}
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   104
	if (v->type != VEH_TRAIN) return CT_INVALID; // We can't refit the vehicle to carry the cargo we want
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   105
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   106
	/* Below this line it's safe to assume that the vehicle in question is a train */
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   107
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   108
	if (v->cargo_cap != 0) return CT_INVALID; // trying to replace a vehicle with cargo capacity into another one with incompatible cargo type
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   109
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   110
	/* the old engine didn't have cargo capacity, but the new one does
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   111
	 * now we will figure out what cargo the train is carrying and refit to fit this */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9686
diff changeset
   112
	v = v->First();
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   113
	do {
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   114
		if (v->cargo_cap == 0) continue;
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   115
		/* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   116
		if (v->cargo_type == new_cargo_type) return CT_NO_REFIT;
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   117
		if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type;
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9686
diff changeset
   118
	} while ((v = v->Next()) != NULL);
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   119
	return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   120
}
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   121
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   122
/** Replaces a vehicle (used to be called autorenew)
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   123
 * This function is only called from MaybeReplaceVehicle()
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   124
 * Must be called with _current_player set to the owner of the vehicle
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   125
 * @param w Vehicle to replace
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   126
 * @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   127
 * @param p The vehicle owner (faster than refinding the pointer)
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   128
 * @param new_engine_type The EngineID to replace to
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   129
 * @return value is cost of the replacement or CMD_ERROR
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   130
 */
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   131
static CommandCost ReplaceVehicle(Vehicle **w, byte flags, Money total_cost, const Player *p, EngineID new_engine_type)
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   132
{
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   133
	CommandCost cost;
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   134
	CommandCost sell_value;
2599
7dd6060f9749 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2592
diff changeset
   135
	Vehicle *old_v = *w;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   136
	const UnitID cached_unitnumber = old_v->unitnumber;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   137
	bool new_front = false;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   138
	Vehicle *new_v = NULL;
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   139
	char *vehicle_name = NULL;
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   140
	CargoID replacement_cargo_type;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   141
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   142
	replacement_cargo_type = GetNewCargoTypeForReplace(old_v, new_engine_type);
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   143
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   144
	/* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo type */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   145
	if (replacement_cargo_type == CT_INVALID) return CommandCost();
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   146
6043
99c8fb13658b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 6014
diff changeset
   147
	sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, GetCmdSellVeh(old_v));
4728
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   148
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   149
	/* We give the player a loan of the same amount as the sell value.
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   150
	 * This is needed in case he needs the income from the sale to build the new vehicle.
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   151
	 * We take it back if building fails or when we really sell the old engine */
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   152
	SubtractMoneyFromPlayer(sell_value);
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   153
6043
99c8fb13658b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 6014
diff changeset
   154
	cost = DoCommand(old_v->tile, new_engine_type, 3, flags, GetCmdBuildVeh(old_v));
4728
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   155
	if (CmdFailed(cost)) {
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   156
		/* Take back the money we just gave the player */
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   157
		sell_value.MultiplyCost(-1);
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   158
		SubtractMoneyFromPlayer(sell_value);
4728
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   159
		return cost;
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   160
	}
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   161
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   162
	if (replacement_cargo_type != CT_NO_REFIT) {
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   163
		/* add refit cost */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   164
		CommandCost refit_cost = GetRefitCost(new_engine_type);
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   165
		if (old_v->type == VEH_TRAIN && RailVehInfo(new_engine_type)->railveh_type == RAILVEH_MULTIHEAD) {
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   166
			/* Since it's a dualheaded engine we have to pay once more because the rear end is being refitted too. */
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   167
			refit_cost.AddCost(refit_cost);
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   168
		}
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   169
		cost.AddCost(refit_cost);
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   170
	}
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   171
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   172
	if (flags & DC_EXEC) {
2564
371e0fa97e1e (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2563
diff changeset
   173
		new_v = GetVehicle(_new_vehicle_id);
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4404
diff changeset
   174
		*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
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   175
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   176
		/* refit if needed */
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   177
		if (replacement_cargo_type != CT_NO_REFIT) {
6043
99c8fb13658b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 6014
diff changeset
   178
			if (CmdFailed(DoCommand(0, new_v->index, replacement_cargo_type, DC_EXEC, GetCmdRefitVeh(new_v)))) {
4613
8b561558309f (svn r6468) -Codechange r6464: now use error() when failing refit in autoreplace to ensure that such a failure will not break a game
bjarni
parents: 4611
diff changeset
   179
				/* Being here shows a failure, which most likely is in GetNewCargoTypeForReplace() or incorrect estimation costs */
8b561558309f (svn r6468) -Codechange r6464: now use error() when failing refit in autoreplace to ensure that such a failure will not break a game
bjarni
parents: 4611
diff changeset
   180
				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);
4611
bb035900cae5 (svn r6464) -Fix r6393: killed a warning when compiling without asserts
bjarni
parents: 4604
diff changeset
   181
			}
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   182
		}
4611
bb035900cae5 (svn r6464) -Fix r6393: killed a warning when compiling without asserts
bjarni
parents: 4604
diff changeset
   183
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9703
diff changeset
   184
		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()))) {
3896
292881445b9c (svn r4967) -Fix: [clone vehicles] a cloned train engine heads the same way as the original (pointed out by bobingabout)
bjarni
parents: 3881
diff changeset
   185
			// we are autorenewing to a single engine, so we will turn it as the old one was turned as well
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9703
diff changeset
   186
			SetBit(new_v->u.rail.flags, VRF_REVERSE_DIRECTION);
3896
292881445b9c (svn r4967) -Fix: [clone vehicles] a cloned train engine heads the same way as the original (pointed out by bobingabout)
bjarni
parents: 3881
diff changeset
   187
		}
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   188
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   189
		if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_v)) {
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   190
			/* this is a railcar. We need to move the car into the train
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   191
			 * We add the new engine after the old one instead of replacing it. It will give the same result anyway when we
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   192
			 * sell the old engine in a moment
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   193
			 */
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   194
			/* Get the vehicle in front of the one we move out */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9686
diff changeset
   195
			Vehicle *front = old_v->Previous();
9826
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   196
			if (front == NULL) {
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   197
				/* It would appear that we have the front wagon of a row of wagons without engines */
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   198
				Vehicle *next = old_v->Next();
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   199
				if (next != NULL) {
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   200
					/* Move the chain to the new front wagon */
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   201
					DoCommand(0, (new_v->index << 16) | next->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   202
				}
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   203
			} else {
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   204
				/* 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 */
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   205
				if (IsRearDualheaded(front)) front = front->Previous();
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   206
				/* Now we move the old one out of the train */
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   207
				DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   208
				/* Add the new vehicle */
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   209
				CommandCost tmp_move = DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   210
				if (CmdFailed(tmp_move)) {
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   211
					cost.AddCost(tmp_move);
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   212
					DoCommand(0, new_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN));
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   213
				}
9826
9707ad4c9b60 (svn r12462) [NoAI] -Sync: with trunk r12304:12461.
rubidium
parents: 9800
diff changeset
   214
			}
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   215
		} else {
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   216
			// copy/clone the orders
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   217
			DoCommand(0, (old_v->index << 16) | new_v->index, old_v->IsOrderListShared() ? CO_SHARE : CO_COPY, DC_EXEC, CMD_CLONE_ORDER);
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   218
			new_v->cur_order_index = old_v->cur_order_index;
10645
8cbdb511a674 (svn r13189) [NoAI] -Sync: with trunk r13055:13185.
glx
parents: 10513
diff changeset
   219
			ChangeVehicleViewWindow(old_v->index, new_v->index);
2575
44f0dd3b4a97 (svn r3112) -Feature: [autoreplace] profit counters are now remembered too (request by Darkvater)
bjarni
parents: 2574
diff changeset
   220
			new_v->profit_this_year = old_v->profit_this_year;
44f0dd3b4a97 (svn r3112) -Feature: [autoreplace] profit counters are now remembered too (request by Darkvater)
bjarni
parents: 2574
diff changeset
   221
			new_v->profit_last_year = old_v->profit_last_year;
3679
8e7e14ee361a (svn r4599) -Fix: [Cloning, autoreplace] FS#141 clone service-interval
bjarni
parents: 3657
diff changeset
   222
			new_v->service_interval = old_v->service_interval;
9624
b71483f2330f (svn r9915) [NoAI] -Sync with trunk -r9815:9914
glx
parents: 9620
diff changeset
   223
			DoCommand(0, old_v->group_id, new_v->index, flags, CMD_ADD_VEHICLE_GROUP);
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   224
			new_front = true;
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4404
diff changeset
   225
			new_v->unitnumber = old_v->unitnumber; // use the same unit number
9620
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
   226
			new_v->dest_tile  = old_v->dest_tile;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   227
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   228
			new_v->current_order = old_v->current_order;
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   229
			if (old_v->type == VEH_TRAIN && GetNextVehicle(old_v) != NULL){
2842
48ac5c48f78a (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   230
				Vehicle *temp_v = GetNextVehicle(old_v);
48ac5c48f78a (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   231
48ac5c48f78a (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   232
				// move the entire train to the new engine, excluding the old engine
48ac5c48f78a (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   233
				if (IsMultiheaded(old_v) && temp_v == old_v->u.rail.other_multiheaded_part) {
48ac5c48f78a (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   234
					// we got front and rear of a multiheaded engine right after each other. We should work with the next in line instead
48ac5c48f78a (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   235
					temp_v = GetNextVehicle(temp_v);
48ac5c48f78a (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   236
				}
48ac5c48f78a (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   237
48ac5c48f78a (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   238
				if (temp_v != NULL) {
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   239
					CommandCost tmp_move = DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   240
					if (CmdFailed(tmp_move)) {
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   241
						cost.AddCost(tmp_move);
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   242
						DoCommand(0, temp_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN));
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   243
					}
2681
ac4d59ac8651 (svn r3223) -Fix: [autoreplace] fixed crash when replacing a train engine without any cars (introduced in r3220)
bjarni
parents: 2678
diff changeset
   244
				}
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   245
			}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   246
		}
10513
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   247
		if (CmdSucceeded(cost)) {
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   248
			/* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   249
			MoveVehicleCargo(new_v->type == VEH_TRAIN ? new_v->First() : new_v, old_v);
2716
bc7847ae0e43 (svn r3261) - Autoreplace: If a replaced vehicle had a custom name, transfer it to the new vehicle. Fixes "[ 1370039 ] Autoreplaced vehicles lose their names"
peter1138
parents: 2704
diff changeset
   250
10513
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   251
			/* Get the name of the old vehicle if it has a custom name. */
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   252
			if (old_v->name != NULL) vehicle_name = strdup(old_v->name);
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   253
		}
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   254
	} else { // flags & DC_EXEC not set
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   255
		CommandCost tmp_move;
9703
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   256
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   257
		if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v)) {
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   258
			Vehicle *next_veh = GetNextUnit(old_v); // don't try to move the rear multiheaded engine or articulated parts
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   259
			if (next_veh != NULL) {
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   260
				/* Verify that the wagons can be placed on the engine in question.
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   261
				 * This is done by building an engine, test if the wagons can be added and then sell the test engine. */
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   262
				DoCommand(old_v->tile, new_engine_type, 3, DC_EXEC, GetCmdBuildVeh(old_v));
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   263
				Vehicle *temp = GetVehicle(_new_vehicle_id);
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   264
				tmp_move = DoCommand(0, (temp->index << 16) | next_veh->index, 1, 0, CMD_MOVE_RAIL_VEHICLE);
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   265
				DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v));
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   266
			}
9626
79f2b5a0cdd7 (svn r10118) [NoAI] -Sync with trunk r10015:r10096
glx
parents: 9625
diff changeset
   267
		}
79f2b5a0cdd7 (svn r10118) [NoAI] -Sync with trunk r10015:r10096
glx
parents: 9625
diff changeset
   268
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   269
		/* Ensure that the player will not end up having negative money while autoreplacing
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   270
		 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   271
		if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) {
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   272
			/* Pay back the loan */
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   273
			sell_value.MultiplyCost(-1);
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   274
			SubtractMoneyFromPlayer(sell_value);
4728
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   275
			return CMD_ERROR;
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   276
		}
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   277
	}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   278
4728
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   279
	/* Take back the money we just gave the player just before building the vehicle
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   280
	 * The player will get the same amount now that the sale actually takes place */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   281
	sell_value.MultiplyCost(-1);
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   282
	SubtractMoneyFromPlayer(sell_value);
4728
cdc127bf9ac4 (svn r6640) -Fix: [autoreplace] autoreplace can now use the money for selling the old vehicle to build the new one
bjarni
parents: 4725
diff changeset
   283
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   284
	/* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9626
diff changeset
   285
	cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v)));
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   286
10513
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   287
	if (CmdFailed(cost)) return cost;
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   288
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   289
	if (new_front) {
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   290
		/* now we assign the old unitnumber to the new vehicle */
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   291
		new_v->unitnumber = cached_unitnumber;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   292
	}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   293
4554
ff008b423676 (svn r6393) -Fix: [autoreplace] now refit costs are added to the estimated costs (could spend more than allowed when estimate and actual cost were not the same)
bjarni
parents: 4551
diff changeset
   294
	/* Transfer the name of the old vehicle */
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   295
	if ((flags & DC_EXEC) && vehicle_name != NULL) {
2716
bc7847ae0e43 (svn r3261) - Autoreplace: If a replaced vehicle had a custom name, transfer it to the new vehicle. Fixes "[ 1370039 ] Autoreplaced vehicles lose their names"
peter1138
parents: 2704
diff changeset
   296
		_cmd_text = vehicle_name;
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3490
diff changeset
   297
		DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE);
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
   298
		free(vehicle_name);
2716
bc7847ae0e43 (svn r3261) - Autoreplace: If a replaced vehicle had a custom name, transfer it to the new vehicle. Fixes "[ 1370039 ] Autoreplaced vehicles lose their names"
peter1138
parents: 2704
diff changeset
   299
	}
bc7847ae0e43 (svn r3261) - Autoreplace: If a replaced vehicle had a custom name, transfer it to the new vehicle. Fixes "[ 1370039 ] Autoreplaced vehicles lose their names"
peter1138
parents: 2704
diff changeset
   300
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   301
	return cost;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   302
}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   303
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   304
/** Removes wagons from a train until it get a certain length
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   305
 * @param v The vehicle
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   306
 * @param old_total_length The wanted max length
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   307
 * @return The profit from selling the wagons
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   308
 */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   309
static CommandCost WagonRemoval(Vehicle *v, uint16 old_total_length)
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   310
{
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   311
	if (v->type != VEH_TRAIN) return CommandCost();
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   312
	Vehicle *front = v;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   313
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   314
	CommandCost cost = CommandCost();
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   315
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   316
	while (front->u.rail.cached_total_length > old_total_length) {
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   317
		/* the train is too long. We will remove cars one by one from the start of the train until it's short enough */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   318
		while (v != NULL && RailVehInfo(v->engine_type)->railveh_type != RAILVEH_WAGON) {
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   319
			/* We move backwards in the train until we find a wagon */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   320
			v = GetNextVehicle(v);
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   321
		}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   322
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   323
		if (v == NULL) {
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   324
			/* We sold all the wagons and the train is still not short enough */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   325
			SetDParam(0, front->unitnumber);
10645
8cbdb511a674 (svn r13189) [NoAI] -Sync: with trunk r13055:13185.
glx
parents: 10513
diff changeset
   326
			AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NS_ADVICE, front->index, 0);
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   327
			return cost;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   328
		}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   329
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   330
		/* We found a wagon we can sell */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   331
		Vehicle *temp = v;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   332
		v = GetNextVehicle(v);
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   333
		DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); // remove the wagon from the train
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   334
		MoveVehicleCargo(front, temp); // move the cargo back on the train
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   335
		cost.AddCost(DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON)); // sell the wagon
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   336
	}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   337
	return cost;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   338
}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   339
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   340
/** Get the EngineID of the replacement for a vehicle
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   341
 * @param v The vehicle to find a replacement for
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   342
 * @param p The vehicle's owner (it's faster to forward the pointer than refinding it)
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   343
 * @return the EngineID of the replacement. INVALID_ENGINE if no buildable replacement is found
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   344
 */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   345
static EngineID GetNewEngineType(const Vehicle *v, const Player *p)
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   346
{
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   347
	if (v->type == VEH_TRAIN && IsRearDualheaded(v)) {
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   348
		/* we build the rear ends of multiheaded trains with the front ones */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   349
		return INVALID_ENGINE;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   350
	}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   351
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   352
	EngineID e = EngineReplacementForPlayer(p, v->engine_type, v->group_id);
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   353
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   354
	if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_player)) {
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   355
		return e;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   356
	}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   357
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   358
	if (v->NeedsAutorenewing(p) && // replace if engine is too old
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   359
	    IsEngineBuildable(v->engine_type, v->type, _current_player)) { // engine can still be build
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   360
		return v->engine_type;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   361
	}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   362
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   363
	return INVALID_ENGINE;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   364
}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   365
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   366
/** replaces a vehicle if it's set for autoreplace or is too old
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   367
 * (used to be called autorenew)
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   368
 * @param v The vehicle to replace
4434
4175805666a5 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4404
diff changeset
   369
 * if the vehicle is a train, v needs to be the front engine
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   370
 * @param flags
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   371
 * @param display_costs If set, a cost animation is shown (only if DC_EXEC is set)
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   372
 *        This bool also takes autorenew money into consideration
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   373
 * @return the costs, the success bool and sometimes an error message
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   374
 */
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   375
CommandCost MaybeReplaceVehicle(Vehicle *v, uint32 flags, bool display_costs)
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   376
{
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   377
	Vehicle *w;
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   378
	Player *p = GetPlayer(v->owner);
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   379
	CommandCost cost;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   380
	bool stopped = false;
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   381
	BackuppedVehicle backup(true);
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   382
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   383
	/* We only want "real" vehicle types. */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   384
	assert(IsPlayerBuildableVehicleType(v));
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   385
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   386
	/* Ensure that this bool is cleared. */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   387
	assert(!v->leave_depot_instantly);
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   388
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   389
	/* We can't sell if the current player don't own the vehicle. */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   390
	assert(v->owner == _current_player);
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   391
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   392
	if (!v->IsInDepot()) {
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   393
		/* The vehicle should be inside the depot */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   394
		switch (v->type) {
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   395
			default: NOT_REACHED();
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   396
			case VEH_TRAIN:    return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED); break;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   397
			case VEH_ROAD:     return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);     break;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   398
			case VEH_SHIP:     return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN);    break;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   399
			case VEH_AIRCRAFT: return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED);   break;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   400
		}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   401
	}
2805
b6098e7ca457 (svn r3353) Simplify the automatic length adjustment algorithm for replacing trains: Use the length of the train before the replacement as reference length
tron
parents: 2804
diff changeset
   402
b6098e7ca457 (svn r3353) Simplify the automatic length adjustment algorithm for replacing trains: Use the length of the train before the replacement as reference length
tron
parents: 2804
diff changeset
   403
	/* Remember the length in case we need to trim train later on
3175
bc706c6deaee (svn r3811) -Fix: [autoreplace]: (FS#67) autoreplacing trains now keep their tile length instead of their pixel length
bjarni
parents: 3173
diff changeset
   404
	 * If it's not a train, the value is unused
bc706c6deaee (svn r3811) -Fix: [autoreplace]: (FS#67) autoreplacing trains now keep their tile length instead of their pixel length
bjarni
parents: 3173
diff changeset
   405
	 * round up to the length of the tiles used for the train instead of the train length instead
bc706c6deaee (svn r3811) -Fix: [autoreplace]: (FS#67) autoreplacing trains now keep their tile length instead of their pixel length
bjarni
parents: 3173
diff changeset
   406
	 * Useful when newGRF uses custom length */
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   407
	uint16 old_total_length = (v->type == VEH_TRAIN ?
3422
12cdb13ddb56 (svn r4249) -Codechange: Replace more occurences of 16 by TILE_SIZE and of 8 by TILE_HEIGHT. Reverted one change from the previous commit because it was faulty
celestar
parents: 3421
diff changeset
   408
		(v->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE :
12cdb13ddb56 (svn r4249) -Codechange: Replace more occurences of 16 by TILE_SIZE and of 8 by TILE_HEIGHT. Reverted one change from the previous commit because it was faulty
celestar
parents: 3421
diff changeset
   409
		-1
12cdb13ddb56 (svn r4249) -Codechange: Replace more occurences of 16 by TILE_SIZE and of 8 by TILE_HEIGHT. Reverted one change from the previous commit because it was faulty
celestar
parents: 3421
diff changeset
   410
	);
3175
bc706c6deaee (svn r3811) -Fix: [autoreplace]: (FS#67) autoreplacing trains now keep their tile length instead of their pixel length
bjarni
parents: 3173
diff changeset
   411
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   412
	if (!(v->vehstatus & VS_STOPPED)) {
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   413
		/* The vehicle is moving so we better stop it before we might alter consist or sell it */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   414
		v->vehstatus |= VS_STOPPED;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   415
		/* Remember that we stopped the vehicle */
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   416
		stopped = true;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   417
	}
2590
5603f43c8cd3 (svn r3127) -Fix: [autoreplace] fixed a condition where a vehicle could fail to stop when autoreplacing
bjarni
parents: 2579
diff changeset
   418
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   419
	{
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
   420
		cost = CommandCost(EXPENSES_NEW_VEHICLES);
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   421
		w = v;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   422
		do {
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   423
			EngineID new_engine = GetNewEngineType(w, p);
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   424
			if (new_engine == INVALID_ENGINE) continue;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   425
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   426
			if (!backup.ContainsBackup()) {
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   427
				/* We are going to try to replace a vehicle but we don't have any backup so we will make one. */
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   428
				backup.Backup(v, p);
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   429
			}
10513
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   430
			/* Now replace the vehicle.
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   431
			 * First we need to cache if it's the front vehicle as we need to update the v pointer if it is.
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   432
			 * If the replacement fails then we can't trust the data in the vehicle hence the reason to cache the result. */
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   433
			bool IsFront = w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE;
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   434
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   435
			cost.AddCost(ReplaceVehicle(&w, DC_EXEC, cost.GetCost(), p, new_engine));
9626
79f2b5a0cdd7 (svn r10118) [NoAI] -Sync with trunk r10015:r10096
glx
parents: 9625
diff changeset
   436
10513
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   437
			if (IsFront) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   438
				/* now we bought a new engine and sold the old one. We need to fix the
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   439
				 * pointers in order to avoid pointing to the old one for trains: these
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   440
				 * pointers should point to the front engine and not the cars
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   441
				 */
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   442
				v = w;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   443
			}
10513
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   444
		} while (CmdSucceeded(cost) && w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL);
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   445
10513
33cb70ff2f5d (svn r13056) [NoAI] -Sync: with trunk r12996:13055.
rubidium
parents: 10455
diff changeset
   446
		if (CmdSucceeded(cost) && v->type == VEH_TRAIN && p->renew_keep_length) {
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   447
			/* Remove wagons until the wanted length is reached */
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   448
			cost.AddCost(WagonRemoval(v, old_total_length));
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   449
		}
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   450
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   451
		if (flags & DC_QUERY_COST || cost.GetCost() == 0) {
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   452
			/* We didn't do anything during the replace so we will just exit here */
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   453
			v = backup.Restore(v, p);
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   454
			if (stopped) v->vehstatus &= ~VS_STOPPED;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   455
			return cost;
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   456
		}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   457
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   458
		if (display_costs) {
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   459
			/* We want to ensure that we will not get below p->engine_renew_money.
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   460
			 * We will not actually pay this amount. It's for display and checks only. */
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   461
			CommandCost tmp = cost;
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   462
			tmp.AddCost((Money)p->engine_renew_money);
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   463
			if (CmdSucceeded(tmp) && GetAvailableMoneyForCommand() < tmp.GetCost()) {
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   464
				/* We don't have enough money so we will set cost to failed */
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   465
				cost.AddCost((Money)p->engine_renew_money);
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   466
				cost.AddCost(CMD_ERROR);
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   467
			}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   468
		}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   469
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   470
		if (display_costs && CmdFailed(cost)) {
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   471
			if (GetAvailableMoneyForCommand() < cost.GetCost() && IsLocalPlayer()) {
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   472
				StringID message;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   473
				SetDParam(0, v->unitnumber);
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   474
				switch (v->type) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   475
					case VEH_TRAIN:    message = STR_TRAIN_AUTORENEW_FAILED;       break;
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   476
					case VEH_ROAD:     message = STR_ROADVEHICLE_AUTORENEW_FAILED; break;
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   477
					case VEH_SHIP:     message = STR_SHIP_AUTORENEW_FAILED;        break;
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   478
					case VEH_AIRCRAFT: message = STR_AIRCRAFT_AUTORENEW_FAILED;    break;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   479
						// This should never happen
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   480
					default: NOT_REACHED(); message = 0; break;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   481
				}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   482
10645
8cbdb511a674 (svn r13189) [NoAI] -Sync: with trunk r13055:13185.
glx
parents: 10513
diff changeset
   483
				AddNewsItem(message, NS_ADVICE, v->index, 0);
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   484
			}
2617
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   485
		}
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   486
	}
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   487
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   488
	if (display_costs && IsLocalPlayer() && (flags & DC_EXEC) && CmdSucceeded(cost)) {
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   489
		ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost());
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   490
	}
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   491
10455
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   492
	if (!(flags & DC_EXEC) || CmdFailed(cost)) {
22c441f5adf9 (svn r12997) [NoAI] -Sync: with trunk r12895:12996.
rubidium
parents: 10294
diff changeset
   493
		v = backup.Restore(v, p);
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   494
	}
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   495
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   496
	/* Start the vehicle if we stopped it earlier */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   497
	if (stopped) v->vehstatus &= ~VS_STOPPED;
10294
7798ae816af8 (svn r12826) [NoAI] -Sync: with trunk r12780:12824
glx
parents: 10249
diff changeset
   498
4662
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   499
	return cost;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   500
}