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