author | richk |
Tue, 17 Jun 2008 13:41:57 +0000 | |
branch | NewGRF_ports |
changeset 10995 | 311b38c7f9a7 |
parent 10994 | cd9968b6f96b |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
3 |
/** @file train_cmd.cpp Handling of trains. */ |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
4 |
|
0 | 5 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1883
diff
changeset
|
6 |
#include "openttd.h" |
3234
986c30171e92
(svn r3907) Replace many bridge related direct map accesses with calls to shiny new functions and mark some strange constructs with XXX
tron
parents:
3191
diff
changeset
|
7 |
#include "bridge_map.h" |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
8 |
#include "debug.h" |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
9 |
#include "tile_cmd.h" |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
10 |
#include "landscape.h" |
2561
c78c3d248897
(svn r3098) static, const, uint -> TileIndex, indentation, bracing, unused return values, ... mostly related to the clone vehicle GUI
tron
parents:
2552
diff
changeset
|
11 |
#include "gui.h" |
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
12 |
#include "station_map.h" |
3154 | 13 |
#include "tunnel_map.h" |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
14 |
#include "articulated_vehicles.h" |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
15 |
#include "command_func.h" |
0 | 16 |
#include "pathfind.h" |
1247 | 17 |
#include "npf.h" |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
18 |
#include "station_base.h" |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
19 |
#include "news_func.h" |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
20 |
#include "engine_func.h" |
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
21 |
#include "engine_base.h" |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
22 |
#include "player_func.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
23 |
#include "player_base.h" |
10242
52b4a9006029
(svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents:
10211
diff
changeset
|
24 |
#include "depot_base.h" |
52b4a9006029
(svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents:
10211
diff
changeset
|
25 |
#include "depot_func.h" |
1542
2ca6d1624e6d
(svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
1531
diff
changeset
|
26 |
#include "waypoint.h" |
1752
cdbfb2f23e72
(svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents:
1698
diff
changeset
|
27 |
#include "vehicle_gui.h" |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
28 |
#include "train.h" |
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
29 |
#include "bridge.h" |
2982
286f69caaf05
(svn r3557) - NewGRF: move callback enums to a new file as more than just engines will use them
peter1138
parents:
2970
diff
changeset
|
30 |
#include "newgrf_callbacks.h" |
2962
dbd168a4703a
(svn r3524) - Split newgrf features from engine.[ch] into newgrf_engine.[ch], and add the new files to project files.
peter1138
parents:
2952
diff
changeset
|
31 |
#include "newgrf_engine.h" |
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
32 |
#include "newgrf_sound.h" |
3727
b4d9a8ab4ce4
(svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents:
3657
diff
changeset
|
33 |
#include "newgrf_text.h" |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
34 |
#include "direction_func.h" |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
35 |
#include "yapf/yapf.h" |
6417
26acff62d001
(svn r8826) -Codechange: Replace _cargoc's separate arrays with a regular struct array (with accessor) and implement new initialization method using cargo labels.
peter1138
parents:
6324
diff
changeset
|
36 |
#include "cargotype.h" |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
37 |
#include "group.h" |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
38 |
#include "table/sprites.h" |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
39 |
#include "tunnelbridge_map.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
40 |
#include "strings_func.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
41 |
#include "functions.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
42 |
#include "window_func.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
43 |
#include "date_func.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
44 |
#include "vehicle_func.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
45 |
#include "sound_func.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
46 |
#include "signal_func.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
47 |
#include "variables.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
48 |
#include "autoreplace_gui.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
49 |
#include "gfx_func.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
50 |
#include "settings_type.h" |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
51 |
#include "order_func.h" |
10274
b3c58f3df92b
(svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents:
10242
diff
changeset
|
52 |
#include "newgrf_station.h" |
b3c58f3df92b
(svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents:
10242
diff
changeset
|
53 |
#include "effectvehicle_func.h" |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
54 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
55 |
#include "table/strings.h" |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
56 |
#include "table/train_cmd.h" |
0 | 57 |
|
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
58 |
static bool TrainCheckIfLineEnds(Vehicle *v); |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
59 |
static void TrainController(Vehicle *v, Vehicle *nomove, bool update_image); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
60 |
static TileIndex TrainApproachingCrossingTile(const Vehicle *v); |
0 | 61 |
|
4344
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4329
diff
changeset
|
62 |
static const byte _vehicle_initial_x_fract[4] = {10, 8, 4, 8}; |
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4329
diff
changeset
|
63 |
static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10}; |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
64 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
65 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
66 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
67 |
* Determine the side in which the train will leave the tile |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
68 |
* |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
69 |
* @param direction vehicle direction |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
70 |
* @param track vehicle track bits |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
71 |
* @return side of tile the train will leave |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
72 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
73 |
static inline DiagDirection TrainExitDir(Direction direction, TrackBits track) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
74 |
{ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
75 |
static const TrackBits state_dir_table[DIAGDIR_END] = { TRACK_BIT_RIGHT, TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER }; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
76 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
77 |
DiagDirection diagdir = DirToDiagDir(direction); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
78 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
79 |
/* Determine the diagonal direction in which we will exit this tile */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
80 |
if (!HasBit(direction, 0) && track != state_dir_table[diagdir]) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
81 |
diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
82 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
83 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
84 |
return diagdir; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
85 |
} |
0 | 86 |
|
5163
83acad83bbdd
(svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents:
5162
diff
changeset
|
87 |
|
83acad83bbdd
(svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents:
5162
diff
changeset
|
88 |
/** Return the cargo weight multiplier to use for a rail vehicle |
5316
b04421921eae
(svn r7473) -Fix (r7269): Pass a cargo type to determine the freight weight
peter1138
parents:
5303
diff
changeset
|
89 |
* @param cargo Cargo type to get multiplier for |
5163
83acad83bbdd
(svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents:
5162
diff
changeset
|
90 |
* @return Cargo weight multiplier |
83acad83bbdd
(svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents:
5162
diff
changeset
|
91 |
*/ |
5316
b04421921eae
(svn r7473) -Fix (r7269): Pass a cargo type to determine the freight weight
peter1138
parents:
5303
diff
changeset
|
92 |
byte FreightWagonMult(CargoID cargo) |
5163
83acad83bbdd
(svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents:
5162
diff
changeset
|
93 |
{ |
6440
14530751458a
(svn r8850) -Codechange: Use the cargo type's is_freight flag instead of checking the cargo type to determine if the cargo type is freight cargo type cargo type.
peter1138
parents:
6417
diff
changeset
|
94 |
if (!GetCargo(cargo)->is_freight) return 1; |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
95 |
return _settings_game.vehicle.freight_trains; |
5163
83acad83bbdd
(svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents:
5162
diff
changeset
|
96 |
} |
83acad83bbdd
(svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents:
5162
diff
changeset
|
97 |
|
83acad83bbdd
(svn r7269) -Feature: Add freight trains patch option. This option is a multiplier for the weight of cargo on freight trains, to simulate longer heavier trains. The default value of 1 behaves as before.
peter1138
parents:
5162
diff
changeset
|
98 |
|
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
99 |
/** |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
100 |
* Recalculates the cached total power of a train. Should be called when the consist is changed |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
101 |
* @param v First vehicle of the consist. |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
102 |
*/ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
103 |
void TrainPowerChanged(Vehicle *v) |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
104 |
{ |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
105 |
uint32 total_power = 0; |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
106 |
uint32 max_te = 0; |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
107 |
|
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
108 |
for (const Vehicle *u = v; u != NULL; u = u->Next()) { |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
109 |
/* Power is not added for articulated parts */ |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
110 |
if (IsArticulatedPart(u)) continue; |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
111 |
|
6498
aff910a05c6e
(svn r8935) -Codechange: unification of track type between road and rail tiles, unification of ground type between normal rail tiles and depots/waypoints and removing the need for RailTileSubType.
rubidium
parents:
6481
diff
changeset
|
112 |
RailType railtype = GetRailType(u->tile); |
6020 | 113 |
bool engine_has_power = HasPowerOnRail(u->u.rail.railtype, railtype); |
114 |
bool wagon_has_power = HasPowerOnRail(v->u.rail.railtype, railtype); |
|
115 |
||
116 |
const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type); |
|
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
117 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
118 |
if (engine_has_power) { |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
119 |
uint16 power = GetVehicleProperty(u, 0x0B, rvi_u->power); |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
120 |
if (power != 0) { |
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
121 |
/* Halve power for multiheaded parts */ |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
122 |
if (IsMultiheaded(u)) power /= 2; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
123 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
124 |
total_power += power; |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
125 |
/* Tractive effort in (tonnes * 1000 * 10 =) N */ |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
126 |
max_te += (u->u.rail.cached_veh_weight * 10000 * GetVehicleProperty(u, 0x1F, rvi_u->tractive_effort)) / 256; |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
127 |
} |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
128 |
} |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
129 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
130 |
if (HasBit(u->u.rail.flags, VRF_POWEREDWAGON) && (wagon_has_power)) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
131 |
total_power += RailVehInfo(u->u.rail.first_engine)->pow_wag_power; |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
132 |
} |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
133 |
} |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
134 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
135 |
if (v->u.rail.cached_power != total_power || v->u.rail.cached_max_te != max_te) { |
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
136 |
/* If it has no power (no catenary), stop the train */ |
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
137 |
if (total_power == 0) v->vehstatus |= VS_STOPPED; |
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
138 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
139 |
v->u.rail.cached_power = total_power; |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
140 |
v->u.rail.cached_max_te = max_te; |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
141 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
142 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
143 |
} |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
144 |
} |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
145 |
|
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
146 |
|
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
147 |
/** |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
148 |
* Recalculates the cached weight of a train and its vehicles. Should be called each time the cargo on |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
149 |
* the consist changes. |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
150 |
* @param v First vehicle of the consist. |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
151 |
*/ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
152 |
static void TrainCargoChanged(Vehicle *v) |
2639 | 153 |
{ |
5162
f1d90a883266
(svn r7268) -Codechange: change rail vehicle cached weight from 16 to 32 bit values to avoid possible overflows.
peter1138
parents:
5116
diff
changeset
|
154 |
uint32 weight = 0; |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
155 |
|
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
156 |
for (Vehicle *u = v; u != NULL; u = u->Next()) { |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
157 |
uint32 vweight = GetCargo(u->cargo_type)->weight * u->cargo.Count() * FreightWagonMult(u->cargo_type) / 16; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
158 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
159 |
/* Vehicle weight is not added for articulated parts. */ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
160 |
if (!IsArticulatedPart(u)) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
161 |
/* vehicle weight is the sum of the weight of the vehicle and the weight of its cargo */ |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
162 |
vweight += GetVehicleProperty(u, 0x16, RailVehInfo(u->engine_type)->weight); |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
163 |
|
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
164 |
/* powered wagons have extra weight added */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
165 |
if (HasBit(u->u.rail.flags, VRF_POWEREDWAGON)) |
4017
035aa73fb8d0
(svn r5240) - NewGRF: when there is more than one engine in a consist, base livery overrides and powered wagon weight/power on the last engine before the wagon.
peter1138
parents:
3988
diff
changeset
|
166 |
vweight += RailVehInfo(u->u.rail.first_engine)->pow_wag_weight; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
167 |
} |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
168 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
169 |
/* consist weight is the sum of the weight of all vehicles in the consist */ |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
170 |
weight += vweight; |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
171 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
172 |
/* store vehicle weight in cache */ |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
173 |
u->u.rail.cached_veh_weight = vweight; |
6476 | 174 |
} |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
175 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
176 |
/* store consist weight in cache */ |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
177 |
v->u.rail.cached_weight = weight; |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
178 |
|
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
179 |
/* Now update train power (tractive effort is dependent on weight) */ |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
180 |
TrainPowerChanged(v); |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
181 |
} |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
182 |
|
3355
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
183 |
|
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
184 |
/** |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
185 |
* Recalculates the cached stuff of a train. Should be called each time a vehicle is added |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
186 |
* to/removed from the chain, and when the game is loaded. |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
187 |
* Note: this needs to be called too for 'wagon chains' (in the depot, without an engine) |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
188 |
* @param v First vehicle of the chain. |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
189 |
*/ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
190 |
void TrainConsistChanged(Vehicle *v) |
2639 | 191 |
{ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
192 |
uint16 max_speed = UINT16_MAX; |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
193 |
|
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
194 |
assert(v->type == VEH_TRAIN); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
195 |
assert(IsFrontEngine(v) || IsFreeWagon(v)); |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
196 |
|
6476 | 197 |
const RailVehicleInfo *rvi_v = RailVehInfo(v->engine_type); |
198 |
EngineID first_engine = IsFrontEngine(v) ? v->engine_type : INVALID_ENGINE; |
|
2587
d554e2a03afa
(svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents:
2574
diff
changeset
|
199 |
v->u.rail.cached_total_length = 0; |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
200 |
v->u.rail.compatible_railtypes = RAILTYPES_NONE; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
201 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
202 |
bool train_can_tilt = true; |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
203 |
|
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
204 |
for (Vehicle *u = v; u != NULL; u = u->Next()) { |
1908
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
205 |
const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type); |
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
206 |
|
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
207 |
/* Check the v->first cache. */ |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
208 |
assert(u->First() == v); |
2993
48b4308ed6a1
(svn r3570) - Explicitly update v->first in TrainConsistChanged() if necessary, as this is far faster than brute forcing it later.
peter1138
parents:
2989
diff
changeset
|
209 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
210 |
/* update the 'first engine' */ |
6476 | 211 |
u->u.rail.first_engine = v == u ? INVALID_ENGINE : first_engine; |
6074 | 212 |
u->u.rail.railtype = rvi_u->railtype; |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
213 |
|
10994
cd9968b6f96b
(svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents:
10991
diff
changeset
|
214 |
if (IsTrainEngine(u)) first_engine = u->engine_type; |
cd9968b6f96b
(svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents:
10991
diff
changeset
|
215 |
|
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
216 |
/* Set user defined data to its default value */ |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
217 |
u->u.rail.user_def_data = rvi_u->user_def_data; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
218 |
} |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
219 |
|
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
220 |
for (Vehicle *u = v; u != NULL; u = u->Next()) { |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
221 |
/* Update user defined data (must be done before other properties) */ |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
222 |
u->u.rail.user_def_data = GetVehicleProperty(u, 0x25, u->u.rail.user_def_data); |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
223 |
} |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
224 |
|
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
225 |
for (Vehicle *u = v; u != NULL; u = u->Next()) { |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
226 |
const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type); |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
227 |
|
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
228 |
if (!HasBit(EngInfo(u->engine_type)->misc_flags, EF_RAIL_TILTS)) train_can_tilt = false; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
229 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
230 |
/* Cache wagon override sprite group. NULL is returned if there is none */ |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
231 |
u->u.rail.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->u.rail.first_engine); |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
232 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
233 |
/* Reset color map */ |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
234 |
u->colormap = PAL_NONE; |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
235 |
|
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
236 |
if (rvi_u->visual_effect != 0) { |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
237 |
u->u.rail.cached_vis_effect = rvi_u->visual_effect; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
238 |
} else { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
239 |
if (IsTrainWagon(u) || IsArticulatedPart(u)) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
240 |
/* Wagons and articulated parts have no effect by default */ |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
241 |
u->u.rail.cached_vis_effect = 0x40; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
242 |
} else if (rvi_u->engclass == 0) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
243 |
/* Steam is offset by -4 units */ |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
244 |
u->u.rail.cached_vis_effect = 4; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
245 |
} else { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
246 |
/* Diesel fumes and sparks come from the centre */ |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
247 |
u->u.rail.cached_vis_effect = 8; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
248 |
} |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
249 |
} |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
250 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
251 |
if (!IsArticulatedPart(u)) { |
3926
eb6d2e32fc79
(svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents:
3900
diff
changeset
|
252 |
/* Check powered wagon / visual effect callback */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
253 |
if (HasBit(EngInfo(u->engine_type)->callbackmask, CBM_TRAIN_WAGON_POWER)) { |
3926
eb6d2e32fc79
(svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents:
3900
diff
changeset
|
254 |
uint16 callback = GetVehicleCallback(CBID_TRAIN_WAGON_POWER, 0, 0, u->engine_type, u); |
eb6d2e32fc79
(svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents:
3900
diff
changeset
|
255 |
|
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
256 |
if (callback != CALLBACK_FAILED) u->u.rail.cached_vis_effect = GB(callback, 0, 8); |
3926
eb6d2e32fc79
(svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents:
3900
diff
changeset
|
257 |
} |
eb6d2e32fc79
(svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents:
3900
diff
changeset
|
258 |
|
6119
b47985557d1e
(svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents:
6109
diff
changeset
|
259 |
if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RAILVEH_WAGON && |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
260 |
UsesWagonOverride(u) && !HasBit(u->u.rail.cached_vis_effect, 7)) { |
6119
b47985557d1e
(svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents:
6109
diff
changeset
|
261 |
/* wagon is powered */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
262 |
SetBit(u->u.rail.flags, VRF_POWEREDWAGON); // cache 'powered' status |
6476 | 263 |
} else { |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
264 |
ClrBit(u->u.rail.flags, VRF_POWEREDWAGON); |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
265 |
} |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
266 |
|
3355
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
267 |
/* Do not count powered wagons for the compatible railtypes, as wagons always |
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
268 |
have railtype normal */ |
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
269 |
if (rvi_u->power > 0) { |
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
270 |
v->u.rail.compatible_railtypes |= GetRailTypeInfo(u->u.rail.railtype)->powered_railtypes; |
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
271 |
} |
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
272 |
|
5116
2a33a74925c5
(svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents:
4996
diff
changeset
|
273 |
/* Some electric engines can be allowed to run on normal rail. It happens to all |
2a33a74925c5
(svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents:
4996
diff
changeset
|
274 |
* existing electric engines when elrails are disabled and then re-enabled */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
275 |
if (HasBit(u->u.rail.flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL)) { |
5116
2a33a74925c5
(svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents:
4996
diff
changeset
|
276 |
u->u.rail.railtype = RAILTYPE_RAIL; |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
277 |
u->u.rail.compatible_railtypes |= RAILTYPES_RAIL; |
5116
2a33a74925c5
(svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents:
4996
diff
changeset
|
278 |
} |
2a33a74925c5
(svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents:
4996
diff
changeset
|
279 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
280 |
/* max speed is the minimum of the speed limits of all vehicles in the consist */ |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
281 |
if ((rvi_u->railveh_type != RAILVEH_WAGON || _settings_game.vehicle.wagon_speed_limits) && !UsesWagonOverride(u)) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
282 |
uint16 speed = GetVehicleProperty(u, 0x09, rvi_u->max_speed); |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
283 |
if (speed != 0) max_speed = min(speed, max_speed); |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
284 |
} |
1908
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
285 |
} |
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
286 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
287 |
if (u->cargo_type == rvi_u->cargo_type && u->cargo_subtype == 0) { |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
288 |
/* Set cargo capacity if we've not been refitted */ |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
289 |
u->cargo_cap = GetVehicleProperty(u, 0x14, rvi_u->capacity); |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
290 |
} |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
291 |
|
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
292 |
/* check the vehicle length (callback) */ |
6476 | 293 |
uint16 veh_len = CALLBACK_FAILED; |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
294 |
if (HasBit(EngInfo(u->engine_type)->callbackmask, CBM_VEHICLE_LENGTH)) { |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
295 |
veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, u->engine_type, u); |
3956
0f43adbc293a
(svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents:
3955
diff
changeset
|
296 |
} |
0f43adbc293a
(svn r5105) - NewGRF: Move callbackmask from *VehicleInfo to EngineInfo. This simplifies code that works with more than one vehicle type.
peter1138
parents:
3955
diff
changeset
|
297 |
if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor; |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
298 |
veh_len = Clamp(veh_len, 0, u->Next() == NULL ? 7 : 5); // the clamp on vehicles not the last in chain is stricter, as too short wagons can break the 'follow next vehicle' code |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
299 |
u->u.rail.cached_veh_length = 8 - veh_len; |
2587
d554e2a03afa
(svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents:
2574
diff
changeset
|
300 |
v->u.rail.cached_total_length += u->u.rail.cached_veh_length; |
6476 | 301 |
} |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
302 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
303 |
/* store consist weight/max speed in cache */ |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
304 |
v->u.rail.cached_max_speed = max_speed; |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
305 |
v->u.rail.cached_tilt = train_can_tilt; |
3355
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
306 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
307 |
/* recalculate cached weights and power too (we do this *after* the rest, so it is known which wagons are powered and need extra weight added) */ |
1908
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
308 |
TrainCargoChanged(v); |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
309 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
310 |
if (IsFrontEngine(v)) { |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
311 |
UpdateTrainAcceleration(v); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
312 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
313 |
} |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
314 |
} |
1247 | 315 |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
316 |
enum AccelType { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
317 |
AM_ACCEL, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
318 |
AM_BRAKE |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
319 |
}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
320 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
321 |
/** new acceleration*/ |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
322 |
static int GetTrainAcceleration(Vehicle *v, bool mode) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
323 |
{ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
324 |
static const int absolute_max_speed = UINT16_MAX; |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
325 |
int max_speed = absolute_max_speed; |
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
326 |
int speed = v->cur_speed * 10 / 16; // km-ish/h -> mp/h |
6476 | 327 |
int curvecount[2] = {0, 0}; |
328 |
||
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
329 |
/*first find the curve speed limit */ |
6476 | 330 |
int numcurve = 0; |
331 |
int sum = 0; |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
332 |
int pos = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
333 |
int lastpos = -1; |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
334 |
for (const Vehicle *u = v; u->Next() != NULL; u = u->Next(), pos++) { |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
335 |
Direction this_dir = u->direction; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
336 |
Direction next_dir = u->Next()->direction; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
337 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
338 |
DirDiff dirdiff = DirDifference(this_dir, next_dir); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
339 |
if (dirdiff == DIRDIFF_SAME) continue; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
340 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
341 |
if (dirdiff == DIRDIFF_45LEFT) curvecount[0]++; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
342 |
if (dirdiff == DIRDIFF_45RIGHT) curvecount[1]++; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
343 |
if (dirdiff == DIRDIFF_45LEFT || dirdiff == DIRDIFF_45RIGHT) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
344 |
if (lastpos != -1) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
345 |
numcurve++; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
346 |
sum += pos - lastpos; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
347 |
if (pos - lastpos == 1) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
348 |
max_speed = 88; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
349 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
350 |
} |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
351 |
lastpos = pos; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
352 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
353 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
354 |
/*if we have a 90 degree turn, fix the speed limit to 60 */ |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
355 |
if (dirdiff == DIRDIFF_90LEFT || dirdiff == DIRDIFF_90RIGHT) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
356 |
max_speed = 61; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
357 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
358 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
359 |
|
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
360 |
if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
361 |
int total = curvecount[0] + curvecount[1]; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
362 |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
363 |
if (curvecount[0] == 1 && curvecount[1] == 1) { |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
364 |
max_speed = absolute_max_speed; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
365 |
} else if (total > 1) { |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
366 |
if (numcurve > 0) sum /= numcurve; |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
367 |
max_speed = 232 - (13 - Clamp(sum, 1, 12)) * (13 - Clamp(sum, 1, 12)); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
368 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
369 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
370 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
371 |
if (max_speed != absolute_max_speed) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
372 |
/* Apply the engine's rail type curve speed advantage, if it slowed by curves */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
373 |
const RailtypeInfo *rti = GetRailTypeInfo(v->u.rail.railtype); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
374 |
max_speed += (max_speed / 2) * rti->curve_speed; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
375 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
376 |
if (v->u.rail.cached_tilt) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
377 |
/* Apply max_speed bonus of 20% for a tilting train */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
378 |
max_speed += max_speed / 5; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
379 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
380 |
} |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
381 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
382 |
if (IsTileType(v->tile, MP_STATION) && IsFrontEngine(v)) { |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
383 |
if (v->current_order.ShouldStopAtStation(v, GetStationIndex(v->tile))) { |
6324
ebf3649bd88f
(svn r8709) -Fix/Codechange: Rename the function GetStationPlatforms into GetPlatformLength because that is what it really does. Overload it because there is already a GetPlatformLength (one gives the length of the whole platform, the other gives the remaining length in a given direction). Turned both functions into methods of Station. While messing around with it, fix a problem where loading times for overhanging trains are miscomputed.
celestar
parents:
6320
diff
changeset
|
384 |
int station_length = GetStationByTile(v->tile)->GetPlatformLength(v->tile, DirToDiagDir(v->direction)); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
385 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
386 |
int st_max_speed = 120; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
387 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
388 |
int delta_v = v->cur_speed / (station_length + 1); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
389 |
if (v->max_speed > (v->cur_speed - delta_v)) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
390 |
st_max_speed = v->cur_speed - (delta_v / 10); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
391 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
392 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
393 |
st_max_speed = max(st_max_speed, 25 * station_length); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
394 |
max_speed = min(max_speed, st_max_speed); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
395 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
396 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
397 |
|
6476 | 398 |
int mass = v->u.rail.cached_weight; |
399 |
int power = v->u.rail.cached_power * 746; |
|
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
400 |
max_speed = min(max_speed, v->u.rail.cached_max_speed); |
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
401 |
|
6476 | 402 |
int num = 0; //number of vehicles, change this into the number of axles later |
403 |
int incl = 0; |
|
404 |
int drag_coeff = 20; //[1e-4] |
|
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
405 |
for (const Vehicle *u = v; u != NULL; u = u->Next()) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
406 |
num++; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
407 |
drag_coeff += 3; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
408 |
|
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
409 |
if (u->u.rail.track == TRACK_BIT_DEPOT) max_speed = min(max_speed, 61); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
410 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
411 |
if (HasBit(u->u.rail.flags, VRF_GOINGUP)) { |
4434
4175805666a5
(svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents:
4432
diff
changeset
|
412 |
incl += u->u.rail.cached_veh_weight * 60; //3% slope, quite a bit actually |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
413 |
} else if (HasBit(u->u.rail.flags, VRF_GOINGDOWN)) { |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
414 |
incl -= u->u.rail.cached_veh_weight * 60; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
415 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
416 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
417 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
418 |
v->max_speed = max_speed; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
419 |
|
6476 | 420 |
const int area = 120; |
421 |
const int friction = 35; //[1e-3] |
|
422 |
int resistance; |
|
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
423 |
if (v->u.rail.railtype != RAILTYPE_MAGLEV) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
424 |
resistance = 13 * mass / 10; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
425 |
resistance += 60 * num; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
426 |
resistance += friction * mass * speed / 1000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
427 |
resistance += (area * drag_coeff * speed * speed) / 10000; |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
428 |
} else { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
429 |
resistance = (area * (drag_coeff / 2) * speed * speed) / 10000; |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
430 |
} |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
431 |
resistance += incl; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
432 |
resistance *= 4; //[N] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
433 |
|
5277
5d278741650c
(svn r7421) -Fix (r2475): Changed "kick off" acceleration resulted in only a small amount of power being applied whilst moving off and then double the power at 1 mph. This resulted in a perceived delay before trains moved. Fix this by applying the full power of the engine (or the kick off, whichever is greater). Essay over.
peter1138
parents:
5259
diff
changeset
|
434 |
/* Due to the mph to m/s conversion below, at speeds below 3 mph the force is |
5d278741650c
(svn r7421) -Fix (r2475): Changed "kick off" acceleration resulted in only a small amount of power being applied whilst moving off and then double the power at 1 mph. This resulted in a perceived delay before trains moved. Fix this by applying the full power of the engine (or the kick off, whichever is greater). Essay over.
peter1138
parents:
5259
diff
changeset
|
435 |
* actually double the train's power */ |
6476 | 436 |
const int max_te = v->u.rail.cached_max_te; // [N] |
437 |
int force; |
|
5277
5d278741650c
(svn r7421) -Fix (r2475): Changed "kick off" acceleration resulted in only a small amount of power being applied whilst moving off and then double the power at 1 mph. This resulted in a perceived delay before trains moved. Fix this by applying the full power of the engine (or the kick off, whichever is greater). Essay over.
peter1138
parents:
5259
diff
changeset
|
438 |
if (speed > 2) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
439 |
switch (v->u.rail.railtype) { |
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
440 |
case RAILTYPE_RAIL: |
3355
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
441 |
case RAILTYPE_ELECTRIC: |
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
442 |
case RAILTYPE_MONO: |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
443 |
force = power / speed; //[N] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
444 |
force *= 22; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
445 |
force /= 10; |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
446 |
if (mode == AM_ACCEL && force > max_te) force = max_te; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
447 |
break; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
448 |
|
6476 | 449 |
default: NOT_REACHED(); |
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
450 |
case RAILTYPE_MAGLEV: |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
451 |
force = power / 25; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
452 |
break; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
453 |
} |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
454 |
} else { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
455 |
/* "kickoff" acceleration */ |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
456 |
force = (mode == AM_ACCEL && v->u.rail.railtype != RAILTYPE_MAGLEV) ? min(max_te, power) : power; |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
457 |
force = max(force, (mass * 8) + resistance); |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
458 |
} |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
459 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
460 |
if (force <= 0) force = 10000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
461 |
|
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
462 |
if (v->u.rail.railtype != RAILTYPE_MAGLEV) force = min(force, mass * 10 * 200); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
463 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
464 |
if (mode == AM_ACCEL) { |
1684
d09bc0beec4c
(svn r2188) Revert r2185, because trains always accerlated (Requested by Celestar)
tron
parents:
1683
diff
changeset
|
465 |
return (force - resistance) / (mass * 4); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
466 |
} else { |
5279
658a16e02195
(svn r7425) -Fix (r1681): With realistic acceleration, guarantee a minimum braking
peter1138
parents:
5277
diff
changeset
|
467 |
return min((-force - resistance) / (mass * 4), -10000 / (mass * 4)); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
468 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
469 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
470 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
471 |
void UpdateTrainAcceleration(Vehicle *v) |
0 | 472 |
{ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
473 |
assert(IsFrontEngine(v)); |
0 | 474 |
|
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
475 |
v->max_speed = v->u.rail.cached_max_speed; |
0 | 476 |
|
6476 | 477 |
uint power = v->u.rail.cached_power; |
478 |
uint weight = v->u.rail.cached_weight; |
|
0 | 479 |
assert(weight != 0); |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
480 |
v->acceleration = Clamp(power / weight * 4, 1, 255); |
0 | 481 |
} |
482 |
||
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
483 |
SpriteID Train::GetImage(Direction direction) const |
0 | 484 |
{ |
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
485 |
uint8 spritenum = this->spritenum; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
486 |
SpriteID sprite; |
0 | 487 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
488 |
if (HasBit(this->u.rail.flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction); |
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
489 |
|
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
490 |
if (is_custom_sprite(spritenum)) { |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
491 |
sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum))); |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
492 |
if (sprite != 0) return sprite; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
493 |
|
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
494 |
spritenum = GetEngine(this->engine_type)->image_index; |
0 | 495 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
496 |
|
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
497 |
sprite = _engine_sprite_base[spritenum] + ((direction + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]); |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
498 |
|
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
499 |
if (this->cargo.Count() >= this->cargo_cap / 2U) sprite += _wagon_full_adder[spritenum]; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
500 |
|
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
501 |
return sprite; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
502 |
} |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
503 |
|
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
504 |
static SpriteID GetRailIcon(EngineID engine, bool rear_head, int &y) |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
505 |
{ |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
506 |
Direction dir = rear_head ? DIR_E : DIR_W; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
507 |
uint8 spritenum = RailVehInfo(engine)->image_index; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
508 |
|
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
509 |
if (is_custom_sprite(spritenum)) { |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
510 |
SpriteID sprite = GetCustomVehicleIcon(engine, dir); |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
511 |
if (sprite != 0) { |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
512 |
y += _traininfo_vehicle_pitch; // TODO Make this per-GRF |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
513 |
return sprite; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
514 |
} |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
515 |
|
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
516 |
spritenum = GetEngine(engine)->image_index; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
517 |
} |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
518 |
|
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
519 |
if (rear_head) spritenum++; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
520 |
|
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
521 |
return ((6 + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]) + _engine_sprite_base[spritenum]; |
0 | 522 |
} |
523 |
||
5919
2b58160d667d
(svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents:
5902
diff
changeset
|
524 |
void DrawTrainEngine(int x, int y, EngineID engine, SpriteID pal) |
0 | 525 |
{ |
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
526 |
if (RailVehInfo(engine)->railveh_type == RAILVEH_MULTIHEAD) { |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
527 |
int yf = y; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
528 |
int yr = y; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
529 |
|
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
530 |
SpriteID spritef = GetRailIcon(engine, false, yf); |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
531 |
SpriteID spriter = GetRailIcon(engine, true, yr); |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
532 |
DrawSprite(spritef, pal, x - 14, yf); |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
533 |
DrawSprite(spriter, pal, x + 15, yr); |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
534 |
} else { |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
535 |
SpriteID sprite = GetRailIcon(engine, false, y); |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
536 |
DrawSprite(sprite, pal, x, y); |
0 | 537 |
} |
538 |
} |
|
539 |
||
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
540 |
static CommandCost CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags) |
0 | 541 |
{ |
6476 | 542 |
const RailVehicleInfo *rvi = RailVehInfo(engine); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
543 |
CommandCost value(EXPENSES_NEW_VEHICLES, (GetEngineProperty(engine, 0x17, rvi->base_cost) * _price.build_railwagon) >> 8); |
6476 | 544 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
545 |
uint num_vehicles = 1 + CountArticulatedParts(engine, false); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
546 |
|
0 | 547 |
if (!(flags & DC_QUERY_COST)) { |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
548 |
/* Check that the wagon can drive on the track in question */ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
549 |
if (!IsCompatibleRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
550 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
551 |
/* Allow for the wagon and the articulated parts, plus one to "terminate" the list. */ |
10994
cd9968b6f96b
(svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents:
10991
diff
changeset
|
552 |
Vehicle **vl = AllocaM(Vehicle*, num_vehicles + 1); |
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
553 |
memset(vl, 0, sizeof(*vl) * (num_vehicles + 1)); |
4831
327d181a60ee
(svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents:
4806
diff
changeset
|
554 |
|
6800
6c09e1e86fcb
(svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents:
6743
diff
changeset
|
555 |
if (!Vehicle::AllocateList(vl, num_vehicles)) |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
556 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
0 | 557 |
|
558 |
if (flags & DC_EXEC) { |
|
6476 | 559 |
Vehicle *v = vl[0]; |
2639 | 560 |
v->spritenum = rvi->image_index; |
0 | 561 |
|
6476 | 562 |
Vehicle *u = NULL; |
563 |
||
564 |
Vehicle *w; |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
565 |
FOR_ALL_VEHICLES(w) { |
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
566 |
if (w->type == VEH_TRAIN && w->tile == tile && |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
567 |
IsFreeWagon(w) && w->engine_type == engine && |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
568 |
!HASBITS(w->vehstatus, VS_CRASHED)) { /// do not connect new wagon with crashed/flooded consists |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
569 |
u = GetLastVehicleInChain(w); |
0 | 570 |
break; |
571 |
} |
|
572 |
} |
|
573 |
||
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
574 |
v = new (v) Train(); |
0 | 575 |
v->engine_type = engine; |
576 |
||
6476 | 577 |
DiagDirection dir = GetRailDepotDirection(tile); |
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
578 |
|
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3140
diff
changeset
|
579 |
v->direction = DiagDirToDir(dir); |
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
580 |
v->tile = tile; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
581 |
|
6476 | 582 |
int x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir]; |
583 |
int y = TileY(tile) * TILE_SIZE | _vehicle_initial_y_fract[dir]; |
|
0 | 584 |
|
585 |
v->x_pos = x; |
|
586 |
v->y_pos = y; |
|
6476 | 587 |
v->z_pos = GetSlopeZ(x, y); |
0 | 588 |
v->owner = _current_player; |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
589 |
v->u.rail.track = TRACK_BIT_DEPOT; |
0 | 590 |
v->vehstatus = VS_HIDDEN | VS_DEFPAL; |
591 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
592 |
// v->subtype = 0; |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
593 |
SetTrainWagon(v); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
594 |
|
0 | 595 |
if (u != NULL) { |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
596 |
u->SetNext(v); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
597 |
} else { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
598 |
SetFreeWagon(v); |
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
599 |
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); |
0 | 600 |
} |
601 |
||
602 |
v->cargo_type = rvi->cargo_type; |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
603 |
// v->cargo_subtype = 0; |
0 | 604 |
v->cargo_cap = rvi->capacity; |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
605 |
v->value = value.GetCost(); |
0 | 606 |
// v->day_counter = 0; |
607 |
||
6074 | 608 |
v->u.rail.railtype = rvi->railtype; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
609 |
|
4329
9759d5c52010
(svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents:
4293
diff
changeset
|
610 |
v->build_year = _cur_year; |
0 | 611 |
v->cur_image = 0xAC2; |
2804
b932fe148d36
(svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents:
2758
diff
changeset
|
612 |
v->random_bits = VehicleRandomBits(); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
613 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
614 |
v->group_id = DEFAULT_GROUP; |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
615 |
|
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
616 |
AddArticulatedParts(vl, VEH_TRAIN); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
617 |
|
2564 | 618 |
_new_vehicle_id = v->index; |
0 | 619 |
|
620 |
VehiclePositionChanged(v); |
|
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
621 |
TrainConsistChanged(v->First()); |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
622 |
UpdateTrainGroupID(v->First()); |
0 | 623 |
|
624 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
2970
77fa46c313c0
(svn r3535) -Feature: [autoreplace] allow autoreplacing of train wagons
bjarni
parents:
2962
diff
changeset
|
625 |
if (IsLocalPlayer()) { |
6800
6c09e1e86fcb
(svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents:
6743
diff
changeset
|
626 |
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window |
2970
77fa46c313c0
(svn r3535) -Feature: [autoreplace] allow autoreplacing of train wagons
bjarni
parents:
2962
diff
changeset
|
627 |
} |
6195
b90cf92697b9
(svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents:
6173
diff
changeset
|
628 |
GetPlayer(_current_player)->num_engines[engine]++; |
0 | 629 |
} |
630 |
} |
|
631 |
||
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
632 |
return value; |
0 | 633 |
} |
634 |
||
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
635 |
/** Move all free vehicles in the depot to the train */ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
636 |
static void NormalizeTrainVehInDepot(const Vehicle *u) |
0 | 637 |
{ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
638 |
const Vehicle *v; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
639 |
|
0 | 640 |
FOR_ALL_VEHICLES(v) { |
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
641 |
if (v->type == VEH_TRAIN && IsFreeWagon(v) && |
0 | 642 |
v->tile == u->tile && |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
643 |
v->u.rail.track == TRACK_BIT_DEPOT) { |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
644 |
if (CmdFailed(DoCommand(0, v->index | (u->index << 16), 1, DC_EXEC, |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
645 |
CMD_MOVE_RAIL_VEHICLE))) |
0 | 646 |
break; |
647 |
} |
|
648 |
} |
|
649 |
} |
|
650 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
651 |
static CommandCost EstimateTrainCost(EngineID engine, const RailVehicleInfo *rvi) |
0 | 652 |
{ |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
653 |
return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine, 0x17, rvi->base_cost) * (_price.build_railvehicle >> 3) >> 5); |
0 | 654 |
} |
655 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
656 |
static void AddRearEngineToMultiheadedTrain(Vehicle *v, Vehicle *u, bool building) |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
657 |
{ |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
658 |
u = new (u) Train(); |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
659 |
u->direction = v->direction; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
660 |
u->owner = v->owner; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
661 |
u->tile = v->tile; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
662 |
u->x_pos = v->x_pos; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
663 |
u->y_pos = v->y_pos; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
664 |
u->z_pos = v->z_pos; |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
665 |
u->u.rail.track = TRACK_BIT_DEPOT; |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
666 |
u->vehstatus = v->vehstatus & ~VS_STOPPED; |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
667 |
// u->subtype = 0; |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
668 |
SetMultiheaded(u); |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
669 |
u->spritenum = v->spritenum + 1; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
670 |
u->cargo_type = v->cargo_type; |
3870
766d7bd6c299
(svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents:
3836
diff
changeset
|
671 |
u->cargo_subtype = v->cargo_subtype; |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
672 |
u->cargo_cap = v->cargo_cap; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
673 |
u->u.rail.railtype = v->u.rail.railtype; |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
674 |
if (building) v->SetNext(u); |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
675 |
u->engine_type = v->engine_type; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
676 |
u->build_year = v->build_year; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
677 |
if (building) v->value >>= 1; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
678 |
u->value = v->value; |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
679 |
u->cur_image = 0xAC2; |
2804
b932fe148d36
(svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents:
2758
diff
changeset
|
680 |
u->random_bits = VehicleRandomBits(); |
1060
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
681 |
VehiclePositionChanged(u); |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
682 |
} |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
683 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
684 |
/** Build a railroad vehicle. |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
685 |
* @param tile tile of the depot where rail-vehicle is built |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
686 |
* @param flags type of operation |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
687 |
* @param p1 engine type id |
3816
2cdb29b57c1f
(svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents:
3815
diff
changeset
|
688 |
* @param p2 bit 0 when set, the train will get number 0, otherwise it will get a free number |
2cdb29b57c1f
(svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents:
3815
diff
changeset
|
689 |
* bit 1 prevents any free cars from being added to the train |
0 | 690 |
*/ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
691 |
CommandCost CmdBuildRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 692 |
{ |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
693 |
/* Check if the engine-type is valid (for the player) */ |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
694 |
if (!IsEngineBuildable(p1, VEH_TRAIN, _current_player)) return_cmd_error(STR_RAIL_VEHICLE_NOT_AVAILABLE); |
1196
115f46e3807d
(svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents:
1192
diff
changeset
|
695 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
696 |
/* Check if the train is actually being built in a depot belonging |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
697 |
* to the player. Doesn't matter if only the cost is queried */ |
1443
d9c101f7634b
(svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents:
1438
diff
changeset
|
698 |
if (!(flags & DC_QUERY_COST)) { |
10242
52b4a9006029
(svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents:
10211
diff
changeset
|
699 |
if (!IsRailDepotTile(tile)) return CMD_ERROR; |
1901
fb05044cf5c3
(svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents:
1895
diff
changeset
|
700 |
if (!IsTileOwner(tile, _current_player)) return CMD_ERROR; |
1443
d9c101f7634b
(svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents:
1438
diff
changeset
|
701 |
} |
1221
2b159ebb4899
(svn r1725) - Fix: now trains can only be built in depots and you have to own the depot too (hacked client protection)
bjarni
parents:
1214
diff
changeset
|
702 |
|
6476 | 703 |
const RailVehicleInfo *rvi = RailVehInfo(p1); |
2244 | 704 |
|
6119
b47985557d1e
(svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents:
6109
diff
changeset
|
705 |
if (rvi->railveh_type == RAILVEH_WAGON) return CmdBuildRailWagon(p1, tile, flags); |
0 | 706 |
|
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
707 |
CommandCost value = EstimateTrainCost(p1, rvi); |
6476 | 708 |
|
709 |
uint num_vehicles = |
|
710 |
(rvi->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
711 |
CountArticulatedParts(p1, false); |
0 | 712 |
|
713 |
if (!(flags & DC_QUERY_COST)) { |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
714 |
/* Check if depot and new engine uses the same kind of tracks * |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
715 |
* We need to see if the engine got power on the tile to avoid eletric engines in non-electric depots */ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
716 |
if (!HasPowerOnRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
717 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
718 |
/* Allow for the dual-heads and the articulated parts, plus one to "terminate" the list. */ |
10994
cd9968b6f96b
(svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents:
10991
diff
changeset
|
719 |
Vehicle **vl = AllocaM(Vehicle*, num_vehicles + 1); |
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
720 |
memset(vl, 0, sizeof(*vl) * (num_vehicles + 1)); |
4831
327d181a60ee
(svn r6755) - Fix: Pass the newly created vehicle when checking for articulated engines. As this could result in more parts being added than previously counted, we check to see if we need to allocate more vehicles as we add parts.
peter1138
parents:
4806
diff
changeset
|
721 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
722 |
if (!Vehicle::AllocateList(vl, num_vehicles)) { |
0 | 723 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
724 |
} |
0 | 725 |
|
6476 | 726 |
Vehicle *v = vl[0]; |
727 |
||
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
728 |
UnitID unit_num = HasBit(p2, 0) ? 0 : GetFreeUnitNumber(VEH_TRAIN); |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
729 |
if (unit_num > _settings_game.vehicle.max_trains) |
3816
2cdb29b57c1f
(svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents:
3815
diff
changeset
|
730 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
2cdb29b57c1f
(svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents:
3815
diff
changeset
|
731 |
|
0 | 732 |
if (flags & DC_EXEC) { |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
733 |
DiagDirection dir = GetRailDepotDirection(tile); |
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
734 |
int x = TileX(tile) * TILE_SIZE + _vehicle_initial_x_fract[dir]; |
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
735 |
int y = TileY(tile) * TILE_SIZE + _vehicle_initial_y_fract[dir]; |
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
736 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
737 |
v = new (v) Train(); |
0 | 738 |
v->unitnumber = unit_num; |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3140
diff
changeset
|
739 |
v->direction = DiagDirToDir(dir); |
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
740 |
v->tile = tile; |
0 | 741 |
v->owner = _current_player; |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
742 |
v->x_pos = x; |
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
743 |
v->y_pos = y; |
6476 | 744 |
v->z_pos = GetSlopeZ(x, y); |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
745 |
// v->running_ticks = 0; |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
746 |
v->u.rail.track = TRACK_BIT_DEPOT; |
0 | 747 |
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL; |
748 |
v->spritenum = rvi->image_index; |
|
749 |
v->cargo_type = rvi->cargo_type; |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
750 |
// v->cargo_subtype = 0; |
0 | 751 |
v->cargo_cap = rvi->capacity; |
752 |
v->max_speed = rvi->max_speed; |
|
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
753 |
v->value = value.GetCost(); |
1266
9dc5638fe8cc
(svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents:
1247
diff
changeset
|
754 |
v->last_station_visited = INVALID_STATION; |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
755 |
// v->dest_tile = 0; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
756 |
|
2477
d454d5152386
(svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents:
2475
diff
changeset
|
757 |
v->engine_type = p1; |
0 | 758 |
|
6074 | 759 |
const Engine *e = GetEngine(p1); |
0 | 760 |
v->reliability = e->reliability; |
761 |
v->reliability_spd_dec = e->reliability_spd_dec; |
|
762 |
v->max_age = e->lifelength * 366; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
763 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
764 |
v->name = NULL; |
6074 | 765 |
v->u.rail.railtype = rvi->railtype; |
2564 | 766 |
_new_vehicle_id = v->index; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
767 |
|
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
768 |
v->service_interval = _settings_game.vehicle.servint_trains; |
0 | 769 |
v->date_of_last_service = _date; |
4329
9759d5c52010
(svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents:
4293
diff
changeset
|
770 |
v->build_year = _cur_year; |
0 | 771 |
v->cur_image = 0xAC2; |
2804
b932fe148d36
(svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents:
2758
diff
changeset
|
772 |
v->random_bits = VehicleRandomBits(); |
0 | 773 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
774 |
// v->vehicle_flags = 0; |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
775 |
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE); |
6502
7ca154aca2b7
(svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents:
6501
diff
changeset
|
776 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
777 |
v->group_id = DEFAULT_GROUP; |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
778 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
779 |
// v->subtype = 0; |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
780 |
SetFrontEngine(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
781 |
SetTrainEngine(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
782 |
|
0 | 783 |
VehiclePositionChanged(v); |
784 |
||
6119
b47985557d1e
(svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents:
6109
diff
changeset
|
785 |
if (rvi->railveh_type == RAILVEH_MULTIHEAD) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
786 |
SetMultiheaded(v); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
787 |
AddRearEngineToMultiheadedTrain(vl[0], vl[1], true); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
788 |
/* Now we need to link the front and rear engines together |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
789 |
* other_multiheaded_part is the pointer that links to the other half of the engine |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
790 |
* vl[0] is the front and vl[1] is the rear |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
791 |
*/ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
792 |
vl[0]->u.rail.other_multiheaded_part = vl[1]; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
793 |
vl[1]->u.rail.other_multiheaded_part = vl[0]; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
794 |
} else { |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
795 |
AddArticulatedParts(vl, VEH_TRAIN); |
2244 | 796 |
} |
0 | 797 |
|
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
798 |
TrainConsistChanged(v); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
799 |
UpdateTrainGroupID(v); |
2244 | 800 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
801 |
if (!HasBit(p2, 1)) { // check if the cars should be added to the new vehicle |
2244 | 802 |
NormalizeTrainVehInDepot(v); |
803 |
} |
|
0 | 804 |
|
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
805 |
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); |
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
806 |
InvalidateWindowClassesData(WC_TRAINS_LIST, 0); |
0 | 807 |
InvalidateWindow(WC_COMPANY, v->owner); |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
808 |
if (IsLocalPlayer()) { |
6800
6c09e1e86fcb
(svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents:
6743
diff
changeset
|
809 |
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Train window |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
810 |
} |
6195
b90cf92697b9
(svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents:
6173
diff
changeset
|
811 |
|
b90cf92697b9
(svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents:
6173
diff
changeset
|
812 |
GetPlayer(_current_player)->num_engines[p1]++; |
0 | 813 |
} |
814 |
} |
|
1128
d3ffc98b92ad
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1109
diff
changeset
|
815 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
816 |
return value; |
0 | 817 |
} |
818 |
||
819 |
||
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
820 |
/* Check if all the wagons of the given train are in a depot, returns the |
3183
cf71bd234ebd
(svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents:
3172
diff
changeset
|
821 |
* number of cars (including loco) then. If not it returns -1 */ |
4648
ab94e3a447a8
(svn r6524) -Code cleanup r6515: cleaned up the command to start/stop all vehicles in a depot.
bjarni
parents:
4632
diff
changeset
|
822 |
int CheckTrainInDepot(const Vehicle *v, bool needs_to_be_stopped) |
0 | 823 |
{ |
824 |
TileIndex tile = v->tile; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
825 |
|
0 | 826 |
/* check if stopped in a depot */ |
10242
52b4a9006029
(svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents:
10211
diff
changeset
|
827 |
if (!IsRailDepotTile(tile) || v->cur_speed != 0) return -1; |
0 | 828 |
|
6476 | 829 |
int count = 0; |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
830 |
for (; v != NULL; v = v->Next()) { |
2844 | 831 |
/* This count is used by the depot code to determine the number of engines |
832 |
* in the consist. Exclude articulated parts so that autoreplacing to |
|
3986
fdffc0b284cf
(svn r5176) fixing the typo, that should have been fixed in last commit
bjarni
parents:
3985
diff
changeset
|
833 |
* engines with more articulated parts than before works correctly. |
3985
87cf4955787d
(svn r5175) -Fix: [autoreplace] FS#186 autoreplaced trains can leave all wagons in depot
bjarni
parents:
3977
diff
changeset
|
834 |
* |
87cf4955787d
(svn r5175) -Fix: [autoreplace] FS#186 autoreplaced trains can leave all wagons in depot
bjarni
parents:
3977
diff
changeset
|
835 |
* Also skip counting rear ends of multiheaded engines */ |
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
836 |
if (!IsArticulatedPart(v) && !IsRearDualheaded(v)) count++; |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
837 |
if (v->u.rail.track != TRACK_BIT_DEPOT || v->tile != tile || |
4529
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
838 |
(IsFrontEngine(v) && needs_to_be_stopped && !(v->vehstatus & VS_STOPPED))) { |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
839 |
return -1; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
840 |
} |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
841 |
} |
0 | 842 |
|
843 |
return count; |
|
844 |
} |
|
845 |
||
4529
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
846 |
/* Used to check if the train is inside the depot and verifying that the VS_STOPPED flag is set */ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
847 |
int CheckTrainStoppedInDepot(const Vehicle *v) |
4529
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
848 |
{ |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
849 |
return CheckTrainInDepot(v, true); |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
850 |
} |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
851 |
|
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
852 |
/* Used to check if the train is inside the depot, but not checking the VS_STOPPED flag */ |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
853 |
inline bool CheckTrainIsInsideDepot(const Vehicle *v) |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
854 |
{ |
6476 | 855 |
return CheckTrainInDepot(v, false) > 0; |
4529
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
856 |
} |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
857 |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
858 |
/** |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
859 |
* Unlink a rail wagon from the consist. |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
860 |
* @param v Vehicle to remove. |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
861 |
* @param first The first vehicle of the consist. |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
862 |
* @return The first vehicle of the consist. |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
863 |
*/ |
0 | 864 |
static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first) |
865 |
{ |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
866 |
/* unlinking the first vehicle of the chain? */ |
0 | 867 |
if (v == first) { |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
868 |
v = GetNextVehicle(v); |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
869 |
if (v == NULL) return NULL; |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
870 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
871 |
if (IsTrainWagon(v)) SetFreeWagon(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
872 |
|
0 | 873 |
return v; |
874 |
} |
|
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
875 |
|
6476 | 876 |
Vehicle *u; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
877 |
for (u = first; GetNextVehicle(u) != v; u = GetNextVehicle(u)) {} |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
878 |
GetLastEnginePart(u)->SetNext(GetNextVehicle(v)); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
879 |
return first; |
0 | 880 |
} |
881 |
||
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
882 |
static Vehicle *FindGoodVehiclePos(const Vehicle *src) |
0 | 883 |
{ |
884 |
Vehicle *dst; |
|
2477
d454d5152386
(svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents:
2475
diff
changeset
|
885 |
EngineID eng = src->engine_type; |
0 | 886 |
TileIndex tile = src->tile; |
887 |
||
888 |
FOR_ALL_VEHICLES(dst) { |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
889 |
if (dst->type == VEH_TRAIN && IsFreeWagon(dst) && dst->tile == tile && !HASBITS(dst->vehstatus, VS_CRASHED)) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
890 |
/* check so all vehicles in the line have the same engine. */ |
0 | 891 |
Vehicle *v = dst; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
892 |
|
0 | 893 |
while (v->engine_type == eng) { |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
894 |
v = v->Next(); |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
895 |
if (v == NULL) return dst; |
0 | 896 |
} |
897 |
} |
|
898 |
} |
|
899 |
||
900 |
return NULL; |
|
901 |
} |
|
902 |
||
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
903 |
/* |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
904 |
* add a vehicle v behind vehicle dest |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
905 |
* use this function since it sets flags as needed |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
906 |
*/ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
907 |
static void AddWagonToConsist(Vehicle *v, Vehicle *dest) |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
908 |
{ |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
909 |
UnlinkWagon(v, v->First()); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
910 |
if (dest == NULL) return; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
911 |
|
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
912 |
Vehicle *next = dest->Next(); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
913 |
v->SetNext(NULL); |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
914 |
dest->SetNext(v); |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
915 |
v->SetNext(next); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
916 |
ClearFreeWagon(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
917 |
ClearFrontEngine(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
918 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
919 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
920 |
/* |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
921 |
* move around on the train so rear engines are placed correctly according to the other engines |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
922 |
* always call with the front engine |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
923 |
*/ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
924 |
static void NormaliseTrainConsist(Vehicle *v) |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
925 |
{ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
926 |
if (IsFreeWagon(v)) return; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
927 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
928 |
assert(IsFrontEngine(v)); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
929 |
|
2952 | 930 |
for (; v != NULL; v = GetNextVehicle(v)) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
931 |
if (!IsMultiheaded(v) || !IsTrainEngine(v)) continue; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
932 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
933 |
/* make sure that there are no free cars before next engine */ |
6476 | 934 |
Vehicle *u; |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
935 |
for (u = v; u->Next() != NULL && !IsTrainEngine(u->Next()); u = u->Next()) {} |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
936 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
937 |
if (u == v->u.rail.other_multiheaded_part) continue; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
938 |
AddWagonToConsist(v->u.rail.other_multiheaded_part, u); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
939 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
940 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
941 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
942 |
/** Move a rail vehicle around inside the depot. |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
943 |
* @param tile unused |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
944 |
* @param flags type of operation |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
945 |
* @param p1 various bitstuffed elements |
2639 | 946 |
* - p1 (bit 0 - 15) source vehicle index |
947 |
* - p1 (bit 16 - 31) what wagon to put the source wagon AFTER, XXX - INVALID_VEHICLE to make a new line |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
948 |
* @param p2 (bit 0) move all vehicles following the source vehicle |
0 | 949 |
*/ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
950 |
CommandCost CmdMoveRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 951 |
{ |
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
952 |
VehicleID s = GB(p1, 0, 16); |
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
953 |
VehicleID d = GB(p1, 16, 16); |
0 | 954 |
|
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
955 |
if (!IsValidVehicleID(s)) return CMD_ERROR; |
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
956 |
|
6476 | 957 |
Vehicle *src = GetVehicle(s); |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
958 |
|
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
959 |
if (src->type != VEH_TRAIN || !CheckOwnership(src->owner)) return CMD_ERROR; |
0 | 960 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
961 |
/* Do not allow moving crashed vehicles inside the depot, it is likely to cause asserts later */ |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
962 |
if (HASBITS(src->vehstatus, VS_CRASHED)) return CMD_ERROR; |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
963 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
964 |
/* if nothing is selected as destination, try and find a matching vehicle to drag to. */ |
6476 | 965 |
Vehicle *dst; |
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
966 |
if (d == INVALID_VEHICLE) { |
4198
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
967 |
dst = IsTrainEngine(src) ? NULL : FindGoodVehiclePos(src); |
0 | 968 |
} else { |
6462 | 969 |
if (!IsValidVehicleID(d)) return CMD_ERROR; |
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
970 |
dst = GetVehicle(d); |
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
971 |
if (dst->type != VEH_TRAIN || !CheckOwnership(dst->owner)) return CMD_ERROR; |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
972 |
|
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
973 |
/* Do not allow appending to crashed vehicles, too */ |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
974 |
if (HASBITS(dst->vehstatus, VS_CRASHED)) return CMD_ERROR; |
0 | 975 |
} |
976 |
||
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
977 |
/* if an articulated part is being handled, deal with its parent vehicle */ |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
978 |
while (IsArticulatedPart(src)) src = src->Previous(); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
979 |
if (dst != NULL) { |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
980 |
while (IsArticulatedPart(dst)) dst = dst->Previous(); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
981 |
} |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
982 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
983 |
/* don't move the same vehicle.. */ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
984 |
if (src == dst) return CommandCost(); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
985 |
|
0 | 986 |
/* locate the head of the two chains */ |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
987 |
Vehicle *src_head = src->First(); |
6476 | 988 |
Vehicle *dst_head; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
989 |
if (dst != NULL) { |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
990 |
dst_head = dst->First(); |
6462 | 991 |
if (dst_head->tile != src_head->tile) return CMD_ERROR; |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
992 |
/* Now deal with articulated part of destination wagon */ |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
993 |
dst = GetLastEnginePart(dst); |
6462 | 994 |
} else { |
995 |
dst_head = NULL; |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
996 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
997 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
998 |
if (IsRearDualheaded(src)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
999 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1000 |
/* when moving all wagons, we can't have the same src_head and dst_head */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1001 |
if (HasBit(p2, 0) && src_head == dst_head) return CommandCost(); |
0 | 1002 |
|
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1003 |
{ |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
1004 |
int max_len = _settings_game.vehicle.mammoth_trains ? 100 : 10; |
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1005 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1006 |
/* check if all vehicles in the source train are stopped inside a depot. */ |
6476 | 1007 |
int src_len = CheckTrainStoppedInDepot(src_head); |
3183
cf71bd234ebd
(svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents:
3172
diff
changeset
|
1008 |
if (src_len < 0) return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED); |
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1009 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1010 |
/* check the destination row if the source and destination aren't the same. */ |
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1011 |
if (src_head != dst_head) { |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1012 |
int dst_len = 0; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1013 |
|
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1014 |
if (dst_head != NULL) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1015 |
/* check if all vehicles in the dest train are stopped. */ |
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1016 |
dst_len = CheckTrainStoppedInDepot(dst_head); |
3183
cf71bd234ebd
(svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents:
3172
diff
changeset
|
1017 |
if (dst_len < 0) return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED); |
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1018 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1019 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1020 |
/* We are moving between rows, so only count the wagons from the source |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1021 |
* row that are being moved. */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1022 |
if (HasBit(p2, 0)) { |
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1023 |
const Vehicle *u; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1024 |
for (u = src_head; u != src && u != NULL; u = GetNextVehicle(u)) |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1025 |
src_len--; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1026 |
} else { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1027 |
/* If moving only one vehicle, just count that. */ |
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1028 |
src_len = 1; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1029 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1030 |
|
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1031 |
if (src_len + dst_len > max_len) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1032 |
/* Abort if we're adding too many wagons to a train. */ |
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1033 |
if (dst_head != NULL && IsFrontEngine(dst_head)) return_cmd_error(STR_8819_TRAIN_TOO_LONG); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1034 |
/* Abort if we're making a train on a new row. */ |
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1035 |
if (dst_head == NULL && IsTrainEngine(src)) return_cmd_error(STR_8819_TRAIN_TOO_LONG); |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1036 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1037 |
} else { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1038 |
/* Abort if we're creating a new train on an existing row. */ |
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1039 |
if (src_len > max_len && src == src_head && IsTrainEngine(GetNextVehicle(src_head))) |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1040 |
return_cmd_error(STR_8819_TRAIN_TOO_LONG); |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1041 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1042 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1043 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1044 |
/* moving a loco to a new line?, then we need to assign a unitnumber. */ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1045 |
if (dst == NULL && !IsFrontEngine(src) && IsTrainEngine(src)) { |
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
1046 |
UnitID unit_num = GetFreeUnitNumber(VEH_TRAIN); |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
1047 |
if (unit_num > _settings_game.vehicle.max_trains) |
0 | 1048 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
1049 |
||
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1050 |
if (flags & DC_EXEC) src->unitnumber = unit_num; |
0 | 1051 |
} |
1052 |
||
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1053 |
/* |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1054 |
* Check whether the vehicles in the source chain are in the destination |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1055 |
* chain. This can easily be done by checking whether the first vehicle |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1056 |
* of the source chain is in the destination chain as the Next/Previous |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1057 |
* pointers always make a doubly linked list of it where the assumption |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1058 |
* v->Next()->Previous() == v holds (assuming v->Next() != NULL). |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1059 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1060 |
bool src_in_dst = false; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1061 |
for (Vehicle *v = dst_head; !src_in_dst && v != NULL; v = v->Next()) src_in_dst = v == src; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1062 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1063 |
/* |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1064 |
* If the source chain is in the destination chain then the user is |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1065 |
* only reordering the vehicles, thus not attaching a new vehicle. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1066 |
* Therefor the 'allow wagon attach' callback does not need to be |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1067 |
* called. If it would be called strange things would happen because |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1068 |
* one 'attaches' an already 'attached' vehicle causing more trouble |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1069 |
* than it actually solves (infinite loops and such). |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1070 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1071 |
if (dst_head != NULL && !src_in_dst) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1072 |
/* |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1073 |
* When performing the 'allow wagon attach' callback, we have to check |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1074 |
* that for each and every wagon, not only the first one. This means |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1075 |
* that we have to test one wagon, attach it to the train and then test |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1076 |
* the next wagon till we have reached the end. We have to restore it |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1077 |
* to the state it was before we 'tried' attaching the train when the |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1078 |
* attaching fails or succeeds because we are not 'only' doing this |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1079 |
* in the DC_EXEC state. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1080 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1081 |
Vehicle *dst_tail = dst_head; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1082 |
while (dst_tail->Next() != NULL) dst_tail = dst_tail->Next(); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1083 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1084 |
Vehicle *orig_tail = dst_tail; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1085 |
Vehicle *next_to_attach = src; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1086 |
Vehicle *src_previous = src->Previous(); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1087 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1088 |
while (next_to_attach != NULL) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1089 |
/* Back up and clear the first_engine data to avoid using wagon override group */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1090 |
EngineID first_engine = next_to_attach->u.rail.first_engine; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1091 |
next_to_attach->u.rail.first_engine = INVALID_ENGINE; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1092 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1093 |
uint16 callback = GetVehicleCallbackParent(CBID_TRAIN_ALLOW_WAGON_ATTACH, 0, 0, dst_head->engine_type, next_to_attach, dst_head); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1094 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1095 |
/* Restore original first_engine data */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1096 |
next_to_attach->u.rail.first_engine = first_engine; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1097 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1098 |
if (callback != CALLBACK_FAILED) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1099 |
StringID error = STR_NULL; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1100 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1101 |
if (callback == 0xFD) error = STR_INCOMPATIBLE_RAIL_TYPES; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1102 |
if (callback < 0xFD) error = GetGRFStringID(GetEngineGRFID(dst_head->engine_type), 0xD000 + callback); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1103 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1104 |
if (error != STR_NULL) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1105 |
/* |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1106 |
* The attaching is not allowed. In this case 'next_to_attach' |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1107 |
* can contain some vehicles of the 'source' and the destination |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1108 |
* train can have some too. We 'just' add the to-be added wagons |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1109 |
* to the chain and then split it where it was previously |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1110 |
* separated, i.e. the tail of the original destination train. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1111 |
* Furthermore the 'previous' link of the original source vehicle needs |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1112 |
* to be restored, otherwise the train goes missing in the depot. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1113 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1114 |
dst_tail->SetNext(next_to_attach); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1115 |
orig_tail->SetNext(NULL); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1116 |
if (src_previous != NULL) src_previous->SetNext(src); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1117 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1118 |
return_cmd_error(error); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1119 |
} |
3727
b4d9a8ab4ce4
(svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents:
3657
diff
changeset
|
1120 |
} |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1121 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1122 |
/* Only check further wagons if told to move the chain */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1123 |
if (!HasBit(p2, 0)) break; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1124 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1125 |
/* |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1126 |
* Adding a next wagon to the chain so we can test the other wagons. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1127 |
* First 'take' the first wagon from 'next_to_attach' and move it |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1128 |
* to the next wagon. Then add that to the tail of the destination |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1129 |
* train and update the tail with the new vehicle. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1130 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1131 |
Vehicle *to_add = next_to_attach; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1132 |
next_to_attach = next_to_attach->Next(); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1133 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1134 |
to_add->SetNext(NULL); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1135 |
dst_tail->SetNext(to_add); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1136 |
dst_tail = dst_tail->Next(); |
3727
b4d9a8ab4ce4
(svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents:
3657
diff
changeset
|
1137 |
} |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1138 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1139 |
/* |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1140 |
* When we reach this the attaching is allowed. It also means that the |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1141 |
* chain of vehicles to attach is empty, so we do not need to merge that. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1142 |
* This means only the splitting needs to be done. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1143 |
* Furthermore the 'previous' link of the original source vehicle needs |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1144 |
* to be restored, otherwise the train goes missing in the depot. |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1145 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1146 |
orig_tail->SetNext(NULL); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1147 |
if (src_previous != NULL) src_previous->SetNext(src); |
3727
b4d9a8ab4ce4
(svn r4703) - NewGRF: add support for callback 0x1D, "can wagon be attached". This can be used to disallow freight wagons to be placed in passenger trains, along with other uses.
peter1138
parents:
3657
diff
changeset
|
1148 |
} |
0 | 1149 |
|
1150 |
/* do it? */ |
|
1151 |
if (flags & DC_EXEC) { |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1152 |
/* If we move the front Engine and if the second vehicle is not an engine |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1153 |
add the whole vehicle to the DEFAULT_GROUP */ |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1154 |
if (IsFrontEngine(src) && !IsDefaultGroupID(src->group_id)) { |
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
1155 |
Vehicle *v = GetNextVehicle(src); |
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
1156 |
|
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
1157 |
if (v != NULL && IsTrainEngine(v)) { |
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
1158 |
v->group_id = src->group_id; |
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
1159 |
src->group_id = DEFAULT_GROUP; |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1160 |
} |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1161 |
} |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1162 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1163 |
if (HasBit(p2, 0)) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1164 |
/* unlink ALL wagons */ |
0 | 1165 |
if (src != src_head) { |
1166 |
Vehicle *v = src_head; |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
1167 |
while (GetNextVehicle(v) != src) v = GetNextVehicle(v); |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1168 |
GetLastEnginePart(v)->SetNext(NULL); |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1169 |
} else { |
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1170 |
InvalidateWindowData(WC_VEHICLE_DEPOT, src_head->tile); // We removed a line |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1171 |
src_head = NULL; |
0 | 1172 |
} |
1173 |
} else { |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1174 |
/* if moving within the same chain, dont use dst_head as it may get invalidated */ |
4198
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
1175 |
if (src_head == dst_head) dst_head = NULL; |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1176 |
/* unlink single wagon from linked list */ |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1177 |
src_head = UnlinkWagon(src, src_head); |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1178 |
GetLastEnginePart(src)->SetNext(NULL); |
0 | 1179 |
} |
1180 |
||
1181 |
if (dst == NULL) { |
|
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1182 |
/* We make a new line in the depot, so we know already that we invalidate the window data */ |
4806
a75776d5c5a4
(svn r6728) -Fix r6651: solved issue where moving rail vehicles in a depot could read from a NULL pointer
bjarni
parents:
4739
diff
changeset
|
1183 |
InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile); |
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1184 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1185 |
/* move the train to an empty line. for locomotives, we set the type to TS_Front. for wagons, 4. */ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1186 |
if (IsTrainEngine(src)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1187 |
if (!IsFrontEngine(src)) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1188 |
/* setting the type to 0 also involves setting up the orders field. */ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1189 |
SetFrontEngine(src); |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1190 |
assert(src->orders == NULL); |
0 | 1191 |
src->num_orders = 0; |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1192 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1193 |
/* Decrease the engines number of the src engine_type */ |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1194 |
if (!IsDefaultGroupID(src->group_id) && IsValidGroupID(src->group_id)) { |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1195 |
GetGroup(src->group_id)->num_engines[src->engine_type]--; |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1196 |
} |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1197 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1198 |
/* If we move an engine to a new line affect it to the DEFAULT_GROUP */ |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1199 |
src->group_id = DEFAULT_GROUP; |
0 | 1200 |
} |
1201 |
} else { |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1202 |
SetFreeWagon(src); |
0 | 1203 |
} |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1204 |
dst_head = src; |
0 | 1205 |
} else { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1206 |
if (IsFrontEngine(src)) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1207 |
/* the vehicle was previously a loco. need to free the order list and delete vehicle windows etc. */ |
0 | 1208 |
DeleteWindowById(WC_VEHICLE_VIEW, src->index); |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1209 |
DeleteVehicleOrders(src); |
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
1210 |
RemoveVehicleFromGroup(src); |
0 | 1211 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1212 |
|
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1213 |
if (IsFrontEngine(src) || IsFreeWagon(src)) { |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1214 |
InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile); |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1215 |
ClearFrontEngine(src); |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1216 |
ClearFreeWagon(src); |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1217 |
src->unitnumber = 0; // doesn't occupy a unitnumber anymore. |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1218 |
} |
0 | 1219 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1220 |
/* link in the wagon(s) in the chain. */ |
0 | 1221 |
{ |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
1222 |
Vehicle *v; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
1223 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1224 |
for (v = src; GetNextVehicle(v) != NULL; v = GetNextVehicle(v)) {} |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1225 |
GetLastEnginePart(v)->SetNext(dst->Next()); |
0 | 1226 |
} |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1227 |
dst->SetNext(src); |
0 | 1228 |
} |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1229 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1230 |
if (src->u.rail.other_multiheaded_part != NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1231 |
if (src->u.rail.other_multiheaded_part == src_head) { |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1232 |
src_head = src_head->Next(); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1233 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1234 |
AddWagonToConsist(src->u.rail.other_multiheaded_part, src); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1235 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1236 |
|
2607
08f42ae6fbfc
(svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents:
2602
diff
changeset
|
1237 |
/* If there is an engine behind first_engine we moved away, it should become new first_engine |
08f42ae6fbfc
(svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents:
2602
diff
changeset
|
1238 |
* To do this, CmdMoveRailVehicle must be called once more |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1239 |
* we can't loop forever here because next time we reach this line we will have a front engine */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1240 |
if (src_head != NULL && !IsFrontEngine(src_head) && IsTrainEngine(src_head)) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1241 |
/* As in CmdMoveRailVehicle src_head->group_id will be equal to DEFAULT_GROUP |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1242 |
* we need to save the group and reaffect it to src_head */ |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1243 |
const GroupID tmp_g = src_head->group_id; |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
1244 |
CmdMoveRailVehicle(0, flags, src_head->index | (INVALID_VEHICLE << 16), 1); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1245 |
SetTrainGroupID(src_head, tmp_g); |
4434
4175805666a5
(svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents:
4432
diff
changeset
|
1246 |
src_head = NULL; // don't do anything more to this train since the new call will do it |
2607
08f42ae6fbfc
(svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents:
2602
diff
changeset
|
1247 |
} |
08f42ae6fbfc
(svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents:
2602
diff
changeset
|
1248 |
|
4198
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
1249 |
if (src_head != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1250 |
NormaliseTrainConsist(src_head); |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
1251 |
TrainConsistChanged(src_head); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1252 |
UpdateTrainGroupID(src_head); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1253 |
if (IsFrontEngine(src_head)) { |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1254 |
/* Update the refit button and window */ |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1255 |
InvalidateWindow(WC_VEHICLE_REFIT, src_head->index); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1256 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, src_head->index, VVW_WIDGET_REFIT_VEH); |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1257 |
} |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1258 |
/* Update the depot window */ |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1259 |
InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile); |
6476 | 1260 |
} |
0 | 1261 |
|
4198
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
1262 |
if (dst_head != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1263 |
NormaliseTrainConsist(dst_head); |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1264 |
TrainConsistChanged(dst_head); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1265 |
UpdateTrainGroupID(dst_head); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1266 |
if (IsFrontEngine(dst_head)) { |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1267 |
/* Update the refit button and window */ |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1268 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, dst_head->index, VVW_WIDGET_REFIT_VEH); |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1269 |
InvalidateWindow(WC_VEHICLE_REFIT, dst_head->index); |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
1270 |
} |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1271 |
/* Update the depot window */ |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1272 |
InvalidateWindow(WC_VEHICLE_DEPOT, dst_head->tile); |
0 | 1273 |
} |
1274 |
||
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
1275 |
InvalidateWindowClassesData(WC_TRAINS_LIST, 0); |
0 | 1276 |
} |
1277 |
||
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1278 |
return CommandCost(); |
0 | 1279 |
} |
1280 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1281 |
/** Start/Stop a train. |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
1282 |
* @param tile unused |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1283 |
* @param flags type of operation |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1284 |
* @param p1 train to start/stop |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1285 |
* @param p2 unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1286 |
*/ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1287 |
CommandCost CmdStartStopTrain(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 1288 |
{ |
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
1289 |
if (!IsValidVehicleID(p1)) return CMD_ERROR; |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1290 |
|
6476 | 1291 |
Vehicle *v = GetVehicle(p1); |
0 | 1292 |
|
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
1293 |
if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1294 |
|
4244
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
1295 |
/* Check if this train can be started/stopped. The callback will fail or |
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
1296 |
* return 0xFF if it can. */ |
6476 | 1297 |
uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v); |
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
1298 |
if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF) { |
4244
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
1299 |
StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback); |
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
1300 |
return_cmd_error(error); |
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
1301 |
} |
44dcec623504
(svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents:
4242
diff
changeset
|
1302 |
|
4251
85befcd5f435
(svn r5853) -Fix: [elrails] FS#178 Electric Trains can leave Conventional Depot
bjarni
parents:
4244
diff
changeset
|
1303 |
if (v->vehstatus & VS_STOPPED && v->u.rail.cached_power == 0) return_cmd_error(STR_TRAIN_START_NO_CATENARY); |
85befcd5f435
(svn r5853) -Fix: [elrails] FS#178 Electric Trains can leave Conventional Depot
bjarni
parents:
4244
diff
changeset
|
1304 |
|
0 | 1305 |
if (flags & DC_EXEC) { |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
1306 |
if (v->vehstatus & VS_STOPPED && v->u.rail.track == TRACK_BIT_DEPOT) { |
3139
4c950c7ec5c9
(svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents:
3022
diff
changeset
|
1307 |
DeleteVehicleNews(p1, STR_8814_TRAIN_IS_WAITING_IN_DEPOT); |
4c950c7ec5c9
(svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents:
3022
diff
changeset
|
1308 |
} |
4c950c7ec5c9
(svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents:
3022
diff
changeset
|
1309 |
|
0 | 1310 |
v->vehstatus ^= VS_STOPPED; |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1311 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
0 | 1312 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
1313 |
} |
|
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1314 |
return CommandCost(); |
0 | 1315 |
} |
1316 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1317 |
/** Sell a (single) train wagon/engine. |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
1318 |
* @param tile unused |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1319 |
* @param flags type of operation |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1320 |
* @param p1 the wagon/engine index |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1321 |
* @param p2 the selling mode |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1322 |
* - p2 = 0: only sell the single dragged wagon/engine (and any belonging rear-engines) |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1323 |
* - p2 = 1: sell the vehicle and all vehicles following it in the chain |
10242
52b4a9006029
(svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents:
10211
diff
changeset
|
1324 |
* if the wagon is dragged, don't delete the possibly belonging rear-engine to some front |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1325 |
* - p2 = 2: when selling attached locos, rearrange all vehicles after it to separate lines; |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1326 |
* all wagons of the same type will go on the same line. Used by the AI currently |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1327 |
*/ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1328 |
CommandCost CmdSellRailWagon(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 1329 |
{ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1330 |
/* Check if we deleted a vehicle window */ |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1331 |
Window *w = NULL; |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1332 |
|
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
1333 |
if (!IsValidVehicleID(p1) || p2 > 2) return CMD_ERROR; |
0 | 1334 |
|
6476 | 1335 |
Vehicle *v = GetVehicle(p1); |
0 | 1336 |
|
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
1337 |
if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1338 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1339 |
if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1340 |
|
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1341 |
while (IsArticulatedPart(v)) v = v->Previous(); |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1342 |
Vehicle *first = v->First(); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1343 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1344 |
/* make sure the vehicle is stopped in the depot */ |
3183
cf71bd234ebd
(svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents:
3172
diff
changeset
|
1345 |
if (CheckTrainStoppedInDepot(first) < 0) { |
cf71bd234ebd
(svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents:
3172
diff
changeset
|
1346 |
return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED); |
cf71bd234ebd
(svn r3829) Reduce the use of _error_message by directly returning error codes instead of using this global variable
tron
parents:
3172
diff
changeset
|
1347 |
} |
0 | 1348 |
|
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
1349 |
if (IsRearDualheaded(v)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1350 |
|
1842
7efaede8ac8e
(svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents:
1802
diff
changeset
|
1351 |
if (flags & DC_EXEC) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1352 |
if (v == first && IsFrontEngine(first)) { |
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
1353 |
DeleteWindowById(WC_VEHICLE_VIEW, first->index); |
2618
7546b838230b
(svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents:
2617
diff
changeset
|
1354 |
} |
0 | 1355 |
InvalidateWindow(WC_VEHICLE_DEPOT, first->tile); |
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
1356 |
InvalidateWindowClassesData(WC_TRAINS_LIST, 0); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1357 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1358 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1359 |
CommandCost cost(EXPENSES_NEW_VEHICLES); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1360 |
switch (p2) { |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1361 |
case 0: case 2: { /* Delete given wagon */ |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1362 |
bool switch_engine = false; // update second wagon to engine? |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1363 |
byte ori_subtype = v->subtype; // backup subtype of deleted wagon in case DeleteVehicle() changes |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1364 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1365 |
/* 1. Delete the engine, if it is dualheaded also delete the matching |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1366 |
* rear engine of the loco (from the point of deletion onwards) */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1367 |
Vehicle *rear = (IsMultiheaded(v) && |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1368 |
IsTrainEngine(v)) ? v->u.rail.other_multiheaded_part : NULL; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1369 |
|
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1370 |
if (rear != NULL) { |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1371 |
cost.AddCost(-rear->value); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1372 |
if (flags & DC_EXEC) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1373 |
UnlinkWagon(rear, first); |
5256
90226d8da8fb
(svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents:
5252
diff
changeset
|
1374 |
DeleteDepotHighlightOfVehicle(rear); |
6800
6c09e1e86fcb
(svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents:
6743
diff
changeset
|
1375 |
delete rear; |
0 | 1376 |
} |
1377 |
} |
|
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1378 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1379 |
/* 2. We are selling the first engine, some special action might be required |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1380 |
* here, so take attention */ |
1770
b6bda4dc6da2
(svn r2274) - Codechange: some comments, parentheses and EngineID typedef for engine_type
Darkvater
parents:
1766
diff
changeset
|
1381 |
if ((flags & DC_EXEC) && v == first) { |
6476 | 1382 |
Vehicle *new_f = GetNextVehicle(first); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1383 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1384 |
/* 2.2 If there are wagons present after the deleted front engine, check |
6648
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1385 |
* if the second wagon (which will be first) is an engine. If it is one, |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1386 |
* promote it as a new train, retaining the unitnumber, orders */ |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1387 |
if (new_f != NULL && IsTrainEngine(new_f)) { |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1388 |
switch_engine = true; |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1389 |
/* Copy important data from the front engine */ |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1390 |
new_f->unitnumber = first->unitnumber; |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1391 |
new_f->current_order = first->current_order; |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1392 |
new_f->cur_order_index = first->cur_order_index; |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1393 |
new_f->orders = first->orders; |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1394 |
new_f->num_orders = first->num_orders; |
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
1395 |
new_f->group_id = first->group_id; |
6648
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1396 |
|
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1397 |
if (first->prev_shared != NULL) { |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1398 |
first->prev_shared->next_shared = new_f; |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1399 |
new_f->prev_shared = first->prev_shared; |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1400 |
} |
6648
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1401 |
|
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1402 |
if (first->next_shared != NULL) { |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1403 |
first->next_shared->prev_shared = new_f; |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1404 |
new_f->next_shared = first->next_shared; |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1405 |
} |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1406 |
|
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1407 |
/* |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1408 |
* Remove all order information from the front train, to |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1409 |
* prevent the order and the shared order list to be |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1410 |
* destroyed by Destroy/DeleteVehicle. |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1411 |
*/ |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1412 |
first->orders = NULL; |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1413 |
first->prev_shared = NULL; |
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1414 |
first->next_shared = NULL; |
6870
ca3fd1fbe311
(svn r11219) [NewGRF_ports] -Sync: with trunk r11035:11218.
rubidium
parents:
6868
diff
changeset
|
1415 |
first->group_id = DEFAULT_GROUP; |
6648
fe5626842ec1
(svn r9277) -Fix [FS#685]: shared orders got messed up when the 'first' trains got removed in the depot.
rubidium
parents:
6642
diff
changeset
|
1416 |
|
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1417 |
/* If we deleted a window then open a new one for the 'new' train */ |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1418 |
if (IsLocalPlayer() && w != NULL) ShowVehicleViewWindow(new_f); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1419 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1420 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1421 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1422 |
/* 3. Delete the requested wagon */ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1423 |
cost.AddCost(-v->value); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1424 |
if (flags & DC_EXEC) { |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1425 |
first = UnlinkWagon(v, first); |
5256
90226d8da8fb
(svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents:
5252
diff
changeset
|
1426 |
DeleteDepotHighlightOfVehicle(v); |
6800
6c09e1e86fcb
(svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents:
6743
diff
changeset
|
1427 |
delete v; |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1428 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1429 |
/* 4 If the second wagon was an engine, update it to front_engine |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1430 |
* which UnlinkWagon() has changed to TS_Free_Car */ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1431 |
if (switch_engine) SetFrontEngine(first); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1432 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1433 |
/* 5. If the train still exists, update its acceleration, window, etc. */ |
1917
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1434 |
if (first != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1435 |
NormaliseTrainConsist(first); |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
1436 |
TrainConsistChanged(first); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1437 |
UpdateTrainGroupID(first); |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1438 |
if (IsFrontEngine(first)) InvalidateWindow(WC_VEHICLE_REFIT, first->index); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1439 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1440 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1441 |
|
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1442 |
/* (6.) Borked AI. If it sells an engine it expects all wagons lined |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1443 |
* up on a new line to be added to the newly built loco. Replace it is. |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1444 |
* Totally braindead cause building a new engine adds all loco-less |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1445 |
* engines to its train anyways */ |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
1446 |
if (p2 == 2 && HasBit(ori_subtype, TS_FRONT)) { |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1447 |
for (v = first; v != NULL;) { |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1448 |
Vehicle *tmp = GetNextVehicle(v); |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
1449 |
DoCommand(v->tile, v->index | INVALID_VEHICLE << 16, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1450 |
v = tmp; |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1451 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1452 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1453 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1454 |
} break; |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1455 |
case 1: { /* Delete wagon and all wagons after it given certain criteria */ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1456 |
/* Start deleting every vehicle after the selected one |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1457 |
* If we encounter a matching rear-engine to a front-engine |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1458 |
* earlier in the chain (before deletion), leave it alone */ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1459 |
for (Vehicle *tmp; v != NULL; v = tmp) { |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
1460 |
tmp = GetNextVehicle(v); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1461 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1462 |
if (IsMultiheaded(v)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1463 |
if (IsTrainEngine(v)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1464 |
/* We got a front engine of a multiheaded set. Now we will sell the rear end too */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1465 |
Vehicle *rear = v->u.rail.other_multiheaded_part; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1466 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1467 |
if (rear != NULL) { |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1468 |
cost.AddCost(-rear->value); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1469 |
|
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1470 |
/* If this is a multiheaded vehicle with nothing |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1471 |
* between the parts, tmp will be pointing to the |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1472 |
* rear part, which is unlinked from the train and |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1473 |
* deleted here. However, because tmp has already |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1474 |
* been set it needs to be updated now so that the |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1475 |
* loop never sees the rear part. */ |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1476 |
if (tmp == rear) tmp = GetNextVehicle(tmp); |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1477 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1478 |
if (flags & DC_EXEC) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1479 |
first = UnlinkWagon(rear, first); |
5256
90226d8da8fb
(svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents:
5252
diff
changeset
|
1480 |
DeleteDepotHighlightOfVehicle(rear); |
6800
6c09e1e86fcb
(svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents:
6743
diff
changeset
|
1481 |
delete rear; |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1482 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1483 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1484 |
} else if (v->u.rail.other_multiheaded_part != NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1485 |
/* The front to this engine is earlier in this train. Do nothing */ |
2549 | 1486 |
continue; |
1487 |
} |
|
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1488 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1489 |
|
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1490 |
cost.AddCost(-v->value); |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1491 |
if (flags & DC_EXEC) { |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1492 |
first = UnlinkWagon(v, first); |
5256
90226d8da8fb
(svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents:
5252
diff
changeset
|
1493 |
DeleteDepotHighlightOfVehicle(v); |
6800
6c09e1e86fcb
(svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents:
6743
diff
changeset
|
1494 |
delete v; |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1495 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1496 |
} |
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1497 |
|
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
1498 |
/* 3. If it is still a valid train after selling, update its acceleration and cached values */ |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1499 |
if (flags & DC_EXEC && first != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1500 |
NormaliseTrainConsist(first); |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
1501 |
TrainConsistChanged(first); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1502 |
UpdateTrainGroupID(first); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1503 |
InvalidateWindow(WC_VEHICLE_REFIT, first->index); |
1905
f43d9e821deb
(svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents:
1901
diff
changeset
|
1504 |
} |
1766
7c048e309f18
(svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents:
1758
diff
changeset
|
1505 |
} break; |
0 | 1506 |
} |
1507 |
return cost; |
|
1508 |
} |
|
1509 |
||
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1510 |
void Train::UpdateDeltaXY(Direction direction) |
0 | 1511 |
{ |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1512 |
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0) |
0 | 1513 |
static const uint32 _delta_xy_table[8] = { |
1514 |
MKIT(3, 3, -1, -1), |
|
1515 |
MKIT(3, 7, -1, -3), |
|
1516 |
MKIT(3, 3, -1, -1), |
|
1517 |
MKIT(7, 3, -3, -1), |
|
1518 |
MKIT(3, 3, -1, -1), |
|
1519 |
MKIT(3, 7, -1, -3), |
|
1520 |
MKIT(3, 3, -1, -1), |
|
1521 |
MKIT(7, 3, -3, -1), |
|
1522 |
}; |
|
1523 |
#undef MKIT |
|
1524 |
||
1525 |
uint32 x = _delta_xy_table[direction]; |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1526 |
this->x_offs = GB(x, 0, 8); |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1527 |
this->y_offs = GB(x, 8, 8); |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1528 |
this->x_extent = GB(x, 16, 8); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1529 |
this->y_extent = GB(x, 24, 8); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1530 |
this->z_extent = 6; |
0 | 1531 |
} |
1532 |
||
1533 |
static void UpdateVarsAfterSwap(Vehicle *v) |
|
1534 |
{ |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1535 |
v->UpdateDeltaXY(v->direction); |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1536 |
v->cur_image = v->GetImage(v->direction); |
0 | 1537 |
BeginVehicleMove(v); |
1538 |
VehiclePositionChanged(v); |
|
1539 |
EndVehicleMove(v); |
|
1540 |
} |
|
1541 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1542 |
static inline void SetLastSpeed(Vehicle *v, int spd) |
2639 | 1543 |
{ |
0 | 1544 |
int old = v->u.rail.last_speed; |
1545 |
if (spd != old) { |
|
1546 |
v->u.rail.last_speed = spd; |
|
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
1547 |
if (_settings_client.gui.vehicle_speed || (old == 0) != (spd == 0)) { |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1548 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1549 |
} |
0 | 1550 |
} |
1551 |
} |
|
1552 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1553 |
static void SwapTrainFlags(byte *swap_flag1, byte *swap_flag2) |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1554 |
{ |
6476 | 1555 |
byte flag1 = *swap_flag1; |
1556 |
byte flag2 = *swap_flag2; |
|
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1557 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1558 |
/* Clear the flags */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1559 |
ClrBit(*swap_flag1, VRF_GOINGUP); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1560 |
ClrBit(*swap_flag1, VRF_GOINGDOWN); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1561 |
ClrBit(*swap_flag2, VRF_GOINGUP); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1562 |
ClrBit(*swap_flag2, VRF_GOINGDOWN); |
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1563 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1564 |
/* Reverse the rail-flags (if needed) */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1565 |
if (HasBit(flag1, VRF_GOINGUP)) { |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1566 |
SetBit(*swap_flag2, VRF_GOINGDOWN); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1567 |
} else if (HasBit(flag1, VRF_GOINGDOWN)) { |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1568 |
SetBit(*swap_flag2, VRF_GOINGUP); |
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1569 |
} |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1570 |
if (HasBit(flag2, VRF_GOINGUP)) { |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1571 |
SetBit(*swap_flag1, VRF_GOINGDOWN); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1572 |
} else if (HasBit(flag2, VRF_GOINGDOWN)) { |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1573 |
SetBit(*swap_flag1, VRF_GOINGUP); |
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1574 |
} |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1575 |
} |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1576 |
|
0 | 1577 |
static void ReverseTrainSwapVeh(Vehicle *v, int l, int r) |
1578 |
{ |
|
1579 |
Vehicle *a, *b; |
|
1580 |
||
1581 |
/* locate vehicles to swap */ |
|
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1582 |
for (a = v; l != 0; l--) a = a->Next(); |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1583 |
for (b = v; r != 0; r--) b = b->Next(); |
0 | 1584 |
|
1585 |
if (a != b) { |
|
1586 |
/* swap the hidden bits */ |
|
1587 |
{ |
|
1588 |
uint16 tmp = (a->vehstatus & ~VS_HIDDEN) | (b->vehstatus&VS_HIDDEN); |
|
1589 |
b->vehstatus = (b->vehstatus & ~VS_HIDDEN) | (a->vehstatus&VS_HIDDEN); |
|
1590 |
a->vehstatus = tmp; |
|
1591 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1592 |
|
5984 | 1593 |
Swap(a->u.rail.track, b->u.rail.track); |
1594 |
Swap(a->direction, b->direction); |
|
0 | 1595 |
|
1596 |
/* toggle direction */ |
|
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
1597 |
if (a->u.rail.track != TRACK_BIT_DEPOT) a->direction = ReverseDir(a->direction); |
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
1598 |
if (b->u.rail.track != TRACK_BIT_DEPOT) b->direction = ReverseDir(b->direction); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1599 |
|
5984 | 1600 |
Swap(a->x_pos, b->x_pos); |
1601 |
Swap(a->y_pos, b->y_pos); |
|
1602 |
Swap(a->tile, b->tile); |
|
1603 |
Swap(a->z_pos, b->z_pos); |
|
0 | 1604 |
|
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1605 |
SwapTrainFlags(&a->u.rail.flags, &b->u.rail.flags); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1606 |
|
0 | 1607 |
/* update other vars */ |
1608 |
UpdateVarsAfterSwap(a); |
|
1609 |
UpdateVarsAfterSwap(b); |
|
1554
c0b2287254f4
(svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents:
1552
diff
changeset
|
1610 |
|
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
1611 |
/* call the proper EnterTile function unless we are in a wormhole */ |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
1612 |
if (a->u.rail.track != TRACK_BIT_WORMHOLE) VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos); |
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
1613 |
if (b->u.rail.track != TRACK_BIT_WORMHOLE) VehicleEnterTile(b, b->tile, b->x_pos, b->y_pos); |
0 | 1614 |
} else { |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
1615 |
if (a->u.rail.track != TRACK_BIT_DEPOT) a->direction = ReverseDir(a->direction); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1616 |
UpdateVarsAfterSwap(a); |
1554
c0b2287254f4
(svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents:
1552
diff
changeset
|
1617 |
|
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
1618 |
if (a->u.rail.track != TRACK_BIT_WORMHOLE) VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos); |
0 | 1619 |
} |
3355
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
1620 |
|
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
1621 |
/* Update train's power incase tiles were different rail type */ |
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
1622 |
TrainPowerChanged(v); |
0 | 1623 |
} |
1624 |
||
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1625 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1626 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1627 |
* Check if the vehicle is a train |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1628 |
* @param v vehicle on tile |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1629 |
* @return v if it is a train, NULL otherwise |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1630 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1631 |
static void *TrainOnTileEnum(Vehicle *v, void *) |
744
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1632 |
{ |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1633 |
return (v->type == VEH_TRAIN) ? v : NULL; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1634 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1635 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1636 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1637 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1638 |
* Checks if a train is approaching a rail-road crossing |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1639 |
* @param v vehicle on tile |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1640 |
* @param data tile with crossing we are testing |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1641 |
* @return v if it is approaching a crossing, NULL otherwise |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1642 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1643 |
static void *TrainApproachingCrossingEnum(Vehicle *v, void *data) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1644 |
{ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1645 |
/* not a train || not front engine || crashed */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1646 |
if (v->type != VEH_TRAIN || !IsFrontEngine(v) || v->vehstatus & VS_CRASHED) return NULL; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1647 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1648 |
TileIndex tile = *(TileIndex*)data; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1649 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1650 |
if (TrainApproachingCrossingTile(v) != tile) return NULL; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1651 |
|
744
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1652 |
return v; |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1653 |
} |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1654 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1655 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1656 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1657 |
* Finds a vehicle approaching rail-road crossing |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1658 |
* @param tile tile to test |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1659 |
* @return pointer to vehicle approaching the crossing |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1660 |
* @pre tile is a rail-road crossing |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1661 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1662 |
static Vehicle *TrainApproachingCrossing(TileIndex tile) |
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1663 |
{ |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1664 |
assert(IsLevelCrossingTile(tile)); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1665 |
|
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
1666 |
DiagDirection dir = AxisToDiagDir(GetCrossingRailAxis(tile)); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1667 |
TileIndex tile_from = tile + TileOffsByDiagDir(dir); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1668 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1669 |
Vehicle *v = (Vehicle*)VehicleFromPos(tile_from, &tile, &TrainApproachingCrossingEnum); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1670 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1671 |
if (v != NULL) return v; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1672 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1673 |
dir = ReverseDiagDir(dir); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1674 |
tile_from = tile + TileOffsByDiagDir(dir); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1675 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1676 |
return (Vehicle*)VehicleFromPos(tile_from, &tile, &TrainApproachingCrossingEnum); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1677 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1678 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1679 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1680 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1681 |
* Sets correct crossing state |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1682 |
* @param tile tile to update |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1683 |
* @param sound should we play sound? |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1684 |
* @pre tile is a rail-road crossing |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1685 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1686 |
void UpdateLevelCrossing(TileIndex tile, bool sound) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1687 |
{ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1688 |
assert(IsLevelCrossingTile(tile)); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1689 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1690 |
/* train on crossing || train approaching crossing */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1691 |
bool new_state = VehicleFromPos(tile, NULL, &TrainOnTileEnum) || TrainApproachingCrossing(tile); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1692 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1693 |
if (new_state != IsCrossingBarred(tile)) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1694 |
if (new_state && sound) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1695 |
SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1696 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1697 |
SetCrossingBarred(tile, new_state); |
2639 | 1698 |
MarkTileDirtyByTile(tile); |
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1699 |
} |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1700 |
} |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1701 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1702 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1703 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1704 |
* Bars crossing and plays ding-ding sound if not barred already |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1705 |
* @param tile tile with crossing |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1706 |
* @pre tile is a rail-road crossing |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1707 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1708 |
static inline void MaybeBarCrossingWithSound(TileIndex tile) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1709 |
{ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1710 |
if (!IsCrossingBarred(tile)) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1711 |
BarCrossing(tile); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1712 |
SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1713 |
MarkTileDirtyByTile(tile); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1714 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1715 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1716 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1717 |
|
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1718 |
/** |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1719 |
* Advances wagons for train reversing, needed for variable length wagons. |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1720 |
* This one is called before the train is reversed. |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1721 |
* @param v First vehicle in chain |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1722 |
*/ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1723 |
static void AdvanceWagonsBeforeSwap(Vehicle *v) |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1724 |
{ |
6476 | 1725 |
Vehicle *base = v; |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1726 |
Vehicle *first = base; // first vehicle to move |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1727 |
Vehicle *last = GetLastVehicleInChain(v); // last vehicle to move |
6476 | 1728 |
uint length = CountVehiclesInChain(v); |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1729 |
|
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1730 |
while (length > 2) { |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1731 |
last = last->Previous(); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1732 |
first = first->Next(); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1733 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1734 |
int differential = base->u.rail.cached_veh_length - last->u.rail.cached_veh_length; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1735 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1736 |
/* do not update images now |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1737 |
* negative differential will be handled in AdvanceWagonsAfterSwap() */ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1738 |
for (int i = 0; i < differential; i++) TrainController(first, last->Next(), false); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1739 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1740 |
base = first; // == base->Next() |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1741 |
length -= 2; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1742 |
} |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1743 |
} |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1744 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1745 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1746 |
/** |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1747 |
* Advances wagons for train reversing, needed for variable length wagons. |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1748 |
* This one is called after the train is reversed. |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1749 |
* @param v First vehicle in chain |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1750 |
*/ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1751 |
static void AdvanceWagonsAfterSwap(Vehicle *v) |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1752 |
{ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1753 |
/* first of all, fix the situation when the train was entering a depot */ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1754 |
Vehicle *dep = v; // last vehicle in front of just left depot |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1755 |
while (dep->Next() != NULL && (dep->u.rail.track == TRACK_BIT_DEPOT || dep->Next()->u.rail.track != TRACK_BIT_DEPOT)) { |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1756 |
dep = dep->Next(); // find first vehicle outside of a depot, with next vehicle inside a depot |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1757 |
} |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1758 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1759 |
Vehicle *leave = dep->Next(); // first vehicle in a depot we are leaving now |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1760 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1761 |
if (leave != NULL) { |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1762 |
/* 'pull' next wagon out of the depot, so we won't miss it (it could stay in depot forever) */ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1763 |
int d = TicksToLeaveDepot(dep); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1764 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1765 |
if (d <= 0) { |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1766 |
leave->vehstatus &= ~VS_HIDDEN; // move it out of the depot |
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
1767 |
leave->u.rail.track = TrackToTrackBits(GetRailDepotTrack(leave->tile)); |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1768 |
for (int i = 0; i >= d; i--) TrainController(leave, NULL, false); // maybe move it, and maybe let another wagon leave |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1769 |
} |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1770 |
} else { |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1771 |
dep = NULL; // no vehicle in a depot, so no vehicle leaving a depot |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1772 |
} |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1773 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1774 |
Vehicle *base = v; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1775 |
Vehicle *first = base; // first vehicle to move |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1776 |
Vehicle *last = GetLastVehicleInChain(v); // last vehicle to move |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1777 |
uint length = CountVehiclesInChain(v); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1778 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1779 |
/* we have to make sure all wagons that leave a depot because of train reversing are moved coorectly |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1780 |
* they have already correct spacing, so we have to make sure they are moved how they should */ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1781 |
bool nomove = (dep == NULL); // if there is no vehicle leaving a depot, limit the number of wagons moved immediatelly |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1782 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1783 |
while (length > 2) { |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1784 |
/* we reached vehicle (originally) in front of a depot, stop now |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1785 |
* (we would move wagons that are alredy moved with new wagon length) */ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1786 |
if (base == dep) break; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1787 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1788 |
/* the last wagon was that one leaving a depot, so do not move it anymore */ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1789 |
if (last == dep) nomove = true; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1790 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1791 |
last = last->Previous(); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1792 |
first = first->Next(); |
6476 | 1793 |
|
1794 |
int differential = last->u.rail.cached_veh_length - base->u.rail.cached_veh_length; |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1795 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1796 |
/* do not update images now */ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1797 |
for (int i = 0; i < differential; i++) TrainController(first, (nomove ? last->Next() : NULL), false); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1798 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1799 |
base = first; // == base->Next() |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1800 |
length -= 2; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1801 |
} |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1802 |
} |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1803 |
|
2951 | 1804 |
|
0 | 1805 |
static void ReverseTrainDirection(Vehicle *v) |
1806 |
{ |
|
10242
52b4a9006029
(svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents:
10211
diff
changeset
|
1807 |
if (IsRailDepotTile(v->tile)) { |
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1808 |
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1809 |
} |
0 | 1810 |
|
743
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1811 |
/* Check if we were approaching a rail/road-crossing */ |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1812 |
TileIndex crossing = TrainApproachingCrossingTile(v); |
743
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1813 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1814 |
/* count number of vehicles */ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1815 |
int r = CountVehiclesInChain(v) - 1; // number of vehicles - 1 |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1816 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1817 |
AdvanceWagonsBeforeSwap(v); |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1818 |
|
0 | 1819 |
/* swap start<>end, start+1<>end-1, ... */ |
6476 | 1820 |
int l = 0; |
0 | 1821 |
do { |
1822 |
ReverseTrainSwapVeh(v, l++, r--); |
|
1823 |
} while (l <= r); |
|
1824 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1825 |
AdvanceWagonsAfterSwap(v); |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1826 |
|
10242
52b4a9006029
(svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents:
10211
diff
changeset
|
1827 |
if (IsRailDepotTile(v->tile)) { |
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1828 |
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1829 |
} |
0 | 1830 |
|
10211
c1391c8ed5c6
(svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents:
10210
diff
changeset
|
1831 |
ToggleBit(v->u.rail.flags, VRF_TOGGLE_REVERSE); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1832 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1833 |
ClrBit(v->u.rail.flags, VRF_REVERSING); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1834 |
|
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
1835 |
/* recalculate cached data */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
1836 |
TrainConsistChanged(v); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
1837 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
1838 |
/* update all images */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
1839 |
for (Vehicle *u = v; u != NULL; u = u->Next()) u->cur_image = u->GetImage(u->direction); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
1840 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1841 |
/* update crossing we were approaching */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1842 |
if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1843 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1844 |
/* maybe we are approaching crossing now, after reversal */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1845 |
crossing = TrainApproachingCrossingTile(v); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1846 |
if (crossing != INVALID_TILE) MaybeBarCrossingWithSound(crossing); |
0 | 1847 |
} |
1848 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1849 |
/** Reverse train. |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
1850 |
* @param tile unused |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1851 |
* @param flags type of operation |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1852 |
* @param p1 train to reverse |
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1853 |
* @param p2 if true, reverse a unit in a train (needs to be in a depot) |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1854 |
*/ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1855 |
CommandCost CmdReverseTrainDirection(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 1856 |
{ |
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
1857 |
if (!IsValidVehicleID(p1)) return CMD_ERROR; |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1858 |
|
6476 | 1859 |
Vehicle *v = GetVehicle(p1); |
0 | 1860 |
|
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
1861 |
if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1862 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
1863 |
if (p2 != 0) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1864 |
/* turn a single unit around */ |
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1865 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1866 |
if (IsMultiheaded(v) || HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_ARTIC_ENGINE)) { |
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1867 |
return_cmd_error(STR_ONLY_TURN_SINGLE_UNIT); |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1868 |
} |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1869 |
|
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1870 |
Vehicle *front = v->First(); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1871 |
/* make sure the vehicle is stopped in the depot */ |
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1872 |
if (CheckTrainStoppedInDepot(front) < 0) { |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1873 |
return_cmd_error(STR_881A_TRAINS_CAN_ONLY_BE_ALTERED); |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1874 |
} |
3257
aed8705e3847
(svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents:
3256
diff
changeset
|
1875 |
|
aed8705e3847
(svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents:
3256
diff
changeset
|
1876 |
if (flags & DC_EXEC) { |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1877 |
ToggleBit(v->u.rail.flags, VRF_REVERSE_DIRECTION); |
4856
3ed01482b9de
(svn r6782) - Fix (r3947): Invalidate depot & vehicle windows when reversing a single engine with ctrl-click.
peter1138
parents:
4839
diff
changeset
|
1878 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
3ed01482b9de
(svn r6782) - Fix (r3947): Invalidate depot & vehicle windows when reversing a single engine with ctrl-click.
peter1138
parents:
4839
diff
changeset
|
1879 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
3257
aed8705e3847
(svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents:
3256
diff
changeset
|
1880 |
} |
aed8705e3847
(svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents:
3256
diff
changeset
|
1881 |
} else { |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1882 |
/* turn the whole train around */ |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
1883 |
if (v->vehstatus & VS_CRASHED || v->breakdown_ctr != 0) return CMD_ERROR; |
3257
aed8705e3847
(svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents:
3256
diff
changeset
|
1884 |
|
aed8705e3847
(svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents:
3256
diff
changeset
|
1885 |
if (flags & DC_EXEC) { |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
1886 |
if (_settings_game.vehicle.realistic_acceleration && v->cur_speed != 0) { |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1887 |
ToggleBit(v->u.rail.flags, VRF_REVERSING); |
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1888 |
} else { |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1889 |
v->cur_speed = 0; |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1890 |
SetLastSpeed(v, 0); |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1891 |
ReverseTrainDirection(v); |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1892 |
} |
0 | 1893 |
} |
1894 |
} |
|
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1895 |
return CommandCost(); |
0 | 1896 |
} |
1897 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1898 |
/** Force a train through a red signal |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
1899 |
* @param tile unused |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1900 |
* @param flags type of operation |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1901 |
* @param p1 train to ignore the red signal |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1902 |
* @param p2 unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1903 |
*/ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1904 |
CommandCost CmdForceTrainProceed(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 1905 |
{ |
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
1906 |
if (!IsValidVehicleID(p1)) return CMD_ERROR; |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1907 |
|
6476 | 1908 |
Vehicle *v = GetVehicle(p1); |
0 | 1909 |
|
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
1910 |
if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1911 |
|
2639 | 1912 |
if (flags & DC_EXEC) v->u.rail.force_proceed = 0x50; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1913 |
|
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1914 |
return CommandCost(); |
0 | 1915 |
} |
1916 |
||
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1917 |
/** Refits a train to the specified cargo type. |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
1918 |
* @param tile unused |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1919 |
* @param flags type of operation |
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1920 |
* @param p1 vehicle ID of the train to refit |
3954
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
1921 |
* param p2 various bitstuffed elements |
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
1922 |
* - p2 = (bit 0-7) - the new cargo type to refit to |
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
1923 |
* - p2 = (bit 8-15) - the new cargo subtype to refit to |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1924 |
* - p2 = (bit 16) - refit only this vehicle |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
1925 |
* @return cost of refit or error |
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1926 |
*/ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1927 |
CommandCost CmdRefitRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 1928 |
{ |
2635 | 1929 |
CargoID new_cid = GB(p2, 0, 8); |
3954
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
1930 |
byte new_subtype = GB(p2, 8, 8); |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1931 |
bool only_this = HasBit(p2, 16); |
842 | 1932 |
|
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
1933 |
if (!IsValidVehicleID(p1)) return CMD_ERROR; |
915 | 1934 |
|
6476 | 1935 |
Vehicle *v = GetVehicle(p1); |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1936 |
|
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
1937 |
if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; |
2244 | 1938 |
if (CheckTrainStoppedInDepot(v) < 0) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1939 |
if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE); |
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1940 |
|
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1941 |
/* Check cargo */ |
6642
2e690e6fdc03
(svn r9259) -Codechange: NUM_CARGO isn't a valid cargo type...
peter1138
parents:
6629
diff
changeset
|
1942 |
if (new_cid >= NUM_CARGO) return CMD_ERROR; |
0 | 1943 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
1944 |
CommandCost cost(EXPENSES_TRAIN_RUN); |
6476 | 1945 |
uint num = 0; |
915 | 1946 |
|
0 | 1947 |
do { |
491
0642006d876c
(svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents:
445
diff
changeset
|
1948 |
/* XXX: We also refit all the attached wagons en-masse if they |
0642006d876c
(svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents:
445
diff
changeset
|
1949 |
* can be refitted. This is how TTDPatch does it. TODO: Have |
0642006d876c
(svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents:
445
diff
changeset
|
1950 |
* some nice [Refit] button near each wagon. --pasky */ |
2704
dc9ae68dfd74
(svn r3248) - Codechange: Change interface of CanRefitTo() to supply the engine type directly instead of getting it from a vehicle. This allows the function to be used before vehicles are involved.
peter1138
parents:
2677
diff
changeset
|
1951 |
if (!CanRefitTo(v->engine_type, new_cid)) continue; |
1802
448f187042d3
(svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents:
1794
diff
changeset
|
1952 |
|
1859
b3af0081df39
(svn r2365) - Change: [refitting] Make refitting capacities for trains newgrf compatible. Train vehicles can now carry twice as much mail/goods as other cargo, and four times as much passengers.
hackykid
parents:
1842
diff
changeset
|
1953 |
if (v->cargo_cap != 0) { |
1895
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1954 |
uint16 amount = CALLBACK_FAILED; |
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1955 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
1956 |
if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_REFIT_CAPACITY)) { |
3988
b76291f6ce29
(svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents:
3986
diff
changeset
|
1957 |
/* Back up the vehicle's cargo type */ |
1895
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1958 |
CargoID temp_cid = v->cargo_type; |
3988
b76291f6ce29
(svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents:
3986
diff
changeset
|
1959 |
byte temp_subtype = v->cargo_subtype; |
1895
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1960 |
v->cargo_type = new_cid; |
3988
b76291f6ce29
(svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents:
3986
diff
changeset
|
1961 |
v->cargo_subtype = new_subtype; |
b76291f6ce29
(svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents:
3986
diff
changeset
|
1962 |
/* Check the refit capacity callback */ |
3390
f910bc27153e
(svn r4198) - NewGRF: Rename GetCallBackResult() to GetVehicleCallback(), as other types will exist later, and use separate parameters instead of bitshifting.
peter1138
parents:
3355
diff
changeset
|
1963 |
amount = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v); |
3988
b76291f6ce29
(svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents:
3986
diff
changeset
|
1964 |
/* Restore the original cargo type */ |
1895
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1965 |
v->cargo_type = temp_cid; |
3988
b76291f6ce29
(svn r5187) - NewGRF: temporarily change a vehicle's cargo subtype when calling the refit capacity callback.
peter1138
parents:
3986
diff
changeset
|
1966 |
v->cargo_subtype = temp_subtype; |
1895
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1967 |
} |
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1968 |
|
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1969 |
if (amount == CALLBACK_FAILED) { // callback failed or not used, use default |
6476 | 1970 |
const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1971 |
CargoID old_cid = rvi->cargo_type; |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1972 |
/* normally, the capacity depends on the cargo type, a rail vehicle can |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1973 |
* carry twice as much mail/goods as normal cargo, and four times as |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1974 |
* many passengers |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1975 |
*/ |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1976 |
amount = rvi->capacity; |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1977 |
switch (old_cid) { |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1978 |
case CT_PASSENGERS: break; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1979 |
case CT_MAIL: |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1980 |
case CT_GOODS: amount *= 2; break; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1981 |
default: amount *= 4; break; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1982 |
} |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1983 |
switch (new_cid) { |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1984 |
case CT_PASSENGERS: break; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1985 |
case CT_MAIL: |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1986 |
case CT_GOODS: amount /= 2; break; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1987 |
default: amount /= 4; break; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
1988 |
} |
6476 | 1989 |
} |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1990 |
|
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1991 |
if (amount != 0) { |
4242
e6baba95f369
(svn r5819) - NewGRF: add support for refit costs specified in NewGRF. This may affect the default refit costs for the default rail vehicles.
peter1138
parents:
4198
diff
changeset
|
1992 |
if (new_cid != v->cargo_type) { |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1993 |
cost.AddCost(GetRefitCost(v->engine_type)); |
4242
e6baba95f369
(svn r5819) - NewGRF: add support for refit costs specified in NewGRF. This may affect the default refit costs for the default rail vehicles.
peter1138
parents:
4198
diff
changeset
|
1994 |
} |
e6baba95f369
(svn r5819) - NewGRF: add support for refit costs specified in NewGRF. This may affect the default refit costs for the default rail vehicles.
peter1138
parents:
4198
diff
changeset
|
1995 |
|
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1996 |
num += amount; |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1997 |
if (flags & DC_EXEC) { |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
1998 |
v->cargo.Truncate((v->cargo_type == new_cid) ? amount : 0); |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1999 |
v->cargo_type = new_cid; |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
2000 |
v->cargo_cap = amount; |
3954
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
2001 |
v->cargo_subtype = new_subtype; |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
2002 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
2003 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
2004 |
InvalidateWindowClassesData(WC_TRAINS_LIST, 0); |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
2005 |
} |
0 | 2006 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2007 |
} |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
2008 |
} while ((v = v->Next()) != NULL && !only_this); |
0 | 2009 |
|
3008
7b519db78ce2
(svn r3588) - Refit: Use only one global to return the refit capacity rather than one per vehicle type.
peter1138
parents:
3007
diff
changeset
|
2010 |
_returned_refit_capacity = num; |
0 | 2011 |
|
4708
5129e3add448
(svn r6618) - After refitting a train, update its cached variables as they may change.
peter1138
parents:
4656
diff
changeset
|
2012 |
/* Update the train's cached variables */ |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
2013 |
if (flags & DC_EXEC) TrainConsistChanged(GetVehicle(p1)->First()); |
4708
5129e3add448
(svn r6618) - After refitting a train, update its cached variables as they may change.
peter1138
parents:
4656
diff
changeset
|
2014 |
|
0 | 2015 |
return cost; |
2016 |
} |
|
2017 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2018 |
struct TrainFindDepotData { |
0 | 2019 |
uint best_length; |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1969
diff
changeset
|
2020 |
TileIndex tile; |
2475 | 2021 |
PlayerID owner; |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
2022 |
/** |
2639 | 2023 |
* true if reversing is necessary for the train to get to this depot |
2024 |
* This value is unused when new depot finding and NPF are both disabled |
|
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
2025 |
*/ |
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
2026 |
bool reverse; |
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2027 |
}; |
0 | 2028 |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2029 |
static bool NtpCallbFindDepot(TileIndex tile, TrainFindDepotData *tfdd, int track, uint length) |
0 | 2030 |
{ |
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3267
diff
changeset
|
2031 |
if (IsTileType(tile, MP_RAILWAY) && |
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3267
diff
changeset
|
2032 |
IsTileOwner(tile, tfdd->owner) && |
4182
20d711816338
(svn r5624) Use {IsPlainRailTile,IsRailDepot,IsRailWaypoint,HasSignals}() instead of GetRailTile{T,Subt}ype() - this is more concise and a bit more flexible if/when the rail tile encoding changes
tron
parents:
4095
diff
changeset
|
2033 |
IsRailDepot(tile)) { |
4406
4ff711f834b9
(svn r6159) -Fix: FindClosestTrainDepot hardly ever found a depot with NPF off due to absence of distance-normalization (Rojer)
Darkvater
parents:
4389
diff
changeset
|
2034 |
/* approximate number of tiles by dividing by DIAG_FACTOR */ |
4ff711f834b9
(svn r6159) -Fix: FindClosestTrainDepot hardly ever found a depot with NPF off due to absence of distance-normalization (Rojer)
Darkvater
parents:
4389
diff
changeset
|
2035 |
tfdd->best_length = length / DIAG_FACTOR; |
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3267
diff
changeset
|
2036 |
tfdd->tile = tile; |
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3267
diff
changeset
|
2037 |
return true; |
0 | 2038 |
} |
2039 |
||
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2040 |
return false; |
0 | 2041 |
} |
2042 |
||
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2043 |
/** returns the tile of a depot to goto to. The given vehicle must not be |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2044 |
* crashed! */ |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2045 |
static TrainFindDepotData FindClosestTrainDepot(Vehicle *v, int max_distance) |
0 | 2046 |
{ |
6476 | 2047 |
assert(!(v->vehstatus & VS_CRASHED)); |
2048 |
||
0 | 2049 |
TrainFindDepotData tfdd; |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
2050 |
tfdd.owner = v->owner; |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2051 |
tfdd.best_length = UINT_MAX; |
1777
d328484bd6f2
(svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents:
1770
diff
changeset
|
2052 |
tfdd.reverse = false; |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
2053 |
|
6476 | 2054 |
TileIndex tile = v->tile; |
10242
52b4a9006029
(svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents:
10211
diff
changeset
|
2055 |
if (IsRailDepotTile(tile)) { |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
2056 |
tfdd.tile = tile; |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
2057 |
tfdd.best_length = 0; |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
2058 |
return tfdd; |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
2059 |
} |
0 | 2060 |
|
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
2061 |
switch (_settings_game.pf.pathfinder_for_trains) { |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2062 |
case VPF_YAPF: { /* YAPF */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2063 |
bool found = YapfFindNearestRailDepotTwoWay(v, max_distance, NPF_INFINITE_PENALTY, &tfdd.tile, &tfdd.reverse); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2064 |
tfdd.best_length = found ? max_distance / 2 : UINT_MAX; // some fake distance or NOT_FOUND |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2065 |
} break; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2066 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2067 |
case VPF_NPF: { /* NPF */ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2068 |
const Vehicle *last = GetLastVehicleInChain(v); |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2069 |
Trackdir trackdir = GetVehicleTrackdir(v); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2070 |
Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last)); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2071 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2072 |
assert(trackdir != INVALID_TRACKDIR); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2073 |
NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, TRANSPORT_RAIL, 0, v->owner, v->u.rail.compatible_railtypes, NPF_INFINITE_PENALTY); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2074 |
if (ftd.best_bird_dist == 0) { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2075 |
/* Found target */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2076 |
tfdd.tile = ftd.node.tile; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2077 |
/* Our caller expects a number of tiles, so we just approximate that |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2078 |
* number by this. It might not be completely what we want, but it will |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2079 |
* work for now :-) We can possibly change this when the old pathfinder |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2080 |
* is removed. */ |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2081 |
tfdd.best_length = ftd.best_path_dist / NPF_TILE_LENGTH; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2082 |
if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) tfdd.reverse = true; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2083 |
} |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2084 |
} break; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2085 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2086 |
default: |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2087 |
case VPF_NTP: { /* NTP */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2088 |
/* search in the forward direction first. */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2089 |
DiagDirection i = TrainExitDir(v->direction, v->u.rail.track); |
3355
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
2090 |
NewTrainPathfind(tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd); |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2091 |
if (tfdd.best_length == UINT_MAX){ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2092 |
tfdd.reverse = true; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2093 |
/* search in backwards direction */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2094 |
i = TrainExitDir(ReverseDir(v->direction), v->u.rail.track); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2095 |
NewTrainPathfind(tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2096 |
} |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2097 |
} break; |
0 | 2098 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2099 |
|
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
2100 |
return tfdd; |
0 | 2101 |
} |
2102 |
||
10200
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2103 |
bool Train::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse) |
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2104 |
{ |
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2105 |
TrainFindDepotData tfdd = FindClosestTrainDepot(this, 0); |
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2106 |
if (tfdd.best_length == (uint)-1) return false; |
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2107 |
|
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2108 |
if (location != NULL) *location = tfdd.tile; |
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2109 |
if (destination != NULL) *destination = GetDepotByTile(tfdd.tile)->index; |
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2110 |
if (reverse != NULL) *reverse = tfdd.reverse; |
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2111 |
|
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2112 |
return true; |
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2113 |
} |
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2114 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
2115 |
/** Send a train to a depot |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3487
diff
changeset
|
2116 |
* @param tile unused |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2117 |
* @param flags type of operation |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
2118 |
* @param p1 train to send to the depot |
4451
66603f0f732e
(svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents:
4434
diff
changeset
|
2119 |
* @param p2 various bitmasked elements |
4506
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
2120 |
* - p2 bit 0-3 - DEPOT_ flags (see vehicle.h) |
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
2121 |
* - p2 bit 8-10 - VLW flag (for mass goto depot) |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
2122 |
*/ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2123 |
CommandCost CmdSendTrainToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 2124 |
{ |
4506
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
2125 |
if (p2 & DEPOT_MASS_SEND) { |
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
2126 |
/* Mass goto depot requested */ |
4546
9345e0569655
(svn r6378) -Codechange: Rename VLW_FLAGS to VLW_MASK as it is a mask
Darkvater
parents:
4544
diff
changeset
|
2127 |
if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR; |
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
2128 |
return SendAllVehiclesToDepot(VEH_TRAIN, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_MASK), p1); |
4463
3a70624c40eb
(svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents:
4451
diff
changeset
|
2129 |
} |
3a70624c40eb
(svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents:
4451
diff
changeset
|
2130 |
|
4506
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
2131 |
if (!IsValidVehicleID(p1)) return CMD_ERROR; |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
2132 |
|
6476 | 2133 |
Vehicle *v = GetVehicle(p1); |
1237
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
2134 |
|
10200
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2135 |
if (v->type != VEH_TRAIN) return CMD_ERROR; |
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2136 |
|
aba3af04cdbd
(svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents:
10184
diff
changeset
|
2137 |
return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK)); |
0 | 2138 |
} |
2139 |
||
2140 |
||
6573 | 2141 |
void OnTick_Train() |
0 | 2142 |
{ |
2143 |
_age_cargo_skip_counter = (_age_cargo_skip_counter == 0) ? 184 : (_age_cargo_skip_counter - 1); |
|
2144 |
} |
|
2145 |
||
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2146 |
static const int8 _vehicle_smoke_pos[8] = { |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2147 |
1, 1, 1, 0, -1, -1, -1, 0 |
0 | 2148 |
}; |
2149 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2150 |
static void HandleLocomotiveSmokeCloud(const Vehicle *v) |
0 | 2151 |
{ |
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
2152 |
bool sound = false; |
0 | 2153 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2154 |
if (v->vehstatus & VS_TRAIN_SLOWING || v->load_unload_time_rem != 0 || v->cur_speed < 2) { |
0 | 2155 |
return; |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2156 |
} |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2157 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2158 |
const Vehicle *u = v; |
0 | 2159 |
|
2160 |
do { |
|
6074 | 2161 |
const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2162 |
int effect_offset = GB(v->u.rail.cached_vis_effect, 0, 4) - 8; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2163 |
byte effect_type = GB(v->u.rail.cached_vis_effect, 4, 2); |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2164 |
bool disable_effect = HasBit(v->u.rail.cached_vis_effect, 6); |
0 | 2165 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2166 |
/* no smoke? */ |
6119
b47985557d1e
(svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents:
6109
diff
changeset
|
2167 |
if ((rvi->railveh_type == RAILVEH_WAGON && effect_type == 0) || |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2168 |
disable_effect || |
3546
1cb79c89c9a9
(svn r4412) When a vehicle is in a depot or tunnel it's always flagged as VS_HIDDEN. So after checking for VS_HIDDEN and the result is false there's no need to check if it's flagged as being in a depot or tunnel
tron
parents:
3491
diff
changeset
|
2169 |
v->vehstatus & VS_HIDDEN) { |
0 | 2170 |
continue; |
2639 | 2171 |
} |
0 | 2172 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2173 |
/* No smoke in depots or tunnels */ |
10242
52b4a9006029
(svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents:
10211
diff
changeset
|
2174 |
if (IsRailDepotTile(v->tile) || IsTunnelTile(v->tile)) continue; |
3590
e36eef17cf3f
(svn r4479) -Fix (FS#90) electric engines (or rather their pantographs) no longer emit sparks when engine is pulled on convrail (MeusH)
celestar
parents:
3560
diff
changeset
|
2175 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2176 |
/* No sparks for electric vehicles on nonelectrified tracks */ |
6480 | 2177 |
if (!HasPowerOnRail(v->u.rail.railtype, GetTileRailType(v->tile))) continue; |
2612
744aef3af72f
(svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents:
2610
diff
changeset
|
2178 |
|
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2179 |
if (effect_type == 0) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2180 |
/* Use default effect type for engine class. */ |
6074 | 2181 |
effect_type = rvi->engclass; |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2182 |
} else { |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2183 |
effect_type--; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2184 |
} |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2185 |
|
6476 | 2186 |
int x = _vehicle_smoke_pos[v->direction] * effect_offset; |
2187 |
int y = _vehicle_smoke_pos[(v->direction + 2) % 8] * effect_offset; |
|
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2188 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2189 |
if (HasBit(v->u.rail.flags, VRF_REVERSE_DIRECTION)) { |
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
2190 |
x = -x; |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
2191 |
y = -y; |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
2192 |
} |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
2193 |
|
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2194 |
switch (effect_type) { |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2195 |
case 0: |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2196 |
/* steam smoke. */ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2197 |
if (GB(v->tick_counter, 0, 4) == 0) { |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2198 |
CreateEffectVehicleRel(v, x, y, 10, EV_STEAM_SMOKE); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2199 |
sound = true; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2200 |
} |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2201 |
break; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2202 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2203 |
case 1: |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2204 |
/* diesel smoke */ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2205 |
if (u->cur_speed <= 40 && Chance16(15, 128)) { |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2206 |
CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2207 |
sound = true; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2208 |
} |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2209 |
break; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2210 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2211 |
case 2: |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2212 |
/* blue spark */ |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2213 |
if (GB(v->tick_counter, 0, 2) == 0 && Chance16(1, 45)) { |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2214 |
CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2215 |
sound = true; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2216 |
} |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2217 |
break; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2218 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2219 |
default: |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2220 |
break; |
0 | 2221 |
} |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
2222 |
} while ((v = v->Next()) != NULL); |
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
2223 |
|
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
2224 |
if (sound) PlayVehicleSound(u, VSE_TRAIN_EFFECT); |
0 | 2225 |
} |
2226 |
||
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2227 |
void Train::PlayLeaveStationSound() const |
0 | 2228 |
{ |
541 | 2229 |
static const SoundFx sfx[] = { |
2230 |
SND_04_TRAIN, |
|
2231 |
SND_0A_TRAIN_HORN, |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2232 |
SND_0A_TRAIN_HORN, |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2233 |
SND_47_MAGLEV_2, |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2234 |
SND_41_MAGLEV |
541 | 2235 |
}; |
2236 |
||
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2237 |
if (PlayVehicleSound(this, VSE_START)) return; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2238 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2239 |
EngineID engtype = this->engine_type; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2240 |
SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], this); |
0 | 2241 |
} |
2242 |
||
2243 |
static bool CheckTrainStayInDepot(Vehicle *v) |
|
2244 |
{ |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2245 |
/* bail out if not all wagons are in the same depot or not in a depot at all */ |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
2246 |
for (const Vehicle *u = v; u != NULL; u = u->Next()) { |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2247 |
if (u->u.rail.track != TRACK_BIT_DEPOT || u->tile != v->tile) return false; |
2639 | 2248 |
} |
0 | 2249 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2250 |
/* if the train got no power, then keep it in the depot */ |
4252
e06bade21a2e
(svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents:
4251
diff
changeset
|
2251 |
if (v->u.rail.cached_power == 0) { |
e06bade21a2e
(svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents:
4251
diff
changeset
|
2252 |
v->vehstatus |= VS_STOPPED; |
e06bade21a2e
(svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents:
4251
diff
changeset
|
2253 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
e06bade21a2e
(svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents:
4251
diff
changeset
|
2254 |
return true; |
e06bade21a2e
(svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents:
4251
diff
changeset
|
2255 |
} |
e06bade21a2e
(svn r5854) -Fix: [elrails] now electric engines are stopped if they enter a non-electrified depot even if they would have left right away if the depot had catenary
bjarni
parents:
4251
diff
changeset
|
2256 |
|
0 | 2257 |
if (v->u.rail.force_proceed == 0) { |
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
2258 |
if (++v->load_unload_time_rem < 37) { |
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
2259 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 2260 |
return true; |
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
2261 |
} |
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
2262 |
|
0 | 2263 |
v->load_unload_time_rem = 0; |
2264 |
||
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
2265 |
if (UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner) == SIGSEG_FULL) { |
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
2266 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 2267 |
return true; |
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
2268 |
} |
0 | 2269 |
} |
2916
8f1aa489701f
(svn r3472) - [PBS] Remove from trunk. Anyone interested can still find it in branch/pbs. This reverts revisions r3158, r3140, r3075, r2977, r2674, r2625, r2621, r2529, r2528, r2525, r2524, r2519, r2517, r2516, r2507, r2499. (in conjunction with Tron)
Darkvater
parents:
2883
diff
changeset
|
2270 |
|
578
86e352980acd
(svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents:
555
diff
changeset
|
2271 |
VehicleServiceInDepot(v); |
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
2272 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2273 |
v->PlayLeaveStationSound(); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2274 |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2275 |
v->u.rail.track = TRACK_BIT_X; |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2276 |
if (v->direction & 2) v->u.rail.track = TRACK_BIT_Y; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2277 |
|
0 | 2278 |
v->vehstatus &= ~VS_HIDDEN; |
2279 |
v->cur_speed = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2280 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2281 |
v->UpdateDeltaXY(v->direction); |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2282 |
v->cur_image = v->GetImage(v->direction); |
0 | 2283 |
VehiclePositionChanged(v); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2284 |
UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner); |
0 | 2285 |
UpdateTrainAcceleration(v); |
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
2286 |
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); |
0 | 2287 |
|
2288 |
return false; |
|
2289 |
} |
|
2290 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2291 |
/** Check for station tiles */ |
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2292 |
struct TrainTrackFollowerData { |
0 | 2293 |
TileIndex dest_coords; |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2294 |
StationID station_index; ///< station index we're heading for |
0 | 2295 |
uint best_bird_dist; |
2296 |
uint best_track_dist; |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2297 |
TrackdirByte best_track; |
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2298 |
}; |
0 | 2299 |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2300 |
static bool NtpCallbFindStation(TileIndex tile, TrainTrackFollowerData *ttfd, Trackdir track, uint length) |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1969
diff
changeset
|
2301 |
{ |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2302 |
/* heading for nowhere? */ |
2951 | 2303 |
if (ttfd->dest_coords == 0) return false; |
0 | 2304 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2305 |
/* did we reach the final station? */ |
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
2306 |
if ((ttfd->station_index == INVALID_STATION && tile == ttfd->dest_coords) || ( |
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
2307 |
IsTileType(tile, MP_STATION) && |
3334
b72ac8637a30
(svn r4113) Moved all relevant map functions for stations to station_map.[ch]
celestar
parents:
3322
diff
changeset
|
2308 |
IsRailwayStation(tile) && |
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
2309 |
GetStationIndex(tile) == ttfd->station_index |
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
2310 |
)) { |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2311 |
/* We do not check for dest_coords if we have a station_index, |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2312 |
* because in that case the dest_coords are just an |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2313 |
* approximation of where the station is */ |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2314 |
|
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2315 |
/* found station */ |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2316 |
ttfd->best_track = track; |
6629
b35b983c6cdb
(svn r9146) -Fix [NTP][FS#676](r6800): "Train is lost" message is generated incorrectly. Forgot to set the best bird distance to zero in case we found the destination to indicate success. (de_zeurkous)
KUDr
parents:
6589
diff
changeset
|
2317 |
ttfd->best_bird_dist = 0; |
0 | 2318 |
return true; |
2319 |
} else { |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2320 |
/* didn't find station, keep track of the best path so far. */ |
6476 | 2321 |
uint dist = DistanceManhattan(tile, ttfd->dest_coords); |
0 | 2322 |
if (dist < ttfd->best_bird_dist) { |
2323 |
ttfd->best_bird_dist = dist; |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
2324 |
ttfd->best_track = track; |
0 | 2325 |
} |
2326 |
return false; |
|
2327 |
} |
|
2328 |
} |
|
2329 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2330 |
static void FillWithStationData(TrainTrackFollowerData *fd, const Vehicle *v) |
0 | 2331 |
{ |
2639 | 2332 |
fd->dest_coords = v->dest_tile; |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2333 |
fd->station_index = v->current_order.IsType(OT_GOTO_STATION) ? v->current_order.GetDestination() : INVALID_STATION; |
0 | 2334 |
} |
2335 |
||
2336 |
static const byte _initial_tile_subcoord[6][4][3] = { |
|
4344
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4329
diff
changeset
|
2337 |
{{ 15, 8, 1 }, { 0, 0, 0 }, { 0, 8, 5 }, { 0, 0, 0 }}, |
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4329
diff
changeset
|
2338 |
{{ 0, 0, 0 }, { 8, 0, 3 }, { 0, 0, 0 }, { 8, 15, 7 }}, |
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4329
diff
changeset
|
2339 |
{{ 0, 0, 0 }, { 7, 0, 2 }, { 0, 7, 6 }, { 0, 0, 0 }}, |
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4329
diff
changeset
|
2340 |
{{ 15, 8, 2 }, { 0, 0, 0 }, { 0, 0, 0 }, { 8, 15, 6 }}, |
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4329
diff
changeset
|
2341 |
{{ 15, 7, 0 }, { 8, 0, 4 }, { 0, 0, 0 }, { 0, 0, 0 }}, |
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
4329
diff
changeset
|
2342 |
{{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 8, 4 }, { 7, 15, 0 }}, |
0 | 2343 |
}; |
2344 |
||
2345 |
static const byte _search_directions[6][4] = { |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2346 |
{ 0, 9, 2, 9 }, ///< track 1 |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2347 |
{ 9, 1, 9, 3 }, ///< track 2 |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2348 |
{ 9, 0, 3, 9 }, ///< track upper |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2349 |
{ 1, 9, 9, 2 }, ///< track lower |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2350 |
{ 3, 2, 9, 9 }, ///< track left |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2351 |
{ 9, 9, 1, 0 }, ///< track right |
0 | 2352 |
}; |
2353 |
||
2354 |
static const byte _pick_track_table[6] = {1, 3, 2, 2, 0, 0}; |
|
2355 |
||
2356 |
/* choose a track */ |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2357 |
static Track ChooseTrainTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks) |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2358 |
{ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2359 |
Track best_track; |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2360 |
/* pathfinders are able to tell that route was only 'guessed' */ |
4870
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2361 |
bool path_not_found = false; |
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2362 |
|
2758
9cd8a21e9a7b
(svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents:
2752
diff
changeset
|
2363 |
#ifdef PF_BENCHMARK |
3341
b8febc9509d7
(svn r4125) - Feature: Add a general TIC() TOC() mechanism using rdtsc or something similar on non-i386 architectures to performance-tune (critical) code. Some systems are probably missing, but those can be added later.
Darkvater
parents:
3334
diff
changeset
|
2364 |
TIC() |
0 | 2365 |
#endif |
2366 |
||
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2367 |
assert((tracks & ~TRACK_BIT_MASK) == 0); |
0 | 2368 |
|
1247 | 2369 |
/* quick return in case only one possible track is available */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2370 |
if (KillFirstBit(tracks) == TRACK_BIT_NONE) return FindFirstTrack(tracks); |
0 | 2371 |
|
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
2372 |
switch (_settings_game.pf.pathfinder_for_trains) { |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2373 |
case VPF_YAPF: { /* YAPF */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2374 |
Trackdir trackdir = YapfChooseRailTrack(v, tile, enterdir, tracks, &path_not_found); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2375 |
if (trackdir != INVALID_TRACKDIR) { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2376 |
best_track = TrackdirToTrack(trackdir); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2377 |
} else { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2378 |
best_track = FindFirstTrack(tracks); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2379 |
} |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2380 |
} break; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2381 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2382 |
case VPF_NPF: { /* NPF */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2383 |
void *perf = NpfBeginInterval(); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2384 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2385 |
NPFFindStationOrTileData fstd; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2386 |
NPFFillWithOrderData(&fstd, v); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2387 |
/* The enterdir for the new tile, is the exitdir for the old tile */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2388 |
Trackdir trackdir = GetVehicleTrackdir(v); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2389 |
assert(trackdir != INVALID_TRACKDIR); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2390 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2391 |
NPFFoundTargetData ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_RAIL, 0, v->owner, v->u.rail.compatible_railtypes); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2392 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2393 |
if (ftd.best_trackdir == INVALID_TRACKDIR) { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2394 |
/* We are already at our target. Just do something |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2395 |
* @todo maybe display error? |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2396 |
* @todo: go straight ahead if possible? */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2397 |
best_track = FindFirstTrack(tracks); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2398 |
} else { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2399 |
/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2400 |
* the direction we need to take to get there, if ftd.best_bird_dist is not 0, |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2401 |
* we did not find our target, but ftd.best_trackdir contains the direction leading |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2402 |
* to the tile closest to our target. */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2403 |
if (ftd.best_bird_dist != 0) path_not_found = true; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2404 |
/* Discard enterdir information, making it a normal track */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2405 |
best_track = TrackdirToTrack(ftd.best_trackdir); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2406 |
} |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2407 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2408 |
int time = NpfEndInterval(perf); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2409 |
DEBUG(yapf, 4, "[NPFT] %d us - %d rounds - %d open - %d closed -- ", time, 0, _aystar_stats_open_size, _aystar_stats_closed_size); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2410 |
} break; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2411 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2412 |
default: |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2413 |
case VPF_NTP: { /* NTP */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2414 |
void *perf = NpfBeginInterval(); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2415 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2416 |
TrainTrackFollowerData fd; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2417 |
FillWithStationData(&fd, v); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2418 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2419 |
/* New train pathfinding */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2420 |
fd.best_bird_dist = UINT_MAX; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2421 |
fd.best_track_dist = UINT_MAX; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2422 |
fd.best_track = INVALID_TRACKDIR; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2423 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2424 |
NewTrainPathfind(tile - TileOffsByDiagDir(enterdir), v->dest_tile, |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2425 |
v->u.rail.compatible_railtypes, enterdir, (NTPEnumProc*)NtpCallbFindStation, &fd); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2426 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2427 |
/* check whether the path was found or only 'guessed' */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2428 |
if (fd.best_bird_dist != 0) path_not_found = true; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2429 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2430 |
if (fd.best_track == INVALID_TRACKDIR) { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2431 |
/* blaha */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2432 |
best_track = FindFirstTrack(tracks); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2433 |
} else { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2434 |
best_track = TrackdirToTrack(fd.best_track); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2435 |
} |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2436 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2437 |
int time = NpfEndInterval(perf); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2438 |
DEBUG(yapf, 4, "[NTPT] %d us - %d rounds - %d open - %d closed -- ", time, 0, 0, 0); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2439 |
} break; |
0 | 2440 |
} |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2441 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2442 |
/* handle "path not found" state */ |
4870
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2443 |
if (path_not_found) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2444 |
/* PF didn't find the route */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2445 |
if (!HasBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION)) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2446 |
/* it is first time the problem occurred, set the "path not found" flag */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2447 |
SetBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2448 |
/* and notify user about the event */ |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
2449 |
if (_settings_client.gui.lost_train_warn && v->owner == _local_player) { |
4870
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2450 |
SetDParam(0, v->unitnumber); |
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2451 |
AddNewsItem( |
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2452 |
STR_TRAIN_IS_LOST, |
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
2453 |
NS_ADVICE, |
4870
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2454 |
v->index, |
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2455 |
0); |
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2456 |
} |
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2457 |
} |
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2458 |
} else { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2459 |
/* route found, is the train marked with "path not found" flag? */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2460 |
if (HasBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION)) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2461 |
/* clear the flag as the PF's problem was solved */ |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2462 |
ClrBit(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2463 |
/* can we also delete the "News" item somehow? */ |
4870
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2464 |
} |
0ee22ed51ada
(svn r6800) -Feature change: [train is lost] message is now generated immediately when pathfinder can't find the path. (thanks MeusH, peter1138 and Brianetta for ideas and help).
KUDr
parents:
4856
diff
changeset
|
2465 |
} |
0 | 2466 |
|
2758
9cd8a21e9a7b
(svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents:
2752
diff
changeset
|
2467 |
#ifdef PF_BENCHMARK |
3341
b8febc9509d7
(svn r4125) - Feature: Add a general TIC() TOC() mechanism using rdtsc or something similar on non-i386 architectures to performance-tune (critical) code. Some systems are probably missing, but those can be added later.
Darkvater
parents:
3334
diff
changeset
|
2468 |
TOC("PF time = ", 1) |
0 | 2469 |
#endif |
2470 |
||
2471 |
return best_track; |
|
2472 |
} |
|
2473 |
||
2474 |
||
2475 |
static bool CheckReverseTrain(Vehicle *v) |
|
2476 |
{ |
|
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
2477 |
if (_settings_game.difficulty.line_reverse_mode != 0 || |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2478 |
v->u.rail.track == TRACK_BIT_DEPOT || v->u.rail.track == TRACK_BIT_WORMHOLE || |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2479 |
!(v->direction & 1)) { |
0 | 2480 |
return false; |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2481 |
} |
0 | 2482 |
|
6476 | 2483 |
TrainTrackFollowerData fd; |
0 | 2484 |
FillWithStationData(&fd, v); |
2485 |
||
6476 | 2486 |
uint reverse_best = 0; |
0 | 2487 |
|
2488 |
assert(v->u.rail.track); |
|
2489 |
||
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
2490 |
switch (_settings_game.pf.pathfinder_for_trains) { |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2491 |
case VPF_YAPF: /* YAPF */ |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2492 |
reverse_best = YapfCheckReverseTrain(v); |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2493 |
break; |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2494 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2495 |
case VPF_NPF: { /* NPF */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2496 |
NPFFindStationOrTileData fstd; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2497 |
NPFFoundTargetData ftd; |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2498 |
Vehicle *last = GetLastVehicleInChain(v); |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2499 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2500 |
NPFFillWithOrderData(&fstd, v); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2501 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2502 |
Trackdir trackdir = GetVehicleTrackdir(v); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2503 |
Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last)); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2504 |
assert(trackdir != INVALID_TRACKDIR); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2505 |
assert(trackdir_rev != INVALID_TRACKDIR); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2506 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2507 |
ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, &fstd, TRANSPORT_RAIL, 0, v->owner, v->u.rail.compatible_railtypes); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2508 |
if (ftd.best_bird_dist != 0) { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2509 |
/* We didn't find anything, just keep on going straight ahead */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2510 |
reverse_best = false; |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
2511 |
} else { |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2512 |
if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2513 |
reverse_best = true; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2514 |
} else { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2515 |
reverse_best = false; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2516 |
} |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
2517 |
} |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2518 |
} break; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2519 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2520 |
default: |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2521 |
case VPF_NTP: { /* NTP */ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2522 |
int i = _search_directions[FindFirstTrack(v->u.rail.track)][DirToDiagDir(v->direction)]; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2523 |
|
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2524 |
int best_track = -1; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2525 |
uint reverse = 0; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2526 |
uint best_bird_dist = 0; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2527 |
uint best_track_dist = 0; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2528 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2529 |
for (;;) { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2530 |
fd.best_bird_dist = UINT_MAX; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2531 |
fd.best_track_dist = UINT_MAX; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2532 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2533 |
NewTrainPathfind(v->tile, v->dest_tile, v->u.rail.compatible_railtypes, (DiagDirection)(reverse ^ i), (NTPEnumProc*)NtpCallbFindStation, &fd); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2534 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2535 |
if (best_track != -1) { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2536 |
if (best_bird_dist != 0) { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2537 |
if (fd.best_bird_dist != 0) { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2538 |
/* neither reached the destination, pick the one with the smallest bird dist */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2539 |
if (fd.best_bird_dist > best_bird_dist) goto bad; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2540 |
if (fd.best_bird_dist < best_bird_dist) goto good; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2541 |
} else { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2542 |
/* we found the destination for the first time */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2543 |
goto good; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2544 |
} |
1247 | 2545 |
} else { |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2546 |
if (fd.best_bird_dist != 0) { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2547 |
/* didn't find destination, but we've found the destination previously */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2548 |
goto bad; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2549 |
} else { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2550 |
/* both old & new reached the destination, compare track length */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2551 |
if (fd.best_track_dist > best_track_dist) goto bad; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2552 |
if (fd.best_track_dist < best_track_dist) goto good; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2553 |
} |
1247 | 2554 |
} |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2555 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2556 |
/* if we reach this position, there's two paths of equal value so far. |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2557 |
* pick one randomly. */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2558 |
int r = GB(Random(), 0, 8); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2559 |
if (_pick_track_table[i] == (v->direction & 3)) r += 80; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2560 |
if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2561 |
if (r <= 127) goto bad; |
0 | 2562 |
} |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2563 |
good:; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2564 |
best_track = i; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2565 |
best_bird_dist = fd.best_bird_dist; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2566 |
best_track_dist = fd.best_track_dist; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2567 |
reverse_best = reverse; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2568 |
bad:; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2569 |
if (reverse != 0) break; |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2570 |
reverse = 2; |
0 | 2571 |
} |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2572 |
} break; |
0 | 2573 |
} |
2574 |
||
2575 |
return reverse_best != 0; |
|
2576 |
} |
|
2577 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2578 |
TileIndex Train::GetOrderStationLocation(StationID station) |
0 | 2579 |
{ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2580 |
if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2581 |
|
10210
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
10200
diff
changeset
|
2582 |
const Station *st = GetStation(station); |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
10200
diff
changeset
|
2583 |
if (!(st->facilities & FACIL_TRAIN)) { |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
10200
diff
changeset
|
2584 |
/* The destination station has no trainstation tiles. */ |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
10200
diff
changeset
|
2585 |
this->cur_order_index++; |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
10200
diff
changeset
|
2586 |
return 0; |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
10200
diff
changeset
|
2587 |
} |
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
10200
diff
changeset
|
2588 |
|
a2131f7a315d
(svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents:
10200
diff
changeset
|
2589 |
return st->xy; |
0 | 2590 |
} |
2591 |
||
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2592 |
void Train::MarkDirty() |
0 | 2593 |
{ |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2594 |
Vehicle *v = this; |
0 | 2595 |
do { |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2596 |
v->cur_image = v->GetImage(v->direction); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2597 |
MarkSingleVehicleDirty(v); |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
2598 |
} while ((v = v->Next()) != NULL); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2599 |
|
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2600 |
/* need to update acceleration and cached values since the goods on the train changed. */ |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2601 |
TrainCargoChanged(this); |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2602 |
UpdateTrainAcceleration(this); |
0 | 2603 |
} |
2604 |
||
2605 |
static int UpdateTrainSpeed(Vehicle *v) |
|
2606 |
{ |
|
2607 |
uint accel; |
|
2608 |
||
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2609 |
if (v->vehstatus & VS_STOPPED || HasBit(v->u.rail.flags, VRF_REVERSING)) { |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
2610 |
if (_settings_game.vehicle.realistic_acceleration) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2611 |
accel = GetTrainAcceleration(v, AM_BRAKE) * 2; |
2639 | 2612 |
} else { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2613 |
accel = v->acceleration * -2; |
2639 | 2614 |
} |
0 | 2615 |
} else { |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
2616 |
if (_settings_game.vehicle.realistic_acceleration) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2617 |
accel = GetTrainAcceleration(v, AM_ACCEL); |
2639 | 2618 |
} else { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2619 |
accel = v->acceleration; |
2639 | 2620 |
} |
0 | 2621 |
} |
2622 |
||
6476 | 2623 |
uint spd = v->subspeed + accel * 2; |
0 | 2624 |
v->subspeed = (byte)spd; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2625 |
{ |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2626 |
int tempmax = v->max_speed; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2627 |
if (v->cur_speed > v->max_speed) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2628 |
tempmax = v->cur_speed - (v->cur_speed / 10) - 1; |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2629 |
v->cur_speed = spd = Clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2630 |
} |
0 | 2631 |
|
2632 |
if (!(v->direction & 1)) spd = spd * 3 >> 2; |
|
2633 |
||
2634 |
spd += v->progress; |
|
2635 |
v->progress = (byte)spd; |
|
2636 |
return (spd >> 8); |
|
2637 |
} |
|
2638 |
||
1551
eb05e6a90e67
(svn r2055) -CodeChange: Begun introducting StationID
celestar
parents:
1542
diff
changeset
|
2639 |
static void TrainEnterStation(Vehicle *v, StationID station) |
0 | 2640 |
{ |
2641 |
v->last_station_visited = station; |
|
2642 |
||
2643 |
/* check if a train ever visited this station before */ |
|
6476 | 2644 |
Station *st = GetStation(station); |
0 | 2645 |
if (!(st->had_vehicle_of_type & HVOT_TRAIN)) { |
2646 |
st->had_vehicle_of_type |= HVOT_TRAIN; |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
2647 |
SetDParam(0, st->index); |
0 | 2648 |
AddNewsItem( |
2649 |
STR_8801_CITIZENS_CELEBRATE_FIRST, |
|
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
2650 |
v->owner == _local_player ? NS_ARRIVAL_PLAYER : NS_ARRIVAL_OTHER, |
0 | 2651 |
v->index, |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
2652 |
0 |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
2653 |
); |
0 | 2654 |
} |
2655 |
||
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2656 |
v->BeginLoading(); |
10274
b3c58f3df92b
(svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents:
10242
diff
changeset
|
2657 |
|
b3c58f3df92b
(svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents:
10242
diff
changeset
|
2658 |
StationAnimationTrigger(st, v->tile, STAT_ANIM_TRAIN_ARRIVES); |
0 | 2659 |
} |
2660 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2661 |
static byte AfterSetTrainPos(Vehicle *v, bool new_tile) |
0 | 2662 |
{ |
6476 | 2663 |
byte old_z = v->z_pos; |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2664 |
v->z_pos = GetSlopeZ(v->x_pos, v->y_pos); |
0 | 2665 |
|
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2666 |
if (new_tile) { |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2667 |
ClrBit(v->u.rail.flags, VRF_GOINGUP); |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2668 |
ClrBit(v->u.rail.flags, VRF_GOINGDOWN); |
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2669 |
|
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2670 |
if (v->u.rail.track == TRACK_BIT_X || v->u.rail.track == TRACK_BIT_Y) { |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2671 |
/* Any track that isn't TRACK_BIT_X or TRACK_BIT_Y cannot be sloped. |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2672 |
* To check whether the current tile is sloped, and in which |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2673 |
* direction it is sloped, we get the 'z' at the center of |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2674 |
* the tile (middle_z) and the edge of the tile (old_z), |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2675 |
* which we then can compare. */ |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2676 |
static const int HALF_TILE_SIZE = TILE_SIZE / 2; |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2677 |
static const int INV_TILE_SIZE_MASK = ~(TILE_SIZE - 1); |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2678 |
|
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2679 |
byte middle_z = GetSlopeZ((v->x_pos & INV_TILE_SIZE_MASK) | HALF_TILE_SIZE, (v->y_pos & INV_TILE_SIZE_MASK) | HALF_TILE_SIZE); |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2680 |
|
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2681 |
/* For some reason tunnel tiles are always given as sloped :( |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2682 |
* But they are not sloped... */ |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2683 |
if (middle_z != v->z_pos && !IsTunnelTile(TileVirtXY(v->x_pos, v->y_pos))) { |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2684 |
SetBit(v->u.rail.flags, (middle_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN); |
3184
7405329343ce
(svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents:
3183
diff
changeset
|
2685 |
} |
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2686 |
} |
0 | 2687 |
} |
2688 |
||
2689 |
VehiclePositionChanged(v); |
|
2690 |
EndVehicleMove(v); |
|
2691 |
return old_z; |
|
2692 |
} |
|
2693 |
||
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3154
diff
changeset
|
2694 |
static const Direction _new_vehicle_direction_table[11] = { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2695 |
DIR_N , DIR_NW, DIR_W , INVALID_DIR, |
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2696 |
DIR_NE, DIR_N , DIR_SW, INVALID_DIR, |
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3154
diff
changeset
|
2697 |
DIR_E , DIR_SE, DIR_S |
0 | 2698 |
}; |
2699 |
||
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2700 |
static inline Direction GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile) |
0 | 2701 |
{ |
926
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
2702 |
uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 + |
bd4312619522
(svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents:
919
diff
changeset
|
2703 |
TileX(new_tile) - TileX(old_tile) + 1; |
0 | 2704 |
assert(offs < 11); |
2705 |
return _new_vehicle_direction_table[offs]; |
|
2706 |
} |
|
2707 |
||
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2708 |
static inline int GetDirectionToVehicle(const Vehicle *v, int x, int y) |
0 | 2709 |
{ |
2710 |
byte offs; |
|
2711 |
||
2712 |
x -= v->x_pos; |
|
2713 |
if (x >= 0) { |
|
2714 |
offs = (x > 2) ? 0 : 1; |
|
2715 |
} else { |
|
2716 |
offs = (x < -2) ? 2 : 1; |
|
2717 |
} |
|
2718 |
||
2719 |
y -= v->y_pos; |
|
2720 |
if (y >= 0) { |
|
2721 |
offs += ((y > 2) ? 0 : 1) * 4; |
|
2722 |
} else { |
|
2723 |
offs += ((y < -2) ? 2 : 1) * 4; |
|
2724 |
} |
|
2725 |
||
2726 |
assert(offs < 11); |
|
2727 |
return _new_vehicle_direction_table[offs]; |
|
2728 |
} |
|
2729 |
||
2730 |
/* Check if the vehicle is compatible with the specified tile */ |
|
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2731 |
static inline bool CheckCompatibleRail(const Vehicle *v, TileIndex tile) |
0 | 2732 |
{ |
1048 | 2733 |
return |
2549 | 2734 |
IsTileOwner(tile, v->owner) && ( |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
2735 |
!IsFrontEngine(v) || |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2736 |
HasBit(v->u.rail.compatible_railtypes, GetRailType(tile)) |
2549 | 2737 |
); |
0 | 2738 |
} |
2739 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2740 |
struct RailtypeSlowdownParams { |
0 | 2741 |
byte small_turn, large_turn; |
2742 |
byte z_up; // fraction to remove when moving up |
|
2743 |
byte z_down; // fraction to remove when moving down |
|
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2744 |
}; |
0 | 2745 |
|
3355
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
2746 |
static const RailtypeSlowdownParams _railtype_slowdown[] = { |
0 | 2747 |
// normal accel |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2748 |
{256 / 4, 256 / 2, 256 / 4, 2}, ///< normal |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2749 |
{256 / 4, 256 / 2, 256 / 4, 2}, ///< electrified |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2750 |
{256 / 4, 256 / 2, 256 / 4, 2}, ///< monorail |
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2751 |
{0, 256 / 2, 256 / 4, 2}, ///< maglev |
0 | 2752 |
}; |
2753 |
||
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2754 |
/** Modify the speed of the vehicle due to a turn */ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2755 |
static inline void AffectSpeedByDirChange(Vehicle *v, Direction new_dir) |
0 | 2756 |
{ |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
2757 |
if (_settings_game.vehicle.realistic_acceleration) return; |
3158
a821c8fec829
(svn r3784) Add a type and functions to handle direction changes
tron
parents:
3157
diff
changeset
|
2758 |
|
6476 | 2759 |
DirDiff diff = DirDifference(v->direction, new_dir); |
3158
a821c8fec829
(svn r3784) Add a type and functions to handle direction changes
tron
parents:
3157
diff
changeset
|
2760 |
if (diff == DIRDIFF_SAME) return; |
0 | 2761 |
|
6476 | 2762 |
const RailtypeSlowdownParams *rsp = &_railtype_slowdown[v->u.rail.railtype]; |
3158
a821c8fec829
(svn r3784) Add a type and functions to handle direction changes
tron
parents:
3157
diff
changeset
|
2763 |
v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8; |
0 | 2764 |
} |
2765 |
||
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2766 |
/** Modify the speed of the vehicle due to a change in altitude */ |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2767 |
static inline void AffectSpeedByZChange(Vehicle *v, byte old_z) |
0 | 2768 |
{ |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
2769 |
if (old_z == v->z_pos || _settings_game.vehicle.realistic_acceleration) return; |
0 | 2770 |
|
6476 | 2771 |
const RailtypeSlowdownParams *rsp = &_railtype_slowdown[v->u.rail.railtype]; |
0 | 2772 |
|
2773 |
if (old_z < v->z_pos) { |
|
2774 |
v->cur_speed -= (v->cur_speed * rsp->z_up >> 8); |
|
2775 |
} else { |
|
2776 |
uint16 spd = v->cur_speed + rsp->z_down; |
|
2639 | 2777 |
if (spd <= v->max_speed) v->cur_speed = spd; |
0 | 2778 |
} |
2779 |
} |
|
2780 |
||
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3140
diff
changeset
|
2781 |
static void TrainMovedChangeSignals(TileIndex tile, DiagDirection dir) |
0 | 2782 |
{ |
3267
591027d10884
(svn r3979) Move GetRailFoundation() to rail_map.h and use it and friends to get information about rail tiles
tron
parents:
3259
diff
changeset
|
2783 |
if (IsTileType(tile, MP_RAILWAY) && |
3792
2eb8388731bf
(svn r4788) - Codechange: RAILTYPE_{NORMAL,ELECTRIC,...} and RAIL_TYPE_{NORMAL,SIGNAL,...} have nearly the same name, rename RAIL_TYPE_* to RAIL_TILE_* of extra clarity
rubidium
parents:
3773
diff
changeset
|
2784 |
GetRailTileType(tile) == RAIL_TILE_SIGNALS) { |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2785 |
TrackdirBits tracks = TrackBitsToTrackdirBits(GetTrackBits(tile)) & DiagdirReachesTrackdirs(dir); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2786 |
Trackdir trackdir = FindFirstTrackdir(tracks); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2787 |
UpdateSignalsOnSegment(tile, TrackdirToExitdir(trackdir), GetTileOwner(tile)); |
0 | 2788 |
} |
2789 |
} |
|
2790 |
||
2791 |
||
2792 |
static void SetVehicleCrashed(Vehicle *v) |
|
2793 |
{ |
|
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
2794 |
if (v->u.rail.crash_anim_pos != 0) return; |
0 | 2795 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2796 |
/* we may need to update crossing we were approaching */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2797 |
TileIndex crossing = TrainApproachingCrossingTile(v); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2798 |
|
0 | 2799 |
v->u.rail.crash_anim_pos++; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2800 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2801 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2802 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2803 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2804 |
if (v->u.rail.track == TRACK_BIT_DEPOT) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2805 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2806 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2807 |
|
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
2808 |
InvalidateWindowClassesData(WC_TRAINS_LIST, 0); |
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
2809 |
|
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
2810 |
for (; v != NULL; v = v->Next()) { |
0 | 2811 |
v->vehstatus |= VS_CRASHED; |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2812 |
MarkSingleVehicleDirty(v); |
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
2813 |
} |
0 | 2814 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2815 |
/* must be updated after the train has been marked crashed */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2816 |
if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing); |
0 | 2817 |
} |
2818 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2819 |
static uint CountPassengersInTrain(const Vehicle *v) |
0 | 2820 |
{ |
2549 | 2821 |
uint num = 0; |
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
2822 |
|
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
2823 |
for (; v != NULL; v = v->Next()) { |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2824 |
if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) num += v->cargo.Count(); |
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
2825 |
} |
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
2826 |
|
0 | 2827 |
return num; |
2828 |
} |
|
2829 |
||
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2830 |
struct TrainCollideChecker { |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2831 |
Vehicle *v; ///< vehicle we are testing for collision |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2832 |
uint num; ///< number of dead if train collided |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2833 |
}; |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2834 |
|
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2835 |
static void *FindTrainCollideEnum(Vehicle *v, void *data) |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2836 |
{ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2837 |
TrainCollideChecker *tcc = (TrainCollideChecker*)data; |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2838 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2839 |
if (v->type != VEH_TRAIN) return NULL; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2840 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2841 |
/* get first vehicle now to make most usual checks faster */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2842 |
Vehicle *coll = v->First(); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2843 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2844 |
/* can't collide with own wagons && can't crash in depot && the same height level */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2845 |
if (coll != tcc->v && v->u.rail.track != TRACK_BIT_DEPOT && abs(v->z_pos - tcc->v->z_pos) < 6) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2846 |
int x_diff = v->x_pos - tcc->v->x_pos; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2847 |
int y_diff = v->y_pos - tcc->v->y_pos; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2848 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2849 |
/* needed to disable possible crash of competitor train in station by building diagonal track at its end */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2850 |
if (x_diff * x_diff + y_diff * y_diff > 25) return NULL; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2851 |
|
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2852 |
if (!(tcc->v->vehstatus & VS_CRASHED)) { |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2853 |
/* two drivers + passengers killed in train tcc->v (if it was not crashed already) */ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2854 |
tcc->num += 2 + CountPassengersInTrain(tcc->v); |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2855 |
SetVehicleCrashed(tcc->v); |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2856 |
} |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2857 |
|
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2858 |
if (!(coll->vehstatus & VS_CRASHED)) { |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2859 |
/* two drivers + passengers killed in train coll (if it was not crashed already) */ |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2860 |
tcc->num += 2 + CountPassengersInTrain(coll); |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2861 |
SetVehicleCrashed(coll); |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2862 |
} |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2863 |
} |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2864 |
|
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2865 |
return NULL; |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2866 |
} |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2867 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
2868 |
/** |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2869 |
* Checks whether the specified train has a collision with another vehicle. If |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
2870 |
* so, destroys this vehicle, and the other vehicle if its subtype has TS_Front. |
22 | 2871 |
* Reports the incident in a flashy news item, modifies station ratings and |
2872 |
* plays a sound. |
|
2873 |
*/ |
|
0 | 2874 |
static void CheckTrainCollision(Vehicle *v) |
2875 |
{ |
|
2876 |
/* can't collide in depot */ |
|
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2877 |
if (v->u.rail.track == TRACK_BIT_DEPOT) return; |
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2878 |
|
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2879 |
assert(v->u.rail.track == TRACK_BIT_WORMHOLE || TileVirtXY(v->x_pos, v->y_pos) == v->tile); |
0 | 2880 |
|
6476 | 2881 |
TrainCollideChecker tcc; |
0 | 2882 |
tcc.v = v; |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2883 |
tcc.num = 0; |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2884 |
|
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2885 |
/* find colliding vehicles */ |
6743
cabfaa4a0295
(svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents:
6725
diff
changeset
|
2886 |
if (v->u.rail.track == TRACK_BIT_WORMHOLE) { |
cabfaa4a0295
(svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents:
6725
diff
changeset
|
2887 |
VehicleFromPos(v->tile, &tcc, FindTrainCollideEnum); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2888 |
VehicleFromPos(GetOtherTunnelBridgeEnd(v->tile), &tcc, FindTrainCollideEnum); |
6743
cabfaa4a0295
(svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents:
6725
diff
changeset
|
2889 |
} else { |
cabfaa4a0295
(svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents:
6725
diff
changeset
|
2890 |
VehicleFromPosXY(v->x_pos, v->y_pos, &tcc, FindTrainCollideEnum); |
cabfaa4a0295
(svn r10766) [NewGRF_ports] -Sync: with trunk r10651-10765
richk
parents:
6725
diff
changeset
|
2891 |
} |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2892 |
|
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2893 |
/* any dead -> no crash */ |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2894 |
if (tcc.num == 0) return; |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2895 |
|
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2896 |
SetDParam(0, tcc.num); |
0 | 2897 |
AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL, |
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
2898 |
NS_ACCIDENT_VEHICLE, |
0 | 2899 |
v->index, |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2900 |
0 |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2901 |
); |
0 | 2902 |
|
2903 |
ModifyStationRatingAround(v->tile, v->owner, -160, 30); |
|
541 | 2904 |
SndPlayVehicleFx(SND_13_BIG_CRASH, v); |
0 | 2905 |
} |
2906 |
||
2907 |
static void *CheckVehicleAtSignal(Vehicle *v, void *data) |
|
2908 |
{ |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2909 |
Direction dir = *(Direction*)data; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2910 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2911 |
if (v->type == VEH_TRAIN && IsFrontEngine(v)) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2912 |
DirDiff diff = ChangeDirDiff(DirDifference(v->direction, dir), DIRDIFF_90RIGHT); |
3158
a821c8fec829
(svn r3784) Add a type and functions to handle direction changes
tron
parents:
3157
diff
changeset
|
2913 |
|
a821c8fec829
(svn r3784) Add a type and functions to handle direction changes
tron
parents:
3157
diff
changeset
|
2914 |
if (diff == DIRDIFF_90RIGHT || (v->cur_speed <= 5 && diff <= DIRDIFF_REVERSE)) return v; |
0 | 2915 |
} |
1432
9086ccebb3f1
(svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents:
1431
diff
changeset
|
2916 |
return NULL; |
0 | 2917 |
} |
2918 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2919 |
static void TrainController(Vehicle *v, Vehicle *nomove, bool update_image) |
0 | 2920 |
{ |
1961
10ce7350f76b
(svn r2467) - Fix: [newgrf] Prevent trains with shorter wagons breaking apart when reversing in some cases. (Therax)
hackykid
parents:
1942
diff
changeset
|
2921 |
Vehicle *prev; |
0 | 2922 |
|
22 | 2923 |
/* For every vehicle after and including the given vehicle */ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2924 |
for (prev = v->Previous(); v != nomove; prev = v, v = v->Next()) { |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2925 |
DiagDirection enterdir = DIAGDIR_BEGIN; |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2926 |
bool update_signals_crossing = false; // will we update signals or crossing state? |
0 | 2927 |
BeginVehicleMove(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2928 |
|
6479 | 2929 |
GetNewVehiclePosResult gp = GetNewVehiclePos(v); |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2930 |
if (v->u.rail.track != TRACK_BIT_WORMHOLE) { |
22 | 2931 |
/* Not inside tunnel */ |
6478 | 2932 |
if (gp.old_tile == gp.new_tile) { |
22 | 2933 |
/* Staying in the old tile */ |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2934 |
if (v->u.rail.track == TRACK_BIT_DEPOT) { |
6320
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
2935 |
/* Inside depot */ |
0 | 2936 |
gp.x = v->x_pos; |
2937 |
gp.y = v->y_pos; |
|
2938 |
} else { |
|
6320
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
2939 |
/* Not inside depot */ |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2940 |
|
5919
2b58160d667d
(svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents:
5902
diff
changeset
|
2941 |
if (IsFrontEngine(v) && !TrainCheckIfLineEnds(v)) return; |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2942 |
|
6476 | 2943 |
uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2944 |
if (HasBit(r, VETS_CANNOT_ENTER)) { |
0 | 2945 |
goto invalid_rail; |
1247 | 2946 |
} |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
2947 |
if (HasBit(r, VETS_ENTERED_STATION)) { |
6317
c73bda71ac16
(svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents:
6195
diff
changeset
|
2948 |
TrainEnterStation(v, r >> VETS_STATION_ID_OFFSET); |
0 | 2949 |
return; |
2950 |
} |
|
2951 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2952 |
if (v->current_order.IsType(OT_LEAVESTATION)) { |
6589
addf4b6946d0
(svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents:
6585
diff
changeset
|
2953 |
v->current_order.Free(); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
2954 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
0 | 2955 |
} |
2956 |
} |
|
2957 |
} else { |
|
2958 |
/* A new tile is about to be entered. */ |
|
2959 |
||
2960 |
/* Determine what direction we're entering the new tile from */ |
|
6477 | 2961 |
Direction dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile); |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
2962 |
enterdir = DirToDiagDir(dir); |
6320
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
2963 |
assert(IsValidDiagDirection(enterdir)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2964 |
|
0 | 2965 |
/* Get the status of the tracks in the new tile and mask |
2966 |
* away the bits that aren't reachable. */ |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2967 |
TrackStatus ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL, 0, ReverseDiagDir(enterdir)); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2968 |
TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(enterdir); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2969 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2970 |
TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
2971 |
TrackBits red_signals = TrackdirBitsToTrackBits(TrackStatusToRedSignals(ts) & reachable_trackdirs); |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2972 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2973 |
TrackBits bits = TrackdirBitsToTrackBits(trackdirbits); |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
2974 |
if (_settings_game.pf.pathfinder_for_trains != VPF_NTP && _settings_game.pf.forbid_90_deg && prev == NULL) { |
1247 | 2975 |
/* We allow wagons to make 90 deg turns, because forbid_90_deg |
2976 |
* can be switched on halfway a turn */ |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2977 |
bits &= ~TrackCrossesTracks(FindFirstTrack(v->u.rail.track)); |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
2978 |
} |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
2979 |
|
6320
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
2980 |
if (bits == TRACK_BIT_NONE) goto invalid_rail; |
0 | 2981 |
|
2982 |
/* Check if the new tile contrains tracks that are compatible |
|
2983 |
* with the current train, if not, bail out. */ |
|
6320
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
2984 |
if (!CheckCompatibleRail(v, gp.new_tile)) goto invalid_rail; |
0 | 2985 |
|
6476 | 2986 |
TrackBits chosen_track; |
0 | 2987 |
if (prev == NULL) { |
2988 |
/* Currently the locomotive is active. Determine which one of the |
|
2989 |
* available tracks to choose */ |
|
6481 | 2990 |
chosen_track = TrackToTrackBits(ChooseTrainTrack(v, gp.new_tile, enterdir, bits)); |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2991 |
assert(chosen_track & bits); |
0 | 2992 |
|
2993 |
/* Check if it's a red signal and that force proceed is not clicked. */ |
|
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2994 |
if (red_signals & chosen_track && v->u.rail.force_proceed == 0) { |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2995 |
/* In front of a red signal */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
2996 |
Trackdir i = FindFirstTrackdir(trackdirbits); |
6477 | 2997 |
|
2998 |
if (!HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(i))) { |
|
2999 |
v->cur_speed = 0; |
|
3000 |
v->subspeed = 0; |
|
3001 |
v->progress = 255 - 100; |
|
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
3002 |
if (++v->load_unload_time_rem < _settings_game.pf.wait_oneway_signal * 20) return; |
6477 | 3003 |
} else if (HasSignalOnTrackdir(gp.new_tile, i)) { |
3004 |
v->cur_speed = 0; |
|
3005 |
v->subspeed = 0; |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
3006 |
v->progress = 255 - 10; |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
3007 |
if (++v->load_unload_time_rem < _settings_game.pf.wait_twoway_signal * 73) { |
6477 | 3008 |
TileIndex o_tile = gp.new_tile + TileOffsByDiagDir(enterdir); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3009 |
Direction rdir = ReverseDir(dir); |
6477 | 3010 |
|
3011 |
/* check if a train is waiting on the other side */ |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3012 |
if (VehicleFromPos(o_tile, &rdir, &CheckVehicleAtSignal) == NULL) return; |
6477 | 3013 |
} |
3014 |
} |
|
3015 |
goto reverse_train_direction; |
|
3016 |
} |
|
0 | 3017 |
} else { |
6320
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
3018 |
static const TrackBits _matching_tracks[8] = { |
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
3019 |
TRACK_BIT_LEFT | TRACK_BIT_RIGHT, TRACK_BIT_X, |
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
3020 |
TRACK_BIT_UPPER | TRACK_BIT_LOWER, TRACK_BIT_Y, |
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
3021 |
TRACK_BIT_LEFT | TRACK_BIT_RIGHT, TRACK_BIT_X, |
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
3022 |
TRACK_BIT_UPPER | TRACK_BIT_LOWER, TRACK_BIT_Y |
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
3023 |
}; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3024 |
|
0 | 3025 |
/* The wagon is active, simply follow the prev vehicle. */ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
3026 |
chosen_track = (TrackBits)(byte)(_matching_tracks[GetDirectionToVehicle(prev, gp.x, gp.y)] & bits); |
0 | 3027 |
} |
3028 |
||
6320
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
3029 |
/* Make sure chosen track is a valid track */ |
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
3030 |
assert( |
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
3031 |
chosen_track == TRACK_BIT_X || chosen_track == TRACK_BIT_Y || |
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
3032 |
chosen_track == TRACK_BIT_UPPER || chosen_track == TRACK_BIT_LOWER || |
5fc5a089b147
(svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents:
6319
diff
changeset
|
3033 |
chosen_track == TRACK_BIT_LEFT || chosen_track == TRACK_BIT_RIGHT); |
0 | 3034 |
|
3035 |
/* Update XY to reflect the entrance to the new tile, and select the direction to use */ |
|
6476 | 3036 |
const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir]; |
3037 |
gp.x = (gp.x & ~0xF) | b[0]; |
|
3038 |
gp.y = (gp.y & ~0xF) | b[1]; |
|
3039 |
Direction chosen_dir = (Direction)b[2]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3040 |
|
0 | 3041 |
/* Call the landscape function and tell it that the vehicle entered the tile */ |
6476 | 3042 |
uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
3043 |
if (HasBit(r, VETS_CANNOT_ENTER)) { |
0 | 3044 |
goto invalid_rail; |
1247 | 3045 |
} |
0 | 3046 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
3047 |
if (IsFrontEngine(v)) v->load_unload_time_rem = 0; |
0 | 3048 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
3049 |
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) { |
0 | 3050 |
v->tile = gp.new_tile; |
3355
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
3051 |
|
6480 | 3052 |
if (GetTileRailType(gp.new_tile) != GetTileRailType(gp.old_tile)) { |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3053 |
TrainPowerChanged(v->First()); |
3355
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
3054 |
} |
a653b8e47f27
(svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents:
3341
diff
changeset
|
3055 |
|
0 | 3056 |
v->u.rail.track = chosen_track; |
1330
8a67d04016ce
(svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents:
1313
diff
changeset
|
3057 |
assert(v->u.rail.track); |
0 | 3058 |
} |
3059 |
||
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3060 |
/* We need to update signal status, but after the vehicle position hash |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3061 |
* has been updated by AfterSetTrainPos() */ |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3062 |
update_signals_crossing = true; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3063 |
|
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3064 |
if (prev == NULL) AffectSpeedByDirChange(v, chosen_dir); |
0 | 3065 |
|
3066 |
v->direction = chosen_dir; |
|
3067 |
} |
|
3068 |
} else { |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3069 |
/* In a tunnel or on a bridge |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3070 |
* - for tunnels, only the part when the vehicle is not visible (part of enter/exit tile too) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3071 |
* - for bridges, only the middle part - without the bridge heads */ |
6467 | 3072 |
if (!(v->vehstatus & VS_HIDDEN)) { |
3073 |
v->cur_speed = |
|
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3074 |
min(v->cur_speed, GetBridgeSpec(GetBridgeType(v->tile))->speed); |
6467 | 3075 |
} |
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
3076 |
|
6877
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3077 |
if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) { |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
3078 |
v->x_pos = gp.x; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
3079 |
v->y_pos = gp.y; |
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
3080 |
VehiclePositionChanged(v); |
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
3081 |
if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v); |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
3082 |
continue; |
0 | 3083 |
} |
3084 |
} |
|
3085 |
||
3086 |
/* update image of train, as well as delta XY */ |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3087 |
v->UpdateDeltaXY(v->direction); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3088 |
if (update_image) v->cur_image = v->GetImage(v->direction); |
0 | 3089 |
|
3090 |
v->x_pos = gp.x; |
|
3091 |
v->y_pos = gp.y; |
|
3092 |
||
3093 |
/* update the Z position of the vehicle */ |
|
6476 | 3094 |
byte 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
|
3095 |
|
0 | 3096 |
if (prev == NULL) { |
22 | 3097 |
/* This is the first vehicle in the train */ |
0 | 3098 |
AffectSpeedByZChange(v, old_z); |
3099 |
} |
|
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3100 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3101 |
if (update_signals_crossing) { |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3102 |
if (IsFrontEngine(v)) TrainMovedChangeSignals(gp.new_tile, enterdir); |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3103 |
|
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3104 |
/* Signals can only change when the first |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3105 |
* (above) or the last vehicle moves. */ |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3106 |
if (v->Next() == NULL) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3107 |
TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir)); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3108 |
if (IsLevelCrossingTile(gp.old_tile)) UpdateLevelCrossing(gp.old_tile); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3109 |
} |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3110 |
} |
0 | 3111 |
} |
1438 | 3112 |
return; |
0 | 3113 |
|
3114 |
invalid_rail: |
|
22 | 3115 |
/* We've reached end of line?? */ |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
3116 |
if (prev != NULL) error("Disconnecting train"); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3117 |
|
0 | 3118 |
reverse_train_direction: |
3119 |
v->load_unload_time_rem = 0; |
|
3120 |
v->cur_speed = 0; |
|
3121 |
v->subspeed = 0; |
|
3122 |
ReverseTrainDirection(v); |
|
3123 |
} |
|
3124 |
||
1418
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3125 |
/** |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3126 |
* Deletes/Clears the last wagon of a crashed train. It takes the engine of the |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3127 |
* train, then goes to the last wagon and deletes that. Each call to this function |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3128 |
* will remove the last wagon of a crashed train. If this wagon was on a crossing, |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3129 |
* or inside a tunnel/bridge, recalculate the signals as they might need updating |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
3130 |
* @param v the Vehicle of which last wagon is to be removed |
1418
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3131 |
*/ |
0 | 3132 |
static void DeleteLastWagon(Vehicle *v) |
3133 |
{ |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3134 |
Vehicle *first = v->First(); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3135 |
|
1418
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3136 |
/* Go to the last wagon and delete the link pointing there |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3137 |
* *u is then the one-before-last wagon, and *v the last |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3138 |
* one which will physicially be removed */ |
6476 | 3139 |
Vehicle *u = v; |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3140 |
for (; v->Next() != NULL; v = v->Next()) u = v; |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3141 |
u->SetNext(NULL); |
0 | 3142 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3143 |
if (first == v) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3144 |
/* Removing front vehicle (the last to go) */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3145 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3146 |
InvalidateWindow(WC_COMPANY, v->owner); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3147 |
} else { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3148 |
/* Recalculate cached train properties */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3149 |
TrainConsistChanged(first); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3150 |
/* Update the depot window if the first vehicle is in depot - |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3151 |
* if v == first, then it is updated in PreDestructor() */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3152 |
if (first->u.rail.track == TRACK_BIT_DEPOT) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3153 |
InvalidateWindow(WC_VEHICLE_DEPOT, first->tile); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3154 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3155 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3156 |
|
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
3157 |
InvalidateWindowClassesData(WC_TRAINS_LIST, 0); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3158 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3159 |
MarkSingleVehicleDirty(v); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3160 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3161 |
/* 'v' shouldn't be accessed after it has been deleted */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3162 |
TrackBits track = v->u.rail.track; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3163 |
TileIndex tile = v->tile; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3164 |
Owner owner = v->owner; |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
3165 |
|
6800
6c09e1e86fcb
(svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents:
6743
diff
changeset
|
3166 |
delete v; |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3167 |
v = NULL; // make sure nobody will won't try to read 'v' anymore |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3168 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3169 |
/* check if the wagon was on a road/rail-crossing */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3170 |
if (IsLevelCrossingTile(tile)) UpdateLevelCrossing(tile); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3171 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3172 |
/* Update signals */ |
10242
52b4a9006029
(svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents:
10211
diff
changeset
|
3173 |
if (IsTileType(tile, MP_TUNNELBRIDGE) || IsRailDepotTile(tile)) { |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3174 |
UpdateSignalsOnSegment(tile, INVALID_DIAGDIR, owner); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3175 |
} else { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3176 |
SetSignalsOnBothDir(tile, (Track)(FIND_FIRST_BIT(track)), owner); |
98 | 3177 |
} |
0 | 3178 |
} |
3179 |
||
3180 |
static void ChangeTrainDirRandomly(Vehicle *v) |
|
3181 |
{ |
|
3160 | 3182 |
static const DirDiff delta[] = { |
3183 |
DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT |
|
3184 |
}; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3185 |
|
0 | 3186 |
do { |
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
3187 |
/* We don't need to twist around vehicles if they're not visible */ |
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
3188 |
if (!(v->vehstatus & VS_HIDDEN)) { |
3977
edb5b94e2094
(svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents:
3956
diff
changeset
|
3189 |
v->direction = ChangeDir(v->direction, delta[GB(Random(), 0, 2)]); |
0 | 3190 |
BeginVehicleMove(v); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
3191 |
v->UpdateDeltaXY(v->direction); |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3192 |
v->cur_image = v->GetImage(v->direction); |
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
3193 |
/* Refrain from updating the z position of the vehicle when on |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3194 |
* a bridge, because AfterSetTrainPos will put the vehicle under |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3195 |
* the bridge in that case */ |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
3196 |
if (v->u.rail.track != TRACK_BIT_WORMHOLE) AfterSetTrainPos(v, false); |
0 | 3197 |
} |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3198 |
} while ((v = v->Next()) != NULL); |
0 | 3199 |
} |
3200 |
||
3201 |
static void HandleCrashedTrain(Vehicle *v) |
|
3202 |
{ |
|
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3203 |
int state = ++v->u.rail.crash_anim_pos; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3204 |
|
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
3205 |
if (state == 4 && !(v->vehstatus & VS_HIDDEN)) { |
1359
8ba976aed634
(svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents:
1337
diff
changeset
|
3206 |
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE); |
0 | 3207 |
} |
3208 |
||
6476 | 3209 |
uint32 r; |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
3210 |
if (state <= 200 && Chance16R(1, 7, r)) { |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3211 |
int index = (r * 10 >> 16); |
0 | 3212 |
|
6476 | 3213 |
Vehicle *u = v; |
0 | 3214 |
do { |
3215 |
if (--index < 0) { |
|
1137
c492b777af49
(svn r1638) -Fix: Train crashes should no longer desync the game. This is more of a
celestar
parents:
1132
diff
changeset
|
3216 |
r = Random(); |
0 | 3217 |
|
3218 |
CreateEffectVehicleRel(u, |
|
2140
d708eb80ab8b
(svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents:
2135
diff
changeset
|
3219 |
GB(r, 8, 3) + 2, |
d708eb80ab8b
(svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents:
2135
diff
changeset
|
3220 |
GB(r, 16, 3) + 2, |
d708eb80ab8b
(svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents:
2135
diff
changeset
|
3221 |
GB(r, 0, 3) + 5, |
1359
8ba976aed634
(svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents:
1337
diff
changeset
|
3222 |
EV_EXPLOSION_SMALL); |
0 | 3223 |
break; |
3224 |
} |
|
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3225 |
} while ((u = u->Next()) != NULL); |
0 | 3226 |
} |
3227 |
||
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3228 |
if (state <= 240 && !(v->tick_counter & 3)) ChangeTrainDirRandomly(v); |
0 | 3229 |
|
10242
52b4a9006029
(svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents:
10211
diff
changeset
|
3230 |
if (state >= 4440 && !(v->tick_counter & 0x1F)) { |
0 | 3231 |
DeleteLastWagon(v); |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
3232 |
InvalidateWindow(WC_REPLACE_VEHICLE, (v->group_id << 16) | VEH_TRAIN); |
1128
d3ffc98b92ad
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1109
diff
changeset
|
3233 |
} |
0 | 3234 |
} |
3235 |
||
3236 |
static void HandleBrokenTrain(Vehicle *v) |
|
3237 |
{ |
|
3238 |
if (v->breakdown_ctr != 1) { |
|
3239 |
v->breakdown_ctr = 1; |
|
3240 |
v->cur_speed = 0; |
|
3241 |
||
3242 |
if (v->breakdowns_since_last_service != 255) |
|
3243 |
v->breakdowns_since_last_service++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3244 |
|
0 | 3245 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
3246 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3247 |
|
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
3248 |
if (!PlayVehicleSound(v, VSE_BREAKDOWN)) { |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
3249 |
SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ? |
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
3250 |
SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v); |
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
3251 |
} |
0 | 3252 |
|
3253 |
if (!(v->vehstatus & VS_HIDDEN)) { |
|
3254 |
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); |
|
10274
b3c58f3df92b
(svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents:
10242
diff
changeset
|
3255 |
if (u != NULL) u->u.effect.animation_state = v->breakdown_delay * 2; |
0 | 3256 |
} |
3257 |
} |
|
3258 |
||
3259 |
if (!(v->tick_counter & 3)) { |
|
3260 |
if (!--v->breakdown_delay) { |
|
3261 |
v->breakdown_ctr = 0; |
|
3262 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
|
3263 |
} |
|
3264 |
} |
|
3265 |
} |
|
3266 |
||
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3267 |
/** Maximum speeds for train that is broken down or approaching line end */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3268 |
static const uint16 _breakdown_speeds[16] = { |
0 | 3269 |
225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15 |
3270 |
}; |
|
3271 |
||
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3272 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3273 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3274 |
* Train is approaching line end, slow down and possibly reverse |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3275 |
* |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3276 |
* @param v front train engine |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3277 |
* @param signal not line end, just a red signal |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3278 |
* @return true iff we did NOT have to reverse |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3279 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3280 |
static bool TrainApproachingLineEnd(Vehicle *v, bool signal) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3281 |
{ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3282 |
/* Calc position within the current tile */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3283 |
uint x = v->x_pos & 0xF; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3284 |
uint y = v->y_pos & 0xF; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3285 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3286 |
/* for diagonal directions, 'x' will be 0..15 - |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3287 |
* for other directions, it will be 1, 3, 5, ..., 15 */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3288 |
switch (v->direction) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3289 |
case DIR_N : x = ~x + ~y + 25; break; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3290 |
case DIR_NW: x = y; /* FALLTHROUGH */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3291 |
case DIR_NE: x = ~x + 16; break; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3292 |
case DIR_E : x = ~x + y + 9; break; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3293 |
case DIR_SE: x = y; break; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3294 |
case DIR_S : x = x + y - 7; break; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3295 |
case DIR_W : x = ~y + x + 9; break; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3296 |
default: break; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3297 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3298 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3299 |
/* do not reverse when approaching red signal */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3300 |
if (!signal && x + 4 >= TILE_SIZE) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3301 |
/* we are too near the tile end, reverse now */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3302 |
v->cur_speed = 0; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3303 |
ReverseTrainDirection(v); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3304 |
return false; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3305 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3306 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3307 |
/* slow down */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3308 |
v->vehstatus |= VS_TRAIN_SLOWING; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3309 |
uint16 break_speed = _breakdown_speeds[x & 0xF]; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3310 |
if (break_speed < v->cur_speed) v->cur_speed = break_speed; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3311 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3312 |
return true; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3313 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3314 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3315 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3316 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3317 |
* Determines whether train would like to leave the tile |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3318 |
* @param v train to test |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3319 |
* @return true iff vehicle is NOT entering or inside a depot or tunnel/bridge |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3320 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3321 |
static bool TrainCanLeaveTile(const Vehicle *v) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3322 |
{ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3323 |
/* Exit if inside a tunnel/bridge or a depot */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3324 |
if (v->u.rail.track == TRACK_BIT_WORMHOLE || v->u.rail.track == TRACK_BIT_DEPOT) return false; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3325 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3326 |
TileIndex tile = v->tile; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3327 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3328 |
/* entering a tunnel/bridge? */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3329 |
if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3330 |
DiagDirection dir = GetTunnelBridgeDirection(tile); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3331 |
if (DiagDirToDir(dir) == v->direction) return false; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3332 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3333 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3334 |
/* entering a depot? */ |
10242
52b4a9006029
(svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents:
10211
diff
changeset
|
3335 |
if (IsRailDepotTile(tile)) { |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3336 |
DiagDirection dir = ReverseDiagDir(GetRailDepotDirection(tile)); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3337 |
if (DiagDirToDir(dir) == v->direction) return false; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3338 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3339 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3340 |
return true; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3341 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3342 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3343 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3344 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3345 |
* Determines whether train is approaching a rail-road crossing |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3346 |
* (thus making it barred) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3347 |
* @param v front engine of train |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3348 |
* @return TileIndex of crossing the train is approaching, else INVALID_TILE |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3349 |
* @pre v in non-crashed front engine |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3350 |
*/ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3351 |
static TileIndex TrainApproachingCrossingTile(const Vehicle *v) |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3352 |
{ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3353 |
assert(IsFrontEngine(v)); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3354 |
assert(!(v->vehstatus & VS_CRASHED)); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3355 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3356 |
if (!TrainCanLeaveTile(v)) return INVALID_TILE; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3357 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3358 |
DiagDirection dir = TrainExitDir(v->direction, v->u.rail.track); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3359 |
TileIndex tile = v->tile + TileOffsByDiagDir(dir); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3360 |
|
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3361 |
/* not a crossing || wrong axis || unusable rail (wrong type or owner) */ |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3362 |
if (!IsLevelCrossingTile(tile) || DiagDirToAxis(dir) == GetCrossingRoadAxis(tile) || |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3363 |
!CheckCompatibleRail(v, tile)) { |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3364 |
return INVALID_TILE; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3365 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3366 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3367 |
return tile; |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3368 |
} |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3369 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3370 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3371 |
/** |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3372 |
* Checks for line end. Also, bars crossing at next tile if needed |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3373 |
* |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3374 |
* @param v vehicle we are checking |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3375 |
* @return true iff we did NOT have to reverse |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3376 |
*/ |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3377 |
static bool TrainCheckIfLineEnds(Vehicle *v) |
0 | 3378 |
{ |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3379 |
/* First, handle broken down train */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3380 |
|
6476 | 3381 |
int t = v->breakdown_ctr; |
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
3382 |
if (t > 1) { |
0 | 3383 |
v->vehstatus |= VS_TRAIN_SLOWING; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3384 |
|
6476 | 3385 |
uint16 break_speed = _breakdown_speeds[GB(~t, 4, 4)]; |
2484
8e0c88a833fb
(svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents:
2482
diff
changeset
|
3386 |
if (break_speed < v->cur_speed) v->cur_speed = break_speed; |
0 | 3387 |
} else { |
3388 |
v->vehstatus &= ~VS_TRAIN_SLOWING; |
|
3389 |
} |
|
3390 |
||
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3391 |
if (!TrainCanLeaveTile(v)) return true; |
0 | 3392 |
|
22 | 3393 |
/* Determine the non-diagonal direction in which we will exit this tile */ |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3394 |
DiagDirection dir = TrainExitDir(v->direction, v->u.rail.track); |
22 | 3395 |
/* Calculate next tile */ |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3396 |
TileIndex tile = v->tile + TileOffsByDiagDir(dir); |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3397 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3398 |
/* Determine the track status on the next tile */ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3399 |
TrackStatus ts = GetTileTrackStatus(tile, TRANSPORT_RAIL, 0, ReverseDiagDir(dir)); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3400 |
TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(dir); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3401 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3402 |
TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3403 |
TrackdirBits red_signals = TrackStatusToRedSignals(ts) & reachable_trackdirs; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3404 |
|
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3405 |
/* We are sure the train is not entering a depot, it is detected above */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3406 |
|
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3407 |
/* mask unreachable track bits if we are forbidden to do 90deg turns */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3408 |
TrackBits bits = TrackdirBitsToTrackBits(trackdirbits); |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
3409 |
if (_settings_game.pf.pathfinder_for_trains != VPF_NTP && _settings_game.pf.forbid_90_deg) { |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3410 |
bits &= ~TrackCrossesTracks(FindFirstTrack(v->u.rail.track)); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3411 |
} |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3412 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3413 |
/* no suitable trackbits at all || unusable rail (wrong type or owner) */ |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3414 |
if (bits == TRACK_BIT_NONE || !CheckCompatibleRail(v, tile)) { |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3415 |
return TrainApproachingLineEnd(v, false); |
0 | 3416 |
} |
3417 |
||
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3418 |
/* approaching red signal */ |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3419 |
if ((trackdirbits & red_signals) != 0) return TrainApproachingLineEnd(v, true); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3420 |
|
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3421 |
/* approaching a rail/road crossing? then make it red */ |
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3422 |
if (IsLevelCrossingTile(tile)) MaybeBarCrossingWithSound(tile); |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3423 |
|
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3424 |
return true; |
0 | 3425 |
} |
3426 |
||
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3427 |
|
0 | 3428 |
static void TrainLocoHandler(Vehicle *v, bool mode) |
3429 |
{ |
|
3430 |
/* train has crashed? */ |
|
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3431 |
if (v->vehstatus & VS_CRASHED) { |
0 | 3432 |
if (!mode) HandleCrashedTrain(v); |
3433 |
return; |
|
3434 |
} |
|
3435 |
||
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3436 |
if (v->u.rail.force_proceed != 0) v->u.rail.force_proceed--; |
0 | 3437 |
|
3438 |
/* train is broken down? */ |
|
3439 |
if (v->breakdown_ctr != 0) { |
|
3440 |
if (v->breakdown_ctr <= 2) { |
|
3441 |
HandleBrokenTrain(v); |
|
3442 |
return; |
|
3443 |
} |
|
10274
b3c58f3df92b
(svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents:
10242
diff
changeset
|
3444 |
if (!v->current_order.IsType(OT_LOADING)) v->breakdown_ctr--; |
0 | 3445 |
} |
3446 |
||
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
3447 |
if (HasBit(v->u.rail.flags, VRF_REVERSING) && v->cur_speed == 0) { |
0 | 3448 |
ReverseTrainDirection(v); |
3449 |
} |
|
3450 |
||
3451 |
/* exit if train is stopped */ |
|
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3452 |
if (v->vehstatus & VS_STOPPED && v->cur_speed == 0) return; |
0 | 3453 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3454 |
if (ProcessOrders(v) && CheckReverseTrain(v)) { |
0 | 3455 |
v->load_unload_time_rem = 0; |
3456 |
v->cur_speed = 0; |
|
3457 |
v->subspeed = 0; |
|
3458 |
ReverseTrainDirection(v); |
|
3459 |
return; |
|
3460 |
} |
|
3461 |
||
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
3462 |
v->HandleLoading(mode); |
0 | 3463 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3464 |
if (v->current_order.IsType(OT_LOADING)) return; |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3465 |
|
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3466 |
if (CheckTrainStayInDepot(v)) return; |
0 | 3467 |
|
3468 |
if (!mode) HandleLocomotiveSmokeCloud(v); |
|
3469 |
||
6476 | 3470 |
int j = UpdateTrainSpeed(v); |
6877
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3471 |
|
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3472 |
/* we need to invalidate the widget if we are stopping from 'Stopping 0 km/h' to 'Stopped' */ |
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3473 |
if (v->cur_speed == 0 && v->u.rail.last_speed == 0 && v->vehstatus & VS_STOPPED) { |
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3474 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3475 |
} |
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3476 |
|
0 | 3477 |
if (j == 0) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
3478 |
/* if the vehicle has speed 0, update the last_speed field. */ |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3479 |
if (v->cur_speed != 0) return; |
0 | 3480 |
} else { |
3481 |
TrainCheckIfLineEnds(v); |
|
3482 |
||
3483 |
do { |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3484 |
TrainController(v, NULL, true); |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
3485 |
CheckTrainCollision(v); |
0 | 3486 |
if (v->cur_speed <= 0x100) |
3487 |
break; |
|
3488 |
} while (--j != 0); |
|
3489 |
} |
|
3490 |
||
3491 |
SetLastSpeed(v, v->cur_speed); |
|
3492 |
} |
|
3493 |
||
3494 |
||
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3495 |
|
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3496 |
Money Train::GetRunningCost() const |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3497 |
{ |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3498 |
Money cost = 0; |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3499 |
const Vehicle *v = this; |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3500 |
|
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3501 |
do { |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3502 |
const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3503 |
|
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3504 |
byte cost_factor = GetVehicleProperty(v, 0x0D, rvi->running_cost); |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3505 |
if (cost_factor == 0) continue; |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3506 |
|
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
3507 |
/* Halve running cost for multiheaded parts */ |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
3508 |
if (IsMultiheaded(v)) cost_factor /= 2; |
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
3509 |
|
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3510 |
cost += cost_factor * GetPriceByIndex(rvi->running_cost_class); |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3511 |
} while ((v = GetNextVehicle(v)) != NULL); |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3512 |
|
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3513 |
return cost; |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3514 |
} |
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3515 |
|
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3516 |
|
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3517 |
void Train::Tick() |
0 | 3518 |
{ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3519 |
if (_age_cargo_skip_counter == 0) this->cargo.AgeCargo(); |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3520 |
|
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3521 |
this->tick_counter++; |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3522 |
|
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3523 |
if (IsFrontEngine(this)) { |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3524 |
if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++; |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3525 |
this->current_order_time++; |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3526 |
|
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3527 |
TrainLocoHandler(this, false); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3528 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
3529 |
/* make sure vehicle wasn't deleted. */ |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3530 |
if (this->type == VEH_TRAIN && IsFrontEngine(this)) |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3531 |
TrainLocoHandler(this, true); |
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3532 |
} else if (IsFreeWagon(this) && HASBITS(this->vehstatus, VS_CRASHED)) { |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
3533 |
/* Delete flooded standalone wagon chain */ |
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
3534 |
if (++this->u.rail.crash_anim_pos >= 4400) DeleteVehicleChain(this); |
0 | 3535 |
} |
3536 |
} |
|
3537 |
||
3538 |
static void CheckIfTrainNeedsService(Vehicle *v) |
|
3539 |
{ |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3540 |
static const uint MAX_ACCEPTABLE_DEPOT_DIST = 16; |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3541 |
|
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
3542 |
if (_settings_game.vehicle.servint_trains == 0 || !v->NeedsAutomaticServicing()) return; |
6868
7eb395287b3d
(svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents:
6800
diff
changeset
|
3543 |
if (v->IsInDepot()) { |
4529
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
3544 |
VehicleServiceInDepot(v); |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
3545 |
return; |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
3546 |
} |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
3547 |
|
6476 | 3548 |
TrainFindDepotData tfdd = FindClosestTrainDepot(v, MAX_ACCEPTABLE_DEPOT_DIST); |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3549 |
/* Only go to the depot if it is not too far out of our way. */ |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
3550 |
if (tfdd.best_length == (uint)-1 || tfdd.best_length > MAX_ACCEPTABLE_DEPOT_DIST) { |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3551 |
if (v->current_order.IsType(OT_GOTO_DEPOT)) { |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3552 |
/* If we were already heading for a depot but it has |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3553 |
* suddenly moved farther away, we continue our normal |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3554 |
* schedule? */ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3555 |
v->current_order.MakeDummy(); |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3556 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
0 | 3557 |
} |
3558 |
return; |
|
3559 |
} |
|
3560 |
||
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3561 |
const Depot *depot = GetDepotByTile(tfdd.tile); |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3562 |
|
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3563 |
if (v->current_order.IsType(OT_GOTO_DEPOT) && |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3564 |
v->current_order.GetDestination() != depot->index && |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
3565 |
!Chance16(3, 16)) { |
0 | 3566 |
return; |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3567 |
} |
0 | 3568 |
|
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3569 |
v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE); |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3570 |
v->dest_tile = tfdd.tile; |
6872
1c4a4a609f85
(svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents:
6871
diff
changeset
|
3571 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH); |
0 | 3572 |
} |
3573 |
||
6877
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3574 |
void Train::OnNewDay() |
0 | 3575 |
{ |
6877
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3576 |
if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this); |
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3577 |
|
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3578 |
if (IsFrontEngine(this)) { |
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3579 |
CheckVehicleBreakdown(this); |
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3580 |
AgeVehicle(this); |
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3581 |
|
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3582 |
CheckIfTrainNeedsService(this); |
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3583 |
|
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3584 |
CheckOrders(this); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3585 |
|
0 | 3586 |
/* update destination */ |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3587 |
if (this->current_order.IsType(OT_GOTO_STATION)) { |
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3588 |
TileIndex tile = GetStation(this->current_order.GetDestination())->train_tile; |
6877
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3589 |
if (tile != 0) this->dest_tile = tile; |
2639 | 3590 |
} |
0 | 3591 |
|
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3592 |
if (this->running_ticks != 0) { |
0 | 3593 |
/* running costs */ |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3594 |
CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() * this->running_ticks / (364 * DAY_TICKS)); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3595 |
|
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3596 |
this->profit_this_year -= cost.GetCost(); |
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3597 |
this->running_ticks = 0; |
6877
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3598 |
|
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3599 |
SubtractMoneyFromPlayerFract(this->owner, cost); |
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3600 |
|
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3601 |
InvalidateWindow(WC_VEHICLE_DETAILS, this->index); |
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
3602 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 3603 |
} |
6877
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3604 |
} else if (IsTrainEngine(this)) { |
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6719
diff
changeset
|
3605 |
/* Also age engines that aren't front engines */ |
6877
889301acc299
(svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents:
6872
diff
changeset
|
3606 |
AgeVehicle(this); |
0 | 3607 |
} |
3608 |
} |
|
3609 |
||
6573 | 3610 |
void TrainsYearlyLoop() |
0 | 3611 |
{ |
3612 |
Vehicle *v; |
|
3613 |
||
3614 |
FOR_ALL_VEHICLES(v) { |
|
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
3615 |
if (v->type == VEH_TRAIN && IsFrontEngine(v)) { |
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
3616 |
/* show warning if train is not generating enough income last 2 years (corresponds to a red icon in the vehicle list) */ |
10991
d8811e327d12
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents:
10731
diff
changeset
|
3617 |
if (_settings_client.gui.train_income_warn && v->owner == _local_player && v->age >= 730 && v->GetDisplayProfitThisYear() < 0) { |
6878
7d1ff2f621c7
(svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents:
6877
diff
changeset
|
3618 |
SetDParam(1, v->GetDisplayProfitThisYear()); |
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
3619 |
SetDParam(0, v->unitnumber); |
0 | 3620 |
AddNewsItem( |
3621 |
STR_TRAIN_IS_UNPROFITABLE, |
|
10724
68a692eacf22
(svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents:
10274
diff
changeset
|
3622 |
NS_ADVICE, |
0 | 3623 |
v->index, |
3624 |
0); |
|
3625 |
} |
|
3626 |
||
3627 |
v->profit_last_year = v->profit_this_year; |
|
3628 |
v->profit_this_year = 0; |
|
3629 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
3630 |
} |
|
3631 |
} |
|
3632 |
} |
|
3633 |
||
3634 |
||
6573 | 3635 |
void InitializeTrains() |
0 | 3636 |
{ |
3637 |
_age_cargo_skip_counter = 1; |
|
3638 |
} |
|
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3639 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3640 |
/* |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3641 |
* Link front and rear multiheaded engines to each other |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3642 |
* This is done when loading a savegame |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3643 |
*/ |
6573 | 3644 |
void ConnectMultiheadedTrains() |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3645 |
{ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3646 |
Vehicle *v; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3647 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3648 |
FOR_ALL_VEHICLES(v) { |
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
3649 |
if (v->type == VEH_TRAIN) { |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3650 |
v->u.rail.other_multiheaded_part = NULL; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3651 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3652 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3653 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3654 |
FOR_ALL_VEHICLES(v) { |
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
3655 |
if (v->type == VEH_TRAIN && IsFrontEngine(v)) { |
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
3656 |
for (Vehicle *u = v; u != NULL; u = u->Next()) { |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3657 |
if (u->u.rail.other_multiheaded_part != NULL) continue; // we already linked this one |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3658 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3659 |
if (IsMultiheaded(u)) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3660 |
if (!IsTrainEngine(u)) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3661 |
/* we got a rear car without a front car. We will convert it to a front one */ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3662 |
SetTrainEngine(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3663 |
u->spritenum--; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3664 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3665 |
|
6476 | 3666 |
Vehicle *w; |
10184
fcf5fb2548eb
(svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents:
6878
diff
changeset
|
3667 |
for (w = u->Next(); w != NULL && (w->engine_type != u->engine_type || w->u.rail.other_multiheaded_part != NULL); w = GetNextVehicle(w)) {} |
6476 | 3668 |
if (w != NULL) { |
3669 |
/* we found a car to partner with this engine. Now we will make sure it face the right way */ |
|
3670 |
if (IsTrainEngine(w)) { |
|
3671 |
ClearTrainEngine(w); |
|
3672 |
w->spritenum++; |
|
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3673 |
} |
6476 | 3674 |
w->u.rail.other_multiheaded_part = u; |
3675 |
u->u.rail.other_multiheaded_part = w; |
|
3676 |
} else { |
|
3677 |
/* we got a front car and no rear cars. We will fake this one for forget that it should have been multiheaded */ |
|
3678 |
ClearMultiheaded(u); |
|
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3679 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3680 |
} |
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
3681 |
} |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3682 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3683 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3684 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3685 |
|
6719
4cc327ad39d5
(svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents:
6683
diff
changeset
|
3686 |
/** |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3687 |
* Converts all trains to the new subtype format introduced in savegame 16.2 |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3688 |
* It also links multiheaded engines or make them forget they are multiheaded if no suitable partner is found |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3689 |
*/ |
6573 | 3690 |
void ConvertOldMultiheadToNew() |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3691 |
{ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3692 |
Vehicle *v; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3693 |
FOR_ALL_VEHICLES(v) { |
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
3694 |
if (v->type == VEH_TRAIN) { |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
3695 |
SetBit(v->subtype, 7); // indicates that it's the old format and needs to be converted in the next loop |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3696 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3697 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3698 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3699 |
FOR_ALL_VEHICLES(v) { |
6585
7da94b26498a
(svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents:
6574
diff
changeset
|
3700 |
if (v->type == VEH_TRAIN) { |
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
3701 |
if (HasBit(v->subtype, 7) && ((v->subtype & ~0x80) == 0 || (v->subtype & ~0x80) == 4)) { |
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
3702 |
for (Vehicle *u = v; u != NULL; u = u->Next()) { |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3703 |
const RailVehicleInfo *rvi = RailVehInfo(u->engine_type); |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3704 |
|
6871
5a9dc001e1ad
(svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents:
6870
diff
changeset
|
3705 |
ClrBit(u->subtype, 7); |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3706 |
switch (u->subtype) { |
4434
4175805666a5
(svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents:
4432
diff
changeset
|
3707 |
case 0: /* TS_Front_Engine */ |
6119
b47985557d1e
(svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents:
6109
diff
changeset
|
3708 |
if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(u); |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3709 |
SetFrontEngine(u); |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3710 |
SetTrainEngine(u); |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3711 |
break; |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3712 |
|
4434
4175805666a5
(svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents:
4432
diff
changeset
|
3713 |
case 1: /* TS_Artic_Part */ |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3714 |
u->subtype = 0; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3715 |
SetArticulatedPart(u); |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3716 |
break; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3717 |
|
4434
4175805666a5
(svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents:
4432
diff
changeset
|
3718 |
case 2: /* TS_Not_First */ |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3719 |
u->subtype = 0; |
6119
b47985557d1e
(svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents:
6109
diff
changeset
|
3720 |
if (rvi->railveh_type == RAILVEH_WAGON) { |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3721 |
// normal wagon |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3722 |
SetTrainWagon(u); |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3723 |
break; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3724 |
} |
6119
b47985557d1e
(svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents:
6109
diff
changeset
|
3725 |
if (rvi->railveh_type == RAILVEH_MULTIHEAD && rvi->image_index == u->spritenum - 1) { |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3726 |
// rear end of a multiheaded engine |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3727 |
SetMultiheaded(u); |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3728 |
break; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3729 |
} |
6119
b47985557d1e
(svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents:
6109
diff
changeset
|
3730 |
if (rvi->railveh_type == RAILVEH_MULTIHEAD) SetMultiheaded(u); |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3731 |
SetTrainEngine(u); |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3732 |
break; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3733 |
|
4434
4175805666a5
(svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents:
4432
diff
changeset
|
3734 |
case 4: /* TS_Free_Car */ |
3017
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3735 |
u->subtype = 0; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3736 |
SetTrainWagon(u); |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3737 |
SetFreeWagon(u); |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3738 |
break; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3739 |
default: NOT_REACHED(); break; |
915fae59d5e0
(svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents:
3008
diff
changeset
|
3740 |
} |
10731
67db0d431d5e
(svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents:
10724
diff
changeset
|
3741 |
} |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3742 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3743 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3744 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3745 |
} |