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