Mercurial > hg > openttd
annotate src/autoreplace_cmd.cpp @ 9889:2bedf7c40bee draft
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
author | frosch <frosch@openttd.org> |
---|---|
date | Sun, 10 Aug 2008 15:02:21 +0000 |
parents | 369c99a0b939 |
children | 0cc9c6c36c43 |
rev | line source |
---|---|
6264
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
1 /* $Id$ */ |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
2 |
8999
0a4c639d1b8f
(svn r12794) -Doc: added missing doxygen comment in autoreplace_cmd.cpp
bjarni <bjarni@openttd.org>
parents:
8997
diff
changeset
|
3 /** @file autoreplace_cmd.cpp Deals with autoreplace execution but not the setup */ |
0a4c639d1b8f
(svn r12794) -Doc: added missing doxygen comment in autoreplace_cmd.cpp
bjarni <bjarni@openttd.org>
parents:
8997
diff
changeset
|
4 |
6264
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
5 #include "stdafx.h" |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
6 #include "openttd.h" |
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
|
7 #include "roadveh.h" |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
8 #include "ship.h" |
8763
d6e363672edb
(svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium <rubidium@openttd.org>
parents:
8736
diff
changeset
|
9 #include "news_func.h" |
8254
7d580c9c41fb
(svn r11818) -Codechange: split player.h into smaller pieces.
rubidium <rubidium@openttd.org>
parents:
8230
diff
changeset
|
10 #include "player_func.h" |
6264
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
11 #include "debug.h" |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
12 #include "vehicle_gui.h" |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
13 #include "train.h" |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
14 #include "aircraft.h" |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
15 #include "cargotype.h" |
6643
18d58b36b9b3
(svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium <rubidium@openttd.org>
parents:
6551
diff
changeset
|
16 #include "group.h" |
8114
2d6af5d7a142
(svn r11675) -Codechange: split the string types from the string functions.
rubidium <rubidium@openttd.org>
parents:
8064
diff
changeset
|
17 #include "strings_func.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
|
18 #include "command_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" |
8211
165064de4629
(svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium <rubidium@openttd.org>
parents:
8144
diff
changeset
|
21 #include "variables.h" |
8212
11263ebe590a
(svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium <rubidium@openttd.org>
parents:
8211
diff
changeset
|
22 #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
|
23 #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
|
24 |
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
|
25 #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
|
26 |
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
|
27 /** 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
|
28 * @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
|
29 * @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
|
30 * @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
|
31 * @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
|
32 */ |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
33 static bool EnginesGotCargoInCommon(EngineID engine_a, EngineID engine_b, VehicleType type) |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
34 { |
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 uint32 available_cargos_a = GetUnionOfArticulatedRefitMasks(engine_a, type, true); |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
36 uint32 available_cargos_b = GetUnionOfArticulatedRefitMasks(engine_b, type, true); |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
37 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
|
38 } |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
39 |
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 /** |
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 * 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
|
42 * @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
|
43 * @param to Destination engine |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
44 * @param player Player to check for |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
45 * @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
|
46 */ |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
47 bool CheckAutoreplaceValidity(EngineID from, EngineID to, PlayerID player) |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
48 { |
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 /* 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
|
50 * check that it's an engine that is in the engine array */ |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
51 if (!IsEngineIndex(from) || !IsEngineIndex(to)) return false; |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
52 |
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 /* 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
|
54 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
|
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 VehicleType type = GetEngine(from)->type; |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
57 |
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 /* check that the new vehicle type is available to the player and its type is the same as the original one */ |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
59 if (!IsEngineBuildable(to, type, player)) return false; |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
60 |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
61 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
|
62 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
|
63 const RailVehicleInfo *rvi_from = RailVehInfo(from); |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
64 const RailVehicleInfo *rvi_to = RailVehInfo(to); |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
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 /* make sure the railtypes are compatible */ |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
67 if ((GetRailTypeInfo(rvi_from->railtype)->compatible_railtypes & GetRailTypeInfo(rvi_to->railtype)->compatible_railtypes) == 0) return false; |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
68 |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
69 /* make sure we do not replace wagons with engines or vise versa */ |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
70 if ((rvi_from->railveh_type == RAILVEH_WAGON) != (rvi_to->railveh_type == RAILVEH_WAGON)) return false; |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
71 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
|
72 } |
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 |
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 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
|
75 /* make sure that we do not replace a tram with a normal road vehicles or vise versa */ |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
76 if (HasBit(EngInfo(from)->misc_flags, EF_ROAD_TRAM) != HasBit(EngInfo(to)->misc_flags, EF_ROAD_TRAM)) return false; |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
77 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
|
78 |
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 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
|
80 /* make sure that we do not replace a plane with a helicopter or vise versa */ |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
81 if ((AircraftVehInfo(from)->subtype & AIR_CTOL) != (AircraftVehInfo(to)->subtype & AIR_CTOL)) return false; |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
82 break; |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
83 |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
84 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
|
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 |
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 /* the engines needs to be able to carry the same cargo */ |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
88 return EnginesGotCargoInCommon(from, to, type); |
2bedf7c40bee
(svn r14037) -Fix (r8610): The autoreplace gui showed vehicle types for replacement which CmdSetAutoReplace() did not accept.
frosch <frosch@openttd.org>
parents:
9883
diff
changeset
|
89 } |
8628
97445f45ba39
(svn r12230) -Codechange: [autoreplace] made a function to detect if a vehicle needs autorenewing
bjarni <bjarni@openttd.org>
parents:
8469
diff
changeset
|
90 |
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
|
91 /* |
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
|
92 * move the cargo from one engine to another if 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
|
93 */ |
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
|
94 static void MoveVehicleCargo(Vehicle *dest, Vehicle *source) |
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
|
95 { |
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
|
96 Vehicle *v = dest; |
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
|
97 |
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
|
98 do { |
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
|
99 do { |
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
|
100 if (source->cargo_type != dest->cargo_type) |
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
|
101 continue; // cargo not compatible |
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
|
102 |
7010
f0f87c5a431e
(svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium <rubidium@openttd.org>
parents:
6990
diff
changeset
|
103 if (dest->cargo.Count() == dest->cargo_cap) |
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
|
104 continue; // the destination vehicle is already full |
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
|
105 |
7010
f0f87c5a431e
(svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium <rubidium@openttd.org>
parents:
6990
diff
changeset
|
106 uint units_moved = min(source->cargo.Count(), dest->cargo_cap - dest->cargo.Count()); |
f0f87c5a431e
(svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium <rubidium@openttd.org>
parents:
6990
diff
changeset
|
107 source->cargo.MoveTo(&dest->cargo, units_moved); |
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
|
108 |
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
|
109 // copy the age of the cargo |
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
|
110 dest->day_counter = source->day_counter; |
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
|
111 dest->tick_counter = source->tick_counter; |
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
|
112 |
7492
09743324277c
(svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium <rubidium@openttd.org>
parents:
7258
diff
changeset
|
113 } while (source->cargo.Count() > 0 && (dest = dest->Next()) != NULL); |
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
|
114 dest = v; |
7492
09743324277c
(svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium <rubidium@openttd.org>
parents:
7258
diff
changeset
|
115 } while ((source = source->Next()) != NULL); |
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
|
116 |
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
|
117 /* |
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
|
118 * The of the train will be incorrect at this moment. This is due |
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
|
119 * to the fact that removing the old wagon updates the weight of |
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
|
120 * the complete train, which is without the weight of cargo we just |
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
|
121 * moved back into some (of the) new wagon(s). |
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
|
122 */ |
9704
e1476334067a
(svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents:
9628
diff
changeset
|
123 if (dest->type == VEH_TRAIN) TrainConsistChanged(dest->First(), true); |
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
|
124 } |
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
|
125 |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
126 |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
127 /** |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
128 * 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
|
129 * @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
|
130 * @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
|
131 * @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
|
132 */ |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
133 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
|
134 { |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
135 const 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
|
136 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
|
137 |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
138 uint32 union_refit_mask_a = GetUnionOfArticulatedRefitMasks(v->engine_type, v->type, false); |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
139 uint32 union_refit_mask_b = GetUnionOfArticulatedRefitMasks(engine_type, v->type, false); |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
140 |
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
|
141 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
|
142 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
|
143 } 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
|
144 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
|
145 } |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
146 |
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 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
|
148 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
|
149 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
|
150 |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
151 if (!HasBit(union_refit_mask_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
|
152 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
|
153 } |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
154 |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
155 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
|
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 /** |
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 * 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
|
160 * @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
|
161 * @param engine_type The EngineID of the vehicle that is being replaced 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
|
162 * @return The cargo type to replace to |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
163 * CT_NO_REFIT is returned if no refit is needed |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
164 * CT_INVALID is returned when both old and new vehicle got cargo capacity and refitting the new one to the old one's cargo type isn't possible |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
165 */ |
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 static CargoID GetNewCargoTypeForReplace(Vehicle *v, EngineID engine_type) |
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
167 { |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
168 CargoID cargo_type; |
6264
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
169 |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
170 if (GetUnionOfArticulatedRefitMasks(engine_type, v->type, true) == 0) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
171 |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
172 if (IsArticulatedVehicleCarryingDifferentCargos(v, &cargo_type)) return CT_INVALID; // We cannot refit to mixed cargos in an automated way |
6264
37ecb2d0cdfb
(svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff
changeset
|
173 |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
174 uint32 available_cargo_types = GetIntersectionOfArticulatedRefitMasks(engine_type, v->type, true); |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
175 |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
176 if (cargo_type == CT_INVALID) { |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
177 if (v->type != VEH_TRAIN) return CT_NO_REFIT; // If the vehicle does not carry anything at all, every replacement is fine. |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
178 |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
179 /* 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
|
180 * 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
|
181 |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
182 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
|
183 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
|
184 /* 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
|
185 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
|
186 /* Do we have to refit the vehicle, or is it already carrying the right cargo? */ |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
187 uint16 *default_capacity = GetCapacityOfArticulatedParts(engine_type, v->type); |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
188 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
|
189 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
|
190 } |
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
|
191 |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
192 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
|
193 } |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
194 } |
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
|
195 |
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 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
|
197 } else { |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
198 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
|
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 if (!VerifyAutoreplaceRefitForOrders(v, engine_type)) return CT_INVALID; // Some refit orders loose their effect |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
201 |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
202 /* Do we have to refit the vehicle, or is it already carrying the right cargo? */ |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
203 uint16 *default_capacity = GetCapacityOfArticulatedParts(engine_type, v->type); |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
204 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
|
205 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
|
206 } |
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 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
|
209 } |
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
|
210 } |
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
|
211 |
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
|
212 /** Replaces a vehicle (used to be called autorenew) |
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
|
213 * This function is only called from MaybeReplaceVehicle() |
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
|
214 * Must be called with _current_player set to the owner of the vehicle |
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
|
215 * @param w Vehicle to replace |
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 * @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts |
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
|
217 * @param p The vehicle owner (faster than refinding the pointer) |
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 * @param new_engine_type The EngineID to replace to |
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
|
219 * @return value is cost of the replacement or CMD_ERROR |
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
|
220 */ |
9628
f1d95c5d5553
(svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz <smatz@openttd.org>
parents:
9280
diff
changeset
|
221 static CommandCost ReplaceVehicle(Vehicle **w, uint32 flags, Money total_cost, const Player *p, EngineID new_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
|
222 { |
6943
fd42cb9816c6
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents:
6800
diff
changeset
|
223 CommandCost cost; |
fd42cb9816c6
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents:
6800
diff
changeset
|
224 CommandCost sell_value; |
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
|
225 Vehicle *old_v = *w; |
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
|
226 const UnitID cached_unitnumber = old_v->unitnumber; |
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
|
227 bool new_front = false; |
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
|
228 Vehicle *new_v = NULL; |
8258
6017c5ebeb7e
(svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138 <peter1138@openttd.org>
parents:
8254
diff
changeset
|
229 char *vehicle_name = NULL; |
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
|
230 CargoID replacement_cargo_type; |
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
|
231 |
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
|
232 replacement_cargo_type = GetNewCargoTypeForReplace(old_v, new_engine_type); |
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
|
233 |
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
|
234 /* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo type */ |
6950
d2846442a133
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents:
6943
diff
changeset
|
235 if (replacement_cargo_type == CT_INVALID) return CommandCost(); |
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
|
236 |
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
|
237 sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, GetCmdSellVeh(old_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
|
238 |
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
|
239 /* We give the player a loan of the same amount as the sell value. |
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
|
240 * This is needed in case he needs the income from the sale to build the new vehicle. |
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
|
241 * We take it back if building fails or when we really sell the old engine */ |
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
|
242 SubtractMoneyFromPlayer(sell_value); |
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
|
243 |
9628
f1d95c5d5553
(svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz <smatz@openttd.org>
parents:
9280
diff
changeset
|
244 cost = DoCommand(old_v->tile, new_engine_type, 0, flags | DC_AUTOREPLACE, GetCmdBuildVeh(old_v)); |
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
|
245 if (CmdFailed(cost)) { |
6950
d2846442a133
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents:
6943
diff
changeset
|
246 /* Take back the money we just gave the player */ |
d2846442a133
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents:
6943
diff
changeset
|
247 sell_value.MultiplyCost(-1); |
d2846442a133
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents:
6943
diff
changeset
|
248 SubtractMoneyFromPlayer(sell_value); |
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
|
249 return cost; |
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
|
250 } |
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
|
251 |
6746
1faa0cbb6895
(svn r9981) -Fix: fixed a rare event that could cause autoreplace to run out of money and generate an error (spotted by elmex and UndernotBuilder)
bjarni <bjarni@openttd.org>
parents:
6735
diff
changeset
|
252 if (replacement_cargo_type != CT_NO_REFIT) { |
1faa0cbb6895
(svn r9981) -Fix: fixed a rare event that could cause autoreplace to run out of money and generate an error (spotted by elmex and UndernotBuilder)
bjarni <bjarni@openttd.org>
parents:
6735
diff
changeset
|
253 /* add refit cost */ |
6943
fd42cb9816c6
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents:
6800
diff
changeset
|
254 CommandCost refit_cost = GetRefitCost(new_engine_type); |
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
|
255 if (old_v->type == VEH_TRAIN && RailVehInfo(new_engine_type)->railveh_type == RAILVEH_MULTIHEAD) { |
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
|
256 /* Since it's a dualheaded engine we have to pay once more because the rear end is being refitted too. */ |
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
|
257 refit_cost.AddCost(refit_cost); |
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
|
258 } |
6950
d2846442a133
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents:
6943
diff
changeset
|
259 cost.AddCost(refit_cost); |
6746
1faa0cbb6895
(svn r9981) -Fix: fixed a rare event that could cause autoreplace to run out of money and generate an error (spotted by elmex and UndernotBuilder)
bjarni <bjarni@openttd.org>
parents:
6735
diff
changeset
|
260 } |
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
|
261 |
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
|
262 if (flags & DC_EXEC) { |
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
|
263 new_v = GetVehicle(_new_vehicle_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
|
264 *w = new_v; //we changed the vehicle, so MaybeReplaceVehicle needs to work on the new one. Now we tell it what the new one is |
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
|
265 |
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
|
266 /* refit if 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
|
267 if (replacement_cargo_type != CT_NO_REFIT) { |
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
|
268 if (CmdFailed(DoCommand(0, new_v->index, replacement_cargo_type, DC_EXEC, GetCmdRefitVeh(new_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
|
269 /* Being here shows a failure, which most likely is in GetNewCargoTypeForReplace() or incorrect estimation costs */ |
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
|
270 error("Autoreplace failed to refit. Replace engine %d to %d and refit to cargo %d", old_v->engine_type, new_v->engine_type, replacement_cargo_type); |
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
|
271 } |
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
|
272 } |
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
|
273 |
7928
4e8dfd103163
(svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13 <skidd13@openttd.org>
parents:
7527
diff
changeset
|
274 if (new_v->type == VEH_TRAIN && HasBit(old_v->u.rail.flags, VRF_REVERSE_DIRECTION) && !IsMultiheaded(new_v) && !(new_v->Next() != NULL && IsArticulatedPart(new_v->Next()))) { |
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
|
275 // we are autorenewing to a single engine, so we will turn it as the old one was turned as well |
7931
44ff7a6d801f
(svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13 <skidd13@openttd.org>
parents:
7928
diff
changeset
|
276 SetBit(new_v->u.rail.flags, VRF_REVERSE_DIRECTION); |
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
|
277 } |
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
|
278 |
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
|
279 if (old_v->type == VEH_TRAIN && !IsFrontEngine(old_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
|
280 /* this is a railcar. We need to move the car into the train |
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
|
281 * We add the new engine after the old one instead of replacing it. It will give the same result anyway when we |
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
|
282 * sell the old engine in a moment |
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
|
283 */ |
6735
8420208013c0
(svn r9967) -Fix (r9938): autoreplace would in certain conditions move dualheaded engines in a train (usually to the rear)
bjarni <bjarni@openttd.org>
parents:
6706
diff
changeset
|
284 /* Get the vehicle in front of the one we move out */ |
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
|
285 Vehicle *front = old_v->Previous(); |
8736
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
286 if (front == NULL) { |
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
287 /* It would appear that we have the front wagon of a row of wagons without engines */ |
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
288 Vehicle *next = old_v->Next(); |
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
289 if (next != NULL) { |
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
290 /* Move the chain to the new front wagon */ |
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
291 DoCommand(0, (new_v->index << 16) | next->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
292 } |
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
293 } else { |
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
294 /* If the vehicle in front is the rear end of a dualheaded engine, then we need to use the one in front of that one */ |
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
295 if (IsRearDualheaded(front)) front = front->Previous(); |
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
296 /* Now we move the old one out of the train */ |
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
297 DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
298 /* Add the new vehicle */ |
9061
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
299 CommandCost tmp_move = DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
300 if (CmdFailed(tmp_move)) { |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
301 cost.AddCost(tmp_move); |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
302 DoCommand(0, new_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN)); |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
303 } |
8736
22afd83e2b0a
(svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni <bjarni@openttd.org>
parents:
8680
diff
changeset
|
304 } |
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
|
305 } 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
|
306 // copy/clone the orders |
8469
066d7c3ca93d
(svn r12040) -Codechange: Change IsOrderListShared from a simple function to a class member(MagicBuzz).
belugas <belugas@openttd.org>
parents:
8363
diff
changeset
|
307 DoCommand(0, (old_v->index << 16) | new_v->index, old_v->IsOrderListShared() ? CO_SHARE : CO_COPY, DC_EXEC, CMD_CLONE_ORDER); |
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
|
308 new_v->cur_order_index = old_v->cur_order_index; |
9280
5c1bbffb1f51
(svn r13146) -Codechange: vehicle_base.h doesn't need to be included in vehicle_gui.h.
rubidium <rubidium@openttd.org>
parents:
9234
diff
changeset
|
309 ChangeVehicleViewWindow(old_v->index, new_v->index); |
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
|
310 new_v->profit_this_year = old_v->profit_this_year; |
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
|
311 new_v->profit_last_year = old_v->profit_last_year; |
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
|
312 new_v->service_interval = old_v->service_interval; |
6647
cf7f082a390d
(svn r9878) -Fix (9874): some vehicle count's were not properly updated on delete or autoreplace of vehicles.
rubidium <rubidium@openttd.org>
parents:
6643
diff
changeset
|
313 DoCommand(0, old_v->group_id, new_v->index, flags, CMD_ADD_VEHICLE_GROUP); |
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
|
314 new_front = 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
|
315 new_v->unitnumber = old_v->unitnumber; // use the same unit number |
6551
c7b61d76f63b
(svn r9753) -Fix [FS#732]: trains are lost after autorenewal/autoreplace
bjarni <bjarni@openttd.org>
parents:
6505
diff
changeset
|
316 new_v->dest_tile = old_v->dest_tile; |
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
|
317 |
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
|
318 new_v->current_order = old_v->current_order; |
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
|
319 if (old_v->type == VEH_TRAIN && GetNextVehicle(old_v) != NULL){ |
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
|
320 Vehicle *temp_v = GetNextVehicle(old_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
|
321 |
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
|
322 // move the entire train to the new engine, excluding the old engine |
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
|
323 if (IsMultiheaded(old_v) && temp_v == old_v->u.rail.other_multiheaded_part) { |
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
|
324 // we got front and rear of a multiheaded engine right after each other. We should work with the next in line instead |
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
|
325 temp_v = GetNextVehicle(temp_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
|
326 } |
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
|
327 |
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
|
328 if (temp_v != NULL) { |
9061
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
329 CommandCost tmp_move = DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
330 if (CmdFailed(tmp_move)) { |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
331 cost.AddCost(tmp_move); |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
332 DoCommand(0, temp_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN)); |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
333 } |
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
|
334 } |
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
|
335 } |
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
|
336 } |
9140
31020aa29732
(svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni <bjarni@openttd.org>
parents:
9081
diff
changeset
|
337 if (CmdSucceeded(cost)) { |
31020aa29732
(svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni <bjarni@openttd.org>
parents:
9081
diff
changeset
|
338 /* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */ |
31020aa29732
(svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni <bjarni@openttd.org>
parents:
9081
diff
changeset
|
339 MoveVehicleCargo(new_v->type == VEH_TRAIN ? new_v->First() : new_v, old_v); |
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
|
340 |
9140
31020aa29732
(svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni <bjarni@openttd.org>
parents:
9081
diff
changeset
|
341 /* Get the name of the old vehicle if it has a custom name. */ |
31020aa29732
(svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni <bjarni@openttd.org>
parents:
9081
diff
changeset
|
342 if (old_v->name != NULL) vehicle_name = strdup(old_v->name); |
31020aa29732
(svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni <bjarni@openttd.org>
parents:
9081
diff
changeset
|
343 } |
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
|
344 } else { // flags & DC_EXEC not set |
6950
d2846442a133
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents:
6943
diff
changeset
|
345 CommandCost tmp_move; |
7524
a415946a7ff1
(svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni <bjarni@openttd.org>
parents:
7497
diff
changeset
|
346 |
a415946a7ff1
(svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni <bjarni@openttd.org>
parents:
7497
diff
changeset
|
347 if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v)) { |
7527
6508f031bb0b
(svn r11046) -Codechange: added function to get the next movable (non-articulated, non-read end of dualheaded engine) vehicle in a train
bjarni <bjarni@openttd.org>
parents:
7526
diff
changeset
|
348 Vehicle *next_veh = GetNextUnit(old_v); // don't try to move the rear multiheaded engine or articulated parts |
7524
a415946a7ff1
(svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni <bjarni@openttd.org>
parents:
7497
diff
changeset
|
349 if (next_veh != NULL) { |
a415946a7ff1
(svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni <bjarni@openttd.org>
parents:
7497
diff
changeset
|
350 /* Verify that the wagons can be placed on the engine in question. |
a415946a7ff1
(svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni <bjarni@openttd.org>
parents:
7497
diff
changeset
|
351 * This is done by building an engine, test if the wagons can be added and then sell the test engine. */ |
9628
f1d95c5d5553
(svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz <smatz@openttd.org>
parents:
9280
diff
changeset
|
352 DoCommand(old_v->tile, new_engine_type, 0, DC_EXEC | DC_AUTOREPLACE, GetCmdBuildVeh(old_v)); |
7524
a415946a7ff1
(svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni <bjarni@openttd.org>
parents:
7497
diff
changeset
|
353 Vehicle *temp = GetVehicle(_new_vehicle_id); |
a415946a7ff1
(svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni <bjarni@openttd.org>
parents:
7497
diff
changeset
|
354 tmp_move = DoCommand(0, (temp->index << 16) | next_veh->index, 1, 0, CMD_MOVE_RAIL_VEHICLE); |
a415946a7ff1
(svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni <bjarni@openttd.org>
parents:
7497
diff
changeset
|
355 DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v)); |
a415946a7ff1
(svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni <bjarni@openttd.org>
parents:
7497
diff
changeset
|
356 } |
6800
567aecb1051a
(svn r10039) -Fix: [autoreplace] when trying to replace an engine that can't carry the wagons the old one carries (GRF restrictoin), then don't try to replace at all
bjarni <bjarni@openttd.org>
parents:
6747
diff
changeset
|
357 } |
567aecb1051a
(svn r10039) -Fix: [autoreplace] when trying to replace an engine that can't carry the wagons the old one carries (GRF restrictoin), then don't try to replace at all
bjarni <bjarni@openttd.org>
parents:
6747
diff
changeset
|
358 |
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
|
359 /* Ensure that the player will not end up having negative money while 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
|
360 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */ |
6952
752d9adbb23a
(svn r10207) -Codechange: remove the redundant player_money in favour of the money64, which is now renamed to player_money.
rubidium <rubidium@openttd.org>
parents:
6950
diff
changeset
|
361 if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) { |
6950
d2846442a133
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents:
6943
diff
changeset
|
362 /* Pay back the loan */ |
d2846442a133
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents:
6943
diff
changeset
|
363 sell_value.MultiplyCost(-1); |
d2846442a133
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents:
6943
diff
changeset
|
364 SubtractMoneyFromPlayer(sell_value); |
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
|
365 return CMD_ERROR; |
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
|
366 } |
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
|
367 } |
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
|
368 |
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
|
369 /* Take back the money we just gave the player just before building the vehicle |
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
|
370 * The player will get the same amount now that the sale actually takes place */ |
6950
d2846442a133
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents:
6943
diff
changeset
|
371 sell_value.MultiplyCost(-1); |
d2846442a133
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents:
6943
diff
changeset
|
372 SubtractMoneyFromPlayer(sell_value); |
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
|
373 |
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
|
374 /* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */ |
6950
d2846442a133
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents:
6943
diff
changeset
|
375 cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v))); |
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
|
376 |
9140
31020aa29732
(svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni <bjarni@openttd.org>
parents:
9081
diff
changeset
|
377 if (CmdFailed(cost)) return cost; |
31020aa29732
(svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni <bjarni@openttd.org>
parents:
9081
diff
changeset
|
378 |
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
|
379 if (new_front) { |
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
|
380 /* now we assign the old unitnumber to the new vehicle */ |
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
|
381 new_v->unitnumber = cached_unitnumber; |
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
|
382 } |
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
|
383 |
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
|
384 /* Transfer the name of the old vehicle */ |
8258
6017c5ebeb7e
(svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138 <peter1138@openttd.org>
parents:
8254
diff
changeset
|
385 if ((flags & DC_EXEC) && vehicle_name != NULL) { |
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
|
386 _cmd_text = vehicle_name; |
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
|
387 DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE); |
8258
6017c5ebeb7e
(svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138 <peter1138@openttd.org>
parents:
8254
diff
changeset
|
388 free(vehicle_name); |
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
|
389 } |
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
|
390 |
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
|
391 return cost; |
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
|
392 } |
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
|
393 |
9002
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
394 /** Removes wagons from a train until it get a certain length |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
395 * @param v The vehicle |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
396 * @param old_total_length The wanted max length |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
397 * @return The profit from selling the wagons |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
398 */ |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
399 static CommandCost WagonRemoval(Vehicle *v, uint16 old_total_length) |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
400 { |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
401 if (v->type != VEH_TRAIN) return CommandCost(); |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
402 Vehicle *front = v; |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
403 |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
404 CommandCost cost = CommandCost(); |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
405 |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
406 while (front->u.rail.cached_total_length > old_total_length) { |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
407 /* the train is too long. We will remove cars one by one from the start of the train until it's short enough */ |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
408 while (v != NULL && RailVehInfo(v->engine_type)->railveh_type != RAILVEH_WAGON) { |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
409 /* We move backwards in the train until we find a wagon */ |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
410 v = GetNextVehicle(v); |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
411 } |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
412 |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
413 if (v == NULL) { |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
414 /* We sold all the wagons and the train is still not short enough */ |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
415 SetDParam(0, front->unitnumber); |
9234
02827bf3dc03
(svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium <rubidium@openttd.org>
parents:
9141
diff
changeset
|
416 AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NS_ADVICE, front->index, 0); |
9002
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
417 return cost; |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
418 } |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
419 |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
420 /* We found a wagon we can sell */ |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
421 Vehicle *temp = v; |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
422 v = GetNextVehicle(v); |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
423 DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); // remove the wagon from the train |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
424 MoveVehicleCargo(front, temp); // move the cargo back on the train |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
425 cost.AddCost(DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON)); // sell the wagon |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
426 } |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
427 return cost; |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
428 } |
c9d48ed87e3a
(svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents:
8999
diff
changeset
|
429 |
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
|
430 /** 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
|
431 * @param v The vehicle to find a replacement for |
19392bf555ff
(svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents:
8996
diff
changeset
|
432 * @param p The vehicle's owner (it's faster to forward the pointer than refinding it) |
19392bf555ff
(svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents:
8996
diff
changeset
|
433 * @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
|
434 */ |
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
|
435 static EngineID GetNewEngineType(const Vehicle *v, const Player *p) |
19392bf555ff
(svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents:
8996
diff
changeset
|
436 { |
9725
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
437 assert(v->type != VEH_TRAIN || !IsArticulatedPart(v)); |
5b38d93ef486
(svn r13850) -Fix [FS#2146]: Fix resp. implement refitting of articulated vehicles during autoreplace.
frosch <frosch@openttd.org>
parents:
9704
diff
changeset
|
438 |
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
|
439 if (v->type == VEH_TRAIN && IsRearDualheaded(v)) { |
19392bf555ff
(svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents:
8996
diff
changeset
|
440 /* 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
|
441 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
|
442 } |
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
|
443 |
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
|
444 EngineID e = EngineReplacementForPlayer(p, v->engine_type, v->group_id); |
19392bf555ff
(svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents:
8996
diff
changeset
|
445 |
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
|
446 if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_player)) { |
19392bf555ff
(svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents:
8996
diff
changeset
|
447 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
|
448 } |
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
|
449 |
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
|
450 if (v->NeedsAutorenewing(p) && // replace if engine is too old |
19392bf555ff
(svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents:
8996
diff
changeset
|
451 IsEngineBuildable(v->engine_type, v->type, _current_player)) { // engine can still be build |
19392bf555ff
(svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents:
8996
diff
changeset
|
452 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
|
453 } |
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
|
454 |
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
|
455 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
|
456 } |
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
|
457 |
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
|
458 /** replaces a vehicle if it's set for autoreplace or is too old |
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
|
459 * (used to be called autorenew) |
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
|
460 * @param v The vehicle to replace |
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
|
461 * if the vehicle is a train, v needs to be the front engine |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
462 * @param flags |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
463 * @param display_costs If set, a cost animation is shown (only if DC_EXEC is set) |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
464 * This bool also takes autorenew money into consideration |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
465 * @return the costs, the success bool and sometimes an error message |
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
|
466 */ |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
467 CommandCost MaybeReplaceVehicle(Vehicle *v, uint32 flags, bool display_costs) |
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
|
468 { |
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
|
469 Vehicle *w; |
9061
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
470 Player *p = GetPlayer(v->owner); |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
471 CommandCost cost; |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
472 bool stopped = false; |
9061
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
473 BackuppedVehicle backup(true); |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
474 |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
475 /* We only want "real" vehicle types. */ |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
476 assert(IsPlayerBuildableVehicleType(v)); |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
477 |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
478 /* Ensure that this bool is cleared. */ |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
479 assert(!v->leave_depot_instantly); |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
480 |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
481 /* We can't sell if the current player don't own the vehicle. */ |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
482 assert(v->owner == _current_player); |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
483 |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
484 if (!v->IsInDepot()) { |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
485 /* The vehicle should be inside the depot */ |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
486 switch (v->type) { |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
487 default: NOT_REACHED(); |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
488 case VEH_TRAIN: return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED); break; |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
489 case VEH_ROAD: return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE); break; |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
490 case VEH_SHIP: return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN); break; |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
491 case VEH_AIRCRAFT: return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED); break; |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
492 } |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
493 } |
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
|
494 |
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
|
495 /* Remember the length in case we need to trim train later on |
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
|
496 * If it's not a train, the value is unused |
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
|
497 * round up to the length of the tiles used for the train instead of the train length instead |
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
|
498 * Useful when newGRF uses custom length */ |
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
|
499 uint16 old_total_length = (v->type == VEH_TRAIN ? |
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
|
500 (v->u.rail.cached_total_length + TILE_SIZE - 1) / TILE_SIZE * TILE_SIZE : |
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
|
501 -1 |
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
|
502 ); |
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
|
503 |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
504 if (!(v->vehstatus & VS_STOPPED)) { |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
505 /* The vehicle is moving so we better stop it before we might alter consist or sell it */ |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
506 v->vehstatus |= VS_STOPPED; |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
507 /* Remember that we stopped the vehicle */ |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
508 stopped = true; |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
509 } |
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
|
510 |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
511 { |
8230
5b61305fcdd4
(svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium <rubidium@openttd.org>
parents:
8212
diff
changeset
|
512 cost = CommandCost(EXPENSES_NEW_VEHICLES); |
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
|
513 w = 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
|
514 do { |
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
|
515 EngineID new_engine = GetNewEngineType(w, p); |
19392bf555ff
(svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents:
8996
diff
changeset
|
516 if (new_engine == INVALID_ENGINE) continue; |
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
|
517 |
9061
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
518 if (!backup.ContainsBackup()) { |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
519 /* We are going to try to replace a vehicle but we don't have any backup so we will make one. */ |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
520 backup.Backup(v, p); |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
521 } |
9141
ded2eb483235
(svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
bjarni <bjarni@openttd.org>
parents:
9140
diff
changeset
|
522 /* Now replace the vehicle. |
ded2eb483235
(svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
bjarni <bjarni@openttd.org>
parents:
9140
diff
changeset
|
523 * First we need to cache if it's the front vehicle as we need to update the v pointer if it is. |
ded2eb483235
(svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
bjarni <bjarni@openttd.org>
parents:
9140
diff
changeset
|
524 * If the replacement fails then we can't trust the data in the vehicle hence the reason to cache the result. */ |
ded2eb483235
(svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
bjarni <bjarni@openttd.org>
parents:
9140
diff
changeset
|
525 bool IsFront = w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE; |
ded2eb483235
(svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
bjarni <bjarni@openttd.org>
parents:
9140
diff
changeset
|
526 |
9061
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
527 cost.AddCost(ReplaceVehicle(&w, DC_EXEC, cost.GetCost(), p, new_engine)); |
6800
567aecb1051a
(svn r10039) -Fix: [autoreplace] when trying to replace an engine that can't carry the wagons the old one carries (GRF restrictoin), then don't try to replace at all
bjarni <bjarni@openttd.org>
parents:
6747
diff
changeset
|
528 |
9141
ded2eb483235
(svn r13002) -Fix (r13001): [autoreplace] previous fix broke updating of a pointer to the front vehicle in certain cases
bjarni <bjarni@openttd.org>
parents:
9140
diff
changeset
|
529 if (IsFront) { |
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
|
530 /* now we bought a new engine and sold the old one. We need to fix the |
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
|
531 * pointers in order to avoid pointing to the old one for trains: these |
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
|
532 * pointers should point to the front engine and not the cars |
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
|
533 */ |
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
|
534 v = w; |
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
|
535 } |
9140
31020aa29732
(svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni <bjarni@openttd.org>
parents:
9081
diff
changeset
|
536 } while (CmdSucceeded(cost) && w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != NULL); |
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
|
537 |
9140
31020aa29732
(svn r13001) -Fix [FS#1994](r12913): [autoreplace] we should stop working on vehicles right away if we fail to replace them and certainly not presume their data is valid
bjarni <bjarni@openttd.org>
parents:
9081
diff
changeset
|
538 if (CmdSucceeded(cost) && v->type == VEH_TRAIN && p->renew_keep_length) { |
9061
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
539 /* Remove wagons until the wanted length is reached */ |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
540 cost.AddCost(WagonRemoval(v, old_total_length)); |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
541 } |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
542 |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
543 if (flags & DC_QUERY_COST || cost.GetCost() == 0) { |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
544 /* We didn't do anything during the replace so we will just exit here */ |
9081
f77eb484bc54
(svn r12940) -Fix [FS#1974](r12913): [autoreplace] a vehicle backup should include the cargo packets in the vehicle as well
bjarni <bjarni@openttd.org>
parents:
9061
diff
changeset
|
545 v = backup.Restore(v, p); |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
546 if (stopped) v->vehstatus &= ~VS_STOPPED; |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
547 return cost; |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
548 } |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
549 |
9061
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
550 if (display_costs) { |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
551 /* We want to ensure that we will not get below p->engine_renew_money. |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
552 * We will not actually pay this amount. It's for display and checks only. */ |
9061
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
553 CommandCost tmp = cost; |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
554 tmp.AddCost((Money)p->engine_renew_money); |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
555 if (CmdSucceeded(tmp) && GetAvailableMoneyForCommand() < tmp.GetCost()) { |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
556 /* We don't have enough money so we will set cost to failed */ |
9061
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
557 cost.AddCost((Money)p->engine_renew_money); |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
558 cost.AddCost(CMD_ERROR); |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
559 } |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
560 } |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
561 |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
562 if (display_costs && CmdFailed(cost)) { |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
563 if (GetAvailableMoneyForCommand() < cost.GetCost() && IsLocalPlayer()) { |
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
|
564 StringID message; |
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
|
565 SetDParam(0, v->unitnumber); |
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
|
566 switch (v->type) { |
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
|
567 case VEH_TRAIN: message = STR_TRAIN_AUTORENEW_FAILED; break; |
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
|
568 case VEH_ROAD: message = STR_ROADVEHICLE_AUTORENEW_FAILED; break; |
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
|
569 case VEH_SHIP: message = STR_SHIP_AUTORENEW_FAILED; break; |
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
|
570 case VEH_AIRCRAFT: message = STR_AIRCRAFT_AUTORENEW_FAILED; break; |
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
|
571 // This should never happen |
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
|
572 default: NOT_REACHED(); message = 0; break; |
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
|
573 } |
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
|
574 |
9234
02827bf3dc03
(svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium <rubidium@openttd.org>
parents:
9141
diff
changeset
|
575 AddNewsItem(message, NS_ADVICE, v->index, 0); |
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
|
576 } |
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
|
577 } |
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
|
578 } |
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
|
579 |
9061
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
580 if (display_costs && IsLocalPlayer() && (flags & DC_EXEC) && CmdSucceeded(cost)) { |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
581 ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost()); |
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
582 } |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
583 |
9061
888b60158039
(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents:
9002
diff
changeset
|
584 if (!(flags & DC_EXEC) || CmdFailed(cost)) { |
9081
f77eb484bc54
(svn r12940) -Fix [FS#1974](r12913): [autoreplace] a vehicle backup should include the cargo packets in the vehicle as well
bjarni <bjarni@openttd.org>
parents:
9061
diff
changeset
|
585 v = backup.Restore(v, p); |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
586 } |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
587 |
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
588 /* Start the vehicle if we stopped it earlier */ |
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
|
589 if (stopped) v->vehstatus &= ~VS_STOPPED; |
8996
6a811008eb9f
(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents:
8962
diff
changeset
|
590 |
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
|
591 return cost; |
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
|
592 } |