annotate src/autoreplace_cmd.cpp @ 8996:6a811008eb9f draft

(svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost This allowed cleaning up the code in MaybeReplaceVehicle()
author bjarni <bjarni@openttd.org>
date Sat, 19 Apr 2008 15:14:20 +0000
parents 322e2779f67a
children 19392bf555ff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
1 /* $Id$ */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
2
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
3 #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
4 #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
5 #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
6 #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
7 #include "news_func.h"
8254
7d580c9c41fb (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium <rubidium@openttd.org>
parents: 8230
diff changeset
8 #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
9 #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
10 #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
11 #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
12 #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
13 #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
14 #include "group.h"
8114
2d6af5d7a142 (svn r11675) -Codechange: split the string types from the string functions.
rubidium <rubidium@openttd.org>
parents: 8064
diff changeset
15 #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
16 #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
17 #include "vehicle_func.h"
8131
e300ac8001ae (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents: 8116
diff changeset
18 #include "functions.h"
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
19 #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
20 #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
21 #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
22
8264
2495310e220f (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium <rubidium@openttd.org>
parents: 8258
diff changeset
23 #include "table/strings.h"
2495310e220f (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium <rubidium@openttd.org>
parents: 8258
diff changeset
24
8628
97445f45ba39 (svn r12230) -Codechange: [autoreplace] made a function to detect if a vehicle needs autorenewing
bjarni <bjarni@openttd.org>
parents: 8469
diff changeset
25
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
26 /*
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
27 * 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
28 */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
29 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
30 {
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
31 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
32
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
33 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
34 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
35 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
36 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
37
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
38 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
39 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
40
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
41 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
42 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
43
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
44 // 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
45 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
46 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
47
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
48 } 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
49 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
50 } 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
51
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
52 /*
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
53 * 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
54 * 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
55 * 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
56 * 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
57 */
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
58 if (dest->type == VEH_TRAIN) TrainConsistChanged(dest->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
59 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
60
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
61 static bool VerifyAutoreplaceRefitForOrders(const Vehicle *v, const 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
62 {
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
63 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
64 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
65
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
66 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
67 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
68 } 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
69 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
70 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
71
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
72 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
73 if (!o->IsRefit()) continue;
db9c6b0c9f07 (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium <rubidium@openttd.org>
parents: 8786
diff changeset
74 if (!CanRefitTo(v->engine_type, o->GetRefitCargo())) continue;
db9c6b0c9f07 (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium <rubidium@openttd.org>
parents: 8786
diff changeset
75 if (!CanRefitTo(engine_type, o->GetRefitCargo())) 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
76 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
77
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
78 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
79 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
80
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
81 /**
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
82 * 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
83 * @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
84 * @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
85 * @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
86 * 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
87 * CT_INVALID is returned when both old and new vehicle got cargo capacity and refitting the new one to the old one's cargo type isn't possible
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
88 */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
89 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
90 {
6505
14da45e0e4be (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni <bjarni@openttd.org>
parents: 6491
diff changeset
91 CargoID new_cargo_type = GetEngineCargoType(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
92
6505
14da45e0e4be (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni <bjarni@openttd.org>
parents: 6491
diff changeset
93 if (new_cargo_type == CT_INVALID) return CT_NO_REFIT; // Don't try to refit an engine with no cargo capacity
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
94
8362
8212ccaf2858 (svn r11928) -Fix (r6393): [autoreplace] autoreplace could refit train engines to the wrong cargo type if the old engine had no cargo capacity and the new one had
bjarni <bjarni@openttd.org>
parents: 8264
diff changeset
95 if (v->cargo_cap != 0 && (v->cargo_type == new_cargo_type || CanRefitTo(engine_type, v->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
96 if (VerifyAutoreplaceRefitForOrders(v, 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
97 return v->cargo_type == new_cargo_type ? (CargoID)CT_NO_REFIT : v->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
98 } 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
99 return CT_INVALID;
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 if (v->type != VEH_TRAIN) return CT_INVALID; // We can't refit the vehicle to carry the cargo we want
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
103
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* Below this line it's safe to assume that the vehicle in question is a 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
105
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
106 if (v->cargo_cap != 0) return CT_INVALID; // trying to replace a vehicle with cargo capacity into another one with incompatible cargo type
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
107
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* the old engine didn't have cargo capacity, but the new one does
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 * now we will figure out what cargo the train is carrying and refit to fit this */
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
110 v = 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
111 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
112 if (v->cargo_cap == 0) continue;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
113 /* Now we found a cargo type being carried on the train and we will see if it is possible to carry to this one */
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 if (v->cargo_type == new_cargo_type) return 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
115 if (CanRefitTo(engine_type, v->cargo_type)) return v->cargo_type;
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
116 } while ((v = v->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
117 return CT_NO_REFIT; // We failed to find a cargo type on the old vehicle and we will not refit the new one
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* Replaces a vehicle (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
121 * 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
122 * 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
123 * @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
124 * @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 * @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
126 */
6990
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6952
diff changeset
127 static CommandCost ReplaceVehicle(Vehicle **w, byte flags, Money total_cost)
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
128 {
6943
fd42cb9816c6 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents: 6800
diff changeset
129 CommandCost cost;
fd42cb9816c6 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents: 6800
diff changeset
130 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
131 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
132 const Player *p = GetPlayer(old_v->owner);
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
133 EngineID 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
134 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
135 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
136 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
137 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
138 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
139
8680
6a8d872901f2 (svn r12346) -Fix [FS#1748, FS#1825](r9874, r11872): Remove duplicated and inconsistent code wrt. autoreplace with rules in both vehicles' group and ALL_GROUP.
frosch <frosch@openttd.org>
parents: 8629
diff changeset
140 /* Check if there is a autoreplacement set for the vehicle */
6a8d872901f2 (svn r12346) -Fix [FS#1748, FS#1825](r9874, r11872): Remove duplicated and inconsistent code wrt. autoreplace with rules in both vehicles' group and ALL_GROUP.
frosch <frosch@openttd.org>
parents: 8629
diff changeset
141 new_engine_type = EngineReplacementForPlayer(p, old_v->engine_type, old_v->group_id);
6a8d872901f2 (svn r12346) -Fix [FS#1748, FS#1825](r9874, r11872): Remove duplicated and inconsistent code wrt. autoreplace with rules in both vehicles' group and ALL_GROUP.
frosch <frosch@openttd.org>
parents: 8629
diff changeset
142 /* if not, just renew to the same type */
6650
7efd4e045ac8 (svn r9881) -Fix (FS#782, r9874): accidentally removed one condition too many causing asserts.
rubidium <rubidium@openttd.org>
parents: 6647
diff changeset
143 if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type;
7efd4e045ac8 (svn r9881) -Fix (FS#782, r9874): accidentally removed one condition too many causing asserts.
rubidium <rubidium@openttd.org>
parents: 6647
diff changeset
144
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
145 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
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 /* 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
148 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
149
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
150 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
151
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
152 /* 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
153 * 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
154 * 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
155 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
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 cost = DoCommand(old_v->tile, new_engine_type, 3, flags, GetCmdBuildVeh(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
158 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
159 /* 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
160 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
161 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
162 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
163 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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
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
165 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
166 /* add refit cost */
6943
fd42cb9816c6 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents: 6800
diff changeset
167 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
168 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
169 /* 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
170 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
171 }
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
172 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
173 }
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
174
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
175 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
176 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
177 *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
178
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
179 /* 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
180 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
181 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
182 /* 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
183 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
184 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
185 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
186
7928
4e8dfd103163 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7527
diff changeset
187 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
188 // 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
189 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
190 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
192 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
193 /* 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
194 * 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
195 * 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
196 */
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
197 /* 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
198 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
199 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
200 /* 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
201 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
202 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
203 /* 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
204 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
205 }
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
206 } 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
207 /* 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
208 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
209 /* 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
210 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
211 /* Add the new 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
212 DoCommand(0, (front->index << 16) | new_v->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
213 }
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
214 } 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
215 // 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
216 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
217 new_v->cur_order_index = old_v->cur_order_index;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
218 ChangeVehicleViewWindow(old_v, 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
219 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
220 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
221 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
222 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
223 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
224 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
225 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
226
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
228 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
229 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
230
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 // 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
232 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
233 // 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
234 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
235 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 if (temp_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
238 DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_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
239 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* We are done setting up the new vehicle. Now we move the cargo from the old one to the new one */
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
243 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
244
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 // Get the name of the old vehicle if it has a custom name.
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
246 if (old_v->name != NULL) vehicle_name = strdup(old_v->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
247 } 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
248 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
249
a415946a7ff1 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni <bjarni@openttd.org>
parents: 7497
diff changeset
250 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
251 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
252 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
253 /* 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
254 * This is done by building an engine, test if the wagons can be added and then sell the test engine. */
a415946a7ff1 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni <bjarni@openttd.org>
parents: 7497
diff changeset
255 DoCommand(old_v->tile, new_engine_type, 3, DC_EXEC, GetCmdBuildVeh(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
256 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
257 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
258 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
259 }
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
260 }
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
261
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
262 /* 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
263 * 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
264 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
265 /* 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
266 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
267 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
268 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
269 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* 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
273 * 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
274 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
275 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
276
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* 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
278 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
279
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
281 /* 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
282 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
283 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
284
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
285 /* 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
286 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
287 _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
288 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
289 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
290 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
291
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
292 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
293 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
294
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
295 /** 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
296 * (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
297 * @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
298 * 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
299 * @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
300 * @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
301 * 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
302 * @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
303 */
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
304 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
305 {
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 Vehicle *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
307 const 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
308 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
309 bool stopped = false;
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
310
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
311 /* 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
312 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
313
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
314 /* 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
315 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
316
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
317 /* 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
318 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
319
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
320 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
321 /* 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
322 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
323 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
324 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
325 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
326 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
327 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
328 }
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
329 }
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
330
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
331 /* 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
332 * 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
333 * 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
334 * 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
335 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
336 (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
337 -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
338 );
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
339
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
340 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
341 /* 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
342 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
343 /* 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
344 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
345 }
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
346
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
347 {
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
348 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
349 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
350 do {
7526
e463f6756541 (svn r11045) -Codechange: added a function to tell if a vehicle is the rear part of a dualheaded train engine
bjarni <bjarni@openttd.org>
parents: 7525
diff changeset
351 if (w->type == VEH_TRAIN && IsRearDualheaded(w)) {
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
352 /* we build the rear ends of multiheaded trains with the front ones */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
353 continue;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
354 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
355
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
356 // check if the vehicle should be replaced
8628
97445f45ba39 (svn r12230) -Codechange: [autoreplace] made a function to detect if a vehicle needs autorenewing
bjarni <bjarni@openttd.org>
parents: 8469
diff changeset
357 if (!w->NeedsAutorenewing(p) || // replace if engine is too old
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
358 w->max_age == 0) { // rail cars got a max age of 0
8680
6a8d872901f2 (svn r12346) -Fix [FS#1748, FS#1825](r9874, r11872): Remove duplicated and inconsistent code wrt. autoreplace with rules in both vehicles' group and ALL_GROUP.
frosch <frosch@openttd.org>
parents: 8629
diff changeset
359 if (!EngineHasReplacementForPlayer(p, w->engine_type, w->group_id)) 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
360 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
361
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
362 /* Now replace the vehicle */
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
363 cost.AddCost(ReplaceVehicle(&w, flags, cost.GetCost()));
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
364
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 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
366 (w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_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
367 /* 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
368 * 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
369 * 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
370 */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
371 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
372 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 } while (w->type == VEH_TRAIN && (w = GetNextVehicle(w)) != 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
374
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
375 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
376 /* We didn't do anything during the replace so we will just exit here */
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
377 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
378 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
379 }
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
380
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
381 if (display_costs && !(flags & DC_EXEC)) {
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
382 /* 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
383 * We will not actually pay this amount. It's for display and checks only. */
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
384 cost.AddCost((Money)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
385 if (CmdSucceeded(cost) && GetAvailableMoneyForCommand() < cost.GetCost()) {
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
386 /* We don't have enough money so we will set cost to failed */
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
387 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
388 }
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
389 }
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
390
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
391 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
392 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
393 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
394 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
395 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
396 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
397 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
398 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
399 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
400 // 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
401 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
402 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
403
8763
d6e363672edb (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium <rubidium@openttd.org>
parents: 8736
diff changeset
404 AddNewsItem(message, NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, DNC_NONE, 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
405 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
406 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
407 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
408
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
409 if (flags & DC_EXEC && CmdSucceeded(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
410 /* If setting is on to try not to exceed the old length of the train with the replacement */
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
411 if (v->type == VEH_TRAIN && p->renew_keep_length) {
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
412 Vehicle *temp;
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
413 w = 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
414
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
415 while (v->u.rail.cached_total_length > old_total_length) {
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
416 // the train is too long. We will remove cars one by one from the start of the train until it's short enough
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
417 while (w != NULL && RailVehInfo(w->engine_type)->railveh_type != RAILVEH_WAGON) {
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
418 w = GetNextVehicle(w);
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
419 }
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
420 if (w == NULL) {
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
421 // we failed to make the train short enough
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
422 SetDParam(0, v->unitnumber);
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
423 AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NM_SMALL, NF_VIEWPORT | NF_VEHICLE, NT_ADVICE, DNC_NONE, v->index, 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
424 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
425 }
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
426 temp = w;
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
427 w = GetNextVehicle(w);
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
428 DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_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
429 MoveVehicleCargo(v, temp);
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
430 cost.AddCost(DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON));
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
431 }
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
432 }
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
433 if (display_costs && IsLocalPlayer()) {
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
434 ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost());
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
435 }
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
436 }
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
437
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
438 /* 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
439 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
440
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
441 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
442 }