Mercurial > hg > openttd
annotate src/autoreplace_cmd.cpp @ 13697:4e2fb4386b62 draft
(svn r18222) -Codechange: make the sign list window big font aware
author | rubidium <rubidium@openttd.org> |
---|---|
date | Sun, 22 Nov 2009 14:40:25 +0000 |
parents | 07fc7c78b30b |
children | 62925b6feb3b |
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 |
12778
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12690
diff
changeset
|
3 /* |
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12690
diff
changeset
|
4 * This file is part of OpenTTD. |
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12690
diff
changeset
|
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. |
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12690
diff
changeset
|
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12690
diff
changeset
|
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>. |
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12690
diff
changeset
|
8 */ |
bc7926153e19
(svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents:
12690
diff
changeset
|
9 |
8999
0a4c639d1b8f
(svn r12794) -Doc: added missing doxygen comment in autoreplace_cmd.cpp
bjarni <bjarni@openttd.org>
parents:
8997
diff
changeset
|
10 /** @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
|
11 |
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
|
12 #include "stdafx.h" |
10208
39cf8eebfda5
(svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium <rubidium@openttd.org>
parents:
10207
diff
changeset
|
13 #include "company_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
|
14 #include "train.h" |
9936
bd9b969d5c38
(svn r14091) -Cleanup (r14083): Remove superfluous '#include's.
frosch <frosch@openttd.org>
parents:
9930
diff
changeset
|
15 #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
|
16 #include "command_func.h" |
9936
bd9b969d5c38
(svn r14091) -Cleanup (r14083): Remove superfluous '#include's.
frosch <frosch@openttd.org>
parents:
9930
diff
changeset
|
17 #include "engine_base.h" |
bd9b969d5c38
(svn r14091) -Cleanup (r14083): Remove superfluous '#include's.
frosch <frosch@openttd.org>
parents:
9930
diff
changeset
|
18 #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
|
19 #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
|
20 #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
|
21 #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
|
22 #include "articulated_vehicles.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
|
23 |
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
|
24 #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
|
25 |
12690
1e91f0c18469
(svn r17147) -Fix [FS#3048]: Keep vehicle news and viewports following vehicles, when autoreplacing/renewing them.
frosch <frosch@openttd.org>
parents:
12619
diff
changeset
|
26 extern void ChangeVehicleViewports(VehicleID from_index, VehicleID to_index); |
1e91f0c18469
(svn r17147) -Fix [FS#3048]: Keep vehicle news and viewports following vehicles, when autoreplacing/renewing them.
frosch <frosch@openttd.org>
parents:
12619
diff
changeset
|
27 extern void ChangeVehicleNews(VehicleID from_index, VehicleID to_index); |
1e91f0c18469
(svn r17147) -Fix [FS#3048]: Keep vehicle news and viewports following vehicles, when autoreplacing/renewing them.
frosch <frosch@openttd.org>
parents:
12619
diff
changeset
|
28 extern void ChangeVehicleViewWindow(VehicleID from_index, VehicleID to_index); |
1e91f0c18469
(svn r17147) -Fix [FS#3048]: Keep vehicle news and viewports following vehicles, when autoreplacing/renewing them.
frosch <frosch@openttd.org>
parents:
12619
diff
changeset
|
29 |
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
|
30 /** 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
|
31 * @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
|
32 * @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
|
33 * @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
|
34 * @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
|
35 */ |
13221
4a355fe42b41
(svn r17728) -Cleanup: Remove some more unneeded/unused parameters.
frosch <frosch@openttd.org>
parents:
13218
diff
changeset
|
36 static bool EnginesHaveCargoInCommon(EngineID engine_a, EngineID engine_b) |
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
|
37 { |
13221
4a355fe42b41
(svn r17728) -Cleanup: Remove some more unneeded/unused parameters.
frosch <frosch@openttd.org>
parents:
13218
diff
changeset
|
38 uint32 available_cargos_a = GetUnionOfArticulatedRefitMasks(engine_a, true); |
4a355fe42b41
(svn r17728) -Cleanup: Remove some more unneeded/unused parameters.
frosch <frosch@openttd.org>
parents:
13218
diff
changeset
|
39 uint32 available_cargos_b = GetUnionOfArticulatedRefitMasks(engine_b, true); |
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
|
40 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
|
41 } |
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 |
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 /** |
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 * 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
|
45 * @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
|
46 * @param to Destination engine |
10207
c54d140df948
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents:
10198
diff
changeset
|
47 * @param company Company to check for |
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
|
48 * @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
|
49 */ |
10207
c54d140df948
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents:
10198
diff
changeset
|
50 bool CheckAutoreplaceValidity(EngineID from, EngineID to, CompanyID company) |
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
|
51 { |
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 /* 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
|
53 * check that it's an engine that is in the engine array */ |
11951
8bbe44fa53e2
(svn r16354) -Codechange: use 'new' pool accessors and methods for Engine too
smatz <smatz@openttd.org>
parents:
11949
diff
changeset
|
54 if (!Engine::IsValidID(from) || !Engine::IsValidID(to)) return false; |
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
|
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 /* 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
|
57 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
|
58 |
13218
03a409fa4c29
(svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
frosch <frosch@openttd.org>
parents:
13186
diff
changeset
|
59 const Engine *e_from = Engine::Get(from); |
03a409fa4c29
(svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
frosch <frosch@openttd.org>
parents:
13186
diff
changeset
|
60 const Engine *e_to = Engine::Get(to); |
03a409fa4c29
(svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
frosch <frosch@openttd.org>
parents:
13186
diff
changeset
|
61 VehicleType type = e_from->type; |
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
|
62 |
10207
c54d140df948
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents:
10198
diff
changeset
|
63 /* check that the new vehicle type is available to the company and its type is the same as the original one */ |
c54d140df948
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents:
10198
diff
changeset
|
64 if (!IsEngineBuildable(to, type, company)) return false; |
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
|
65 |
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 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
|
67 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
|
68 /* make sure the railtypes are compatible */ |
13218
03a409fa4c29
(svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
frosch <frosch@openttd.org>
parents:
13186
diff
changeset
|
69 if ((GetRailTypeInfo(e_from->u.rail.railtype)->compatible_railtypes & GetRailTypeInfo(e_to->u.rail.railtype)->compatible_railtypes) == 0) return false; |
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
|
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 /* make sure we do not replace wagons with engines or vise versa */ |
13218
03a409fa4c29
(svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
frosch <frosch@openttd.org>
parents:
13186
diff
changeset
|
72 if ((e_from->u.rail.railveh_type == RAILVEH_WAGON) != (e_to->u.rail.railveh_type == RAILVEH_WAGON)) return false; |
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
|
73 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
|
74 } |
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 |
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 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
|
77 /* make sure that we do not replace a tram with a normal road vehicles or vise versa */ |
13218
03a409fa4c29
(svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
frosch <frosch@openttd.org>
parents:
13186
diff
changeset
|
78 if (HasBit(e_from->info.misc_flags, EF_ROAD_TRAM) != HasBit(e_to->info.misc_flags, EF_ROAD_TRAM)) return false; |
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
|
79 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
|
80 |
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 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
|
82 /* make sure that we do not replace a plane with a helicopter or vise versa */ |
13218
03a409fa4c29
(svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
frosch <frosch@openttd.org>
parents:
13186
diff
changeset
|
83 if ((e_from->u.air.subtype & AIR_CTOL) != (e_to->u.air.subtype & AIR_CTOL)) return false; |
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
|
84 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
|
85 |
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 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
|
87 } |
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
|
88 |
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
|
89 /* the engines needs to be able to carry the same cargo */ |
13221
4a355fe42b41
(svn r17728) -Cleanup: Remove some more unneeded/unused parameters.
frosch <frosch@openttd.org>
parents:
13218
diff
changeset
|
90 return EnginesHaveCargoInCommon(from, to); |
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
|
91 } |
8628
97445f45ba39
(svn r12230) -Codechange: [autoreplace] made a function to detect if a vehicle needs autorenewing
bjarni <bjarni@openttd.org>
parents:
8469
diff
changeset
|
92 |
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 /** 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
|
94 * @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
|
95 * @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
|
96 * @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
|
97 */ |
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
|
98 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
|
99 { |
10198
6df8ceececc8
(svn r14411) -Fix (r14406): IsFrontEngine() is only valid for trains.
frosch <frosch@openttd.org>
parents:
10194
diff
changeset
|
100 assert(!part_of_chain || new_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
|
101 /* 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
|
102 for (Vehicle *src = old_veh; src != NULL; src = src->Next()) { |
12302
fc8e41f34b8c
(svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents:
12300
diff
changeset
|
103 if (!part_of_chain && src->type == VEH_TRAIN && src != old_veh && src != Train::From(old_veh)->other_multiheaded_part && !Train::From(src)->IsArticulatedPart()) { |
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
|
104 /* Skip vehicles, which do not belong to old_veh */ |
12306
d0ff55a976ad
(svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents:
12302
diff
changeset
|
105 src = Train::From(src)->GetLastEnginePart(); |
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
|
106 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
|
107 } |
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
|
108 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
|
109 |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
110 /* 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
|
111 for (Vehicle *dest = new_head; dest != NULL && src->cargo.Count() > 0; dest = dest->Next()) { |
12302
fc8e41f34b8c
(svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents:
12300
diff
changeset
|
112 if (!part_of_chain && dest->type == VEH_TRAIN && dest != new_head && dest != Train::From(new_head)->other_multiheaded_part && !Train::From(dest)->IsArticulatedPart()) { |
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
|
113 /* Skip vehicles, which do not belong to new_head */ |
12306
d0ff55a976ad
(svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents:
12302
diff
changeset
|
114 dest = Train::From(dest)->GetLastEnginePart(); |
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
|
115 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
|
116 } |
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
|
117 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
|
118 |
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 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
|
120 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
|
121 |
13291
07fc7c78b30b
(svn r17800) -Codechange: first steps into making CargoList a template
rubidium <rubidium@openttd.org>
parents:
13221
diff
changeset
|
122 src->cargo.MoveTo(&dest->cargo, amount, VehicleCargoList::MTA_UNLOAD, 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
|
123 } |
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 |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
126 /* Update train weight etc., the old vehicle will be sold anyway */ |
12114
f020ec6be498
(svn r16527) -Codechange: use static member functions instead of simple casts when converting Vehicle to specialised vehicle types. Includes safety check
smatz <smatz@openttd.org>
parents:
12089
diff
changeset
|
127 if (part_of_chain && new_head->type == VEH_TRAIN) TrainConsistChanged(Train::From(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
|
128 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
129 |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
130 /** |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
131 * 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
|
132 * @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
|
133 * @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
|
134 * @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
|
135 */ |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
136 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
|
137 { |
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
|
138 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
|
139 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
|
140 |
13221
4a355fe42b41
(svn r17728) -Cleanup: Remove some more unneeded/unused parameters.
frosch <frosch@openttd.org>
parents:
13218
diff
changeset
|
141 uint32 union_refit_mask_a = GetUnionOfArticulatedRefitMasks(v->engine_type, false); |
4a355fe42b41
(svn r17728) -Cleanup: Remove some more unneeded/unused parameters.
frosch <frosch@openttd.org>
parents:
13218
diff
changeset
|
142 uint32 union_refit_mask_b = GetUnionOfArticulatedRefitMasks(engine_type, false); |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
143 |
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
|
144 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
|
145 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
|
146 } 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
|
147 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
|
148 } |
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
|
149 |
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
|
150 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
|
151 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
|
152 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
|
153 |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
154 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
|
155 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
|
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 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
|
159 } |
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 |
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
|
161 /** |
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 * 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
|
163 * @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
|
164 * @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
|
165 * @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
|
166 * @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
|
167 * 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
|
168 * 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
|
169 */ |
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
|
170 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
|
171 { |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
172 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
|
173 |
13221
4a355fe42b41
(svn r17728) -Cleanup: Remove some more unneeded/unused parameters.
frosch <frosch@openttd.org>
parents:
13218
diff
changeset
|
174 if (GetUnionOfArticulatedRefitMasks(engine_type, true) == 0) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity |
9725
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 (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
|
177 |
13221
4a355fe42b41
(svn r17728) -Cleanup: Remove some more unneeded/unused parameters.
frosch <frosch@openttd.org>
parents:
13218
diff
changeset
|
178 uint32 available_cargo_types = GetIntersectionOfArticulatedRefitMasks(engine_type, true); |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
179 |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
180 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
|
181 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
|
182 |
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
|
183 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
|
184 |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
185 /* 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
|
186 * 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
|
187 |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
188 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
|
189 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
|
190 /* 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
|
191 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
|
192 /* Do we have to refit the vehicle, or is it already carrying the right cargo? */ |
13221
4a355fe42b41
(svn r17728) -Cleanup: Remove some more unneeded/unused parameters.
frosch <frosch@openttd.org>
parents:
13218
diff
changeset
|
193 CargoArray default_capacity = GetCapacityOfArticulatedParts(engine_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
|
194 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
|
195 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
|
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; |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
199 } |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
200 } |
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
|
201 |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
202 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
|
203 } else { |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
204 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
|
205 |
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
|
206 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
|
207 |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
208 /* Do we have to refit the vehicle, or is it already carrying the right cargo? */ |
13221
4a355fe42b41
(svn r17728) -Cleanup: Remove some more unneeded/unused parameters.
frosch <frosch@openttd.org>
parents:
13218
diff
changeset
|
209 CargoArray default_capacity = GetCapacityOfArticulatedParts(engine_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
|
210 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
|
211 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
|
212 } |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
213 |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
214 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
|
215 } |
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
|
216 } |
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
|
217 |
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
|
218 /** 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
|
219 * @param v The vehicle to find a replacement for |
10207
c54d140df948
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents:
10198
diff
changeset
|
220 * @param c The vehicle's owner (it's faster to forward the pointer than refinding it) |
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
|
221 * @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
|
222 */ |
10207
c54d140df948
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents:
10198
diff
changeset
|
223 static EngineID GetNewEngineType(const Vehicle *v, const Company *c) |
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
|
224 { |
12302
fc8e41f34b8c
(svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents:
12300
diff
changeset
|
225 assert(v->type != VEH_TRAIN || !Train::From(v)->IsArticulatedPart()); |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
226 |
12302
fc8e41f34b8c
(svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents:
12300
diff
changeset
|
227 if (v->type == VEH_TRAIN && Train::From(v)->IsRearDualheaded()) { |
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
|
228 /* 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
|
229 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
|
230 } |
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 |
10207
c54d140df948
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents:
10198
diff
changeset
|
232 EngineID e = EngineReplacementForCompany(c, v->engine_type, v->group_id); |
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
|
233 |
10207
c54d140df948
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents:
10198
diff
changeset
|
234 if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_company)) { |
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
|
235 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
|
236 } |
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 |
10207
c54d140df948
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents:
10198
diff
changeset
|
238 if (v->NeedsAutorenewing(c) && // replace if engine is too old |
c54d140df948
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents:
10198
diff
changeset
|
239 IsEngineBuildable(v->engine_type, v->type, _current_company)) { // engine can still be build |
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
|
240 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
|
241 } |
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
|
242 |
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
|
243 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
|
244 } |
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
|
245 |
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
|
246 /** 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
|
247 * 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
|
248 * @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
|
249 * @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
|
250 * @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
|
251 * @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
|
252 */ |
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
|
253 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
|
254 { |
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 *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
|
256 |
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 /* Shall the vehicle be replaced? */ |
11922
0a4b63f3f3c3
(svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents:
11913
diff
changeset
|
258 const Company *c = Company::Get(_current_company); |
10207
c54d140df948
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents:
10198
diff
changeset
|
259 EngineID e = GetNewEngineType(old_veh, c); |
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 (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
|
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 /* 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
|
263 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
|
264 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
|
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 /* 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
|
267 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
|
268 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
|
269 |
11922
0a4b63f3f3c3
(svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents:
11913
diff
changeset
|
270 Vehicle *new_veh = Vehicle::Get(_new_vehicle_id); |
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
|
271 *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
|
272 |
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 /* 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
|
274 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
|
275 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
|
276 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
|
277 } |
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 /* Try to reverse the vehicle, but do not care if it fails as the new type might not be reversible */ |
12114
f020ec6be498
(svn r16527) -Codechange: use static member functions instead of simple casts when converting Vehicle to specialised vehicle types. Includes safety check
smatz <smatz@openttd.org>
parents:
12089
diff
changeset
|
280 if (new_veh->type == VEH_TRAIN && HasBit(Train::From(old_veh)->flags, VRF_REVERSE_DIRECTION)) { |
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
|
281 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
|
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 |
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 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
|
285 } |
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 |
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 /** 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
|
288 * @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
|
289 * @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
|
290 * @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
|
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 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
|
293 { |
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 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
|
295 } |
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 |
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 /** 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
|
298 * @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
|
299 * @param after The vehicle to insert 'v' after, or NULL to start new chain |
13070
1c4c4ae8d716
(svn r17570) -Fix: a number of Doxygen warnings about missing parameters, which were sometimes missing and sometimes just typos
rubidium <rubidium@openttd.org>
parents:
13067
diff
changeset
|
300 * @param flags the command flags to use |
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
|
301 * @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
|
302 * @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
|
303 */ |
11090
df23c4e04638
(svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents:
10960
diff
changeset
|
304 static inline CommandCost MoveVehicle(const Vehicle *v, const Vehicle *after, DoCommandFlag flags, bool whole_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
|
305 { |
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 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
|
307 } |
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 /** 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
|
310 * @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
|
311 * @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
|
312 * @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
|
313 */ |
11090
df23c4e04638
(svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents:
10960
diff
changeset
|
314 static CommandCost CopyHeadSpecificThings(Vehicle *old_head, Vehicle *new_head, DoCommandFlag flags) |
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
|
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 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
|
317 |
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 /* 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
|
319 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
|
320 |
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 /* 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
|
322 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
|
323 |
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 /* 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
|
325 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
|
326 /* 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
|
327 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
|
328 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
|
329 |
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 /* 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
|
331 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
|
332 } |
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 |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
334 /* 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
|
335 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
|
336 /* 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
|
337 if (old_head->name != NULL) { |
10499
45ca88a8de7d
(svn r14754) -Codechange: get rid of _cmd_text and just pass it as (optional) parameter.
rubidium <rubidium@openttd.org>
parents:
10219
diff
changeset
|
338 DoCommand(0, new_head->index, 0, DC_EXEC | DC_AUTOREPLACE, CMD_RENAME_VEHICLE, old_head->name); |
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
|
339 } |
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 /* 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
|
342 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
|
343 |
12690
1e91f0c18469
(svn r17147) -Fix [FS#3048]: Keep vehicle news and viewports following vehicles, when autoreplacing/renewing them.
frosch <frosch@openttd.org>
parents:
12619
diff
changeset
|
344 /* Switch vehicle windows/news to the new vehicle, so they are not closed/deleted when the old vehicle is sold */ |
1e91f0c18469
(svn r17147) -Fix [FS#3048]: Keep vehicle news and viewports following vehicles, when autoreplacing/renewing them.
frosch <frosch@openttd.org>
parents:
12619
diff
changeset
|
345 ChangeVehicleViewports(old_head->index, new_head->index); |
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
|
346 ChangeVehicleViewWindow(old_head->index, new_head->index); |
12690
1e91f0c18469
(svn r17147) -Fix [FS#3048]: Keep vehicle news and viewports following vehicles, when autoreplacing/renewing them.
frosch <frosch@openttd.org>
parents:
12619
diff
changeset
|
347 ChangeVehicleNews(old_head->index, new_head->index); |
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
|
348 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
349 |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
350 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
|
351 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
352 |
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
|
353 /** 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
|
354 * @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
|
355 * @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
|
356 * @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
|
357 * @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
|
358 */ |
11090
df23c4e04638
(svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents:
10960
diff
changeset
|
359 static CommandCost ReplaceFreeUnit(Vehicle **single_unit, DoCommandFlag flags, bool *nothing_to_do) |
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
|
360 { |
12302
fc8e41f34b8c
(svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents:
12300
diff
changeset
|
361 Train *old_v = Train::From(*single_unit); |
fc8e41f34b8c
(svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents:
12300
diff
changeset
|
362 assert(!old_v->IsArticulatedPart() && !old_v->IsRearDualheaded()); |
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
|
363 |
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 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
|
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 /* 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
|
367 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
|
368 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
|
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 /* 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
|
371 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
|
372 *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
|
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 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
|
375 /* 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
|
376 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
|
377 |
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 /* 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
|
379 * 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
|
380 * 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
|
381 * 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
|
382 * 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
|
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 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
|
385 |
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 *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
|
387 } |
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 |
10194
bf9ac6242b4b
(svn r14407) -Cleanup (r14406): Remove a redundant test. (thanks SmatZ)
frosch <frosch@openttd.org>
parents:
10193
diff
changeset
|
389 /* Sell the old vehicle */ |
bf9ac6242b4b
(svn r14407) -Cleanup (r14406): Remove a redundant test. (thanks SmatZ)
frosch <frosch@openttd.org>
parents:
10193
diff
changeset
|
390 cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v))); |
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
|
391 |
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 /* 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
|
393 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
|
394 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
|
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 } |
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 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
|
399 } |
5498150a3b12
(svn r14406) -Fix [FS#Eddi]: Autoreplace did not work for vehicles in free wagon chains.
frosch <frosch@openttd.org>
parents:
10151
diff
changeset
|
400 |
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
|
401 /** 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
|
402 * @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
|
403 * @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
|
404 * @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
|
405 * @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
|
406 * @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
|
407 */ |
11090
df23c4e04638
(svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents:
10960
diff
changeset
|
408 static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon_removal, bool *nothing_to_do) |
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
|
409 { |
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 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
|
411 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
|
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 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
|
414 |
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 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
|
416 /* Store the length of the old vehicle chain, rounded up to whole tiles */ |
12114
f020ec6be498
(svn r16527) -Codechange: use static member functions instead of simple casts when converting Vehicle to specialised vehicle types. Includes safety check
smatz <smatz@openttd.org>
parents:
12089
diff
changeset
|
417 uint16 old_total_length = (Train::From(old_head)->tcache.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE; |
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
|
418 |
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 int num_units = 0; ///< Number of units in the chain |
12306
d0ff55a976ad
(svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents:
12302
diff
changeset
|
420 for (Train *w = Train::From(old_head); w != NULL; w = w->GetNextUnit()) num_units++; |
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
|
421 |
11985
e05790b0a6c6
(svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents:
11951
diff
changeset
|
422 Train **old_vehs = CallocT<Train *>(num_units); ///< Will store vehicles of the old chain in their order |
e05790b0a6c6
(svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents:
11951
diff
changeset
|
423 Train **new_vehs = CallocT<Train *>(num_units); ///< New vehicles corresponding to old_vehs or NULL if no replacement |
e05790b0a6c6
(svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents:
11951
diff
changeset
|
424 Money *new_costs = MallocT<Money>(num_units); ///< Costs for buying and refitting the new vehicles |
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
|
425 |
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 /* 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
|
427 * 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
|
428 int i; |
11985
e05790b0a6c6
(svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents:
11951
diff
changeset
|
429 Train *w; |
12306
d0ff55a976ad
(svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents:
12302
diff
changeset
|
430 for (w = Train::From(old_head), i = 0; w != NULL; w = w->GetNextUnit(), 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
|
431 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
|
432 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
|
433 |
11985
e05790b0a6c6
(svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents:
11951
diff
changeset
|
434 CommandCost ret = BuildReplacementVehicle(old_vehs[i], (Vehicle**)&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
|
435 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
|
436 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
|
437 |
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 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
|
439 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
|
440 } |
11985
e05790b0a6c6
(svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents:
11951
diff
changeset
|
441 Train *new_head = (new_vehs[0] != NULL ? new_vehs[0] : old_vehs[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
|
442 |
9968
4ff475766d4f
(svn r14125) -Fix [FS#2237]: Segfault when autoreplace failed very early.
frosch <frosch@openttd.org>
parents:
9936
diff
changeset
|
443 /* 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
|
444 if (cost.Succeeded()) { |
9968
4ff475766d4f
(svn r14125) -Fix [FS#2237]: Segfault when autoreplace failed very early.
frosch <frosch@openttd.org>
parents:
9936
diff
changeset
|
445 /* Separate the head, so we can start constructing the new chain */ |
12306
d0ff55a976ad
(svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents:
12302
diff
changeset
|
446 Train *second = Train::From(old_head)->GetNextUnit(); |
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
|
447 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
|
448 |
12306
d0ff55a976ad
(svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents:
12302
diff
changeset
|
449 assert(Train::From(new_head)->GetNextUnit() == 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
|
450 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
451 /* Append engines to the new chain |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
452 * 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
|
453 * OTOH the vehicle attach callback is more expensive this way :s */ |
11985
e05790b0a6c6
(svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents:
11951
diff
changeset
|
454 Train *last_engine = NULL; ///< Shall store the last engine unit after this step |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
455 if (cost.Succeeded()) { |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
456 for (int i = num_units - 1; i > 0; i--) { |
11985
e05790b0a6c6
(svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents:
11951
diff
changeset
|
457 Train *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
|
458 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
459 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
|
460 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
461 if (last_engine == NULL) last_engine = append; |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
462 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
|
463 if (cost.Failed()) break; |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
464 } |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
465 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
|
466 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
467 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
468 /* When wagon removal is enabled and the new engines without any wagons are already longer than the old, we have to fail */ |
12619
2f530108f787
(svn r17062) -Change: unify the naming of some 125 strings
rubidium <rubidium@openttd.org>
parents:
12306
diff
changeset
|
469 if (cost.Succeeded() && wagon_removal && new_head->tcache.cached_total_length > old_total_length) cost = CommandCost(STR_ERROR_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
|
470 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
471 /* Append/insert wagons into the new vehicle chain |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
472 * 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
|
473 */ |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
474 if (cost.Succeeded()) { |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
475 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
|
476 assert(last_engine != NULL); |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
477 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
|
478 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
479 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
|
480 /* Insert wagon after 'last_engine' */ |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
481 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
|
482 |
11987
81554d4e6d76
(svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents:
11986
diff
changeset
|
483 if (res.Succeeded() && wagon_removal && new_head->tcache.cached_total_length > old_total_length) { |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
484 MoveVehicle(append, NULL, DC_EXEC | DC_AUTOREPLACE, false); |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
485 break; |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
486 } |
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
|
487 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
488 cost.AddCost(res); |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
489 if (cost.Failed()) break; |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
490 } else { |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
491 /* 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
|
492 assert(append == last_engine); |
12306
d0ff55a976ad
(svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents:
12302
diff
changeset
|
493 last_engine = last_engine->GetPrevUnit(); |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
494 } |
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
|
495 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
496 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
497 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
498 /* 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
|
499 if (cost.Succeeded() && wagon_removal) { |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
500 for (int i = 1; i < num_units; i++) { |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
501 Vehicle *wagon = new_vehs[i]; |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
502 if (wagon == NULL) continue; |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
503 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
|
504 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
505 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
|
506 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
507 /* Sell wagon */ |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
508 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
|
509 assert(ret.Succeeded()); |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
510 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
|
511 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
512 /* 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
|
513 * 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
|
514 cost.AddCost(-new_costs[i]); |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
515 } |
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
|
516 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
517 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
518 /* 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
|
519 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
|
520 |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
521 if (cost.Succeeded()) { |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
522 /* Success ! */ |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
523 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
|
524 *chain = new_head; |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
525 } |
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
|
526 |
11342
fff0ccb2819a
(svn r15692) -Fix [FS#2721]: Just sell the old engines after autorenew/replace. Don't bother about trains exceeding the trainlimit, which will be sold anyway.
frosch <frosch@openttd.org>
parents:
11330
diff
changeset
|
527 /* Transfer cargo of old vehicles and sell them */ |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
528 for (int i = 0; i < num_units; i++) { |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
529 Vehicle *w = old_vehs[i]; |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
530 /* 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
|
531 * 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
|
532 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
|
533 |
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
|
534 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
|
535 |
11342
fff0ccb2819a
(svn r15692) -Fix [FS#2721]: Just sell the old engines after autorenew/replace. Don't bother about trains exceeding the trainlimit, which will be sold anyway.
frosch <frosch@openttd.org>
parents:
11330
diff
changeset
|
536 /* Sell the vehicle. |
fff0ccb2819a
(svn r15692) -Fix [FS#2721]: Just sell the old engines after autorenew/replace. Don't bother about trains exceeding the trainlimit, which will be sold anyway.
frosch <frosch@openttd.org>
parents:
11330
diff
changeset
|
537 * Note: This might temporarly construct new trains, so use DC_AUTOREPLACE to prevent |
fff0ccb2819a
(svn r15692) -Fix [FS#2721]: Just sell the old engines after autorenew/replace. Don't bother about trains exceeding the trainlimit, which will be sold anyway.
frosch <frosch@openttd.org>
parents:
11330
diff
changeset
|
538 * it from failing due to engine limits. */ |
fff0ccb2819a
(svn r15692) -Fix [FS#2721]: Just sell the old engines after autorenew/replace. Don't bother about trains exceeding the trainlimit, which will be sold anyway.
frosch <frosch@openttd.org>
parents:
11330
diff
changeset
|
539 cost.AddCost(DoCommand(0, w->index, 0, flags | DC_AUTOREPLACE, GetCmdSellVeh(w))); |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
540 if ((flags & DC_EXEC) != 0) { |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
541 old_vehs[i] = NULL; |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
542 if (i == 0) old_head = NULL; |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
543 } |
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
|
544 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
545 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
546 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
547 /* 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
|
548 * 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
|
549 * Note: The vehicle attach callback is disabled here :) */ |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
550 if ((flags & DC_EXEC) == 0) { |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
551 /* Separate the head, so we can reattach the old vehicles */ |
12306
d0ff55a976ad
(svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents:
12302
diff
changeset
|
552 Train *second = Train::From(old_head)->GetNextUnit(); |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
553 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
|
554 |
12306
d0ff55a976ad
(svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents:
12302
diff
changeset
|
555 assert(Train::From(old_head)->GetNextUnit() == 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
|
556 |
9969
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
557 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
|
558 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
|
559 assert(ret.Succeeded()); |
7a0342e24e35
(svn r14126) -Codechange (r14125): Add some tabs.
frosch <frosch@openttd.org>
parents:
9968
diff
changeset
|
560 } |
9968
4ff475766d4f
(svn r14125) -Fix [FS#2237]: Segfault when autoreplace failed very early.
frosch <frosch@openttd.org>
parents:
9936
diff
changeset
|
561 } |
4ff475766d4f
(svn r14125) -Fix [FS#2237]: Segfault when autoreplace failed very early.
frosch <frosch@openttd.org>
parents:
9936
diff
changeset
|
562 } |
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
|
563 |
9968
4ff475766d4f
(svn r14125) -Fix [FS#2237]: Segfault when autoreplace failed very early.
frosch <frosch@openttd.org>
parents:
9936
diff
changeset
|
564 /* 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
|
565 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
|
566 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
|
567 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
|
568 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
|
569 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
|
570 } |
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 } |
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 } |
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 |
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 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
|
575 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
|
576 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
|
577 } 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
|
578 /* 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
|
579 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
|
580 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
|
581 |
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 /* 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
|
583 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
|
584 *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
|
585 |
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 /* 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
|
587 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
|
588 |
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 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
|
590 /* 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
|
591 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
|
592 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
|
593 *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
|
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 |
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 /* 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
|
597 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
|
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 /* 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
|
601 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
|
602 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
|
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 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
605 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
606 |
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 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
|
608 } |
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 |
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
|
610 /** 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
|
611 * 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
|
612 * @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
|
613 * @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
|
614 * @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
|
615 * @param p2 not used |
13067
082cdb4504ac
(svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents:
12778
diff
changeset
|
616 * @param text unused |
082cdb4504ac
(svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents:
12778
diff
changeset
|
617 * @return the cost of this operation or an error |
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
|
618 */ |
11090
df23c4e04638
(svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents:
10960
diff
changeset
|
619 CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) |
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
|
620 { |
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 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
|
622 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
|
623 |
11949
c89301974eb8
(svn r16352) -Codechange: use PoolItem::GetIfValid() instead of PoolItem::IsValidID() and PoolItem::Get()
smatz <smatz@openttd.org>
parents:
11924
diff
changeset
|
624 Vehicle *v = Vehicle::GetIfValid(p1); |
c89301974eb8
(svn r16352) -Codechange: use PoolItem::GetIfValid() instead of PoolItem::IsValidID() and PoolItem::Get()
smatz <smatz@openttd.org>
parents:
11924
diff
changeset
|
625 if (v == NULL) return CMD_ERROR; |
c89301974eb8
(svn r16352) -Codechange: use PoolItem::GetIfValid() instead of PoolItem::IsValidID() and PoolItem::Get()
smatz <smatz@openttd.org>
parents:
11924
diff
changeset
|
626 |
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
|
627 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
|
628 if (!v->IsInDepot()) return CMD_ERROR; |
12089
482bdd64bbe8
(svn r16498) -Codechange: Remove hardly used HASBITS.
frosch <frosch@openttd.org>
parents:
11987
diff
changeset
|
629 if (v->vehstatus & VS_CRASHED) return CMD_ERROR; |
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
|
630 |
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
|
631 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
|
632 if (v->type == VEH_TRAIN) { |
12300
7ec79afe8c18
(svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents:
12279
diff
changeset
|
633 Train *t = Train::From(v); |
12302
fc8e41f34b8c
(svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents:
12300
diff
changeset
|
634 if (t->IsArticulatedPart() || t->IsRearDualheaded()) return CMD_ERROR; |
12300
7ec79afe8c18
(svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents:
12279
diff
changeset
|
635 free_wagon = !t->IsFrontEngine(); |
7ec79afe8c18
(svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents:
12279
diff
changeset
|
636 if (free_wagon && t->First()->IsFrontEngine()) return CMD_ERROR; |
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
|
637 } 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
|
638 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
|
639 } |
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 |
11922
0a4b63f3f3c3
(svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents:
11913
diff
changeset
|
641 const Company *c = Company::Get(_current_company); |
11913
f67b6243e05a
(svn r16315) -Codechange: move the autorenew settings to a new CompanySettings struct
yexo <yexo@openttd.org>
parents:
11342
diff
changeset
|
642 bool wagon_removal = c->settings.renew_keep_length; |
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
|
643 |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
644 /* 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
|
645 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
|
646 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
|
647 while (w != NULL && !any_replacements) { |
10207
c54d140df948
(svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents:
10198
diff
changeset
|
648 any_replacements = (GetNewEngineType(w, c) != INVALID_ENGINE); |
12306
d0ff55a976ad
(svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents:
12302
diff
changeset
|
649 w = (!free_wagon && w->type == VEH_TRAIN ? Train::From(w)->GetNextUnit() : 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
|
650 } |
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 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
|
653 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
|
654 |
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 /* 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
|
656 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
|
657 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
|
658 |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
659 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
|
660 |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
661 /* 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
|
662 * 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
|
663 * 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
|
664 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
|
665 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
|
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 cost.AddCost(ReplaceFreeUnit(&v, flags & ~DC_EXEC, ¬hing_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 cost.AddCost(ReplaceChain(&v, flags & ~DC_EXEC, wagon_removal, ¬hing_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 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
|
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 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
|
674 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
|
675 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
|
676 ret = ReplaceFreeUnit(&v, flags, ¬hing_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
|
677 } 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
|
678 ret = ReplaceChain(&v, flags, wagon_removal, ¬hing_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
|
679 } |
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
|
680 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
|
681 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
682 |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
683 /* 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
|
684 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
|
685 } |
0cc9c6c36c43
(svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents:
9889
diff
changeset
|
686 |
12619
2f530108f787
(svn r17062) -Change: unify the naming of some 125 strings
rubidium <rubidium@openttd.org>
parents:
12306
diff
changeset
|
687 if (cost.Succeeded() && nothing_to_do) cost = CommandCost(STR_ERROR_AUTOREPLACE_NOTHING_TO_DO); |
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
|
688 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
|
689 } |