annotate src/autoreplace_cmd.cpp @ 7928:4e8dfd103163 draft

(svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
author skidd13 <skidd13@openttd.org>
date Mon, 19 Nov 2007 21:02:30 +0000
parents 6508f031bb0b
children 44ff7a6d801f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
1 /* $Id$ */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
2
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
3 #include "stdafx.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
4 #include "openttd.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
5 #include "roadveh.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
6 #include "ship.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 "table/strings.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 "functions.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
9 #include "news.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
10 #include "command.h"
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
11 #include "player.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 "engine.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 "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
14 #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
15 #include "depot.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
16 #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
17 #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
18 #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
19 #include "group.h"
7049
59b52eeff08b (svn r10314) -Codechange: Refer to vehicle names by index
peter1138 <peter1138@openttd.org>
parents: 7010
diff changeset
20 #include "strings.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
21
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
22
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
23 /*
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 * 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
25 */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
26 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
27 {
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
29
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
31 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
32 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
33 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
34
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
35 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
36 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
37
7010
f0f87c5a431e (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium <rubidium@openttd.org>
parents: 6990
diff changeset
38 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
39 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
40
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 // 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
42 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
43 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
44
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
45 } 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
46 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
47 } 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
48
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /*
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
50 * 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
51 * 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
52 * 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
53 * 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
54 */
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
55 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
56 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
59 {
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
60 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
61 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
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 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
64 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
65 } 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
66 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
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
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
69 FOR_VEHICLE_ORDERS(u, 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
70 if (!(o->refit_cargo < NUM_CARGO)) 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
71 if (!CanRefitTo(v->engine_type, o->refit_cargo)) 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
72 if (!CanRefitTo(engine_type, o->refit_cargo)) return 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
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
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
75 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
76 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
77
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
78 /**
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 * 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
80 * @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
81 * @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
82 * @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
83 * 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
84 * 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
85 */
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
87 {
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
88 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
89
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
90 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
91
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
92 if (v->cargo_type == new_cargo_type || CanRefitTo(engine_type, 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
93 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
94 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
95 } 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
96 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
97 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
98 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
100
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
101 /* 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
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 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
104
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* 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
106 * 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
107 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
108 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
109 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
110 /* 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
111 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
112 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
113 } 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
114 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
115 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
116
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
117 /* Replaces a vehicle (used to be called autorenew)
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
118 * 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
119 * 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
120 * @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
121 * @param flags is the flags to use when calling DoCommand(). Mainly DC_EXEC counts
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
122 * @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
123 */
6990
a19700261804 (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium <rubidium@openttd.org>
parents: 6952
diff changeset
124 static CommandCost ReplaceVehicle(Vehicle **w, byte flags, Money total_cost)
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
125 {
6943
fd42cb9816c6 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents: 6800
diff changeset
126 CommandCost cost;
fd42cb9816c6 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents: 6800
diff changeset
127 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
128 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
129 const Player *p = GetPlayer(old_v->owner);
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
130 EngineID new_engine_type;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
131 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
132 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
133 Vehicle *new_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
134 char vehicle_name[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
135 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
136
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
137 /* If the vehicle belongs to a group, check if the group is protected from the global autoreplace.
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
138 * If not, chek if an global auto replacement is defined */
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
139 new_engine_type = (IsValidGroupID(old_v->group_id) && GetGroup(old_v->group_id)->replace_protection) ?
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
140 INVALID_ENGINE :
7258
6c0d77eb1fc2 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium <rubidium@openttd.org>
parents: 7049
diff changeset
141 EngineReplacementForPlayer(p, old_v->engine_type, ALL_GROUP);
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
142
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
143 /* If we don't set new_egnine_type previously, we try to check if an autoreplacement was defined
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
144 * for the group and the engine_type of the vehicle */
7258
6c0d77eb1fc2 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium <rubidium@openttd.org>
parents: 7049
diff changeset
145 if (new_engine_type == INVALID_ENGINE && !IsAllGroupID(old_v->group_id)) {
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
146 new_engine_type = EngineReplacementForPlayer(p, old_v->engine_type, old_v->group_id);
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
147 }
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
148
6650
7efd4e045ac8 (svn r9881) -Fix (FS#782, r9874): accidentally removed one condition too many causing asserts.
rubidium <rubidium@openttd.org>
parents: 6647
diff changeset
149 if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type;
7efd4e045ac8 (svn r9881) -Fix (FS#782, r9874): accidentally removed one condition too many causing asserts.
rubidium <rubidium@openttd.org>
parents: 6647
diff changeset
150
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
151 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
152
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
153 /* 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
154 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
155
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
156 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
157
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
158 /* 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
159 * 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
160 * 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
161 SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
162 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
163
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
164 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
165 if (CmdFailed(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
166 SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
167 /* 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
168 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
169 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
170 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
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
6746
1faa0cbb6895 (svn r9981) -Fix: fixed a rare event that could cause autoreplace to run out of money and generate an error (spotted by elmex and UndernotBuilder)
bjarni <bjarni@openttd.org>
parents: 6735
diff changeset
173 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
174 /* add refit cost */
6943
fd42cb9816c6 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents: 6800
diff changeset
175 CommandCost refit_cost = GetRefitCost(new_engine_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
176 if (old_v->type == VEH_TRAIN && IsMultiheaded(old_v)) refit_cost.AddCost(refit_cost); // pay for both ends
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
177 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
178 }
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
179
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
180 if (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
181 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
182 *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
183
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
184 /* 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
185 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
186 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
187 /* 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
188 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
189 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
190 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
191
7928
4e8dfd103163 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7527
diff changeset
192 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
193 // we are autorenewing to a single engine, so we will turn it as the old one was turned as well
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
194 SETBIT(new_v->u.rail.flags, VRF_REVERSE_DIRECTION);
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
195 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
196
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
197 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
198 /* 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
199 * 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
200 * 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
201 */
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
202 /* 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
203 Vehicle *front = old_v->Previous();
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
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 */
7526
e463f6756541 (svn r11045) -Codechange: added a function to tell if a vehicle is the rear part of a dualheaded train engine
bjarni <bjarni@openttd.org>
parents: 7525
diff changeset
205 if (IsRearDualheaded(front)) front = front->Previous();
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
206 /* Now we move the old one out of 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
207 DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
6706
1ccff3d6924e (svn r9938) -Fix [FS#799]: 100 wagons train + replace engine
bjarni <bjarni@openttd.org>
parents: 6650
diff changeset
208 /* Add the new vehicle */
1ccff3d6924e (svn r9938) -Fix [FS#799]: 100 wagons train + replace engine
bjarni <bjarni@openttd.org>
parents: 6650
diff changeset
209 DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
210 } 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
211 // copy/clone the orders
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
212 DoCommand(0, (old_v->index << 16) | new_v->index, IsOrderListShared(old_v) ? CO_SHARE : CO_COPY, DC_EXEC, CMD_CLONE_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
213 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
214 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
215 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
216 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
217 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
218 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
219 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
220 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
221 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
222
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
223 new_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
224 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
225 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
226
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
227 // 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
228 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
229 // 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
230 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
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
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 (temp_v != NULL) {
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
234 DoCommand(0, (new_v->index << 16) | temp_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
235 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
236 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
237 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* 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
239 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
240
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
241 // Get the name of the old vehicle if it has a custom 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
242 if (!IsCustomName(old_v->string_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
243 vehicle_name[0] = '\0';
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 } else {
7049
59b52eeff08b (svn r10314) -Codechange: Refer to vehicle names by index
peter1138 <peter1138@openttd.org>
parents: 7010
diff changeset
245 SetDParam(0, old_v->index);
59b52eeff08b (svn r10314) -Codechange: Refer to vehicle names by index
peter1138 <peter1138@openttd.org>
parents: 7010
diff changeset
246 GetString(vehicle_name, STR_VEHICLE_NAME, lastof(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
247 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
248 } 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
249 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
250
a415946a7ff1 (svn r11043) -Fix (r10039) [FS#1185]: Autorenew/autoreplace fails silently with multiple multi-headed engines
bjarni <bjarni@openttd.org>
parents: 7497
diff changeset
251 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
252 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
253 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
254 /* 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
255 * 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
256 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
257 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
258 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
259 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
260 }
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
261 }
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
262
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
263 /* 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
264 * 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
265 if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) {
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
266 SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
267 /* 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
268 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
269 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
270 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
271 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
272 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
273
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* 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
275 * The player will get the same amount now that the sale actually takes place */
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
277 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
278 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
279
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
280 /* 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
281 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
282
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
283 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
284 /* 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
285 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
286 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* Transfer the name of the old 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
289 if ((flags & DC_EXEC) && vehicle_name[0] != '\0') {
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 _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
291 DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_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
292 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
293
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
294 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
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 /** 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
298 * (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
299 * @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
300 * if the vehicle is a train, v needs to be the front 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
301 * @param check Checks if the replace is valid. No action is done at all
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
302 * @param display_costs If set, a cost animation is shown (only if check is 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
303 * @return CMD_ERROR if something went wrong. Otherwise the price of the 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
304 */
6943
fd42cb9816c6 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium <rubidium@openttd.org>
parents: 6800
diff changeset
305 CommandCost MaybeReplaceVehicle(Vehicle *v, bool check, 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
306 {
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
307 Vehicle *w;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
308 const Player *p = GetPlayer(v->owner);
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
309 byte flags = 0;
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
310 CommandCost cost, temp_cost;
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
311 bool stopped;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
312
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
313 /* 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
314 * 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
315 * 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
316 * 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
317 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
318 (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
319 -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
320 );
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
321
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
322
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
323 _current_player = v->owner;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
324
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
325 assert(IsPlayerBuildableVehicleType(v));
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
326
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
327 assert(v->vehstatus & VS_STOPPED); // the vehicle should have been stopped in VehicleEnteredDepotThisTick() 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
328
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
329 /* Remember the flag v->leave_depot_instantly because if we replace the vehicle, the vehicle holding this flag will be sold
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
330 * If it is set, then we only stopped the vehicle to replace it (if needed) and we will need to start it again.
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
331 * We also need to reset the flag since it should remain false except from when the vehicle enters a depot until autoreplace is handled in the same tick */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
332 stopped = v->leave_depot_instantly;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
333 v->leave_depot_instantly = 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
334
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
335 for (;;) {
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
336 cost = 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
337 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
338 do {
7526
e463f6756541 (svn r11045) -Codechange: added a function to tell if a vehicle is the rear part of a dualheaded train engine
bjarni <bjarni@openttd.org>
parents: 7525
diff changeset
339 if (w->type == VEH_TRAIN && IsRearDualheaded(w)) {
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
340 /* we build the rear ends of multiheaded trains with the front ones */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
341 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
342 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
343
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
344 // check if the vehicle should be 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
345 if (!p->engine_renew ||
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
346 w->age - w->max_age < (p->engine_renew_months * 30) || // replace if engine 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
347 w->max_age == 0) { // rail cars got a max age of 0
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
348 /* If the vehicle belongs to a group, check if the group is protected from the global autoreplace.
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
349 If not, chek if an global auto remplacement is defined */
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
350 if (IsValidGroupID(w->group_id)) {
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
351 if (!EngineHasReplacementForPlayer(p, w->engine_type, w->group_id) && (
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
352 GetGroup(w->group_id)->replace_protection ||
7258
6c0d77eb1fc2 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium <rubidium@openttd.org>
parents: 7049
diff changeset
353 !EngineHasReplacementForPlayer(p, w->engine_type, ALL_GROUP))) {
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
354 continue;
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
355 }
7258
6c0d77eb1fc2 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium <rubidium@openttd.org>
parents: 7049
diff changeset
356 } else if (IsDefaultGroupID(w->group_id)) {
6c0d77eb1fc2 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium <rubidium@openttd.org>
parents: 7049
diff changeset
357 if (!EngineHasReplacementForPlayer(p, w->engine_type, DEFAULT_GROUP) &&
6c0d77eb1fc2 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium <rubidium@openttd.org>
parents: 7049
diff changeset
358 !EngineHasReplacementForPlayer(p, w->engine_type, ALL_GROUP)) {
6c0d77eb1fc2 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium <rubidium@openttd.org>
parents: 7049
diff changeset
359 continue;
6c0d77eb1fc2 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium <rubidium@openttd.org>
parents: 7049
diff changeset
360 }
6c0d77eb1fc2 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium <rubidium@openttd.org>
parents: 7049
diff changeset
361 } else if (!EngineHasReplacementForPlayer(p, w->engine_type, ALL_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
362 continue;
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
363 }
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
364 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
366 /* Now replace the vehicle */
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
367 temp_cost = ReplaceVehicle(&w, flags, cost.GetCost());
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
368
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
369 if (CmdFailed(temp_cost)) break; // replace failed for some reason. Leave the vehicle alone
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
370
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
371 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
372 (w->type != VEH_TRAIN || w->u.rail.first_engine == INVALID_ENGINE)) {
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
373 /* 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
374 * 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
375 * 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
376 */
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
377 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
378 }
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
379 cost.AddCost(temp_cost);
6264
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
380 } 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
381
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
382 if (!(flags & DC_EXEC) && (p->player_money < (cost.GetCost() + p->engine_renew_money) || cost.GetCost() == 0)) {
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
383 if (!check && p->player_money < (cost.GetCost() + p->engine_renew_money) && ( _local_player == v->owner ) && cost.GetCost() != 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
384 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
385 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
386 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
387 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
388 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
389 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
390 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
391 // 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
392 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
393 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
394
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
395 AddNewsItem(message, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
396 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
397 if (stopped) v->vehstatus &= ~VS_STOPPED;
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 if (display_costs) _current_player = OWNER_NONE;
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
400 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
403 break; // we are done replacing since the loop ran once with 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
404 } else if (check) {
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 /* It's a test only and we know that we can do this
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 * NOTE: payment for wagon removal is NOT included in this price */
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 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
408 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
409 // now we redo the loop, but this time we actually do stuff since we know that we can do it
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
410 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
411 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
412
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
413 /* If setting is on to try not to exceed the old length of the train with the replacement */
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 if (v->type == VEH_TRAIN && p->renew_keep_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
415 Vehicle *temp;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
416 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
417
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 while (v->u.rail.cached_total_length > old_total_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
419 // the train is too long. We will remove cars one by one from the start of the train until it's short enough
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
420 while (w != NULL && RailVehInfo(w->engine_type)->railveh_type != RAILVEH_WAGON) {
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 w = GetNextVehicle(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
422 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
423 if (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
424 // we failed to make the train short enough
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
425 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
426 AddNewsItem(STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
427 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
428 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
429 temp = 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
430 w = GetNextVehicle(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
431 DoCommand(0, (INVALID_VEHICLE << 16) | temp->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
432 MoveVehicleCargo(v, temp);
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
433 cost.AddCost(DoCommand(0, temp->index, 0, DC_EXEC, CMD_SELL_RAIL_WAGON));
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
434 }
37ecb2d0cdfb (svn r9073) -Codechange: moved 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
37ecb2d0cdfb (svn r9073) -Codechange: moved 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 if (stopped) v->vehstatus &= ~VS_STOPPED;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
438 if (display_costs) {
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
439 if (IsLocalPlayer()) ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost());
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
440 _current_player = OWNER_NONE;
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
441 }
37ecb2d0cdfb (svn r9073) -Codechange: moved autoreplace to a file of it's own (now autoreplace has a cmd and a gui file)
bjarni <bjarni@openttd.org>
parents:
diff changeset
442 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
443 }