annotate src/autoreplace_cmd.cpp @ 9061:888b60158039 draft

(svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles) Autoreplace uses this with the following benefits: -Mass autoreplace (the button in the depot window) will now estimate costs correctly -Autoreplace now either replaces correctly or manages to keep the original vehicle (no more broken trains) Thanks to Ammler for testing this
author bjarni <bjarni@openttd.org>
date Sun, 27 Apr 2008 20:09:29 +0000
parents c9d48ed87e3a
children f77eb484bc54
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
1 /* $Id$ */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
2
8999
0a4c639d1b8f (svn r12794) -Doc: added missing doxygen comment in autoreplace_cmd.cpp
bjarni <bjarni@openttd.org>
parents: 8997
diff changeset
3 /** @file autoreplace_cmd.cpp Deals with autoreplace execution but not the setup */
0a4c639d1b8f (svn r12794) -Doc: added missing doxygen comment in autoreplace_cmd.cpp
bjarni <bjarni@openttd.org>
parents: 8997
diff changeset
4
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
5 #include "stdafx.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
6 #include "openttd.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
7 #include "roadveh.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
8 #include "ship.h"
8763
d6e363672edb (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium <rubidium@openttd.org>
parents: 8736
diff changeset
9 #include "news_func.h"
8254
7d580c9c41fb (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium <rubidium@openttd.org>
parents: 8230
diff changeset
10 #include "player_func.h"
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
11 #include "debug.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
12 #include "vehicle_gui.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
13 #include "train.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
14 #include "aircraft.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
15 #include "cargotype.h"
6643
18d58b36b9b3 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium <rubidium@openttd.org>
parents: 6551
diff changeset
16 #include "group.h"
8114
2d6af5d7a142 (svn r11675) -Codechange: split the string types from the string functions.
rubidium <rubidium@openttd.org>
parents: 8064
diff changeset
17 #include "strings_func.h"
8116
9cc845deddfe (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium <rubidium@openttd.org>
parents: 8114
diff changeset
18 #include "command_func.h"
8144
d18c8a0bb638 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium <rubidium@openttd.org>
parents: 8131
diff changeset
19 #include "vehicle_func.h"
8131
e300ac8001ae (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents: 8116
diff changeset
20 #include "functions.h"
8211
165064de4629 (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium <rubidium@openttd.org>
parents: 8144
diff changeset
21 #include "variables.h"
8212
11263ebe590a (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium <rubidium@openttd.org>
parents: 8211
diff changeset
22 #include "autoreplace_func.h"
8363
e912fe1c1424 (svn r11929) -Fix (r9981)[FS#1624]: [autoreplace] fixed a case where a single headed locomotive caused an assert when being replaced to a dualheaded one
bjarni <bjarni@openttd.org>
parents: 8362
diff changeset
23 #include "articulated_vehicles.h"
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
24
8264
2495310e220f (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium <rubidium@openttd.org>
parents: 8258
diff changeset
25 #include "table/strings.h"
2495310e220f (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium <rubidium@openttd.org>
parents: 8258
diff changeset
26
8628
97445f45ba39 (svn r12230) -Codechange: [autoreplace] made a function to detect if a vehicle needs autorenewing
bjarni <bjarni@openttd.org>
parents: 8469
diff changeset
27
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
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 * 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
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 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
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 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
34
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
36 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
37 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
38 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
39
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
40 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
41 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
42
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
43 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
44 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
45
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 // 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
47 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
48 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
49
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->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
51 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
52 } 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
53
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /*
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
56 * 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
57 * 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
58 * 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
59 */
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
60 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
61 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
64 {
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
66 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
67
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
69 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
70 } 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
71 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
72 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
73
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
74 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
75 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
76 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
77 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
78 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
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
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /**
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 * 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
85 * @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
86 * @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
87 * @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
88 * 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
89 * 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
90 */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
91 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
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 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
94
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
95 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
96
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
97 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
98 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
99 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
100 } 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
101 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
102 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
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 /* 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
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 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
109
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
110 /* 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
111 * 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
112 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
113 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
114 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
115 /* 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
116 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
117 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
118 } 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
119 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
120 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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
8997
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
122 /** Replaces a vehicle (used to be called autorenew)
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
123 * 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
124 * 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
125 * @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
126 * @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts
8997
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
127 * @param p The vehicle owner (faster than refinding the pointer)
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
128 * @param new_engine_type The EngineID to replace to
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
129 * @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
130 */
8997
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
131 static CommandCost ReplaceVehicle(Vehicle **w, byte flags, Money total_cost, const Player *p, EngineID new_engine_type)
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
132 {
6943
fd42cb9816c6 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents: 6800
diff changeset
133 CommandCost cost;
fd42cb9816c6 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents: 6800
diff changeset
134 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
135 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
136 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
137 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
138 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
139 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
140 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
141
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
142 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
143
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
144 /* 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
145 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
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 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
148
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
149 /* 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
150 * 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
151 * 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
152 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
153
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
154 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
155 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
156 /* 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
157 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
158 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
159 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
160 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
161
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
162 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
163 /* add refit cost */
6943
fd42cb9816c6 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents: 6800
diff changeset
164 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
165 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
166 /* 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
167 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
168 }
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
169 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
170 }
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
171
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
172 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
173 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
174 *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
175
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* 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
177 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
178 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
179 /* 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
180 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
181 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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
7928
4e8dfd103163 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7527
diff changeset
184 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
185 // 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
186 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
187 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
189 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
190 /* 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
191 * 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
192 * 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
193 */
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
194 /* 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
195 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
196 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
197 /* 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
198 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
199 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
200 /* 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
201 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
202 }
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 } 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
204 /* 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
205 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
206 /* 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
207 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
208 /* Add the new vehicle */
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
209 CommandCost tmp_move = DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
210 if (CmdFailed(tmp_move)) {
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
211 cost.AddCost(tmp_move);
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
212 DoCommand(0, new_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN));
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
213 }
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
214 }
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
215 } 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
216 // 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
217 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
218 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
219 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
220 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
221 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
222 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
223 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
224 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
225 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
226 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
227
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
229 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
230 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
231
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
232 // 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
233 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
234 // 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
235 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
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
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 if (temp_v != NULL) {
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
239 CommandCost tmp_move = DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
240 if (CmdFailed(tmp_move)) {
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
241 cost.AddCost(tmp_move);
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
242 DoCommand(0, temp_v->index, 1, DC_EXEC, GetCmdSellVeh(VEH_TRAIN));
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
243 }
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 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
246 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* 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
248 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
249
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
250 // 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
251 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
252 } 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
253 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
254
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 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
256 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
257 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
258 /* 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
259 * 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
260 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
261 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
262 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
263 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
264 }
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
265 }
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
266
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
267 /* 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
268 * 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
269 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
270 /* 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
271 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
272 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
273 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
274 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
275 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* 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
278 * 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
279 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
280 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
281
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
282 /* sell the 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
283 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
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 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
286 /* 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
287 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
288 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
289
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* 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
291 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
292 _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
293 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
294 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
295 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
298 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
299
9002
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
300 /** Removes wagons from a train until it get a certain length
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
301 * @param v The vehicle
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
302 * @param old_total_length The wanted max length
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
303 * @return The profit from selling the wagons
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
304 */
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
305 static CommandCost WagonRemoval(Vehicle *v, uint16 old_total_length)
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
306 {
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
307 if (v->type != VEH_TRAIN) return CommandCost();
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
308 Vehicle *front = v;
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
309
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
310 CommandCost cost = CommandCost();
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
311
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
312 while (front->u.rail.cached_total_length > old_total_length) {
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
313 /* the train is too long. We will remove cars one by one from the start of the train until it's short enough */
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
314 while (v != NULL && RailVehInfo(v->engine_type)->railveh_type != RAILVEH_WAGON) {
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
315 /* We move backwards in the train until we find a wagon */
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
316 v = GetNextVehicle(v);
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
317 }
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
318
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
319 if (v == NULL) {
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
320 /* We sold all the wagons and the train is still not short enough */
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
321 SetDParam(0, front->unitnumber);
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
322 AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NM_SMALL, NF_VIEWPORT | NF_VEHICLE, NT_ADVICE, DNC_NONE, front->index, 0);
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
323 return cost;
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
324 }
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
325
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
326 /* We found a wagon we can sell */
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
327 Vehicle *temp = v;
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
328 v = GetNextVehicle(v);
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
329 DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); // remove the wagon from the train
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
330 MoveVehicleCargo(front, temp); // move the cargo back on the train
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
331 cost.AddCost(DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON)); // sell the wagon
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
332 }
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
333 return cost;
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
334 }
c9d48ed87e3a (svn r12797) -Codechange: [autoreplace] moved wagon removal to a function of it's own
bjarni <bjarni@openttd.org>
parents: 8999
diff changeset
335
8997
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
336 /** Get the EngineID of the replacement for a vehicle
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
337 * @param v The vehicle to find a replacement for
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
338 * @param p The vehicle's owner (it's faster to forward the pointer than refinding it)
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
339 * @return the EngineID of the replacement. INVALID_ENGINE if no buildable replacement is found
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
340 */
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
341 static EngineID GetNewEngineType(const Vehicle *v, const Player *p)
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
342 {
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
343 if (v->type == VEH_TRAIN && IsRearDualheaded(v)) {
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
344 /* we build the rear ends of multiheaded trains with the front ones */
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
345 return INVALID_ENGINE;
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
346 }
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
347
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
348 EngineID e = EngineReplacementForPlayer(p, v->engine_type, v->group_id);
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
349
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
350 if (e != INVALID_ENGINE && IsEngineBuildable(e, v->type, _current_player)) {
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
351 return e;
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
352 }
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
353
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
354 if (v->NeedsAutorenewing(p) && // replace if engine is too old
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
355 IsEngineBuildable(v->engine_type, v->type, _current_player)) { // engine can still be build
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
356 return v->engine_type;
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
357 }
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
358
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
359 return INVALID_ENGINE;
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
360 }
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
361
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
362 /** 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
363 * (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
364 * @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
365 * 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
366 * @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
367 * @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
368 * 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
369 * @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
370 */
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
371 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
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 Vehicle *w;
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
374 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
375 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
376 bool stopped = false;
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
377 BackuppedVehicle backup(true);
8996
6a811008eb9f (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents: 8962
diff changeset
378
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 /* 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
380 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
381
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 /* 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
383 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
384
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 /* 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
386 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
387
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 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
389 /* 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
390 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
391 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
392 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
393 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
394 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
395 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
396 }
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
397 }
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
398
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* 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
400 * 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
401 * 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
402 * 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
403 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
404 (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
405 -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
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
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
408 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
409 /* 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
410 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
411 /* 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
412 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
413 }
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
414
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
415 {
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
416 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
417 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
418 do {
8997
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
419 EngineID new_engine = GetNewEngineType(w, p);
19392bf555ff (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni <bjarni@openttd.org>
parents: 8996
diff changeset
420 if (new_engine == INVALID_ENGINE) continue;
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
421
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
422 if (!backup.ContainsBackup()) {
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
423 /* We are going to try to replace a vehicle but we don't have any backup so we will make one. */
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
424 backup.Backup(v, p);
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
425 }
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
426 /* Now replace the vehicle */
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
427 cost.AddCost(ReplaceVehicle(&w, DC_EXEC, cost.GetCost(), p, new_engine));
6800
567aecb1051a (svn r10039) -Fix: [autoreplace] when trying to replace an engine that can't carry the wagons the old one carries (GRF restrictoin), then don't try to replace at all
bjarni <bjarni@openttd.org>
parents: 6747
diff changeset
428
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
429 if (w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE) {
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
430 /* 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
431 * 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
432 * 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
433 */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
434 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
435 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
436 } 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
437
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
438 if (v->type == VEH_TRAIN && p->renew_keep_length) {
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
439 /* Remove wagons until the wanted length is reached */
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
440 cost.AddCost(WagonRemoval(v, old_total_length));
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
441 }
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
442
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
443 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
444 /* We didn't do anything during the replace so we will just exit here */
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
445 v = backup.Restore(v);
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
446 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
447 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
448 }
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
449
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
450 if (display_costs) {
8996
6a811008eb9f (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents: 8962
diff changeset
451 /* 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
452 * We will not actually pay this amount. It's for display and checks only. */
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
453 CommandCost tmp = cost;
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
454 tmp.AddCost((Money)p->engine_renew_money);
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
455 if (CmdSucceeded(tmp) && GetAvailableMoneyForCommand() < tmp.GetCost()) {
8996
6a811008eb9f (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents: 8962
diff changeset
456 /* We don't have enough money so we will set cost to failed */
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
457 cost.AddCost((Money)p->engine_renew_money);
8996
6a811008eb9f (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents: 8962
diff changeset
458 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
459 }
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
460 }
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
461
6a811008eb9f (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents: 8962
diff changeset
462 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
463 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
464 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
465 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
466 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
467 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
468 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
469 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
470 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
471 // 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
472 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
473 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
474
8763
d6e363672edb (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium <rubidium@openttd.org>
parents: 8736
diff changeset
475 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
476 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
477 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
478 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
479
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
480 if (display_costs && IsLocalPlayer() && (flags & DC_EXEC) && CmdSucceeded(cost)) {
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
481 ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost());
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
482 }
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
483
9061
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
484 if (!(flags & DC_EXEC) || CmdFailed(cost)) {
888b60158039 (svn r12913) -Add: ability to backup and restore a player's economic data and data for a vehicle (or chain of vehicles)
bjarni <bjarni@openttd.org>
parents: 9002
diff changeset
485 v = backup.Restore(v);
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
486 }
6a811008eb9f (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents: 8962
diff changeset
487
6a811008eb9f (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni <bjarni@openttd.org>
parents: 8962
diff changeset
488 /* 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
489 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
490
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
491 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
492 }