author | bjarni |
Thu, 27 Jan 2005 21:00:05 +0000 | |
changeset 1196 | 115f46e3807d |
parent 1192 | cd9b8a18a183 |
child 1197 | 4322cf8d6ae7 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
507
8aa8100b0b22
(svn r815) Include strings.h only in the files which need it.
tron
parents:
495
diff
changeset
|
3 |
#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
|
4 |
#include "map.h" |
0 | 5 |
#include "vehicle.h" |
6 |
#include "command.h" |
|
7 |
#include "pathfind.h" |
|
8 |
#include "station.h" |
|
9 |
#include "table/train_cmd.h" |
|
10 |
#include "gfx.h" |
|
11 |
#include "news.h" |
|
12 |
#include "engine.h" |
|
13 |
#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
|
14 |
#include "sound.h" |
0 | 15 |
|
16 |
#define is_firsthead_sprite(spritenum) \ |
|
17 |
(is_custom_sprite(spritenum) \ |
|
18 |
? is_custom_firsthead_sprite(spritenum) \ |
|
19 |
: _engine_sprite_add[spritenum] == 0) |
|
20 |
||
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
21 |
static bool TrainCheckIfLineEnds(Vehicle *v); |
0 | 22 |
|
23 |
static const byte _vehicle_initial_x_fract[4] = {10,8,4,8}; |
|
24 |
static const byte _vehicle_initial_y_fract[4] = {8,4,8,10}; |
|
25 |
static const byte _state_dir_table[4] = { 0x20, 8, 0x10, 4 }; |
|
26 |
||
27 |
static const byte _signal_onedir[14] = { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
28 |
0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0, |
0 | 29 |
0x40, 0x40, 0x40, 0x10, 0x80, 0x20 |
30 |
}; |
|
31 |
||
32 |
static const byte _signal_otherdir[14] = { |
|
33 |
0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0, |
|
34 |
0x80, 0x80, 0x80, 0x20, 0x40, 0x10 |
|
35 |
}; |
|
36 |
||
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
37 |
static const byte _curve_neighbours45[8][2] = { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
38 |
{7, 1}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
39 |
{0, 2}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
40 |
{1, 3}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
41 |
{2, 4}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
42 |
{3, 5}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
43 |
{4, 6}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
44 |
{5, 7}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
45 |
{6, 0}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
46 |
}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
47 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
48 |
static const byte _curve_neighbours90[8][2] = { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
49 |
{6, 2}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
50 |
{7, 3}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
51 |
{0, 4}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
52 |
{1, 5}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
53 |
{2, 6}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
54 |
{3, 7}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
55 |
{4, 0}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
56 |
{5, 1}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
57 |
}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
58 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
59 |
enum AccelType { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
60 |
AM_ACCEL, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
61 |
AM_BRAKE |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
62 |
}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
63 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
64 |
//new acceleration |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
65 |
static int GetTrainAcceleration(Vehicle *v, bool mode) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
66 |
{ |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
67 |
Vehicle *u = v; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
68 |
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
|
69 |
int power = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
70 |
int mass = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
71 |
int max_speed = 2000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
72 |
int area = 120; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
73 |
int friction = 35; //[1e-3] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
74 |
int drag_coeff = 20; //[1e-4] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
75 |
int incl = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
76 |
int resistance; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
77 |
int speed = v->cur_speed; //[mph] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
78 |
int force = 0x3FFFFFFF; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
79 |
int pos = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
80 |
int lastpos = -1; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
81 |
int curvecount[2] = {0, 0}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
82 |
int *dist = NULL; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
83 |
int sum = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
84 |
int numcurve = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
85 |
int i; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
86 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
87 |
speed *= 10; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
88 |
speed /= 16; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
89 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
90 |
//first find the curve speed limit |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
91 |
for (; u->next != NULL; u = u->next, pos++) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
92 |
int dir = u->direction; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
93 |
int ndir = u->next->direction; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
94 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
95 |
for (i = 0; i < 2; i++) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
96 |
if ( _curve_neighbours45[dir][i] == ndir) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
97 |
curvecount[i]++; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
98 |
if (lastpos != -1) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
99 |
dist = realloc(dist, sizeof(int) * ++numcurve); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
100 |
dist[numcurve - 1] = pos - lastpos; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
101 |
if (pos - lastpos == 1) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
102 |
max_speed = 88; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
103 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
104 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
105 |
lastpos = pos; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
106 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
107 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
108 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
109 |
//if we have a 90 degree turn, fix the speed limit to 60 |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
110 |
if ( _curve_neighbours90[dir][0] == ndir || _curve_neighbours90[dir][1] == ndir) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
111 |
max_speed = 61; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
112 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
113 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
114 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
115 |
for(i = 0; i < numcurve; i++) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
116 |
sum += dist[i]; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
117 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
118 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
119 |
if (numcurve > 0) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
120 |
sum /= numcurve; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
121 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
122 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
123 |
if ((curvecount[0] != 0 || curvecount[1] != 0) && (max_speed > 88)) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
124 |
int total = curvecount[0] + curvecount[1]; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
125 |
if (curvecount[0] == 1 && curvecount[1] == 1) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
126 |
max_speed = 0xFFFF; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
127 |
} else if (total > 1) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
128 |
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
|
129 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
130 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
131 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
132 |
max_speed += (max_speed / 2) * v->u.rail.railtype; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
133 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
134 |
if (IsTileType(v->tile, MP_STATION) && v->subtype == TS_Front_Engine) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
135 |
static const TileIndexDiffC _station_dir_from_vdir[] = { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
136 |
{0, 0}, {-1, 0}, {0, 0}, {0, 1}, {0, 0}, {1, 0}, {0, 0}, {0, -1} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
137 |
}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
138 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
139 |
if (((v->current_order.station == _map2[v->tile]) || !(v->current_order.flags & OF_NON_STOP)) && v->last_station_visited != _map2[v->tile]) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
140 |
int station_length = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
141 |
TileIndex tile = v->tile; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
142 |
int delta_v; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
143 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
144 |
max_speed = 120; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
145 |
do { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
146 |
station_length++; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
147 |
tile = TILE_ADD(tile, ToTileIndexDiff(_station_dir_from_vdir[v->direction])); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
148 |
} while (IsTileType(tile, MP_STATION)); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
149 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
150 |
delta_v = v->cur_speed / (station_length + 1); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
151 |
if (v->max_speed > (v->cur_speed - delta_v)) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
152 |
max_speed = v->cur_speed - (delta_v / 10); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
153 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
154 |
max_speed = max(max_speed, 25 * station_length); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
155 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
156 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
157 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
158 |
for (u = v; u != NULL; u = u->next) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
159 |
const RailVehicleInfo *rvi = RailVehInfo(u->engine_type); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
160 |
int vmass; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
161 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
162 |
num++; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
163 |
power += rvi->power * 746; //[W] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
164 |
drag_coeff += 3; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
165 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
166 |
if (rvi->max_speed != 0) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
167 |
max_speed = min(rvi->max_speed, max_speed); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
168 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
169 |
if (u->u.rail.track == 0x80) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
170 |
max_speed = 61; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
171 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
172 |
vmass = rvi->weight; //[t] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
173 |
vmass += (_cargoc.weights[u->cargo_type] * u->cargo_count) / 16; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
174 |
mass += vmass; //[t] |
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 |
if (!IsTileType(u->tile, MP_TUNNELBRIDGE)) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
177 |
if (HASBIT(u->u.rail.flags, VRF_GOINGUP)) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
178 |
incl += vmass * 60; //3% slope, quite a bit actually |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
179 |
} else if (HASBIT(u->u.rail.flags, VRF_GOINGDOWN)) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
180 |
incl -= vmass * 60; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
181 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
182 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
183 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
184 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
185 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
186 |
// these are shown in the UI |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
187 |
v->u.rail.cached_weight = mass; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
188 |
v->u.rail.cached_power = power / 746; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
189 |
v->max_speed = max_speed; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
190 |
|
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 |
if (v->u.rail.railtype != 2) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
193 |
resistance = 13 * mass / 10; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
194 |
resistance += 60 * num; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
195 |
resistance += friction * mass * speed / 1000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
196 |
resistance += (area * drag_coeff * speed * speed) / 10000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
197 |
} else |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
198 |
resistance = (area * (drag_coeff / 2) * speed * speed) / 10000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
199 |
resistance += incl; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
200 |
resistance *= 4; //[N] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
201 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
202 |
if (speed > 0) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
203 |
switch (v->u.rail.railtype) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
204 |
case 0: |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
205 |
case 1: |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
206 |
{ |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
207 |
force = power / speed; //[N] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
208 |
force *= 22; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
209 |
force /= 10; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
210 |
} break; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
211 |
case 2: |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
212 |
force = power / 25; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
213 |
break; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
214 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
215 |
} else |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
216 |
//"kickoff" acceleration |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
217 |
force = resistance * 10; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
218 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
219 |
if (force <= 0) force = 10000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
220 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
221 |
if (v->u.rail.railtype != 2) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
222 |
force = min(force, mass * 10 * 200); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
223 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
224 |
if (mode == AM_ACCEL) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
225 |
return (force - resistance) / (mass * 4); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
226 |
} else { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
227 |
return min((-force - resistance) /(mass * 4), (10000 / (mass * 4))); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
228 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
229 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
230 |
|
0 | 231 |
void UpdateTrainAcceleration(Vehicle *v) |
232 |
{ |
|
233 |
uint acc, power=0, max_speed=5000, weight=0; |
|
234 |
Vehicle *u = v; |
|
235 |
||
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
236 |
assert(v->subtype == TS_Front_Engine); |
0 | 237 |
|
238 |
// compute stuff like max speed, power, and weight. |
|
239 |
do { |
|
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
240 |
const RailVehicleInfo *rvi = RailVehInfo(u->engine_type); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
241 |
|
0 | 242 |
// power is sum of the power for all engines |
243 |
power += rvi->power; |
|
244 |
||
245 |
// limit the max speed to the speed of the slowest vehicle. |
|
246 |
if (rvi->max_speed && rvi->max_speed <= max_speed) max_speed = rvi->max_speed; |
|
247 |
||
248 |
// weight is the sum of the weight of the wagon and the weight of the cargo. |
|
249 |
weight += rvi->weight; |
|
250 |
weight += (_cargoc.weights[u->cargo_type] * u->cargo_count) >> 4; |
|
251 |
||
252 |
} while ( (u=u->next) != NULL); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
253 |
|
0 | 254 |
// these are shown in the UI |
255 |
v->u.rail.cached_weight = weight; |
|
256 |
v->u.rail.cached_power = power; |
|
257 |
v->max_speed = max_speed; |
|
258 |
||
259 |
assert(weight != 0); |
|
260 |
||
261 |
// compute acceleration |
|
262 |
acc = power / weight * 4; |
|
263 |
||
264 |
if (acc >= 255) acc=255; |
|
265 |
if (acc == 0) acc++; |
|
266 |
||
267 |
v->acceleration = (byte)acc; |
|
268 |
} |
|
269 |
||
270 |
int GetTrainImage(Vehicle *v, byte direction) |
|
271 |
{ |
|
272 |
int img = v->spritenum; |
|
273 |
int base; |
|
274 |
||
275 |
if (is_custom_sprite(img)) { |
|
276 |
base = GetCustomVehicleSprite(v, direction + 4 * is_custom_secondhead_sprite(img)); |
|
277 |
if (base) return base; |
|
278 |
img = _engine_original_sprites[v->engine_type]; |
|
279 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
280 |
|
0 | 281 |
base = _engine_sprite_base[img] + ((direction + _engine_sprite_add[img]) & _engine_sprite_and[img]); |
282 |
||
283 |
if (v->cargo_count >= (v->cargo_cap >> 1)) |
|
284 |
base += _wagon_full_adder[img]; |
|
285 |
return base; |
|
286 |
} |
|
287 |
||
288 |
void DrawTrainEngine(int x, int y, int engine, uint32 image_ormod) |
|
289 |
{ |
|
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
290 |
const RailVehicleInfo *rvi = RailVehInfo(engine); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
291 |
|
0 | 292 |
int img = rvi->image_index; |
293 |
uint32 image = 0; |
|
294 |
||
295 |
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
|
296 |
image = GetCustomVehicleIcon(engine, 6); |
0 | 297 |
if (!image) img = _engine_original_sprites[engine]; |
298 |
} |
|
299 |
if (!image) { |
|
300 |
image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img]; |
|
301 |
} |
|
302 |
||
303 |
if (rvi->flags & RVI_MULTIHEAD) { |
|
304 |
DrawSprite(image | image_ormod, x-14, y); |
|
305 |
x += 15; |
|
306 |
image = 0; |
|
307 |
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
|
308 |
image = GetCustomVehicleIcon(engine, 2); |
0 | 309 |
if (!image) img = _engine_original_sprites[engine]; |
310 |
} |
|
311 |
if (!image) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
312 |
image = ((6 + _engine_sprite_add[img+1]) & _engine_sprite_and[img+1]) + _engine_sprite_base[img+1]; |
0 | 313 |
} |
314 |
} |
|
315 |
DrawSprite(image | image_ormod, x, y); |
|
316 |
} |
|
317 |
||
318 |
void DrawTrainEngineInfo(int engine, int x, int y, int maxw) |
|
319 |
{ |
|
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
320 |
const RailVehicleInfo *rvi = RailVehInfo(engine); |
0 | 321 |
int cap; |
322 |
uint multihead = ((rvi->flags & RVI_MULTIHEAD) ? 1 : 0); |
|
323 |
||
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
324 |
SetDParam(0, ((_price.build_railvehicle >> 3) * rvi->base_cost) >> 5); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
325 |
SetDParam(2, rvi->max_speed * 10 >> 4); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
326 |
SetDParam(3, rvi->power << multihead); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
327 |
SetDParam(1, rvi->weight << multihead); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
328 |
|
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
329 |
SetDParam(4, (rvi->running_cost_base * _price.running_rail[rvi->engclass] >> 8) << multihead); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
330 |
|
0 | 331 |
cap = rvi->capacity; |
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
332 |
SetDParam(5, STR_8838_N_A); |
0 | 333 |
if (cap != 0) { |
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
334 |
SetDParam(6, cap << multihead); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
335 |
SetDParam(5, _cargoc.names_long_p[rvi->cargo_type]); |
0 | 336 |
} |
337 |
DrawStringMultiCenter(x, y, STR_885B_COST_WEIGHT_T_SPEED_POWER, maxw); |
|
338 |
} |
|
339 |
||
340 |
||
410 | 341 |
static int32 CmdBuildRailWagon(uint engine, uint tile, uint32 flags) |
0 | 342 |
{ |
343 |
int32 value; |
|
344 |
Vehicle *v; |
|
345 |
const RailVehicleInfo *rvi; |
|
346 |
int dir; |
|
347 |
const Engine *e; |
|
348 |
int x,y; |
|
349 |
||
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
|
350 |
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
|
351 |
|
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
352 |
rvi = RailVehInfo(engine); |
0 | 353 |
value = (rvi->base_cost * _price.build_railwagon) >> 8; |
354 |
||
355 |
if (!(flags & DC_QUERY_COST)) { |
|
356 |
_error_message = STR_00E1_TOO_MANY_VEHICLES_IN_GAME; |
|
357 |
||
358 |
v = AllocateVehicle(); |
|
359 |
if (v == NULL) |
|
360 |
return CMD_ERROR; |
|
361 |
||
362 |
if (flags & DC_EXEC) { |
|
363 |
byte img = rvi->image_index; |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
364 |
Vehicle *u, *w; |
0 | 365 |
|
366 |
v->spritenum = img; |
|
367 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
368 |
u = NULL; |
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
369 |
|
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
370 |
FOR_ALL_VEHICLES(w) { |
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
371 |
if (w->type == VEH_Train && w->tile == (TileIndex)tile && |
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
372 |
w->subtype == TS_Free_Car && w->engine_type == engine) { |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
373 |
u = GetLastVehicleInChain(w); |
0 | 374 |
break; |
375 |
} |
|
376 |
} |
|
377 |
||
378 |
v->engine_type = engine; |
|
379 |
||
380 |
dir = _map5[tile] & 3; |
|
381 |
||
382 |
v->direction = (byte)(dir*2+1); |
|
383 |
v->tile = (TileIndex)tile; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
384 |
|
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
|
385 |
x = TileX(tile) * 16 | _vehicle_initial_x_fract[dir]; |
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
|
386 |
y = TileY(tile) * 16 | _vehicle_initial_y_fract[dir]; |
0 | 387 |
|
388 |
v->x_pos = x; |
|
389 |
v->y_pos = y; |
|
390 |
v->z_pos = GetSlopeZ(x,y); |
|
391 |
v->owner = _current_player; |
|
392 |
v->z_height = 6; |
|
393 |
v->u.rail.track = 0x80; |
|
394 |
v->vehstatus = VS_HIDDEN | VS_DEFPAL; |
|
395 |
||
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
396 |
v->subtype = TS_Free_Car; |
0 | 397 |
if (u != NULL) { |
398 |
u->next = v; |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
399 |
v->subtype = TS_Not_First; |
0 | 400 |
v->u.rail.first_engine = u->u.rail.first_engine; |
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
401 |
if (v->u.rail.first_engine == 0xffff && u->subtype == TS_Front_Engine ) |
0 | 402 |
v->u.rail.first_engine = u->engine_type; |
403 |
} else { |
|
404 |
v->u.rail.first_engine = 0xffff; |
|
405 |
} |
|
406 |
||
407 |
v->cargo_type = rvi->cargo_type; |
|
408 |
v->cargo_cap = rvi->capacity; |
|
409 |
v->value = value; |
|
410 |
// v->day_counter = 0; |
|
411 |
||
412 |
e = &_engines[engine]; |
|
413 |
v->u.rail.railtype = e->railtype; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
414 |
|
0 | 415 |
v->build_year = _cur_year; |
416 |
v->type = VEH_Train; |
|
417 |
v->cur_image = 0xAC2; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
418 |
|
0 | 419 |
_new_wagon_id = v->index; |
420 |
||
421 |
VehiclePositionChanged(v); |
|
422 |
||
423 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
424 |
} |
|
425 |
} |
|
426 |
||
427 |
return value; |
|
428 |
} |
|
429 |
||
430 |
// Move all free vehicles in the depot to the train |
|
431 |
static void NormalizeTrainVehInDepot(Vehicle *u) |
|
432 |
{ |
|
433 |
Vehicle *v; |
|
434 |
FOR_ALL_VEHICLES(v) { |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
435 |
if (v->type == VEH_Train && v->subtype == TS_Free_Car && |
0 | 436 |
v->tile == u->tile && |
437 |
v->u.rail.track == 0x80) { |
|
438 |
if (DoCommandByTile(0,v->index | (u->index<<16), 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE) == CMD_ERROR) |
|
439 |
break; |
|
440 |
} |
|
441 |
} |
|
442 |
} |
|
443 |
||
444 |
static const byte _railveh_unk1[] = { |
|
445 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
446 |
0, 0, 0, 1, 1, 0, 0, 0, |
|
447 |
0, 0, 0, 0, 1, 0, 1, 0, |
|
448 |
0, 1, 1, 0, 0, 0, 0, 0, |
|
449 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
450 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
451 |
0, 0, 0, 0, 0, 0, 0, 1, |
|
452 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
453 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
454 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
455 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
456 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
457 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
458 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
459 |
0, 0, 0, 0, |
|
460 |
}; |
|
461 |
||
462 |
static const byte _railveh_score[] = { |
|
463 |
1, 4, 7, 19, 20, 30, 31, 19, |
|
464 |
20, 21, 22, 10, 11, 30, 31, 32, |
|
465 |
33, 34, 35, 29, 45, 32, 50, 40, |
|
466 |
41, 51, 52, 0, 0, 0, 0, 0, |
|
467 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
468 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
469 |
0, 0, 0, 0, 0, 0, 60, 62, |
|
470 |
63, 0, 0, 0, 0, 0, 0, 0, |
|
471 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
472 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
473 |
0, 0, 0, 0, 70, 71, 72, 73, |
|
474 |
74, 0, 0, 0, 0, 0, 0, 0, |
|
475 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
476 |
0, 0, 0, 0, 0, 0, 0, 0, |
|
477 |
0, 0, 0, 0, |
|
478 |
}; |
|
479 |
||
480 |
||
812
79c99885ad00
(svn r1283) -Add: AutoRenew is now a client-side patch instead of a game-side patch
truelight
parents:
755
diff
changeset
|
481 |
int32 EstimateTrainCost(const RailVehicleInfo *rvi) |
0 | 482 |
{ |
483 |
return (rvi->base_cost * (_price.build_railvehicle >> 3)) >> 5; |
|
484 |
} |
|
485 |
||
1109
1bab892228cd
(svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents:
1103
diff
changeset
|
486 |
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
|
487 |
{ |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
488 |
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
|
489 |
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
|
490 |
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
|
491 |
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
|
492 |
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
|
493 |
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
|
494 |
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
|
495 |
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
|
496 |
v->u.rail.first_engine = 0xffff; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
497 |
u->vehstatus = v->vehstatus & ~VS_STOPPED; |
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
498 |
u->subtype = TS_Not_First; |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
499 |
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
|
500 |
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
|
501 |
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
|
502 |
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
|
503 |
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
|
504 |
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
|
505 |
u->build_year = v->build_year; |
1109
1bab892228cd
(svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents:
1103
diff
changeset
|
506 |
if (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
|
507 |
v->value = u->value = v->value >> 1; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
508 |
else |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
509 |
u->value = v->value; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
510 |
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
|
511 |
u->cur_image = 0xAC2; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
512 |
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
|
513 |
} |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
514 |
|
0 | 515 |
/* Build a railroad vehicle |
516 |
* p1 = vehicle type id |
|
517 |
*/ |
|
518 |
||
519 |
int32 CmdBuildRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
520 |
{ |
|
521 |
const RailVehicleInfo *rvi; |
|
522 |
int value,dir; |
|
523 |
Vehicle *v, *u; |
|
524 |
byte unit_num; |
|
525 |
Engine *e; |
|
526 |
uint tile; |
|
527 |
||
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
|
528 |
if (!(IsEngineBuildable(p1, VEH_Train))) return CMD_ERROR; |
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
|
529 |
|
0 | 530 |
_cmd_build_rail_veh_var1 = 0; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
531 |
|
0 | 532 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
533 |
||
534 |
tile = TILE_FROM_XY(x,y); |
|
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
535 |
rvi = RailVehInfo(p1); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
536 |
|
0 | 537 |
if (rvi->flags & RVI_WAGON) { |
538 |
return CmdBuildRailWagon(p1, tile, flags); |
|
539 |
} |
|
540 |
||
541 |
value = EstimateTrainCost(rvi); |
|
542 |
||
543 |
if (!(flags & DC_QUERY_COST)) { |
|
544 |
v = AllocateVehicle(); |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
545 |
if (v == NULL || IsOrderPoolFull()) |
0 | 546 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
547 |
||
548 |
unit_num = GetFreeUnitNumber(VEH_Train); |
|
549 |
if (unit_num > _patches.max_trains) |
|
550 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
|
551 |
||
552 |
if (flags & DC_EXEC) { |
|
553 |
v->unitnumber = unit_num; |
|
554 |
||
555 |
dir = _map5[tile] & 3; |
|
556 |
||
557 |
v->direction = (byte)(dir*2+1); |
|
558 |
v->tile = (TileIndex)tile; |
|
559 |
v->owner = _current_player; |
|
560 |
v->x_pos = (x |= _vehicle_initial_x_fract[dir]); |
|
561 |
v->y_pos = (y |= _vehicle_initial_y_fract[dir]); |
|
562 |
v->z_pos = GetSlopeZ(x,y); |
|
563 |
v->z_height = 6; |
|
564 |
v->u.rail.track = 0x80; |
|
565 |
v->u.rail.first_engine = 0xffff; |
|
566 |
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL; |
|
567 |
v->spritenum = rvi->image_index; |
|
568 |
v->cargo_type = rvi->cargo_type; |
|
569 |
v->cargo_cap = rvi->capacity; |
|
570 |
v->max_speed = rvi->max_speed; |
|
571 |
v->value = value; |
|
817
4f9377b7fd2b
(svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents:
812
diff
changeset
|
572 |
v->last_station_visited = 0xFFFF; |
0 | 573 |
v->dest_tile = 0; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
574 |
|
0 | 575 |
v->engine_type = (byte)p1; |
576 |
e = &_engines[p1]; |
|
577 |
||
578 |
v->reliability = e->reliability; |
|
579 |
v->reliability_spd_dec = e->reliability_spd_dec; |
|
580 |
v->max_age = e->lifelength * 366; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
581 |
|
0 | 582 |
v->string_id = STR_SV_TRAIN_NAME; |
583 |
v->u.rail.railtype = e->railtype; |
|
584 |
_new_train_id = v->index; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
585 |
|
0 | 586 |
v->service_interval = _patches.servint_trains; |
587 |
v->date_of_last_service = _date; |
|
588 |
v->build_year = _cur_year; |
|
589 |
v->type = VEH_Train; |
|
590 |
v->cur_image = 0xAC2; |
|
591 |
||
592 |
VehiclePositionChanged(v); |
|
593 |
||
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
594 |
if (rvi->flags&RVI_MULTIHEAD && (u = AllocateVehicle()) != NULL) |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
595 |
AddRearEngineToMultiheadedTrain(v, u, true); |
0 | 596 |
|
597 |
UpdateTrainAcceleration(v); |
|
598 |
NormalizeTrainVehInDepot(v); |
|
599 |
||
600 |
InvalidateWindow(WC_VEHICLE_DEPOT, tile); |
|
588 | 601 |
RebuildVehicleLists(); |
0 | 602 |
InvalidateWindow(WC_COMPANY, v->owner); |
603 |
} |
|
604 |
} |
|
605 |
_cmd_build_rail_veh_var1 = _railveh_unk1[p1]; |
|
606 |
_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
|
607 |
|
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
|
608 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window |
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
|
609 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
610 |
return value; |
0 | 611 |
} |
612 |
||
613 |
||
614 |
bool IsTrainDepotTile(TileIndex tile) |
|
615 |
{ |
|
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
616 |
return IsTileType(tile, MP_RAILWAY) && |
0 | 617 |
(_map5[tile] & 0xFC) == 0xC0; |
618 |
} |
|
619 |
||
410 | 620 |
static bool IsTunnelTile(TileIndex tile) |
0 | 621 |
{ |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
622 |
return IsTileType(tile, MP_TUNNELBRIDGE) && |
0 | 623 |
(_map5[tile]&0x80) == 0; |
624 |
} |
|
625 |
||
626 |
||
627 |
int CheckStoppedInDepot(Vehicle *v) |
|
628 |
{ |
|
629 |
int count; |
|
630 |
TileIndex tile = v->tile; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
631 |
|
0 | 632 |
/* check if stopped in a depot */ |
633 |
if (!IsTrainDepotTile(tile) || v->cur_speed != 0) { |
|
634 |
errmsg: |
|
635 |
_error_message = STR_881A_TRAINS_CAN_ONLY_BE_ALTERED; |
|
636 |
return -1; |
|
637 |
} |
|
638 |
||
639 |
count = 0; |
|
640 |
do { |
|
641 |
count++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
642 |
if (v->u.rail.track != 0x80 || v->tile != (TileIndex)tile || |
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
643 |
(v->subtype == TS_Front_Engine && !(v->vehstatus&VS_STOPPED))) |
0 | 644 |
goto errmsg; |
645 |
} while ( (v=v->next) != NULL); |
|
646 |
||
647 |
return count; |
|
648 |
} |
|
649 |
||
650 |
// unlink a rail wagon from the linked list. |
|
651 |
// returns the new value of first |
|
652 |
static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first) |
|
653 |
{ |
|
654 |
// unlinking the first vehicle of the chain? |
|
655 |
v->u.rail.first_engine = 0xffff; |
|
656 |
if (v == first) { |
|
657 |
Vehicle *u; |
|
658 |
if ((v=v->next) == NULL) return NULL; |
|
659 |
for (u=v; u; u=u->next) u->u.rail.first_engine = v->engine_type; |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
660 |
v->subtype = TS_Free_Car; |
0 | 661 |
return v; |
662 |
} else { |
|
663 |
Vehicle *u; |
|
664 |
for(u=first; u->next!=v; u=u->next) {} |
|
665 |
u->next = v->next; |
|
666 |
return first; |
|
667 |
} |
|
668 |
} |
|
669 |
||
670 |
static Vehicle *FindGoodVehiclePos(Vehicle *src) |
|
671 |
{ |
|
672 |
Vehicle *dst; |
|
673 |
uint16 eng = src->engine_type; |
|
674 |
TileIndex tile = src->tile; |
|
675 |
||
676 |
FOR_ALL_VEHICLES(dst) { |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
677 |
if (dst->type == VEH_Train && dst->subtype == TS_Free_Car && dst->tile==tile) { |
0 | 678 |
// check so all vehicles in the line have the same engine. |
679 |
Vehicle *v = dst; |
|
680 |
while (v->engine_type == eng) { |
|
681 |
if ((v = v->next) == NULL) return dst; |
|
682 |
} |
|
683 |
} |
|
684 |
} |
|
685 |
||
686 |
return NULL; |
|
687 |
} |
|
688 |
||
689 |
/* p1 & 0xffff= source vehicle index |
|
690 |
p1 & 0xffff0000 = what wagon to put the wagon AFTER, 0xffff0000 to make a new line |
|
691 |
p2 & 1 = move all vehicles following the vehicle.. |
|
692 |
*/ |
|
693 |
||
694 |
int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
695 |
{ |
|
696 |
Vehicle *src, *dst, *src_head, *dst_head; |
|
697 |
bool is_loco; |
|
698 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
699 |
src = GetVehicle(p1 & 0xFFFF); |
0 | 700 |
if (src->type != VEH_Train) return CMD_ERROR; |
701 |
||
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
702 |
is_loco = !(RailVehInfo(src->engine_type)->flags & RVI_WAGON) |
0 | 703 |
&& is_firsthead_sprite(src->spritenum); |
704 |
||
705 |
// if nothing is selected as destination, try and find a matching vehicle to drag to. |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
706 |
if (((int32)p1 >> 16) == -1) { |
0 | 707 |
dst = NULL; |
708 |
if (!is_loco) dst = FindGoodVehiclePos(src); |
|
709 |
} else { |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
710 |
dst = GetVehicle(((int32)p1 >> 16)); |
0 | 711 |
} |
712 |
||
713 |
// don't move the same vehicle.. |
|
714 |
if (src == dst) |
|
715 |
return 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
716 |
|
0 | 717 |
/* the player must be the owner */ |
718 |
if (!CheckOwnership(src->owner) || (dst!=NULL && !CheckOwnership(dst->owner))) |
|
719 |
return CMD_ERROR; |
|
720 |
||
721 |
/* locate the head of the two chains */ |
|
722 |
src_head = GetFirstVehicleInChain(src); |
|
723 |
dst_head = NULL; |
|
724 |
if (dst != NULL) dst_head = GetFirstVehicleInChain(dst); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
725 |
|
0 | 726 |
/* check if all vehicles in the source train are stopped */ |
727 |
if (CheckStoppedInDepot(src_head) < 0) |
|
728 |
return CMD_ERROR; |
|
729 |
||
730 |
/* check if all the vehicles in the dest train are stopped, |
|
731 |
* and that the length of the dest train is no longer than XXX vehicles */ |
|
732 |
if (dst_head != NULL) { |
|
733 |
int num = CheckStoppedInDepot(dst_head); |
|
734 |
if (num < 0) |
|
735 |
return CMD_ERROR; |
|
736 |
||
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
737 |
if (num > (_patches.mammoth_trains ? 100 : 9) && dst_head->subtype == TS_Front_Engine ) |
0 | 738 |
return_cmd_error(STR_8819_TRAIN_TOO_LONG); |
739 |
||
740 |
// if it's a multiheaded vehicle we're dragging to, drag to the vehicle before.. |
|
741 |
while (is_custom_secondhead_sprite(dst->spritenum) |
|
742 |
|| (!is_custom_sprite(dst->spritenum) && _engine_sprite_add[dst->spritenum] != 0)) { |
|
743 |
Vehicle *v = GetPrevVehicleInChain(dst); |
|
744 |
if (!v || src == v) break; |
|
745 |
dst = v; |
|
746 |
} |
|
747 |
||
748 |
assert(dst_head->tile == src_head->tile); |
|
749 |
} |
|
750 |
||
751 |
// when moving all wagons, we can't have the same src_head and dst_head |
|
752 |
if (p2 & 1 && src_head == dst_head) |
|
753 |
return 0; |
|
754 |
||
755 |
// moving a loco to a new line?, then we need to assign a unitnumber. |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
756 |
if (dst == NULL && src->subtype != TS_Front_Engine && is_loco) { |
0 | 757 |
uint unit_num = GetFreeUnitNumber(VEH_Train); |
758 |
if (unit_num > _patches.max_trains) |
|
759 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
|
760 |
||
761 |
if (flags & DC_EXEC) |
|
762 |
src->unitnumber = unit_num; |
|
763 |
} |
|
764 |
||
765 |
||
766 |
/* do it? */ |
|
767 |
if (flags & DC_EXEC) { |
|
768 |
if (p2 & 1) { |
|
769 |
// unlink ALL wagons |
|
770 |
if (src != src_head) { |
|
771 |
Vehicle *v = src_head; |
|
772 |
while (v->next != src) v=v->next; |
|
773 |
v->next = NULL; |
|
774 |
} |
|
775 |
} else { |
|
776 |
// unlink single wagon from linked list |
|
777 |
UnlinkWagon(src, src_head); |
|
778 |
src->next = NULL; |
|
779 |
} |
|
780 |
||
781 |
if (dst == NULL) { |
|
782 |
// move the train to an empty line. for locomotives, we set the type to 0. for wagons, 4. |
|
783 |
if (is_loco) { |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
784 |
if (src->subtype != TS_Front_Engine) { |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
785 |
// setting the type to 0 also involves setting up the orders field. |
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
786 |
src->subtype = TS_Front_Engine; |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
787 |
assert(src->orders == NULL); |
0 | 788 |
src->num_orders = 0; |
789 |
} |
|
790 |
dst_head = src; |
|
791 |
} else { |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
792 |
src->subtype = TS_Free_Car; |
0 | 793 |
} |
794 |
src->u.rail.first_engine = 0xffff; |
|
795 |
} else { |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
796 |
if (src->subtype == TS_Front_Engine) { |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
797 |
// the vehicle was previously a loco. need to free the order list and delete vehicle windows etc. |
0 | 798 |
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
|
799 |
DeleteVehicleOrders(src); |
0 | 800 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
801 |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
802 |
src->subtype = TS_Not_First; |
0 | 803 |
src->unitnumber = 0; // doesn't occupy a unitnumber anymore. |
804 |
||
805 |
// setup first_engine |
|
806 |
src->u.rail.first_engine = dst->u.rail.first_engine; |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
807 |
if (src->u.rail.first_engine == 0xffff && dst->subtype == TS_Front_Engine) |
0 | 808 |
src->u.rail.first_engine = dst->engine_type; |
809 |
||
810 |
// link in the wagon(s) in the chain. |
|
811 |
{ |
|
812 |
Vehicle *v = src; |
|
813 |
while (v->next != NULL) { |
|
814 |
v->next->u.rail.first_engine = v->u.rail.first_engine; |
|
815 |
v = v->next; |
|
816 |
} |
|
817 |
v->next = dst->next; |
|
818 |
} |
|
819 |
dst->next = src; |
|
820 |
} |
|
821 |
||
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
822 |
if (src_head->subtype == TS_Front_Engine) |
0 | 823 |
UpdateTrainAcceleration(src_head); |
824 |
InvalidateWindow(WC_VEHICLE_DETAILS, src_head->index); |
|
825 |
||
826 |
if (dst_head) { |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
827 |
if (dst_head->subtype == TS_Front_Engine) |
0 | 828 |
UpdateTrainAcceleration(dst_head); |
829 |
InvalidateWindow(WC_VEHICLE_DETAILS, dst_head->index); |
|
830 |
} |
|
831 |
||
832 |
InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile); |
|
737
ceb3dce50a37
(svn r1189) Fix vehicle list update glitch when moving waggons in depots
tron
parents:
715
diff
changeset
|
833 |
RebuildVehicleLists(); |
0 | 834 |
} |
835 |
||
836 |
return 0; |
|
837 |
} |
|
838 |
||
839 |
/* p1 = train to start / stop */ |
|
840 |
int32 CmdStartStopTrain(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
841 |
{ |
|
842 |
Vehicle *v; |
|
843 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
844 |
v = GetVehicle(p1); |
0 | 845 |
|
846 |
if (!CheckOwnership(v->owner)) |
|
847 |
return CMD_ERROR; |
|
848 |
||
849 |
if (flags & DC_EXEC) { |
|
850 |
v->u.rail.days_since_order_progr = 0; |
|
851 |
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
|
852 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 853 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
854 |
} |
|
855 |
return 0; |
|
856 |
} |
|
857 |
||
858 |
// p1 = wagon/loco index |
|
859 |
// p2 = mode |
|
860 |
// 0: sell just the vehicle |
|
861 |
// 1: sell the vehicle and all vehicles that follow it in the chain |
|
862 |
// 2: when selling attached locos, rearrange all vehicles after it to separate lines. |
|
863 |
int32 CmdSellRailWagon(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
864 |
{ |
|
865 |
Vehicle *v, *first,*last; |
|
866 |
int32 cost; |
|
867 |
||
868 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
869 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
870 |
v = GetVehicle(p1); |
0 | 871 |
|
872 |
if (!CheckOwnership(v->owner)) |
|
873 |
return CMD_ERROR; |
|
874 |
||
875 |
// get first vehicle in chain |
|
876 |
first = v; |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
877 |
if (first->subtype != TS_Front_Engine) { |
0 | 878 |
first = GetFirstVehicleInChain(first); |
242
f4894a2cdfe8
(svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents:
193
diff
changeset
|
879 |
last = GetLastVehicleInChain(first); |
f4894a2cdfe8
(svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents:
193
diff
changeset
|
880 |
//now if: |
f4894a2cdfe8
(svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents:
193
diff
changeset
|
881 |
// 1) we delete a whole a chain, and |
244
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
882 |
// 2) we don't actually try to delete the last engine |
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
883 |
// 3) the first and the last vehicle of that train are of the same type, and |
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
884 |
// 4) the first and the last vehicle of the chain are not identical |
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
885 |
// 5) and of "engine" type (i.e. not a carriage) |
242
f4894a2cdfe8
(svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents:
193
diff
changeset
|
886 |
// then let the last vehicle live |
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
887 |
if ( (p2 == 1) && (v != last) && ( last->engine_type == first->engine_type ) && (last != first) && (first->subtype == TS_Front_Engine) ) |
242
f4894a2cdfe8
(svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents:
193
diff
changeset
|
888 |
last = GetPrevVehicleInChain(last); |
f4894a2cdfe8
(svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents:
193
diff
changeset
|
889 |
else |
f4894a2cdfe8
(svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents:
193
diff
changeset
|
890 |
last = NULL; |
0 | 891 |
} else { |
892 |
if (p2 != 1) { |
|
893 |
// sell last part of multiheaded? |
|
894 |
last = GetLastVehicleInChain(v); |
|
73
34d5f66cfad7
(svn r74) -Fix: [1009631] Wrong multihead selling (Bodewes)
truelight
parents:
22
diff
changeset
|
895 |
// Check if the end-part is the same engine and check if it is the rear-end |
34d5f66cfad7
(svn r74) -Fix: [1009631] Wrong multihead selling (Bodewes)
truelight
parents:
22
diff
changeset
|
896 |
if (last->engine_type != first->engine_type || is_firsthead_sprite(last->spritenum)) |
0 | 897 |
last = NULL; |
898 |
} else { |
|
899 |
last = NULL; |
|
900 |
} |
|
901 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
902 |
|
0 | 903 |
// make sure the vehicle is stopped in the depot |
904 |
if (CheckStoppedInDepot(first) < 0) |
|
905 |
return CMD_ERROR; |
|
906 |
||
907 |
||
908 |
if (flags & DC_EXEC) { |
|
909 |
// always redraw the depot. maybe redraw train list |
|
910 |
InvalidateWindow(WC_VEHICLE_DEPOT, first->tile); |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
911 |
if (first->subtype == TS_Front_Engine) { |
588 | 912 |
RebuildVehicleLists(); |
164
0cbdf3c9bde1
(svn r165) -Feature: Option to sort vehicles in vehicle-list window by different criteria. Total independent sort for all types and players. Periodic resort of list every 10 TTD days. Thank you for your graphical inspiration follow and buxo (since none of you provided any code).
darkvater
parents:
159
diff
changeset
|
913 |
} |
0 | 914 |
// when selling an attached locomotive. we need to delete its window. |
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
915 |
if (v->subtype == TS_Front_Engine) { |
0 | 916 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
917 |
|
0 | 918 |
// rearrange all vehicles that follow to separate lines. |
919 |
if (p2 == 2) { |
|
920 |
Vehicle *u,*tmp; |
|
921 |
u = v->next; |
|
922 |
while (u != last) { |
|
923 |
tmp = u; |
|
924 |
u = u->next; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
925 |
DoCommandByTile(tmp->tile, tmp->index | ((-1)<<16), 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
0 | 926 |
} |
927 |
} |
|
928 |
} |
|
929 |
||
930 |
// delete the vehicles |
|
931 |
cost = 0; |
|
932 |
for(;;) { |
|
933 |
Vehicle *tmp; |
|
934 |
||
935 |
assert(first); |
|
936 |
first = UnlinkWagon(v, first); |
|
937 |
cost -= v->value; |
|
938 |
tmp = v; |
|
939 |
DeleteVehicle(tmp); |
|
244
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
940 |
if ( v == last ) { |
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
941 |
last = NULL; |
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
942 |
break; |
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
943 |
} |
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
944 |
if ( (v=v->next) == last || p2 != 1) break; |
0 | 945 |
} |
946 |
||
947 |
// delete last vehicle of multiheaded train? |
|
948 |
if (last) { |
|
949 |
first = UnlinkWagon(last, first); |
|
950 |
cost -= last->value; |
|
951 |
DeleteVehicle(last); |
|
952 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
953 |
|
0 | 954 |
// an attached train changed? |
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
955 |
if (first && first->subtype == TS_Front_Engine) { |
0 | 956 |
UpdateTrainAcceleration(first); |
957 |
InvalidateWindow(WC_VEHICLE_DETAILS, first->index); |
|
958 |
} |
|
959 |
} else { |
|
960 |
cost = 0; |
|
961 |
for(;;) { |
|
962 |
cost -= v->value; |
|
244
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
963 |
if ( v == last ) { |
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
964 |
last = NULL; |
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
965 |
break; |
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
966 |
} |
57a3922a029c
(svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents:
242
diff
changeset
|
967 |
if ( (v=v->next) == last || p2 != 1) break; |
0 | 968 |
} |
969 |
if (last) cost -= last->value; |
|
970 |
} |
|
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
|
971 |
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window |
0 | 972 |
|
973 |
return cost; |
|
974 |
} |
|
975 |
||
976 |
static void UpdateTrainDeltaXY(Vehicle *v, int direction) |
|
977 |
{ |
|
978 |
#define MKIT(a,b,c,d) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0) |
|
979 |
static const uint32 _delta_xy_table[8] = { |
|
980 |
MKIT(3, 3, -1, -1), |
|
981 |
MKIT(3, 7, -1, -3), |
|
982 |
MKIT(3, 3, -1, -1), |
|
983 |
MKIT(7, 3, -3, -1), |
|
984 |
MKIT(3, 3, -1, -1), |
|
985 |
MKIT(3, 7, -1, -3), |
|
986 |
MKIT(3, 3, -1, -1), |
|
987 |
MKIT(7, 3, -3, -1), |
|
988 |
}; |
|
989 |
#undef MKIT |
|
990 |
||
991 |
uint32 x = _delta_xy_table[direction]; |
|
992 |
||
993 |
v->x_offs = (byte)x; |
|
994 |
v->y_offs = (byte)(x>>=8); |
|
995 |
v->sprite_width = (byte)(x>>=8); |
|
996 |
v->sprite_height = (byte)(x>>=8); |
|
997 |
} |
|
998 |
||
999 |
static void UpdateVarsAfterSwap(Vehicle *v) |
|
1000 |
{ |
|
1001 |
UpdateTrainDeltaXY(v, v->direction); |
|
1002 |
v->cur_image = GetTrainImage(v, v->direction); |
|
1003 |
BeginVehicleMove(v); |
|
1004 |
VehiclePositionChanged(v); |
|
1005 |
EndVehicleMove(v); |
|
1006 |
} |
|
1007 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1008 |
static void SetLastSpeed(Vehicle *v, int spd) { |
0 | 1009 |
int old = v->u.rail.last_speed; |
1010 |
if (spd != old) { |
|
1011 |
v->u.rail.last_speed = spd; |
|
1012 |
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
|
1013 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1014 |
} |
1015 |
} |
|
1016 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1017 |
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
|
1018 |
{ |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1019 |
byte flag1, flag2; |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1020 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1021 |
flag1 = *swap_flag1; |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1022 |
flag2 = *swap_flag2; |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1023 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1024 |
/* Clear the flags */ |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1025 |
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
|
1026 |
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
|
1027 |
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
|
1028 |
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
|
1029 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1030 |
/* 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
|
1031 |
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
|
1032 |
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
|
1033 |
} 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
|
1034 |
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
|
1035 |
} |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1036 |
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
|
1037 |
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
|
1038 |
} 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
|
1039 |
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
|
1040 |
} |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1041 |
} |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1042 |
|
0 | 1043 |
static void ReverseTrainSwapVeh(Vehicle *v, int l, int r) |
1044 |
{ |
|
1045 |
Vehicle *a, *b; |
|
1046 |
||
1047 |
/* locate vehicles to swap */ |
|
1048 |
for(a=v; l!=0; l--) { a = a->next; } |
|
1049 |
for(b=v; r!=0; r--) { b = b->next; } |
|
1050 |
||
1051 |
if (a != b) { |
|
1052 |
/* swap the hidden bits */ |
|
1053 |
{ |
|
1054 |
uint16 tmp = (a->vehstatus & ~VS_HIDDEN) | (b->vehstatus&VS_HIDDEN); |
|
1055 |
b->vehstatus = (b->vehstatus & ~VS_HIDDEN) | (a->vehstatus&VS_HIDDEN); |
|
1056 |
a->vehstatus = tmp; |
|
1057 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1058 |
|
0 | 1059 |
/* swap variables */ |
1060 |
swap_byte(&a->u.rail.track, &b->u.rail.track); |
|
1061 |
swap_byte(&a->direction, &b->direction); |
|
1062 |
||
1063 |
/* toggle direction */ |
|
1064 |
if (!(a->u.rail.track & 0x80)) a->direction ^= 4; |
|
1065 |
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
|
1066 |
|
0 | 1067 |
/* 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
|
1068 |
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
|
1069 |
swap_int32(&a->y_pos, &b->y_pos); |
0 | 1070 |
swap_tile(&a->tile, &b->tile); |
1071 |
swap_byte(&a->z_pos, &b->z_pos); |
|
1072 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1073 |
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
|
1074 |
|
0 | 1075 |
/* update other vars */ |
1076 |
UpdateVarsAfterSwap(a); |
|
1077 |
UpdateVarsAfterSwap(b); |
|
1078 |
} else { |
|
1079 |
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
|
1080 |
UpdateVarsAfterSwap(a); |
0 | 1081 |
} |
1082 |
} |
|
1083 |
||
744
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1084 |
/* 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
|
1085 |
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
|
1086 |
{ |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1087 |
if (v->tile != *(const TileIndex*)data || v->type != VEH_Train) |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1088 |
return NULL; |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1089 |
|
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1090 |
return v; |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1091 |
} |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1092 |
|
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1093 |
static void DisableTrainCrossing(TileIndex tile) |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1094 |
{ |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1095 |
/* Test if we have a rail/road-crossing */ |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1096 |
if (IsTileType(tile, MP_STREET) && (_map5[tile] & 0xF0) == 0x10) { |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1097 |
/* Check if there is a train on the tile itself */ |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1098 |
if (VehicleFromPos(tile, &tile, TestTrainOnCrossing) == NULL) { |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1099 |
/* If light is on, switch light off */ |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1100 |
if (_map5[tile] & 4) { |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1101 |
_map5[tile] &= ~4; |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1102 |
MarkTileDirtyByTile(tile); |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1103 |
} |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1104 |
} |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1105 |
} |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1106 |
} |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1107 |
|
0 | 1108 |
static void ReverseTrainDirection(Vehicle *v) |
1109 |
{ |
|
1110 |
int l = 0, r = -1; |
|
1111 |
Vehicle *u; |
|
1112 |
||
1113 |
if (IsTrainDepotTile(v->tile)) |
|
1114 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
1115 |
||
743
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1116 |
/* 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
|
1117 |
{ |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1118 |
TileIndex tile = v->tile; |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1119 |
int t; |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1120 |
/* Determine the non-diagonal direction in which we will exit this tile */ |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1121 |
t = v->direction >> 1; |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1122 |
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
|
1123 |
t = (t - 1) & 3; |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1124 |
} |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1125 |
/* Calculate next tile */ |
900 | 1126 |
tile += TileOffsByDir(t); |
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1127 |
|
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1128 |
/* Check if the train left a rail/road-crossing */ |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1129 |
DisableTrainCrossing(tile); |
743
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1130 |
} |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1131 |
|
0 | 1132 |
// count number of vehicles |
1133 |
u = v; |
|
1134 |
do r++; while ( (u = u->next) != NULL ); |
|
1135 |
||
1136 |
/* swap start<>end, start+1<>end-1, ... */ |
|
1137 |
do { |
|
1138 |
ReverseTrainSwapVeh(v, l++, r--); |
|
1139 |
} while (l <= r); |
|
1140 |
||
1141 |
if (IsTrainDepotTile(v->tile)) |
|
1142 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
1143 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1144 |
CLRBIT(v->u.rail.flags, VRF_REVERSING); |
0 | 1145 |
} |
1146 |
||
1147 |
/* p1 = vehicle */ |
|
1148 |
int32 CmdReverseTrainDirection(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
1149 |
{ |
|
1150 |
Vehicle *v; |
|
1151 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1152 |
v = GetVehicle(p1); |
0 | 1153 |
|
1154 |
if (!CheckOwnership(v->owner)) |
|
1155 |
return CMD_ERROR; |
|
1156 |
||
1157 |
_error_message = STR_EMPTY; |
|
1158 |
||
1159 |
// if (v->u.rail.track & 0x80 || IsTrainDepotTile(v->tile)) |
|
1160 |
// return CMD_ERROR; |
|
1161 |
||
1162 |
if (v->u.rail.crash_anim_pos != 0 || v->breakdown_ctr != 0) |
|
1163 |
return CMD_ERROR; |
|
1164 |
||
1165 |
if (flags & DC_EXEC) { |
|
1166 |
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
|
1167 |
TOGGLEBIT(v->u.rail.flags, VRF_REVERSING); |
0 | 1168 |
} else { |
1169 |
v->cur_speed = 0; |
|
1170 |
SetLastSpeed(v, 0); |
|
1171 |
ReverseTrainDirection(v); |
|
1172 |
} |
|
1173 |
} |
|
1174 |
return 0; |
|
1175 |
} |
|
1176 |
||
1177 |
int32 CmdForceTrainProceed(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
1178 |
{ |
|
1179 |
Vehicle *v; |
|
1180 |
||
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1181 |
v = GetVehicle(p1); |
0 | 1182 |
|
1183 |
if (!CheckOwnership(v->owner)) |
|
1184 |
return CMD_ERROR; |
|
1185 |
||
1186 |
if (flags & DC_EXEC) |
|
1187 |
v->u.rail.force_proceed = 0x50; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1188 |
|
0 | 1189 |
return 0; |
1190 |
} |
|
1191 |
||
1192 |
// p1 = vehicle to refit |
|
842 | 1193 |
// p2 = new cargo (0xFF) |
1194 |
// p2 = skip check for stopped in hanger (0x0100) |
|
0 | 1195 |
int32 CmdRefitRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
1196 |
{ |
|
1197 |
Vehicle *v; |
|
1198 |
int32 cost; |
|
1199 |
uint num; |
|
1200 |
||
842 | 1201 |
byte SkipStoppedInDepotCheck = (p2 & 0x100) >> 8; |
1202 |
||
1203 |
p2 = p2 & 0xFF; |
|
1204 |
||
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
|
1205 |
SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN); |
915 | 1206 |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1207 |
v = GetVehicle(p1); |
842 | 1208 |
if (!CheckOwnership(v->owner) || ((CheckStoppedInDepot(v) < 0) && !(SkipStoppedInDepotCheck))) |
0 | 1209 |
return CMD_ERROR; |
1210 |
||
1211 |
cost = 0; |
|
1212 |
num = 0; |
|
915 | 1213 |
|
0 | 1214 |
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
|
1215 |
/* 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
|
1216 |
* 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
|
1217 |
* some nice [Refit] button near each wagon. --pasky */ |
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
1218 |
if ((!(RailVehInfo(v->engine_type)->flags & RVI_WAGON) |
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
|
1219 |
|| (_engine_refit_masks[v->engine_type] & (1 << p2))) |
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
|
1220 |
&& (byte) p2 != v->cargo_type && v->cargo_cap != 0) { |
15
255d0f685325
(svn r16) Fix: Train refit cost based on _price.ship_base
dominik
parents:
11
diff
changeset
|
1221 |
cost += (_price.build_railvehicle >> 8); |
0 | 1222 |
num += v->cargo_cap; |
1223 |
if (flags & DC_EXEC) { |
|
842 | 1224 |
//autorefitted train cars wants to keep the cargo |
1225 |
//it will be checked if the cargo is valid in CmdReplaceVehicle |
|
1226 |
if (!(SkipStoppedInDepotCheck)) |
|
1227 |
v->cargo_count = 0; |
|
0 | 1228 |
v->cargo_type = (byte)p2; |
1229 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
1230 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1231 |
} |
842 | 1232 |
// SkipStoppedInDepotCheck is called by CmdReplace and it should only apply to the single car it is called for |
1233 |
} while ( (v=v->next) != NULL || SkipStoppedInDepotCheck ); |
|
0 | 1234 |
|
1235 |
_returned_refit_amount = num; |
|
1236 |
||
1237 |
return cost; |
|
1238 |
} |
|
1239 |
||
1240 |
int GetDepotByTile(uint tile) |
|
1241 |
{ |
|
1242 |
Depot *d; |
|
1243 |
int i=0; |
|
1244 |
for(d=_depots; d->xy != (TileIndex)tile; d++) { i++; } |
|
1245 |
return i; |
|
1246 |
} |
|
1247 |
||
1248 |
typedef struct TrainFindDepotData { |
|
1249 |
uint best_length; |
|
1250 |
uint tile; |
|
1251 |
byte owner; |
|
1252 |
} TrainFindDepotData; |
|
1253 |
||
1254 |
static bool TrainFindDepotEnumProc(uint tile, TrainFindDepotData *tfdd, int track, uint length, byte *state) |
|
1255 |
{ |
|
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
1256 |
if (IsTileType(tile, MP_RAILWAY) && _map_owner[tile] == tfdd->owner) { |
0 | 1257 |
if ((_map5[tile] & ~0x3) == 0xC0) { |
1258 |
if (length < tfdd->best_length) { |
|
1259 |
tfdd->best_length = length; |
|
1260 |
tfdd->tile = tile; |
|
1261 |
} |
|
1262 |
return true; |
|
1263 |
} |
|
1264 |
||
1265 |
// make sure the train doesn't run against a oneway signal |
|
1266 |
if ((_map5[tile] & 0xC0) == 0x40) { |
|
1267 |
if (!(_map3_lo[tile] & _signal_onedir[track]) && _map3_lo[tile] & _signal_otherdir[track]) |
|
1268 |
return true; |
|
1269 |
} |
|
1270 |
} |
|
1271 |
||
1272 |
// stop searching if we've found a destination that is closer already. |
|
1273 |
return length >= tfdd->best_length; |
|
1274 |
} |
|
1275 |
||
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
|
1276 |
// returns the tile of a depot to goto to. |
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
|
1277 |
static TrainFindDepotData FindClosestTrainDepot(Vehicle *v) |
0 | 1278 |
{ |
1279 |
int i; |
|
1280 |
TrainFindDepotData tfdd; |
|
1281 |
uint tile = v->tile; |
|
1282 |
||
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
|
1283 |
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
|
1284 |
tfdd.best_length = (uint)-1; |
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
|
1285 |
|
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
|
1286 |
if (IsTrainDepotTile(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
|
1287 |
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
|
1288 |
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
|
1289 |
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
|
1290 |
} |
0 | 1291 |
|
1292 |
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
|
1293 |
|
0 | 1294 |
if (!_patches.new_depot_finding) { |
1295 |
// search in all directions |
|
1296 |
for(i=0; i!=4; i++) |
|
1297 |
NewTrainPathfind(tile, i, (TPFEnumProc*)TrainFindDepotEnumProc, &tfdd, NULL); |
|
1298 |
} else { |
|
1299 |
// search in the forward direction first. |
|
1300 |
i = v->direction >> 1; |
|
1301 |
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { i = (i - 1) & 3; } |
|
1302 |
NewTrainPathfind(tile, i, (TPFEnumProc*)TrainFindDepotEnumProc, &tfdd, NULL); |
|
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
|
1303 |
if (tfdd.best_length == (uint)-1){ |
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
|
1304 |
// 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
|
1305 |
i = (v->direction^4) >> 1; |
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
|
1306 |
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { i = (i - 1) & 3; } |
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
|
1307 |
NewTrainPathfind(tile, i, (TPFEnumProc*)TrainFindDepotEnumProc, &tfdd, NULL); |
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
|
1308 |
} |
0 | 1309 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1310 |
|
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
|
1311 |
return tfdd; |
0 | 1312 |
} |
1313 |
||
1314 |
int32 CmdTrainGotoDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
1315 |
{ |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1316 |
Vehicle *v = GetVehicle(p1); |
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
|
1317 |
TrainFindDepotData tfdd; |
0 | 1318 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1319 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
0 | 1320 |
if (flags & DC_EXEC) { |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1321 |
if (v->current_order.flags & OF_UNLOAD) { |
0 | 1322 |
v->u.rail.days_since_order_progr = 0; |
1323 |
v->cur_order_index++; |
|
1324 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1325 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1326 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1327 |
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
|
1328 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1329 |
} |
1330 |
return 0; |
|
1331 |
} |
|
1332 |
||
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
|
1333 |
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
|
1334 |
if (tfdd.best_length == (uint)-1) |
0 | 1335 |
return_cmd_error(STR_883A_UNABLE_TO_FIND_ROUTE_TO); |
1336 |
||
1337 |
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
|
1338 |
v->dest_tile = tfdd.tile; |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1339 |
v->current_order.type = OT_GOTO_DEPOT; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1340 |
v->current_order.flags = OF_NON_STOP | OF_FULL_LOAD; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1341 |
v->current_order.station = GetDepotByTile(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
|
1342 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1343 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1344 |
|
0 | 1345 |
return 0; |
1346 |
} |
|
1347 |
||
1348 |
/* p1 = vehicle |
|
1349 |
* p2 = new service int |
|
1350 |
*/ |
|
1351 |
int32 CmdChangeTrainServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
1352 |
{ |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
1353 |
Vehicle *v = GetVehicle(p1); |
0 | 1354 |
|
1355 |
if (!CheckOwnership(v->owner)) |
|
1356 |
return CMD_ERROR; |
|
1357 |
||
1358 |
if (flags & DC_EXEC) { |
|
1359 |
v->service_interval = (uint16)p2; |
|
1360 |
InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 8); |
|
1361 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1362 |
|
0 | 1363 |
return 0; |
1364 |
} |
|
1365 |
||
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
|
1366 |
void OnTick_Train(void) |
0 | 1367 |
{ |
1368 |
_age_cargo_skip_counter = (_age_cargo_skip_counter == 0) ? 184 : (_age_cargo_skip_counter - 1); |
|
1369 |
} |
|
1370 |
||
1371 |
static const int8 _vehicle_smoke_pos[16] = { |
|
1372 |
-4, -4, -4, 0, 4, 4, 4, 0, |
|
1373 |
-4, 0, 4, 4, 4, 0,-4,-4, |
|
1374 |
}; |
|
1375 |
||
1376 |
static void HandleLocomotiveSmokeCloud(Vehicle *v) |
|
1377 |
{ |
|
1378 |
Vehicle *u; |
|
1379 |
||
1380 |
if (v->vehstatus & VS_TRAIN_SLOWING || v->load_unload_time_rem != 0 || v->cur_speed < 2) |
|
1381 |
return; |
|
1382 |
||
1383 |
u = v; |
|
1384 |
||
1385 |
do { |
|
1386 |
int engtype = v->engine_type; |
|
1387 |
||
1388 |
// no smoke? |
|
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
1389 |
if (RailVehInfo(engtype)->flags & 2 |
0 | 1390 |
|| _engines[engtype].railtype > 0 |
1391 |
|| (v->vehstatus&VS_HIDDEN) || (v->u.rail.track & 0xC0) ) |
|
1392 |
continue; |
|
1393 |
||
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
1394 |
switch (RailVehInfo(engtype)->engclass) { |
0 | 1395 |
case 0: |
1396 |
// steam smoke. |
|
1397 |
if ( (v->tick_counter&0xF) == 0 && !IsTrainDepotTile(v->tile) && !IsTunnelTile(v->tile)) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1398 |
CreateEffectVehicleRel(v, |
0 | 1399 |
(_vehicle_smoke_pos[v->direction]), |
1400 |
(_vehicle_smoke_pos[v->direction+8]), |
|
1401 |
10, |
|
1402 |
EV_STEAM_SMOKE); |
|
1403 |
} |
|
1404 |
break; |
|
1405 |
||
1406 |
case 1: |
|
1407 |
// diesel smoke |
|
1408 |
if (u->cur_speed <= 40 && !IsTrainDepotTile(v->tile) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x1E00) { |
|
1409 |
CreateEffectVehicleRel(v, 0,0,10, EV_SMOKE_3); |
|
1410 |
} |
|
1411 |
break; |
|
1412 |
||
1413 |
case 2: |
|
1414 |
// blue spark |
|
1415 |
if ( (v->tick_counter&0x3) == 0 && !IsTrainDepotTile(v->tile) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x5B0) { |
|
1416 |
CreateEffectVehicleRel(v, 0,0,10, EV_SMOKE_2); |
|
1417 |
} |
|
1418 |
break; |
|
1419 |
} |
|
1420 |
} while ( (v = v->next) != NULL ); |
|
1421 |
||
1422 |
} |
|
1423 |
||
1424 |
static void TrainPlayLeaveStationSound(Vehicle *v) |
|
1425 |
{ |
|
541 | 1426 |
static const SoundFx sfx[] = { |
1427 |
SND_04_TRAIN, |
|
1428 |
SND_0A_TRAIN_HORN, |
|
1429 |
SND_0A_TRAIN_HORN |
|
1430 |
}; |
|
1431 |
||
0 | 1432 |
int engtype = v->engine_type; |
1433 |
||
1434 |
switch (_engines[engtype].railtype) { |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
1435 |
case 0: |
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
1436 |
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
|
1437 |
break; |
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
1438 |
case 1: |
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
1439 |
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
|
1440 |
break; |
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
1441 |
case 2: |
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
1442 |
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
|
1443 |
break; |
0 | 1444 |
} |
1445 |
} |
|
1446 |
||
1447 |
static bool CheckTrainStayInDepot(Vehicle *v) |
|
1448 |
{ |
|
1449 |
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
|
1450 |
|
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
|
1451 |
// bail out if not all wagons are in the same depot or not in a depot at all |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
1452 |
for (u = v; u != NULL; u = u->next) |
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
|
1453 |
if (u->u.rail.track != 0x80 || u->tile != v->tile) |
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
|
1454 |
return false; |
0 | 1455 |
|
1456 |
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
|
1457 |
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
|
1458 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 1459 |
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
|
1460 |
} |
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
1461 |
|
0 | 1462 |
v->load_unload_time_rem = 0; |
1463 |
||
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
|
1464 |
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
|
1465 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 1466 |
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
|
1467 |
} |
0 | 1468 |
} |
1469 |
||
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
|
1470 |
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
|
1471 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 1472 |
TrainPlayLeaveStationSound(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1473 |
|
0 | 1474 |
v->u.rail.track = 1; |
1475 |
if (v->direction & 2) |
|
1476 |
v->u.rail.track = 2; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1477 |
|
0 | 1478 |
v->vehstatus &= ~VS_HIDDEN; |
1479 |
v->cur_speed = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1480 |
|
0 | 1481 |
UpdateTrainDeltaXY(v, v->direction); |
1482 |
v->cur_image = GetTrainImage(v, v->direction); |
|
1483 |
VehiclePositionChanged(v); |
|
1484 |
UpdateSignalsOnSegment(v->tile, v->direction); |
|
1485 |
UpdateTrainAcceleration(v); |
|
1486 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
1487 |
||
1488 |
return false; |
|
1489 |
} |
|
1490 |
||
1491 |
typedef struct TrainTrackFollowerData { |
|
1492 |
TileIndex dest_coords; |
|
1493 |
int station_index; // station index we're heading for |
|
1494 |
uint best_bird_dist; |
|
1495 |
uint best_track_dist; |
|
1496 |
byte best_track; |
|
1497 |
} TrainTrackFollowerData; |
|
1498 |
||
1499 |
static bool TrainTrackFollower(uint tile, TrainTrackFollowerData *ttfd, int track, uint length, byte *state){ |
|
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
1500 |
if (IsTileType(tile, MP_RAILWAY) && (_map5[tile]&0xC0) == 0x40) { |
0 | 1501 |
// the tile has a signal |
1502 |
byte m3 = _map3_lo[tile]; |
|
1503 |
if (!(m3 & _signal_onedir[track])) { |
|
1504 |
// if one way signal not pointing towards us, stop going in this direction. |
|
1505 |
if (m3 & _signal_otherdir[track]) |
|
1506 |
return true; |
|
1507 |
} else if (_map2[tile] & _signal_onedir[track]) { |
|
1508 |
// green signal in our direction. either one way or two way. |
|
1509 |
*state = true; |
|
1510 |
} else if (m3 & _signal_otherdir[track]) { |
|
1511 |
// two way signal. unless we passed another green signal on the way, |
|
1512 |
// stop going in this direction. |
|
1513 |
if (!*state) return true; |
|
1514 |
} |
|
1515 |
} |
|
1516 |
||
1517 |
// heading for nowhere? |
|
1518 |
if (ttfd->dest_coords == 0) |
|
1519 |
return false; |
|
1520 |
||
1521 |
// did we reach the final station? |
|
166
683136f1e8a8
(svn r167) -Fix: [972247] bug in '[a] weird non-uniform stations handling'
truelight
parents:
164
diff
changeset
|
1522 |
if ((ttfd->station_index == -1 && tile == ttfd->dest_coords) || |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
1523 |
(IsTileType(tile, MP_STATION) && IS_BYTE_INSIDE(_map5[tile], 0, 8) && _map2[tile] == ttfd->station_index)) { |
166
683136f1e8a8
(svn r167) -Fix: [972247] bug in '[a] weird non-uniform stations handling'
truelight
parents:
164
diff
changeset
|
1524 |
/* We do not check for dest_coords if we have a station_index, |
683136f1e8a8
(svn r167) -Fix: [972247] bug in '[a] weird non-uniform stations handling'
truelight
parents:
164
diff
changeset
|
1525 |
* because in that case the dest_coords are just an |
683136f1e8a8
(svn r167) -Fix: [972247] bug in '[a] weird non-uniform stations handling'
truelight
parents:
164
diff
changeset
|
1526 |
* approximation of where the station is */ |
0 | 1527 |
// found station |
1528 |
ttfd->best_bird_dist = 0; |
|
1529 |
if (length < ttfd->best_track_dist) { |
|
1530 |
ttfd->best_track_dist = length; |
|
1531 |
ttfd->best_track = state[1]; |
|
1532 |
} |
|
1533 |
return true; |
|
1534 |
} else { |
|
1535 |
uint dist; |
|
1536 |
||
1537 |
// we've actually found the destination already. no point searching in directions longer than this. |
|
1538 |
if (ttfd->best_track_dist != (uint)-1) |
|
1539 |
return length >= ttfd->best_track_dist; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1540 |
|
0 | 1541 |
// didn't find station |
1542 |
dist = GetTileDist(tile, ttfd->dest_coords); |
|
1543 |
if (dist < ttfd->best_bird_dist) { |
|
1544 |
ttfd->best_bird_dist = dist; |
|
1545 |
ttfd->best_track = state[1]; |
|
1546 |
} |
|
1547 |
return false; |
|
1548 |
} |
|
1549 |
} |
|
1550 |
||
1551 |
static void FillWithStationData(TrainTrackFollowerData *fd, Vehicle *v) |
|
1552 |
{ |
|
103
3af99b9373a1
(svn r104) Fix: wrong pathfinding when northern station tile is missing (blathijs)
dominik
parents:
98
diff
changeset
|
1553 |
fd->dest_coords = v->dest_tile; |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1554 |
if (v->current_order.type == OT_GOTO_STATION) |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1555 |
fd->station_index = v->current_order.station; |
103
3af99b9373a1
(svn r104) Fix: wrong pathfinding when northern station tile is missing (blathijs)
dominik
parents:
98
diff
changeset
|
1556 |
else |
3af99b9373a1
(svn r104) Fix: wrong pathfinding when northern station tile is missing (blathijs)
dominik
parents:
98
diff
changeset
|
1557 |
fd->station_index = -1; |
3af99b9373a1
(svn r104) Fix: wrong pathfinding when northern station tile is missing (blathijs)
dominik
parents:
98
diff
changeset
|
1558 |
|
0 | 1559 |
} |
1560 |
||
1561 |
static const byte _initial_tile_subcoord[6][4][3] = { |
|
1562 |
{{ 15, 8, 1 },{ 0, 0, 0 },{ 0, 8, 5 },{ 0, 0, 0 }}, |
|
1563 |
{{ 0, 0, 0 },{ 8, 0, 3 },{ 0, 0, 0 },{ 8,15, 7 }}, |
|
1564 |
{{ 0, 0, 0 },{ 7, 0, 2 },{ 0, 7, 6 },{ 0, 0, 0 }}, |
|
1565 |
{{ 15, 8, 2 },{ 0, 0, 0 },{ 0, 0, 0 },{ 8,15, 6 }}, |
|
1566 |
{{ 15, 7, 0 },{ 8, 0, 4 },{ 0, 0, 0 },{ 0, 0, 0 }}, |
|
1567 |
{{ 0, 0, 0 },{ 0, 0, 0 },{ 0, 8, 4 },{ 7,15, 0 }}, |
|
1568 |
}; |
|
1569 |
||
1570 |
static const uint32 _reachable_tracks[4] = { |
|
1571 |
0x10091009, |
|
1572 |
0x00160016, |
|
1573 |
0x05200520, |
|
1574 |
0x2A002A00, |
|
1575 |
}; |
|
1576 |
||
1577 |
static const byte _search_directions[6][4] = { |
|
1578 |
{ 0, 9, 2, 9 }, // track 1 |
|
1579 |
{ 9, 1, 9, 3 }, // track 2 |
|
1580 |
{ 9, 0, 3, 9 }, // track upper |
|
1581 |
{ 1, 9, 9, 2 }, // track lower |
|
1582 |
{ 3, 2, 9, 9 }, // track left |
|
1583 |
{ 9, 9, 1, 0 }, // track right |
|
1584 |
}; |
|
1585 |
||
1586 |
static const byte _pick_track_table[6] = {1, 3, 2, 2, 0, 0}; |
|
1587 |
||
1588 |
/* choose a track */ |
|
1589 |
static byte ChooseTrainTrack(Vehicle *v, uint tile, int direction, byte trackbits) |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1590 |
{ |
0 | 1591 |
TrainTrackFollowerData fd; |
1592 |
int bits = trackbits; |
|
1593 |
uint best_track; |
|
1594 |
#if 0 |
|
1595 |
int time = rdtsc(); |
|
1596 |
static float f; |
|
1597 |
#endif |
|
1598 |
||
1599 |
assert( (bits & ~0x3F) == 0); |
|
1600 |
||
1601 |
/* quick return in case only one possible direction is available */ |
|
1602 |
if (KILL_FIRST_BIT(bits) == 0) |
|
1603 |
return FIND_FIRST_BIT(bits); |
|
1604 |
||
1605 |
FillWithStationData(&fd, v); |
|
1606 |
||
1607 |
if (_patches.new_pathfinding) { |
|
1608 |
fd.best_bird_dist = (uint)-1; |
|
1609 |
fd.best_track_dist = (uint)-1; |
|
1610 |
fd.best_track = 0xFF; |
|
900 | 1611 |
NewTrainPathfind(tile - TileOffsByDir(direction), direction, (TPFEnumProc*)TrainTrackFollower, &fd, NULL); |
0 | 1612 |
|
1613 |
// printf("Train %d %s\n", v->unitnumber, fd.best_track_dist == -1 ? "NOTFOUND" : "FOUND"); |
|
1614 |
||
1615 |
if (fd.best_track == 0xff) { |
|
1616 |
// blaha |
|
1617 |
best_track = FIND_FIRST_BIT(bits); |
|
1618 |
} else { |
|
1619 |
best_track = fd.best_track & 7; |
|
1620 |
} |
|
1621 |
} else { |
|
1622 |
int i, r; |
|
1623 |
uint best_bird_dist = 0; |
|
1624 |
uint best_track_dist = 0; |
|
1625 |
byte train_dir = v->direction & 3; |
|
1626 |
||
1627 |
||
1628 |
best_track = -1; |
|
1629 |
||
1630 |
do { |
|
1631 |
i = FIND_FIRST_BIT(bits); |
|
1632 |
bits = KILL_FIRST_BIT(bits); |
|
1633 |
||
1634 |
fd.best_bird_dist = (uint)-1; |
|
1635 |
fd.best_track_dist = (uint)-1; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1636 |
|
0 | 1637 |
NewTrainPathfind(tile, _search_directions[i][direction], (TPFEnumProc*)TrainTrackFollower, &fd, NULL); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1638 |
if (best_track != (uint)-1) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1639 |
if (best_track_dist == (uint)-1) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1640 |
if (fd.best_track_dist == (uint)-1) { |
0 | 1641 |
/* neither reached the destination, pick the one with the smallest bird dist */ |
1642 |
if (fd.best_bird_dist > best_bird_dist) goto bad; |
|
1643 |
if (fd.best_bird_dist < best_bird_dist) goto good; |
|
1644 |
} else { |
|
1645 |
/* we found the destination for the first time */ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1646 |
goto good; |
0 | 1647 |
} |
1648 |
} else { |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1649 |
if (fd.best_track_dist == (uint)-1) { |
0 | 1650 |
/* didn't find destination, but we've found the destination previously */ |
1651 |
goto bad; |
|
1652 |
} else { |
|
1653 |
/* both old & new reached the destination, compare track length */ |
|
1654 |
if (fd.best_track_dist > best_track_dist) goto bad; |
|
1655 |
if (fd.best_track_dist < best_track_dist) goto good; |
|
1656 |
} |
|
1657 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1658 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1659 |
/* if we reach this position, there's two paths of equal value so far. |
0 | 1660 |
* pick one randomly. */ |
1661 |
r = (byte)Random(); |
|
1662 |
if (_pick_track_table[i] == train_dir) r += 80; |
|
1663 |
if (_pick_track_table[best_track] == train_dir) r -= 80; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1664 |
|
0 | 1665 |
if (r <= 127) goto bad; |
1666 |
} |
|
1667 |
good:; |
|
1668 |
best_track = i; |
|
1669 |
best_bird_dist = fd.best_bird_dist; |
|
1670 |
best_track_dist = fd.best_track_dist; |
|
1671 |
bad:; |
|
1672 |
} while (bits != 0); |
|
1673 |
// printf("Train %d %s\n", v->unitnumber, best_track_dist == -1 ? "NOTFOUND" : "FOUND"); |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1674 |
assert(best_track != (uint)-1); |
0 | 1675 |
} |
1676 |
||
1677 |
#if 0 |
|
1678 |
time = rdtsc() - time; |
|
1679 |
f = f * 0.99 + 0.01 * time; |
|
1680 |
printf("PF time = %d %f\n", time, f); |
|
1681 |
#endif |
|
1682 |
||
1683 |
return best_track; |
|
1684 |
} |
|
1685 |
||
1686 |
||
1687 |
static bool CheckReverseTrain(Vehicle *v) |
|
1688 |
{ |
|
1689 |
TrainTrackFollowerData fd; |
|
1690 |
int i, r; |
|
1691 |
int best_track; |
|
1692 |
uint best_bird_dist = 0; |
|
1693 |
uint best_track_dist = 0; |
|
1694 |
uint reverse, reverse_best; |
|
1695 |
||
1696 |
if (_opt.diff.line_reverse_mode != 0 || |
|
1697 |
v->u.rail.track & 0xC0 || |
|
1698 |
!(v->direction & 1)) |
|
1699 |
return false; |
|
1700 |
||
1701 |
FillWithStationData(&fd, v); |
|
1702 |
||
1703 |
best_track = -1; |
|
1704 |
reverse_best = reverse = 0; |
|
1705 |
||
1706 |
assert(v->u.rail.track); |
|
1707 |
||
1708 |
i = _search_directions[FIND_FIRST_BIT(v->u.rail.track)][v->direction>>1]; |
|
1709 |
||
1710 |
while(true) { |
|
1711 |
fd.best_bird_dist = (uint)-1; |
|
1712 |
fd.best_track_dist = (uint)-1; |
|
1713 |
||
1714 |
NewTrainPathfind(v->tile, reverse ^ i, (TPFEnumProc*)TrainTrackFollower, &fd, NULL); |
|
1715 |
||
1716 |
if (best_track != -1) { |
|
1717 |
if (best_bird_dist != 0) { |
|
1718 |
if (fd.best_bird_dist != 0) { |
|
1719 |
/* neither reached the destination, pick the one with the smallest bird dist */ |
|
1720 |
if (fd.best_bird_dist > best_bird_dist) goto bad; |
|
1721 |
if (fd.best_bird_dist < best_bird_dist) goto good; |
|
1722 |
} else { |
|
1723 |
/* we found the destination for the first time */ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1724 |
goto good; |
0 | 1725 |
} |
1726 |
} else { |
|
1727 |
if (fd.best_bird_dist != 0) { |
|
1728 |
/* didn't find destination, but we've found the destination previously */ |
|
1729 |
goto bad; |
|
1730 |
} else { |
|
1731 |
/* both old & new reached the destination, compare track length */ |
|
1732 |
if (fd.best_track_dist > best_track_dist) goto bad; |
|
1733 |
if (fd.best_track_dist < best_track_dist) goto good; |
|
1734 |
} |
|
1735 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1736 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1737 |
/* if we reach this position, there's two paths of equal value so far. |
0 | 1738 |
* pick one randomly. */ |
1739 |
r = (byte)Random(); |
|
1740 |
if (_pick_track_table[i] == (v->direction & 3)) r += 80; |
|
1741 |
if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80; |
|
1742 |
if (r <= 127) goto bad; |
|
1743 |
} |
|
1744 |
good:; |
|
1745 |
best_track = i; |
|
1746 |
best_bird_dist = fd.best_bird_dist; |
|
1747 |
best_track_dist = fd.best_track_dist; |
|
1748 |
reverse_best = reverse; |
|
1749 |
bad:; |
|
1750 |
if (reverse != 0) |
|
1751 |
break; |
|
1752 |
reverse = 2; |
|
1753 |
} |
|
1754 |
||
1755 |
return reverse_best != 0; |
|
1756 |
} |
|
1757 |
||
1758 |
static bool ProcessTrainOrder(Vehicle *v) |
|
1759 |
{ |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1760 |
const Order *order; |
0 | 1761 |
bool result; |
1762 |
||
1763 |
// These are un-interruptible |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1764 |
if (v->current_order.type >= OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1765 |
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
|
1766 |
// Let a depot order in the orderlist interrupt. |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1767 |
if (v->current_order.type != OT_GOTO_DEPOT || |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1768 |
!(v->current_order.flags & OF_UNLOAD)) |
0 | 1769 |
return false; |
1770 |
} |
|
1771 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1772 |
if (v->current_order.type == OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1773 |
(v->current_order.flags & (OF_UNLOAD | OF_FULL_LOAD)) == (OF_UNLOAD | OF_FULL_LOAD) && |
593 | 1774 |
!VehicleNeedsService(v)) { |
0 | 1775 |
v->cur_order_index++; |
1776 |
} |
|
1777 |
||
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
|
1778 |
// check if we've reached the waypoint? |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1779 |
if (v->current_order.type == OT_GOTO_WAYPOINT && v->tile == v->dest_tile) { |
0 | 1780 |
v->cur_order_index++; |
1781 |
} |
|
1782 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1783 |
// check if we've reached a non-stop station while TTDPatch nonstop is enabled.. |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1784 |
if (_patches.new_nonstop && v->current_order.flags & OF_NON_STOP && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1785 |
v->current_order.station == _map2[v->tile]) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1786 |
v->cur_order_index++; |
0 | 1787 |
} |
1788 |
||
1789 |
// Get the current order |
|
1790 |
if (v->cur_order_index >= v->num_orders) |
|
1791 |
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
|
1792 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1793 |
order = GetVehicleOrder(v, v->cur_order_index); |
0 | 1794 |
|
1795 |
// 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
|
1796 |
if (order == NULL) { |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1797 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1798 |
v->current_order.flags = 0; |
0 | 1799 |
v->dest_tile = 0; |
1800 |
return false; |
|
1801 |
} |
|
1802 |
||
1803 |
// 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
|
1804 |
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
|
1805 |
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
|
1806 |
order->station == v->current_order.station) |
0 | 1807 |
return false; |
1808 |
||
1809 |
// 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
|
1810 |
v->current_order = *order; |
0 | 1811 |
|
1812 |
v->dest_tile = 0; |
|
1813 |
||
1814 |
result = false; |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1815 |
switch (order->type) { |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1816 |
case OT_GOTO_STATION: |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1817 |
if (order->station == v->last_station_visited) |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1818 |
v->last_station_visited = 0xFFFF; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1819 |
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
|
1820 |
result = CheckReverseTrain(v); |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1821 |
break; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1822 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1823 |
case OT_GOTO_DEPOT: |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1824 |
v->dest_tile = _depots[order->station].xy; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1825 |
result = CheckReverseTrain(v); |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1826 |
break; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1827 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1828 |
case OT_GOTO_WAYPOINT: |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1829 |
v->dest_tile = _waypoints[order->station].xy; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1830 |
result = CheckReverseTrain(v); |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1831 |
break; |
0 | 1832 |
} |
1833 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1834 |
InvalidateVehicleOrder(v); |
0 | 1835 |
|
1836 |
return result; |
|
1837 |
} |
|
1838 |
||
1839 |
static void MarkTrainDirty(Vehicle *v) |
|
1840 |
{ |
|
1841 |
do { |
|
1842 |
v->cur_image = GetTrainImage(v, v->direction); |
|
1843 |
MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1); |
|
1844 |
} while ( (v=v->next) != NULL); |
|
1845 |
} |
|
1846 |
||
1847 |
static void HandleTrainLoading(Vehicle *v, bool mode) |
|
1848 |
{ |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1849 |
if (v->current_order.type == OT_NOTHING) |
0 | 1850 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1851 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1852 |
if (v->current_order.type != OT_DUMMY) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1853 |
if (v->current_order.type != OT_LOADING) |
0 | 1854 |
return; |
1855 |
||
1856 |
if (mode) |
|
1857 |
return; |
|
1858 |
||
1859 |
// 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
|
1860 |
if (v->current_order.flags & OF_NON_STOP) |
0 | 1861 |
v->u.rail.days_since_order_progr = 0; |
1862 |
||
1863 |
if (--v->load_unload_time_rem) |
|
1864 |
return; |
|
1865 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1866 |
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
|
1867 |
v->u.rail.days_since_order_progr = 0; /* Prevent a train lost message for full loading trains */ |
0 | 1868 |
SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC); |
1869 |
if (LoadUnloadVehicle(v)) { |
|
1870 |
InvalidateWindow(WC_TRAINS_LIST, v->owner); |
|
1871 |
MarkTrainDirty(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1872 |
|
0 | 1873 |
// need to update acceleration since the goods on the train changed. |
1874 |
UpdateTrainAcceleration(v); |
|
1875 |
} |
|
1876 |
return; |
|
1877 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1878 |
|
0 | 1879 |
TrainPlayLeaveStationSound(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1880 |
|
0 | 1881 |
{ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1882 |
Order b = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1883 |
v->current_order.type = OT_LEAVESTATION; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1884 |
v->current_order.flags = 0; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1885 |
|
0 | 1886 |
// If this was not the final order, don't remove it from the list. |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1887 |
if (!(b.flags & OF_NON_STOP)) |
0 | 1888 |
return; |
1889 |
} |
|
1890 |
} |
|
1891 |
||
1892 |
v->u.rail.days_since_order_progr = 0; |
|
1893 |
v->cur_order_index++; |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1894 |
InvalidateVehicleOrder(v); |
0 | 1895 |
} |
1896 |
||
1897 |
static int UpdateTrainSpeed(Vehicle *v) |
|
1898 |
{ |
|
1899 |
uint spd; |
|
1900 |
uint accel; |
|
1901 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1902 |
if (v->vehstatus & VS_STOPPED || HASBIT(v->u.rail.flags, VRF_REVERSING)) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1903 |
if (_patches.realistic_acceleration) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1904 |
accel = GetTrainAcceleration(v, AM_BRAKE) * 2; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1905 |
else |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1906 |
accel = v->acceleration * -2; |
0 | 1907 |
} else { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1908 |
if (_patches.realistic_acceleration) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1909 |
accel = GetTrainAcceleration(v, AM_ACCEL); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1910 |
else |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1911 |
accel = v->acceleration; |
0 | 1912 |
} |
1913 |
||
1914 |
spd = v->subspeed + accel * 2; |
|
1915 |
v->subspeed = (byte)spd; |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1916 |
{ |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1917 |
int tempmax = v->max_speed; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1918 |
if (v->cur_speed > v->max_speed) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1919 |
tempmax = v->cur_speed - (v->cur_speed / 10) - 1; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
1920 |
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
|
1921 |
} |
0 | 1922 |
|
1923 |
if (!(v->direction & 1)) spd = spd * 3 >> 2; |
|
1924 |
||
1925 |
spd += v->progress; |
|
1926 |
v->progress = (byte)spd; |
|
1927 |
return (spd >> 8); |
|
1928 |
} |
|
1929 |
||
1930 |
static void TrainEnterStation(Vehicle *v, int station) |
|
1931 |
{ |
|
1932 |
Station *st; |
|
1933 |
uint32 flags; |
|
1934 |
||
1935 |
v->last_station_visited = station; |
|
1936 |
||
1937 |
/* 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
|
1938 |
st = GetStation(station); |
0 | 1939 |
if (!(st->had_vehicle_of_type & HVOT_TRAIN)) { |
1940 |
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
|
1941 |
SetDParam(0, st->index); |
0 | 1942 |
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); |
1943 |
AddNewsItem( |
|
1944 |
STR_8801_CITIZENS_CELEBRATE_FIRST, |
|
1945 |
flags, |
|
1946 |
v->index, |
|
1947 |
0); |
|
1948 |
} |
|
1949 |
||
1950 |
// Did we reach the final destination? |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1951 |
if (v->current_order.type == OT_GOTO_STATION && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1952 |
v->current_order.station == (byte)station) { |
0 | 1953 |
// Yeah, keep the load/unload flags |
1954 |
// 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
|
1955 |
v->current_order.type = OT_LOADING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1956 |
v->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1957 |
v->current_order.flags |= OF_NON_STOP; |
0 | 1958 |
} else { |
1959 |
// No, just do a simple load |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1960 |
v->current_order.type = OT_LOADING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1961 |
v->current_order.flags = 0; |
0 | 1962 |
} |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1963 |
v->current_order.station = 0; |
0 | 1964 |
|
1965 |
SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC); |
|
1966 |
if (LoadUnloadVehicle(v) != 0) { |
|
1967 |
InvalidateWindow(WC_TRAINS_LIST, v->owner); |
|
1968 |
MarkTrainDirty(v); |
|
1969 |
UpdateTrainAcceleration(v); |
|
1970 |
} |
|
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
|
1971 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1972 |
} |
1973 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1974 |
static byte AfterSetTrainPos(Vehicle *v, bool new_tile) |
0 | 1975 |
{ |
1976 |
byte new_z, old_z; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1977 |
|
0 | 1978 |
// need this hint so it returns the right z coordinate on bridges. |
1979 |
_get_z_hint = v->z_pos; |
|
1980 |
new_z = GetSlopeZ(v->x_pos, v->y_pos); |
|
1981 |
_get_z_hint = 0; |
|
1982 |
||
1983 |
old_z = v->z_pos; |
|
1984 |
v->z_pos = new_z; |
|
1985 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1986 |
if (new_tile) { |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1987 |
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
|
1988 |
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
|
1989 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1990 |
if (new_z != old_z) { |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1991 |
SETBIT(v->u.rail.flags, (new_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1992 |
} |
0 | 1993 |
} |
1994 |
||
1995 |
VehiclePositionChanged(v); |
|
1996 |
EndVehicleMove(v); |
|
1997 |
return old_z; |
|
1998 |
} |
|
1999 |
||
2000 |
static const byte _new_vehicle_direction_table[11] = { |
|
2001 |
0, 7, 6, 0, |
|
2002 |
1, 0, 5, 0, |
|
2003 |
2, 3, 4, |
|
2004 |
}; |
|
2005 |
||
2006 |
static int GetNewVehicleDirectionByTile(uint new_tile, uint old_tile) |
|
2007 |
{ |
|
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
|
2008 |
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
|
2009 |
TileX(new_tile) - TileX(old_tile) + 1; |
0 | 2010 |
assert(offs < 11); |
2011 |
return _new_vehicle_direction_table[offs]; |
|
2012 |
} |
|
2013 |
||
2014 |
static int GetNewVehicleDirection(Vehicle *v, int x, int y) |
|
2015 |
{ |
|
2016 |
uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1); |
|
2017 |
assert(offs < 11); |
|
2018 |
return _new_vehicle_direction_table[offs]; |
|
2019 |
} |
|
2020 |
||
2021 |
static int GetDirectionToVehicle(Vehicle *v, int x, int y) |
|
2022 |
{ |
|
2023 |
byte offs; |
|
2024 |
||
2025 |
x -= v->x_pos; |
|
2026 |
if (x >= 0) { |
|
2027 |
offs = (x > 2) ? 0 : 1; |
|
2028 |
} else { |
|
2029 |
offs = (x < -2) ? 2 : 1; |
|
2030 |
} |
|
2031 |
||
2032 |
y -= v->y_pos; |
|
2033 |
if (y >= 0) { |
|
2034 |
offs += ((y > 2) ? 0 : 1) * 4; |
|
2035 |
} else { |
|
2036 |
offs += ((y < -2) ? 2 : 1) * 4; |
|
2037 |
} |
|
2038 |
||
2039 |
assert(offs < 11); |
|
2040 |
return _new_vehicle_direction_table[offs]; |
|
2041 |
} |
|
2042 |
||
2043 |
/* Check if the vehicle is compatible with the specified tile */ |
|
1048 | 2044 |
static bool CheckCompatibleRail(const Vehicle *v, TileIndex tile) |
0 | 2045 |
{ |
1048 | 2046 |
switch (TileType(tile)) { |
2047 |
case MP_RAILWAY: |
|
2048 |
case MP_STATION: |
|
2049 |
// normal tracks, jump to owner check |
|
2050 |
break; |
|
2051 |
||
2052 |
case MP_TUNNELBRIDGE: |
|
2053 |
if ((_map5[tile] & 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
|
2054 |
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
|
2055 |
uint tileh = GetTileSlope(tile, &height); |
1048 | 2056 |
|
2057 |
// correct Z position of a train going under a bridge on slopes |
|
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
|
2058 |
if (CORRECT_Z(tileh)) height += 8; |
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
|
2059 |
|
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
|
2060 |
if (v->z_pos != height) return true; // train is going over bridge |
1048 | 2061 |
} |
2062 |
break; |
|
2063 |
||
2064 |
case MP_STREET: |
|
2065 |
// tracks over roads, do owner check of tracks (_map_owner[tile]) |
|
2066 |
return |
|
2067 |
_map_owner[tile] == v->owner && |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2068 |
(v->subtype != TS_Front_Engine || (_map3_hi[tile] & 0xF) == v->u.rail.railtype); |
1048 | 2069 |
|
2070 |
default: |
|
2071 |
return true; |
|
2072 |
} |
|
2073 |
||
2074 |
return |
|
2075 |
_map_owner[tile] == v->owner && |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2076 |
(v->subtype != TS_Front_Engine || (_map3_lo[tile] & 0xF) == v->u.rail.railtype); |
0 | 2077 |
} |
2078 |
||
2079 |
typedef struct { |
|
2080 |
byte small_turn, large_turn; |
|
2081 |
byte z_up; // fraction to remove when moving up |
|
2082 |
byte z_down; // fraction to remove when moving down |
|
2083 |
} RailtypeSlowdownParams; |
|
2084 |
||
2085 |
static const RailtypeSlowdownParams _railtype_slowdown[3] = { |
|
2086 |
// normal accel |
|
2087 |
{256/4, 256/2, 256/4, 2}, // normal |
|
2088 |
{256/4, 256/2, 256/4, 2}, // monorail |
|
2089 |
{0, 256/2, 256/4, 2}, // maglev |
|
2090 |
}; |
|
2091 |
||
2092 |
/* Modify the speed of the vehicle due to a turn */ |
|
2093 |
static void AffectSpeedByDirChange(Vehicle *v, byte new_dir) |
|
2094 |
{ |
|
2095 |
byte diff; |
|
2096 |
const RailtypeSlowdownParams *rsp; |
|
2097 |
||
2098 |
if (_patches.realistic_acceleration || (diff = (v->direction - new_dir) & 7) == 0) |
|
2099 |
return; |
|
2100 |
||
2101 |
rsp = &_railtype_slowdown[v->u.rail.railtype]; |
|
2102 |
v->cur_speed -= ((diff == 1 || diff == 7) ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8; |
|
2103 |
} |
|
2104 |
||
2105 |
/* Modify the speed of the vehicle due to a change in altitude */ |
|
2106 |
static void AffectSpeedByZChange(Vehicle *v, byte old_z) |
|
2107 |
{ |
|
2108 |
const RailtypeSlowdownParams *rsp; |
|
2109 |
if (old_z == v->z_pos || _patches.realistic_acceleration) |
|
2110 |
return; |
|
2111 |
||
2112 |
rsp = &_railtype_slowdown[v->u.rail.railtype]; |
|
2113 |
||
2114 |
if (old_z < v->z_pos) { |
|
2115 |
v->cur_speed -= (v->cur_speed * rsp->z_up >> 8); |
|
2116 |
} else { |
|
2117 |
uint16 spd = v->cur_speed + rsp->z_down; |
|
2118 |
if (spd <= v->max_speed) |
|
2119 |
v->cur_speed = spd; |
|
2120 |
} |
|
2121 |
} |
|
2122 |
||
2123 |
static const byte _otherside_signal_directions[14] = { |
|
2124 |
1, 3, 1, 3, 5, 3, 0, 0, |
|
2125 |
5, 7, 7, 5, 7, 1, |
|
2126 |
}; |
|
2127 |
||
2128 |
static void TrainMovedChangeSignals(uint tile, int dir) |
|
2129 |
{ |
|
2130 |
int i; |
|
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
2131 |
if (IsTileType(tile, MP_RAILWAY) && (_map5[tile] & 0xC0) == 0x40) { |
0 | 2132 |
i = FindFirstBit2x64((_map5[tile]+(_map5[tile]<<8)) & _reachable_tracks[dir]); |
2133 |
UpdateSignalsOnSegment(tile, _otherside_signal_directions[i]); |
|
2134 |
} |
|
2135 |
} |
|
2136 |
||
2137 |
||
2138 |
typedef struct TrainCollideChecker { |
|
2139 |
Vehicle *v, *v_skip; |
|
2140 |
||
2141 |
} TrainCollideChecker; |
|
2142 |
||
1095 | 2143 |
static void *FindTrainCollideEnum(Vehicle *v, TrainCollideChecker *tcc) |
0 | 2144 |
{ |
2145 |
if (v == tcc->v || v == tcc->v_skip || v->type != VEH_Train || v->u.rail.track==0x80) |
|
2146 |
return 0; |
|
2147 |
||
2148 |
if ( myabs(v->z_pos - tcc->v->z_pos) > 6 || |
|
2149 |
myabs(v->x_pos - tcc->v->x_pos) >= 6 || |
|
2150 |
myabs(v->y_pos - tcc->v->y_pos) >= 6) |
|
2151 |
return NULL; |
|
2152 |
return v; |
|
2153 |
} |
|
2154 |
||
2155 |
static void SetVehicleCrashed(Vehicle *v) |
|
2156 |
{ |
|
2157 |
Vehicle *u; |
|
2158 |
||
2159 |
if (v->u.rail.crash_anim_pos != 0) |
|
2160 |
return; |
|
2161 |
||
2162 |
v->u.rail.crash_anim_pos++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2163 |
|
0 | 2164 |
u = v; |
2165 |
BEGIN_ENUM_WAGONS(v) |
|
2166 |
v->vehstatus |= VS_CRASHED; |
|
2167 |
END_ENUM_WAGONS(v) |
|
2168 |
||
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
|
2169 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, u->index, STATUS_BAR); |
0 | 2170 |
} |
2171 |
||
2172 |
static int CountPassengersInTrain(Vehicle *v) |
|
2173 |
{ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2174 |
int num = 0; |
0 | 2175 |
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
|
2176 |
if (v->cargo_type == CT_PASSENGERS) num += v->cargo_count; |
0 | 2177 |
END_ENUM_WAGONS(v) |
2178 |
return num; |
|
2179 |
} |
|
2180 |
||
22 | 2181 |
/* |
2182 |
* Checks whether the specified tried has a collision with another vehicle. If |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2183 |
* so, destroys this vehicle, and the other vehicle if its subtype is 0 (TS_Front_Engine). |
22 | 2184 |
* Reports the incident in a flashy news item, modifies station ratings and |
2185 |
* plays a sound. |
|
2186 |
*/ |
|
0 | 2187 |
static void CheckTrainCollision(Vehicle *v) |
2188 |
{ |
|
2189 |
TrainCollideChecker tcc; |
|
98 | 2190 |
Vehicle *coll,*realcoll; |
0 | 2191 |
int num; |
2192 |
||
2193 |
/* can't collide in depot */ |
|
2194 |
if (v->u.rail.track == 0x80) |
|
2195 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2196 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2197 |
if ( !(v->u.rail.track == 0x40) ) |
98 | 2198 |
assert((uint)TILE_FROM_XY(v->x_pos, v->y_pos) == v->tile); |
0 | 2199 |
|
2200 |
tcc.v = v; |
|
2201 |
tcc.v_skip = v->next; |
|
2202 |
||
2203 |
/* find colliding vehicle */ |
|
98 | 2204 |
realcoll = coll = VehicleFromPos(TILE_FROM_XY(v->x_pos, v->y_pos), &tcc, (VehicleFromPosProc*)FindTrainCollideEnum); |
0 | 2205 |
if (coll == NULL) |
2206 |
return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2207 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2208 |
|
0 | 2209 |
coll = GetFirstVehicleInChain(coll); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2210 |
|
0 | 2211 |
/* it can't collide with its own wagons */ |
98 | 2212 |
if ( (v == coll) || ( (v->u.rail.track & 0x40) && ( (v->direction & 2) != (realcoll->direction & 2) ) ) ) |
0 | 2213 |
return; |
2214 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2215 |
//two drivers + passangers killed in train v |
0 | 2216 |
num = 2 + CountPassengersInTrain(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2217 |
if(!(coll->vehstatus&VS_CRASHED)) |
0 | 2218 |
//two drivers + passangers killed in train coll (if it was not crashed already) |
2219 |
num += 2 + CountPassengersInTrain(coll); |
|
2220 |
||
2221 |
SetVehicleCrashed(v); |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2222 |
if (coll->subtype == TS_Front_Engine) |
0 | 2223 |
SetVehicleCrashed(coll); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2224 |
|
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2225 |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
2226 |
SetDParam(0, num); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2227 |
|
0 | 2228 |
AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL, |
2229 |
NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0), |
|
2230 |
v->index, |
|
2231 |
0); |
|
2232 |
||
2233 |
ModifyStationRatingAround(v->tile, v->owner, -160, 30); |
|
541 | 2234 |
SndPlayVehicleFx(SND_13_BIG_CRASH, v); |
0 | 2235 |
} |
2236 |
||
2237 |
static void *CheckVehicleAtSignal(Vehicle *v, void *data) |
|
2238 |
{ |
|
2239 |
uint32 d = (uint32)data; |
|
2240 |
||
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2241 |
if (v->type == VEH_Train && v->subtype == TS_Front_Engine && v->tile == (TileIndex)(d >> 8)) { |
0 | 2242 |
byte diff = (v->direction - (byte)d + 2) & 7; |
2243 |
if (diff == 2 || (v->cur_speed <= 5 && diff <= 4)) |
|
2244 |
return (void*)1; |
|
2245 |
} |
|
2246 |
return 0; |
|
2247 |
} |
|
2248 |
||
2249 |
static void TrainController(Vehicle *v) |
|
2250 |
{ |
|
2251 |
Vehicle *prev = NULL; |
|
2252 |
GetNewVehiclePosResult gp; |
|
2253 |
uint32 r, tracks,ts; |
|
2254 |
int dir, i; |
|
2255 |
byte chosen_dir; |
|
2256 |
byte chosen_track; |
|
2257 |
byte old_z; |
|
2258 |
||
22 | 2259 |
/* For every vehicle after and including the given vehicle */ |
0 | 2260 |
for(;;) { |
2261 |
BeginVehicleMove(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2262 |
|
0 | 2263 |
if (v->u.rail.track != 0x40) { |
22 | 2264 |
/* Not inside tunnel */ |
0 | 2265 |
if (GetNewVehiclePos(v, &gp)) { |
22 | 2266 |
/* Staying in the old tile */ |
0 | 2267 |
if (v->u.rail.track == 0x80) { |
2268 |
/* inside depot */ |
|
2269 |
gp.x = v->x_pos; |
|
2270 |
gp.y = v->y_pos; |
|
2271 |
} else { |
|
22 | 2272 |
/* 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
|
2273 |
|
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2274 |
if (!TrainCheckIfLineEnds(v)) |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2275 |
return; |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2276 |
|
0 | 2277 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
2278 |
if (r & 0x8) |
|
2279 |
goto invalid_rail; |
|
2280 |
if (r & 0x2) { |
|
2281 |
TrainEnterStation(v, r >> 8); |
|
2282 |
return; |
|
2283 |
} |
|
2284 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2285 |
if (v->current_order.type == OT_LEAVESTATION) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2286 |
v->current_order.type = OT_NOTHING; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2287 |
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
|
2288 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 2289 |
} |
2290 |
} |
|
2291 |
} else { |
|
2292 |
/* A new tile is about to be entered. */ |
|
2293 |
||
2294 |
/* Determine what direction we're entering the new tile from */ |
|
2295 |
dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile); |
|
2296 |
assert(dir==1 || dir==3 || dir==5 || dir==7); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2297 |
|
0 | 2298 |
/* Get the status of the tracks in the new tile and mask |
2299 |
* away the bits that aren't reachable. */ |
|
159
139cf78bfb28
(svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents:
156
diff
changeset
|
2300 |
ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL) & _reachable_tracks[dir >> 1]; |
0 | 2301 |
|
2302 |
/* Combine the from & to directions. |
|
2303 |
* Now, the lower byte contains the track status, and the byte at bit 16 contains |
|
2304 |
* the signal status. */ |
|
2305 |
tracks = ts|(ts >> 8); |
|
2306 |
if ( (byte) tracks == 0) |
|
2307 |
goto invalid_rail; |
|
2308 |
||
2309 |
/* Check if the new tile contrains tracks that are compatible |
|
2310 |
* with the current train, if not, bail out. */ |
|
2311 |
if (!CheckCompatibleRail(v, gp.new_tile)) |
|
2312 |
goto invalid_rail; |
|
2313 |
||
2314 |
if (prev == NULL) { |
|
2315 |
/* Currently the locomotive is active. Determine which one of the |
|
2316 |
* available tracks to choose */ |
|
2317 |
chosen_track = 1 << ChooseTrainTrack(v, gp.new_tile, dir>>1, (byte)tracks); |
|
2318 |
||
2319 |
/* Check if it's a red signal and that force proceed is not clicked. */ |
|
2320 |
if ( (tracks>>16)&chosen_track && v->u.rail.force_proceed == 0) goto red_light; |
|
2321 |
} else { |
|
2322 |
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
|
2323 |
|
0 | 2324 |
/* The wagon is active, simply follow the prev vehicle. */ |
2325 |
chosen_track = (byte)(_matching_tracks[GetDirectionToVehicle(prev, gp.x, gp.y)] & tracks); |
|
2326 |
} |
|
2327 |
||
2328 |
/* make sure chosen track is a valid track */ |
|
2329 |
assert(chosen_track==1 || chosen_track==2 || chosen_track==4 || chosen_track==8 || chosen_track==16 || chosen_track==32); |
|
2330 |
||
2331 |
/* Update XY to reflect the entrance to the new tile, and select the direction to use */ |
|
2332 |
{ |
|
2333 |
const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][dir>>1]; |
|
2334 |
gp.x = (gp.x & ~0xF) | b[0]; |
|
2335 |
gp.y = (gp.y & ~0xF) | b[1]; |
|
2336 |
chosen_dir = b[2]; |
|
2337 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2338 |
|
0 | 2339 |
/* Call the landscape function and tell it that the vehicle entered the tile */ |
2340 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
2341 |
if (r&0x8) |
|
2342 |
goto invalid_rail; |
|
2343 |
||
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2344 |
if (v->subtype == TS_Front_Engine) v->load_unload_time_rem = 0; |
0 | 2345 |
|
2346 |
if (!(r&0x4)) { |
|
2347 |
v->tile = gp.new_tile; |
|
2348 |
v->u.rail.track = chosen_track; |
|
2349 |
} |
|
2350 |
||
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2351 |
if (v->subtype == TS_Front_Engine) |
0 | 2352 |
TrainMovedChangeSignals(gp.new_tile, dir>>1); |
2353 |
||
22 | 2354 |
/* Signals can only change when the first |
2355 |
* (above) or the last vehicle moves. */ |
|
0 | 2356 |
if (v->next == NULL) |
2357 |
TrainMovedChangeSignals(gp.old_tile, (dir>>1) ^ 2); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2358 |
|
0 | 2359 |
if (prev == NULL) { |
2360 |
AffectSpeedByDirChange(v, chosen_dir); |
|
2361 |
} |
|
2362 |
||
2363 |
v->direction = chosen_dir; |
|
2364 |
} |
|
2365 |
} else { |
|
2366 |
/* in tunnel */ |
|
2367 |
GetNewVehiclePos(v, &gp); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2368 |
|
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
2369 |
if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && |
0 | 2370 |
!(_map5[gp.new_tile] & 0xF0)) { |
2371 |
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
|
2372 |
if (r & 0x4) goto common; |
|
2373 |
} |
|
2374 |
||
2375 |
v->x_pos = gp.x; |
|
2376 |
v->y_pos = gp.y; |
|
2377 |
VehiclePositionChanged(v); |
|
98 | 2378 |
if (prev == NULL) |
2379 |
CheckTrainCollision(v); |
|
0 | 2380 |
goto next_vehicle; |
2381 |
} |
|
2382 |
common:; |
|
2383 |
||
2384 |
/* update image of train, as well as delta XY */ |
|
2385 |
dir = GetNewVehicleDirection(v, gp.x, gp.y); |
|
2386 |
UpdateTrainDeltaXY(v, dir); |
|
2387 |
v->cur_image = GetTrainImage(v, dir); |
|
2388 |
||
2389 |
v->x_pos = gp.x; |
|
2390 |
v->y_pos = gp.y; |
|
2391 |
||
2392 |
/* 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
|
2393 |
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
|
2394 |
|
0 | 2395 |
if (prev == NULL) { |
22 | 2396 |
/* This is the first vehicle in the train */ |
0 | 2397 |
AffectSpeedByZChange(v, old_z); |
2398 |
CheckTrainCollision(v); |
|
2399 |
} |
|
2400 |
||
22 | 2401 |
next_vehicle:; |
0 | 2402 |
/* continue with next vehicle */ |
2403 |
prev = v; |
|
2404 |
if ((v=v->next) == NULL) |
|
2405 |
return; |
|
2406 |
} |
|
2407 |
||
2408 |
invalid_rail: |
|
22 | 2409 |
/* We've reached end of line?? */ |
0 | 2410 |
if (prev != NULL) { |
2411 |
error("!Disconnecting train"); |
|
2412 |
} |
|
2413 |
goto reverse_train_direction; |
|
2414 |
||
2415 |
red_light: { |
|
22 | 2416 |
/* We're in front of a red signal ?? */ |
0 | 2417 |
/* find the first set bit in ts. need to do it in 2 steps, since |
2418 |
* FIND_FIRST_BIT only handles 6 bits at a time. */ |
|
2419 |
i = FindFirstBit2x64(ts); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2420 |
|
0 | 2421 |
if (!(_map3_lo[gp.new_tile] & _signal_otherdir[i])) { |
2422 |
v->cur_speed = 0; |
|
2423 |
v->subspeed = 0; |
|
2424 |
v->progress = 255-100; |
|
2425 |
if (++v->load_unload_time_rem < _patches.wait_oneway_signal * 20) |
|
2426 |
return; |
|
2427 |
} else if (_map3_lo[gp.new_tile] & _signal_onedir[i]){ |
|
2428 |
v->cur_speed = 0; |
|
2429 |
v->subspeed = 0; |
|
2430 |
v->progress = 255-10; |
|
2431 |
if (++v->load_unload_time_rem < _patches.wait_twoway_signal * 73) { |
|
900 | 2432 |
uint o_tile = gp.new_tile + TileOffsByDir(dir >> 1); |
0 | 2433 |
/* check if a train is waiting on the other side */ |
2434 |
if (VehicleFromPos(o_tile, (void*)( (o_tile<<8) | (dir^4)), (VehicleFromPosProc*)CheckVehicleAtSignal) == NULL) |
|
2435 |
return; |
|
2436 |
} |
|
2437 |
} |
|
2438 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2439 |
|
0 | 2440 |
reverse_train_direction: |
2441 |
v->load_unload_time_rem = 0; |
|
2442 |
v->cur_speed = 0; |
|
2443 |
v->subspeed = 0; |
|
2444 |
ReverseTrainDirection(v); |
|
2445 |
||
2446 |
} |
|
2447 |
||
98 | 2448 |
extern uint CheckTunnelBusy(uint tile, int *length); |
2449 |
||
0 | 2450 |
static void DeleteLastWagon(Vehicle *v) |
2451 |
{ |
|
2452 |
Vehicle *u = v; |
|
2453 |
int t; |
|
2454 |
||
2455 |
while (v->next != NULL) { |
|
2456 |
u = v; |
|
2457 |
v = v->next; |
|
2458 |
} |
|
2459 |
u->next = NULL; |
|
2460 |
||
2461 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
2462 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
|
588 | 2463 |
RebuildVehicleLists(); |
0 | 2464 |
InvalidateWindow(WC_COMPANY, v->owner); |
2465 |
||
2466 |
BeginVehicleMove(v); |
|
2467 |
EndVehicleMove(v); |
|
2468 |
DeleteVehicle(v); |
|
2469 |
||
2470 |
if (!((t=v->u.rail.track) & 0xC0)) { |
|
2471 |
SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(t)); |
|
2472 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2473 |
|
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
2474 |
/* Check if the wagon was on a road/rail-crossing and disable it if no others are on it */ |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
2475 |
DisableTrainCrossing(v->tile); |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
2476 |
|
98 | 2477 |
if (v->u.rail.track == 0x40) { |
2478 |
int length; |
|
2479 |
TileIndex endtile = CheckTunnelBusy(v->tile, &length); |
|
2480 |
if ((v->direction == 1) || (v->direction == 5) ) |
|
2481 |
SetSignalsOnBothDir(v->tile,0); |
|
2482 |
SetSignalsOnBothDir(endtile,0); |
|
2483 |
if ((v->direction == 3) || (v->direction == 7) ) |
|
2484 |
SetSignalsOnBothDir(v->tile,1); |
|
2485 |
SetSignalsOnBothDir(endtile,1); |
|
2486 |
} |
|
0 | 2487 |
} |
2488 |
||
2489 |
static void ChangeTrainDirRandomly(Vehicle *v) |
|
2490 |
{ |
|
2491 |
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
|
2492 |
|
0 | 2493 |
do { |
98 | 2494 |
//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
|
2495 |
if (!(v->u.rail.track & 0x40)) |
1137
c492b777af49
(svn r1638) -Fix: Train crashes should no longer desync the game. This is more of a
celestar
parents:
1132
diff
changeset
|
2496 |
v->direction = (v->direction + _random_dir_change[Random()&3]) & 7; |
0 | 2497 |
if (!(v->vehstatus & VS_HIDDEN)) { |
2498 |
BeginVehicleMove(v); |
|
2499 |
UpdateTrainDeltaXY(v, v->direction); |
|
2500 |
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
|
2501 |
AfterSetTrainPos(v, false); |
0 | 2502 |
} |
2503 |
} while ( (v=v->next) != NULL); |
|
2504 |
} |
|
2505 |
||
2506 |
static void HandleCrashedTrain(Vehicle *v) |
|
2507 |
{ |
|
2508 |
int state = ++v->u.rail.crash_anim_pos, index; |
|
2509 |
uint32 r; |
|
2510 |
Vehicle *u; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2511 |
|
98 | 2512 |
if ( (state == 4) && (v->u.rail.track != 0x40) ) { |
0 | 2513 |
CreateEffectVehicleRel(v, 4, 4, 8, EV_CRASHED_SMOKE); |
2514 |
} |
|
2515 |
||
1137
c492b777af49
(svn r1638) -Fix: Train crashes should no longer desync the game. This is more of a
celestar
parents:
1132
diff
changeset
|
2516 |
if (state <= 200 && (uint16)(r=Random()) <= 0x2492) { |
0 | 2517 |
index = (r * 10 >> 16); |
2518 |
||
2519 |
u = v; |
|
2520 |
do { |
|
2521 |
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
|
2522 |
r = Random(); |
0 | 2523 |
|
2524 |
CreateEffectVehicleRel(u, |
|
2525 |
2 + ((r>>8)&7), |
|
2526 |
2 + ((r>>16)&7), |
|
2527 |
5 + (r&7), |
|
2528 |
EV_DEMOLISH); |
|
2529 |
break; |
|
2530 |
} |
|
2531 |
} while ( (u=u->next) != NULL); |
|
2532 |
} |
|
2533 |
||
2534 |
if (state <= 240 && !(v->tick_counter&3)) { |
|
2535 |
ChangeTrainDirRandomly(v); |
|
2536 |
} |
|
2537 |
||
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
|
2538 |
if (state >= 4440 && !(v->tick_counter&0x1F)) { |
0 | 2539 |
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
|
2540 |
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
|
2541 |
} |
0 | 2542 |
} |
2543 |
||
2544 |
static void HandleBrokenTrain(Vehicle *v) |
|
2545 |
{ |
|
2546 |
if (v->breakdown_ctr != 1) { |
|
2547 |
v->breakdown_ctr = 1; |
|
2548 |
v->cur_speed = 0; |
|
2549 |
||
2550 |
if (v->breakdowns_since_last_service != 255) |
|
2551 |
v->breakdowns_since_last_service++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2552 |
|
0 | 2553 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
2554 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2555 |
|
541 | 2556 |
SndPlayVehicleFx((_opt.landscape != LT_CANDY) ? |
2557 |
SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v); |
|
0 | 2558 |
|
2559 |
if (!(v->vehstatus & VS_HIDDEN)) { |
|
2560 |
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); |
|
2561 |
if (u) |
|
2562 |
u->u.special.unk0 = v->breakdown_delay * 2; |
|
2563 |
} |
|
2564 |
} |
|
2565 |
||
2566 |
if (!(v->tick_counter & 3)) { |
|
2567 |
if (!--v->breakdown_delay) { |
|
2568 |
v->breakdown_ctr = 0; |
|
2569 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
|
2570 |
} |
|
2571 |
} |
|
2572 |
} |
|
2573 |
||
2574 |
static const byte _breakdown_speeds[16] = { |
|
2575 |
225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15 |
|
2576 |
}; |
|
2577 |
||
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2578 |
static bool TrainCheckIfLineEnds(Vehicle *v) |
0 | 2579 |
{ |
2580 |
uint tile; |
|
2581 |
uint x,y; |
|
2582 |
int t; |
|
2583 |
uint32 ts; |
|
2584 |
||
2585 |
if ((uint)(t=v->breakdown_ctr) > 1) { |
|
2586 |
v->vehstatus |= VS_TRAIN_SLOWING; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2587 |
|
0 | 2588 |
t = _breakdown_speeds[ ((~t) >> 4) & 0xF]; |
2589 |
if ((uint16)t <= v->cur_speed) |
|
2590 |
v->cur_speed = t; |
|
2591 |
} else { |
|
2592 |
v->vehstatus &= ~VS_TRAIN_SLOWING; |
|
2593 |
} |
|
2594 |
||
2595 |
// exit if inside a tunnel |
|
2596 |
if (v->u.rail.track & 0x40) |
|
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2597 |
return true; |
0 | 2598 |
|
2599 |
tile = v->tile; |
|
2600 |
||
2601 |
// tunnel entrance? |
|
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
2602 |
if (IsTileType(tile, MP_TUNNELBRIDGE) && |
0 | 2603 |
(_map5[tile] & 0xF0) == 0 && (byte)((_map5[tile] & 3)*2+1) == v->direction) |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2604 |
return true; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2605 |
|
0 | 2606 |
// depot? |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2607 |
/* 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
|
2608 |
(by building a depot right against a station) */ |
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
2609 |
/* if (IsTileType(tile, MP_RAILWAY) && (_map5[tile] & 0xFC) == 0xC0) |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2610 |
return true;*/ |
0 | 2611 |
|
22 | 2612 |
/* Determine the non-diagonal direction in which we will exit this tile */ |
0 | 2613 |
t = v->direction >> 1; |
2614 |
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) { |
|
2615 |
t = (t - 1) & 3; |
|
2616 |
} |
|
22 | 2617 |
/* Calculate next tile */ |
900 | 2618 |
tile += TileOffsByDir(t); |
22 | 2619 |
// 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
|
2620 |
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
|
2621 |
|
22 | 2622 |
/* Calc position within the current tile ?? */ |
0 | 2623 |
x = v->x_pos & 0xF; |
2624 |
y = v->y_pos & 0xF; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2625 |
|
0 | 2626 |
switch(v->direction) { |
2627 |
case 0: |
|
2628 |
x = (~x) + (~y) + 24; |
|
2629 |
break; |
|
2630 |
case 7: |
|
2631 |
x = y; |
|
2632 |
/* fall through */ |
|
2633 |
case 1: |
|
2634 |
x = (~x) + 16; |
|
2635 |
break; |
|
2636 |
case 2: |
|
2637 |
x = (~x) + y + 8; |
|
2638 |
break; |
|
2639 |
case 3: |
|
2640 |
x = y; |
|
2641 |
break; |
|
2642 |
case 4: |
|
2643 |
x = x + y - 8; |
|
2644 |
break; |
|
2645 |
case 6: |
|
2646 |
x = (~y) + x + 8; |
|
2647 |
break; |
|
2648 |
} |
|
2649 |
||
2650 |
if ( (uint16)ts != 0) { |
|
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2651 |
/* If we approach a rail-piece which we can't enter, don't enter it! */ |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2652 |
if (x + 4 > 15 && !CheckCompatibleRail(v, tile)) { |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2653 |
v->cur_speed = 0; |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2654 |
ReverseTrainDirection(v); |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2655 |
return false; |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2656 |
} |
0 | 2657 |
if ((ts &= (ts >> 16)) == 0) { |
2658 |
// make a rail/road crossing red |
|
1035
0a170deb6e33
(svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents:
1024
diff
changeset
|
2659 |
if (IsTileType(tile, MP_STREET) && (_map5[tile] & 0xF0) == 0x10) { |
0 | 2660 |
if (!(_map5[tile] & 4)) { |
2661 |
_map5[tile] |= 4; |
|
541 | 2662 |
SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v); |
0 | 2663 |
MarkTileDirtyByTile(tile); |
2664 |
} |
|
2665 |
} |
|
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2666 |
return true; |
0 | 2667 |
} |
2668 |
} else if (x + 4 > 15) { |
|
2669 |
v->cur_speed = 0; |
|
2670 |
ReverseTrainDirection(v); |
|
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2671 |
return false; |
0 | 2672 |
} |
2673 |
||
2674 |
// slow down |
|
2675 |
v->vehstatus |= VS_TRAIN_SLOWING; |
|
2676 |
t = _breakdown_speeds[x & 0xF]; |
|
2677 |
if (!(v->direction&1)) t>>=1; |
|
2678 |
if ((uint16)t < v->cur_speed) |
|
2679 |
v->cur_speed = t; |
|
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2680 |
|
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2681 |
return true; |
0 | 2682 |
} |
2683 |
||
2684 |
static void TrainLocoHandler(Vehicle *v, bool mode) |
|
2685 |
{ |
|
2686 |
int j; |
|
2687 |
||
2688 |
/* train has crashed? */ |
|
2689 |
if (v->u.rail.crash_anim_pos != 0) { |
|
2690 |
if (!mode) HandleCrashedTrain(v); |
|
2691 |
return; |
|
2692 |
} |
|
2693 |
||
2694 |
if (v->u.rail.force_proceed != 0) |
|
2695 |
v->u.rail.force_proceed--; |
|
2696 |
||
2697 |
/* train is broken down? */ |
|
2698 |
if (v->breakdown_ctr != 0) { |
|
2699 |
if (v->breakdown_ctr <= 2) { |
|
2700 |
HandleBrokenTrain(v); |
|
2701 |
return; |
|
2702 |
} |
|
2703 |
v->breakdown_ctr--; |
|
2704 |
} |
|
2705 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2706 |
if (HASBIT(v->u.rail.flags, VRF_REVERSING) && v->cur_speed == 0) { |
0 | 2707 |
ReverseTrainDirection(v); |
2708 |
} |
|
2709 |
||
2710 |
/* exit if train is stopped */ |
|
2711 |
if (v->vehstatus & VS_STOPPED && v->cur_speed == 0) |
|
2712 |
return; |
|
2713 |
||
2714 |
||
2715 |
if (ProcessTrainOrder(v)) { |
|
2716 |
v->load_unload_time_rem = 0; |
|
2717 |
v->cur_speed = 0; |
|
2718 |
v->subspeed = 0; |
|
2719 |
ReverseTrainDirection(v); |
|
2720 |
return; |
|
2721 |
} |
|
2722 |
||
2723 |
HandleTrainLoading(v, mode); |
|
2724 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2725 |
if (v->current_order.type == OT_LOADING) |
0 | 2726 |
return; |
2727 |
||
2728 |
if (CheckTrainStayInDepot(v)) |
|
2729 |
return; |
|
2730 |
||
2731 |
if (!mode) HandleLocomotiveSmokeCloud(v); |
|
2732 |
||
2733 |
j = UpdateTrainSpeed(v); |
|
2734 |
if (j == 0) { |
|
2735 |
// if the vehicle has speed 0, update the last_speed field. |
|
2736 |
if (v->cur_speed != 0) |
|
2737 |
return; |
|
2738 |
} else { |
|
2739 |
TrainCheckIfLineEnds(v); |
|
2740 |
||
2741 |
do { |
|
2742 |
TrainController(v); |
|
2743 |
if (v->cur_speed <= 0x100) |
|
2744 |
break; |
|
2745 |
} while (--j != 0); |
|
2746 |
} |
|
2747 |
||
2748 |
SetLastSpeed(v, v->cur_speed); |
|
2749 |
} |
|
2750 |
||
2751 |
||
2752 |
void Train_Tick(Vehicle *v) |
|
2753 |
{ |
|
2754 |
if (_age_cargo_skip_counter == 0 && v->cargo_days != 0xff) |
|
2755 |
v->cargo_days++; |
|
2756 |
||
2757 |
v->tick_counter++; |
|
2758 |
||
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2759 |
if (v->subtype == TS_Front_Engine) { |
0 | 2760 |
TrainLocoHandler(v, false); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2761 |
|
0 | 2762 |
// make sure vehicle wasn't deleted. |
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2763 |
if (v->type == VEH_Train && v->subtype == TS_Front_Engine) |
0 | 2764 |
TrainLocoHandler(v, true); |
1132
7378651941bf
(svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents:
1128
diff
changeset
|
2765 |
} else if (v->subtype == TS_Free_Car && HASBITS(v->vehstatus, VS_CRASHED)) { |
7378651941bf
(svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents:
1128
diff
changeset
|
2766 |
// Delete flooded standalone wagon |
7378651941bf
(svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents:
1128
diff
changeset
|
2767 |
if (++v->u.rail.crash_anim_pos >= 4400) |
7378651941bf
(svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents:
1128
diff
changeset
|
2768 |
DeleteVehicle(v); |
0 | 2769 |
} |
2770 |
} |
|
2771 |
||
2772 |
||
2773 |
static const byte _depot_track_ind[4] = {0,1,0,1}; |
|
2774 |
||
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
|
2775 |
// Validation for the news item "Train is waiting in depot" |
1095 | 2776 |
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
|
2777 |
{ |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
2778 |
Vehicle *v = GetVehicle(data_a); |
1064
1b6564cec6be
(svn r1565) -Fix: [ 1104969 ] Aircraft in hangar messages are now revalidated before
celestar
parents:
1060
diff
changeset
|
2779 |
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
|
2780 |
} |
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
|
2781 |
|
0 | 2782 |
void TrainEnterDepot(Vehicle *v, uint tile) |
2783 |
{ |
|
2784 |
SetSignalsOnBothDir(tile, _depot_track_ind[_map5[tile]&3]); |
|
2785 |
||
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2786 |
if (v->subtype != TS_Front_Engine) |
0 | 2787 |
v = GetFirstVehicleInChain(v); |
2788 |
||
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
|
2789 |
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
|
2790 |
|
0 | 2791 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
2792 |
||
2793 |
v->load_unload_time_rem = 0; |
|
2794 |
v->cur_speed = 0; |
|
2795 |
||
842 | 2796 |
MaybeReplaceVehicle(v); |
0 | 2797 |
|
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
|
2798 |
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
|
2799 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2800 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2801 |
Order t; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2802 |
|
0 | 2803 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2804 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2805 |
t = v->current_order; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2806 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2807 |
v->current_order.flags = 0; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2808 |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2809 |
if (t.flags & OF_UNLOAD) { // Part of the orderlist? |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2810 |
v->u.rail.days_since_order_progr = 0; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2811 |
v->cur_order_index++; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2812 |
} else if (t.flags & OF_FULL_LOAD) { // User initiated? |
0 | 2813 |
v->vehstatus |= VS_STOPPED; |
2814 |
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
|
2815 |
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
|
2816 |
AddValidatedNewsItem( |
0 | 2817 |
STR_8814_TRAIN_IS_WAITING_IN_DEPOT, |
2818 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
2819 |
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
|
2820 |
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
|
2821 |
ValidateTrainInDepot); |
0 | 2822 |
} |
2823 |
} |
|
2824 |
} |
|
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
|
2825 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 2826 |
} |
2827 |
||
2828 |
static void CheckIfTrainNeedsService(Vehicle *v) |
|
2829 |
{ |
|
2830 |
byte 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
|
2831 |
TrainFindDepotData tfdd; |
0 | 2832 |
|
76
30511cbc5188
(svn r77) -Fix: [1010788] AI service interval bug (tnx truesatan)
truelight
parents:
73
diff
changeset
|
2833 |
if (_patches.servint_trains == 0) |
0 | 2834 |
return; |
2835 |
||
593 | 2836 |
if (!VehicleNeedsService(v)) |
0 | 2837 |
return; |
2838 |
||
2839 |
if (v->vehstatus & VS_STOPPED) |
|
2840 |
return; |
|
2841 |
||
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2842 |
if (_patches.gotodepot && VehicleHasDepotOrders(v)) |
0 | 2843 |
return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2844 |
|
0 | 2845 |
// Don't interfere with a depot visit scheduled by the user, or a |
2846 |
// depot visit by the order list. |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2847 |
if (v->current_order.type == OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2848 |
(v->current_order.flags & (OF_FULL_LOAD | OF_UNLOAD)) != 0) |
0 | 2849 |
return; |
2850 |
||
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
|
2851 |
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
|
2852 |
/* 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
|
2853 |
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
|
2854 |
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
|
2855 |
/* 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
|
2856 |
* 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
|
2857 |
* schedule? */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2858 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2859 |
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
|
2860 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 2861 |
} |
2862 |
return; |
|
2863 |
} |
|
2864 |
||
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
|
2865 |
depot = GetDepotByTile(tfdd.tile); |
0 | 2866 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2867 |
if (v->current_order.type == OT_GOTO_DEPOT && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2868 |
v->current_order.station != depot && |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2869 |
!CHANCE16(3,16)) |
0 | 2870 |
return; |
2871 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2872 |
v->current_order.type = OT_GOTO_DEPOT; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2873 |
v->current_order.flags = OF_NON_STOP; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2874 |
v->current_order.station = 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
|
2875 |
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
|
2876 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 2877 |
} |
2878 |
||
2879 |
int32 GetTrainRunningCost(Vehicle *v) |
|
2880 |
{ |
|
2881 |
int32 cost = 0; |
|
2882 |
||
2883 |
do { |
|
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
2884 |
const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); |
0 | 2885 |
if (rvi->running_cost_base) |
2886 |
cost += rvi->running_cost_base * _price.running_rail[rvi->engclass]; |
|
2887 |
} while ( (v=v->next) != NULL ); |
|
2888 |
||
2889 |
return cost; |
|
2890 |
} |
|
2891 |
||
2892 |
void OnNewDay_Train(Vehicle *v) |
|
2893 |
{ |
|
2894 |
TileIndex tile; |
|
2895 |
||
2896 |
if ((++v->day_counter & 7) == 0) |
|
2897 |
DecreaseVehicleValue(v); |
|
2898 |
||
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2899 |
if (v->subtype == TS_Front_Engine) { |
0 | 2900 |
CheckVehicleBreakdown(v); |
2901 |
AgeVehicle(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2902 |
|
0 | 2903 |
CheckIfTrainNeedsService(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2904 |
|
0 | 2905 |
// 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
|
2906 |
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 | 2907 |
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
|
2908 |
SetDParam(0, v->unitnumber); |
0 | 2909 |
AddNewsItem( |
2910 |
STR_TRAIN_IS_LOST, |
|
2911 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
2912 |
v->index, |
|
2913 |
0); |
|
2914 |
} |
|
2915 |
||
1053
dfb5243315f1
(svn r1554) -Fix: [ 1103187 ] Order Check messages are now validated before
celestar
parents:
1049
diff
changeset
|
2916 |
CheckOrders(v->index, OC_INIT); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2917 |
|
0 | 2918 |
/* update destination */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2919 |
if (v->current_order.type == OT_GOTO_STATION && |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
2920 |
(tile = GetStation(v->current_order.station)->train_tile) != 0) |
0 | 2921 |
v->dest_tile = tile; |
2922 |
||
2923 |
if ((v->vehstatus & VS_STOPPED) == 0) { |
|
2924 |
/* running costs */ |
|
2925 |
int32 cost = GetTrainRunningCost(v) / 364; |
|
2926 |
||
2927 |
v->profit_this_year -= cost >> 8; |
|
2928 |
||
2929 |
SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN); |
|
2930 |
SubtractMoneyFromPlayerFract(v->owner, cost); |
|
2931 |
||
2932 |
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
|
2933 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 2934 |
} |
2935 |
} |
|
2936 |
} |
|
2937 |
||
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
|
2938 |
void TrainsYearlyLoop(void) |
0 | 2939 |
{ |
2940 |
Vehicle *v; |
|
2941 |
||
2942 |
FOR_ALL_VEHICLES(v) { |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2943 |
if (v->type == VEH_Train && v->subtype == TS_Front_Engine) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2944 |
|
0 | 2945 |
// show warning if train is not generating enough income last 2 years (corresponds to a red icon in the vehicle list) |
2946 |
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
|
2947 |
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
|
2948 |
SetDParam(0, v->unitnumber); |
0 | 2949 |
AddNewsItem( |
2950 |
STR_TRAIN_IS_UNPROFITABLE, |
|
2951 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
2952 |
v->index, |
|
2953 |
0); |
|
2954 |
} |
|
2955 |
||
2956 |
v->profit_last_year = v->profit_this_year; |
|
2957 |
v->profit_this_year = 0; |
|
2958 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
2959 |
} |
|
2960 |
} |
|
2961 |
} |
|
2962 |
||
2963 |
extern void ShowTrainViewWindow(Vehicle *v); |
|
2964 |
||
2965 |
void HandleClickOnTrain(Vehicle *v) |
|
2966 |
{ |
|
1067
a083a6dd1419
(svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents:
1064
diff
changeset
|
2967 |
if (v->subtype != TS_Front_Engine) v = GetFirstVehicleInChain(v); |
0 | 2968 |
ShowTrainViewWindow(v); |
2969 |
} |
|
2970 |
||
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
|
2971 |
void InitializeTrains(void) |
0 | 2972 |
{ |
2973 |
_age_cargo_skip_counter = 1; |
|
2974 |
} |