src/autoreplace_cmd.cpp
author peter1138
Sun, 03 Aug 2008 17:35:08 +0000
changeset 9834 7474149531c1
parent 9725 982452c3357e
child 9883 77893558228d
permissions -rw-r--r--
(svn r13977) -Codechange: Let ResizeWindowForWidget() handle hidden (zero height or zero width) widgets.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
8999
43af94f84514 (svn r12794) -Doc: added missing doxygen comment in autoreplace_cmd.cpp
bjarni
parents: 8997
diff changeset
     3
/** @file autoreplace_cmd.cpp Deals with autoreplace execution but not the setup */
43af94f84514 (svn r12794) -Doc: added missing doxygen comment in autoreplace_cmd.cpp
bjarni
parents: 8997
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1881
diff changeset
     6
#include "openttd.h"
3959
48f178431701 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3957
diff changeset
     7
#include "roadveh.h"
3961
9868b766fda7 (svn r5120) Add IsShipInDepot{Stopped,}() and remove some redundant checks
tron
parents: 3959
diff changeset
     8
#include "ship.h"
8763
81fadd257f67 (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 8736
diff changeset
     9
#include "news_func.h"
8254
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8230
diff changeset
    10
#include "player_func.h"
1601
83fbe3bfb847 (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
d65cd19f7117 (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
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
    13
#include "train.h"
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
    14
#include "aircraft.h"
6113
a10ede03ac58 (svn r8849) -Codechange: Replace hardcoded global/climate cargo mapping tables with dynamically generated data. Change associated code to use new functions.
peter1138
parents: 6106
diff changeset
    15
#include "cargotype.h"
6643
f81bee57bc09 (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: 6551
diff changeset
    16
#include "group.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8064
diff changeset
    17
#include "strings_func.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
    18
#include "command_func.h"
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8131
diff changeset
    19
#include "vehicle_func.h"
8131
160939e24ed3 (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: 8116
diff changeset
    20
#include "functions.h"
8211
29a8510dfd62 (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8144
diff changeset
    21
#include "variables.h"
8212
cf3fce5c7464 (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium
parents: 8211
diff changeset
    22
#include "autoreplace_func.h"
8363
dd6cae395a9b (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: 8362
diff changeset
    23
#include "articulated_vehicles.h"
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
    24
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    25
#include "table/strings.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    26
8628
2e7e4cdfe96f (svn r12230) -Codechange: [autoreplace] made a function to detect if a vehicle needs autorenewing
bjarni
parents: 8469
diff changeset
    27
2599
ad75080afea4 (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
/*
ad75080afea4 (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
ad75080afea4 (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
 */
ad75080afea4 (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)
ad75080afea4 (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
{
ad75080afea4 (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;
ad75080afea4 (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
ad75080afea4 (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 {
ad75080afea4 (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 {
ad75080afea4 (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
a08cb4b5c179 (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
ad75080afea4 (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
7010
6f0d9f03180d (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: 6990
diff changeset
    40
			if (dest->cargo.Count() == dest->cargo_cap)
4434
a08cb4b5c179 (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
ad75080afea4 (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
7010
6f0d9f03180d (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: 6990
diff changeset
    43
			uint units_moved = min(source->cargo.Count(), dest->cargo_cap - dest->cargo.Count());
6f0d9f03180d (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: 6990
diff changeset
    44
			source->cargo.MoveTo(&dest->cargo, units_moved);
2599
ad75080afea4 (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
ad75080afea4 (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
ad75080afea4 (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;
ad75080afea4 (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;
ad75080afea4 (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
7492
35acee076719 (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: 7258
diff changeset
    50
		} while (source->cargo.Count() > 0 && (dest = dest->Next()) != NULL);
2599
ad75080afea4 (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;
7492
35acee076719 (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: 7258
diff changeset
    52
	} while ((source = source->Next()) != NULL);
5693
21dd11c7a7c9 (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: 5686
diff changeset
    53
21dd11c7a7c9 (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: 5686
diff changeset
    54
	/*
21dd11c7a7c9 (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: 5686
diff changeset
    55
	 * The of the train will be incorrect at this moment. This is due
21dd11c7a7c9 (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: 5686
diff changeset
    56
	 * to the fact that removing the old wagon updates the weight of
21dd11c7a7c9 (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: 5686
diff changeset
    57
	 * the complete train, which is without the weight of cargo we just
21dd11c7a7c9 (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: 5686
diff changeset
    58
	 * moved back into some (of the) new wagon(s).
21dd11c7a7c9 (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: 5686
diff changeset
    59
	 */
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9628
diff changeset
    60
	if (dest->type == VEH_TRAIN) TrainConsistChanged(dest->First(), true);
2599
ad75080afea4 (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
}
ad75080afea4 (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
9725
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    63
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    64
/**
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    65
 * Tests whether refit orders that applied to v will also apply to the new vehicle type
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    66
 * @param v The vehicle to be replaced
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    67
 * @param engine_type The type we want to replace with
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    68
 * @return true iff all refit orders stay valid
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    69
 */
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    70
static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, EngineID engine_type)
4741
2828a02d6f26 (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
{
2828a02d6f26 (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
	const Order *o;
2828a02d6f26 (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
	const Vehicle *u;
2828a02d6f26 (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
9725
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    75
	uint32 union_refit_mask_a = GetUnionOfArticulatedRefitMasks(v->engine_type, v->type, false);
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    76
	uint32 union_refit_mask_b = GetUnionOfArticulatedRefitMasks(engine_type, v->type, false);
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    77
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
    78
	if (v->type == VEH_TRAIN) {
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7492
diff changeset
    79
		u = v->First();
4741
2828a02d6f26 (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
	} else {
2828a02d6f26 (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
		u = v;
2828a02d6f26 (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
	}
2828a02d6f26 (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
    83
2828a02d6f26 (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
    84
	FOR_VEHICLE_ORDERS(u, o) {
8838
068d63397dc3 (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 8786
diff changeset
    85
		if (!o->IsRefit()) continue;
9725
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    86
		CargoID cargo_type = o->GetRefitCargo();
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    87
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    88
		if (!HasBit(union_refit_mask_a, cargo_type)) continue;
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
    89
		if (!HasBit(union_refit_mask_b, cargo_type)) return false;
4741
2828a02d6f26 (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
    90
	}
2828a02d6f26 (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
    91
2828a02d6f26 (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
    92
	return true;
2828a02d6f26 (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
    93
}
2828a02d6f26 (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
    94
4554
8342a6d0a366 (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
/**
8342a6d0a366 (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
 * Function to find what type of cargo to refit to when autoreplacing
8342a6d0a366 (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
    97
 * @param *v Original vehicle, that is being replaced
8342a6d0a366 (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
    98
 * @param engine_type The EngineID of the vehicle that is being replaced to
8342a6d0a366 (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
    99
 * @return The cargo type to replace to
8342a6d0a366 (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
   100
 *    CT_NO_REFIT is returned if no refit is needed
8342a6d0a366 (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
   101
 *    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
8342a6d0a366 (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
   102
 */
8342a6d0a366 (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
   103
static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type)
8342a6d0a366 (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
{
9725
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   105
	CargoID cargo_type;
4554
8342a6d0a366 (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
9725
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   107
	if (GetUnionOfArticulatedRefitMasks(engine_type, v->type, true) == 0) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity
4554
8342a6d0a366 (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
9725
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   109
	if (IsArticulatedVehicleCarryingDifferentCargos(v, &cargo_type)) return CT_INVALID; // We cannot refit to mixed cargos in an automated way
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   110
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   111
	uint32 available_cargo_types = GetIntersectionOfArticulatedRefitMasks(engine_type, v->type, true);
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   112
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   113
	if (cargo_type == CT_INVALID) {
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   114
		if (v->type != VEH_TRAIN) return CT_NO_REFIT; // If the vehicle does not carry anything at all, every replacement is fine.
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   115
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   116
		/* the old engine didn't have cargo capacity, but the new one does
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   117
		 * now we will figure out what cargo the train is carrying and refit to fit this */
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   118
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   119
		for (v = v->First(); v != NULL; v = v->Next()) {
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   120
			if (v->cargo_cap == 0) continue;
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   121
			/* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   122
			if (HasBit(available_cargo_types, v->cargo_type)) {
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   123
				/* Do we have to refit the vehicle, or is it already carrying the right cargo? */
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   124
				uint16 *default_capacity = GetCapacityOfArticulatedParts(engine_type, v->type);
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   125
				for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   126
					if (cid != cargo_type && default_capacity[cid] > 0) return cargo_type;
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   127
				}
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   128
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   129
				return CT_NO_REFIT;
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   130
			}
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   131
		}
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   132
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   133
		return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   134
	} else {
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   135
		if (!HasBit(available_cargo_types, cargo_type)) return CT_INVALID; // We can't refit the vehicle to carry the cargo we want
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   136
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   137
		if (!VerifyAutoreplaceRefitForOrders(v, engine_type)) return CT_INVALID; // Some refit orders loose their effect
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   138
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   139
		/* Do we have to refit the vehicle, or is it already carrying the right cargo? */
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   140
		uint16 *default_capacity = GetCapacityOfArticulatedParts(engine_type, v->type);
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   141
		for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   142
			if (cid != cargo_type && default_capacity[cid] > 0) return cargo_type;
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   143
		}
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   144
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   145
		return CT_NO_REFIT;
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   146
	}
4554
8342a6d0a366 (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
}
8342a6d0a366 (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
8997
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   149
/** Replaces a vehicle (used to be called autorenew)
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   150
 * This function is only called from MaybeReplaceVehicle()
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   151
 * Must be called with _current_player set to the owner of the vehicle
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   152
 * @param w Vehicle to replace
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   153
 * @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts
8997
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   154
 * @param p The vehicle owner (faster than refinding the pointer)
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   155
 * @param new_engine_type The EngineID to replace to
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   156
 * @return value is cost of the replacement or CMD_ERROR
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   157
 */
9628
981ba3800c35 (svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz
parents: 9280
diff changeset
   158
static CommandCost ReplaceVehicle(Vehicle **w, uint32 flags, Money total_cost, const Player *p, EngineID new_engine_type)
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   159
{
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6800
diff changeset
   160
	CommandCost cost;
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6800
diff changeset
   161
	CommandCost sell_value;
2599
ad75080afea4 (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
   162
	Vehicle *old_v = *w;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   163
	const UnitID cached_unitnumber = old_v->unitnumber;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   164
	bool new_front = false;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   165
	Vehicle *new_v = NULL;
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   166
	char *vehicle_name = NULL;
4554
8342a6d0a366 (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
   167
	CargoID replacement_cargo_type;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   168
4554
8342a6d0a366 (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
   169
	replacement_cargo_type = GetNewCargoTypeForReplace(old_v, new_engine_type);
8342a6d0a366 (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
   170
8342a6d0a366 (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
	/* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo type */
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   172
	if (replacement_cargo_type == CT_INVALID) return CommandCost();
4554
8342a6d0a366 (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
   173
5792
c4dad358b30b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 5763
diff changeset
   174
	sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, GetCmdSellVeh(old_v));
4728
d4426f6c6191 (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
   175
d4426f6c6191 (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
   176
	/* We give the player a loan of the same amount as the sell value.
d4426f6c6191 (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
   177
	 * This is needed in case he needs the income from the sale to build the new vehicle.
d4426f6c6191 (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
   178
	 * We take it back if building fails or when we really sell the old engine */
d4426f6c6191 (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
   179
	SubtractMoneyFromPlayer(sell_value);
d4426f6c6191 (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
   180
9628
981ba3800c35 (svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz
parents: 9280
diff changeset
   181
	cost = DoCommand(old_v->tile, new_engine_type, 0, flags | DC_AUTOREPLACE, GetCmdBuildVeh(old_v));
4728
d4426f6c6191 (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
   182
	if (CmdFailed(cost)) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   183
		/* Take back the money we just gave the player */
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   184
		sell_value.MultiplyCost(-1);
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   185
		SubtractMoneyFromPlayer(sell_value);
4728
d4426f6c6191 (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
   186
		return cost;
d4426f6c6191 (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
   187
	}
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   188
6746
ec46969766e3 (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: 6735
diff changeset
   189
	if (replacement_cargo_type != CT_NO_REFIT) {
ec46969766e3 (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: 6735
diff changeset
   190
		/* add refit cost */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6800
diff changeset
   191
		CommandCost refit_cost = GetRefitCost(new_engine_type);
8363
dd6cae395a9b (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: 8362
diff changeset
   192
		if (old_v->type == VEH_TRAIN && RailVehInfo(new_engine_type)->railveh_type == RAILVEH_MULTIHEAD) {
dd6cae395a9b (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: 8362
diff changeset
   193
			/* Since it's a dualheaded engine we have to pay once more because the rear end is being refitted too. */
dd6cae395a9b (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: 8362
diff changeset
   194
			refit_cost.AddCost(refit_cost);
dd6cae395a9b (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: 8362
diff changeset
   195
		}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   196
		cost.AddCost(refit_cost);
6746
ec46969766e3 (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: 6735
diff changeset
   197
	}
4554
8342a6d0a366 (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
   198
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   199
	if (flags & DC_EXEC) {
2564
24533c2db978 (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2563
diff changeset
   200
		new_v = GetVehicle(_new_vehicle_id);
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4404
diff changeset
   201
		*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
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   202
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   203
		/* refit if needed */
4554
8342a6d0a366 (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
   204
		if (replacement_cargo_type != CT_NO_REFIT) {
5792
c4dad358b30b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 5763
diff changeset
   205
			if (CmdFailed(DoCommand(0, new_v->index, replacement_cargo_type, DC_EXEC, GetCmdRefitVeh(new_v)))) {
4613
6fbc87fa5e3b (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
   206
				/* Being here shows a failure, which most likely is in GetNewCargoTypeForReplace() or incorrect estimation costs */
6fbc87fa5e3b (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
   207
				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
89bc316009c9 (svn r6464) -Fix r6393: killed a warning when compiling without asserts
bjarni
parents: 4604
diff changeset
   208
			}
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   209
		}
4611
89bc316009c9 (svn r6464) -Fix r6393: killed a warning when compiling without asserts
bjarni
parents: 4604
diff changeset
   210
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7527
diff changeset
   211
		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
580ff4e6a49e (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
   212
			// we are autorenewing to a single engine, so we will turn it as the old one was turned as well
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   213
			SetBit(new_v->u.rail.flags, VRF_REVERSE_DIRECTION);
3896
580ff4e6a49e (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
   214
		}
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   215
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   216
		if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_v)) {
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   217
			/* this is a railcar. We need to move the car into the train
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   218
			 * We add the new engine after the old one instead of replacing it. It will give the same result anyway when we
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   219
			 * sell the old engine in a moment
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   220
			 */
6735
dd792fed65e2 (svn r9967) -Fix (r9938): autoreplace would in certain conditions move dualheaded engines in a train (usually to the rear)
bjarni
parents: 6706
diff changeset
   221
			/* Get the vehicle in front of the one we move out */
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7492
diff changeset
   222
			Vehicle *front = old_v->Previous();
8736
ff99c0258370 (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: 8680
diff changeset
   223
			if (front == NULL) {
ff99c0258370 (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: 8680
diff changeset
   224
				/* It would appear that we have the front wagon of a row of wagons without engines */
ff99c0258370 (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: 8680
diff changeset
   225
				Vehicle *next = old_v->Next();
ff99c0258370 (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: 8680
diff changeset
   226
				if (next != NULL) {
ff99c0258370 (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: 8680
diff changeset
   227
					/* Move the chain to the new front wagon */
ff99c0258370 (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: 8680
diff changeset
   228
					DoCommand(0, (new_v->index << 16) | next->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
ff99c0258370 (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: 8680
diff changeset
   229
				}
ff99c0258370 (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: 8680
diff changeset
   230
			} else {
ff99c0258370 (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: 8680
diff changeset
   231
				/* 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 */
ff99c0258370 (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: 8680
diff changeset
   232
				if (IsRearDualheaded(front)) front = front->Previous();
ff99c0258370 (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: 8680
diff changeset
   233
				/* Now we move the old one out of the train */
ff99c0258370 (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: 8680
diff changeset
   234
				DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
ff99c0258370 (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: 8680
diff changeset
   235
				/* Add the new vehicle */
9061
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   236
				CommandCost tmp_move = DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   237
				if (CmdFailed(tmp_move)) {
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   238
					cost.AddCost(tmp_move);
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   239
					DoCommand(0, new_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN));
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   240
				}
8736
ff99c0258370 (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: 8680
diff changeset
   241
			}
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   242
		} else {
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   243
			// copy/clone the orders
8469
c3621b1a6454 (svn r12040) -Codechange: Change IsOrderListShared from a simple function to a class member(MagicBuzz).
belugas
parents: 8363
diff changeset
   244
			DoCommand(0, (old_v->index << 16) | new_v->index, old_v->IsOrderListShared() ? CO_SHARE : CO_COPY, DC_EXEC, CMD_CLONE_ORDER);
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   245
			new_v->cur_order_index = old_v->cur_order_index;
9280
8adb36dcebab (svn r13146) -Codechange: vehicle_base.h doesn't need to be included in vehicle_gui.h.
rubidium
parents: 9234
diff changeset
   246
			ChangeVehicleViewWindow(old_v->index, new_v->index);
2575
7a3e28317661 (svn r3112) -Feature: [autoreplace] profit counters are now remembered too (request by Darkvater)
bjarni
parents: 2574
diff changeset
   247
			new_v->profit_this_year = old_v->profit_this_year;
7a3e28317661 (svn r3112) -Feature: [autoreplace] profit counters are now remembered too (request by Darkvater)
bjarni
parents: 2574
diff changeset
   248
			new_v->profit_last_year = old_v->profit_last_year;
3679
252b4ab2aad4 (svn r4599) -Fix: [Cloning, autoreplace] FS#141 clone service-interval
bjarni
parents: 3657
diff changeset
   249
			new_v->service_interval = old_v->service_interval;
6647
aa2284cbb2dc (svn r9878) -Fix (9874): some vehicle count's were not properly updated on delete or autoreplace of vehicles.
rubidium
parents: 6643
diff changeset
   250
			DoCommand(0, old_v->group_id, new_v->index, flags, CMD_ADD_VEHICLE_GROUP);
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   251
			new_front = true;
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4404
diff changeset
   252
			new_v->unitnumber = old_v->unitnumber; // use the same unit number
6551
85a70a72ffef (svn r9753) -Fix [FS#732]: trains are lost after autorenewal/autoreplace
bjarni
parents: 6505
diff changeset
   253
			new_v->dest_tile  = old_v->dest_tile;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   254
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   255
			new_v->current_order = old_v->current_order;
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   256
			if (old_v->type == VEH_TRAIN && GetNextVehicle(old_v) != NULL){
2842
954112262878 (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   257
				Vehicle *temp_v = GetNextVehicle(old_v);
954112262878 (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   258
954112262878 (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   259
				// move the entire train to the new engine, excluding the old engine
954112262878 (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   260
				if (IsMultiheaded(old_v) && temp_v == old_v->u.rail.other_multiheaded_part) {
954112262878 (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   261
					// we got front and rear of a multiheaded engine right after each other. We should work with the next in line instead
954112262878 (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   262
					temp_v = GetNextVehicle(temp_v);
954112262878 (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   263
				}
954112262878 (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   264
954112262878 (svn r3390) -Fix: [autoreplace] fixed issue where autoreplace failed to attach the cars if the front engine was replaced
bjarni
parents: 2819
diff changeset
   265
				if (temp_v != NULL) {
9061
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   266
					CommandCost tmp_move = DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   267
					if (CmdFailed(tmp_move)) {
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   268
						cost.AddCost(tmp_move);
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   269
						DoCommand(0, temp_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN));
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   270
					}
2681
d7376b436f9c (svn r3223) -Fix: [autoreplace] fixed crash when replacing a train engine without any cars (introduced in r3220)
bjarni
parents: 2678
diff changeset
   271
				}
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   272
			}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   273
		}
9140
7c8d8562b942 (svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni
parents: 9081
diff changeset
   274
		if (CmdSucceeded(cost)) {
7c8d8562b942 (svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni
parents: 9081
diff changeset
   275
			/* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
7c8d8562b942 (svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni
parents: 9081
diff changeset
   276
			MoveVehicleCargo(new_v->type == VEH_TRAIN ? new_v->First() : new_v, old_v);
2716
2eb2687665e7 (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
   277
9140
7c8d8562b942 (svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni
parents: 9081
diff changeset
   278
			/* Get the name of the old vehicle if it has a custom name. */
7c8d8562b942 (svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni
parents: 9081
diff changeset
   279
			if (old_v->name != NULL) vehicle_name = strdup(old_v->name);
7c8d8562b942 (svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni
parents: 9081
diff changeset
   280
		}
4554
8342a6d0a366 (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
   281
	} else { // flags & DC_EXEC not set
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   282
		CommandCost tmp_move;
7524
64f81c03d870 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7497
diff changeset
   283
64f81c03d870 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7497
diff changeset
   284
		if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v)) {
7527
a1d3a14ae669 (svn r11046) -Codechange: added function to get the next movable (non-articulated, non-read end of dualheaded engine) vehicle in a train
bjarni
parents: 7526
diff changeset
   285
			Vehicle *next_veh = GetNextUnit(old_v); // don't try to move the rear multiheaded engine or articulated parts
7524
64f81c03d870 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7497
diff changeset
   286
			if (next_veh != NULL) {
64f81c03d870 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7497
diff changeset
   287
				/* Verify that the wagons can be placed on the engine in question.
64f81c03d870 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7497
diff changeset
   288
				 * This is done by building an engine, test if the wagons can be added and then sell the test engine. */
9628
981ba3800c35 (svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz
parents: 9280
diff changeset
   289
				DoCommand(old_v->tile, new_engine_type, 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_v));
7524
64f81c03d870 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7497
diff changeset
   290
				Vehicle *temp = GetVehicle(_new_vehicle_id);
64f81c03d870 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7497
diff changeset
   291
				tmp_move = DoCommand(0, (temp->index << 16) | next_veh->index, 1, 0, CMD_MOVE_RAIL_VEHICLE);
64f81c03d870 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7497
diff changeset
   292
				DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v));
64f81c03d870 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni
parents: 7497
diff changeset
   293
			}
6800
2bea073750a3 (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: 6747
diff changeset
   294
		}
2bea073750a3 (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: 6747
diff changeset
   295
4554
8342a6d0a366 (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
   296
		/* Ensure that the player will not end up having negative money while autoreplacing
8342a6d0a366 (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
   297
		 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */
6952
b19643469024 (svn r10207) -Codechange: remove the redundant player_money in favour of the money64, which is now renamed to player_money.
rubidium
parents: 6950
diff changeset
   298
		if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   299
			/* Pay back the loan */
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   300
			sell_value.MultiplyCost(-1);
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   301
			SubtractMoneyFromPlayer(sell_value);
4728
d4426f6c6191 (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
   302
			return CMD_ERROR;
d4426f6c6191 (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
   303
		}
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   304
	}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   305
4728
d4426f6c6191 (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
   306
	/* Take back the money we just gave the player just before building the vehicle
d4426f6c6191 (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
   307
	 * The player will get the same amount now that the sale actually takes place */
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   308
	sell_value.MultiplyCost(-1);
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   309
	SubtractMoneyFromPlayer(sell_value);
4728
d4426f6c6191 (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
   310
4554
8342a6d0a366 (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
   311
	/* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   312
	cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v)));
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   313
9140
7c8d8562b942 (svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni
parents: 9081
diff changeset
   314
	if (CmdFailed(cost)) return cost;
7c8d8562b942 (svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni
parents: 9081
diff changeset
   315
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   316
	if (new_front) {
4554
8342a6d0a366 (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
   317
		/* now we assign the old unitnumber to the new vehicle */
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   318
		new_v->unitnumber = cached_unitnumber;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   319
	}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   320
4554
8342a6d0a366 (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
   321
	/* Transfer the name of the old vehicle */
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   322
	if ((flags & DC_EXEC) && vehicle_name != NULL) {
2716
2eb2687665e7 (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
   323
		_cmd_text = vehicle_name;
3491
35d747bb5e82 (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
   324
		DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE);
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   325
		free(vehicle_name);
2716
2eb2687665e7 (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
   326
	}
2eb2687665e7 (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
   327
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   328
	return cost;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   329
}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   330
9002
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   331
/** Removes wagons from a train until it get a certain length
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   332
 * @param v The vehicle
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   333
 * @param old_total_length The wanted max length
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   334
 * @return The profit from selling the wagons
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   335
 */
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   336
static CommandCost WagonRemoval(Vehicle *v, uint16 old_total_length)
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   337
{
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   338
	if (v->type != VEH_TRAIN) return CommandCost();
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   339
	Vehicle *front = v;
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   340
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   341
	CommandCost cost = CommandCost();
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   342
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   343
	while (front->u.rail.cached_total_length > old_total_length) {
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   344
		/* the train is too long. We will remove cars one by one from the start of the train until it's short enough */
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   345
		while (v != NULL && RailVehInfo(v->engine_type)->railveh_type != RAILVEH_WAGON) {
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   346
			/* We move backwards in the train until we find a wagon */
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   347
			v = GetNextVehicle(v);
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   348
		}
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   349
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   350
		if (v == NULL) {
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   351
			/* We sold all the wagons and the train is still not short enough */
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   352
			SetDParam(0, front->unitnumber);
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9141
diff changeset
   353
			AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NS_ADVICE, front->index, 0);
9002
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   354
			return cost;
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   355
		}
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   356
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   357
		/* We found a wagon we can sell */
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   358
		Vehicle *temp = v;
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   359
		v = GetNextVehicle(v);
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   360
		DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); // remove the wagon from the train
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   361
		MoveVehicleCargo(front, temp); // move the cargo back on the train
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   362
		cost.AddCost(DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON)); // sell the wagon
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   363
	}
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   364
	return cost;
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   365
}
65efdd3cfa3f (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni
parents: 8999
diff changeset
   366
8997
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   367
/** Get the EngineID of the replacement for a vehicle
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   368
 * @param v The vehicle to find a replacement for
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   369
 * @param p The vehicle's owner (it's faster to forward the pointer than refinding it)
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   370
 * @return the EngineID of the replacement. INVALID_ENGINE if no buildable replacement is found
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   371
 */
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   372
static EngineID GetNewEngineType(const Vehicle *v, const Player *p)
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   373
{
9725
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   374
	assert(v->type != VEH_TRAIN || !IsArticulatedPart(v));
982452c3357e (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch
parents: 9704
diff changeset
   375
8997
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   376
	if (v->type == VEH_TRAIN && IsRearDualheaded(v)) {
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   377
		/* we build the rear ends of multiheaded trains with the front ones */
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   378
		return INVALID_ENGINE;
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   379
	}
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   380
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   381
	EngineID e = EngineReplacementForPlayer(p, v->engine_type, v->group_id);
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   382
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   383
	if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_player)) {
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   384
		return e;
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   385
	}
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   386
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   387
	if (v->NeedsAutorenewing(p) && // replace if engine is too old
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   388
	    IsEngineBuildable(v->engine_type, v->type, _current_player)) { // engine can still be build
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   389
		return v->engine_type;
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   390
	}
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   391
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   392
	return INVALID_ENGINE;
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   393
}
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   394
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   395
/** replaces a vehicle if it's set for autoreplace or is too old
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   396
 * (used to be called autorenew)
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   397
 * @param v The vehicle to replace
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4404
diff changeset
   398
 * if the vehicle is a train, v needs to be the front engine
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   399
 * @param flags
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   400
 * @param display_costs If set, a cost animation is shown (only if DC_EXEC is set)
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   401
 *        This bool also takes autorenew money into consideration
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   402
 * @return the costs, the success bool and sometimes an error message
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   403
 */
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   404
CommandCost MaybeReplaceVehicle(Vehicle *v, uint32 flags, bool display_costs)
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   405
{
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   406
	Vehicle *w;
9061
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   407
	Player *p = GetPlayer(v->owner);
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   408
	CommandCost cost;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   409
	bool stopped = false;
9061
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   410
	BackuppedVehicle backup(true);
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   411
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   412
	/* We only want "real" vehicle types. */
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   413
	assert(IsPlayerBuildableVehicleType(v));
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   414
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   415
	/* Ensure that this bool is cleared. */
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   416
	assert(!v->leave_depot_instantly);
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   417
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   418
	/* We can't sell if the current player don't own the vehicle. */
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   419
	assert(v->owner == _current_player);
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   420
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   421
	if (!v->IsInDepot()) {
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   422
		/* The vehicle should be inside the depot */
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   423
		switch (v->type) {
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   424
			default: NOT_REACHED();
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   425
			case VEH_TRAIN:    return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED); break;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   426
			case VEH_ROAD:     return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);     break;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   427
			case VEH_SHIP:     return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN);    break;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   428
			case VEH_AIRCRAFT: return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED);   break;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   429
		}
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   430
	}
2805
29e2b43a1e20 (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
   431
29e2b43a1e20 (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
   432
	/* Remember the length in case we need to trim train later on
3175
30c8cb7cc416 (svn r3811) -Fix: [autoreplace]: (FS#67) autoreplacing trains now keep their tile length instead of their pixel length
bjarni
parents: 3173
diff changeset
   433
	 * If it's not a train, the value is unused
30c8cb7cc416 (svn r3811) -Fix: [autoreplace]: (FS#67) autoreplacing trains now keep their tile length instead of their pixel length
bjarni
parents: 3173
diff changeset
   434
	 * round up to the length of the tiles used for the train instead of the train length instead
30c8cb7cc416 (svn r3811) -Fix: [autoreplace]: (FS#67) autoreplacing trains now keep their tile length instead of their pixel length
bjarni
parents: 3173
diff changeset
   435
	 * Useful when newGRF uses custom length */
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   436
	uint16 old_total_length = (v->type == VEH_TRAIN ?
3422
a6eba3443452 (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
   437
		(v->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE :
a6eba3443452 (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
   438
		-1
a6eba3443452 (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
   439
	);
3175
30c8cb7cc416 (svn r3811) -Fix: [autoreplace]: (FS#67) autoreplacing trains now keep their tile length instead of their pixel length
bjarni
parents: 3173
diff changeset
   440
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   441
	if (!(v->vehstatus & VS_STOPPED)) {
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   442
		/* The vehicle is moving so we better stop it before we might alter consist or sell it */
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   443
		v->vehstatus |= VS_STOPPED;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   444
		/* Remember that we stopped the vehicle */
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   445
		stopped = true;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   446
	}
2590
64e3f69a4ad8 (svn r3127) -Fix: [autoreplace] fixed a condition where a vehicle could fail to stop when autoreplacing
bjarni
parents: 2579
diff changeset
   447
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   448
	{
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8212
diff changeset
   449
		cost = CommandCost(EXPENSES_NEW_VEHICLES);
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   450
		w = v;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   451
		do {
8997
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   452
			EngineID new_engine = GetNewEngineType(w, p);
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   453
			if (new_engine == INVALID_ENGINE) continue;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   454
9061
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   455
			if (!backup.ContainsBackup()) {
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   456
				/* We are going to try to replace a vehicle but we don't have any backup so we will make one. */
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   457
				backup.Backup(v, p);
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   458
			}
9141
0989911b79dd (svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
bjarni
parents: 9140
diff changeset
   459
			/* Now replace the vehicle.
0989911b79dd (svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
bjarni
parents: 9140
diff changeset
   460
			 * First we need to cache if it's the front vehicle as we need to update the v pointer if it is.
0989911b79dd (svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
bjarni
parents: 9140
diff changeset
   461
			 * If the replacement fails then we can't trust the data in the vehicle hence the reason to cache the result. */
0989911b79dd (svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
bjarni
parents: 9140
diff changeset
   462
			bool IsFront = w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE;
0989911b79dd (svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
bjarni
parents: 9140
diff changeset
   463
9061
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   464
			cost.AddCost(ReplaceVehicle(&w, DC_EXEC, cost.GetCost(), p, new_engine));
6800
2bea073750a3 (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: 6747
diff changeset
   465
9141
0989911b79dd (svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
bjarni
parents: 9140
diff changeset
   466
			if (IsFront) {
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   467
				/* now we bought a new engine and sold the old one. We need to fix the
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   468
				 * pointers in order to avoid pointing to the old one for trains: these
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   469
				 * pointers should point to the front engine and not the cars
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   470
				 */
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   471
				v = w;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   472
			}
9140
7c8d8562b942 (svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni
parents: 9081
diff changeset
   473
		} while (CmdSucceeded(cost) && w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL);
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   474
9140
7c8d8562b942 (svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni
parents: 9081
diff changeset
   475
		if (CmdSucceeded(cost) && v->type == VEH_TRAIN && p->renew_keep_length) {
9061
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   476
			/* Remove wagons until the wanted length is reached */
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   477
			cost.AddCost(WagonRemoval(v, old_total_length));
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   478
		}
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   479
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   480
		if (flags & DC_QUERY_COST || cost.GetCost() == 0) {
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   481
			/* We didn't do anything during the replace so we will just exit here */
9081
38b6cc9fd473 (svn r12940) -Fix [FS#1974](r12913): [autoreplace] a vehicle backup should include the cargo packets in the vehicle as well
bjarni
parents: 9061
diff changeset
   482
			v = backup.Restore(v, p);
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   483
			if (stopped) v->vehstatus &= ~VS_STOPPED;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   484
			return cost;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   485
		}
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   486
9061
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   487
		if (display_costs) {
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   488
			/* We want to ensure that we will not get below p->engine_renew_money.
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   489
			 * We will not actually pay this amount. It's for display and checks only. */
9061
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   490
			CommandCost tmp = cost;
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   491
			tmp.AddCost((Money)p->engine_renew_money);
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   492
			if (CmdSucceeded(tmp) && GetAvailableMoneyForCommand() < tmp.GetCost()) {
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   493
				/* We don't have enough money so we will set cost to failed */
9061
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   494
				cost.AddCost((Money)p->engine_renew_money);
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   495
				cost.AddCost(CMD_ERROR);
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   496
			}
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   497
		}
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   498
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   499
		if (display_costs && CmdFailed(cost)) {
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   500
			if (GetAvailableMoneyForCommand() < cost.GetCost() && IsLocalPlayer()) {
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   501
				StringID message;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   502
				SetDParam(0, v->unitnumber);
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   503
				switch (v->type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   504
					case VEH_TRAIN:    message = STR_TRAIN_AUTORENEW_FAILED;       break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   505
					case VEH_ROAD:     message = STR_ROADVEHICLE_AUTORENEW_FAILED; break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   506
					case VEH_SHIP:     message = STR_SHIP_AUTORENEW_FAILED;        break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   507
					case VEH_AIRCRAFT: message = STR_AIRCRAFT_AUTORENEW_FAILED;    break;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   508
						// This should never happen
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   509
					default: NOT_REACHED(); message = 0; break;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   510
				}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   511
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9141
diff changeset
   512
				AddNewsItem(message, NS_ADVICE, v->index, 0);
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   513
			}
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   514
		}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   515
	}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   516
9061
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   517
	if (display_costs && IsLocalPlayer() && (flags & DC_EXEC) && CmdSucceeded(cost)) {
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   518
		ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost());
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   519
	}
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   520
9061
8ebd40e86171 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni
parents: 9002
diff changeset
   521
	if (!(flags & DC_EXEC) || CmdFailed(cost)) {
9081
38b6cc9fd473 (svn r12940) -Fix [FS#1974](r12913): [autoreplace] a vehicle backup should include the cargo packets in the vehicle as well
bjarni
parents: 9061
diff changeset
   522
		v = backup.Restore(v, p);
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   523
	}
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   524
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   525
	/* Start the vehicle if we stopped it earlier */
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   526
	if (stopped) v->vehstatus &= ~VS_STOPPED;
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8962
diff changeset
   527
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   528
	return cost;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   529
}