src/autoreplace_cmd.cpp
author rubidium
Sat, 19 May 2007 20:15:50 +0000
changeset 6650 b9c81a1f8b7e
parent 6647 aa2284cbb2dc
child 6706 bfbaaa96b51b
permissions -rw-r--r--
(svn r9881) -Fix (FS#782, r9874): accidentally removed one condition too many causing asserts.
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
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1881
diff changeset
     4
#include "openttd.h"
3959
48f178431701 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3957
diff changeset
     5
#include "roadveh.h"
3961
9868b766fda7 (svn r5120) Add IsShipInDepot{Stopped,}() and remove some redundant checks
tron
parents: 3959
diff changeset
     6
#include "ship.h"
507
04b5403aaf6b (svn r815) Include strings.h only in the files which need it.
tron
parents: 445
diff changeset
     7
#include "table/strings.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2148
diff changeset
     8
#include "functions.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     9
#include "news.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include "player.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "engine.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
    13
#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
    14
#include "vehicle_gui.h"
1758
2158a0938e45 (svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents: 1757
diff changeset
    15
#include "depot.h"
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
    16
#include "train.h"
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
    17
#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
    18
#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
    19
#include "group.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
    21
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
    22
/*
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
    23
 * 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
    24
 */
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
    25
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
    26
{
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
    27
	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
    28
	int units_moved;
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
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
	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
    31
		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
    32
			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
    33
				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
    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
			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
    36
				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
    37
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
    38
			units_moved = min(source->cargo_count, dest->cargo_cap - dest->cargo_count);
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
			source->cargo_count -= units_moved;
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
    40
			dest->cargo_count   += units_moved;
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
    41
			dest->cargo_source   = source->cargo_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
    42
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
    43
			// 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
    44
			dest->cargo_days   = source->cargo_days;
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
			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
    46
			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
    47
2678
71ca8ee16593 (svn r3220) -Fix: [autoreplace] Autoreplaced trains now replace their cargo to the train instead of just the new engine
bjarni
parents: 2677
diff changeset
    48
		} 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
    49
		dest = v;
2678
71ca8ee16593 (svn r3220) -Fix: [autoreplace] Autoreplaced trains now replace their cargo to the train instead of just the new engine
bjarni
parents: 2677
diff changeset
    50
	} 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
    51
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
    52
	/*
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
	 * 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
    54
	 * 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
    55
	 * 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
    56
	 * 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
    57
	 */
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
    58
	if (dest->type == VEH_TRAIN) TrainConsistChanged(dest->first);
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
    59
}
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
    60
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
    61
static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, const EngineID engine_type)
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
    62
{
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
    63
	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
    64
	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
    65
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
    66
	if (v->type == VEH_TRAIN) {
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
    67
		u = GetFirstVehicleInChain(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
    68
	} 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
    69
		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
    70
	}
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
	FOR_VEHICLE_ORDERS(u, o) {
4782
6aa485b21dae (svn r6696) -Codechange: changed all comparision for refit cargo in orders against CT_NO_REFIT to checks for valid cargo IDs
bjarni
parents: 4762
diff changeset
    73
		if (!(o->refit_cargo < NUM_CARGO)) continue;
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
    74
		if (!CanRefitTo(v->engine_type, o->refit_cargo)) continue;
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
    75
		if (!CanRefitTo(engine_type, o->refit_cargo)) return false;
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
    76
	}
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
    77
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
    78
	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
    79
}
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
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
    81
/**
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
    82
 * 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
    83
 * @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
    84
 * @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
    85
 * @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
    86
 *    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
    87
 *    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
    88
 */
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
    89
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
    90
{
6505
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6491
diff changeset
    91
	CargoID new_cargo_type = GetEngineCargoType(engine_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
    92
6505
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6491
diff changeset
    93
	if (new_cargo_type == CT_INVALID) 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
    94
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
    95
	if (v->cargo_type == new_cargo_type || CanRefitTo(engine_type, v->cargo_type)) {
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
    96
		if (VerifyAutoreplaceRefitForOrders(v, engine_type)) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    97
			return v->cargo_type == new_cargo_type ? (CargoID)CT_NO_REFIT : v->cargo_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
    98
		} 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
    99
			return CT_INVALID;
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
   100
		}
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
   101
	}
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
   102
	if (v->type != VEH_TRAIN) return CT_INVALID; // We can't refit the vehicle to carry the cargo we want
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
   103
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
	/* Below this line it's safe to assume that the vehicle in question is a train */
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
   105
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
	if (v->cargo_cap != 0) return CT_INVALID; // trying to replace a vehicle with cargo capacity into another one with incompatible cargo 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
   107
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
	/* the old engine didn't have cargo capacity, but the new one does
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
   109
	 * now we will figure out what cargo the train is carrying and refit to fit this */
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
   110
	v = GetFirstVehicleInChain(v);
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
   111
	do {
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
   112
		if (v->cargo_cap == 0) continue;
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
   113
		/* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
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
   114
		if (v->cargo_type == new_cargo_type) return CT_NO_REFIT;
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
   115
		if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type;
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6264
diff changeset
   116
	} while ((v = v->next) != 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
   117
	return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one
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
   118
}
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
   119
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   120
/* Replaces a vehicle (used to be called autorenew)
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   121
 * This function is only called from MaybeReplaceVehicle()
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   122
 * 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
   123
 * @param w Vehicle to replace
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   124
 * @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   125
 * @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
   126
 */
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
   127
static int32 ReplaceVehicle(Vehicle **w, byte flags, int32 total_cost)
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   128
{
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   129
	int32 cost;
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
   130
	int32 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
   131
	Vehicle *old_v = *w;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   132
	const Player *p = GetPlayer(old_v->owner);
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   133
	EngineID new_engine_type;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   134
	const UnitID cached_unitnumber = old_v->unitnumber;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   135
	bool new_front = false;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   136
	Vehicle *new_v = 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
   137
	char vehicle_name[32];
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
   138
	CargoID replacement_cargo_type;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   139
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
   140
	/* If the vehicle belongs to a group, check if the group is protected from the global autoreplace.
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
   141
	 *  If not, chek if an global auto replacement is defined */
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
   142
	new_engine_type = (IsValidGroupID(old_v->group_id) && GetGroup(old_v->group_id)->replace_protection) ?
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
   143
			INVALID_ENGINE :
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
   144
			EngineReplacementForPlayer(p, old_v->engine_type, DEFAULT_GROUP);
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
   145
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
   146
	/* If we don't set new_egnine_type previously, we try to check if an autoreplacement was defined
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
   147
	 *  for the group and the engine_type of the vehicle */
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
   148
	if (new_engine_type == INVALID_ENGINE && !IsDefaultGroupID(old_v->group_id)) {
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
   149
		new_engine_type = EngineReplacementForPlayer(p, old_v->engine_type, old_v->group_id);
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
   150
	}
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   151
6650
b9c81a1f8b7e (svn r9881) -Fix (FS#782, r9874): accidentally removed one condition too many causing asserts.
rubidium
parents: 6647
diff changeset
   152
	if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type;
b9c81a1f8b7e (svn r9881) -Fix (FS#782, r9874): accidentally removed one condition too many causing asserts.
rubidium
parents: 6647
diff changeset
   153
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
   154
	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
   155
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
   156
	/* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo 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
   157
	if (replacement_cargo_type == CT_INVALID) return 0;
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
   158
5792
c4dad358b30b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 5763
diff changeset
   159
	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
   160
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
   161
	/* 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
   162
	 * 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
   163
	 * 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
   164
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
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
   165
	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
   166
5792
c4dad358b30b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 5763
diff changeset
   167
	cost = DoCommand(old_v->tile, new_engine_type, 3, flags, 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
   168
	if (CmdFailed(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
   169
		SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
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
   170
		SubtractMoneyFromPlayer(-sell_value); // Take back the money we just gave the player
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
   171
		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
   172
	}
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   173
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
   174
	if (replacement_cargo_type != CT_NO_REFIT) cost += GetRefitCost(new_engine_type); // add refit cost
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
   175
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   176
	if (flags & DC_EXEC) {
2564
24533c2db978 (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2563
diff changeset
   177
		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
   178
		*w = new_v; //we changed the vehicle, so MaybeReplaceVehicle needs to work on the new one. Now we tell it what the new one is
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   179
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   180
		/* 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
   181
		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
   182
			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
   183
				/* 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
   184
				error("Autoreplace failed to refit. Replace engine %d to %d and refit to cargo %d", old_v->engine_type, new_v->engine_type, replacement_cargo_type);
4611
89bc316009c9 (svn r6464) -Fix r6393: killed a warning when compiling without asserts
bjarni
parents: 4604
diff changeset
   185
			}
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   186
		}
4611
89bc316009c9 (svn r6464) -Fix r6393: killed a warning when compiling without asserts
bjarni
parents: 4604
diff changeset
   187
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
   188
		if (new_v->type == VEH_TRAIN && HASBIT(old_v->u.rail.flags, VRF_REVERSE_DIRECTION) && !IsMultiheaded(new_v) && !(new_v->next != NULL && IsArticulatedPart(new_v->next))) {
3896
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
   189
			// we are autorenewing to a single engine, so we will turn it as the old one was turned as well
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
   190
			SETBIT(new_v->u.rail.flags, VRF_REVERSE_DIRECTION);
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
   191
		}
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   192
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
   193
		if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_v)) {
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   194
			/* this is a railcar. We need to move the car into the train
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   195
			 * We add the new engine after the old one instead of replacing it. It will give the same result anyway when we
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   196
			 * sell the old engine in a moment
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   197
			 */
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
   198
			DoCommand(0, (GetPrevVehicleInChain(old_v)->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
2678
71ca8ee16593 (svn r3220) -Fix: [autoreplace] Autoreplaced trains now replace their cargo to the train instead of just the new engine
bjarni
parents: 2677
diff changeset
   199
			/* Now we move the old one out of the train */
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
   200
			DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   201
		} else {
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   202
			// copy/clone the orders
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
   203
			DoCommand(0, (old_v->index << 16) | new_v->index, IsOrderListShared(old_v) ? CO_SHARE : CO_COPY, DC_EXEC, CMD_CLONE_ORDER);
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   204
			new_v->cur_order_index = old_v->cur_order_index;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   205
			ChangeVehicleViewWindow(old_v, new_v);
2575
7a3e28317661 (svn r3112) -Feature: [autoreplace] profit counters are now remembered too (request by Darkvater)
bjarni
parents: 2574
diff changeset
   206
			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
   207
			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
   208
			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
   209
			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
   210
			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
   211
			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
   212
			new_v->dest_tile  = old_v->dest_tile;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   213
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   214
			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
   215
			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
   216
				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
   217
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
   218
				// 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
   219
				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
   220
					// 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
   221
					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
   222
				}
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
   223
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
   224
				if (temp_v != NULL) {
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
   225
					DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
2681
d7376b436f9c (svn r3223) -Fix: [autoreplace] fixed crash when replacing a train engine without any cars (introduced in r3220)
bjarni
parents: 2678
diff changeset
   226
				}
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
   227
			} else if (!IsDefaultGroupID(old_v->group_id) && IsValidGroupID(old_v->group_id)) {
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
   228
				/* Increase the new num engines of the group for the ships, aircraft, and road vehicles
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
   229
					The old new num engine is decrease in the destroyvehicle function */
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
   230
				GetGroup(old_v->group_id)->num_engines[new_v->engine_type]++;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   231
			}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   232
		}
2678
71ca8ee16593 (svn r3220) -Fix: [autoreplace] Autoreplaced trains now replace their cargo to the train instead of just the new engine
bjarni
parents: 2677
diff changeset
   233
		/* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
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
   234
		MoveVehicleCargo(new_v->type == VEH_TRAIN ? GetFirstVehicleInChain(new_v) : 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
   235
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
   236
		// Get the name of the old vehicle if it has a custom name.
4862
ac034ec9ed2b (svn r6788) - Codechange: Add and use a function to test if a string ID is a custom name.
peter1138
parents: 4784
diff changeset
   237
		if (!IsCustomName(old_v->string_id)) {
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
   238
			vehicle_name[0] = '\0';
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
   239
		} else {
4912
0f51b47cb983 (svn r6884) -Codechange: Add strict bounds checking in string formatting system.
Darkvater
parents: 4862
diff changeset
   240
			GetName(vehicle_name, old_v->string_id & 0x7FF, lastof(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
   241
		}
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
   242
	} else { // flags & DC_EXEC not set
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
   243
		/* 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
   244
		 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */
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
   245
		if (p->money64 < (cost + total_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
   246
			SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
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
   247
			SubtractMoneyFromPlayer(-sell_value); // Pay back the loan
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
   248
			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
   249
		}
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   250
	}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   251
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
   252
	/* 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
   253
	 * The player will get the same amount now that the sale actually takes place */
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
   254
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
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
   255
	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
   256
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
   257
	/* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */
5792
c4dad358b30b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 5763
diff changeset
   258
	cost += 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
   259
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   260
	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
   261
		/* 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
   262
		new_v->unitnumber = cached_unitnumber;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   263
	}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   264
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
   265
	/* Transfer the name of the old vehicle */
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
   266
	if ((flags & DC_EXEC) && vehicle_name[0] != '\0') {
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
   267
		_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
   268
		DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE);
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
   269
	}
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
   270
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   271
	return cost;
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
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   274
/** replaces a vehicle if it's set for autoreplace or is too old
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   275
 * (used to be called autorenew)
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   276
 * @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
   277
 * if the vehicle is a train, v needs to be the front engine
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   278
 * @param check Checks if the replace is valid. No action is done at all
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   279
 * @param display_costs If set, a cost animation is shown (only if check is false)
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   280
 * @return CMD_ERROR if something went wrong. Otherwise the price of the replace
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   281
 */
6264
c2138d65160b (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni
parents: 6259
diff changeset
   282
int32 MaybeReplaceVehicle(Vehicle *v, bool check, bool display_costs)
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   283
{
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   284
	Vehicle *w;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   285
	const Player *p = GetPlayer(v->owner);
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   286
	byte flags = 0;
2591
00e40545fc9c (svn r3128) -Fix: [autoreplace] fixed bug that made the player pay twice for autoreplacing and could end up with negative money
bjarni
parents: 2590
diff changeset
   287
	int32 cost, temp_cost = 0;
5686
3300a2de25e7 (svn r8147) -Fix: [autoreplace] v->leave_depot_instantly was not always reset correctly
bjarni
parents: 5683
diff changeset
   288
	bool stopped;
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
   289
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
   290
	/* 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
   291
	 * 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
   292
	 * 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
   293
	 * 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
   294
	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
   295
		(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
   296
		-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
   297
	);
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
   298
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   299
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   300
	_current_player = v->owner;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   301
5794
8b26323509ce (svn r8353) -Cleanup: added IsPlayerBuildableVehicleType() to autoreplace code (duplicated code removal)
bjarni
parents: 5792
diff changeset
   302
	assert(IsPlayerBuildableVehicleType(v));
2579
8048bed6e67e (svn r3116) -Fix: [autoreplace] fixed issue where autorenewing/autoreplacing a plane could lock up an airport
bjarni
parents: 2575
diff changeset
   303
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
   304
	assert(v->vehstatus & VS_STOPPED); // the vehicle should have been stopped in VehicleEnteredDepotThisTick() if needed
2590
64e3f69a4ad8 (svn r3127) -Fix: [autoreplace] fixed a condition where a vehicle could fail to stop when autoreplacing
bjarni
parents: 2579
diff changeset
   305
5686
3300a2de25e7 (svn r8147) -Fix: [autoreplace] v->leave_depot_instantly was not always reset correctly
bjarni
parents: 5683
diff changeset
   306
	/* Remember the flag v->leave_depot_instantly because if we replace the vehicle, the vehicle holding this flag will be sold
3300a2de25e7 (svn r8147) -Fix: [autoreplace] v->leave_depot_instantly was not always reset correctly
bjarni
parents: 5683
diff changeset
   307
	 * If it is set, then we only stopped the vehicle to replace it (if needed) and we will need to start it again.
3300a2de25e7 (svn r8147) -Fix: [autoreplace] v->leave_depot_instantly was not always reset correctly
bjarni
parents: 5683
diff changeset
   308
	 * We also need to reset the flag since it should remain false except from when the vehicle enters a depot until autoreplace is handled in the same tick */
3300a2de25e7 (svn r8147) -Fix: [autoreplace] v->leave_depot_instantly was not always reset correctly
bjarni
parents: 5683
diff changeset
   309
	stopped = v->leave_depot_instantly;
3300a2de25e7 (svn r8147) -Fix: [autoreplace] v->leave_depot_instantly was not always reset correctly
bjarni
parents: 5683
diff changeset
   310
	v->leave_depot_instantly = false;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   311
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   312
	for (;;) {
2591
00e40545fc9c (svn r3128) -Fix: [autoreplace] fixed bug that made the player pay twice for autoreplacing and could end up with negative money
bjarni
parents: 2590
diff changeset
   313
		cost = 0;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   314
		w = v;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   315
		do {
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
   316
			if (w->type == VEH_TRAIN && IsMultiheaded(w) && !IsTrainEngine(w)) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   317
				/* we build the rear ends of multiheaded trains with the front ones */
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   318
				continue;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   319
			}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
   320
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   321
			// check if the vehicle should be replaced
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   322
			if (!p->engine_renew ||
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   323
					w->age - w->max_age < (p->engine_renew_months * 30) || // replace if engine is too old
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   324
					w->max_age == 0) { // rail cars got a max age of 0
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
   325
				/* If the vehicle belongs to a group, check if the group is protected from the global autoreplace.
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
   326
				   If not, chek if an global auto remplacement is defined */
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
   327
				if (IsValidGroupID(w->group_id)) {
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
   328
					if (!EngineHasReplacementForPlayer(p, w->engine_type, w->group_id) && (
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
   329
							GetGroup(w->group_id)->replace_protection ||
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
   330
							!EngineHasReplacementForPlayer(p, w->engine_type, DEFAULT_GROUP))) {
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
   331
						continue;
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
   332
					}
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
   333
				} else if (!EngineHasReplacementForPlayer(p, w->engine_type, DEFAULT_GROUP)) {
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   334
					continue;
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
   335
				}
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   336
			}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   337
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   338
			/* Now replace the vehicle */
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
   339
			temp_cost = ReplaceVehicle(&w, flags, cost);
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   340
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   341
			if (flags & DC_EXEC &&
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
   342
					(w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE)) {
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   343
				/* 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
   344
				 * pointers in order to avoid pointing to the old one for trains: these
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   345
				 * pointers should point to the front engine and not the cars
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   346
				 */
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   347
				v = w;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   348
			}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   349
3413
bebbb9d71cc8 (svn r4228) -Fix: [autoreplace] allow replacement of wagons even when the engine fails to be replaced
bjarni
parents: 3412
diff changeset
   350
			if (!CmdFailed(temp_cost)) {
bebbb9d71cc8 (svn r4228) -Fix: [autoreplace] allow replacement of wagons even when the engine fails to be replaced
bjarni
parents: 3412
diff changeset
   351
				cost += temp_cost;
bebbb9d71cc8 (svn r4228) -Fix: [autoreplace] allow replacement of wagons even when the engine fails to be replaced
bjarni
parents: 3412
diff changeset
   352
			}
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
   353
		} while (w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL);
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   354
4515
6b957ee9f07f (svn r6300) -Fix: FS#321 autoreplace / wagon removal
bjarni
parents: 4506
diff changeset
   355
		if (!(flags & DC_EXEC) && (p->money64 < (int32)(cost + p->engine_renew_money) || cost == 0)) {
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   356
			if (!check && p->money64 < (int32)(cost + p->engine_renew_money) && ( _local_player == v->owner ) && cost != 0) {
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   357
				StringID message;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   358
				SetDParam(0, v->unitnumber);
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   359
				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
   360
					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
   361
					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
   362
					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
   363
					case VEH_AIRCRAFT: message = STR_AIRCRAFT_AUTORENEW_FAILED;    break;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   364
						// This should never happen
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   365
					default: NOT_REACHED(); message = 0; break;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   366
				}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   367
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   368
				AddNewsItem(message, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   369
			}
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   370
			if (stopped) v->vehstatus &= ~VS_STOPPED;
4676
dd1d2d78d6f2 (svn r6580) -Fix r6552: [depot window] fixed issue where vehicles sometimes failed to restart after being replaced
bjarni
parents: 4675
diff changeset
   371
			if (display_costs) _current_player = OWNER_NONE;
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   372
			return CMD_ERROR;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   373
		}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   374
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   375
		if (flags & DC_EXEC) {
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
   376
			break; // we are done replacing since the loop ran once with DC_EXEC
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   377
		} else if (check) {
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   378
			/* It's a test only and we know that we can do this
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   379
			 * NOTE: payment for wagon removal is NOT included in this price */
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   380
			return cost;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   381
		}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   382
		// now we redo the loop, but this time we actually do stuff since we know that we can do it
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   383
		flags |= DC_EXEC;
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   384
	}
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   385
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
   386
	/* If setting is on to try not to exceed the old length of the train with the replacement */
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
   387
	if (v->type == VEH_TRAIN && p->renew_keep_length) {
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   388
		Vehicle *temp;
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   389
		w = v;
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
   390
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
   391
		while (v->u.rail.cached_total_length > old_total_length) {
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   392
			// the train is too long. We will remove cars one by one from the start of the train until it's short enough
5868
94430141c189 (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 5866
diff changeset
   393
			while (w != NULL && RailVehInfo(w->engine_type)->railveh_type != RAILVEH_WAGON) {
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   394
				w = GetNextVehicle(w);
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   395
			}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   396
			if (w == NULL) {
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   397
				// we failed to make the train short enough
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   398
				SetDParam(0, v->unitnumber);
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   399
				AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   400
				break;
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   401
			}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   402
			temp = w;
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   403
			w = GetNextVehicle(w);
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
   404
			DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
2678
71ca8ee16593 (svn r3220) -Fix: [autoreplace] Autoreplaced trains now replace their cargo to the train instead of just the new engine
bjarni
parents: 2677
diff changeset
   405
			MoveVehicleCargo(v, temp);
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   406
			cost += DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON);
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   407
		}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   408
	}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2611
diff changeset
   409
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   410
	if (stopped) v->vehstatus &= ~VS_STOPPED;
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   411
	if (display_costs) {
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   412
		if (IsLocalPlayer()) ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost);
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   413
		_current_player = OWNER_NONE;
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   414
	}
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
   415
	return cost;
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   416
}