annotate src/autoreplace_cmd.cpp @ 10193:5498150a3b12 draft

(svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
author frosch <frosch@openttd.org>
date Sat, 27 Sep 2008 14:58:46 +0000
parents 8a25a93ea7b4
children bf9ac6242b4b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
1 /* $Id$ */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
2
8999
0a4c639d1b8f (svn r12794) -Doc: added missing doxygen comment in autoreplace_cmd.cpp
bjarni <bjarni@openttd.org>
parents: 8997
diff changeset
3 /** @file autoreplace_cmd.cpp Deals with autoreplace execution but not the setup */
0a4c639d1b8f (svn r12794) -Doc: added missing doxygen comment in autoreplace_cmd.cpp
bjarni <bjarni@openttd.org>
parents: 8997
diff changeset
4
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
5 #include "stdafx.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
6 #include "openttd.h"
8254
7d580c9c41fb (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium <rubidium@openttd.org>
parents: 8230
diff changeset
7 #include "player_func.h"
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
8 #include "debug.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
9 #include "vehicle_gui.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
10 #include "train.h"
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
11 #include "aircraft.h"
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
12 #include "roadveh.h"
9936
bd9b969d5c38 (svn r14091) -Cleanup (r14083): Remove superfluous '#include's.
frosch <frosch@openttd.org>
parents: 9930
diff changeset
13 #include "rail.h"
8116
9cc845deddfe (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium <rubidium@openttd.org>
parents: 8114
diff changeset
14 #include "command_func.h"
9936
bd9b969d5c38 (svn r14091) -Cleanup (r14083): Remove superfluous '#include's.
frosch <frosch@openttd.org>
parents: 9930
diff changeset
15 #include "engine_base.h"
bd9b969d5c38 (svn r14091) -Cleanup (r14083): Remove superfluous '#include's.
frosch <frosch@openttd.org>
parents: 9930
diff changeset
16 #include "engine_func.h"
8144
d18c8a0bb638 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium <rubidium@openttd.org>
parents: 8131
diff changeset
17 #include "vehicle_func.h"
8131
e300ac8001ae (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 <rubidium@openttd.org>
parents: 8116
diff changeset
18 #include "functions.h"
8212
11263ebe590a (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium <rubidium@openttd.org>
parents: 8211
diff changeset
19 #include "autoreplace_func.h"
8363
e912fe1c1424 (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 <bjarni@openttd.org>
parents: 8362
diff changeset
20 #include "articulated_vehicles.h"
10122
dff578338ffb (svn r14306) -Codechange: unify the code to draw the vehicle list.
rubidium <rubidium@openttd.org>
parents: 10015
diff changeset
21 #include "core/alloc_func.hpp"
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
22
8264
2495310e220f (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium <rubidium@openttd.org>
parents: 8258
diff changeset
23 #include "table/strings.h"
2495310e220f (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium <rubidium@openttd.org>
parents: 8258
diff changeset
24
9889
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
25 /** Figure out if two engines got at least one type of cargo in common (refitting if needed)
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
26 * @param engine_a one of the EngineIDs
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
27 * @param engine_b the other EngineID
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
28 * @param type the type of the engines
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
29 * @return true if they can both carry the same type of cargo (or at least one of them got no capacity at all)
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
30 */
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
31 static bool EnginesGotCargoInCommon(EngineID engine_a, EngineID engine_b, VehicleType type)
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
32 {
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
33 uint32 available_cargos_a = GetUnionOfArticulatedRefitMasks(engine_a, type, true);
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
34 uint32 available_cargos_b = GetUnionOfArticulatedRefitMasks(engine_b, type, true);
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
35 return (available_cargos_a == 0 || available_cargos_b == 0 || (available_cargos_a & available_cargos_b) != 0);
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
36 }
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
37
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
38 /**
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
39 * Checks some basic properties whether autoreplace is allowed
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
40 * @param from Origin engine
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
41 * @param to Destination engine
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
42 * @param player Player to check for
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
43 * @return true if autoreplace is allowed
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
44 */
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
45 bool CheckAutoreplaceValidity(EngineID from, EngineID to, PlayerID player)
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
46 {
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
47 /* First we make sure that it's a valid type the user requested
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
48 * check that it's an engine that is in the engine array */
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
49 if (!IsEngineIndex(from) || !IsEngineIndex(to)) return false;
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
50
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
51 /* we can't replace an engine into itself (that would be autorenew) */
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
52 if (from == to) return false;
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
53
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
54 VehicleType type = GetEngine(from)->type;
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
55
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
56 /* check that the new vehicle type is available to the player and its type is the same as the original one */
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
57 if (!IsEngineBuildable(to, type, player)) return false;
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
58
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
59 switch (type) {
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
60 case VEH_TRAIN: {
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
61 const RailVehicleInfo *rvi_from = RailVehInfo(from);
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
62 const RailVehicleInfo *rvi_to = RailVehInfo(to);
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
63
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
64 /* make sure the railtypes are compatible */
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
65 if ((GetRailTypeInfo(rvi_from->railtype)->compatible_railtypes & GetRailTypeInfo(rvi_to->railtype)->compatible_railtypes) == 0) return false;
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
66
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
67 /* make sure we do not replace wagons with engines or vise versa */
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
68 if ((rvi_from->railveh_type == RAILVEH_WAGON) != (rvi_to->railveh_type == RAILVEH_WAGON)) return false;
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
69 break;
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
70 }
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
71
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
72 case VEH_ROAD:
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
73 /* make sure that we do not replace a tram with a normal road vehicles or vise versa */
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
74 if (HasBit(EngInfo(from)->misc_flags, EF_ROAD_TRAM) != HasBit(EngInfo(to)->misc_flags, EF_ROAD_TRAM)) return false;
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
75 break;
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
76
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
77 case VEH_AIRCRAFT:
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
78 /* make sure that we do not replace a plane with a helicopter or vise versa */
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
79 if ((AircraftVehInfo(from)->subtype & AIR_CTOL) != (AircraftVehInfo(to)->subtype & AIR_CTOL)) return false;
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
80 break;
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
81
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
82 default: break;
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
83 }
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
84
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
85 /* the engines needs to be able to carry the same cargo */
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
86 return EnginesGotCargoInCommon(from, to, type);
2bedf7c40bee (svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents: 9883
diff changeset
87 }
8628
97445f45ba39 (svn r12230) -Codechange: [autoreplace] made a function to detect if a vehicle needs autorenewing
bjarni <bjarni@openttd.org>
parents: 8469
diff changeset
88
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
89 /** Transfer cargo from a single (articulated )old vehicle to the new vehicle chain
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
90 * @param old_veh Old vehicle that will be sold
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
91 * @param new_head Head of the completely constructed new vehicle chain
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
92 * @param part_of_chain The vehicle is part of a train
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
93 */
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
94 static void TransferCargo(Vehicle *old_veh, Vehicle *new_head, bool part_of_chain)
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
95 {
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
96 assert(!part_of_chain || IsFrontEngine(new_head));
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
97 /* Loop through source parts */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
98 for (Vehicle *src = old_veh; src != NULL; src = src->Next()) {
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
99 if (!part_of_chain && src->type == VEH_TRAIN && src != old_veh && src != old_veh->u.rail.other_multiheaded_part && !IsArticulatedPart(src)) {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
100 /* Skip vehicles, which do not belong to old_veh */
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
101 src = GetLastEnginePart(src);
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
102 continue;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
103 }
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
104 if (src->cargo_type >= NUM_CARGO || src->cargo.Count() == 0) continue;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
105
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
106 /* Find free space in the new chain */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
107 for (Vehicle *dest = new_head; dest != NULL && src->cargo.Count() > 0; dest = dest->Next()) {
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
108 if (!part_of_chain && dest->type == VEH_TRAIN && dest != new_head && dest != new_head->u.rail.other_multiheaded_part && !IsArticulatedPart(src)) {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
109 /* Skip vehicles, which do not belong to new_head */
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
110 dest = GetLastEnginePart(dest);
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
111 continue;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
112 }
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
113 if (dest->cargo_type != src->cargo_type) continue;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
114
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
115 uint amount = min(src->cargo.Count(), dest->cargo_cap - dest->cargo.Count());
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
116 if (amount <= 0) continue;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
117
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
118 src->cargo.MoveTo(&dest->cargo, amount);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
119 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
120 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
121
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
122 /* Update train weight etc., the old vehicle will be sold anyway */
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
123 if (part_of_chain && new_head->type == VEH_TRAIN) TrainConsistChanged(new_head, true);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
124 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
125
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
126 /**
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
127 * Tests whether refit orders that applied to v will also apply to the new vehicle type
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
128 * @param v The vehicle to be replaced
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
129 * @param engine_type The type we want to replace with
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
130 * @return true iff all refit orders stay valid
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
131 */
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
132 static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, EngineID engine_type)
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
133 {
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
134 const Order *o;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
135 const Vehicle *u;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
136
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
137 uint32 union_refit_mask_a = GetUnionOfArticulatedRefitMasks(v->engine_type, v->type, false);
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
138 uint32 union_refit_mask_b = GetUnionOfArticulatedRefitMasks(engine_type, v->type, false);
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
139
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
140 if (v->type == VEH_TRAIN) {
7497
797ff0b0e0a5 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium <rubidium@openttd.org>
parents: 7492
diff changeset
141 u = v->First();
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
142 } else {
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
143 u = v;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
144 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
145
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
146 FOR_VEHICLE_ORDERS(u, o) {
8838
db9c6b0c9f07 (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium <rubidium@openttd.org>
parents: 8786
diff changeset
147 if (!o->IsRefit()) continue;
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
148 CargoID cargo_type = o->GetRefitCargo();
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
149
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
150 if (!HasBit(union_refit_mask_a, cargo_type)) continue;
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
151 if (!HasBit(union_refit_mask_b, cargo_type)) return false;
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
152 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
153
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
154 return true;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
155 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
156
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
157 /**
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
158 * Function to find what type of cargo to refit to when autoreplacing
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
159 * @param *v Original vehicle, that is being replaced
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
160 * @param engine_type The EngineID of the vehicle that is being replaced to
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
161 * @param part_of_chain The vehicle is part of a train
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
162 * @return The cargo type to replace to
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
163 * CT_NO_REFIT is returned if no refit is needed
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
164 * 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
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
165 */
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
166 static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type, bool part_of_chain)
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
167 {
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
168 CargoID cargo_type;
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
169
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
170 if (GetUnionOfArticulatedRefitMasks(engine_type, v->type, true) == 0) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
171
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
172 if (IsArticulatedVehicleCarryingDifferentCargos(v, &cargo_type)) return CT_INVALID; // We cannot refit to mixed cargos in an automated way
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
173
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
174 uint32 available_cargo_types = GetIntersectionOfArticulatedRefitMasks(engine_type, v->type, true);
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
175
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
176 if (cargo_type == CT_INVALID) {
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
177 if (v->type != VEH_TRAIN) return CT_NO_REFIT; // If the vehicle does not carry anything at all, every replacement is fine.
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
178
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
179 if (!part_of_chain) return CT_NO_REFIT;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
180
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
181 /* the old engine didn't have cargo capacity, but the new one does
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
182 * now we will figure out what cargo the train is carrying and refit to fit this */
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
183
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
184 for (v = v->First(); v != NULL; v = v->Next()) {
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
185 if (v->cargo_cap == 0) continue;
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
186 /* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
187 if (HasBit(available_cargo_types, v->cargo_type)) {
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
188 /* Do we have to refit the vehicle, or is it already carrying the right cargo? */
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
189 uint16 *default_capacity = GetCapacityOfArticulatedParts(engine_type, v->type);
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
190 for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
9883
369c99a0b939 (svn r14031) -Fix (r13850): Determining the refit cargo did not work, when the old vehicle did not carry anything but the new one did.
frosch <frosch@openttd.org>
parents: 9725
diff changeset
191 if (cid != v->cargo_type && default_capacity[cid] > 0) return v->cargo_type;
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
192 }
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
193
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
194 return CT_NO_REFIT;
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
195 }
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
196 }
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
197
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
198 return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
199 } else {
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
200 if (!HasBit(available_cargo_types, cargo_type)) return CT_INVALID; // We can't refit the vehicle to carry the cargo we want
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
201
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
202 if (part_of_chain && !VerifyAutoreplaceRefitForOrders(v, engine_type)) return CT_INVALID; // Some refit orders lose their effect
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
203
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
204 /* Do we have to refit the vehicle, or is it already carrying the right cargo? */
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
205 uint16 *default_capacity = GetCapacityOfArticulatedParts(engine_type, v->type);
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
206 for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
207 if (cid != cargo_type && default_capacity[cid] > 0) return cargo_type;
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
208 }
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
209
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
210 return CT_NO_REFIT;
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
211 }
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
212 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
213
8997
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
214 /** Get the EngineID of the replacement for a vehicle
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
215 * @param v The vehicle to find a replacement for
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
216 * @param p The vehicle's owner (it's faster to forward the pointer than refinding it)
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
217 * @return the EngineID of the replacement. INVALID_ENGINE if no buildable replacement is found
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
218 */
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
219 static EngineID GetNewEngineType(const Vehicle *v, const Player *p)
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
220 {
9725
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
221 assert(v->type != VEH_TRAIN || !IsArticulatedPart(v));
5b38d93ef486 (svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents: 9704
diff changeset
222
8997
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
223 if (v->type == VEH_TRAIN && IsRearDualheaded(v)) {
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
224 /* we build the rear ends of multiheaded trains with the front ones */
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
225 return INVALID_ENGINE;
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
226 }
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
227
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
228 EngineID e = EngineReplacementForPlayer(p, v->engine_type, v->group_id);
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
229
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
230 if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_player)) {
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
231 return e;
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
232 }
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
233
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
234 if (v->NeedsAutorenewing(p) && // replace if engine is too old
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
235 IsEngineBuildable(v->engine_type, v->type, _current_player)) { // engine can still be build
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
236 return v->engine_type;
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
237 }
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
238
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
239 return INVALID_ENGINE;
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
240 }
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
241
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
242 /** Builds and refits a replacement vehicle
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
243 * Important: The old vehicle is still in the original vehicle chain (used for determining the cargo when the old vehicle did not carry anything, but the new one does)
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
244 * @param old_veh A single (articulated/multiheaded) vehicle that shall be replaced.
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
245 * @param new_vehicle Returns the newly build and refittet vehicle
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
246 * @param part_of_chain The vehicle is part of a train
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
247 * @return cost or error
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
248 */
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
249 static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehicle, bool part_of_chain)
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
250 {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
251 *new_vehicle = NULL;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
252
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
253 /* Shall the vehicle be replaced? */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
254 const Player *p = GetPlayer(_current_player);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
255 EngineID e = GetNewEngineType(old_veh, p);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
256 if (e == INVALID_ENGINE) return CommandCost(); // neither autoreplace is set, nor autorenew is triggered
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
257
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
258 /* Does it need to be refitted */
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
259 CargoID refit_cargo = GetNewCargoTypeForReplace(old_veh, e, part_of_chain);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
260 if (refit_cargo == CT_INVALID) return CommandCost(); // incompatible cargos
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
261
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
262 /* Build the new vehicle */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
263 CommandCost cost = DoCommand(old_veh->tile, e, 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_veh));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
264 if (cost.Failed()) return cost;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
265
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
266 Vehicle *new_veh = GetVehicle(_new_vehicle_id);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
267 *new_vehicle = new_veh;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
268
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
269 /* Refit the vehicle if needed */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
270 if (refit_cargo != CT_NO_REFIT) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
271 cost.AddCost(DoCommand(0, new_veh->index, refit_cargo, DC_EXEC, GetCmdRefitVeh(new_veh)));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
272 assert(cost.Succeeded()); // This should be ensured by GetNewCargoTypeForReplace()
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
273 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
274
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
275 /* Try to reverse the vehicle, but do not care if it fails as the new type might not be reversible */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
276 if (new_veh->type == VEH_TRAIN && HasBit(old_veh->u.rail.flags, VRF_REVERSE_DIRECTION)) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
277 DoCommand(0, new_veh->index, true, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
278 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
279
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
280 return cost;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
281 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
282
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
283 /** Issue a start/stop command
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
284 * @param v a vehicle
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
285 * @param evaluate_callback shall the start/stop callback be evaluated?
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
286 * @return success or error
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
287 */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
288 static inline CommandCost StartStopVehicle(const Vehicle *v, bool evaluate_callback)
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
289 {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
290 return DoCommand(0, v->index, evaluate_callback ? 1 : 0, DC_EXEC | DC_AUTOREPLACE, CMD_START_STOP_VEHICLE);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
291 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
292
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
293 /** Issue a train vehicle move command
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
294 * @param v The vehicle to move
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
295 * @param after The vehicle to insert 'v' after, or NULL to start new chain
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
296 * @param whole_chain move all vehicles following 'v' (true), or only 'v' (false)
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
297 * @return success or error
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
298 */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
299 static inline CommandCost MoveVehicle(const Vehicle *v, const Vehicle *after, uint32 flags, bool whole_chain)
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
300 {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
301 return DoCommand(0, v->index | (after != NULL ? after->index : INVALID_VEHICLE) << 16, whole_chain ? 1 : 0, flags, CMD_MOVE_RAIL_VEHICLE);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
302 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
303
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
304 /** Copy head specific things to the new vehicle chain after it was successfully constructed
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
305 * @param old_head The old front vehicle (no wagons attached anymore)
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
306 * @param new_head The new head of the completely replaced vehicle chain
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
307 * @param flags the command flags to use
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
308 */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
309 static CommandCost CopyHeadSpecificThings(Vehicle *old_head, Vehicle *new_head, uint32 flags)
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
310 {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
311 CommandCost cost = CommandCost();
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
312
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
313 /* Share orders */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
314 if (cost.Succeeded() && old_head != new_head) cost.AddCost(DoCommand(0, (old_head->index << 16) | new_head->index, CO_SHARE, DC_EXEC, CMD_CLONE_ORDER));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
315
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
316 /* Copy group membership */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
317 if (cost.Succeeded() && old_head != new_head) cost.AddCost(DoCommand(0, old_head->group_id, new_head->index, DC_EXEC, CMD_ADD_VEHICLE_GROUP));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
318
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
319 /* Perform start/stop check whether the new vehicle suits newgrf restrictions etc. */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
320 if (cost.Succeeded()) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
321 /* Start the vehicle, might be denied by certain things */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
322 assert((new_head->vehstatus & VS_STOPPED) != 0);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
323 cost.AddCost(StartStopVehicle(new_head, true));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
324
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
325 /* Stop the vehicle again, but do not care about evil newgrfs allowing starting but not stopping :p */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
326 if (cost.Succeeded()) cost.AddCost(StartStopVehicle(new_head, false));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
327 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
328
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
329 /* Last do those things which do never fail (resp. we do not care about), but which are not undo-able */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
330 if (cost.Succeeded() && old_head != new_head && (flags & DC_EXEC) != 0) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
331 /* Copy vehicle name */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
332 if (old_head->name != NULL) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
333 _cmd_text = old_head->name;
10151
8a25a93ea7b4 (svn r14337) -Codechange: use CmdRename* and CMD_RENAME_* for vehicle, president and company renaming commands, too
smatz <smatz@openttd.org>
parents: 10122
diff changeset
334 DoCommand(0, new_head->index, 0, DC_EXEC | DC_AUTOREPLACE, CMD_RENAME_VEHICLE);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
335 _cmd_text = NULL;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
336 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
337
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
338 /* Copy other things which cannot be copied by a command and which shall not stay resetted from the build vehicle command */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
339 new_head->CopyVehicleConfigAndStatistics(old_head);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
340
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
341 /* Switch vehicle windows to the new vehicle, so they are not closed when the old vehicle is sold */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
342 ChangeVehicleViewWindow(old_head->index, new_head->index);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
343 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
344
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
345 return cost;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
346 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
347
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
348 /** Replace a single unit in a free wagon chain
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
349 * @param single_unit vehicle to let autoreplace/renew operator on
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
350 * @param flags command flags
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
351 * @param wagon_removal remove wagons when the resulting chain occupies more tiles than the old did
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
352 * @param nothing_to_do is set to 'false' when something was done (only valid when not failed)
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
353 * @return cost or error
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
354 */
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
355 static CommandCost ReplaceFreeUnit(Vehicle **single_unit, uint32 flags, bool *nothing_to_do)
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
356 {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
357 Vehicle *old_v = *single_unit;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
358 assert(old_v->type == VEH_TRAIN && !IsArticulatedPart(old_v) && !IsRearDualheaded(old_v));
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
359
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
360 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
361
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
362 /* Build and refit replacement vehicle */
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
363 Vehicle *new_v = NULL;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
364 cost.AddCost(BuildReplacementVehicle(old_v, &new_v, false));
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
365
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
366 /* Was a new vehicle constructed? */
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
367 if (cost.Succeeded() && new_v != NULL) {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
368 *nothing_to_do = false;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
369
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
370 if ((flags & DC_EXEC) != 0) {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
371 /* Move the new vehicle behind the old */
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
372 MoveVehicle(new_v, old_v, DC_EXEC, false);
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
373
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
374 /* Take over cargo
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
375 * Note: We do only transfer cargo from the old to the new vehicle.
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
376 * I.e. we do not transfer remaining cargo to other vehicles.
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
377 * Else you would also need to consider moving cargo to other free chains,
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
378 * or doing the same in ReplaceChain(), which would be quite troublesome.
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
379 */
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
380 TransferCargo(old_v, new_v, false);
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
381
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
382 *single_unit = new_v;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
383 }
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
384
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
385 if (cost.Succeeded()) {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
386 /* Sell the old vehicle */
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
387 cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v)));
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
388 }
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
389
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
390 /* If we are not in DC_EXEC undo everything */
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
391 if ((flags & DC_EXEC) == 0) {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
392 DoCommand(0, new_v->index, 0, DC_EXEC, GetCmdSellVeh(new_v));
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
393 }
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
394 }
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
395
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
396 return cost;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
397 }
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
398
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
399 /** Replace a whole vehicle chain
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
400 * @param chain vehicle chain to let autoreplace/renew operator on
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
401 * @param flags command flags
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
402 * @param wagon_removal remove wagons when the resulting chain occupies more tiles than the old did
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
403 * @param nothing_to_do is set to 'false' when something was done (only valid when not failed)
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
404 * @return cost or error
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
405 */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
406 static CommandCost ReplaceChain(Vehicle **chain, uint32 flags, bool wagon_removal, bool *nothing_to_do)
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
407 {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
408 Vehicle *old_head = *chain;
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
409 assert(old_head->IsPrimaryVehicle());
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
410
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
411 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
412
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
413 if (old_head->type == VEH_TRAIN) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
414 /* Store the length of the old vehicle chain, rounded up to whole tiles */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
415 uint16 old_total_length = (old_head->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
416
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
417 int num_units = 0; ///< Number of units in the chain
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
418 for (Vehicle *w = old_head; w != NULL; w = GetNextUnit(w)) num_units++;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
419
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
420 Vehicle **old_vehs = CallocT<Vehicle *>(num_units); ///< Will store vehicles of the old chain in their order
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
421 Vehicle **new_vehs = CallocT<Vehicle *>(num_units); ///< New vehicles corresponding to old_vehs or NULL if no replacement
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
422 Money *new_costs = MallocT<Money>(num_units); ///< Costs for buying and refitting the new vehicles
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
423
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
424 /* Collect vehicles and build replacements
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
425 * Note: The replacement vehicles can only successfully build as long as the old vehicles are still in their chain */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
426 int i;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
427 Vehicle *w;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
428 for (w = old_head, i = 0; w != NULL; w = GetNextUnit(w), i++) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
429 assert(i < num_units);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
430 old_vehs[i] = w;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
431
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
432 CommandCost ret = BuildReplacementVehicle(old_vehs[i], &new_vehs[i], true);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
433 cost.AddCost(ret);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
434 if (cost.Failed()) break;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
435
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
436 new_costs[i] = ret.GetCost();
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
437 if (new_vehs[i] != NULL) *nothing_to_do = false;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
438 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
439 Vehicle *new_head = (new_vehs[0] != NULL ? new_vehs[0] : old_vehs[0]);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
440
9968
4ff475766d4f (svn r14125) -Fix [FS#2237]: Segfault when autoreplace failed very early.
frosch <frosch@openttd.org>
parents: 9936
diff changeset
441 /* Note: When autoreplace has already failed here, old_vehs[] is not completely initialized. But it is also not needed. */
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
442 if (cost.Succeeded()) {
9968
4ff475766d4f (svn r14125) -Fix [FS#2237]: Segfault when autoreplace failed very early.
frosch <frosch@openttd.org>
parents: 9936
diff changeset
443 /* Separate the head, so we can start constructing the new chain */
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
444 Vehicle *second = GetNextUnit(old_head);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
445 if (second != NULL) cost.AddCost(MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
446
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
447 assert(GetNextUnit(new_head) == NULL);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
448
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
449 /* Append engines to the new chain
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
450 * We do this from back to front, so that the head of the temporary vehicle chain does not change all the time.
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
451 * OTOH the vehicle attach callback is more expensive this way :s */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
452 Vehicle *last_engine = NULL; ///< Shall store the last engine unit after this step
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
453 if (cost.Succeeded()) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
454 for (int i = num_units - 1; i > 0; i--) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
455 Vehicle *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
456
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
457 if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) continue;
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
458
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
459 if (last_engine == NULL) last_engine = append;
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
460 cost.AddCost(MoveVehicle(append, new_head, DC_EXEC, false));
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
461 if (cost.Failed()) break;
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
462 }
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
463 if (last_engine == NULL) last_engine = new_head;
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
464 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
465
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
466 /* When wagon removal is enabled and the new engines without any wagons are already longer than the old, we have to fail */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
467 if (cost.Succeeded() && wagon_removal && new_head->u.rail.cached_total_length > old_total_length) cost = CommandCost(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
468
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
469 /* Append/insert wagons into the new vehicle chain
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
470 * We do this from back to front, so we can stop when wagon removal or maximum train length (i.e. from mammoth-train setting) is triggered.
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
471 */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
472 if (cost.Succeeded()) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
473 for (int i = num_units - 1; i > 0; i--) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
474 assert(last_engine != NULL);
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
475 Vehicle *append = (new_vehs[i] != NULL ? new_vehs[i] : old_vehs[i]);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
476
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
477 if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
478 /* Insert wagon after 'last_engine' */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
479 CommandCost res = MoveVehicle(append, last_engine, DC_EXEC, false);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
480
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
481 if (res.Succeeded() && wagon_removal && new_head->u.rail.cached_total_length > old_total_length) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
482 MoveVehicle(append, NULL, DC_EXEC | DC_AUTOREPLACE, false);
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
483 break;
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
484 }
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
485
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
486 cost.AddCost(res);
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
487 if (cost.Failed()) break;
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
488 } else {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
489 /* We have reached 'last_engine', continue with the next engine towards the front */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
490 assert(append == last_engine);
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
491 last_engine = GetPrevUnit(last_engine);
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
492 }
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
493 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
494 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
495
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
496 /* Sell superfluous new vehicles that could not be inserted. */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
497 if (cost.Succeeded() && wagon_removal) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
498 for (int i = 1; i < num_units; i++) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
499 Vehicle *wagon = new_vehs[i];
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
500 if (wagon == NULL) continue;
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
501 if (wagon->First() == new_head) break;
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
502
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
503 assert(RailVehInfo(wagon->engine_type)->railveh_type == RAILVEH_WAGON);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
504
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
505 /* Sell wagon */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
506 CommandCost ret = DoCommand(0, wagon->index, 0, DC_EXEC, GetCmdSellVeh(wagon));
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
507 assert(ret.Succeeded());
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
508 new_vehs[i] = NULL;
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
509
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
510 /* Revert the money subtraction when the vehicle was built.
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
511 * This value is different from the sell value, esp. because of refitting */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
512 cost.AddCost(-new_costs[i]);
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
513 }
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
514 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
515
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
516 /* The new vehicle chain is constructed, now take over orders and everything... */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
517 if (cost.Succeeded()) cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags));
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
518
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
519 if (cost.Succeeded()) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
520 /* Success ! */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
521 if ((flags & DC_EXEC) != 0 && new_head != old_head) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
522 *chain = new_head;
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
523 }
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
524
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
525 /* Transfer cargo of old vehicles and sell them*/
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
526 for (int i = 0; i < num_units; i++) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
527 Vehicle *w = old_vehs[i];
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
528 /* Is the vehicle again part of the new chain?
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
529 * Note: We cannot test 'new_vehs[i] != NULL' as wagon removal might cause to remove both */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
530 if (w->First() == new_head) continue;
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
531
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
532 if ((flags & DC_EXEC) != 0) TransferCargo(w, new_head, true);
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
533
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
534 cost.AddCost(DoCommand(0, w->index, 0, flags, GetCmdSellVeh(w)));
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
535 if ((flags & DC_EXEC) != 0) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
536 old_vehs[i] = NULL;
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
537 if (i == 0) old_head = NULL;
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
538 }
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
539 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
540 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
541
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
542 /* If we are not in DC_EXEC undo everything, i.e. rearrange old vehicles.
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
543 * We do this from back to front, so that the head of the temporary vehicle chain does not change all the time.
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
544 * Note: The vehicle attach callback is disabled here :) */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
545 if ((flags & DC_EXEC) == 0) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
546 /* Separate the head, so we can reattach the old vehicles */
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
547 Vehicle *second = GetNextUnit(old_head);
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
548 if (second != NULL) MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
549
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
550 assert(GetNextUnit(old_head) == NULL);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
551
9969
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
552 for (int i = num_units - 1; i > 0; i--) {
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
553 CommandCost ret = MoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false);
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
554 assert(ret.Succeeded());
7a0342e24e35 (svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents: 9968
diff changeset
555 }
9968
4ff475766d4f (svn r14125) -Fix [FS#2237]: Segfault when autoreplace failed very early.
frosch <frosch@openttd.org>
parents: 9936
diff changeset
556 }
4ff475766d4f (svn r14125) -Fix [FS#2237]: Segfault when autoreplace failed very early.
frosch <frosch@openttd.org>
parents: 9936
diff changeset
557 }
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
558
9968
4ff475766d4f (svn r14125) -Fix [FS#2237]: Segfault when autoreplace failed very early.
frosch <frosch@openttd.org>
parents: 9936
diff changeset
559 /* Finally undo buying of new vehicles */
4ff475766d4f (svn r14125) -Fix [FS#2237]: Segfault when autoreplace failed very early.
frosch <frosch@openttd.org>
parents: 9936
diff changeset
560 if ((flags & DC_EXEC) == 0) {
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
561 for (int i = num_units - 1; i >= 0; i--) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
562 if (new_vehs[i] != NULL) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
563 DoCommand(0, new_vehs[i]->index, 0, DC_EXEC, GetCmdSellVeh(new_vehs[i]));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
564 new_vehs[i] = NULL;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
565 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
566 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
567 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
568
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
569 free(old_vehs);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
570 free(new_vehs);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
571 free(new_costs);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
572 } else {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
573 /* Build and refit replacement vehicle */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
574 Vehicle *new_head = NULL;
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
575 cost.AddCost(BuildReplacementVehicle(old_head, &new_head, true));
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
576
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
577 /* Was a new vehicle constructed? */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
578 if (cost.Succeeded() && new_head != NULL) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
579 *nothing_to_do = false;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
580
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
581 /* The new vehicle is constructed, now take over orders and everything... */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
582 cost.AddCost(CopyHeadSpecificThings(old_head, new_head, flags));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
583
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
584 if (cost.Succeeded()) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
585 /* The new vehicle is constructed, now take over cargo */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
586 if ((flags & DC_EXEC) != 0) {
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
587 TransferCargo(old_head, new_head, true);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
588 *chain = new_head;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
589 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
590
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
591 /* Sell the old vehicle */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
592 cost.AddCost(DoCommand(0, old_head->index, 0, flags, GetCmdSellVeh(old_head)));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
593 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
594
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
595 /* If we are not in DC_EXEC undo everything */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
596 if ((flags & DC_EXEC) == 0) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
597 DoCommand(0, new_head->index, 0, DC_EXEC, GetCmdSellVeh(new_head));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
598 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
599 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
600 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
601
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
602 return cost;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
603 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
604
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
605 /** Autoreplaces a vehicle
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
606 * Trains are replaced as a whole chain, free wagons in depot are replaced on their own
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
607 * @param tile not used
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
608 * @param flags type of operation
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
609 * @param p1 Index of vehicle
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
610 * @param p2 not used
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
611 */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
612 CommandCost CmdAutoreplaceVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
613 {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
614 CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, 0);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
615 bool nothing_to_do = true;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
616
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
617 if (!IsValidVehicleID(p1)) return CMD_ERROR;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
618 Vehicle *v = GetVehicle(p1);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
619 if (!CheckOwnership(v->owner)) return CMD_ERROR;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
620 if (!v->IsInDepot()) return CMD_ERROR;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
621 if (HASBITS(v->vehstatus, VS_CRASHED)) return CMD_ERROR;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
622
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
623 bool free_wagon = false;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
624 if (v->type == VEH_TRAIN) {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
625 if (IsArticulatedPart(v) || IsRearDualheaded(v)) return CMD_ERROR;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
626 free_wagon = !IsFrontEngine(v);
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
627 if (free_wagon && IsFrontEngine(v->First())) return CMD_ERROR;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
628 } else {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
629 if (!v->IsPrimaryVehicle()) return CMD_ERROR;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
630 }
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
631
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
632 const Player *p = GetPlayer(_current_player);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
633 bool wagon_removal = p->renew_keep_length;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
634
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
635 /* Test whether any replacement is set, before issuing a whole lot of commands that would end in nothing changed */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
636 Vehicle *w = v;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
637 bool any_replacements = false;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
638 while (w != NULL && !any_replacements) {
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
639 any_replacements = (GetNewEngineType(w, p) != INVALID_ENGINE);
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
640 w = (!free_wagon && w->type == VEH_TRAIN ? GetNextUnit(w) : NULL);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
641 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
642
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
643 if (any_replacements) {
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
644 bool was_stopped = free_wagon || ((v->vehstatus & VS_STOPPED) != 0);
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
645
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
646 /* Stop the vehicle */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
647 if (!was_stopped) cost.AddCost(StartStopVehicle(v, true));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
648 if (cost.Failed()) return cost;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
649
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
650 assert(v->IsStoppedInDepot());
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
651
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
652 /* We have to construct the new vehicle chain to test whether it is valid.
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
653 * Vehicle construction needs random bits, so we have to save the random seeds
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
654 * to prevent desyncs and to replay newgrf callbacks during DC_EXEC */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
655 SavedRandomSeeds saved_seeds;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
656 SaveRandomSeeds(&saved_seeds);
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
657 if (free_wagon) {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
658 cost.AddCost(ReplaceFreeUnit(&v, flags & ~DC_EXEC, &nothing_to_do));
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
659 } else {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
660 cost.AddCost(ReplaceChain(&v, flags & ~DC_EXEC, wagon_removal, &nothing_to_do));
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
661 }
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
662 RestoreRandomSeeds(saved_seeds);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
663
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
664 if (cost.Succeeded() && (flags & DC_EXEC) != 0) {
10193
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
665 CommandCost ret;
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
666 if (free_wagon) {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
667 ret = ReplaceFreeUnit(&v, flags, &nothing_to_do);
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
668 } else {
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
669 ret = ReplaceChain(&v, flags, wagon_removal, &nothing_to_do);
5498150a3b12 (svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents: 10151
diff changeset
670 }
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
671 assert(ret.Succeeded() && ret.GetCost() == cost.GetCost());
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
672 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
673
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
674 /* Restart the vehicle */
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
675 if (!was_stopped) cost.AddCost(StartStopVehicle(v, false));
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
676 }
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
677
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
678 if (cost.Succeeded() && nothing_to_do) cost = CommandCost(STR_AUTOREPLACE_NOTHING_TO_DO);
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
679 return cost;
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9889
diff changeset
680 }