author | matthijs |
Wed, 22 Mar 2006 21:04:13 +0000 | |
branch | 0.4.5 |
changeset 9951 | 7a623ae69d11 |
parent 9924 | 0d005642432e |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
0 | 3 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1883
diff
changeset
|
4 |
#include "openttd.h" |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
5 |
#include "debug.h" |
2163
637ec3c361f5
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2150
diff
changeset
|
6 |
#include "functions.h" |
2561
c78c3d248897
(svn r3098) static, const, uint -> TileIndex, indentation, bracing, unused return values, ... mostly related to the clone vehicle GUI
tron
parents:
2552
diff
changeset
|
7 |
#include "gui.h" |
507
8aa8100b0b22
(svn r815) Include strings.h only in the files which need it.
tron
parents:
495
diff
changeset
|
8 |
#include "table/strings.h" |
679
e959706a3e4d
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
593
diff
changeset
|
9 |
#include "map.h" |
1209
a1ac96655b79
(svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents:
1208
diff
changeset
|
10 |
#include "tile.h" |
0 | 11 |
#include "vehicle.h" |
12 |
#include "command.h" |
|
13 |
#include "pathfind.h" |
|
1247 | 14 |
#include "npf.h" |
0 | 15 |
#include "station.h" |
16 |
#include "table/train_cmd.h" |
|
17 |
#include "news.h" |
|
18 |
#include "engine.h" |
|
19 |
#include "player.h" |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
20 |
#include "sound.h" |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
21 |
#include "depot.h" |
1542
2ca6d1624e6d
(svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
1531
diff
changeset
|
22 |
#include "waypoint.h" |
1752
cdbfb2f23e72
(svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents:
1698
diff
changeset
|
23 |
#include "vehicle_gui.h" |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
24 |
#include "train.h" |
0 | 25 |
|
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
26 |
static bool TrainCheckIfLineEnds(Vehicle *v); |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
27 |
static void TrainController(Vehicle *v); |
0 | 28 |
|
29 |
static const byte _vehicle_initial_x_fract[4] = {10,8,4,8}; |
|
30 |
static const byte _vehicle_initial_y_fract[4] = {8,4,8,10}; |
|
31 |
static const byte _state_dir_table[4] = { 0x20, 8, 0x10, 4 }; |
|
32 |
||
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
33 |
/** |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
34 |
* Recalculates the cached weight of a train and its vehicles. Should be called each time the cargo on |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
35 |
* the consist changes. |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
36 |
* @param v First vehicle of the consist. |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
37 |
*/ |
2752 | 38 |
static void TrainCargoChanged(Vehicle* v) |
2639 | 39 |
{ |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
40 |
Vehicle *u; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
41 |
uint16 weight = 0; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
42 |
|
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
43 |
for (u = v; u != NULL; u = u->next) { |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
44 |
const RailVehicleInfo *rvi = RailVehInfo(u->engine_type); |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
45 |
uint16 vweight = 0; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
46 |
|
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
47 |
vweight += (_cargoc.weights[u->cargo_type] * u->cargo_count) / 16; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
48 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
49 |
// Vehicle weight is not added for articulated parts. |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
50 |
if (!IsArticulatedPart(u)) { |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
51 |
// vehicle weight is the sum of the weight of the vehicle and the weight of its cargo |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
52 |
vweight += rvi->weight; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
53 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
54 |
// powered wagons have extra weight added |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
55 |
if (HASBIT(u->u.rail.flags, VRF_POWEREDWAGON)) |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
56 |
vweight += RailVehInfo(v->engine_type)->pow_wag_weight; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
57 |
} |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
58 |
|
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
59 |
// consist weight is the sum of the weight of all vehicles in the consist |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
60 |
weight += vweight; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
61 |
|
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
62 |
// store vehicle weight in cache |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
63 |
u->u.rail.cached_veh_weight = vweight; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
64 |
}; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
65 |
|
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
66 |
// store consist weight in cache |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
67 |
v->u.rail.cached_weight = weight; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
68 |
} |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
69 |
|
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
70 |
/** |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
71 |
* Recalculates the cached stuff of a train. Should be called each time a vehicle is added |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
72 |
* to/removed from the chain, and when the game is loaded. |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
73 |
* Note: this needs to be called too for 'wagon chains' (in the depot, without an engine) |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
74 |
* @param v First vehicle of the chain. |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
75 |
*/ |
2639 | 76 |
void TrainConsistChanged(Vehicle* v) |
77 |
{ |
|
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
78 |
const RailVehicleInfo *rvi_v; |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
79 |
Vehicle *u; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
80 |
uint16 max_speed = 0xFFFF; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
81 |
uint32 power = 0; |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
82 |
EngineID first_engine; |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
83 |
|
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
84 |
assert(v->type == VEH_Train); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
85 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
86 |
assert(IsFrontEngine(v) || IsFreeWagon(v)); |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
87 |
|
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
88 |
rvi_v = RailVehInfo(v->engine_type); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
89 |
first_engine = IsFrontEngine(v) ? v->engine_type : INVALID_VEHICLE; |
2587
d554e2a03afa
(svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents:
2574
diff
changeset
|
90 |
v->u.rail.cached_total_length = 0; |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
91 |
|
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
92 |
for (u = v; u != NULL; u = u->next) { |
1908
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
93 |
const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type); |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
94 |
uint16 veh_len; |
1908
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
95 |
|
9924
0d005642432e
(svn r3954) - Explicitly update v->first in TrainConsistChanged() if necessary, as this is far faster than brute forcing it later.
Darkvater
parents:
2928
diff
changeset
|
96 |
// Update the v->first cache. This is faster than having to brute force it later. |
0d005642432e
(svn r3954) - Explicitly update v->first in TrainConsistChanged() if necessary, as this is far faster than brute forcing it later.
Darkvater
parents:
2928
diff
changeset
|
97 |
if (u->first == NULL) u->first = v; |
0d005642432e
(svn r3954) - Explicitly update v->first in TrainConsistChanged() if necessary, as this is far faster than brute forcing it later.
Darkvater
parents:
2928
diff
changeset
|
98 |
|
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
99 |
// update the 'first engine' |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
100 |
u->u.rail.first_engine = (v == u) ? INVALID_VEHICLE : first_engine; |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
101 |
|
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
102 |
if (rvi_u->visual_effect != 0) { |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
103 |
u->u.rail.cached_vis_effect = rvi_u->visual_effect; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
104 |
} else { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
105 |
if (IsTrainWagon(u) || IsArticulatedPart(u)) { |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
106 |
// Wagons and articulated parts have no effect by default |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
107 |
u->u.rail.cached_vis_effect = 0x40; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
108 |
} else if (rvi_u->engclass == 0) { |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
109 |
// Steam is offset by -4 units |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
110 |
u->u.rail.cached_vis_effect = 4; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
111 |
} else { |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
112 |
// Diesel fumes and sparks come from the centre |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
113 |
u->u.rail.cached_vis_effect = 8; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
114 |
} |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
115 |
} |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
116 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
117 |
if (!IsArticulatedPart(u)) { |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
118 |
// power is the sum of the powers of all engines and powered wagons in the consist |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
119 |
power += rvi_u->power; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
120 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
121 |
// check if its a powered wagon |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
122 |
CLRBIT(u->u.rail.flags, VRF_POWEREDWAGON); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
123 |
if ((rvi_v->pow_wag_power != 0) && (rvi_u->flags & RVI_WAGON) && UsesWagonOverride(u)) { |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
124 |
if (HASBIT(rvi_u->callbackmask, CBM_WAGON_POWER)) { |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
125 |
uint16 callback = GetCallBackResult(CBID_WAGON_POWER, u->engine_type, u); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
126 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
127 |
if (callback != CALLBACK_FAILED) |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
128 |
u->u.rail.cached_vis_effect = callback; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
129 |
} |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
130 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
131 |
if (u->u.rail.cached_vis_effect < 0x40) { |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
132 |
/* wagon is powered */ |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
133 |
SETBIT(u->u.rail.flags, VRF_POWEREDWAGON); // cache 'powered' status |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
134 |
power += rvi_v->pow_wag_power; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
135 |
} |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
136 |
} |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
137 |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
138 |
// max speed is the minimum of the speed limits of all vehicles in the consist |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
139 |
if (!(rvi_u->flags & RVI_WAGON) || _patches.wagon_speed_limits) |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
140 |
if (rvi_u->max_speed != 0 && !UsesWagonOverride(u)) |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
141 |
max_speed = min(rvi_u->max_speed, max_speed); |
1908
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
142 |
} |
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
143 |
|
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
144 |
// check the vehicle length (callback) |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
145 |
veh_len = CALLBACK_FAILED; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
146 |
if (HASBIT(rvi_u->callbackmask, CBM_VEH_LENGTH)) |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
147 |
veh_len = GetCallBackResult(CBID_VEH_LENGTH, u->engine_type, u); |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
148 |
if (veh_len == CALLBACK_FAILED) |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
149 |
veh_len = rvi_u->shorten_factor; |
1934
b3d568113d4d
(svn r2440) - Fix: [newgrf] Fix the spritesorter to handle overlapping sprites properly, this fixes display problems with really short wagons. (algorithm by patchman, ported by therax)
hackykid
parents:
1927
diff
changeset
|
150 |
veh_len = clamp(veh_len, 0, u->next == NULL ? 7 : 5); // the clamp on vehicles not the last in chain is stricter, as too short wagons can break the 'follow next vehicle' code |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
151 |
u->u.rail.cached_veh_length = 8 - veh_len; |
2587
d554e2a03afa
(svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents:
2574
diff
changeset
|
152 |
v->u.rail.cached_total_length += u->u.rail.cached_veh_length; |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
153 |
|
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
154 |
}; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
155 |
|
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
156 |
// store consist weight/max speed in cache |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
157 |
v->u.rail.cached_max_speed = max_speed; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
158 |
v->u.rail.cached_power = power; |
1908
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
159 |
|
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
160 |
// recalculate cached weights too (we do this *after* the rest, so it is known which wagons are powered and need extra weight added) |
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
161 |
TrainCargoChanged(v); |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
162 |
} |
1247 | 163 |
|
164 |
/* These two arrays are used for realistic acceleration. XXX: How should they |
|
165 |
* be interpreted? */ |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
166 |
static const byte _curve_neighbours45[8][2] = { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
167 |
{7, 1}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
168 |
{0, 2}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
169 |
{1, 3}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
170 |
{2, 4}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
171 |
{3, 5}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
172 |
{4, 6}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
173 |
{5, 7}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
174 |
{6, 0}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
175 |
}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
176 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
177 |
static const byte _curve_neighbours90[8][2] = { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
178 |
{6, 2}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
179 |
{7, 3}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
180 |
{0, 4}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
181 |
{1, 5}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
182 |
{2, 6}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
183 |
{3, 7}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
184 |
{4, 0}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
185 |
{5, 1}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
186 |
}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
187 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
188 |
enum AccelType { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
189 |
AM_ACCEL, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
190 |
AM_BRAKE |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
191 |
}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
192 |
|
2630 | 193 |
static bool TrainShouldStop(const Vehicle* v, TileIndex tile) |
1236
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
194 |
{ |
2630 | 195 |
const Order* o = &v->current_order; |
196 |
||
1236
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
197 |
assert(v->type == VEH_Train); |
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
198 |
assert(IsTileType(v->tile, MP_STATION)); |
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
199 |
//When does a train drive through a station |
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
200 |
//first we deal with the "new nonstop handling" |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
201 |
if (_patches.new_nonstop && o->flags & OF_NON_STOP && |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2044
diff
changeset
|
202 |
_m[tile].m2 == o->station ) |
1236
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
203 |
return false; |
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
204 |
|
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2044
diff
changeset
|
205 |
if (v->last_station_visited == _m[tile].m2) |
1236
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
206 |
return false; |
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
207 |
|
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2044
diff
changeset
|
208 |
if (_m[tile].m2 != o->station && |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
209 |
(o->flags & OF_NON_STOP || _patches.new_nonstop)) |
1236
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
210 |
return false; |
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
211 |
|
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
212 |
return true; |
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
213 |
} |
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
214 |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
215 |
//new acceleration |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
216 |
static int GetTrainAcceleration(Vehicle *v, bool mode) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
217 |
{ |
1531 | 218 |
const Vehicle *u; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
219 |
int num = 0; //number of vehicles, change this into the number of axles later |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
220 |
int power = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
221 |
int mass = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
222 |
int max_speed = 2000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
223 |
int area = 120; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
224 |
int friction = 35; //[1e-3] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
225 |
int drag_coeff = 20; //[1e-4] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
226 |
int incl = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
227 |
int resistance; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
228 |
int speed = v->cur_speed; //[mph] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
229 |
int force = 0x3FFFFFFF; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
230 |
int pos = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
231 |
int lastpos = -1; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
232 |
int curvecount[2] = {0, 0}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
233 |
int sum = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
234 |
int numcurve = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
235 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
236 |
speed *= 10; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
237 |
speed /= 16; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
238 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
239 |
//first find the curve speed limit |
1531 | 240 |
for (u = v; u->next != NULL; u = u->next, pos++) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
241 |
int dir = u->direction; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
242 |
int ndir = u->next->direction; |
1531 | 243 |
int i; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
244 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
245 |
for (i = 0; i < 2; i++) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
246 |
if ( _curve_neighbours45[dir][i] == ndir) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
247 |
curvecount[i]++; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
248 |
if (lastpos != -1) { |
1531 | 249 |
numcurve++; |
250 |
sum += pos - lastpos; |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
251 |
if (pos - lastpos == 1) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
252 |
max_speed = 88; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
253 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
254 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
255 |
lastpos = pos; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
256 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
257 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
258 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
259 |
//if we have a 90 degree turn, fix the speed limit to 60 |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
260 |
if (_curve_neighbours90[dir][0] == ndir || |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
261 |
_curve_neighbours90[dir][1] == ndir) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
262 |
max_speed = 61; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
263 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
264 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
265 |
|
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
266 |
if (numcurve > 0) sum /= numcurve; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
267 |
|
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
268 |
if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
269 |
int total = curvecount[0] + curvecount[1]; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
270 |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
271 |
if (curvecount[0] == 1 && curvecount[1] == 1) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
272 |
max_speed = 0xFFFF; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
273 |
} else if (total > 1) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
274 |
max_speed = 232 - (13 - clamp(sum, 1, 12)) * (13 - clamp(sum, 1, 12)); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
275 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
276 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
277 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
278 |
max_speed += (max_speed / 2) * v->u.rail.railtype; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
279 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
280 |
if (IsTileType(v->tile, MP_STATION) && IsFrontEngine(v)) { |
1236
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
281 |
if (TrainShouldStop(v, v->tile)) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
282 |
int station_length = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
283 |
TileIndex tile = v->tile; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
284 |
int delta_v; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
285 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
286 |
max_speed = 120; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
287 |
do { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
288 |
station_length++; |
1531 | 289 |
tile = TILE_ADD(tile, TileOffsByDir(v->direction / 2)); |
1685
b9cfe79393c0
(svn r2189) Introduce and use IsCompatibleTrainStationTile()
tron
parents:
1684
diff
changeset
|
290 |
} while (IsCompatibleTrainStationTile(tile, v->tile)); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
291 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
292 |
delta_v = v->cur_speed / (station_length + 1); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
293 |
if (v->max_speed > (v->cur_speed - delta_v)) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
294 |
max_speed = v->cur_speed - (delta_v / 10); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
295 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
296 |
max_speed = max(max_speed, 25 * station_length); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
297 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
298 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
299 |
|
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
300 |
mass = v->u.rail.cached_weight; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
301 |
power = v->u.rail.cached_power * 746; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
302 |
max_speed = min(max_speed, v->u.rail.cached_max_speed); |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
303 |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
304 |
for (u = v; u != NULL; u = u->next) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
305 |
num++; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
306 |
drag_coeff += 3; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
307 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
308 |
if (u->u.rail.track == 0x80) |
1882
73937823fdb2
(svn r2388) - Fix: [realistic accel] Very slow trains no longer get an increase in maximum speed when part of them is in a depot.
hackykid
parents:
1859
diff
changeset
|
309 |
max_speed = min(61, max_speed); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
310 |
|
1683
2248e6615517
(svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents:
1681
diff
changeset
|
311 |
if (HASBIT(u->u.rail.flags, VRF_GOINGUP)) { |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
312 |
incl += u->u.rail.cached_veh_weight * 60; //3% slope, quite a bit actually |
1683
2248e6615517
(svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents:
1681
diff
changeset
|
313 |
} else if (HASBIT(u->u.rail.flags, VRF_GOINGDOWN)) { |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
314 |
incl -= u->u.rail.cached_veh_weight * 60; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
315 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
316 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
317 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
318 |
v->max_speed = max_speed; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
319 |
|
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
320 |
if (v->u.rail.railtype != RAILTYPE_MAGLEV) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
321 |
resistance = 13 * mass / 10; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
322 |
resistance += 60 * num; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
323 |
resistance += friction * mass * speed / 1000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
324 |
resistance += (area * drag_coeff * speed * speed) / 10000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
325 |
} else |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
326 |
resistance = (area * (drag_coeff / 2) * speed * speed) / 10000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
327 |
resistance += incl; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
328 |
resistance *= 4; //[N] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
329 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
330 |
if (speed > 0) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
331 |
switch (v->u.rail.railtype) { |
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
332 |
case RAILTYPE_RAIL: |
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
333 |
case RAILTYPE_MONO: |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
334 |
force = power / speed; //[N] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
335 |
force *= 22; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
336 |
force /= 10; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
337 |
break; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
338 |
|
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
339 |
case RAILTYPE_MAGLEV: |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
340 |
force = power / 25; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
341 |
break; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
342 |
} |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
343 |
} else { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
344 |
//"kickoff" acceleration |
1969
cd1eb9fa4484
(svn r2475) -Fix: [ 1214982 & 1217473 ] realistic acceleration problem. (glx)
celestar
parents:
1961
diff
changeset
|
345 |
force = (mass * 8) + resistance; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
346 |
} |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
347 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
348 |
if (force <= 0) force = 10000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
349 |
|
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
350 |
if (v->u.rail.railtype != RAILTYPE_MAGLEV) force = min(force, mass * 10 * 200); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
351 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
352 |
if (mode == AM_ACCEL) { |
1684
d09bc0beec4c
(svn r2188) Revert r2185, because trains always accerlated (Requested by Celestar)
tron
parents:
1683
diff
changeset
|
353 |
return (force - resistance) / (mass * 4); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
354 |
} else { |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
355 |
return min((-force - resistance) / (mass * 4), 10000 / (mass * 4)); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
356 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
357 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
358 |
|
2817 | 359 |
static void UpdateTrainAcceleration(Vehicle* v) |
0 | 360 |
{ |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
361 |
uint power = 0; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
362 |
uint weight = 0; |
0 | 363 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
364 |
assert(IsFrontEngine(v)); |
0 | 365 |
|
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
366 |
weight = v->u.rail.cached_weight; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
367 |
power = v->u.rail.cached_power; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
368 |
v->max_speed = v->u.rail.cached_max_speed; |
0 | 369 |
|
370 |
assert(weight != 0); |
|
371 |
||
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
372 |
v->acceleration = clamp(power / weight * 4, 1, 255); |
0 | 373 |
} |
374 |
||
1475 | 375 |
int GetTrainImage(const Vehicle *v, byte direction) |
0 | 376 |
{ |
377 |
int img = v->spritenum; |
|
378 |
int base; |
|
379 |
||
380 |
if (is_custom_sprite(img)) { |
|
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
381 |
base = GetCustomVehicleSprite(v, direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)); |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
382 |
if (base != 0) return base; |
2464
3dee59d241c0
(svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents:
2463
diff
changeset
|
383 |
img = orig_rail_vehicle_info[v->engine_type].image_index; |
0 | 384 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
385 |
|
0 | 386 |
base = _engine_sprite_base[img] + ((direction + _engine_sprite_add[img]) & _engine_sprite_and[img]); |
387 |
||
2639 | 388 |
if (v->cargo_count >= v->cargo_cap / 2) base += _wagon_full_adder[img]; |
0 | 389 |
return base; |
390 |
} |
|
391 |
||
2570
c7c4d8438516
(svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents:
2564
diff
changeset
|
392 |
extern int _traininfo_vehicle_pitch; |
c7c4d8438516
(svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents:
2564
diff
changeset
|
393 |
|
2477
d454d5152386
(svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents:
2475
diff
changeset
|
394 |
void DrawTrainEngine(int x, int y, EngineID engine, uint32 image_ormod) |
0 | 395 |
{ |
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
396 |
const RailVehicleInfo *rvi = RailVehInfo(engine); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
397 |
|
0 | 398 |
int img = rvi->image_index; |
399 |
uint32 image = 0; |
|
400 |
||
401 |
if (is_custom_sprite(img)) { |
|
358
6eb0d5ec99b1
(svn r546) -newgrf: Use GetCustomVehicleIcon() for fetching sprites for vehicle selection dialogs. (Idea by octo, done by pasky).
darkvater
parents:
337
diff
changeset
|
402 |
image = GetCustomVehicleIcon(engine, 6); |
2570
c7c4d8438516
(svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents:
2564
diff
changeset
|
403 |
if (image == 0) { |
c7c4d8438516
(svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents:
2564
diff
changeset
|
404 |
img = orig_rail_vehicle_info[engine].image_index; |
c7c4d8438516
(svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents:
2564
diff
changeset
|
405 |
} else { |
c7c4d8438516
(svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents:
2564
diff
changeset
|
406 |
y += _traininfo_vehicle_pitch; |
c7c4d8438516
(svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents:
2564
diff
changeset
|
407 |
} |
0 | 408 |
} |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
409 |
if (image == 0) { |
0 | 410 |
image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img]; |
411 |
} |
|
412 |
||
413 |
if (rvi->flags & RVI_MULTIHEAD) { |
|
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
414 |
DrawSprite(image | image_ormod, x - 14, y); |
0 | 415 |
x += 15; |
416 |
image = 0; |
|
417 |
if (is_custom_sprite(img)) { |
|
358
6eb0d5ec99b1
(svn r546) -newgrf: Use GetCustomVehicleIcon() for fetching sprites for vehicle selection dialogs. (Idea by octo, done by pasky).
darkvater
parents:
337
diff
changeset
|
418 |
image = GetCustomVehicleIcon(engine, 2); |
2464
3dee59d241c0
(svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents:
2463
diff
changeset
|
419 |
if (image == 0) img = orig_rail_vehicle_info[engine].image_index; |
0 | 420 |
} |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
421 |
if (image == 0) { |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
422 |
image = |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
423 |
((6 + _engine_sprite_add[img + 1]) & _engine_sprite_and[img + 1]) + |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
424 |
_engine_sprite_base[img + 1]; |
0 | 425 |
} |
426 |
} |
|
427 |
DrawSprite(image | image_ormod, x, y); |
|
428 |
} |
|
429 |
||
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
430 |
static uint CountArticulatedParts(const RailVehicleInfo *rvi, EngineID engine_type) |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
431 |
{ |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
432 |
uint16 callback; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
433 |
uint i; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
434 |
|
2639 | 435 |
if (!HASBIT(rvi->callbackmask, CBM_ARTIC_ENGINE)) return 0; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
436 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
437 |
for (i = 1; i < 10; i++) { |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
438 |
callback = GetCallBackResult(CBID_ARTIC_ENGINE + (i << 8), engine_type, NULL); |
2639 | 439 |
if (callback == CALLBACK_FAILED || callback == 0xFF) break; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
440 |
} |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
441 |
|
2608
f7f41be75b48
(svn r3145) Fix two off-by-one errors in building articulated rail vehicles.
peter1138
parents:
2607
diff
changeset
|
442 |
return i - 1; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
443 |
} |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
444 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
445 |
static void AddArticulatedParts(const RailVehicleInfo *rvi, Vehicle **vl) |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
446 |
{ |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
447 |
const RailVehicleInfo *rvi_artic; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
448 |
EngineID engine_type; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
449 |
Vehicle *v = vl[0]; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
450 |
Vehicle *u = v; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
451 |
uint16 callback; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
452 |
bool flip_image; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
453 |
uint i; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
454 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
455 |
if (!HASBIT(rvi->callbackmask, CBM_ARTIC_ENGINE)) |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
456 |
return; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
457 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
458 |
for (i = 1; i < 10; i++) { |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
459 |
callback = GetCallBackResult(CBID_ARTIC_ENGINE + (i << 8), v->engine_type, NULL); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
460 |
if (callback == CALLBACK_FAILED || callback == 0xFF) |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
461 |
return; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
462 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
463 |
u->next = vl[i]; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
464 |
u = u->next; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
465 |
|
2608
f7f41be75b48
(svn r3145) Fix two off-by-one errors in building articulated rail vehicles.
peter1138
parents:
2607
diff
changeset
|
466 |
engine_type = GB(callback, 0, 7); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
467 |
flip_image = HASBIT(callback, 7); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
468 |
rvi_artic = RailVehInfo(engine_type); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
469 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
470 |
// get common values from first engine |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
471 |
u->direction = v->direction; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
472 |
u->owner = v->owner; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
473 |
u->tile = v->tile; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
474 |
u->x_pos = v->x_pos; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
475 |
u->y_pos = v->y_pos; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
476 |
u->z_pos = v->z_pos; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
477 |
u->z_height = v->z_height; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
478 |
u->u.rail.track = v->u.rail.track; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
479 |
u->u.rail.railtype = v->u.rail.railtype; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
480 |
u->build_year = v->build_year; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
481 |
u->vehstatus = v->vehstatus & ~VS_STOPPED; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
482 |
u->u.rail.first_engine = v->engine_type; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
483 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
484 |
// get more settings from rail vehicle info |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
485 |
u->spritenum = rvi_artic->image_index; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
486 |
if (flip_image) u->spritenum++; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
487 |
u->cargo_type = rvi_artic->cargo_type; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
488 |
u->cargo_cap = rvi_artic->capacity; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
489 |
u->max_speed = 0; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
490 |
u->max_age = 0; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
491 |
u->engine_type = engine_type; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
492 |
u->value = 0; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
493 |
u->type = VEH_Train; |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
494 |
u->subtype = 0; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
495 |
SetArticulatedPart(u); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
496 |
u->cur_image = 0xAC2; |
2804
b932fe148d36
(svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents:
2758
diff
changeset
|
497 |
u->random_bits = VehicleRandomBits(); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
498 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
499 |
VehiclePositionChanged(u); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
500 |
} |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
501 |
} |
0 | 502 |
|
2477
d454d5152386
(svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents:
2475
diff
changeset
|
503 |
static int32 CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags) |
0 | 504 |
{ |
505 |
int32 value; |
|
506 |
const RailVehicleInfo *rvi; |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
507 |
uint num_vehicles; |
0 | 508 |
|
889
7f8630bfea41
(svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents:
882
diff
changeset
|
509 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
7f8630bfea41
(svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents:
882
diff
changeset
|
510 |
|
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
511 |
rvi = RailVehInfo(engine); |
0 | 512 |
value = (rvi->base_cost * _price.build_railwagon) >> 8; |
513 |
||
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
514 |
num_vehicles = 1 + CountArticulatedParts(rvi, engine); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
515 |
|
0 | 516 |
if (!(flags & DC_QUERY_COST)) { |
2609
b2c7aa8ce0c4
(svn r3146) Avoid use of variable size declared arrays when allocating articulated rail vehicles.
peter1138
parents:
2608
diff
changeset
|
517 |
Vehicle *vl[11]; // Allow for wagon and upto 10 artic parts. |
2639 | 518 |
Vehicle* v; |
519 |
int x; |
|
520 |
int y; |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
521 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
522 |
if (!AllocateVehicles(vl, num_vehicles)) |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
523 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
0 | 524 |
|
525 |
if (flags & DC_EXEC) { |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
526 |
Vehicle *u, *w; |
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
527 |
uint dir; |
0 | 528 |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
529 |
v = vl[0]; |
2639 | 530 |
v->spritenum = rvi->image_index; |
0 | 531 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
532 |
u = NULL; |
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
533 |
|
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
534 |
FOR_ALL_VEHICLES(w) { |
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
535 |
if (w->type == VEH_Train && w->tile == tile && |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
536 |
IsFreeWagon(w) && w->engine_type == engine) { |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
537 |
u = GetLastVehicleInChain(w); |
0 | 538 |
break; |
539 |
} |
|
540 |
} |
|
541 |
||
542 |
v->engine_type = engine; |
|
543 |
||
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
544 |
dir = GB(_m[tile].m5, 0, 2); |
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
545 |
|
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
546 |
v->direction = dir * 2 + 1; |
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
547 |
v->tile = tile; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
548 |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
549 |
x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir]; |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
550 |
y = TileY(tile) * TILE_SIZE | _vehicle_initial_y_fract[dir]; |
0 | 551 |
|
552 |
v->x_pos = x; |
|
553 |
v->y_pos = y; |
|
554 |
v->z_pos = GetSlopeZ(x,y); |
|
555 |
v->owner = _current_player; |
|
556 |
v->z_height = 6; |
|
557 |
v->u.rail.track = 0x80; |
|
558 |
v->vehstatus = VS_HIDDEN | VS_DEFPAL; |
|
559 |
||
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
560 |
v->subtype = 0; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
561 |
SetTrainWagon(v); |
0 | 562 |
if (u != NULL) { |
563 |
u->next = v; |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
564 |
} else { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
565 |
SetFreeWagon(v); |
0 | 566 |
} |
567 |
||
568 |
v->cargo_type = rvi->cargo_type; |
|
569 |
v->cargo_cap = rvi->capacity; |
|
570 |
v->value = value; |
|
571 |
// v->day_counter = 0; |
|
572 |
||
2639 | 573 |
v->u.rail.railtype = GetEngine(engine)->railtype; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
574 |
|
0 | 575 |
v->build_year = _cur_year; |
576 |
v->type = VEH_Train; |
|
577 |
v->cur_image = 0xAC2; |
|
2804
b932fe148d36
(svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents:
2758
diff
changeset
|
578 |
v->random_bits = VehicleRandomBits(); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
579 |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
580 |
AddArticulatedParts(rvi, vl); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
581 |
|
0 | 582 |
_new_wagon_id = v->index; |
2564 | 583 |
_new_vehicle_id = v->index; |
0 | 584 |
|
585 |
VehiclePositionChanged(v); |
|
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
586 |
TrainConsistChanged(GetFirstVehicleInChain(v)); |
0 | 587 |
|
588 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
589 |
} |
|
590 |
} |
|
591 |
||
592 |
return value; |
|
593 |
} |
|
594 |
||
595 |
// Move all free vehicles in the depot to the train |
|
2630 | 596 |
static void NormalizeTrainVehInDepot(const Vehicle* u) |
0 | 597 |
{ |
2630 | 598 |
const Vehicle* v; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
599 |
|
0 | 600 |
FOR_ALL_VEHICLES(v) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
601 |
if (v->type == VEH_Train && IsFreeWagon(v) && |
0 | 602 |
v->tile == u->tile && |
603 |
v->u.rail.track == 0x80) { |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
604 |
if (CmdFailed(DoCommandByTile(0, v->index | (u->index << 16), 1, DC_EXEC, |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
605 |
CMD_MOVE_RAIL_VEHICLE))) |
0 | 606 |
break; |
607 |
} |
|
608 |
} |
|
609 |
} |
|
610 |
||
611 |
static const byte _railveh_score[] = { |
|
612 |
1, 4, 7, 19, 20, 30, 31, 19, |
|
613 |
20, 21, 22, 10, 11, 30, 31, 32, |
|
614 |
33, 34, 35, 29, 45, 32, 50, 40, |
|
615 |
41, 51, 52, 0, 0, 0, 0, 0, |
|
616 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
617 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
618 |
0, 0, 0, 0, 0, 0, 60, 62, |
|
619 |
63, 0, 0, 0, 0, 0, 0, 0, |
|
620 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
621 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
622 |
0, 0, 0, 0, 70, 71, 72, 73, |
|
623 |
74, 0, 0, 0, 0, 0, 0, 0, |
|
624 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
625 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
626 |
0, 0, 0, 0, |
|
627 |
}; |
|
628 |
||
629 |
||
2630 | 630 |
static int32 EstimateTrainCost(const RailVehicleInfo* rvi) |
0 | 631 |
{ |
632 |
return (rvi->base_cost * (_price.build_railvehicle >> 3)) >> 5; |
|
633 |
} |
|
634 |
||
2817 | 635 |
static void AddRearEngineToMultiheadedTrain(Vehicle* v, Vehicle* u, bool building) |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
636 |
{ |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
637 |
u->direction = v->direction; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
638 |
u->owner = v->owner; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
639 |
u->tile = v->tile; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
640 |
u->x_pos = v->x_pos; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
641 |
u->y_pos = v->y_pos; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
642 |
u->z_pos = v->z_pos; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
643 |
u->z_height = 6; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
644 |
u->u.rail.track = 0x80; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
645 |
u->vehstatus = v->vehstatus & ~VS_STOPPED; |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
646 |
u->subtype = 0; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
647 |
SetMultiheaded(u); |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
648 |
u->spritenum = v->spritenum + 1; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
649 |
u->cargo_type = v->cargo_type; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
650 |
u->cargo_cap = v->cargo_cap; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
651 |
u->u.rail.railtype = v->u.rail.railtype; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
652 |
if (building) v->next = u; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
653 |
u->engine_type = v->engine_type; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
654 |
u->build_year = v->build_year; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
655 |
if (building) v->value >>= 1; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
656 |
u->value = v->value; |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
657 |
u->type = VEH_Train; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
658 |
u->cur_image = 0xAC2; |
2804
b932fe148d36
(svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents:
2758
diff
changeset
|
659 |
u->random_bits = VehicleRandomBits(); |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
660 |
VehiclePositionChanged(u); |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
661 |
} |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
662 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
663 |
/** Build a railroad vehicle. |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
664 |
* @param x,y tile coordinates (depot) where rail-vehicle is built |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
665 |
* @param p1 engine type id |
2677
2c1fcffb304c
(svn r3219) -Codechange: removed the now obsolite code in the build train command to make half multiheaded engines
bjarni
parents:
2676
diff
changeset
|
666 |
* @param p2 bit 0 prevents any free cars from being added to the train |
0 | 667 |
*/ |
668 |
int32 CmdBuildRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
669 |
{ |
|
670 |
const RailVehicleInfo *rvi; |
|
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
671 |
int value; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
672 |
Vehicle *v; |
1282
e7a73ee62d2f
(svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents:
1266
diff
changeset
|
673 |
UnitID unit_num; |
0 | 674 |
Engine *e; |
1980
9ea0c89fbb58
(svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents:
1977
diff
changeset
|
675 |
TileIndex tile = TileVirtXY(x, y); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
676 |
uint num_vehicles; |
0 | 677 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
678 |
/* Check if the engine-type is valid (for the player) */ |
2854
6b85a5f4b65f
(svn r3402) -Fix: [building/cloning] added a string telling if build failed due to not being able to build a vehicle. Triggered when cloning a retired design
bjarni
parents:
2844
diff
changeset
|
679 |
if (!IsEngineBuildable(p1, VEH_Train)) return_cmd_error(STR_ENGINE_NOT_BUILDABLE); |
1196
115f46e3807d
(svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents:
1192
diff
changeset
|
680 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
681 |
/* Check if the train is actually being built in a depot belonging |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
682 |
* to the player. Doesn't matter if only the cost is queried */ |
1443
d9c101f7634b
(svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents:
1438
diff
changeset
|
683 |
if (!(flags & DC_QUERY_COST)) { |
d9c101f7634b
(svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents:
1438
diff
changeset
|
684 |
if (!IsTileDepotType(tile, TRANSPORT_RAIL)) return CMD_ERROR; |
1901
fb05044cf5c3
(svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents:
1895
diff
changeset
|
685 |
if (!IsTileOwner(tile, _current_player)) return CMD_ERROR; |
1443
d9c101f7634b
(svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents:
1438
diff
changeset
|
686 |
} |
1221
2b159ebb4899
(svn r1725) - Fix: now trains can only be built in depots and you have to own the depot too (hacked client protection)
bjarni
parents:
1214
diff
changeset
|
687 |
|
0 | 688 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
689 |
||
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
690 |
rvi = RailVehInfo(p1); |
2244 | 691 |
e = GetEngine(p1); |
692 |
||
693 |
/* Check if depot and new engine uses the same kind of tracks */ |
|
694 |
if (!IsCompatibleRail(e->railtype, GetRailType(tile))) return CMD_ERROR; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
695 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
696 |
if (rvi->flags & RVI_WAGON) return CmdBuildRailWagon(p1, tile, flags); |
0 | 697 |
|
698 |
value = EstimateTrainCost(rvi); |
|
2261
3f78323707bb
(svn r2781) Fix some of the issues with variables in .h files.
ludde
parents:
2244
diff
changeset
|
699 |
|
2677
2c1fcffb304c
(svn r3219) -Codechange: removed the now obsolite code in the build train command to make half multiheaded engines
bjarni
parents:
2676
diff
changeset
|
700 |
num_vehicles = (rvi->flags & RVI_MULTIHEAD) ? 2 : 1; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
701 |
num_vehicles += CountArticulatedParts(rvi, p1); |
0 | 702 |
|
703 |
if (!(flags & DC_QUERY_COST)) { |
|
2609
b2c7aa8ce0c4
(svn r3146) Avoid use of variable size declared arrays when allocating articulated rail vehicles.
peter1138
parents:
2608
diff
changeset
|
704 |
Vehicle *vl[12]; // Allow for upto 10 artic parts and dual-heads |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
705 |
if (!AllocateVehicles(vl, num_vehicles) || IsOrderPoolFull()) |
0 | 706 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
707 |
||
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
708 |
v = vl[0]; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
709 |
|
0 | 710 |
unit_num = GetFreeUnitNumber(VEH_Train); |
711 |
if (unit_num > _patches.max_trains) |
|
712 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
|
713 |
||
714 |
if (flags & DC_EXEC) { |
|
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
715 |
uint dir; |
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
716 |
|
0 | 717 |
v->unitnumber = unit_num; |
718 |
||
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
719 |
dir = GB(_m[tile].m5, 0, 2); |
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
720 |
|
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
721 |
v->direction = dir * 2 + 1; |
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
722 |
v->tile = tile; |
0 | 723 |
v->owner = _current_player; |
724 |
v->x_pos = (x |= _vehicle_initial_x_fract[dir]); |
|
725 |
v->y_pos = (y |= _vehicle_initial_y_fract[dir]); |
|
726 |
v->z_pos = GetSlopeZ(x,y); |
|
727 |
v->z_height = 6; |
|
728 |
v->u.rail.track = 0x80; |
|
729 |
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL; |
|
730 |
v->spritenum = rvi->image_index; |
|
731 |
v->cargo_type = rvi->cargo_type; |
|
732 |
v->cargo_cap = rvi->capacity; |
|
733 |
v->max_speed = rvi->max_speed; |
|
734 |
v->value = value; |
|
1266
9dc5638fe8cc
(svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents:
1247
diff
changeset
|
735 |
v->last_station_visited = INVALID_STATION; |
0 | 736 |
v->dest_tile = 0; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
737 |
|
2477
d454d5152386
(svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents:
2475
diff
changeset
|
738 |
v->engine_type = p1; |
0 | 739 |
|
740 |
v->reliability = e->reliability; |
|
741 |
v->reliability_spd_dec = e->reliability_spd_dec; |
|
742 |
v->max_age = e->lifelength * 366; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
743 |
|
0 | 744 |
v->string_id = STR_SV_TRAIN_NAME; |
745 |
v->u.rail.railtype = e->railtype; |
|
746 |
_new_train_id = v->index; |
|
2564 | 747 |
_new_vehicle_id = v->index; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
748 |
|
0 | 749 |
v->service_interval = _patches.servint_trains; |
750 |
v->date_of_last_service = _date; |
|
751 |
v->build_year = _cur_year; |
|
752 |
v->type = VEH_Train; |
|
753 |
v->cur_image = 0xAC2; |
|
2804
b932fe148d36
(svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents:
2758
diff
changeset
|
754 |
v->random_bits = VehicleRandomBits(); |
0 | 755 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
756 |
v->subtype = 0; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
757 |
SetFrontEngine(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
758 |
SetTrainEngine(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
759 |
|
0 | 760 |
VehiclePositionChanged(v); |
761 |
||
2677
2c1fcffb304c
(svn r3219) -Codechange: removed the now obsolite code in the build train command to make half multiheaded engines
bjarni
parents:
2676
diff
changeset
|
762 |
if (rvi->flags & RVI_MULTIHEAD) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
763 |
SetMultiheaded(v); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
764 |
AddRearEngineToMultiheadedTrain(vl[0], vl[1], true); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
765 |
/* Now we need to link the front and rear engines together |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
766 |
* other_multiheaded_part is the pointer that links to the other half of the engine |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
767 |
* vl[0] is the front and vl[1] is the rear |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
768 |
*/ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
769 |
vl[0]->u.rail.other_multiheaded_part = vl[1]; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
770 |
vl[1]->u.rail.other_multiheaded_part = vl[0]; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
771 |
} else { |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
772 |
AddArticulatedParts(rvi, vl); |
2244 | 773 |
} |
0 | 774 |
|
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
775 |
TrainConsistChanged(v); |
0 | 776 |
UpdateTrainAcceleration(v); |
2244 | 777 |
|
2677
2c1fcffb304c
(svn r3219) -Codechange: removed the now obsolite code in the build train command to make half multiheaded engines
bjarni
parents:
2676
diff
changeset
|
778 |
if (!HASBIT(p2, 0)) { // check if the cars should be added to the new vehicle |
2244 | 779 |
NormalizeTrainVehInDepot(v); |
780 |
} |
|
0 | 781 |
|
782 |
InvalidateWindow(WC_VEHICLE_DEPOT, tile); |
|
588 | 783 |
RebuildVehicleLists(); |
0 | 784 |
InvalidateWindow(WC_COMPANY, v->owner); |
2618
7546b838230b
(svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents:
2617
diff
changeset
|
785 |
if (IsLocalPlayer()) { |
7546b838230b
(svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents:
2617
diff
changeset
|
786 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window |
7546b838230b
(svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents:
2617
diff
changeset
|
787 |
} |
0 | 788 |
} |
789 |
} |
|
790 |
_cmd_build_rail_veh_score = _railveh_score[p1]; |
|
1128
d3ffc98b92ad
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1109
diff
changeset
|
791 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
792 |
return value; |
0 | 793 |
} |
794 |
||
795 |
||
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
796 |
/* Check if all the wagons of the given train are in a depot, returns the |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
797 |
* number of cars (including loco) then. If not, sets the error message to |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
798 |
* STR_881A_TRAINS_CAN_ONLY_BE_ALTERED and returns -1 */ |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
799 |
int CheckTrainStoppedInDepot(const Vehicle *v) |
0 | 800 |
{ |
801 |
int count; |
|
802 |
TileIndex tile = v->tile; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
803 |
|
0 | 804 |
/* check if stopped in a depot */ |
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
805 |
if (!IsTileDepotType(tile, TRANSPORT_RAIL) || v->cur_speed != 0) { |
0 | 806 |
_error_message = STR_881A_TRAINS_CAN_ONLY_BE_ALTERED; |
807 |
return -1; |
|
808 |
} |
|
809 |
||
810 |
count = 0; |
|
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
811 |
for (; v != NULL; v = v->next) { |
2844 | 812 |
/* This count is used by the depot code to determine the number of engines |
813 |
* in the consist. Exclude articulated parts so that autoreplacing to |
|
814 |
* engines with more articulated parts that before works correctly. */ |
|
815 |
if (!IsArticulatedPart(v)) count++; |
|
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
816 |
if (v->u.rail.track != 0x80 || v->tile != tile || |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
817 |
(IsFrontEngine(v) && !(v->vehstatus & VS_STOPPED))) { |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
818 |
_error_message = STR_881A_TRAINS_CAN_ONLY_BE_ALTERED; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
819 |
return -1; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
820 |
} |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
821 |
} |
0 | 822 |
|
823 |
return count; |
|
824 |
} |
|
825 |
||
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
826 |
/** |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
827 |
* Unlink a rail wagon from the consist. |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
828 |
* @param v Vehicle to remove. |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
829 |
* @param first The first vehicle of the consist. |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
830 |
* @return The first vehicle of the consist. |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
831 |
*/ |
0 | 832 |
static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first) |
833 |
{ |
|
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
834 |
Vehicle *u; |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
835 |
|
0 | 836 |
// unlinking the first vehicle of the chain? |
837 |
if (v == first) { |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
838 |
v = GetNextVehicle(v); |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
839 |
if (v == NULL) return NULL; |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
840 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
841 |
if (IsTrainWagon(v)) SetFreeWagon(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
842 |
|
0 | 843 |
return v; |
844 |
} |
|
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
845 |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
846 |
for (u = first; GetNextVehicle(u) != v; u = GetNextVehicle(u)) {} |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
847 |
GetLastEnginePart(u)->next = GetNextVehicle(v); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
848 |
return first; |
0 | 849 |
} |
850 |
||
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
851 |
static Vehicle *FindGoodVehiclePos(const Vehicle *src) |
0 | 852 |
{ |
853 |
Vehicle *dst; |
|
2477
d454d5152386
(svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents:
2475
diff
changeset
|
854 |
EngineID eng = src->engine_type; |
0 | 855 |
TileIndex tile = src->tile; |
856 |
||
857 |
FOR_ALL_VEHICLES(dst) { |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
858 |
if (dst->type == VEH_Train && IsFreeWagon(dst) && |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
859 |
dst->tile == tile) { |
0 | 860 |
// check so all vehicles in the line have the same engine. |
861 |
Vehicle *v = dst; |
|
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
862 |
|
0 | 863 |
while (v->engine_type == eng) { |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
864 |
v = v->next; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
865 |
if (v == NULL) return dst; |
0 | 866 |
} |
867 |
} |
|
868 |
} |
|
869 |
||
870 |
return NULL; |
|
871 |
} |
|
872 |
||
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
873 |
/* |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
874 |
* add a vehicle v behind vehicle dest |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
875 |
* use this function since it sets flags as needed |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
876 |
*/ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
877 |
static void AddWagonToConsist(Vehicle *v, Vehicle *dest) |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
878 |
{ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
879 |
UnlinkWagon(v, GetFirstVehicleInChain(v)); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
880 |
if (dest == NULL) return; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
881 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
882 |
v->next = dest->next; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
883 |
dest->next = v; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
884 |
ClearFreeWagon(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
885 |
ClearFrontEngine(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
886 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
887 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
888 |
/* |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
889 |
* move around on the train so rear engines are placed correctly according to the other engines |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
890 |
* always call with the front engine |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
891 |
*/ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
892 |
static void NormaliseTrainConsist(Vehicle *v) |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
893 |
{ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
894 |
Vehicle *u; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
895 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
896 |
if (IsFreeWagon(v)) return; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
897 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
898 |
assert(IsFrontEngine(v)); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
899 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
900 |
for(; v != NULL; v = GetNextVehicle(v)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
901 |
if (!IsMultiheaded(v) || !IsTrainEngine(v)) continue; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
902 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
903 |
/* make sure that there are no free cars before next engine */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
904 |
for(u = v; u->next != NULL && !IsTrainEngine(u->next); u = u->next); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
905 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
906 |
if (u == v->u.rail.other_multiheaded_part) continue; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
907 |
AddWagonToConsist(v->u.rail.other_multiheaded_part, u); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
908 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
909 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
910 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
911 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
912 |
/** Move a rail vehicle around inside the depot. |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
913 |
* @param x,y unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
914 |
* @param p1 various bitstuffed elements |
2639 | 915 |
* - p1 (bit 0 - 15) source vehicle index |
916 |
* - p1 (bit 16 - 31) what wagon to put the source wagon AFTER, XXX - INVALID_VEHICLE to make a new line |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
917 |
* @param p2 (bit 0) move all vehicles following the source vehicle |
0 | 918 |
*/ |
919 |
int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
920 |
{ |
|
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
921 |
VehicleID s = GB(p1, 0, 16); |
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
922 |
VehicleID d = GB(p1, 16, 16); |
0 | 923 |
Vehicle *src, *dst, *src_head, *dst_head; |
924 |
||
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
925 |
if (!IsVehicleIndex(s)) return CMD_ERROR; |
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
926 |
|
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
927 |
src = GetVehicle(s); |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
928 |
|
0 | 929 |
if (src->type != VEH_Train) return CMD_ERROR; |
930 |
||
931 |
// if nothing is selected as destination, try and find a matching vehicle to drag to. |
|
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
932 |
if (d == INVALID_VEHICLE) { |
0 | 933 |
dst = NULL; |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
934 |
if (!IsTrainEngine(src)) dst = FindGoodVehiclePos(src); |
0 | 935 |
} else { |
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
936 |
dst = GetVehicle(d); |
0 | 937 |
} |
938 |
||
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
939 |
// if an articulated part is being handled, deal with its parent vehicle |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
940 |
while (IsArticulatedPart(src)) src = GetPrevVehicleInChain(src); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
941 |
if (dst != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
942 |
while (IsArticulatedPart(dst)) dst = GetPrevVehicleInChain(dst); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
943 |
} |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
944 |
|
0 | 945 |
// don't move the same vehicle.. |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
946 |
if (src == dst) return 0; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
947 |
|
0 | 948 |
/* the player must be the owner */ |
2639 | 949 |
if (!CheckOwnership(src->owner) || (dst != NULL && !CheckOwnership(dst->owner))) |
0 | 950 |
return CMD_ERROR; |
951 |
||
952 |
/* locate the head of the two chains */ |
|
953 |
src_head = GetFirstVehicleInChain(src); |
|
954 |
dst_head = NULL; |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
955 |
if (dst != NULL) { |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
956 |
dst_head = GetFirstVehicleInChain(dst); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
957 |
// Now deal with articulated part of destination wagon |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
958 |
dst = GetLastEnginePart(dst); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
959 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
960 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
961 |
if (dst != NULL && IsMultiheaded(dst) && !IsTrainEngine(dst) && IsTrainWagon(src)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
962 |
/* We are moving a wagon to the rear part of a multiheaded engine */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
963 |
if (dst->next == NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
964 |
/* It's the last one, so we will add the wagon just before the rear engine */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
965 |
dst = GetPrevVehicleInChain(dst); |
2825
f81c0b099d69
(svn r3373) -Fix: don't link a wagon to itself, which resulted in a wagon disapearing, and a depot which was unable to remove
truelight
parents:
2819
diff
changeset
|
966 |
/* Now if the vehicle we want to link to is the vehicle itself, drop out */ |
f81c0b099d69
(svn r3373) -Fix: don't link a wagon to itself, which resulted in a wagon disapearing, and a depot which was unable to remove
truelight
parents:
2819
diff
changeset
|
967 |
if (dst == src) return CMD_ERROR; |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
968 |
// if dst is NULL, it means that dst got a rear multiheaded engine as first engine. We can't use that |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
969 |
if (dst == NULL) return CMD_ERROR; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
970 |
} else { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
971 |
/* there are more units on this train, so we will add the wagon after the next one*/ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
972 |
dst = dst->next; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
973 |
} |
1601
9fd461d00287
(svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents:
1554
diff
changeset
|
974 |
} |
9fd461d00287
(svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents:
1554
diff
changeset
|
975 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
976 |
if (IsTrainEngine(src) && dst_head != NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
977 |
/* we need to make sure that we didn't place it between a pair of multiheaded engines */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
978 |
Vehicle *u, *engine = NULL; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
979 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
980 |
for(u = dst_head; u != NULL; u = u->next) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
981 |
if (IsTrainEngine(u) && IsMultiheaded(u) && u->u.rail.other_multiheaded_part != NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
982 |
engine = u; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
983 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
984 |
if (engine != NULL && engine->u.rail.other_multiheaded_part == u) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
985 |
engine = NULL; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
986 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
987 |
if (u == dst) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
988 |
if (engine != NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
989 |
dst = engine->u.rail.other_multiheaded_part; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
990 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
991 |
break; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
992 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
993 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
994 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
995 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
996 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
997 |
if (IsMultiheaded(src) && !IsTrainEngine(src)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
998 |
|
0 | 999 |
// when moving all wagons, we can't have the same src_head and dst_head |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1000 |
if (HASBIT(p2, 0) && src_head == dst_head) return 0; |
0 | 1001 |
|
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1002 |
{ |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1003 |
int src_len = 0; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1004 |
int max_len = _patches.mammoth_trains ? 100 : 9; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1005 |
|
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1006 |
// check if all vehicles in the source train are stopped inside a depot. |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1007 |
src_len = CheckTrainStoppedInDepot(src_head); |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1008 |
if (src_len < 0) return CMD_ERROR; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1009 |
|
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1010 |
// check the destination row if the source and destination aren't the same. |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1011 |
if (src_head != dst_head) { |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1012 |
int dst_len = 0; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1013 |
|
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1014 |
if (dst_head != NULL) { |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1015 |
// check if all vehicles in the dest train are stopped. |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1016 |
dst_len = CheckTrainStoppedInDepot(dst_head); |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1017 |
if (dst_len < 0) return CMD_ERROR; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1018 |
|
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1019 |
assert(dst_head->tile == src_head->tile); |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1020 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1021 |
|
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1022 |
// We are moving between rows, so only count the wagons from the source |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1023 |
// row that are being moved. |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1024 |
if (HASBIT(p2, 0)) { |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1025 |
const Vehicle *u; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1026 |
for (u = src_head; u != src && u != NULL; u = GetNextVehicle(u)) |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1027 |
src_len--; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1028 |
} else { |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1029 |
// If moving only one vehicle, just count that. |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1030 |
src_len = 1; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1031 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1032 |
|
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1033 |
if (src_len + dst_len > max_len) { |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1034 |
// Abort if we're adding too many wagons to a train. |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1035 |
if (dst_head != NULL && IsFrontEngine(dst_head)) return_cmd_error(STR_8819_TRAIN_TOO_LONG); |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1036 |
// Abort if we're making a train on a new row. |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1037 |
if (dst_head == NULL && IsTrainEngine(src)) return_cmd_error(STR_8819_TRAIN_TOO_LONG); |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1038 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1039 |
} else { |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1040 |
// Abort if we're creating a new train on an existing row. |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1041 |
if (src_len > max_len && src == src_head && IsTrainEngine(GetNextVehicle(src_head))) |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1042 |
return_cmd_error(STR_8819_TRAIN_TOO_LONG); |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1043 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1044 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1045 |
|
0 | 1046 |
// moving a loco to a new line?, then we need to assign a unitnumber. |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1047 |
if (dst == NULL && !IsFrontEngine(src) && IsTrainEngine(src)) { |
1282
e7a73ee62d2f
(svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents:
1266
diff
changeset
|
1048 |
UnitID unit_num = GetFreeUnitNumber(VEH_Train); |
0 | 1049 |
if (unit_num > _patches.max_trains) |
1050 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
|
1051 |
||
1052 |
if (flags & DC_EXEC) |
|
1053 |
src->unitnumber = unit_num; |
|
1054 |
} |
|
1055 |
||
1056 |
||
1057 |
/* do it? */ |
|
1058 |
if (flags & DC_EXEC) { |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1059 |
/* clear the ->first cache */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1060 |
{ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1061 |
Vehicle *u; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1062 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1063 |
for (u = src_head; u != NULL; u = u->next) u->first = NULL; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1064 |
for (u = dst_head; u != NULL; u = u->next) u->first = NULL; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1065 |
} |
2607
08f42ae6fbfc
(svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents:
2602
diff
changeset
|
1066 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1067 |
if (HASBIT(p2, 0)) { |
0 | 1068 |
// unlink ALL wagons |
1069 |
if (src != src_head) { |
|
1070 |
Vehicle *v = src_head; |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
1071 |
while (GetNextVehicle(v) != src) v = GetNextVehicle(v); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
1072 |
GetLastEnginePart(v)->next = NULL; |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1073 |
} else { |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1074 |
src_head = NULL; |
0 | 1075 |
} |
1076 |
} else { |
|
1921
263f1bcfc038
(svn r2427) - Fix: CmdMoveRailVehice; Prevent possible assertion failure when moving vehicles within one chain.
hackykid
parents:
1917
diff
changeset
|
1077 |
// if moving within the same chain, dont use dst_head as it may get invalidated |
263f1bcfc038
(svn r2427) - Fix: CmdMoveRailVehice; Prevent possible assertion failure when moving vehicles within one chain.
hackykid
parents:
1917
diff
changeset
|
1078 |
if (src_head == dst_head) |
263f1bcfc038
(svn r2427) - Fix: CmdMoveRailVehice; Prevent possible assertion failure when moving vehicles within one chain.
hackykid
parents:
1917
diff
changeset
|
1079 |
dst_head = NULL; |
0 | 1080 |
// unlink single wagon from linked list |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1081 |
src_head = UnlinkWagon(src, src_head); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
1082 |
GetLastEnginePart(src)->next = NULL; |
0 | 1083 |
} |
1084 |
||
1085 |
if (dst == NULL) { |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1086 |
// move the train to an empty line. for locomotives, we set the type to TS_Front. for wagons, 4. |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1087 |
if (IsTrainEngine(src)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1088 |
if (!IsFrontEngine(src)) { |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1089 |
// setting the type to 0 also involves setting up the orders field. |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1090 |
SetFrontEngine(src); |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1091 |
assert(src->orders == NULL); |
0 | 1092 |
src->num_orders = 0; |
1093 |
} |
|
1094 |
} else { |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1095 |
SetFreeWagon(src); |
0 | 1096 |
} |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1097 |
dst_head = src; |
0 | 1098 |
} else { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1099 |
if (IsFrontEngine(src)) { |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1100 |
// the vehicle was previously a loco. need to free the order list and delete vehicle windows etc. |
0 | 1101 |
DeleteWindowById(WC_VEHICLE_VIEW, src->index); |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1102 |
DeleteVehicleOrders(src); |
0 | 1103 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1104 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1105 |
ClearFrontEngine(src); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1106 |
ClearFreeWagon(src); |
0 | 1107 |
src->unitnumber = 0; // doesn't occupy a unitnumber anymore. |
1108 |
||
1109 |
// link in the wagon(s) in the chain. |
|
1110 |
{ |
|
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
1111 |
Vehicle *v; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
1112 |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
1113 |
for (v = src; GetNextVehicle(v) != NULL; v = GetNextVehicle(v)); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
1114 |
GetLastEnginePart(v)->next = dst->next; |
0 | 1115 |
} |
1116 |
dst->next = src; |
|
1117 |
} |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1118 |
if (src->u.rail.other_multiheaded_part != NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1119 |
if (src->u.rail.other_multiheaded_part == src_head) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1120 |
src_head = src_head->next; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1121 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1122 |
AddWagonToConsist(src->u.rail.other_multiheaded_part, src); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1123 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1124 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1125 |
if (HASBIT(p2, 0) && src_head != NULL && src_head != src) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1126 |
/* if we stole a rear multiheaded engine, we better give it back to the front end */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1127 |
Vehicle *engine = NULL, *u; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1128 |
for (u = src_head; u != NULL; u = u->next) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1129 |
if (IsMultiheaded(u)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1130 |
if (IsTrainEngine(u)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1131 |
engine = u; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1132 |
continue; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1133 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1134 |
/* we got the rear engine to match with the front one */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1135 |
engine = NULL; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1136 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1137 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1138 |
if (engine != NULL && engine->u.rail.other_multiheaded_part != NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1139 |
AddWagonToConsist(engine->u.rail.other_multiheaded_part, engine); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1140 |
// previous line set the front engine to the old front. We need to clear that |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1141 |
engine->u.rail.other_multiheaded_part->first = NULL; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1142 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1143 |
} |
0 | 1144 |
|
2607
08f42ae6fbfc
(svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents:
2602
diff
changeset
|
1145 |
/* If there is an engine behind first_engine we moved away, it should become new first_engine |
08f42ae6fbfc
(svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents:
2602
diff
changeset
|
1146 |
* To do this, CmdMoveRailVehicle must be called once more |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1147 |
* we can't loop forever here because next time we reach this line we will have a front engine */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1148 |
if (src_head != NULL && !IsFrontEngine(src_head) && IsTrainEngine(src_head)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1149 |
CmdMoveRailVehicle(x, y, flags, src_head->index | (INVALID_VEHICLE << 16), 1); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1150 |
src_head = NULL; // don't do anything more to this train since the new call will do it |
2607
08f42ae6fbfc
(svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents:
2602
diff
changeset
|
1151 |
} |
08f42ae6fbfc
(svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents:
2602
diff
changeset
|
1152 |
|
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1153 |
if (src_head) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1154 |
NormaliseTrainConsist(src_head); |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
1155 |
TrainConsistChanged(src_head); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1156 |
if (IsFrontEngine(src_head)) { |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1157 |
UpdateTrainAcceleration(src_head); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1158 |
InvalidateWindow(WC_VEHICLE_DETAILS, src_head->index); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1159 |
/* Update the refit button and window */ |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1160 |
InvalidateWindow(WC_VEHICLE_REFIT, src_head->index); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1161 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, src_head->index, 12); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1162 |
} |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1163 |
/* Update the depot window */ |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1164 |
InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1165 |
}; |
0 | 1166 |
|
1167 |
if (dst_head) { |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1168 |
NormaliseTrainConsist(dst_head); |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1169 |
TrainConsistChanged(dst_head); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1170 |
if (IsFrontEngine(dst_head)) { |
0 | 1171 |
UpdateTrainAcceleration(dst_head); |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1172 |
InvalidateWindow(WC_VEHICLE_DETAILS, dst_head->index); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1173 |
/* Update the refit button and window */ |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1174 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, dst_head->index, 12); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1175 |
InvalidateWindow(WC_VEHICLE_REFIT, dst_head->index); |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
1176 |
} |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1177 |
/* Update the depot window */ |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1178 |
InvalidateWindow(WC_VEHICLE_DEPOT, dst_head->tile); |
0 | 1179 |
} |
1180 |
||
737
ceb3dce50a37
(svn r1189) Fix vehicle list update glitch when moving waggons in depots
tron
parents:
715
diff
changeset
|
1181 |
RebuildVehicleLists(); |
0 | 1182 |
} |
1183 |
||
1184 |
return 0; |
|
1185 |
} |
|
1186 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1187 |
/** Start/Stop a train. |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1188 |
* @param x,y unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1189 |
* @param p1 train to start/stop |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1190 |
* @param p2 unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1191 |
*/ |
0 | 1192 |
int32 CmdStartStopTrain(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
1193 |
{ |
|
1194 |
Vehicle *v; |
|
1195 |
||
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1196 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1197 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1198 |
v = GetVehicle(p1); |
0 | 1199 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1200 |
if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1201 |
|
1202 |
if (flags & DC_EXEC) { |
|
1203 |
v->u.rail.days_since_order_progr = 0; |
|
1204 |
v->vehstatus ^= VS_STOPPED; |
|
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
1205 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1206 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
1207 |
} |
|
1208 |
return 0; |
|
1209 |
} |
|
1210 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1211 |
/** Sell a (single) train wagon/engine. |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1212 |
* @param x,y unused |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1213 |
* @param p1 the wagon/engine index |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1214 |
* @param p2 the selling mode |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1215 |
* - p2 = 0: only sell the single dragged wagon/engine (and any belonging rear-engines) |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1216 |
* - p2 = 1: sell the vehicle and all vehicles following it in the chain |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1217 |
if the wagon is dragged, don't delete the possibly belonging rear-engine to some front |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1218 |
* - p2 = 2: when selling attached locos, rearrange all vehicles after it to separate lines; |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1219 |
* all wagons of the same type will go on the same line. Used by the AI currently |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1220 |
*/ |
0 | 1221 |
int32 CmdSellRailWagon(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
1222 |
{ |
|
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1223 |
Vehicle *v, *tmp, *first; |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1224 |
Vehicle *new_f = NULL; |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1225 |
int32 cost = 0; |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1226 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1227 |
if (!IsVehicleIndex(p1) || p2 > 2) return CMD_ERROR; |
0 | 1228 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1229 |
v = GetVehicle(p1); |
0 | 1230 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1231 |
if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1232 |
|
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1233 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1234 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1235 |
while (IsArticulatedPart(v)) v = GetPrevVehicleInChain(v); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1236 |
first = GetFirstVehicleInChain(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1237 |
|
0 | 1238 |
// make sure the vehicle is stopped in the depot |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1239 |
if (CheckTrainStoppedInDepot(first) < 0) return CMD_ERROR; |
0 | 1240 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1241 |
if (IsMultiheaded(v) && !IsTrainEngine(v)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1242 |
|
1842
7efaede8ac8e
(svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents:
1802
diff
changeset
|
1243 |
if (flags & DC_EXEC) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1244 |
if (v == first && IsFrontEngine(first)) { |
1842
7efaede8ac8e
(svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents:
1802
diff
changeset
|
1245 |
DeleteWindowById(WC_VEHICLE_VIEW, first->index); |
2618
7546b838230b
(svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents:
2617
diff
changeset
|
1246 |
} |
7546b838230b
(svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents:
2617
diff
changeset
|
1247 |
if (IsLocalPlayer() && (p1 == 1 || !(RailVehInfo(v->engine_type)->flags & RVI_WAGON))) { |
1842
7efaede8ac8e
(svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents:
1802
diff
changeset
|
1248 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); |
7efaede8ac8e
(svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents:
1802
diff
changeset
|
1249 |
} |
0 | 1250 |
InvalidateWindow(WC_VEHICLE_DEPOT, first->tile); |
1779
598bf208ad44
(svn r2283) - Fix (regression): [ 1197493 ] train_gui.c:1341: failed assertion ..., somehow RebuildVehicleList() got lost during the rewrite :P
Darkvater
parents:
1777
diff
changeset
|
1251 |
RebuildVehicleLists(); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1252 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1253 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1254 |
switch (p2) { |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1255 |
case 0: case 2: { /* Delete given wagon */ |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1256 |
bool switch_engine = false; // update second wagon to engine? |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1257 |
byte ori_subtype = v->subtype; // backup subtype of deleted wagon in case DeleteVehicle() changes |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1258 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1259 |
/* 1. Delete the engine, if it is dualheaded also delete the matching |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1260 |
* rear engine of the loco (from the point of deletion onwards) */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1261 |
Vehicle *rear = (IsMultiheaded(v) && |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1262 |
IsTrainEngine(v)) ? v->u.rail.other_multiheaded_part : NULL; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1263 |
|
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1264 |
if (rear != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1265 |
cost -= rear->value; |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1266 |
if (flags & DC_EXEC) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1267 |
UnlinkWagon(rear, first); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1268 |
DeleteVehicle(rear); |
0 | 1269 |
} |
1270 |
} |
|
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1271 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1272 |
/* 2. We are selling the first engine, some special action might be required |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1273 |
* here, so take attention */ |
1770
b6bda4dc6da2
(svn r2274) - Codechange: some comments, parentheses and EngineID typedef for engine_type
Darkvater
parents:
1766
diff
changeset
|
1274 |
if ((flags & DC_EXEC) && v == first) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1275 |
new_f = GetNextVehicle(first); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1276 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1277 |
/* 2.1 If the first wagon is sold, update the first-> pointers to NULL */ |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1278 |
for (tmp = first; tmp != NULL; tmp = tmp->next) tmp->first = NULL; |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1279 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1280 |
/* 2.2 If there are wagons present after the deleted front engine, check |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1281 |
* if the second wagon (which will be first) is an engine. If it is one, |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1282 |
* promote it as a new train, retaining the unitnumber, orders */ |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1283 |
if (new_f != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1284 |
if (IsTrainEngine(new_f)) { |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1285 |
switch_engine = true; |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1286 |
/* Copy important data from the front engine */ |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1287 |
new_f->unitnumber = first->unitnumber; |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1288 |
new_f->current_order = first->current_order; |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1289 |
new_f->cur_order_index = first->cur_order_index; |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1290 |
new_f->orders = first->orders; |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1291 |
new_f->num_orders = first->num_orders; |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1292 |
first->orders = NULL; // XXX - to not to delete the orders */ |
2425
99e1e8430a28
(svn r2951) - Fix: [ 1259345 ] Changing engine in netgame opens train window for everyone
Darkvater
parents:
2360
diff
changeset
|
1293 |
if (IsLocalPlayer()) ShowTrainViewWindow(new_f); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1294 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1295 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1296 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1297 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1298 |
/* 3. Delete the requested wagon */ |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
1299 |
cost -= v->value; |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1300 |
if (flags & DC_EXEC) { |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1301 |
first = UnlinkWagon(v, first); |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1302 |
DeleteVehicle(v); |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1303 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1304 |
/* 4 If the second wagon was an engine, update it to front_engine |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1305 |
* which UnlinkWagon() has changed to TS_Free_Car */ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1306 |
if (switch_engine) SetFrontEngine(first); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1307 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1308 |
/* 5. If the train still exists, update its acceleration, window, etc. */ |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1309 |
if (first != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1310 |
NormaliseTrainConsist(first); |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
1311 |
TrainConsistChanged(first); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1312 |
if (IsFrontEngine(first)) { |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1313 |
InvalidateWindow(WC_VEHICLE_DETAILS, first->index); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1314 |
InvalidateWindow(WC_VEHICLE_REFIT, first->index); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1315 |
UpdateTrainAcceleration(first); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1316 |
} |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1317 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1318 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1319 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1320 |
/* (6.) Borked AI. If it sells an engine it expects all wagons lined |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1321 |
* up on a new line to be added to the newly built loco. Replace it is. |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1322 |
* Totally braindead cause building a new engine adds all loco-less |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1323 |
* engines to its train anyways */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1324 |
if (p2 == 2 && HASBIT(ori_subtype, Train_Front)) { |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1325 |
for (v = first; v != NULL; v = tmp) { |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
1326 |
tmp = GetNextVehicle(v); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1327 |
DoCommandByTile(v->tile, v->index | INVALID_VEHICLE << 16, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1328 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1329 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1330 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1331 |
} break; |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1332 |
case 1: { /* Delete wagon and all wagons after it given certain criteria */ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1333 |
/* Start deleting every vehicle after the selected one |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1334 |
* If we encounter a matching rear-engine to a front-engine |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1335 |
* earlier in the chain (before deletion), leave it alone */ |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1336 |
for (; v != NULL; v = tmp) { |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
1337 |
tmp = GetNextVehicle(v); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1338 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1339 |
if (IsMultiheaded(v)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1340 |
if (IsTrainEngine(v)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1341 |
/* We got a front engine of a multiheaded set. Now we will sell the rear end too */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1342 |
Vehicle *rear = v->u.rail.other_multiheaded_part; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1343 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1344 |
if (rear != NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1345 |
cost -= rear->value; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1346 |
if (flags & DC_EXEC) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1347 |
first = UnlinkWagon(rear, first); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1348 |
DeleteVehicle(rear); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1349 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1350 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1351 |
} else if (v->u.rail.other_multiheaded_part != NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1352 |
/* The front to this engine is earlier in this train. Do nothing */ |
2549 | 1353 |
continue; |
1354 |
} |
|
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1355 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1356 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1357 |
cost -= v->value; |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1358 |
if (flags & DC_EXEC) { |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1359 |
first = UnlinkWagon(v, first); |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1360 |
DeleteVehicle(v); |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1361 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1362 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1363 |
|
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
1364 |
/* 3. If it is still a valid train after selling, update its acceleration and cached values */ |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1365 |
if ((flags & DC_EXEC) && first != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1366 |
NormaliseTrainConsist(first); |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
1367 |
TrainConsistChanged(first); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1368 |
if (IsFrontEngine(first)) |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1369 |
UpdateTrainAcceleration(first); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1370 |
InvalidateWindow(WC_VEHICLE_DETAILS, first->index); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1371 |
InvalidateWindow(WC_VEHICLE_REFIT, first->index); |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
1372 |
} |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1373 |
} break; |
0 | 1374 |
} |
1375 |
return cost; |
|
1376 |
} |
|
1377 |
||
1378 |
static void UpdateTrainDeltaXY(Vehicle *v, int direction) |
|
1379 |
{ |
|
1380 |
#define MKIT(a,b,c,d) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0) |
|
1381 |
static const uint32 _delta_xy_table[8] = { |
|
1382 |
MKIT(3, 3, -1, -1), |
|
1383 |
MKIT(3, 7, -1, -3), |
|
1384 |
MKIT(3, 3, -1, -1), |
|
1385 |
MKIT(7, 3, -3, -1), |
|
1386 |
MKIT(3, 3, -1, -1), |
|
1387 |
MKIT(3, 7, -1, -3), |
|
1388 |
MKIT(3, 3, -1, -1), |
|
1389 |
MKIT(7, 3, -3, -1), |
|
1390 |
}; |
|
1391 |
#undef MKIT |
|
1392 |
||
1393 |
uint32 x = _delta_xy_table[direction]; |
|
1394 |
||
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
1395 |
v->x_offs = GB(x, 0, 8); |
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
1396 |
v->y_offs = GB(x, 8, 8); |
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
1397 |
v->sprite_width = GB(x, 16, 8); |
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
1398 |
v->sprite_height = GB(x, 24, 8); |
0 | 1399 |
} |
1400 |
||
1401 |
static void UpdateVarsAfterSwap(Vehicle *v) |
|
1402 |
{ |
|
1403 |
UpdateTrainDeltaXY(v, v->direction); |
|
1404 |
v->cur_image = GetTrainImage(v, v->direction); |
|
1405 |
BeginVehicleMove(v); |
|
1406 |
VehiclePositionChanged(v); |
|
1407 |
EndVehicleMove(v); |
|
1408 |
} |
|
1409 |
||
2639 | 1410 |
static void SetLastSpeed(Vehicle* v, int spd) |
1411 |
{ |
|
0 | 1412 |
int old = v->u.rail.last_speed; |
1413 |
if (spd != old) { |
|
1414 |
v->u.rail.last_speed = spd; |
|
1415 |
if (_patches.vehicle_speed || !old != !spd) |
|
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
1416 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1417 |
} |
1418 |
} |
|
1419 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1420 |
static void SwapTrainFlags(byte *swap_flag1, byte *swap_flag2) |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1421 |
{ |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1422 |
byte flag1, flag2; |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1423 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1424 |
flag1 = *swap_flag1; |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1425 |
flag2 = *swap_flag2; |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1426 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1427 |
/* Clear the flags */ |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1428 |
CLRBIT(*swap_flag1, VRF_GOINGUP); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1429 |
CLRBIT(*swap_flag1, VRF_GOINGDOWN); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1430 |
CLRBIT(*swap_flag2, VRF_GOINGUP); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1431 |
CLRBIT(*swap_flag2, VRF_GOINGDOWN); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1432 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1433 |
/* Reverse the rail-flags (if needed) */ |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1434 |
if (HASBIT(flag1, VRF_GOINGUP)) { |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1435 |
SETBIT(*swap_flag2, VRF_GOINGDOWN); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1436 |
} else if (HASBIT(flag1, VRF_GOINGDOWN)) { |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1437 |
SETBIT(*swap_flag2, VRF_GOINGUP); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1438 |
} |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1439 |
if (HASBIT(flag2, VRF_GOINGUP)) { |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1440 |
SETBIT(*swap_flag1, VRF_GOINGDOWN); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1441 |
} else if (HASBIT(flag2, VRF_GOINGDOWN)) { |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1442 |
SETBIT(*swap_flag1, VRF_GOINGUP); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1443 |
} |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1444 |
} |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1445 |
|
0 | 1446 |
static void ReverseTrainSwapVeh(Vehicle *v, int l, int r) |
1447 |
{ |
|
1448 |
Vehicle *a, *b; |
|
1449 |
||
1450 |
/* locate vehicles to swap */ |
|
2549 | 1451 |
for (a = v; l != 0; l--) a = a->next; |
1452 |
for (b = v; r != 0; r--) b = b->next; |
|
0 | 1453 |
|
1454 |
if (a != b) { |
|
1455 |
/* swap the hidden bits */ |
|
1456 |
{ |
|
1457 |
uint16 tmp = (a->vehstatus & ~VS_HIDDEN) | (b->vehstatus&VS_HIDDEN); |
|
1458 |
b->vehstatus = (b->vehstatus & ~VS_HIDDEN) | (a->vehstatus&VS_HIDDEN); |
|
1459 |
a->vehstatus = tmp; |
|
1460 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1461 |
|
0 | 1462 |
/* swap variables */ |
1463 |
swap_byte(&a->u.rail.track, &b->u.rail.track); |
|
1464 |
swap_byte(&a->direction, &b->direction); |
|
1465 |
||
1466 |
/* toggle direction */ |
|
1467 |
if (!(a->u.rail.track & 0x80)) a->direction ^= 4; |
|
1468 |
if (!(b->u.rail.track & 0x80)) b->direction ^= 4; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1469 |
|
0 | 1470 |
/* swap more variables */ |
1174
27e386195965
(svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents:
1151
diff
changeset
|
1471 |
swap_int32(&a->x_pos, &b->x_pos); |
27e386195965
(svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents:
1151
diff
changeset
|
1472 |
swap_int32(&a->y_pos, &b->y_pos); |
0 | 1473 |
swap_tile(&a->tile, &b->tile); |
1474 |
swap_byte(&a->z_pos, &b->z_pos); |
|
1475 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1476 |
SwapTrainFlags(&a->u.rail.flags, &b->u.rail.flags); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1477 |
|
0 | 1478 |
/* update other vars */ |
1479 |
UpdateVarsAfterSwap(a); |
|
1480 |
UpdateVarsAfterSwap(b); |
|
1554
c0b2287254f4
(svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents:
1552
diff
changeset
|
1481 |
|
c0b2287254f4
(svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents:
1552
diff
changeset
|
1482 |
VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos); |
c0b2287254f4
(svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents:
1552
diff
changeset
|
1483 |
VehicleEnterTile(b, b->tile, b->x_pos, b->y_pos); |
0 | 1484 |
} else { |
1485 |
if (!(a->u.rail.track & 0x80)) a->direction ^= 4; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1486 |
UpdateVarsAfterSwap(a); |
1554
c0b2287254f4
(svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents:
1552
diff
changeset
|
1487 |
|
c0b2287254f4
(svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents:
1552
diff
changeset
|
1488 |
VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos); |
0 | 1489 |
} |
1490 |
} |
|
1491 |
||
744
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1492 |
/* Check if the vehicle is a train and is on the tile we are testing */ |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1493 |
static void *TestTrainOnCrossing(Vehicle *v, void *data) |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1494 |
{ |
2639 | 1495 |
if (v->tile != *(const TileIndex*)data || v->type != VEH_Train) return NULL; |
744
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1496 |
return v; |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1497 |
} |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1498 |
|
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1499 |
static void DisableTrainCrossing(TileIndex tile) |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1500 |
{ |
2639 | 1501 |
if (IsTileType(tile, MP_STREET) && |
1502 |
IsLevelCrossing(tile) && |
|
1503 |
VehicleFromPos(tile, &tile, TestTrainOnCrossing) == NULL && // empty? |
|
1504 |
GB(_m[tile].m5, 2, 1) != 0) { // Lights on? |
|
1505 |
SB(_m[tile].m5, 2, 1, 0); // Switch lights off |
|
1506 |
MarkTileDirtyByTile(tile); |
|
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1507 |
} |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1508 |
} |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1509 |
|
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1510 |
/** |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1511 |
* Advances wagons for train reversing, needed for variable length wagons. |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1512 |
* Needs to be called once before the train is reversed, and once after it. |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1513 |
* @param v First vehicle in chain |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1514 |
* @param before Set to true for the call before reversing, false otherwise |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1515 |
*/ |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1516 |
static void AdvanceWagons(Vehicle *v, bool before) |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1517 |
{ |
2639 | 1518 |
Vehicle* base; |
1519 |
Vehicle* first; |
|
1520 |
int length; |
|
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1521 |
|
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1522 |
base = v; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1523 |
first = base->next; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1524 |
length = CountVehiclesInChain(v); |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1525 |
|
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1526 |
while (length > 2) { |
2639 | 1527 |
Vehicle* last; |
1528 |
int differential; |
|
1529 |
int i; |
|
1530 |
||
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1531 |
// find pairwise matching wagon |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1532 |
// start<>end, start+1<>end-1, ... */ |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1533 |
last = first; |
2639 | 1534 |
for (i = length - 3; i > 0; i--) last = last->next; |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1535 |
|
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1536 |
differential = last->u.rail.cached_veh_length - base->u.rail.cached_veh_length; |
2639 | 1537 |
if (before) differential *= -1; |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1538 |
|
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1539 |
if (differential > 0) { |
2639 | 1540 |
Vehicle* tempnext; |
1541 |
||
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1542 |
// disconnect last car to make sure only this subset moves |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1543 |
tempnext = last->next; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1544 |
last->next = NULL; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1545 |
|
2639 | 1546 |
for (i = 0; i < differential; i++) TrainController(first); |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1547 |
|
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1548 |
last->next = tempnext; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1549 |
} |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1550 |
|
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1551 |
base = first; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1552 |
first = first->next; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1553 |
length -= 2; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1554 |
} |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1555 |
} |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1556 |
|
0 | 1557 |
static void ReverseTrainDirection(Vehicle *v) |
1558 |
{ |
|
1559 |
int l = 0, r = -1; |
|
1560 |
Vehicle *u; |
|
1561 |
||
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
1562 |
if (IsTileDepotType(v->tile, TRANSPORT_RAIL)) |
0 | 1563 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
1564 |
||
2008
c9d6585c96c8
(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents:
2006
diff
changeset
|
1565 |
|
743
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1566 |
/* Check if we were approaching a rail/road-crossing */ |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1567 |
{ |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1568 |
TileIndex tile = v->tile; |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1569 |
int t; |
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1455
diff
changeset
|
1570 |
/* Determine the diagonal direction in which we will exit this tile */ |
743
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1571 |
t = v->direction >> 1; |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1572 |
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) { |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1573 |
t = (t - 1) & 3; |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1574 |
} |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1575 |
/* Calculate next tile */ |
900 | 1576 |
tile += TileOffsByDir(t); |
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1577 |
|
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1578 |
/* Check if the train left a rail/road-crossing */ |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1579 |
DisableTrainCrossing(tile); |
743
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1580 |
} |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1581 |
|
0 | 1582 |
// count number of vehicles |
1583 |
u = v; |
|
1584 |
do r++; while ( (u = u->next) != NULL ); |
|
1585 |
||
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1586 |
AdvanceWagons(v, true); |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1587 |
|
0 | 1588 |
/* swap start<>end, start+1<>end-1, ... */ |
1589 |
do { |
|
1590 |
ReverseTrainSwapVeh(v, l++, r--); |
|
1591 |
} while (l <= r); |
|
1592 |
||
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1593 |
AdvanceWagons(v, false); |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1594 |
|
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
1595 |
if (IsTileDepotType(v->tile, TRANSPORT_RAIL)) |
0 | 1596 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
1597 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1598 |
CLRBIT(v->u.rail.flags, VRF_REVERSING); |
0 | 1599 |
} |
1600 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1601 |
/** Reverse train. |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1602 |
* @param x,y unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1603 |
* @param p1 train to reverse |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1604 |
* @param p2 unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1605 |
*/ |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1606 |
int32 CmdReverseTrainDirection(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
0 | 1607 |
{ |
1608 |
Vehicle *v; |
|
1609 |
||
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1610 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1611 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1612 |
v = GetVehicle(p1); |
0 | 1613 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1614 |
if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1615 |
|
1616 |
_error_message = STR_EMPTY; |
|
1617 |
||
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
1618 |
// if (v->u.rail.track & 0x80 || IsTileDepotType(v->tile, TRANSPORT_RAIL)) |
0 | 1619 |
// return CMD_ERROR; |
1620 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1621 |
if (v->u.rail.crash_anim_pos != 0 || v->breakdown_ctr != 0) return CMD_ERROR; |
0 | 1622 |
|
1623 |
if (flags & DC_EXEC) { |
|
1624 |
if (_patches.realistic_acceleration && v->cur_speed != 0) { |
|
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1625 |
TOGGLEBIT(v->u.rail.flags, VRF_REVERSING); |
0 | 1626 |
} else { |
1627 |
v->cur_speed = 0; |
|
1628 |
SetLastSpeed(v, 0); |
|
1629 |
ReverseTrainDirection(v); |
|
1630 |
} |
|
1631 |
} |
|
1632 |
return 0; |
|
1633 |
} |
|
1634 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1635 |
/** Force a train through a red signal |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1636 |
* @param x,y unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1637 |
* @param p1 train to ignore the red signal |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1638 |
* @param p2 unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1639 |
*/ |
0 | 1640 |
int32 CmdForceTrainProceed(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
1641 |
{ |
|
1642 |
Vehicle *v; |
|
1643 |
||
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1644 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1645 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1646 |
v = GetVehicle(p1); |
0 | 1647 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1648 |
if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1649 |
|
2639 | 1650 |
if (flags & DC_EXEC) v->u.rail.force_proceed = 0x50; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1651 |
|
0 | 1652 |
return 0; |
1653 |
} |
|
1654 |
||
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1655 |
/** Refits a train to the specified cargo type. |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1656 |
* @param x,y unused |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1657 |
* @param p1 vehicle ID of the train to refit |
2244 | 1658 |
* @param p2 the new cargo type to refit to (p2 & 0xFF) |
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1659 |
*/ |
0 | 1660 |
int32 CmdRefitRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
1661 |
{ |
|
2635 | 1662 |
CargoID new_cid = GB(p2, 0, 8); |
0 | 1663 |
Vehicle *v; |
1664 |
int32 cost; |
|
1665 |
uint num; |
|
842 | 1666 |
|
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1667 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
915 | 1668 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1669 |
v = GetVehicle(p1); |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1670 |
|
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1671 |
if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR; |
2244 | 1672 |
if (CheckTrainStoppedInDepot(v) < 0) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED); |
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1673 |
|
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1674 |
/* Check cargo */ |
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1675 |
if (new_cid > NUM_CARGO) return CMD_ERROR; |
0 | 1676 |
|
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1677 |
SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN); |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1678 |
|
0 | 1679 |
cost = 0; |
1680 |
num = 0; |
|
915 | 1681 |
|
0 | 1682 |
do { |
491
0642006d876c
(svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents:
445
diff
changeset
|
1683 |
/* XXX: We also refit all the attached wagons en-masse if they |
0642006d876c
(svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents:
445
diff
changeset
|
1684 |
* can be refitted. This is how TTDPatch does it. TODO: Have |
0642006d876c
(svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents:
445
diff
changeset
|
1685 |
* some nice [Refit] button near each wagon. --pasky */ |
2704
dc9ae68dfd74
(svn r3248) - Codechange: Change interface of CanRefitTo() to supply the engine type directly instead of getting it from a vehicle. This allows the function to be used before vehicles are involved.
peter1138
parents:
2677
diff
changeset
|
1686 |
if (!CanRefitTo(v->engine_type, new_cid)) continue; |
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1687 |
|
1859
b3af0081df39
(svn r2365) - Change: [refitting] Make refitting capacities for trains newgrf compatible. Train vehicles can now carry twice as much mail/goods as other cargo, and four times as much passengers.
hackykid
parents:
1842
diff
changeset
|
1688 |
if (v->cargo_cap != 0) { |
2463
de2605ed3758
(svn r2989) - Make engine/vehicle information tables constant. Duplicate them so NewGRF data can be loaded without wiping out the default data.
peter1138
parents:
2456
diff
changeset
|
1689 |
const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); |
1895
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1690 |
uint16 amount = CALLBACK_FAILED; |
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1691 |
|
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1692 |
if (HASBIT(rvi->callbackmask, CBM_REFIT_CAP)) { |
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1693 |
/* Check the 'refit capacity' callback */ |
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1694 |
CargoID temp_cid = v->cargo_type; |
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1695 |
v->cargo_type = new_cid; |
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1696 |
amount = GetCallBackResult(CBID_REFIT_CAP, v->engine_type, v); |
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1697 |
v->cargo_type = temp_cid; |
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1698 |
} |
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1699 |
|
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1700 |
if (amount == CALLBACK_FAILED) { // callback failed or not used, use default |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1701 |
CargoID old_cid = rvi->cargo_type; |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1702 |
/* normally, the capacity depends on the cargo type, a rail vehicle |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1703 |
* can carry twice as much mail/goods as normal cargo, |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1704 |
* and four times as much passengers */ |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1705 |
amount = rvi->capacity; |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1706 |
(old_cid == CT_PASSENGERS) || |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1707 |
(amount <<= 1, old_cid == CT_MAIL || old_cid == CT_GOODS) || |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1708 |
(amount <<= 1, true); |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1709 |
(new_cid == CT_PASSENGERS) || |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1710 |
(amount >>= 1, new_cid == CT_MAIL || new_cid == CT_GOODS) || |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1711 |
(amount >>= 1, true); |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1712 |
}; |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1713 |
|
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1714 |
if (amount != 0) { |
2639 | 1715 |
if (new_cid != v->cargo_type) cost += _price.build_railvehicle >> 8; |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1716 |
num += amount; |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1717 |
if (flags & DC_EXEC) { |
2244 | 1718 |
v->cargo_count = 0; |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1719 |
v->cargo_type = new_cid; |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1720 |
v->cargo_cap = amount; |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1721 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1722 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1723 |
} |
0 | 1724 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1725 |
} |
2244 | 1726 |
} while ( (v=v->next) != NULL ); |
0 | 1727 |
|
1728 |
_returned_refit_amount = num; |
|
1729 |
||
1730 |
return cost; |
|
1731 |
} |
|
1732 |
||
1733 |
typedef struct TrainFindDepotData { |
|
1734 |
uint best_length; |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1969
diff
changeset
|
1735 |
TileIndex tile; |
2475 | 1736 |
PlayerID owner; |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
1737 |
/** |
2639 | 1738 |
* true if reversing is necessary for the train to get to this depot |
1739 |
* This value is unused when new depot finding and NPF are both disabled |
|
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
1740 |
*/ |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
1741 |
bool reverse; |
0 | 1742 |
} TrainFindDepotData; |
1743 |
||
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
1744 |
static bool NtpCallbFindDepot(TileIndex tile, TrainFindDepotData *tfdd, int track, uint length) |
0 | 1745 |
{ |
1901
fb05044cf5c3
(svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents:
1895
diff
changeset
|
1746 |
if (IsTileType(tile, MP_RAILWAY) && IsTileOwner(tile, tfdd->owner)) { |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2044
diff
changeset
|
1747 |
if ((_m[tile].m5 & ~0x3) == 0xC0) { |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
1748 |
tfdd->best_length = length; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
1749 |
tfdd->tile = tile; |
0 | 1750 |
return true; |
1751 |
} |
|
1752 |
} |
|
1753 |
||
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
1754 |
return false; |
0 | 1755 |
} |
1756 |
||
1758
68668114f92e
(svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents:
1757
diff
changeset
|
1757 |
// returns the tile of a depot to goto to. The given vehicle must not be |
68668114f92e
(svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents:
1757
diff
changeset
|
1758 |
// crashed! |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1759 |
static TrainFindDepotData FindClosestTrainDepot(Vehicle *v) |
0 | 1760 |
{ |
1761 |
int i; |
|
1762 |
TrainFindDepotData tfdd; |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1969
diff
changeset
|
1763 |
TileIndex tile = v->tile; |
0 | 1764 |
|
1758
68668114f92e
(svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents:
1757
diff
changeset
|
1765 |
assert(!(v->vehstatus & VS_CRASHED)); |
68668114f92e
(svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents:
1757
diff
changeset
|
1766 |
|
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1767 |
tfdd.owner = v->owner; |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1768 |
tfdd.best_length = (uint)-1; |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
1769 |
tfdd.reverse = false; |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1770 |
|
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
1771 |
if (IsTileDepotType(tile, TRANSPORT_RAIL)){ |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1772 |
tfdd.tile = tile; |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1773 |
tfdd.best_length = 0; |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1774 |
return tfdd; |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1775 |
} |
0 | 1776 |
|
2549 | 1777 |
if (v->u.rail.track == 0x40) tile = GetVehicleOutOfTunnelTile(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1778 |
|
1247 | 1779 |
if (_patches.new_pathfinding_all) { |
1780 |
NPFFoundTargetData ftd; |
|
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
1781 |
Vehicle* last = GetLastVehicleInChain(v); |
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
1782 |
Trackdir trackdir = GetVehicleTrackdir(v); |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
1783 |
Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last)); |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
1784 |
|
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
1785 |
assert (trackdir != INVALID_TRACKDIR); |
2006
324916f22a8a
(svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents:
1998
diff
changeset
|
1786 |
ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, last->tile, trackdir_rev, TRANSPORT_RAIL, v->owner, v->u.rail.railtype, NPF_INFINITE_PENALTY); |
1247 | 1787 |
if (ftd.best_bird_dist == 0) { |
1788 |
/* Found target */ |
|
1789 |
tfdd.tile = ftd.node.tile; |
|
1675
d004a4d0f6d4
(svn r2179) - Fix: [ 1121437, 1114228 ] Vehicles not entering depots for auto servicing.
matthijs
parents:
1615
diff
changeset
|
1790 |
/* Our caller expects a number of tiles, so we just approximate that |
d004a4d0f6d4
(svn r2179) - Fix: [ 1121437, 1114228 ] Vehicles not entering depots for auto servicing.
matthijs
parents:
1615
diff
changeset
|
1791 |
* number by this. It might not be completely what we want, but it will |
d004a4d0f6d4
(svn r2179) - Fix: [ 1121437, 1114228 ] Vehicles not entering depots for auto servicing.
matthijs
parents:
1615
diff
changeset
|
1792 |
* work for now :-) We can possibly change this when the old pathfinder |
d004a4d0f6d4
(svn r2179) - Fix: [ 1121437, 1114228 ] Vehicles not entering depots for auto servicing.
matthijs
parents:
1615
diff
changeset
|
1793 |
* is removed. */ |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
1794 |
tfdd.best_length = ftd.best_path_dist / NPF_TILE_LENGTH; |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
1795 |
if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
1796 |
tfdd.reverse = true; |
1247 | 1797 |
} |
0 | 1798 |
} else { |
1799 |
// search in the forward direction first. |
|
1800 |
i = v->direction >> 1; |
|
2639 | 1801 |
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) i = (i - 1) & 3; |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
1802 |
NewTrainPathfind(tile, 0, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd); |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1803 |
if (tfdd.best_length == (uint)-1){ |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
1804 |
tfdd.reverse = true; |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1805 |
// search in backwards direction |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1806 |
i = (v->direction^4) >> 1; |
2639 | 1807 |
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) i = (i - 1) & 3; |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
1808 |
NewTrainPathfind(tile, 0, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd); |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1809 |
} |
0 | 1810 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1811 |
|
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1812 |
return tfdd; |
0 | 1813 |
} |
1814 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1815 |
/** Send a train to a depot |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1816 |
* @param x,y unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1817 |
* @param p1 train to send to the depot |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1818 |
* @param p2 unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1819 |
*/ |
1794
44f9deff97ed
(svn r2298) - CodeChange: removed CmdAbuses: CmdSetTownNameType(), CmdStartNewGame(), CmdCreateScenario(), CmdSetNewLandscapeType() and CmdGenRandomNewGame().
Darkvater
parents:
1790
diff
changeset
|
1820 |
int32 CmdSendTrainToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
0 | 1821 |
{ |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1822 |
Vehicle *v; |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1823 |
TrainFindDepotData tfdd; |
0 | 1824 |
|
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1825 |
if (!IsVehicleIndex(p1)) return CMD_ERROR; |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1826 |
|
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1827 |
v = GetVehicle(p1); |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1828 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1829 |
if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR; |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1830 |
|
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1831 |
if (v->vehstatus & VS_CRASHED) return CMD_ERROR; |
1757
b179d50241dc
(svn r2261) - Fix: When crashed vehicles try to find a depot for servicing, openttd asserts.
matthijs
parents:
1752
diff
changeset
|
1832 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1833 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
0 | 1834 |
if (flags & DC_EXEC) { |
1615
cee18da24b41
(svn r2119) - Fix: [ 1172878 ] Trains "Go to depot" button: click twice skip to next order (patch by Loic GUILLOUX). I also added short explanation of OF_/OFB_ difference to order.h.
pasky
parents:
1601
diff
changeset
|
1835 |
if (HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) { |
0 | 1836 |
v->u.rail.days_since_order_progr = 0; |
1837 |
v->cur_order_index++; |
|
1838 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1839 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1840 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1841 |
v->current_order.flags = 0; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
1842 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1843 |
} |
1844 |
return 0; |
|
1845 |
} |
|
1846 |
||
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1847 |
tfdd = FindClosestTrainDepot(v); |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1848 |
if (tfdd.best_length == (uint)-1) |
0 | 1849 |
return_cmd_error(STR_883A_UNABLE_TO_FIND_ROUTE_TO); |
1850 |
||
1851 |
if (flags & DC_EXEC) { |
|
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1852 |
v->dest_tile = tfdd.tile; |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1853 |
v->current_order.type = OT_GOTO_DEPOT; |
1520
c85dc79795e0
(svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents:
1507
diff
changeset
|
1854 |
v->current_order.flags = OF_NON_STOP | OF_FULL_LOAD; |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
1855 |
v->current_order.station = GetDepotByTile(tfdd.tile)->index; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
1856 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
1857 |
/* If there is no depot in front, reverse automatically */ |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
1858 |
if (tfdd.reverse) |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
1859 |
DoCommandByTile(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION); |
0 | 1860 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1861 |
|
0 | 1862 |
return 0; |
1863 |
} |
|
1864 |
||
1865 |
||
1093
e8d26c7dc42f
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1087
diff
changeset
|
1866 |
void OnTick_Train(void) |
0 | 1867 |
{ |
1868 |
_age_cargo_skip_counter = (_age_cargo_skip_counter == 0) ? 184 : (_age_cargo_skip_counter - 1); |
|
1869 |
} |
|
1870 |
||
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1871 |
static const int8 _vehicle_smoke_pos[8] = { |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1872 |
1, 1, 1, 0, -1, -1, -1, 0 |
0 | 1873 |
}; |
1874 |
||
2630 | 1875 |
static void HandleLocomotiveSmokeCloud(const Vehicle* v) |
0 | 1876 |
{ |
2630 | 1877 |
const Vehicle* u; |
0 | 1878 |
|
1879 |
if (v->vehstatus & VS_TRAIN_SLOWING || v->load_unload_time_rem != 0 || v->cur_speed < 2) |
|
1880 |
return; |
|
1881 |
||
1882 |
u = v; |
|
1883 |
||
1884 |
do { |
|
2477
d454d5152386
(svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents:
2475
diff
changeset
|
1885 |
EngineID engtype = v->engine_type; |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1886 |
int effect_offset = GB(v->u.rail.cached_vis_effect, 0, 4) - 8; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1887 |
byte effect_type = GB(v->u.rail.cached_vis_effect, 4, 2); |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1888 |
bool disable_effect = HASBIT(v->u.rail.cached_vis_effect, 6); |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1889 |
int x, y; |
0 | 1890 |
|
1891 |
// no smoke? |
|
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1892 |
if ((RailVehInfo(engtype)->flags & RVI_WAGON && effect_type == 0) || |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1893 |
disable_effect || |
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
1894 |
GetEngine(engtype)->railtype > RAILTYPE_RAIL || |
2639 | 1895 |
v->vehstatus & VS_HIDDEN || |
1896 |
v->u.rail.track & 0xC0) { |
|
0 | 1897 |
continue; |
2639 | 1898 |
} |
0 | 1899 |
|
2612
744aef3af72f
(svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents:
2610
diff
changeset
|
1900 |
// No smoke in depots or tunnels |
744aef3af72f
(svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents:
2610
diff
changeset
|
1901 |
if (IsTileDepotType(v->tile, TRANSPORT_RAIL) || IsTunnelTile(v->tile)) |
744aef3af72f
(svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents:
2610
diff
changeset
|
1902 |
continue; |
744aef3af72f
(svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents:
2610
diff
changeset
|
1903 |
|
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1904 |
if (effect_type == 0) { |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1905 |
// Use default effect type for engine class. |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1906 |
effect_type = RailVehInfo(engtype)->engclass; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1907 |
} else { |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1908 |
effect_type--; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1909 |
} |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1910 |
|
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1911 |
x = _vehicle_smoke_pos[v->direction] * effect_offset; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1912 |
y = _vehicle_smoke_pos[(v->direction + 2) % 8] * effect_offset; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1913 |
|
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1914 |
switch (effect_type) { |
0 | 1915 |
case 0: |
1916 |
// steam smoke. |
|
2612
744aef3af72f
(svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents:
2610
diff
changeset
|
1917 |
if (GB(v->tick_counter, 0, 4) == 0) { |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
1918 |
CreateEffectVehicleRel(v, x, y, 10, EV_STEAM_SMOKE); |
0 | 1919 |
} |
1920 |
break; |
|
1921 |
||
1922 |
case 1: |
|
1923 |
// diesel smoke |
|
2637
8807e6a63f89
(svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents:
2635
diff
changeset
|
1924 |
if (u->cur_speed <= 40 && CHANCE16(15, 128)) { |
1359
8ba976aed634
(svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents:
1337
diff
changeset
|
1925 |
CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE); |
0 | 1926 |
} |
1927 |
break; |
|
1928 |
||
1929 |
case 2: |
|
1930 |
// blue spark |
|
2637
8807e6a63f89
(svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents:
2635
diff
changeset
|
1931 |
if (GB(v->tick_counter, 0, 2) == 0 && CHANCE16(1, 45)) { |
1359
8ba976aed634
(svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents:
1337
diff
changeset
|
1932 |
CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK); |
0 | 1933 |
} |
1934 |
break; |
|
1935 |
} |
|
2637
8807e6a63f89
(svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents:
2635
diff
changeset
|
1936 |
} while ((v = v->next) != NULL); |
0 | 1937 |
} |
1938 |
||
2549 | 1939 |
static void TrainPlayLeaveStationSound(const Vehicle* v) |
0 | 1940 |
{ |
541 | 1941 |
static const SoundFx sfx[] = { |
1942 |
SND_04_TRAIN, |
|
1943 |
SND_0A_TRAIN_HORN, |
|
1944 |
SND_0A_TRAIN_HORN |
|
1945 |
}; |
|
1946 |
||
2477
d454d5152386
(svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents:
2475
diff
changeset
|
1947 |
EngineID engtype = v->engine_type; |
0 | 1948 |
|
1926
68d60188a22f
(svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents:
1925
diff
changeset
|
1949 |
switch (GetEngine(engtype)->railtype) { |
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
1950 |
case RAILTYPE_RAIL: |
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
1951 |
SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], v); |
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
1952 |
break; |
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
1953 |
|
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
1954 |
case RAILTYPE_MONO: |
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
1955 |
SndPlayVehicleFx(SND_47_MAGLEV_2, v); |
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
1956 |
break; |
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
1957 |
|
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
1958 |
case RAILTYPE_MAGLEV: |
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
1959 |
SndPlayVehicleFx(SND_41_MAGLEV, v); |
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
1960 |
break; |
0 | 1961 |
} |
1962 |
} |
|
1963 |
||
1964 |
static bool CheckTrainStayInDepot(Vehicle *v) |
|
1965 |
{ |
|
1966 |
Vehicle *u; |
|
495
bfc16f011bb7
(svn r785) -Fix: A train can leave and enter the same depot at the same time, then the trai simply got stuck
tron
parents:
491
diff
changeset
|
1967 |
|
bfc16f011bb7
(svn r785) -Fix: A train can leave and enter the same depot at the same time, then the trai simply got stuck
tron
parents:
491
diff
changeset
|
1968 |
// bail out if not all wagons are in the same depot or not in a depot at all |
2639 | 1969 |
for (u = v; u != NULL; u = u->next) { |
1970 |
if (u->u.rail.track != 0x80 || u->tile != v->tile) return false; |
|
1971 |
} |
|
0 | 1972 |
|
1973 |
if (v->u.rail.force_proceed == 0) { |
|
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
1974 |
if (++v->load_unload_time_rem < 37) { |
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
1975 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 1976 |
return true; |
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
1977 |
} |
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
1978 |
|
0 | 1979 |
v->load_unload_time_rem = 0; |
1980 |
||
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
1981 |
if (UpdateSignalsOnSegment(v->tile, v->direction)) { |
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
1982 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 1983 |
return true; |
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
1984 |
} |
0 | 1985 |
} |
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2883
diff
changeset
|
1986 |
|
578
86e352980acd
(svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents:
555
diff
changeset
|
1987 |
VehicleServiceInDepot(v); |
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
1988 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 1989 |
TrainPlayLeaveStationSound(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1990 |
|
0 | 1991 |
v->u.rail.track = 1; |
2639 | 1992 |
if (v->direction & 2) v->u.rail.track = 2; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1993 |
|
0 | 1994 |
v->vehstatus &= ~VS_HIDDEN; |
1995 |
v->cur_speed = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1996 |
|
0 | 1997 |
UpdateTrainDeltaXY(v, v->direction); |
1998 |
v->cur_image = GetTrainImage(v, v->direction); |
|
1999 |
VehiclePositionChanged(v); |
|
2000 |
UpdateSignalsOnSegment(v->tile, v->direction); |
|
2001 |
UpdateTrainAcceleration(v); |
|
2002 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
2003 |
||
2004 |
return false; |
|
2005 |
} |
|
2006 |
||
1247 | 2007 |
/* Check for station tiles */ |
0 | 2008 |
typedef struct TrainTrackFollowerData { |
2009 |
TileIndex dest_coords; |
|
1551
eb05e6a90e67
(svn r2055) -CodeChange: Begun introducting StationID
celestar
parents:
1542
diff
changeset
|
2010 |
StationID station_index; // station index we're heading for |
0 | 2011 |
uint best_bird_dist; |
2012 |
uint best_track_dist; |
|
2013 |
byte best_track; |
|
2014 |
} TrainTrackFollowerData; |
|
2015 |
||
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2016 |
static bool NtpCallbFindStation(TileIndex tile, TrainTrackFollowerData *ttfd, int track, uint length) |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1969
diff
changeset
|
2017 |
{ |
0 | 2018 |
// heading for nowhere? |
2019 |
if (ttfd->dest_coords == 0) |
|
2020 |
return false; |
|
2021 |
||
2022 |
// did we reach the final station? |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2023 |
if ((ttfd->station_index == INVALID_STATION && tile == ttfd->dest_coords) || |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2044
diff
changeset
|
2024 |
(IsTileType(tile, MP_STATION) && IS_BYTE_INSIDE(_m[tile].m5, 0, 8) && _m[tile].m2 == ttfd->station_index)) { |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2025 |
/* We do not check for dest_coords if we have a station_index, |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2026 |
* because in that case the dest_coords are just an |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2027 |
* approximation of where the station is */ |
0 | 2028 |
// found station |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2029 |
ttfd->best_track = track; |
0 | 2030 |
return true; |
2031 |
} else { |
|
2032 |
uint dist; |
|
2033 |
||
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2034 |
// didn't find station, keep track of the best path so far. |
1245
768d9bc95aaa
(svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents:
1237
diff
changeset
|
2035 |
dist = DistanceManhattan(tile, ttfd->dest_coords); |
0 | 2036 |
if (dist < ttfd->best_bird_dist) { |
2037 |
ttfd->best_bird_dist = dist; |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2038 |
ttfd->best_track = track; |
0 | 2039 |
} |
2040 |
return false; |
|
2041 |
} |
|
2042 |
} |
|
2043 |
||
2630 | 2044 |
static void FillWithStationData(TrainTrackFollowerData* fd, const Vehicle* v) |
0 | 2045 |
{ |
2639 | 2046 |
fd->dest_coords = v->dest_tile; |
2047 |
if (v->current_order.type == OT_GOTO_STATION) { |
|
2048 |
fd->station_index = v->current_order.station; |
|
2049 |
} else { |
|
2050 |
fd->station_index = INVALID_STATION; |
|
2051 |
} |
|
0 | 2052 |
} |
2053 |
||
2054 |
static const byte _initial_tile_subcoord[6][4][3] = { |
|
2055 |
{{ 15, 8, 1 },{ 0, 0, 0 },{ 0, 8, 5 },{ 0, 0, 0 }}, |
|
2056 |
{{ 0, 0, 0 },{ 8, 0, 3 },{ 0, 0, 0 },{ 8,15, 7 }}, |
|
2057 |
{{ 0, 0, 0 },{ 7, 0, 2 },{ 0, 7, 6 },{ 0, 0, 0 }}, |
|
2058 |
{{ 15, 8, 2 },{ 0, 0, 0 },{ 0, 0, 0 },{ 8,15, 6 }}, |
|
2059 |
{{ 15, 7, 0 },{ 8, 0, 4 },{ 0, 0, 0 },{ 0, 0, 0 }}, |
|
2060 |
{{ 0, 0, 0 },{ 0, 0, 0 },{ 0, 8, 4 },{ 7,15, 0 }}, |
|
2061 |
}; |
|
2062 |
||
2063 |
static const uint32 _reachable_tracks[4] = { |
|
2064 |
0x10091009, |
|
2065 |
0x00160016, |
|
2066 |
0x05200520, |
|
2067 |
0x2A002A00, |
|
2068 |
}; |
|
2069 |
||
2070 |
static const byte _search_directions[6][4] = { |
|
2071 |
{ 0, 9, 2, 9 }, // track 1 |
|
2072 |
{ 9, 1, 9, 3 }, // track 2 |
|
2073 |
{ 9, 0, 3, 9 }, // track upper |
|
2074 |
{ 1, 9, 9, 2 }, // track lower |
|
2075 |
{ 3, 2, 9, 9 }, // track left |
|
2076 |
{ 9, 9, 1, 0 }, // track right |
|
2077 |
}; |
|
2078 |
||
2079 |
static const byte _pick_track_table[6] = {1, 3, 2, 2, 0, 0}; |
|
2758
9cd8a21e9a7b
(svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents:
2752
diff
changeset
|
2080 |
#ifdef PF_BENCHMARK |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2081 |
#if !defined(_MSC_VER) |
2482
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2477
diff
changeset
|
2082 |
unsigned int _rdtsc() |
1247 | 2083 |
{ |
2639 | 2084 |
unsigned int high, low; |
2085 |
||
2086 |
__asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high)); |
|
2087 |
return low; |
|
1247 | 2088 |
} |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2089 |
#else |
2482
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2477
diff
changeset
|
2090 |
#ifndef _M_AMD64 |
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2477
diff
changeset
|
2091 |
static unsigned int _declspec(naked) _rdtsc(void) |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2092 |
{ |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2093 |
_asm { |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2094 |
rdtsc |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2095 |
ret |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2096 |
} |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2097 |
} |
1247 | 2098 |
#endif |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2099 |
#endif |
2482
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2477
diff
changeset
|
2100 |
#endif |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2101 |
|
1247 | 2102 |
|
0 | 2103 |
|
2104 |
/* choose a track */ |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1969
diff
changeset
|
2105 |
static byte ChooseTrainTrack(Vehicle *v, TileIndex tile, int enterdir, TrackdirBits trackdirbits) |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2106 |
{ |
0 | 2107 |
TrainTrackFollowerData fd; |
2108 |
uint best_track; |
|
2758
9cd8a21e9a7b
(svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents:
2752
diff
changeset
|
2109 |
#ifdef PF_BENCHMARK |
2482
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2477
diff
changeset
|
2110 |
int time = _rdtsc(); |
0 | 2111 |
static float f; |
2112 |
#endif |
|
2113 |
||
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
2114 |
assert( (trackdirbits & ~0x3F) == 0); |
0 | 2115 |
|
1247 | 2116 |
/* quick return in case only one possible track is available */ |
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
2117 |
if (KILL_FIRST_BIT(trackdirbits) == 0) |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
2118 |
return FIND_FIRST_BIT(trackdirbits); |
0 | 2119 |
|
1247 | 2120 |
if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */ |
2121 |
NPFFindStationOrTileData fstd; |
|
2122 |
NPFFoundTargetData ftd; |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
2123 |
Trackdir trackdir; |
1247 | 2124 |
|
2125 |
NPFFillWithOrderData(&fstd, v); |
|
2126 |
/* The enterdir for the new tile, is the exitdir for the old tile */ |
|
1752
cdbfb2f23e72
(svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents:
1698
diff
changeset
|
2127 |
trackdir = GetVehicleTrackdir(v); |
1247 | 2128 |
assert(trackdir != 0xff); |
2129 |
||
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2883
diff
changeset
|
2130 |
ftd = NPFRouteToStationOrTile(tile - TileOffsByDir(enterdir), trackdir, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.railtype); |
1698
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2131 |
|
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2132 |
if (ftd.best_trackdir == 0xff) { |
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2133 |
/* We are already at our target. Just do something */ |
1247 | 2134 |
//TODO: maybe display error? |
2135 |
//TODO: go straight ahead if possible? |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
2136 |
best_track = FIND_FIRST_BIT(trackdirbits); |
0 | 2137 |
} else { |
1698
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2138 |
/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains |
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2139 |
the direction we need to take to get there, if ftd.best_bird_dist is not 0, |
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2140 |
we did not find our target, but ftd.best_trackdir contains the direction leading |
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2141 |
to the tile closest to our target. */ |
1247 | 2142 |
/* Discard enterdir information, making it a normal track */ |
2008
c9d6585c96c8
(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents:
2006
diff
changeset
|
2143 |
best_track = TrackdirToTrack(ftd.best_trackdir); |
0 | 2144 |
} |
2145 |
} else { |
|
1247 | 2146 |
FillWithStationData(&fd, v); |
2147 |
||
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2148 |
/* New train pathfinding */ |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2149 |
fd.best_bird_dist = (uint)-1; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2150 |
fd.best_track_dist = (uint)-1; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2151 |
fd.best_track = 0xFF; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2152 |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2153 |
NewTrainPathfind(tile - TileOffsByDir(enterdir), v->dest_tile, |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2154 |
enterdir, (NTPEnumProc*)NtpCallbFindStation, &fd); |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2155 |
|
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2156 |
if (fd.best_track == 0xff) { |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2157 |
// blaha |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2158 |
best_track = FIND_FIRST_BIT(trackdirbits); |
1247 | 2159 |
} else { |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2160 |
best_track = fd.best_track & 7; |
1247 | 2161 |
} |
0 | 2162 |
} |
2163 |
||
2758
9cd8a21e9a7b
(svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents:
2752
diff
changeset
|
2164 |
#ifdef PF_BENCHMARK |
2482
dffcca243dbc
(svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents:
2477
diff
changeset
|
2165 |
time = _rdtsc() - time; |
0 | 2166 |
f = f * 0.99 + 0.01 * time; |
2167 |
printf("PF time = %d %f\n", time, f); |
|
2168 |
#endif |
|
2169 |
||
2170 |
return best_track; |
|
2171 |
} |
|
2172 |
||
2173 |
||
2174 |
static bool CheckReverseTrain(Vehicle *v) |
|
2175 |
{ |
|
2176 |
TrainTrackFollowerData fd; |
|
2177 |
int i, r; |
|
2178 |
int best_track; |
|
2179 |
uint best_bird_dist = 0; |
|
2180 |
uint best_track_dist = 0; |
|
2181 |
uint reverse, reverse_best; |
|
2182 |
||
2183 |
if (_opt.diff.line_reverse_mode != 0 || |
|
2184 |
v->u.rail.track & 0xC0 || |
|
2185 |
!(v->direction & 1)) |
|
2186 |
return false; |
|
2187 |
||
2188 |
FillWithStationData(&fd, v); |
|
2189 |
||
2190 |
best_track = -1; |
|
2191 |
reverse_best = reverse = 0; |
|
2192 |
||
2193 |
assert(v->u.rail.track); |
|
2194 |
||
2195 |
i = _search_directions[FIND_FIRST_BIT(v->u.rail.track)][v->direction>>1]; |
|
2196 |
||
1247 | 2197 |
if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */ |
2198 |
NPFFindStationOrTileData fstd; |
|
2199 |
NPFFoundTargetData ftd; |
|
2200 |
byte trackdir, trackdir_rev; |
|
2201 |
Vehicle* last = GetLastVehicleInChain(v); |
|
2202 |
||
2203 |
NPFFillWithOrderData(&fstd, v); |
|
2204 |
||
1752
cdbfb2f23e72
(svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents:
1698
diff
changeset
|
2205 |
trackdir = GetVehicleTrackdir(v); |
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
2206 |
trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last)); |
1247 | 2207 |
assert(trackdir != 0xff); |
2208 |
assert(trackdir_rev != 0xff); |
|
2209 |
||
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2883
diff
changeset
|
2210 |
ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, last->tile, trackdir_rev, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.railtype); |
1247 | 2211 |
if (ftd.best_bird_dist != 0) { |
2212 |
/* We didn't find anything, just keep on going straight ahead */ |
|
2213 |
reverse_best = false; |
|
2214 |
} else { |
|
1461
b1505c82ebae
(svn r1965) - Fix: [NPF] Forgot to update one line where NPFGetFlag() should have been used two commits ago.
matthijs
parents:
1459
diff
changeset
|
2215 |
if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) |
1247 | 2216 |
reverse_best = true; |
2217 |
else |
|
2218 |
reverse_best = false; |
|
2219 |
} |
|
2220 |
} else { |
|
2221 |
while(true) { |
|
2222 |
fd.best_bird_dist = (uint)-1; |
|
2223 |
fd.best_track_dist = (uint)-1; |
|
2224 |
||
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2225 |
NewTrainPathfind(v->tile, v->dest_tile, reverse ^ i, (NTPEnumProc*)NtpCallbFindStation, &fd); |
1247 | 2226 |
|
2227 |
if (best_track != -1) { |
|
2228 |
if (best_bird_dist != 0) { |
|
2229 |
if (fd.best_bird_dist != 0) { |
|
2230 |
/* neither reached the destination, pick the one with the smallest bird dist */ |
|
2231 |
if (fd.best_bird_dist > best_bird_dist) goto bad; |
|
2232 |
if (fd.best_bird_dist < best_bird_dist) goto good; |
|
2233 |
} else { |
|
2234 |
/* we found the destination for the first time */ |
|
2235 |
goto good; |
|
2236 |
} |
|
0 | 2237 |
} else { |
1247 | 2238 |
if (fd.best_bird_dist != 0) { |
2239 |
/* didn't find destination, but we've found the destination previously */ |
|
2240 |
goto bad; |
|
2241 |
} else { |
|
2242 |
/* both old & new reached the destination, compare track length */ |
|
2243 |
if (fd.best_track_dist > best_track_dist) goto bad; |
|
2244 |
if (fd.best_track_dist < best_track_dist) goto good; |
|
2245 |
} |
|
0 | 2246 |
} |
1247 | 2247 |
|
2248 |
/* if we reach this position, there's two paths of equal value so far. |
|
2249 |
* pick one randomly. */ |
|
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
2250 |
r = GB(Random(), 0, 8); |
1247 | 2251 |
if (_pick_track_table[i] == (v->direction & 3)) r += 80; |
2252 |
if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80; |
|
2253 |
if (r <= 127) goto bad; |
|
0 | 2254 |
} |
1247 | 2255 |
good:; |
2256 |
best_track = i; |
|
2257 |
best_bird_dist = fd.best_bird_dist; |
|
2258 |
best_track_dist = fd.best_track_dist; |
|
2259 |
reverse_best = reverse; |
|
2260 |
bad:; |
|
2261 |
if (reverse != 0) |
|
2262 |
break; |
|
2263 |
reverse = 2; |
|
0 | 2264 |
} |
2265 |
} |
|
2266 |
||
2267 |
return reverse_best != 0; |
|
2268 |
} |
|
2269 |
||
2270 |
static bool ProcessTrainOrder(Vehicle *v) |
|
2271 |
{ |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2272 |
const Order *order; |
0 | 2273 |
bool result; |
2274 |
||
2275 |
// These are un-interruptible |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2276 |
if (v->current_order.type >= OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2277 |
v->current_order.type <= OT_LEAVESTATION) { |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2278 |
// Let a depot order in the orderlist interrupt. |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2279 |
if (v->current_order.type != OT_GOTO_DEPOT || |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2280 |
!(v->current_order.flags & OF_UNLOAD)) |
0 | 2281 |
return false; |
2282 |
} |
|
2283 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2284 |
if (v->current_order.type == OT_GOTO_DEPOT && |
2214
75d037f8dcc4
(svn r2732) -Codechange: Cleaned the order flags some more. Hit some inconsistencies while trying to locate bug 1244167
celestar
parents:
2186
diff
changeset
|
2285 |
(v->current_order.flags & (OF_PART_OF_ORDERS | OF_SERVICE_IF_NEEDED)) == (OF_PART_OF_ORDERS | OF_SERVICE_IF_NEEDED) && |
1520
c85dc79795e0
(svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents:
1507
diff
changeset
|
2286 |
!VehicleNeedsService(v)) { |
0 | 2287 |
v->cur_order_index++; |
2288 |
} |
|
2289 |
||
395
4c990f33dab7
(svn r587) -newgrf: Rename all /Checkpoint/i tokens to 'Waypoint's. The name actually makes some sense and is also compatible with TTDPatch (pasky).
darkvater
parents:
358
diff
changeset
|
2290 |
// check if we've reached the waypoint? |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2291 |
if (v->current_order.type == OT_GOTO_WAYPOINT && v->tile == v->dest_tile) { |
0 | 2292 |
v->cur_order_index++; |
2293 |
} |
|
2294 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2295 |
// check if we've reached a non-stop station while TTDPatch nonstop is enabled.. |
2549 | 2296 |
if (_patches.new_nonstop && |
2297 |
v->current_order.flags & OF_NON_STOP && |
|
2298 |
IsTileType(v->tile, MP_STATION) && |
|
2299 |
v->current_order.station == _m[v->tile].m2) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2300 |
v->cur_order_index++; |
0 | 2301 |
} |
2302 |
||
2303 |
// Get the current order |
|
2639 | 2304 |
if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0; |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2305 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2306 |
order = GetVehicleOrder(v, v->cur_order_index); |
0 | 2307 |
|
2308 |
// If no order, do nothing. |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2309 |
if (order == NULL) { |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2310 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2311 |
v->current_order.flags = 0; |
0 | 2312 |
v->dest_tile = 0; |
2313 |
return false; |
|
2314 |
} |
|
2315 |
||
2316 |
// If it is unchanged, keep it. |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2317 |
if (order->type == v->current_order.type && |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2318 |
order->flags == v->current_order.flags && |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2319 |
order->station == v->current_order.station) |
0 | 2320 |
return false; |
2321 |
||
2322 |
// Otherwise set it, and determine the destination tile. |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2323 |
v->current_order = *order; |
0 | 2324 |
|
2325 |
v->dest_tile = 0; |
|
2326 |
||
2327 |
result = false; |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2328 |
switch (order->type) { |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2329 |
case OT_GOTO_STATION: |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2330 |
if (order->station == v->last_station_visited) |
1266
9dc5638fe8cc
(svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents:
1247
diff
changeset
|
2331 |
v->last_station_visited = INVALID_STATION; |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2332 |
v->dest_tile = GetStation(order->station)->xy; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2333 |
result = CheckReverseTrain(v); |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2334 |
break; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2335 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2336 |
case OT_GOTO_DEPOT: |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
2337 |
v->dest_tile = GetDepot(order->station)->xy; |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2338 |
result = CheckReverseTrain(v); |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2339 |
break; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2340 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2341 |
case OT_GOTO_WAYPOINT: |
1542
2ca6d1624e6d
(svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
1531
diff
changeset
|
2342 |
v->dest_tile = GetWaypoint(order->station)->xy; |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2343 |
result = CheckReverseTrain(v); |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2344 |
break; |
0 | 2345 |
} |
2346 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2347 |
InvalidateVehicleOrder(v); |
0 | 2348 |
|
2349 |
return result; |
|
2350 |
} |
|
2351 |
||
2352 |
static void MarkTrainDirty(Vehicle *v) |
|
2353 |
{ |
|
2354 |
do { |
|
2355 |
v->cur_image = GetTrainImage(v, v->direction); |
|
2356 |
MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1); |
|
2639 | 2357 |
} while ((v = v->next) != NULL); |
0 | 2358 |
} |
2359 |
||
2360 |
static void HandleTrainLoading(Vehicle *v, bool mode) |
|
2361 |
{ |
|
2639 | 2362 |
if (v->current_order.type == OT_NOTHING) return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2363 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2364 |
if (v->current_order.type != OT_DUMMY) { |
2639 | 2365 |
if (v->current_order.type != OT_LOADING) return; |
2366 |
if (mode) return; |
|
0 | 2367 |
|
2368 |
// don't mark the train as lost if we're loading on the final station. |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2369 |
if (v->current_order.flags & OF_NON_STOP) |
0 | 2370 |
v->u.rail.days_since_order_progr = 0; |
2371 |
||
2639 | 2372 |
if (--v->load_unload_time_rem) return; |
0 | 2373 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2374 |
if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) { |
882
b6c4b9afa21b
(svn r1367) -Fix: Full-Loading trains no longer get "lost" after a while (Hackykid)
matthijs
parents:
842
diff
changeset
|
2375 |
v->u.rail.days_since_order_progr = 0; /* Prevent a train lost message for full loading trains */ |
0 | 2376 |
SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC); |
2377 |
if (LoadUnloadVehicle(v)) { |
|
2378 |
InvalidateWindow(WC_TRAINS_LIST, v->owner); |
|
2379 |
MarkTrainDirty(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2380 |
|
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
2381 |
// need to update acceleration and cached values since the goods on the train changed. |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
2382 |
TrainCargoChanged(v); |
0 | 2383 |
UpdateTrainAcceleration(v); |
2384 |
} |
|
2385 |
return; |
|
2386 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2387 |
|
0 | 2388 |
TrainPlayLeaveStationSound(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2389 |
|
0 | 2390 |
{ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2391 |
Order b = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2392 |
v->current_order.type = OT_LEAVESTATION; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2393 |
v->current_order.flags = 0; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2394 |
|
0 | 2395 |
// If this was not the final order, don't remove it from the list. |
2639 | 2396 |
if (!(b.flags & OF_NON_STOP)) return; |
0 | 2397 |
} |
2398 |
} |
|
2399 |
||
2400 |
v->u.rail.days_since_order_progr = 0; |
|
2401 |
v->cur_order_index++; |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2402 |
InvalidateVehicleOrder(v); |
0 | 2403 |
} |
2404 |
||
2405 |
static int UpdateTrainSpeed(Vehicle *v) |
|
2406 |
{ |
|
2407 |
uint spd; |
|
2408 |
uint accel; |
|
2409 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2410 |
if (v->vehstatus & VS_STOPPED || HASBIT(v->u.rail.flags, VRF_REVERSING)) { |
2639 | 2411 |
if (_patches.realistic_acceleration) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2412 |
accel = GetTrainAcceleration(v, AM_BRAKE) * 2; |
2639 | 2413 |
} else { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2414 |
accel = v->acceleration * -2; |
2639 | 2415 |
} |
0 | 2416 |
} else { |
2639 | 2417 |
if (_patches.realistic_acceleration) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2418 |
accel = GetTrainAcceleration(v, AM_ACCEL); |
2639 | 2419 |
} else { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2420 |
accel = v->acceleration; |
2639 | 2421 |
} |
0 | 2422 |
} |
2423 |
||
2424 |
spd = v->subspeed + accel * 2; |
|
2425 |
v->subspeed = (byte)spd; |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2426 |
{ |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2427 |
int tempmax = v->max_speed; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2428 |
if (v->cur_speed > v->max_speed) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2429 |
tempmax = v->cur_speed - (v->cur_speed / 10) - 1; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2430 |
v->cur_speed = spd = clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2431 |
} |
0 | 2432 |
|
2433 |
if (!(v->direction & 1)) spd = spd * 3 >> 2; |
|
2434 |
||
2435 |
spd += v->progress; |
|
2436 |
v->progress = (byte)spd; |
|
2437 |
return (spd >> 8); |
|
2438 |
} |
|
2439 |
||
1551
eb05e6a90e67
(svn r2055) -CodeChange: Begun introducting StationID
celestar
parents:
1542
diff
changeset
|
2440 |
static void TrainEnterStation(Vehicle *v, StationID station) |
0 | 2441 |
{ |
2442 |
Station *st; |
|
2443 |
uint32 flags; |
|
2444 |
||
2445 |
v->last_station_visited = station; |
|
2446 |
||
2447 |
/* check if a train ever visited this station before */ |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
2448 |
st = GetStation(station); |
0 | 2449 |
if (!(st->had_vehicle_of_type & HVOT_TRAIN)) { |
2450 |
st->had_vehicle_of_type |= HVOT_TRAIN; |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
2451 |
SetDParam(0, st->index); |
0 | 2452 |
flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0); |
2453 |
AddNewsItem( |
|
2454 |
STR_8801_CITIZENS_CELEBRATE_FIRST, |
|
2455 |
flags, |
|
2456 |
v->index, |
|
2457 |
0); |
|
2458 |
} |
|
2459 |
||
2460 |
// Did we reach the final destination? |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2461 |
if (v->current_order.type == OT_GOTO_STATION && |
1552
c2ec4a790700
(svn r2056) -Fix: [ 1166999 ] Strange cast removed. Thanks Tron
celestar
parents:
1551
diff
changeset
|
2462 |
v->current_order.station == station) { |
0 | 2463 |
// Yeah, keep the load/unload flags |
2464 |
// Non Stop now means if the order should be increased. |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2465 |
v->current_order.type = OT_LOADING; |
1935
f43f062c9498
(svn r2441) -Feature: You can now give transfer order to set up feeder systems
celestar
parents:
1934
diff
changeset
|
2466 |
v->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD | OF_TRANSFER; |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2467 |
v->current_order.flags |= OF_NON_STOP; |
0 | 2468 |
} else { |
2469 |
// No, just do a simple load |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2470 |
v->current_order.type = OT_LOADING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2471 |
v->current_order.flags = 0; |
0 | 2472 |
} |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2473 |
v->current_order.station = 0; |
0 | 2474 |
|
2475 |
SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC); |
|
2476 |
if (LoadUnloadVehicle(v) != 0) { |
|
2477 |
InvalidateWindow(WC_TRAINS_LIST, v->owner); |
|
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
2478 |
TrainCargoChanged(v); |
0 | 2479 |
UpdateTrainAcceleration(v); |
2480 |
} |
|
2830
873da5589c15
(svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents:
2826
diff
changeset
|
2481 |
MarkTrainDirty(v); |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
2482 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 2483 |
} |
2484 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2485 |
static byte AfterSetTrainPos(Vehicle *v, bool new_tile) |
0 | 2486 |
{ |
2487 |
byte new_z, old_z; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2488 |
|
0 | 2489 |
// need this hint so it returns the right z coordinate on bridges. |
2490 |
_get_z_hint = v->z_pos; |
|
2491 |
new_z = GetSlopeZ(v->x_pos, v->y_pos); |
|
2492 |
_get_z_hint = 0; |
|
2493 |
||
2494 |
old_z = v->z_pos; |
|
2495 |
v->z_pos = new_z; |
|
2496 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2497 |
if (new_tile) { |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2498 |
CLRBIT(v->u.rail.flags, VRF_GOINGUP); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2499 |
CLRBIT(v->u.rail.flags, VRF_GOINGDOWN); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2500 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2501 |
if (new_z != old_z) { |
1980
9ea0c89fbb58
(svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents:
1977
diff
changeset
|
2502 |
TileIndex tile = TileVirtXY(v->x_pos, v->y_pos); |
1683
2248e6615517
(svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents:
1681
diff
changeset
|
2503 |
|
2248e6615517
(svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents:
1681
diff
changeset
|
2504 |
// XXX workaround, whole UP/DOWN detection needs overhaul |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2044
diff
changeset
|
2505 |
if (!IsTileType(tile, MP_TUNNELBRIDGE) || (_m[tile].m5 & 0x80) != 0) |
1683
2248e6615517
(svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents:
1681
diff
changeset
|
2506 |
SETBIT(v->u.rail.flags, (new_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN); |
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2507 |
} |
0 | 2508 |
} |
2509 |
||
2510 |
VehiclePositionChanged(v); |
|
2511 |
EndVehicleMove(v); |
|
2512 |
return old_z; |
|
2513 |
} |
|
2514 |
||
2515 |
static const byte _new_vehicle_direction_table[11] = { |
|
2516 |
0, 7, 6, 0, |
|
2517 |
1, 0, 5, 0, |
|
2518 |
2, 3, 4, |
|
2519 |
}; |
|
2520 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1969
diff
changeset
|
2521 |
static int GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile) |
0 | 2522 |
{ |
926
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
2523 |
uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 + |
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
2524 |
TileX(new_tile) - TileX(old_tile) + 1; |
0 | 2525 |
assert(offs < 11); |
2526 |
return _new_vehicle_direction_table[offs]; |
|
2527 |
} |
|
2528 |
||
2436
177cb6a8339f
(svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents:
2425
diff
changeset
|
2529 |
static int GetNewVehicleDirection(const Vehicle *v, int x, int y) |
0 | 2530 |
{ |
2531 |
uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1); |
|
2532 |
assert(offs < 11); |
|
2533 |
return _new_vehicle_direction_table[offs]; |
|
2534 |
} |
|
2535 |
||
2436
177cb6a8339f
(svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents:
2425
diff
changeset
|
2536 |
static int GetDirectionToVehicle(const Vehicle *v, int x, int y) |
0 | 2537 |
{ |
2538 |
byte offs; |
|
2539 |
||
2540 |
x -= v->x_pos; |
|
2541 |
if (x >= 0) { |
|
2542 |
offs = (x > 2) ? 0 : 1; |
|
2543 |
} else { |
|
2544 |
offs = (x < -2) ? 2 : 1; |
|
2545 |
} |
|
2546 |
||
2547 |
y -= v->y_pos; |
|
2548 |
if (y >= 0) { |
|
2549 |
offs += ((y > 2) ? 0 : 1) * 4; |
|
2550 |
} else { |
|
2551 |
offs += ((y < -2) ? 2 : 1) * 4; |
|
2552 |
} |
|
2553 |
||
2554 |
assert(offs < 11); |
|
2555 |
return _new_vehicle_direction_table[offs]; |
|
2556 |
} |
|
2557 |
||
2558 |
/* Check if the vehicle is compatible with the specified tile */ |
|
1048 | 2559 |
static bool CheckCompatibleRail(const Vehicle *v, TileIndex tile) |
0 | 2560 |
{ |
1214
33e07bbb7779
(svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents:
1209
diff
changeset
|
2561 |
switch (GetTileType(tile)) { |
1048 | 2562 |
case MP_RAILWAY: |
2563 |
case MP_STATION: |
|
2564 |
// normal tracks, jump to owner check |
|
2565 |
break; |
|
2566 |
||
2567 |
case MP_TUNNELBRIDGE: |
|
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2044
diff
changeset
|
2568 |
if ((_m[tile].m5 & 0xC0) == 0xC0) { // is bridge middle part? |
1192
cd9b8a18a183
(svn r1696) Use GetTileSlope() instead of FindLandscapeHeightByTile() where it is sufficient. FindLandscapeHeightByTile() uses GetTileSlope() internally and adds some more info, which is discarded in these cases.
tron
parents:
1179
diff
changeset
|
2569 |
uint height; |
cd9b8a18a183
(svn r1696) Use GetTileSlope() instead of FindLandscapeHeightByTile() where it is sufficient. FindLandscapeHeightByTile() uses GetTileSlope() internally and adds some more info, which is discarded in these cases.
tron
parents:
1179
diff
changeset
|
2570 |
uint tileh = GetTileSlope(tile, &height); |
1048 | 2571 |
|
2572 |
// correct Z position of a train going under a bridge on slopes |
|
1394
449e84bdc04e
(svn r1898) Remove some unused macros from macros.h and move some others to more appropriate headers
tron
parents:
1387
diff
changeset
|
2573 |
if (CorrectZ(tileh)) height += 8; |
1192
cd9b8a18a183
(svn r1696) Use GetTileSlope() instead of FindLandscapeHeightByTile() where it is sufficient. FindLandscapeHeightByTile() uses GetTileSlope() internally and adds some more info, which is discarded in these cases.
tron
parents:
1179
diff
changeset
|
2574 |
|
cd9b8a18a183
(svn r1696) Use GetTileSlope() instead of FindLandscapeHeightByTile() where it is sufficient. FindLandscapeHeightByTile() uses GetTileSlope() internally and adds some more info, which is discarded in these cases.
tron
parents:
1179
diff
changeset
|
2575 |
if (v->z_pos != height) return true; // train is going over bridge |
1048 | 2576 |
} |
2577 |
break; |
|
2578 |
||
2579 |
case MP_STREET: |
|
2360
09e42e4ee139
(svn r2886) Rename the "owner" attribute to "m1", because when it stores an owner it is accessed by [GS]etOwner anyway and when it doesn't store an owner, but arbitrary data, accessing a field called "owner" is confusing.
tron
parents:
2261
diff
changeset
|
2580 |
// tracks over roads, do owner check of tracks |
1048 | 2581 |
return |
2510
8a959a130698
(svn r3036) Use IsCompatibleRail() to check the rail type of level crossings (not a fix, because the old == method works so far)
tron
parents:
2493
diff
changeset
|
2582 |
IsTileOwner(tile, v->owner) && ( |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
2583 |
!IsFrontEngine(v) || |
2510
8a959a130698
(svn r3036) Use IsCompatibleRail() to check the rail type of level crossings (not a fix, because the old == method works so far)
tron
parents:
2493
diff
changeset
|
2584 |
IsCompatibleRail(v->u.rail.railtype, GB(_m[tile].m4, 0, 4)) |
8a959a130698
(svn r3036) Use IsCompatibleRail() to check the rail type of level crossings (not a fix, because the old == method works so far)
tron
parents:
2493
diff
changeset
|
2585 |
); |
1048 | 2586 |
|
2587 |
default: |
|
2588 |
return true; |
|
2589 |
} |
|
2590 |
||
2591 |
return |
|
2549 | 2592 |
IsTileOwner(tile, v->owner) && ( |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
2593 |
!IsFrontEngine(v) || |
2549 | 2594 |
IsCompatibleRail(v->u.rail.railtype, GetRailType(tile)) |
2595 |
); |
|
0 | 2596 |
} |
2597 |
||
2598 |
typedef struct { |
|
2599 |
byte small_turn, large_turn; |
|
2600 |
byte z_up; // fraction to remove when moving up |
|
2601 |
byte z_down; // fraction to remove when moving down |
|
2602 |
} RailtypeSlowdownParams; |
|
2603 |
||
2604 |
static const RailtypeSlowdownParams _railtype_slowdown[3] = { |
|
2605 |
// normal accel |
|
2606 |
{256/4, 256/2, 256/4, 2}, // normal |
|
2607 |
{256/4, 256/2, 256/4, 2}, // monorail |
|
2608 |
{0, 256/2, 256/4, 2}, // maglev |
|
2609 |
}; |
|
2610 |
||
2611 |
/* Modify the speed of the vehicle due to a turn */ |
|
2612 |
static void AffectSpeedByDirChange(Vehicle *v, byte new_dir) |
|
2613 |
{ |
|
2614 |
byte diff; |
|
2615 |
const RailtypeSlowdownParams *rsp; |
|
2616 |
||
2617 |
if (_patches.realistic_acceleration || (diff = (v->direction - new_dir) & 7) == 0) |
|
2618 |
return; |
|
2619 |
||
2620 |
rsp = &_railtype_slowdown[v->u.rail.railtype]; |
|
2621 |
v->cur_speed -= ((diff == 1 || diff == 7) ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8; |
|
2622 |
} |
|
2623 |
||
2624 |
/* Modify the speed of the vehicle due to a change in altitude */ |
|
2625 |
static void AffectSpeedByZChange(Vehicle *v, byte old_z) |
|
2626 |
{ |
|
2627 |
const RailtypeSlowdownParams *rsp; |
|
2639 | 2628 |
if (old_z == v->z_pos || _patches.realistic_acceleration) return; |
0 | 2629 |
|
2630 |
rsp = &_railtype_slowdown[v->u.rail.railtype]; |
|
2631 |
||
2632 |
if (old_z < v->z_pos) { |
|
2633 |
v->cur_speed -= (v->cur_speed * rsp->z_up >> 8); |
|
2634 |
} else { |
|
2635 |
uint16 spd = v->cur_speed + rsp->z_down; |
|
2639 | 2636 |
if (spd <= v->max_speed) v->cur_speed = spd; |
0 | 2637 |
} |
2638 |
} |
|
2639 |
||
2640 |
static const byte _otherside_signal_directions[14] = { |
|
2641 |
1, 3, 1, 3, 5, 3, 0, 0, |
|
2642 |
5, 7, 7, 5, 7, 1, |
|
2643 |
}; |
|
2644 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1969
diff
changeset
|
2645 |
static void TrainMovedChangeSignals(TileIndex tile, int dir) |
0 | 2646 |
{ |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2044
diff
changeset
|
2647 |
if (IsTileType(tile, MP_RAILWAY) && (_m[tile].m5 & 0xC0) == 0x40) { |
2639 | 2648 |
uint i = FindFirstBit2x64((_m[tile].m5 + (_m[tile].m5 << 8)) & _reachable_tracks[dir]); |
0 | 2649 |
UpdateSignalsOnSegment(tile, _otherside_signal_directions[i]); |
2650 |
} |
|
2651 |
} |
|
2652 |
||
2653 |
||
2654 |
typedef struct TrainCollideChecker { |
|
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2655 |
const Vehicle *v; |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2656 |
const Vehicle *v_skip; |
0 | 2657 |
} TrainCollideChecker; |
2658 |
||
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2659 |
static void *FindTrainCollideEnum(Vehicle *v, void *data) |
0 | 2660 |
{ |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2661 |
const TrainCollideChecker* tcc = data; |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2662 |
|
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2663 |
if (v != tcc->v && |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2664 |
v != tcc->v_skip && |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2665 |
v->type == VEH_Train && |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2666 |
v->u.rail.track != 0x80 && |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2667 |
myabs(v->z_pos - tcc->v->z_pos) <= 6 && |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2668 |
myabs(v->x_pos - tcc->v->x_pos) < 6 && |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2669 |
myabs(v->y_pos - tcc->v->y_pos) < 6) { |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2670 |
return v; |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2671 |
} else { |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2672 |
return NULL; |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2673 |
} |
0 | 2674 |
} |
2675 |
||
2676 |
static void SetVehicleCrashed(Vehicle *v) |
|
2677 |
{ |
|
2678 |
Vehicle *u; |
|
2679 |
||
2680 |
if (v->u.rail.crash_anim_pos != 0) |
|
2681 |
return; |
|
2682 |
||
2683 |
v->u.rail.crash_anim_pos++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2684 |
|
0 | 2685 |
u = v; |
2686 |
BEGIN_ENUM_WAGONS(v) |
|
2687 |
v->vehstatus |= VS_CRASHED; |
|
2688 |
END_ENUM_WAGONS(v) |
|
2689 |
||
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
2690 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, u->index, STATUS_BAR); |
0 | 2691 |
} |
2692 |
||
2549 | 2693 |
static uint CountPassengersInTrain(const Vehicle* v) |
0 | 2694 |
{ |
2549 | 2695 |
uint num = 0; |
0 | 2696 |
BEGIN_ENUM_WAGONS(v) |
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2697 |
if (v->cargo_type == CT_PASSENGERS) num += v->cargo_count; |
0 | 2698 |
END_ENUM_WAGONS(v) |
2699 |
return num; |
|
2700 |
} |
|
2701 |
||
22 | 2702 |
/* |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2703 |
* Checks whether the specified train has a collision with another vehicle. If |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
2704 |
* so, destroys this vehicle, and the other vehicle if its subtype has TS_Front. |
22 | 2705 |
* Reports the incident in a flashy news item, modifies station ratings and |
2706 |
* plays a sound. |
|
2707 |
*/ |
|
0 | 2708 |
static void CheckTrainCollision(Vehicle *v) |
2709 |
{ |
|
2710 |
TrainCollideChecker tcc; |
|
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2711 |
Vehicle *coll; |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2712 |
Vehicle *realcoll; |
2639 | 2713 |
uint num; |
0 | 2714 |
|
2715 |
/* can't collide in depot */ |
|
2639 | 2716 |
if (v->u.rail.track == 0x80) return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2717 |
|
1980
9ea0c89fbb58
(svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents:
1977
diff
changeset
|
2718 |
assert(v->u.rail.track == 0x40 || TileVirtXY(v->x_pos, v->y_pos) == v->tile); |
0 | 2719 |
|
2720 |
tcc.v = v; |
|
2721 |
tcc.v_skip = v->next; |
|
2722 |
||
2723 |
/* find colliding vehicle */ |
|
1980
9ea0c89fbb58
(svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents:
1977
diff
changeset
|
2724 |
realcoll = VehicleFromPos(TileVirtXY(v->x_pos, v->y_pos), &tcc, FindTrainCollideEnum); |
2639 | 2725 |
if (realcoll == NULL) return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2726 |
|
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2727 |
coll = GetFirstVehicleInChain(realcoll); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2728 |
|
0 | 2729 |
/* it can't collide with its own wagons */ |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2730 |
if (v == coll || |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2731 |
(v->u.rail.track & 0x40 && (v->direction & 2) != (realcoll->direction & 2))) |
0 | 2732 |
return; |
2733 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2734 |
//two drivers + passangers killed in train v |
0 | 2735 |
num = 2 + CountPassengersInTrain(v); |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2736 |
if (!(coll->vehstatus & VS_CRASHED)) |
0 | 2737 |
//two drivers + passangers killed in train coll (if it was not crashed already) |
2738 |
num += 2 + CountPassengersInTrain(coll); |
|
2739 |
||
2740 |
SetVehicleCrashed(v); |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
2741 |
if (IsFrontEngine(coll)) SetVehicleCrashed(coll); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2742 |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
2743 |
SetDParam(0, num); |
0 | 2744 |
AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL, |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2745 |
NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_VEHICLE, NT_ACCIDENT, 0), |
0 | 2746 |
v->index, |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2747 |
0 |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2748 |
); |
0 | 2749 |
|
2750 |
ModifyStationRatingAround(v->tile, v->owner, -160, 30); |
|
541 | 2751 |
SndPlayVehicleFx(SND_13_BIG_CRASH, v); |
0 | 2752 |
} |
2753 |
||
1432
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2754 |
typedef struct VehicleAtSignalData { |
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2755 |
TileIndex tile; |
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2756 |
byte direction; |
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2757 |
} VehicleAtSignalData; |
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2758 |
|
0 | 2759 |
static void *CheckVehicleAtSignal(Vehicle *v, void *data) |
2760 |
{ |
|
1432
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2761 |
const VehicleAtSignalData* vasd = data; |
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2762 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
2763 |
if (v->type == VEH_Train && IsFrontEngine(v) && |
1432
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2764 |
v->tile == vasd->tile) { |
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2765 |
byte diff = (v->direction - vasd->direction + 2) & 7; |
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2766 |
|
0 | 2767 |
if (diff == 2 || (v->cur_speed <= 5 && diff <= 4)) |
1432
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2768 |
return v; |
0 | 2769 |
} |
1432
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2770 |
return NULL; |
0 | 2771 |
} |
2772 |
||
2773 |
static void TrainController(Vehicle *v) |
|
2774 |
{ |
|
1961
10ce7350f76b
(svn r2467) - Fix: [newgrf] Prevent trains with shorter wagons breaking apart when reversing in some cases. (Therax)
hackykid
parents:
1942
diff
changeset
|
2775 |
Vehicle *prev; |
0 | 2776 |
GetNewVehiclePosResult gp; |
2777 |
uint32 r, tracks,ts; |
|
1247 | 2778 |
int i, enterdir, newdir, dir; |
0 | 2779 |
byte chosen_dir; |
2780 |
byte chosen_track; |
|
2781 |
byte old_z; |
|
2782 |
||
22 | 2783 |
/* For every vehicle after and including the given vehicle */ |
1961
10ce7350f76b
(svn r2467) - Fix: [newgrf] Prevent trains with shorter wagons breaking apart when reversing in some cases. (Therax)
hackykid
parents:
1942
diff
changeset
|
2784 |
for (prev = GetPrevVehicleInChain(v); v != NULL; prev = v, v = v->next) { |
0 | 2785 |
BeginVehicleMove(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2786 |
|
0 | 2787 |
if (v->u.rail.track != 0x40) { |
22 | 2788 |
/* Not inside tunnel */ |
0 | 2789 |
if (GetNewVehiclePos(v, &gp)) { |
22 | 2790 |
/* Staying in the old tile */ |
0 | 2791 |
if (v->u.rail.track == 0x80) { |
2792 |
/* inside depot */ |
|
2793 |
gp.x = v->x_pos; |
|
2794 |
gp.y = v->y_pos; |
|
2795 |
} else { |
|
22 | 2796 |
/* is not inside depot */ |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2797 |
|
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2883
diff
changeset
|
2798 |
if (!TrainCheckIfLineEnds(v)) |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2799 |
return; |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2800 |
|
0 | 2801 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
1247 | 2802 |
if (r & 0x8) { |
2803 |
//debug("%x & 0x8", r); |
|
0 | 2804 |
goto invalid_rail; |
1247 | 2805 |
} |
0 | 2806 |
if (r & 0x2) { |
2807 |
TrainEnterStation(v, r >> 8); |
|
2808 |
return; |
|
2809 |
} |
|
2810 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2811 |
if (v->current_order.type == OT_LEAVESTATION) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2812 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2813 |
v->current_order.flags = 0; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
2814 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 2815 |
} |
2816 |
} |
|
2817 |
} else { |
|
2818 |
/* A new tile is about to be entered. */ |
|
2819 |
||
1247 | 2820 |
byte bits; |
0 | 2821 |
/* Determine what direction we're entering the new tile from */ |
2822 |
dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile); |
|
1247 | 2823 |
enterdir = dir >> 1; |
2824 |
assert(enterdir==0 || enterdir==1 || enterdir==2 || enterdir==3); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2825 |
|
0 | 2826 |
/* Get the status of the tracks in the new tile and mask |
2827 |
* away the bits that aren't reachable. */ |
|
1247 | 2828 |
ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL) & _reachable_tracks[enterdir]; |
0 | 2829 |
|
2830 |
/* Combine the from & to directions. |
|
2831 |
* Now, the lower byte contains the track status, and the byte at bit 16 contains |
|
2832 |
* the signal status. */ |
|
2833 |
tracks = ts|(ts >> 8); |
|
1247 | 2834 |
bits = tracks & 0xFF; |
2835 |
if (_patches.new_pathfinding_all && _patches.forbid_90_deg && prev == NULL) |
|
2836 |
/* We allow wagons to make 90 deg turns, because forbid_90_deg |
|
2837 |
* can be switched on halfway a turn */ |
|
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
2838 |
bits &= ~TrackCrossesTracks(FIND_FIRST_BIT(v->u.rail.track)); |
1247 | 2839 |
|
2840 |
if ( bits == 0) { |
|
2841 |
//debug("%x == 0", bits); |
|
0 | 2842 |
goto invalid_rail; |
1247 | 2843 |
} |
0 | 2844 |
|
2845 |
/* Check if the new tile contrains tracks that are compatible |
|
2846 |
* with the current train, if not, bail out. */ |
|
1247 | 2847 |
if (!CheckCompatibleRail(v, gp.new_tile)) { |
2848 |
//debug("!CheckCompatibleRail(%p, %x)", v, gp.new_tile); |
|
0 | 2849 |
goto invalid_rail; |
1247 | 2850 |
} |
0 | 2851 |
|
2852 |
if (prev == NULL) { |
|
2853 |
/* Currently the locomotive is active. Determine which one of the |
|
2854 |
* available tracks to choose */ |
|
1247 | 2855 |
chosen_track = 1 << ChooseTrainTrack(v, gp.new_tile, enterdir, bits); |
2856 |
assert(chosen_track & tracks); |
|
0 | 2857 |
|
2858 |
/* Check if it's a red signal and that force proceed is not clicked. */ |
|
2859 |
if ( (tracks>>16)&chosen_track && v->u.rail.force_proceed == 0) goto red_light; |
|
2860 |
} else { |
|
2861 |
static byte _matching_tracks[8] = {0x30, 1, 0xC, 2, 0x30, 1, 0xC, 2}; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2862 |
|
0 | 2863 |
/* The wagon is active, simply follow the prev vehicle. */ |
1247 | 2864 |
chosen_track = (byte)(_matching_tracks[GetDirectionToVehicle(prev, gp.x, gp.y)] & bits); |
0 | 2865 |
} |
2866 |
||
2867 |
/* make sure chosen track is a valid track */ |
|
2868 |
assert(chosen_track==1 || chosen_track==2 || chosen_track==4 || chosen_track==8 || chosen_track==16 || chosen_track==32); |
|
2869 |
||
2870 |
/* Update XY to reflect the entrance to the new tile, and select the direction to use */ |
|
2871 |
{ |
|
1247 | 2872 |
const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir]; |
0 | 2873 |
gp.x = (gp.x & ~0xF) | b[0]; |
2874 |
gp.y = (gp.y & ~0xF) | b[1]; |
|
2875 |
chosen_dir = b[2]; |
|
2876 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2877 |
|
0 | 2878 |
/* Call the landscape function and tell it that the vehicle entered the tile */ |
2879 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
1247 | 2880 |
if (r&0x8){ |
2881 |
//debug("%x & 0x8", r); |
|
0 | 2882 |
goto invalid_rail; |
1247 | 2883 |
} |
0 | 2884 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
2885 |
if (IsFrontEngine(v)) v->load_unload_time_rem = 0; |
0 | 2886 |
|
2887 |
if (!(r&0x4)) { |
|
2888 |
v->tile = gp.new_tile; |
|
2889 |
v->u.rail.track = chosen_track; |
|
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
2890 |
assert(v->u.rail.track); |
0 | 2891 |
} |
2892 |
||
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
2893 |
if (IsFrontEngine(v)) |
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2883
diff
changeset
|
2894 |
TrainMovedChangeSignals(gp.new_tile, enterdir); |
0 | 2895 |
|
22 | 2896 |
/* Signals can only change when the first |
2897 |
* (above) or the last vehicle moves. */ |
|
0 | 2898 |
if (v->next == NULL) |
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2883
diff
changeset
|
2899 |
TrainMovedChangeSignals(gp.old_tile, (enterdir) ^ 2); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2900 |
|
0 | 2901 |
if (prev == NULL) { |
2902 |
AffectSpeedByDirChange(v, chosen_dir); |
|
2903 |
} |
|
2904 |
||
2905 |
v->direction = chosen_dir; |
|
2906 |
} |
|
2907 |
} else { |
|
2908 |
/* in tunnel */ |
|
2909 |
GetNewVehiclePos(v, &gp); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2910 |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2911 |
// Check if to exit the tunnel... |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2912 |
if (!IsTunnelTile(gp.new_tile) || |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2913 |
!(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y)&0x4) ) { |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2914 |
v->x_pos = gp.x; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2915 |
v->y_pos = gp.y; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2916 |
VehiclePositionChanged(v); |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2917 |
continue; |
0 | 2918 |
} |
2919 |
} |
|
2920 |
||
2921 |
/* update image of train, as well as delta XY */ |
|
1247 | 2922 |
newdir = GetNewVehicleDirection(v, gp.x, gp.y); |
2923 |
UpdateTrainDeltaXY(v, newdir); |
|
2924 |
v->cur_image = GetTrainImage(v, newdir); |
|
0 | 2925 |
|
2926 |
v->x_pos = gp.x; |
|
2927 |
v->y_pos = gp.y; |
|
2928 |
||
2929 |
/* update the Z position of the vehicle */ |
|
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2930 |
old_z = AfterSetTrainPos(v, (gp.new_tile != gp.old_tile)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2931 |
|
0 | 2932 |
if (prev == NULL) { |
22 | 2933 |
/* This is the first vehicle in the train */ |
0 | 2934 |
AffectSpeedByZChange(v, old_z); |
2935 |
} |
|
2936 |
} |
|
1438 | 2937 |
return; |
0 | 2938 |
|
2939 |
invalid_rail: |
|
22 | 2940 |
/* We've reached end of line?? */ |
2639 | 2941 |
if (prev != NULL) error("!Disconnecting train"); |
0 | 2942 |
goto reverse_train_direction; |
2943 |
||
2944 |
red_light: { |
|
22 | 2945 |
/* We're in front of a red signal ?? */ |
0 | 2946 |
/* find the first set bit in ts. need to do it in 2 steps, since |
2947 |
* FIND_FIRST_BIT only handles 6 bits at a time. */ |
|
2948 |
i = FindFirstBit2x64(ts); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2949 |
|
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2044
diff
changeset
|
2950 |
if (!(_m[gp.new_tile].m3 & SignalAgainstTrackdir(i))) { |
0 | 2951 |
v->cur_speed = 0; |
2952 |
v->subspeed = 0; |
|
2953 |
v->progress = 255-100; |
|
2954 |
if (++v->load_unload_time_rem < _patches.wait_oneway_signal * 20) |
|
2955 |
return; |
|
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2044
diff
changeset
|
2956 |
} else if (_m[gp.new_tile].m3 & SignalAlongTrackdir(i)){ |
0 | 2957 |
v->cur_speed = 0; |
2958 |
v->subspeed = 0; |
|
2959 |
v->progress = 255-10; |
|
2960 |
if (++v->load_unload_time_rem < _patches.wait_twoway_signal * 73) { |
|
1432
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2961 |
TileIndex o_tile = gp.new_tile + TileOffsByDir(enterdir); |
1507
2d09b13f049f
(svn r2011) - Fix: [ 1162209 ] Fix OS/2 build (orudge) #2
darkvater
parents:
1475
diff
changeset
|
2962 |
VehicleAtSignalData vasd; |
2d09b13f049f
(svn r2011) - Fix: [ 1162209 ] Fix OS/2 build (orudge) #2
darkvater
parents:
1475
diff
changeset
|
2963 |
vasd.tile = o_tile; |
2d09b13f049f
(svn r2011) - Fix: [ 1162209 ] Fix OS/2 build (orudge) #2
darkvater
parents:
1475
diff
changeset
|
2964 |
vasd.direction = dir ^ 4; |
1432
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2965 |
|
0 | 2966 |
/* check if a train is waiting on the other side */ |
1432
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2967 |
if (VehicleFromPos(o_tile, &vasd, CheckVehicleAtSignal) == NULL) |
0 | 2968 |
return; |
2969 |
} |
|
2970 |
} |
|
2971 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2972 |
|
0 | 2973 |
reverse_train_direction: |
2974 |
v->load_unload_time_rem = 0; |
|
2975 |
v->cur_speed = 0; |
|
2976 |
v->subspeed = 0; |
|
2977 |
ReverseTrainDirection(v); |
|
2978 |
} |
|
2979 |
||
1430
68847f67a412
(svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents:
1418
diff
changeset
|
2980 |
extern TileIndex CheckTunnelBusy(TileIndex tile, uint *length); |
98 | 2981 |
|
1418
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
2982 |
/** |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
2983 |
* Deletes/Clears the last wagon of a crashed train. It takes the engine of the |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
2984 |
* train, then goes to the last wagon and deletes that. Each call to this function |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
2985 |
* will remove the last wagon of a crashed train. If this wagon was on a crossing, |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
2986 |
* or inside a tunnel, recalculate the signals as they might need updating |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
2987 |
* @param v the @Vehicle of which last wagon is to be removed |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
2988 |
*/ |
0 | 2989 |
static void DeleteLastWagon(Vehicle *v) |
2990 |
{ |
|
2991 |
Vehicle *u = v; |
|
1418
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
2992 |
|
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
2993 |
/* Go to the last wagon and delete the link pointing there |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
2994 |
* *u is then the one-before-last wagon, and *v the last |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
2995 |
* one which will physicially be removed */ |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2996 |
for (; v->next != NULL; v = v->next) u = v; |
0 | 2997 |
u->next = NULL; |
2998 |
||
2999 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
3000 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
|
588 | 3001 |
RebuildVehicleLists(); |
0 | 3002 |
InvalidateWindow(WC_COMPANY, v->owner); |
3003 |
||
3004 |
BeginVehicleMove(v); |
|
3005 |
EndVehicleMove(v); |
|
3006 |
DeleteVehicle(v); |
|
3007 |
||
1418
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3008 |
if (!(v->u.rail.track & 0xC0)) |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3009 |
SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(v->u.rail.track)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3010 |
|
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
3011 |
/* Check if the wagon was on a road/rail-crossing and disable it if no |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
3012 |
* others are on it */ |
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
3013 |
DisableTrainCrossing(v->tile); |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
3014 |
|
1418
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3015 |
if (v->u.rail.track == 0x40) { // inside a tunnel |
1430
68847f67a412
(svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents:
1418
diff
changeset
|
3016 |
TileIndex endtile = CheckTunnelBusy(v->tile, NULL); |
68847f67a412
(svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents:
1418
diff
changeset
|
3017 |
|
68847f67a412
(svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents:
1418
diff
changeset
|
3018 |
if (endtile == INVALID_TILE) // tunnel is busy (error returned) |
1418
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3019 |
return; |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3020 |
|
1431
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3021 |
switch (v->direction) { |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3022 |
case 1: |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3023 |
case 5: |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3024 |
SetSignalsOnBothDir(v->tile, 0); |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3025 |
SetSignalsOnBothDir(endtile, 0); |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3026 |
break; |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3027 |
|
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3028 |
case 3: |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3029 |
case 7: |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3030 |
SetSignalsOnBothDir(v->tile, 1); |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3031 |
SetSignalsOnBothDir(endtile, 1); |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3032 |
break; |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3033 |
|
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3034 |
default: |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3035 |
break; |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3036 |
} |
98 | 3037 |
} |
0 | 3038 |
} |
3039 |
||
3040 |
static void ChangeTrainDirRandomly(Vehicle *v) |
|
3041 |
{ |
|
3042 |
static int8 _random_dir_change[4] = { -1, 0, 0, 1}; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3043 |
|
0 | 3044 |
do { |
98 | 3045 |
//I need to buffer the train direction |
288
6bd97cdf1652
(svn r294) -Fix: autorail always builds rail, instead of occasional rail removal (on pressing hotkey after pressing 'bulldozer')
darkvater
parents:
244
diff
changeset
|
3046 |
if (!(v->u.rail.track & 0x40)) |
2635 | 3047 |
v->direction = (v->direction + _random_dir_change[GB(Random(), 0, 2)]) & 7; |
0 | 3048 |
if (!(v->vehstatus & VS_HIDDEN)) { |
3049 |
BeginVehicleMove(v); |
|
3050 |
UpdateTrainDeltaXY(v, v->direction); |
|
3051 |
v->cur_image = GetTrainImage(v, v->direction); |
|
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
3052 |
AfterSetTrainPos(v, false); |
0 | 3053 |
} |
3054 |
} while ( (v=v->next) != NULL); |
|
3055 |
} |
|
3056 |
||
3057 |
static void HandleCrashedTrain(Vehicle *v) |
|
3058 |
{ |
|
3059 |
int state = ++v->u.rail.crash_anim_pos, index; |
|
3060 |
uint32 r; |
|
3061 |
Vehicle *u; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3062 |
|
2639 | 3063 |
if (state == 4 && v->u.rail.track != 0x40) { |
1359
8ba976aed634
(svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents:
1337
diff
changeset
|
3064 |
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE); |
0 | 3065 |
} |
3066 |
||
2637
8807e6a63f89
(svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents:
2635
diff
changeset
|
3067 |
if (state <= 200 && CHANCE16R(1, 7, r)) { |
0 | 3068 |
index = (r * 10 >> 16); |
3069 |
||
3070 |
u = v; |
|
3071 |
do { |
|
3072 |
if (--index < 0) { |
|
1137
c492b777af49
(svn r1638) -Fix: Train crashes should no longer desync the game. This is more of a
celestar
parents:
1132
diff
changeset
|
3073 |
r = Random(); |
0 | 3074 |
|
3075 |
CreateEffectVehicleRel(u, |
|
2140
d708eb80ab8b
(svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents:
2135
diff
changeset
|
3076 |
GB(r, 8, 3) + 2, |
d708eb80ab8b
(svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents:
2135
diff
changeset
|
3077 |
GB(r, 16, 3) + 2, |
d708eb80ab8b
(svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents:
2135
diff
changeset
|
3078 |
GB(r, 0, 3) + 5, |
1359
8ba976aed634
(svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents:
1337
diff
changeset
|
3079 |
EV_EXPLOSION_SMALL); |
0 | 3080 |
break; |
3081 |
} |
|
3082 |
} while ( (u=u->next) != NULL); |
|
3083 |
} |
|
3084 |
||
3085 |
if (state <= 240 && !(v->tick_counter&3)) { |
|
3086 |
ChangeTrainDirRandomly(v); |
|
3087 |
} |
|
3088 |
||
1128
d3ffc98b92ad
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1109
diff
changeset
|
3089 |
if (state >= 4440 && !(v->tick_counter&0x1F)) { |
0 | 3090 |
DeleteLastWagon(v); |
1128
d3ffc98b92ad
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1109
diff
changeset
|
3091 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); |
d3ffc98b92ad
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1109
diff
changeset
|
3092 |
} |
0 | 3093 |
} |
3094 |
||
3095 |
static void HandleBrokenTrain(Vehicle *v) |
|
3096 |
{ |
|
3097 |
if (v->breakdown_ctr != 1) { |
|
3098 |
v->breakdown_ctr = 1; |
|
3099 |
v->cur_speed = 0; |
|
3100 |
||
3101 |
if (v->breakdowns_since_last_service != 255) |
|
3102 |
v->breakdowns_since_last_service++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3103 |
|
0 | 3104 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
3105 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3106 |
|
541 | 3107 |
SndPlayVehicleFx((_opt.landscape != LT_CANDY) ? |
3108 |
SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v); |
|
0 | 3109 |
|
3110 |
if (!(v->vehstatus & VS_HIDDEN)) { |
|
3111 |
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); |
|
2549 | 3112 |
if (u != NULL) u->u.special.unk0 = v->breakdown_delay * 2; |
0 | 3113 |
} |
3114 |
} |
|
3115 |
||
3116 |
if (!(v->tick_counter & 3)) { |
|
3117 |
if (!--v->breakdown_delay) { |
|
3118 |
v->breakdown_ctr = 0; |
|
3119 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
|
3120 |
} |
|
3121 |
} |
|
3122 |
} |
|
3123 |
||
3124 |
static const byte _breakdown_speeds[16] = { |
|
3125 |
225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15 |
|
3126 |
}; |
|
3127 |
||
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3128 |
static bool TrainCheckIfLineEnds(Vehicle *v) |
0 | 3129 |
{ |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1969
diff
changeset
|
3130 |
TileIndex tile; |
0 | 3131 |
uint x,y; |
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
3132 |
uint16 break_speed; |
2874
30c79e6e1c3e
(svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents:
2855
diff
changeset
|
3133 |
DiagDirection t; |
0 | 3134 |
uint32 ts; |
3135 |
||
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
3136 |
t = v->breakdown_ctr; |
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
3137 |
if (t > 1) { |
0 | 3138 |
v->vehstatus |= VS_TRAIN_SLOWING; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3139 |
|
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
3140 |
break_speed = _breakdown_speeds[GB(~t, 4, 4)]; |
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
3141 |
if (break_speed < v->cur_speed) v->cur_speed = break_speed; |
0 | 3142 |
} else { |
3143 |
v->vehstatus &= ~VS_TRAIN_SLOWING; |
|
3144 |
} |
|
3145 |
||
2639 | 3146 |
if (v->u.rail.track & 0x40) return true; // exit if inside a tunnel |
3147 |
if (v->u.rail.track & 0x80) return true; // exit if inside a depot |
|
2008
c9d6585c96c8
(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents:
2006
diff
changeset
|
3148 |
|
0 | 3149 |
tile = v->tile; |
3150 |
||
3151 |
// tunnel entrance? |
|
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
3152 |
if (IsTunnelTile(tile) && GB(_m[tile].m5, 0, 2) * 2 + 1 == v->direction) |
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
3153 |
return true; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3154 |
|
0 | 3155 |
// depot? |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3156 |
/* XXX -- When enabled, this makes it possible to crash trains of others |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3157 |
(by building a depot right against a station) */ |
2049
ad0d49c916d4
(svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents:
2044
diff
changeset
|
3158 |
/* if (IsTileType(tile, MP_RAILWAY) && (_m[tile].m5 & 0xFC) == 0xC0) |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3159 |
return true;*/ |
0 | 3160 |
|
22 | 3161 |
/* Determine the non-diagonal direction in which we will exit this tile */ |
0 | 3162 |
t = v->direction >> 1; |
3163 |
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) { |
|
3164 |
t = (t - 1) & 3; |
|
3165 |
} |
|
22 | 3166 |
/* Calculate next tile */ |
900 | 3167 |
tile += TileOffsByDir(t); |
22 | 3168 |
// determine the track status on the next tile. |
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
3169 |
ts = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _reachable_tracks[t]; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3170 |
|
22 | 3171 |
/* Calc position within the current tile ?? */ |
0 | 3172 |
x = v->x_pos & 0xF; |
3173 |
y = v->y_pos & 0xF; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3174 |
|
2639 | 3175 |
switch (v->direction) { |
0 | 3176 |
case 0: |
3177 |
x = (~x) + (~y) + 24; |
|
3178 |
break; |
|
3179 |
case 7: |
|
3180 |
x = y; |
|
3181 |
/* fall through */ |
|
3182 |
case 1: |
|
3183 |
x = (~x) + 16; |
|
3184 |
break; |
|
3185 |
case 2: |
|
3186 |
x = (~x) + y + 8; |
|
3187 |
break; |
|
3188 |
case 3: |
|
3189 |
x = y; |
|
3190 |
break; |
|
3191 |
case 4: |
|
3192 |
x = x + y - 8; |
|
3193 |
break; |
|
3194 |
case 6: |
|
3195 |
x = (~y) + x + 8; |
|
3196 |
break; |
|
3197 |
} |
|
3198 |
||
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
3199 |
if (GB(ts, 0, 16) != 0) { |
2874
30c79e6e1c3e
(svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents:
2855
diff
changeset
|
3200 |
/* If we approach a rail-piece which we can't enter, or the back of a depot, don't enter it! */ |
30c79e6e1c3e
(svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents:
2855
diff
changeset
|
3201 |
if (x + 4 > 15 && |
30c79e6e1c3e
(svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents:
2855
diff
changeset
|
3202 |
(!CheckCompatibleRail(v, tile) || |
30c79e6e1c3e
(svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents:
2855
diff
changeset
|
3203 |
(IsTileDepotType(tile, TRANSPORT_RAIL) && |
30c79e6e1c3e
(svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents:
2855
diff
changeset
|
3204 |
GetDepotDirection(tile, TRANSPORT_RAIL) == t))) { |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3205 |
v->cur_speed = 0; |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3206 |
ReverseTrainDirection(v); |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3207 |
return false; |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3208 |
} |
0 | 3209 |
if ((ts &= (ts >> 16)) == 0) { |
3210 |
// make a rail/road crossing red |
|
1927
4126e0e3e884
(svn r2433) - CodeChange: unmagicify all road/train crossings with IsLevelCrossing() function (peter1138)
Darkvater
parents:
1926
diff
changeset
|
3211 |
if (IsTileType(tile, MP_STREET) && IsLevelCrossing(tile)) { |
2493
d834d0c1502a
(svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents:
2484
diff
changeset
|
3212 |
if (GB(_m[tile].m5, 2, 1) == 0) { |
d834d0c1502a
(svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents:
2484
diff
changeset
|
3213 |
SB(_m[tile].m5, 2, 1, 1); |
541 | 3214 |
SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v); |
0 | 3215 |
MarkTileDirtyByTile(tile); |
3216 |
} |
|
3217 |
} |
|
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3218 |
return true; |
0 | 3219 |
} |
3220 |
} else if (x + 4 > 15) { |
|
3221 |
v->cur_speed = 0; |
|
3222 |
ReverseTrainDirection(v); |
|
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3223 |
return false; |
0 | 3224 |
} |
3225 |
||
3226 |
// slow down |
|
3227 |
v->vehstatus |= VS_TRAIN_SLOWING; |
|
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
3228 |
break_speed = _breakdown_speeds[x & 0xF]; |
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
3229 |
if (!(v->direction&1)) break_speed >>= 1; |
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
3230 |
if (break_speed < v->cur_speed) v->cur_speed = break_speed; |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3231 |
|
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3232 |
return true; |
0 | 3233 |
} |
3234 |
||
3235 |
static void TrainLocoHandler(Vehicle *v, bool mode) |
|
3236 |
{ |
|
3237 |
int j; |
|
3238 |
||
3239 |
/* train has crashed? */ |
|
3240 |
if (v->u.rail.crash_anim_pos != 0) { |
|
3241 |
if (!mode) HandleCrashedTrain(v); |
|
3242 |
return; |
|
3243 |
} |
|
3244 |
||
3245 |
if (v->u.rail.force_proceed != 0) |
|
3246 |
v->u.rail.force_proceed--; |
|
3247 |
||
3248 |
/* train is broken down? */ |
|
3249 |
if (v->breakdown_ctr != 0) { |
|
3250 |
if (v->breakdown_ctr <= 2) { |
|
3251 |
HandleBrokenTrain(v); |
|
3252 |
return; |
|
3253 |
} |
|
3254 |
v->breakdown_ctr--; |
|
3255 |
} |
|
3256 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
3257 |
if (HASBIT(v->u.rail.flags, VRF_REVERSING) && v->cur_speed == 0) { |
0 | 3258 |
ReverseTrainDirection(v); |
3259 |
} |
|
3260 |
||
3261 |
/* exit if train is stopped */ |
|
3262 |
if (v->vehstatus & VS_STOPPED && v->cur_speed == 0) |
|
3263 |
return; |
|
3264 |
||
3265 |
||
3266 |
if (ProcessTrainOrder(v)) { |
|
3267 |
v->load_unload_time_rem = 0; |
|
3268 |
v->cur_speed = 0; |
|
3269 |
v->subspeed = 0; |
|
3270 |
ReverseTrainDirection(v); |
|
3271 |
return; |
|
3272 |
} |
|
3273 |
||
3274 |
HandleTrainLoading(v, mode); |
|
3275 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3276 |
if (v->current_order.type == OT_LOADING) |
0 | 3277 |
return; |
3278 |
||
3279 |
if (CheckTrainStayInDepot(v)) |
|
3280 |
return; |
|
3281 |
||
3282 |
if (!mode) HandleLocomotiveSmokeCloud(v); |
|
3283 |
||
3284 |
j = UpdateTrainSpeed(v); |
|
3285 |
if (j == 0) { |
|
3286 |
// if the vehicle has speed 0, update the last_speed field. |
|
3287 |
if (v->cur_speed != 0) |
|
3288 |
return; |
|
3289 |
} else { |
|
3290 |
TrainCheckIfLineEnds(v); |
|
3291 |
||
3292 |
do { |
|
3293 |
TrainController(v); |
|
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
3294 |
CheckTrainCollision(v); |
0 | 3295 |
if (v->cur_speed <= 0x100) |
3296 |
break; |
|
3297 |
} while (--j != 0); |
|
3298 |
} |
|
3299 |
||
3300 |
SetLastSpeed(v, v->cur_speed); |
|
3301 |
} |
|
3302 |
||
3303 |
||
3304 |
void Train_Tick(Vehicle *v) |
|
3305 |
{ |
|
3306 |
if (_age_cargo_skip_counter == 0 && v->cargo_days != 0xff) |
|
3307 |
v->cargo_days++; |
|
3308 |
||
3309 |
v->tick_counter++; |
|
3310 |
||
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
3311 |
if (IsFrontEngine(v)) { |
0 | 3312 |
TrainLocoHandler(v, false); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3313 |
|
0 | 3314 |
// make sure vehicle wasn't deleted. |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
3315 |
if (v->type == VEH_Train && IsFrontEngine(v)) |
0 | 3316 |
TrainLocoHandler(v, true); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
3317 |
} else if (IsFreeWagon(v) && HASBITS(v->vehstatus, VS_CRASHED)) { |
1132
7378651941bf
(svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents:
1128
diff
changeset
|
3318 |
// Delete flooded standalone wagon |
7378651941bf
(svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents:
1128
diff
changeset
|
3319 |
if (++v->u.rail.crash_anim_pos >= 4400) |
7378651941bf
(svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents:
1128
diff
changeset
|
3320 |
DeleteVehicle(v); |
0 | 3321 |
} |
3322 |
} |
|
3323 |
||
3324 |
||
3325 |
static const byte _depot_track_ind[4] = {0,1,0,1}; |
|
3326 |
||
715
83805642c830
(svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents:
679
diff
changeset
|
3327 |
// Validation for the news item "Train is waiting in depot" |
1095 | 3328 |
static bool ValidateTrainInDepot( uint data_a, uint data_b ) |
715
83805642c830
(svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents:
679
diff
changeset
|
3329 |
{ |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
3330 |
Vehicle *v = GetVehicle(data_a); |
1064
1b6564cec6be
(svn r1565) -Fix: [ 1104969 ] Aircraft in hangar messages are now revalidated before
celestar
parents:
1060
diff
changeset
|
3331 |
return (v->u.rail.track == 0x80 && (v->vehstatus | VS_STOPPED)); |
715
83805642c830
(svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents:
679
diff
changeset
|
3332 |
} |
83805642c830
(svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents:
679
diff
changeset
|
3333 |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1969
diff
changeset
|
3334 |
void TrainEnterDepot(Vehicle *v, TileIndex tile) |
0 | 3335 |
{ |
2493
d834d0c1502a
(svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents:
2484
diff
changeset
|
3336 |
SetSignalsOnBothDir(tile, _depot_track_ind[GB(_m[tile].m5, 0, 2)]); |
0 | 3337 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
3338 |
if (!IsFrontEngine(v)) v = GetFirstVehicleInChain(v); |
0 | 3339 |
|
578
86e352980acd
(svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents:
555
diff
changeset
|
3340 |
VehicleServiceInDepot(v); |
86e352980acd
(svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents:
555
diff
changeset
|
3341 |
|
0 | 3342 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
3343 |
||
3344 |
v->load_unload_time_rem = 0; |
|
3345 |
v->cur_speed = 0; |
|
3346 |
||
445
0e3fa3da3899
(svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents:
410
diff
changeset
|
3347 |
TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT); |
0e3fa3da3899
(svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents:
410
diff
changeset
|
3348 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3349 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3350 |
Order t; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3351 |
|
0 | 3352 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3353 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3354 |
t = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3355 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3356 |
v->current_order.flags = 0; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3357 |
|
1530
caa16c506a22
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
3358 |
if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) { // Part of the orderlist? |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3359 |
v->u.rail.days_since_order_progr = 0; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3360 |
v->cur_order_index++; |
1530
caa16c506a22
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
3361 |
} else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) { // User initiated? |
0 | 3362 |
v->vehstatus |= VS_STOPPED; |
3363 |
if (v->owner == _local_player) { |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
3364 |
SetDParam(0, v->unitnumber); |
715
83805642c830
(svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents:
679
diff
changeset
|
3365 |
AddValidatedNewsItem( |
0 | 3366 |
STR_8814_TRAIN_IS_WAITING_IN_DEPOT, |
3367 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
3368 |
v->index, |
|
715
83805642c830
(svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents:
679
diff
changeset
|
3369 |
0, |
83805642c830
(svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents:
679
diff
changeset
|
3370 |
ValidateTrainInDepot); |
0 | 3371 |
} |
3372 |
} |
|
3373 |
} |
|
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
3374 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 3375 |
} |
3376 |
||
3377 |
static void CheckIfTrainNeedsService(Vehicle *v) |
|
3378 |
{ |
|
2630 | 3379 |
const Depot* depot; |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3380 |
TrainFindDepotData tfdd; |
0 | 3381 |
|
2639 | 3382 |
if (_patches.servint_trains == 0) return; |
3383 |
if (!VehicleNeedsService(v)) return; |
|
3384 |
if (v->vehstatus & VS_STOPPED) return; |
|
3385 |
if (_patches.gotodepot && VehicleHasDepotOrders(v)) return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3386 |
|
0 | 3387 |
// Don't interfere with a depot visit scheduled by the user, or a |
3388 |
// depot visit by the order list. |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3389 |
if (v->current_order.type == OT_GOTO_DEPOT && |
1530
caa16c506a22
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
3390 |
(v->current_order.flags & (OF_HALT_IN_DEPOT | OF_PART_OF_ORDERS)) != 0) |
0 | 3391 |
return; |
3392 |
||
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3393 |
tfdd = FindClosestTrainDepot(v); |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3394 |
/* Only go to the depot if it is not too far out of our way. */ |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3395 |
if (tfdd.best_length == (uint)-1 || tfdd.best_length > 16 ) { |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3396 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3397 |
/* If we were already heading for a depot but it has |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3398 |
* suddenly moved farther away, we continue our normal |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3399 |
* schedule? */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3400 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3401 |
v->current_order.flags = 0; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
3402 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 3403 |
} |
3404 |
return; |
|
3405 |
} |
|
3406 |
||
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3407 |
depot = GetDepotByTile(tfdd.tile); |
0 | 3408 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3409 |
if (v->current_order.type == OT_GOTO_DEPOT && |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
3410 |
v->current_order.station != depot->index && |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3411 |
!CHANCE16(3,16)) |
0 | 3412 |
return; |
3413 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3414 |
v->current_order.type = OT_GOTO_DEPOT; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3415 |
v->current_order.flags = OF_NON_STOP; |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
3416 |
v->current_order.station = depot->index; |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3417 |
v->dest_tile = tfdd.tile; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
3418 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 3419 |
} |
3420 |
||
1790
4afb4b4e4278
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents:
1786
diff
changeset
|
3421 |
int32 GetTrainRunningCost(const Vehicle *v) |
0 | 3422 |
{ |
3423 |
int32 cost = 0; |
|
3424 |
||
3425 |
do { |
|
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
3426 |
const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); |
2840
d68a605b2087
(svn r3388) - NewGRF: Allow train running cost class to differ from engine class. Also fix typo in r3384.
peter1138
parents:
2833
diff
changeset
|
3427 |
if (rvi->running_cost_base > 0) |
d68a605b2087
(svn r3388) - NewGRF: Allow train running cost class to differ from engine class. Also fix typo in r3384.
peter1138
parents:
2833
diff
changeset
|
3428 |
cost += rvi->running_cost_base * _price.running_rail[rvi->running_cost_class]; |
d68a605b2087
(svn r3388) - NewGRF: Allow train running cost class to differ from engine class. Also fix typo in r3384.
peter1138
parents:
2833
diff
changeset
|
3429 |
} while ((v = v->next) != NULL); |
0 | 3430 |
|
3431 |
return cost; |
|
3432 |
} |
|
3433 |
||
3434 |
void OnNewDay_Train(Vehicle *v) |
|
3435 |
{ |
|
3436 |
TileIndex tile; |
|
3437 |
||
3438 |
if ((++v->day_counter & 7) == 0) |
|
3439 |
DecreaseVehicleValue(v); |
|
3440 |
||
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
3441 |
if (IsFrontEngine(v)) { |
0 | 3442 |
CheckVehicleBreakdown(v); |
3443 |
AgeVehicle(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3444 |
|
0 | 3445 |
CheckIfTrainNeedsService(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3446 |
|
0 | 3447 |
// check if train hasn't advanced in its order list for a set number of days |
1057
3421ac931b61
(svn r1558) -Fix: [ 1104350 ] Crashed trains do not initiate the lost vehicle
Celestar
parents:
1053
diff
changeset
|
3448 |
if (_patches.lost_train_days && v->num_orders && !(v->vehstatus & (VS_STOPPED | VS_CRASHED) ) && ++v->u.rail.days_since_order_progr >= _patches.lost_train_days && v->owner == _local_player) { |
0 | 3449 |
v->u.rail.days_since_order_progr = 0; |
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
3450 |
SetDParam(0, v->unitnumber); |
0 | 3451 |
AddNewsItem( |
3452 |
STR_TRAIN_IS_LOST, |
|
3453 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
3454 |
v->index, |
|
3455 |
0); |
|
3456 |
} |
|
3457 |
||
1053
dfb5243315f1
(svn r1554) -Fix: [ 1103187 ] Order Check messages are now validated before
celestar
parents:
1049
diff
changeset
|
3458 |
CheckOrders(v->index, OC_INIT); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3459 |
|
0 | 3460 |
/* update destination */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3461 |
if (v->current_order.type == OT_GOTO_STATION && |
2639 | 3462 |
(tile = GetStation(v->current_order.station)->train_tile) != 0) { |
3463 |
v->dest_tile = tile; |
|
3464 |
} |
|
0 | 3465 |
|
3466 |
if ((v->vehstatus & VS_STOPPED) == 0) { |
|
3467 |
/* running costs */ |
|
3468 |
int32 cost = GetTrainRunningCost(v) / 364; |
|
3469 |
||
3470 |
v->profit_this_year -= cost >> 8; |
|
3471 |
||
3472 |
SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN); |
|
3473 |
SubtractMoneyFromPlayerFract(v->owner, cost); |
|
3474 |
||
3475 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
3476 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 3477 |
} |
3478 |
} |
|
3479 |
} |
|
3480 |
||
1093
e8d26c7dc42f
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1087
diff
changeset
|
3481 |
void TrainsYearlyLoop(void) |
0 | 3482 |
{ |
3483 |
Vehicle *v; |
|
3484 |
||
3485 |
FOR_ALL_VEHICLES(v) { |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
3486 |
if (v->type == VEH_Train && IsFrontEngine(v)) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3487 |
|
0 | 3488 |
// show warning if train is not generating enough income last 2 years (corresponds to a red icon in the vehicle list) |
3489 |
if (_patches.train_income_warn && v->owner == _local_player && v->age >= 730 && v->profit_this_year < 0) { |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
3490 |
SetDParam(1, v->profit_this_year); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
3491 |
SetDParam(0, v->unitnumber); |
0 | 3492 |
AddNewsItem( |
3493 |
STR_TRAIN_IS_UNPROFITABLE, |
|
3494 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
3495 |
v->index, |
|
3496 |
0); |
|
3497 |
} |
|
3498 |
||
3499 |
v->profit_last_year = v->profit_this_year; |
|
3500 |
v->profit_this_year = 0; |
|
3501 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
3502 |
} |
|
3503 |
} |
|
3504 |
} |
|
3505 |
||
3506 |
||
1093
e8d26c7dc42f
(svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents:
1087
diff
changeset
|
3507 |
void InitializeTrains(void) |
0 | 3508 |
{ |
3509 |
_age_cargo_skip_counter = 1; |
|
3510 |
} |
|
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3511 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3512 |
/* |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3513 |
* Link front and rear multiheaded engines to each other |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3514 |
* This is done when loading a savegame |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3515 |
*/ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3516 |
void ConnectMultiheadedTrains(void) |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3517 |
{ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3518 |
Vehicle *v; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3519 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3520 |
FOR_ALL_VEHICLES(v) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3521 |
if (v->type == VEH_Train) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3522 |
v->u.rail.other_multiheaded_part = NULL; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3523 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3524 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3525 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3526 |
FOR_ALL_VEHICLES(v) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3527 |
if (v->type == VEH_Train && IsFrontEngine(v)) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3528 |
Vehicle *u = v; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3529 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3530 |
BEGIN_ENUM_WAGONS(u) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3531 |
if (u->u.rail.other_multiheaded_part != NULL) continue; // we already linked this one |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3532 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3533 |
if (IsMultiheaded(u)) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3534 |
if (!IsTrainEngine(u)) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3535 |
/* we got a rear car without a front car. We will convert it to a front one */ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3536 |
SetTrainEngine(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3537 |
u->spritenum--; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3538 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3539 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3540 |
{ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3541 |
Vehicle *w; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3542 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3543 |
for(w = u->next; w != NULL && (w->engine_type != u->engine_type || w->u.rail.other_multiheaded_part != NULL); w = GetNextVehicle(w)); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3544 |
if (w != NULL) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3545 |
/* we found a car to partner with this engine. Now we will make sure it face the right way */ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3546 |
if (IsTrainEngine(w)) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3547 |
ClearTrainEngine(w); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3548 |
w->spritenum++; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3549 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3550 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3551 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3552 |
if (w != NULL) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3553 |
w->u.rail.other_multiheaded_part = u; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3554 |
u->u.rail.other_multiheaded_part = w; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3555 |
} else { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3556 |
/* we got a front car and no rear cars. We will fake this one for forget that it should have been multiheaded */ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3557 |
ClearMultiheaded(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3558 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3559 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3560 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3561 |
} END_ENUM_WAGONS(u) |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3562 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3563 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3564 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3565 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3566 |
/* |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3567 |
* Converts all trains to the new subtype format introduced in savegame 16.2 |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3568 |
* It also links multiheaded engines or make them forget they are multiheaded if no suitable partner is found |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3569 |
*/ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3570 |
void ConvertOldMultiheadToNew(void) |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3571 |
{ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3572 |
Vehicle *v; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3573 |
FOR_ALL_VEHICLES(v) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3574 |
if (v->type == VEH_Train) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3575 |
SETBIT(v->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3576 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3577 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3578 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3579 |
FOR_ALL_VEHICLES(v) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3580 |
if (v->type == VEH_Train) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3581 |
if (HASBIT(v->subtype, 7) && ((v->subtype & ~0x80) == 0 || (v->subtype & ~0x80) == 4)) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3582 |
Vehicle *u = v; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3583 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3584 |
BEGIN_ENUM_WAGONS(u) |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3585 |
const RailVehicleInfo *rvi = RailVehInfo(u->engine_type); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3586 |
CLRBIT(u->subtype, 7); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3587 |
switch (u->subtype) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3588 |
case 0: /* TS_Front_Engine */ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3589 |
if (rvi->flags & RVI_MULTIHEAD) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3590 |
SetMultiheaded(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3591 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3592 |
SetFrontEngine(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3593 |
SetTrainEngine(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3594 |
break; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3595 |
case 1: /* TS_Artic_Part */ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3596 |
u->subtype = 0; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3597 |
SetArticulatedPart(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3598 |
break; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3599 |
case 2: /* TS_Not_First */ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3600 |
u->subtype = 0; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3601 |
if (rvi->flags & RVI_WAGON) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3602 |
// normal wagon |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3603 |
SetTrainWagon(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3604 |
break; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3605 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3606 |
if (rvi->flags & RVI_MULTIHEAD && rvi->image_index == u->spritenum - 1) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3607 |
// rear end of a multiheaded engine |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3608 |
SetMultiheaded(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3609 |
break; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3610 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3611 |
if (rvi->flags & RVI_MULTIHEAD) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3612 |
SetMultiheaded(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3613 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3614 |
SetTrainEngine(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3615 |
break; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3616 |
case 4: /* TS_Free_Car */ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3617 |
u->subtype = 0; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3618 |
SetTrainWagon(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3619 |
SetFreeWagon(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3620 |
break; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3621 |
default: NOT_REACHED(); break; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3622 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3623 |
END_ENUM_WAGONS(u) |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3624 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3625 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3626 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3627 |
} |