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