src/autoreplace_cmd.cpp
author convert-repo
Mon, 07 Apr 2008 16:21:55 +0000
changeset 10076 dfd70e42c4ae
parent 9334 db2f5161fcba
child 10222 b6919c94cc77
permissions -rw-r--r--
update tags
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
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1881
diff changeset
     4
#include "openttd.h"
3959
e2bbaa8d4978 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3957
diff changeset
     5
#include "roadveh.h"
3961
e2783f244ac6 (svn r5120) Add IsShipInDepot{Stopped,}() and remove some redundant checks
tron
parents: 3959
diff changeset
     6
#include "ship.h"
9259
088d3649dd4f (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 9232
diff changeset
     7
#include "news_func.h"
8750
fdd6054e7bae (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8726
diff changeset
     8
#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
     9
#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
    10
#include "vehicle_gui.h"
1758
68668114f92e (svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents: 1757
diff changeset
    11
#include "depot.h"
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
    12
#include "train.h"
4662
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
    13
#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
    14
#include "cargotype.h"
7139
4ae3ab180d05 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 7047
diff changeset
    15
#include "group.h"
8610
17cc343a23dd (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8560
diff changeset
    16
#include "strings_func.h"
8612
6414fc21c2f3 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8610
diff changeset
    17
#include "command_func.h"
8640
1e93b81e96d2 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8627
diff changeset
    18
#include "vehicle_func.h"
8627
448ebf3a8291 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8612
diff changeset
    19
#include "functions.h"
8707
55835d8fbfcd (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8640
diff changeset
    20
#include "variables.h"
8708
0c29fbc79be4 (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium
parents: 8707
diff changeset
    21
#include "autoreplace_func.h"
8859
c5a71ed49252 (svn r11929) -Fix (r9981)[FS#1624]: [autoreplace] fixed a case where a single headed locomotive caused an assert when being replaced to a dualheaded one
bjarni
parents: 8858
diff changeset
    22
#include "articulated_vehicles.h"
2244
e6b5ef68406d (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
    23
8760
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8754
diff changeset
    24
#include "table/strings.h"
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8754
diff changeset
    25
9124
eea8af2b73a8 (svn r12230) -Codechange: [autoreplace] made a function to detect if a vehicle needs autorenewing
bjarni
parents: 8965
diff changeset
    26
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
    27
/*
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
 * 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
    29
 */
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
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
    31
{
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
	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
    33
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
	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
    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
			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
    37
				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
    38
7506
e52d89f5c7c1 (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7486
diff changeset
    39
			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
    40
				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
    41
7506
e52d89f5c7c1 (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7486
diff changeset
    42
			uint units_moved = min(source->cargo.Count(), dest->cargo_cap - dest->cargo.Count());
e52d89f5c7c1 (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 7486
diff changeset
    43
			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
    44
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
			// 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
    46
			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
    47
			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
    48
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7754
diff changeset
    49
		} 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
    50
		dest = v;
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7754
diff changeset
    51
	} 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
    52
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
	 * 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
    55
	 * 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
    56
	 * 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
    57
	 * 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
    58
	 */
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7988
diff changeset
    59
	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
    60
}
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
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
    62
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
    63
{
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
	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
    65
	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
    66
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
    67
	if (v->type == VEH_TRAIN) {
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7988
diff changeset
    68
		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
    69
	} 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
    70
		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
    71
	}
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
	FOR_VEHICLE_ORDERS(u, o) {
9334
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9282
diff changeset
    74
		if (!o->IsRefit()) continue;
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9282
diff changeset
    75
		if (!CanRefitTo(v->engine_type, o->GetRefitCargo())) continue;
db2f5161fcba (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 9282
diff changeset
    76
		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
    77
	}
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
	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
    80
}
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
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
    82
/**
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
 * 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
    84
 * @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
    85
 * @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
    86
 * @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
    87
 *    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
    88
 *    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
    89
 */
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
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
    91
{
7001
d5f19455d650 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6987
diff changeset
    92
	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
    93
7001
d5f19455d650 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6987
diff changeset
    94
	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
    95
8858
576be95ed078 (svn r11928) -Fix (r6393): [autoreplace] autoreplace could refit train engines to the wrong cargo type if the old engine had no cargo capacity and the new one had
bjarni
parents: 8760
diff changeset
    96
	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
    97
		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
    98
			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
    99
		} 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
   100
			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
   101
		}
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
	}
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
   103
	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
   104
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
	/* 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
   106
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
	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
   108
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
	/* 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
   110
	 * now we will figure out what cargo the train is carrying and refit to fit this */
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7988
diff changeset
   111
	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
   112
	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
   113
		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
   114
		/* 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
   115
		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
   116
		if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type;
7988
6075538f6111 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7754
diff changeset
   117
	} 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
   118
	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
   119
}
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
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   121
/* Replaces a vehicle (used to be called autorenew)
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   122
 * This function is only called from MaybeReplaceVehicle()
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   123
 * 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
   124
 * @param w Vehicle to replace
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   125
 * @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   126
 * @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
   127
 */
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7448
diff changeset
   128
static CommandCost ReplaceVehicle(Vehicle **w, byte flags, Money total_cost)
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   129
{
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7296
diff changeset
   130
	CommandCost cost;
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7296
diff changeset
   131
	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
   132
	Vehicle *old_v = *w;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   133
	const Player *p = GetPlayer(old_v->owner);
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   134
	EngineID new_engine_type;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   135
	const UnitID cached_unitnumber = old_v->unitnumber;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   136
	bool new_front = false;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   137
	Vehicle *new_v = NULL;
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
   138
	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
   139
	CargoID replacement_cargo_type;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   140
9176
676d04b1e38a (svn r12346) -Fix [FS#1748, FS#1825](r9874, r11872): Remove duplicated and inconsistent code wrt. autoreplace with rules in both vehicles' group and ALL_GROUP.
frosch
parents: 9125
diff changeset
   141
	/* Check if there is a autoreplacement set for the vehicle */
676d04b1e38a (svn r12346) -Fix [FS#1748, FS#1825](r9874, r11872): Remove duplicated and inconsistent code wrt. autoreplace with rules in both vehicles' group and ALL_GROUP.
frosch
parents: 9125
diff changeset
   142
	new_engine_type = EngineReplacementForPlayer(p, old_v->engine_type, old_v->group_id);
676d04b1e38a (svn r12346) -Fix [FS#1748, FS#1825](r9874, r11872): Remove duplicated and inconsistent code wrt. autoreplace with rules in both vehicles' group and ALL_GROUP.
frosch
parents: 9125
diff changeset
   143
	/* if not, just renew to the same type */
7146
f51e7252112d (svn r9881) -Fix (FS#782, r9874): accidentally removed one condition too many causing asserts.
rubidium
parents: 7143
diff changeset
   144
	if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type;
f51e7252112d (svn r9881) -Fix (FS#782, r9874): accidentally removed one condition too many causing asserts.
rubidium
parents: 7143
diff changeset
   145
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
	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
   147
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
   148
	/* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo type */
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   149
	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
   150
6043
99c8fb13658b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 6014
diff changeset
   151
	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
   152
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
	/* 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
   154
	 * 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
   155
	 * 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
   156
	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
   157
6043
99c8fb13658b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 6014
diff changeset
   158
	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
   159
	if (CmdFailed(cost)) {
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   160
		/* Take back the money we just gave the player */
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   161
		sell_value.MultiplyCost(-1);
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   162
		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
   163
		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
   164
	}
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   165
7242
d72363a4042d (svn r9981) -Fix: fixed a rare event that could cause autoreplace to run out of money and generate an error (spotted by elmex and UndernotBuilder)
bjarni
parents: 7231
diff changeset
   166
	if (replacement_cargo_type != CT_NO_REFIT) {
d72363a4042d (svn r9981) -Fix: fixed a rare event that could cause autoreplace to run out of money and generate an error (spotted by elmex and UndernotBuilder)
bjarni
parents: 7231
diff changeset
   167
		/* add refit cost */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7296
diff changeset
   168
		CommandCost refit_cost = GetRefitCost(new_engine_type);
8859
c5a71ed49252 (svn r11929) -Fix (r9981)[FS#1624]: [autoreplace] fixed a case where a single headed locomotive caused an assert when being replaced to a dualheaded one
bjarni
parents: 8858
diff changeset
   169
		if (old_v->type == VEH_TRAIN && RailVehInfo(new_engine_type)->railveh_type == RAILVEH_MULTIHEAD) {
c5a71ed49252 (svn r11929) -Fix (r9981)[FS#1624]: [autoreplace] fixed a case where a single headed locomotive caused an assert when being replaced to a dualheaded one
bjarni
parents: 8858
diff changeset
   170
			/* Since it's a dualheaded engine we have to pay once more because the rear end is being refitted too. */
c5a71ed49252 (svn r11929) -Fix (r9981)[FS#1624]: [autoreplace] fixed a case where a single headed locomotive caused an assert when being replaced to a dualheaded one
bjarni
parents: 8858
diff changeset
   171
			refit_cost.AddCost(refit_cost);
c5a71ed49252 (svn r11929) -Fix (r9981)[FS#1624]: [autoreplace] fixed a case where a single headed locomotive caused an assert when being replaced to a dualheaded one
bjarni
parents: 8858
diff changeset
   172
		}
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   173
		cost.AddCost(refit_cost);
7242
d72363a4042d (svn r9981) -Fix: fixed a rare event that could cause autoreplace to run out of money and generate an error (spotted by elmex and UndernotBuilder)
bjarni
parents: 7231
diff changeset
   174
	}
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
   175
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   176
	if (flags & DC_EXEC) {
2564
371e0fa97e1e (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2563
diff changeset
   177
		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
   178
		*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
   179
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   180
		/* 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
   181
		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
   182
			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
   183
				/* 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
   184
				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
   185
			}
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   186
		}
4611
bb035900cae5 (svn r6464) -Fix r6393: killed a warning when compiling without asserts
bjarni
parents: 4604
diff changeset
   187
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8023
diff changeset
   188
		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
   189
			// we are autorenewing to a single engine, so we will turn it as the old one was turned as well
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
   190
			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
   191
		}
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   192
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
   193
		if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_v)) {
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   194
			/* 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
   195
			 * 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
   196
			 * sell the old engine in a moment
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   197
			 */
7231
1fbf0b9881e9 (svn r9967) -Fix (r9938): autoreplace would in certain conditions move dualheaded engines in a train (usually to the rear)
bjarni
parents: 7202
diff changeset
   198
			/* Get the vehicle in front of the one we move out */
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7988
diff changeset
   199
			Vehicle *front = old_v->Previous();
9232
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   200
			if (front == NULL) {
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   201
				/* It would appear that we have the front wagon of a row of wagons without engines */
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   202
				Vehicle *next = old_v->Next();
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   203
				if (next != NULL) {
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   204
					/* Move the chain to the new front wagon */
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   205
					DoCommand(0, (new_v->index << 16) | next->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   206
				}
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   207
			} else {
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   208
				/* 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 */
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   209
				if (IsRearDualheaded(front)) front = front->Previous();
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   210
				/* Now we move the old one out of the train */
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   211
				DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   212
				/* Add the new vehicle */
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
diff changeset
   213
				DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
37291ba2aa2a (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 9176
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
8965
a145f62a6272 (svn r12040) -Codechange: Change IsOrderListShared from a simple function to a class member(MagicBuzz).
belugas
parents: 8859
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;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   219
			ChangeVehicleViewWindow(old_v, new_v);
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;
7143
59366b8e133b (svn r9878) -Fix (9874): some vehicle count's were not properly updated on delete or autoreplace of vehicles.
rubidium
parents: 7139
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
7047
578d7e38b601 (svn r9753) -Fix [FS#732]: trains are lost after autorenewal/autoreplace
bjarni
parents: 7001
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) {
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
   239
					DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
2681
ac4d59ac8651 (svn r3223) -Fix: [autoreplace] fixed crash when replacing a train engine without any cars (introduced in r3220)
bjarni
parents: 2678
diff changeset
   240
				}
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   241
			}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   242
		}
2678
a0af6bded583 (svn r3220) -Fix: [autoreplace] Autoreplaced trains now replace their cargo to the train instead of just the new engine
bjarni
parents: 2677
diff changeset
   243
		/* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7988
diff changeset
   244
		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
   245
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
   246
		// Get the name of the old vehicle if it has a custom name.
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
   247
		if (old_v->name != NULL) vehicle_name = strdup(old_v->name);
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
   248
	} else { // flags & DC_EXEC not set
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   249
		CommandCost tmp_move;
8020
58097db31633 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7993
diff changeset
   250
58097db31633 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7993
diff changeset
   251
		if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v)) {
8023
67fdef8a686e (svn r11046) -Codechange: added function to get the next movable (non-articulated, non-read end of dualheaded engine) vehicle in a train
bjarni
parents: 8022
diff changeset
   252
			Vehicle *next_veh = GetNextUnit(old_v); // don't try to move the rear multiheaded engine or articulated parts
8020
58097db31633 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7993
diff changeset
   253
			if (next_veh != NULL) {
58097db31633 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7993
diff changeset
   254
				/* Verify that the wagons can be placed on the engine in question.
58097db31633 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7993
diff changeset
   255
				 * This is done by building an engine, test if the wagons can be added and then sell the test engine. */
58097db31633 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7993
diff changeset
   256
				DoCommand(old_v->tile, new_engine_type, 3, DC_EXEC, GetCmdBuildVeh(old_v));
58097db31633 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7993
diff changeset
   257
				Vehicle *temp = GetVehicle(_new_vehicle_id);
58097db31633 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7993
diff changeset
   258
				tmp_move = DoCommand(0, (temp->index << 16) | next_veh->index, 1, 0, CMD_MOVE_RAIL_VEHICLE);
58097db31633 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7993
diff changeset
   259
				DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v));
58097db31633 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7993
diff changeset
   260
			}
7296
341c1174517d (svn r10039) -Fix: [autoreplace] when trying to replace an engine that can't carry the wagons the old one carries (GRF restrictoin), then don't try to replace at all
bjarni
parents: 7243
diff changeset
   261
		}
341c1174517d (svn r10039) -Fix: [autoreplace] when trying to replace an engine that can't carry the wagons the old one carries (GRF restrictoin), then don't try to replace at all
bjarni
parents: 7243
diff changeset
   262
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
   263
		/* 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
   264
		 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */
7448
424ab8df8adb (svn r10207) -Codechange: remove the redundant player_money in favour of the money64, which is now renamed to player_money.
rubidium
parents: 7446
diff changeset
   265
		if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) {
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   266
			/* Pay back the loan */
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   267
			sell_value.MultiplyCost(-1);
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   268
			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
   269
			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
   270
		}
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   271
	}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   272
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
   273
	/* 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
   274
	 * The player will get the same amount now that the sale actually takes place */
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   275
	sell_value.MultiplyCost(-1);
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   276
	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
   277
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
   278
	/* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   279
	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
   280
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   281
	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
   282
		/* 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
   283
		new_v->unitnumber = cached_unitnumber;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   284
	}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   285
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
   286
	/* Transfer the name of the old vehicle */
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
   287
	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
   288
		_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
   289
		DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE);
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
   290
		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
   291
	}
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
   292
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   293
	return cost;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   294
}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   295
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   296
/** replaces a vehicle if it's set for autoreplace or is too old
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   297
 * (used to be called autorenew)
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   298
 * @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
   299
 * if the vehicle is a train, v needs to be the front engine
4662
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   300
 * @param check Checks if the replace is valid. No action is done at all
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   301
 * @param display_costs If set, a cost animation is shown (only if check is false)
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   302
 * @return CMD_ERROR if something went wrong. Otherwise the price of the replace
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   303
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7296
diff changeset
   304
CommandCost MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs)
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   305
{
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   306
	Vehicle *w;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   307
	const Player *p = GetPlayer(v->owner);
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   308
	byte flags = 0;
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   309
	CommandCost cost, temp_cost;
5937
edacd293525c (svn r8147) -Fix: [autoreplace] v->leave_depot_instantly was not always reset correctly
bjarni
parents: 5934
diff changeset
   310
	bool stopped;
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
   311
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
   312
	/* 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
   313
	 * 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
   314
	 * 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
   315
	 * 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
   316
	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
   317
		(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
   318
		-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
   319
	);
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
   320
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   321
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   322
	_current_player = v->owner;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   323
6045
89a945121961 (svn r8353) -Cleanup: added IsPlayerBuildableVehicleType() to autoreplace code (duplicated code removal)
bjarni
parents: 6043
diff changeset
   324
	assert(IsPlayerBuildableVehicleType(v));
2579
1662ff19b404 (svn r3116) -Fix: [autoreplace] fixed issue where autorenewing/autoreplacing a plane could lock up an airport
bjarni
parents: 2575
diff changeset
   325
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
   326
	assert(v->vehstatus & VS_STOPPED); // the vehicle should have been stopped in VehicleEnteredDepotThisTick() if needed
2590
5603f43c8cd3 (svn r3127) -Fix: [autoreplace] fixed a condition where a vehicle could fail to stop when autoreplacing
bjarni
parents: 2579
diff changeset
   327
5937
edacd293525c (svn r8147) -Fix: [autoreplace] v->leave_depot_instantly was not always reset correctly
bjarni
parents: 5934
diff changeset
   328
	/* Remember the flag v->leave_depot_instantly because if we replace the vehicle, the vehicle holding this flag will be sold
edacd293525c (svn r8147) -Fix: [autoreplace] v->leave_depot_instantly was not always reset correctly
bjarni
parents: 5934
diff changeset
   329
	 * If it is set, then we only stopped the vehicle to replace it (if needed) and we will need to start it again.
edacd293525c (svn r8147) -Fix: [autoreplace] v->leave_depot_instantly was not always reset correctly
bjarni
parents: 5934
diff changeset
   330
	 * We also need to reset the flag since it should remain false except from when the vehicle enters a depot until autoreplace is handled in the same tick */
edacd293525c (svn r8147) -Fix: [autoreplace] v->leave_depot_instantly was not always reset correctly
bjarni
parents: 5934
diff changeset
   331
	stopped = v->leave_depot_instantly;
edacd293525c (svn r8147) -Fix: [autoreplace] v->leave_depot_instantly was not always reset correctly
bjarni
parents: 5934
diff changeset
   332
	v->leave_depot_instantly = false;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   333
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   334
	for (;;) {
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8708
diff changeset
   335
		cost = CommandCost(EXPENSES_NEW_VEHICLES);
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   336
		w = v;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   337
		do {
8022
32b885de2410 (svn r11045) -Codechange: added a function to tell if a vehicle is the rear part of a dualheaded train engine
bjarni
parents: 8021
diff changeset
   338
			if (w->type == VEH_TRAIN && IsRearDualheaded(w)) {
2676
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   339
				/* we build the rear ends of multiheaded trains with the front ones */
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   340
				continue;
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   341
			}
2ba71e034d97 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   342
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   343
			// check if the vehicle should be replaced
9124
eea8af2b73a8 (svn r12230) -Codechange: [autoreplace] made a function to detect if a vehicle needs autorenewing
bjarni
parents: 8965
diff changeset
   344
			if (!w->NeedsAutorenewing(p) || // replace if engine is too old
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   345
					w->max_age == 0) { // rail cars got a max age of 0
9176
676d04b1e38a (svn r12346) -Fix [FS#1748, FS#1825](r9874, r11872): Remove duplicated and inconsistent code wrt. autoreplace with rules in both vehicles' group and ALL_GROUP.
frosch
parents: 9125
diff changeset
   346
				if (!EngineHasReplacementForPlayer(p, w->engine_type, w->group_id)) continue;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   347
			}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   348
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   349
			/* Now replace the vehicle */
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   350
			temp_cost = ReplaceVehicle(&w, flags, cost.GetCost());
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   351
7296
341c1174517d (svn r10039) -Fix: [autoreplace] when trying to replace an engine that can't carry the wagons the old one carries (GRF restrictoin), then don't try to replace at all
bjarni
parents: 7243
diff changeset
   352
			if (CmdFailed(temp_cost)) break; // replace failed for some reason. Leave the vehicle alone
341c1174517d (svn r10039) -Fix: [autoreplace] when trying to replace an engine that can't carry the wagons the old one carries (GRF restrictoin), then don't try to replace at all
bjarni
parents: 7243
diff changeset
   353
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   354
			if (flags & DC_EXEC &&
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
   355
					(w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE)) {
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   356
				/* 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
   357
				 * pointers in order to avoid pointing to the old one for trains: these
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   358
				 * pointers should point to the front engine and not the cars
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   359
				 */
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   360
				v = w;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   361
			}
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   362
			cost.AddCost(temp_cost);
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
   363
		} while (w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL);
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   364
7448
424ab8df8adb (svn r10207) -Codechange: remove the redundant player_money in favour of the money64, which is now renamed to player_money.
rubidium
parents: 7446
diff changeset
   365
		if (!(flags & DC_EXEC) && (p->player_money < (cost.GetCost() + p->engine_renew_money) || cost.GetCost() == 0)) {
424ab8df8adb (svn r10207) -Codechange: remove the redundant player_money in favour of the money64, which is now renamed to player_money.
rubidium
parents: 7446
diff changeset
   366
			if (!check && p->player_money < (cost.GetCost() + p->engine_renew_money) && ( _local_player == v->owner ) && cost.GetCost() != 0) {
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   367
				StringID message;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   368
				SetDParam(0, v->unitnumber);
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   369
				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
   370
					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
   371
					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
   372
					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
   373
					case VEH_AIRCRAFT: message = STR_AIRCRAFT_AUTORENEW_FAILED;    break;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   374
						// This should never happen
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   375
					default: NOT_REACHED(); message = 0; break;
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
9259
088d3649dd4f (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 9232
diff changeset
   378
				AddNewsItem(message, NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, DNC_NONE, v->index, 0);
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   379
			}
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   380
			if (stopped) v->vehstatus &= ~VS_STOPPED;
4676
e58dba0b7093 (svn r6580) -Fix r6552: [depot window] fixed issue where vehicles sometimes failed to restart after being replaced
bjarni
parents: 4675
diff changeset
   381
			if (display_costs) _current_player = OWNER_NONE;
4662
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   382
			return CMD_ERROR;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   383
		}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   384
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   385
		if (flags & DC_EXEC) {
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
   386
			break; // we are done replacing since the loop ran once with DC_EXEC
4662
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   387
		} else if (check) {
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   388
			/* It's a test only and we know that we can do this
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   389
			 * NOTE: payment for wagon removal is NOT included in this price */
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   390
			return cost;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   391
		}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   392
		// now we redo the loop, but this time we actually do stuff since we know that we can do it
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   393
		flags |= DC_EXEC;
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   394
	}
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   395
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
   396
	/* If setting is on to try not to exceed the old length of the train with the replacement */
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
   397
	if (v->type == VEH_TRAIN && p->renew_keep_length) {
2617
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   398
		Vehicle *temp;
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   399
		w = v;
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
   400
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
   401
		while (v->u.rail.cached_total_length > old_total_length) {
2617
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   402
			// the train is too long. We will remove cars one by one from the start of the train until it's short enough
6119
b47985557d1e (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 6117
diff changeset
   403
			while (w != NULL && RailVehInfo(w->engine_type)->railveh_type != RAILVEH_WAGON) {
2617
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   404
				w = GetNextVehicle(w);
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   405
			}
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   406
			if (w == NULL) {
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   407
				// we failed to make the train short enough
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   408
				SetDParam(0, v->unitnumber);
9259
088d3649dd4f (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 9232
diff changeset
   409
				AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NM_SMALL, NF_VIEWPORT | NF_VEHICLE, NT_ADVICE, DNC_NONE, v->index, 0);
2617
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   410
				break;
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   411
			}
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   412
			temp = w;
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   413
			w = GetNextVehicle(w);
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
   414
			DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
2678
a0af6bded583 (svn r3220) -Fix: [autoreplace] Autoreplaced trains now replace their cargo to the train instead of just the new engine
bjarni
parents: 2677
diff changeset
   415
			MoveVehicleCargo(v, temp);
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   416
			cost.AddCost(DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON));
2617
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   417
		}
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   418
	}
a9e1a187de99 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   419
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   420
	if (stopped) v->vehstatus &= ~VS_STOPPED;
4662
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   421
	if (display_costs) {
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   422
		if (IsLocalPlayer()) ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost());
4662
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   423
		_current_player = OWNER_NONE;
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   424
	}
a20121e5442b (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   425
	return cost;
2552
fb174febb0b1 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   426
}