author | peter1138 |
Fri, 04 May 2007 19:24:01 +0000 | |
changeset 7068 | 846986cf3a6e |
parent 7061 | 81d3a68972b5 |
child 7069 | 9c50360195ff |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
3 |
/** @file train_cmd.cpp */ |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
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" |
2163
637ec3c361f5
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2150
diff
changeset
|
9 |
#include "functions.h" |
6949
72d11a1e1e60
(svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents:
6918
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" |
507
8aa8100b0b22
(svn r815) Include strings.h only in the files which need it.
tron
parents:
495
diff
changeset
|
13 |
#include "table/strings.h" |
679
e959706a3e4d
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
593
diff
changeset
|
14 |
#include "map.h" |
1209
a1ac96655b79
(svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents:
1208
diff
changeset
|
15 |
#include "tile.h" |
3154 | 16 |
#include "tunnel_map.h" |
0 | 17 |
#include "vehicle.h" |
18 |
#include "command.h" |
|
19 |
#include "pathfind.h" |
|
1247 | 20 |
#include "npf.h" |
0 | 21 |
#include "station.h" |
22 |
#include "table/train_cmd.h" |
|
23 |
#include "news.h" |
|
24 |
#include "engine.h" |
|
25 |
#include "player.h" |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
26 |
#include "sound.h" |
1313
bba6afb8a995
(svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents:
1282
diff
changeset
|
27 |
#include "depot.h" |
1542
2ca6d1624e6d
(svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents:
1531
diff
changeset
|
28 |
#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
|
29 |
#include "vehicle_gui.h" |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
30 |
#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
|
31 |
#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
|
32 |
#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
|
33 |
#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
|
34 |
#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
|
35 |
#include "newgrf_text.h" |
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
36 |
#include "direction.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
|
37 |
#include "yapf/yapf.h" |
4261
2ec8f5a9747b
(svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents:
4252
diff
changeset
|
38 |
#include "date.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
|
39 |
#include "cargotype.h" |
0 | 40 |
|
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
41 |
static bool TrainCheckIfLineEnds(Vehicle *v); |
5252
6823da581c30
(svn r7378) -Fix (r2428): Don't update vehicle images when turning a train around.
peter1138
parents:
5215
diff
changeset
|
42 |
static void TrainController(Vehicle *v, bool update_image); |
0 | 43 |
|
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
|
44 |
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
|
45 |
static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10}; |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
46 |
static const TrackBits _state_dir_table[4] = { TRACK_BIT_RIGHT, TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER }; |
0 | 47 |
|
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
|
48 |
|
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
|
49 |
/** 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
|
50 |
* @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
|
51 |
* @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
|
52 |
*/ |
5316
b04421921eae
(svn r7473) -Fix (r7269): Pass a cargo type to determine the freight weight
peter1138
parents:
5303
diff
changeset
|
53 |
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
|
54 |
{ |
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
|
55 |
if (!GetCargo(cargo)->is_freight) return 1; |
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
|
56 |
return _patches.freight_trains; |
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
|
57 |
} |
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
|
58 |
|
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
|
59 |
|
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
|
60 |
/** |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
61 |
* 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
|
62 |
* @param v First vehicle of the consist. |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
63 |
*/ |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
64 |
void TrainPowerChanged(Vehicle* v) |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
65 |
{ |
6986
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
66 |
uint32 total_power = 0; |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
67 |
uint32 max_te = 0; |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
68 |
|
6476 | 69 |
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
|
70 |
/* Power is not added for articulated parts */ |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
71 |
if (IsArticulatedPart(u)) continue; |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
72 |
|
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
|
73 |
RailType railtype = GetRailType(u->tile); |
6020 | 74 |
bool engine_has_power = HasPowerOnRail(u->u.rail.railtype, railtype); |
75 |
bool wagon_has_power = HasPowerOnRail(v->u.rail.railtype, railtype); |
|
76 |
||
77 |
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
|
78 |
|
6986
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
79 |
if (engine_has_power) { |
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
80 |
uint16 power = GetVehicleProperty(u, 0x0B, rvi_u->power); |
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
81 |
if (power != 0) { |
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
82 |
total_power += power; |
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
83 |
/* Tractive effort in (tonnes * 1000 * 10 =) N */ |
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
84 |
max_te += (u->u.rail.cached_veh_weight * 10000 * GetVehicleProperty(u, 0x1F, rvi_u->tractive_effort)) / 256; |
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
85 |
} |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
86 |
} |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
87 |
|
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
88 |
if (HASBIT(u->u.rail.flags, VRF_POWEREDWAGON) && (wagon_has_power)) { |
6986
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
89 |
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
|
90 |
} |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
91 |
} |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
92 |
|
6986
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
93 |
if (v->u.rail.cached_power != total_power || v->u.rail.cached_max_te != max_te) { |
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
94 |
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
|
95 |
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
|
96 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
97 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
98 |
} |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
99 |
} |
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
100 |
|
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
101 |
|
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
102 |
/** |
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
|
103 |
* 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
|
104 |
* 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
|
105 |
* @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
|
106 |
*/ |
2752 | 107 |
static void TrainCargoChanged(Vehicle* v) |
2639 | 108 |
{ |
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
|
109 |
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
|
110 |
|
6476 | 111 |
for (Vehicle *u = v; u != NULL; u = u->next) { |
112 |
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
|
113 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
114 |
/* 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
|
115 |
if (!IsArticulatedPart(u)) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
116 |
/* vehicle weight is the sum of the weight of the vehicle and the weight of its cargo */ |
7068
846986cf3a6e
(svn r9780) -Codechange: [NewGRF] Add support for variable vehicle weight for trains (callback 36, property 16)
peter1138
parents:
7061
diff
changeset
|
117 |
vweight += GetVehicleProperty(u, 0x16, RailVehInfo(u->engine_type)->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
|
118 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
119 |
/* powered wagons have extra weight added */ |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
120 |
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
|
121 |
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
|
122 |
} |
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
|
123 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
124 |
/* 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
|
125 |
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
|
126 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
127 |
/* 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
|
128 |
u->u.rail.cached_veh_weight = vweight; |
6476 | 129 |
} |
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
|
130 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
131 |
/* 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
|
132 |
v->u.rail.cached_weight = weight; |
5588
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 |
/* 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
|
135 |
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
|
136 |
} |
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
|
137 |
|
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
|
138 |
|
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
|
139 |
/** |
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
|
140 |
* 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
|
141 |
* 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
|
142 |
* 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
|
143 |
* @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
|
144 |
*/ |
2639 | 145 |
void TrainConsistChanged(Vehicle* v) |
146 |
{ |
|
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
|
147 |
uint16 max_speed = 0xFFFF; |
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
|
148 |
|
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
|
149 |
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
|
150 |
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
|
151 |
|
6476 | 152 |
const RailVehicleInfo *rvi_v = RailVehInfo(v->engine_type); |
153 |
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
|
154 |
v->u.rail.cached_total_length = 0; |
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
|
155 |
v->u.rail.compatible_railtypes = 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
|
156 |
|
6476 | 157 |
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
|
158 |
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
|
159 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
160 |
/* Update the v->first cache. This is faster than having to brute force it later. */ |
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
|
161 |
if (u->first == NULL) u->first = v; |
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
|
162 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
163 |
/* update the 'first engine' */ |
6476 | 164 |
u->u.rail.first_engine = v == u ? INVALID_ENGINE : first_engine; |
6074 | 165 |
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
|
166 |
|
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
|
167 |
if (IsTrainEngine(u)) first_engine = u->engine_type; |
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
|
168 |
|
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
169 |
if (rvi_u->visual_effect != 0) { |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
170 |
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
|
171 |
} else { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
172 |
if (IsTrainWagon(u) || IsArticulatedPart(u)) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
173 |
/* 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
|
174 |
u->u.rail.cached_vis_effect = 0x40; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
175 |
} else if (rvi_u->engclass == 0) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
176 |
/* Steam is offset by -4 units */ |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
177 |
u->u.rail.cached_vis_effect = 4; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
178 |
} else { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
179 |
/* 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
|
180 |
u->u.rail.cached_vis_effect = 8; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
181 |
} |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
182 |
} |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
183 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
184 |
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
|
185 |
/* Check powered wagon / visual effect callback */ |
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
|
186 |
if (HASBIT(EngInfo(u->engine_type)->callbackmask, CBM_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
|
187 |
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
|
188 |
|
eb6d2e32fc79
(svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents:
3900
diff
changeset
|
189 |
if (callback != CALLBACK_FAILED) u->u.rail.cached_vis_effect = callback; |
eb6d2e32fc79
(svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents:
3900
diff
changeset
|
190 |
} |
eb6d2e32fc79
(svn r5061) - NewGRF: always use visual effect callback if it is defined, not just for powered wagons.
peter1138
parents:
3900
diff
changeset
|
191 |
|
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
|
192 |
if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RAILVEH_WAGON && |
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
|
193 |
UsesWagonOverride(u) && (u->u.rail.cached_vis_effect < 0x40)) { |
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
|
194 |
/* wagon is powered */ |
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
|
195 |
SETBIT(u->u.rail.flags, VRF_POWEREDWAGON); // cache 'powered' status |
6476 | 196 |
} else { |
197 |
CLRBIT(u->u.rail.flags, VRF_POWEREDWAGON); |
|
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
198 |
} |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
199 |
|
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
|
200 |
/* 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
|
201 |
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
|
202 |
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
|
203 |
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
|
204 |
} |
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
|
205 |
|
5116
2a33a74925c5
(svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents:
4996
diff
changeset
|
206 |
/* 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
|
207 |
* existing electric engines when elrails are disabled and then re-enabled */ |
2a33a74925c5
(svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents:
4996
diff
changeset
|
208 |
if (HASBIT(u->u.rail.flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL)) { |
2a33a74925c5
(svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents:
4996
diff
changeset
|
209 |
u->u.rail.railtype = RAILTYPE_RAIL; |
2a33a74925c5
(svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents:
4996
diff
changeset
|
210 |
u->u.rail.compatible_railtypes |= (1 << RAILTYPE_RAIL); |
2a33a74925c5
(svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents:
4996
diff
changeset
|
211 |
} |
2a33a74925c5
(svn r7195) -Feature: [FS#297, optional elrails] New patches/vehicles option 'disable electrified railways'.
KUDr
parents:
4996
diff
changeset
|
212 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
213 |
/* max speed is the minimum of the speed limits of all vehicles in the consist */ |
6986
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
214 |
if ((rvi_u->railveh_type != RAILVEH_WAGON || _patches.wagon_speed_limits) && !UsesWagonOverride(u)) { |
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
215 |
uint16 speed = GetVehicleProperty(u, 0x09, rvi_u->max_speed); |
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
216 |
if (speed != 0) max_speed = min(speed, max_speed); |
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
217 |
} |
1908
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
218 |
} |
bc48a30aba72
(svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents:
1905
diff
changeset
|
219 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
220 |
/* check the vehicle length (callback) */ |
6476 | 221 |
uint16 veh_len = CALLBACK_FAILED; |
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
|
222 |
if (HASBIT(EngInfo(u->engine_type)->callbackmask, CBM_VEHICLE_LENGTH)) { |
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
|
223 |
veh_len = GetVehicleCallback(CBID_TRAIN_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
|
224 |
} |
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
|
225 |
if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor; |
1934
b3d568113d4d
(svn r2440) - Fix: [newgrf] Fix the spritesorter to handle overlapping sprites properly, this fixes display problems with really short wagons. (algorithm by patchman, ported by therax)
hackykid
parents:
1927
diff
changeset
|
226 |
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
|
227 |
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
|
228 |
v->u.rail.cached_total_length += u->u.rail.cached_veh_length; |
6476 | 229 |
} |
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
|
230 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
231 |
/* 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
|
232 |
v->u.rail.cached_max_speed = max_speed; |
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
|
233 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
234 |
/* 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
|
235 |
TrainCargoChanged(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
|
236 |
} |
1247 | 237 |
|
238 |
/* These two arrays are used for realistic acceleration. XXX: How should they |
|
239 |
* be interpreted? */ |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
240 |
static const byte _curve_neighbours45[8][2] = { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
241 |
{7, 1}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
242 |
{0, 2}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
243 |
{1, 3}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
244 |
{2, 4}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
245 |
{3, 5}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
246 |
{4, 6}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
247 |
{5, 7}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
248 |
{6, 0}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
249 |
}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
250 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
251 |
static const byte _curve_neighbours90[8][2] = { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
252 |
{6, 2}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
253 |
{7, 3}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
254 |
{0, 4}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
255 |
{1, 5}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
256 |
{2, 6}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
257 |
{3, 7}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
258 |
{4, 0}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
259 |
{5, 1}, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
260 |
}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
261 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
262 |
enum AccelType { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
263 |
AM_ACCEL, |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
264 |
AM_BRAKE |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
265 |
}; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
266 |
|
2630 | 267 |
static bool TrainShouldStop(const Vehicle* v, TileIndex tile) |
1236
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
268 |
{ |
2630 | 269 |
const Order* o = &v->current_order; |
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
270 |
StationID sid = GetStationIndex(tile); |
2630 | 271 |
|
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
|
272 |
assert(v->type == VEH_TRAIN); |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
273 |
/* When does a train drive through a station |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
274 |
* first we deal with the "new nonstop handling" */ |
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
275 |
if (_patches.new_nonstop && o->flags & OF_NON_STOP && sid == o->dest) { |
1236
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
276 |
return false; |
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
277 |
} |
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
278 |
|
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
279 |
if (v->last_station_visited == sid) return false; |
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
280 |
|
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
281 |
if (sid != o->dest && (o->flags & OF_NON_STOP || _patches.new_nonstop)) { |
1236
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
282 |
return false; |
3315
1f65f8260092
(svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents:
3269
diff
changeset
|
283 |
} |
1236
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
284 |
|
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
285 |
return true; |
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
286 |
} |
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
287 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
288 |
/** new acceleration*/ |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
289 |
static int GetTrainAcceleration(Vehicle *v, bool mode) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
290 |
{ |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
291 |
int max_speed = 2000; |
6476 | 292 |
int speed = v->cur_speed * 10 / 16; //[mph] |
293 |
int curvecount[2] = {0, 0}; |
|
294 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
295 |
/*first find the curve speed limit */ |
6476 | 296 |
int numcurve = 0; |
297 |
int sum = 0; |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
298 |
int pos = 0; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
299 |
int lastpos = -1; |
6476 | 300 |
for (const Vehicle *u = v; u->next != NULL; u = u->next, pos++) { |
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3154
diff
changeset
|
301 |
Direction dir = u->direction; |
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3154
diff
changeset
|
302 |
Direction ndir = u->next->direction; |
1531 | 303 |
int i; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
304 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
305 |
for (i = 0; i < 2; i++) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
306 |
if ( _curve_neighbours45[dir][i] == ndir) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
307 |
curvecount[i]++; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
308 |
if (lastpos != -1) { |
1531 | 309 |
numcurve++; |
310 |
sum += pos - lastpos; |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
311 |
if (pos - lastpos == 1) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
312 |
max_speed = 88; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
313 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
314 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
315 |
lastpos = pos; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
316 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
317 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
318 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
319 |
/*if we have a 90 degree turn, fix the speed limit to 60 */ |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
320 |
if (_curve_neighbours90[dir][0] == ndir || |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
321 |
_curve_neighbours90[dir][1] == ndir) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
322 |
max_speed = 61; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
323 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
324 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
325 |
|
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
326 |
if (numcurve > 0) sum /= numcurve; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
327 |
|
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
328 |
if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
329 |
int total = curvecount[0] + curvecount[1]; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
330 |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
331 |
if (curvecount[0] == 1 && curvecount[1] == 1) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
332 |
max_speed = 0xFFFF; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
333 |
} else if (total > 1) { |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
334 |
max_speed = 232 - (13 - clamp(sum, 1, 12)) * (13 - clamp(sum, 1, 12)); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
335 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
336 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
337 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
338 |
max_speed += (max_speed / 2) * v->u.rail.railtype; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
339 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
340 |
if (IsTileType(v->tile, MP_STATION) && IsFrontEngine(v)) { |
1236
394a1b3d6f3e
(svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents:
1235
diff
changeset
|
341 |
if (TrainShouldStop(v, 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
|
342 |
int station_length = GetStationByTile(v->tile)->GetPlatformLength(v->tile, DirToDiagDir(v->direction)); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
343 |
int delta_v; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
344 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
345 |
max_speed = 120; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
346 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
347 |
delta_v = v->cur_speed / (station_length + 1); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
348 |
if (v->max_speed > (v->cur_speed - delta_v)) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
349 |
max_speed = v->cur_speed - (delta_v / 10); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
350 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
351 |
max_speed = max(max_speed, 25 * station_length); |
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 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
354 |
|
6476 | 355 |
int mass = v->u.rail.cached_weight; |
356 |
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
|
357 |
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
|
358 |
|
6476 | 359 |
int num = 0; //number of vehicles, change this into the number of axles later |
360 |
int incl = 0; |
|
361 |
int drag_coeff = 20; //[1e-4] |
|
362 |
for (const Vehicle *u = v; u != NULL; u = u->next) { |
|
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
363 |
num++; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
364 |
drag_coeff += 3; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
365 |
|
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
366 |
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
|
367 |
|
1683
2248e6615517
(svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents:
1681
diff
changeset
|
368 |
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
|
369 |
incl += u->u.rail.cached_veh_weight * 60; //3% slope, quite a bit actually |
1683
2248e6615517
(svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents:
1681
diff
changeset
|
370 |
} 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
|
371 |
incl -= u->u.rail.cached_veh_weight * 60; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
372 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
373 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
374 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
375 |
v->max_speed = max_speed; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
376 |
|
6476 | 377 |
const int area = 120; |
378 |
const int friction = 35; //[1e-3] |
|
379 |
int resistance; |
|
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
380 |
if (v->u.rail.railtype != RAILTYPE_MAGLEV) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
381 |
resistance = 13 * mass / 10; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
382 |
resistance += 60 * num; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
383 |
resistance += friction * mass * speed / 1000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
384 |
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
|
385 |
} else { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
386 |
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
|
387 |
} |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
388 |
resistance += incl; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
389 |
resistance *= 4; //[N] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
390 |
|
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
|
391 |
/* 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
|
392 |
* actually double the train's power */ |
6476 | 393 |
const int max_te = v->u.rail.cached_max_te; // [N] |
394 |
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
|
395 |
if (speed > 2) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
396 |
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
|
397 |
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
|
398 |
case RAILTYPE_ELECTRIC: |
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
399 |
case RAILTYPE_MONO: |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
400 |
force = power / speed; //[N] |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
401 |
force *= 22; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
402 |
force /= 10; |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
403 |
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
|
404 |
break; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
405 |
|
6476 | 406 |
default: NOT_REACHED(); |
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
407 |
case RAILTYPE_MAGLEV: |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
408 |
force = power / 25; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
409 |
break; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
410 |
} |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
411 |
} else { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
412 |
/* "kickoff" acceleration */ |
5588
1bcb6b4c01d8
(svn r7592) -Feature: Add support for tractive effort to 'realistic' acceleration.
peter1138
parents:
5573
diff
changeset
|
413 |
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
|
414 |
force = max(force, (mass * 8) + resistance); |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
415 |
} |
1179
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 |
if (force <= 0) force = 10000; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
418 |
|
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
419 |
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
|
420 |
|
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
421 |
if (mode == AM_ACCEL) { |
1684
d09bc0beec4c
(svn r2188) Revert r2185, because trains always accerlated (Requested by Celestar)
tron
parents:
1683
diff
changeset
|
422 |
return (force - resistance) / (mass * 4); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
423 |
} else { |
5279
658a16e02195
(svn r7425) -Fix (r1681): With realistic acceleration, guarantee a minimum braking
peter1138
parents:
5277
diff
changeset
|
424 |
return min((-force - resistance) / (mass * 4), -10000 / (mass * 4)); |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
425 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
426 |
} |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
427 |
|
2817 | 428 |
static void UpdateTrainAcceleration(Vehicle* v) |
0 | 429 |
{ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
430 |
assert(IsFrontEngine(v)); |
0 | 431 |
|
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
|
432 |
v->max_speed = v->u.rail.cached_max_speed; |
0 | 433 |
|
6476 | 434 |
uint power = v->u.rail.cached_power; |
435 |
uint weight = v->u.rail.cached_weight; |
|
0 | 436 |
assert(weight != 0); |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
437 |
v->acceleration = clamp(power / weight * 4, 1, 255); |
0 | 438 |
} |
439 |
||
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3154
diff
changeset
|
440 |
int GetTrainImage(const Vehicle* v, Direction direction) |
0 | 441 |
{ |
442 |
int img = v->spritenum; |
|
443 |
int base; |
|
444 |
||
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
445 |
if (HASBIT(v->u.rail.flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction); |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
446 |
|
0 | 447 |
if (is_custom_sprite(img)) { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
448 |
base = GetCustomVehicleSprite(v, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img))); |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
449 |
if (base != 0) return base; |
2464
3dee59d241c0
(svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents:
2463
diff
changeset
|
450 |
img = orig_rail_vehicle_info[v->engine_type].image_index; |
0 | 451 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
452 |
|
0 | 453 |
base = _engine_sprite_base[img] + ((direction + _engine_sprite_add[img]) & _engine_sprite_and[img]); |
454 |
||
2639 | 455 |
if (v->cargo_count >= v->cargo_cap / 2) base += _wagon_full_adder[img]; |
0 | 456 |
return base; |
457 |
} |
|
458 |
||
5919
2b58160d667d
(svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents:
5902
diff
changeset
|
459 |
void DrawTrainEngine(int x, int y, EngineID engine, SpriteID pal) |
0 | 460 |
{ |
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
461 |
const RailVehicleInfo *rvi = RailVehInfo(engine); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
462 |
|
0 | 463 |
int img = rvi->image_index; |
5919
2b58160d667d
(svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents:
5902
diff
changeset
|
464 |
SpriteID image = 0; |
0 | 465 |
|
466 |
if (is_custom_sprite(img)) { |
|
3186
5798593c2d95
(svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents:
3185
diff
changeset
|
467 |
image = GetCustomVehicleIcon(engine, DIR_W); |
2570
c7c4d8438516
(svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents:
2564
diff
changeset
|
468 |
if (image == 0) { |
c7c4d8438516
(svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents:
2564
diff
changeset
|
469 |
img = orig_rail_vehicle_info[engine].image_index; |
c7c4d8438516
(svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents:
2564
diff
changeset
|
470 |
} else { |
c7c4d8438516
(svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents:
2564
diff
changeset
|
471 |
y += _traininfo_vehicle_pitch; |
c7c4d8438516
(svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents:
2564
diff
changeset
|
472 |
} |
0 | 473 |
} |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
474 |
if (image == 0) { |
0 | 475 |
image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img]; |
476 |
} |
|
477 |
||
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
|
478 |
if (rvi->railveh_type == RAILVEH_MULTIHEAD) { |
5919
2b58160d667d
(svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents:
5902
diff
changeset
|
479 |
DrawSprite(image, pal, x - 14, y); |
0 | 480 |
x += 15; |
481 |
image = 0; |
|
482 |
if (is_custom_sprite(img)) { |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
483 |
image = GetCustomVehicleIcon(engine, DIR_E); |
2464
3dee59d241c0
(svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents:
2463
diff
changeset
|
484 |
if (image == 0) img = orig_rail_vehicle_info[engine].image_index; |
0 | 485 |
} |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
486 |
if (image == 0) { |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
487 |
image = |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
488 |
((6 + _engine_sprite_add[img + 1]) & _engine_sprite_and[img + 1]) + |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
489 |
_engine_sprite_base[img + 1]; |
0 | 490 |
} |
491 |
} |
|
5919
2b58160d667d
(svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents:
5902
diff
changeset
|
492 |
DrawSprite(image, pal, x, y); |
0 | 493 |
} |
494 |
||
4737
e38af7736681
(svn r6649) - Codechange: Show more correct capacity of articulated wagons in the train purchase list.
peter1138
parents:
4725
diff
changeset
|
495 |
uint CountArticulatedParts(EngineID engine_type) |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
496 |
{ |
6476 | 497 |
if (!HASBIT(EngInfo(engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return 0; |
498 |
||
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
499 |
uint i; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
500 |
for (i = 1; i < 10; i++) { |
6476 | 501 |
uint16 callback = GetVehicleCallback(CBID_TRAIN_ARTIC_ENGINE, i, 0, engine_type, NULL); |
2639 | 502 |
if (callback == CALLBACK_FAILED || callback == 0xFF) break; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
503 |
} |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
504 |
|
2608
f7f41be75b48
(svn r3145) Fix two off-by-one errors in building articulated rail vehicles.
peter1138
parents:
2607
diff
changeset
|
505 |
return i - 1; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
506 |
} |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
507 |
|
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
|
508 |
static void AddArticulatedParts(Vehicle **vl) |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
509 |
{ |
6476 | 510 |
const Vehicle *v = vl[0]; |
511 |
Vehicle *u = 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
|
512 |
|
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
|
513 |
if (!HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_ARTIC_ENGINE)) return; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
514 |
|
6476 | 515 |
for (uint i = 1; i < 10; i++) { |
516 |
uint16 callback = GetVehicleCallback(CBID_TRAIN_ARTIC_ENGINE, i, 0, v->engine_type, v); |
|
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
|
517 |
if (callback == CALLBACK_FAILED || callback == 0xFF) return; |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
518 |
|
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
|
519 |
/* Attempt to use pre-allocated vehicles until they run out. This can happen |
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
|
520 |
* if the callback returns different values depending on the cargo type. */ |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
521 |
u->next = vl[i]; |
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
|
522 |
if (u->next == NULL) u->next = AllocateVehicle(); |
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
|
523 |
if (u->next == NULL) return; |
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
|
524 |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
525 |
u = u->next; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
526 |
|
6476 | 527 |
EngineID engine_type = GB(callback, 0, 7); |
528 |
bool flip_image = HASBIT(callback, 7); |
|
529 |
const RailVehicleInfo *rvi_artic = RailVehInfo(engine_type); |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
530 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
531 |
/* get common values from first engine */ |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
532 |
u->direction = v->direction; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
533 |
u->owner = v->owner; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
534 |
u->tile = v->tile; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
535 |
u->x_pos = v->x_pos; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
536 |
u->y_pos = v->y_pos; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
537 |
u->z_pos = v->z_pos; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
538 |
u->u.rail.track = v->u.rail.track; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
539 |
u->u.rail.railtype = v->u.rail.railtype; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
540 |
u->build_year = v->build_year; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
541 |
u->vehstatus = v->vehstatus & ~VS_STOPPED; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
542 |
u->u.rail.first_engine = v->engine_type; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
543 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
544 |
/* get more settings from rail vehicle info */ |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
545 |
u->spritenum = rvi_artic->image_index; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
546 |
if (flip_image) u->spritenum++; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
547 |
u->cargo_type = rvi_artic->cargo_type; |
3870
766d7bd6c299
(svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents:
3836
diff
changeset
|
548 |
u->cargo_subtype = 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
|
549 |
u->cargo_cap = rvi_artic->capacity; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
550 |
u->max_speed = 0; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
551 |
u->max_age = 0; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
552 |
u->engine_type = engine_type; |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
553 |
u->value = 0; |
7048
06b931095b26
(svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents:
7046
diff
changeset
|
554 |
u = new (u) Train(); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
555 |
u->subtype = 0; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
556 |
SetArticulatedPart(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
|
557 |
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
|
558 |
u->random_bits = VehicleRandomBits(); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
559 |
|
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
560 |
VehiclePositionChanged(u); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
561 |
} |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
562 |
} |
0 | 563 |
|
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
|
564 |
static int32 CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags) |
0 | 565 |
{ |
889
7f8630bfea41
(svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents:
882
diff
changeset
|
566 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
7f8630bfea41
(svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents:
882
diff
changeset
|
567 |
|
6476 | 568 |
const RailVehicleInfo *rvi = RailVehInfo(engine); |
569 |
int32 value = (rvi->base_cost * _price.build_railwagon) >> 8; |
|
570 |
||
571 |
uint num_vehicles = 1 + CountArticulatedParts(engine); |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
572 |
|
0 | 573 |
if (!(flags & DC_QUERY_COST)) { |
2609
b2c7aa8ce0c4
(svn r3146) Avoid use of variable size declared arrays when allocating articulated rail vehicles.
peter1138
parents:
2608
diff
changeset
|
574 |
Vehicle *vl[11]; // Allow for wagon and upto 10 artic parts. |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
575 |
|
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
|
576 |
memset(&vl, 0, sizeof(vl)); |
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
|
577 |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
578 |
if (!AllocateVehicles(vl, num_vehicles)) |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
579 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
0 | 580 |
|
581 |
if (flags & DC_EXEC) { |
|
6476 | 582 |
Vehicle *v = vl[0]; |
2639 | 583 |
v->spritenum = rvi->image_index; |
0 | 584 |
|
6476 | 585 |
Vehicle *u = NULL; |
586 |
||
587 |
Vehicle *w; |
|
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
588 |
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
|
589 |
if (w->type == VEH_TRAIN && w->tile == tile && |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
590 |
IsFreeWagon(w) && w->engine_type == engine) { |
919
b0d6c7642f99
(svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents:
915
diff
changeset
|
591 |
u = GetLastVehicleInChain(w); |
0 | 592 |
break; |
593 |
} |
|
594 |
} |
|
595 |
||
596 |
v->engine_type = engine; |
|
597 |
||
6476 | 598 |
DiagDirection dir = GetRailDepotDirection(tile); |
2150
010d923a81a9
(svn r2660) Get rid of some more shifting/anding/casting
tron
parents:
2140
diff
changeset
|
599 |
|
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3140
diff
changeset
|
600 |
v->direction = DiagDirToDir(dir); |
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
601 |
v->tile = tile; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
602 |
|
6476 | 603 |
int x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir]; |
604 |
int y = TileY(tile) * TILE_SIZE | _vehicle_initial_y_fract[dir]; |
|
0 | 605 |
|
606 |
v->x_pos = x; |
|
607 |
v->y_pos = y; |
|
6476 | 608 |
v->z_pos = GetSlopeZ(x, y); |
0 | 609 |
v->owner = _current_player; |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
610 |
v->u.rail.track = TRACK_BIT_DEPOT; |
0 | 611 |
v->vehstatus = VS_HIDDEN | VS_DEFPAL; |
612 |
||
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
613 |
v->subtype = 0; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
614 |
SetTrainWagon(v); |
0 | 615 |
if (u != NULL) { |
616 |
u->next = v; |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
617 |
} else { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
618 |
SetFreeWagon(v); |
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
619 |
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); |
0 | 620 |
} |
621 |
||
622 |
v->cargo_type = rvi->cargo_type; |
|
3870
766d7bd6c299
(svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents:
3836
diff
changeset
|
623 |
v->cargo_subtype = 0; |
0 | 624 |
v->cargo_cap = rvi->capacity; |
625 |
v->value = value; |
|
626 |
// v->day_counter = 0; |
|
627 |
||
6074 | 628 |
v->u.rail.railtype = rvi->railtype; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
629 |
|
4329
9759d5c52010
(svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents:
4293
diff
changeset
|
630 |
v->build_year = _cur_year; |
7048
06b931095b26
(svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents:
7046
diff
changeset
|
631 |
v = new (v) Train(); |
0 | 632 |
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
|
633 |
v->random_bits = VehicleRandomBits(); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
634 |
|
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
|
635 |
AddArticulatedParts(vl); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
636 |
|
2564 | 637 |
_new_vehicle_id = v->index; |
0 | 638 |
|
639 |
VehiclePositionChanged(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
|
640 |
TrainConsistChanged(GetFirstVehicleInChain(v)); |
0 | 641 |
|
642 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
|
2970
77fa46c313c0
(svn r3535) -Feature: [autoreplace] allow autoreplacing of train wagons
bjarni
parents:
2962
diff
changeset
|
643 |
if (IsLocalPlayer()) { |
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
|
644 |
InvalidateAutoreplaceWindow(VEH_TRAIN); // updates the replace Train window |
2970
77fa46c313c0
(svn r3535) -Feature: [autoreplace] allow autoreplacing of train wagons
bjarni
parents:
2962
diff
changeset
|
645 |
} |
6195
b90cf92697b9
(svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents:
6173
diff
changeset
|
646 |
GetPlayer(_current_player)->num_engines[engine]++; |
0 | 647 |
} |
648 |
} |
|
649 |
||
650 |
return value; |
|
651 |
} |
|
652 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
653 |
/** Move all free vehicles in the depot to the train */ |
2630 | 654 |
static void NormalizeTrainVehInDepot(const Vehicle* u) |
0 | 655 |
{ |
2630 | 656 |
const Vehicle* v; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
657 |
|
0 | 658 |
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
|
659 |
if (v->type == VEH_TRAIN && IsFreeWagon(v) && |
0 | 660 |
v->tile == u->tile && |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
661 |
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
|
662 |
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
|
663 |
CMD_MOVE_RAIL_VEHICLE))) |
0 | 664 |
break; |
665 |
} |
|
666 |
} |
|
667 |
} |
|
668 |
||
2630 | 669 |
static int32 EstimateTrainCost(const RailVehicleInfo* rvi) |
0 | 670 |
{ |
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
|
671 |
return rvi->base_cost * (_price.build_railvehicle >> 3) >> 5; |
0 | 672 |
} |
673 |
||
2817 | 674 |
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
|
675 |
{ |
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->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
|
677 |
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
|
678 |
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
|
679 |
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
|
680 |
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
|
681 |
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
|
682 |
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
|
683 |
u->vehstatus = v->vehstatus & ~VS_STOPPED; |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
684 |
u->subtype = 0; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
685 |
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
|
686 |
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
|
687 |
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
|
688 |
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
|
689 |
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
|
690 |
u->u.rail.railtype = v->u.rail.railtype; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
691 |
if (building) v->next = u; |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
692 |
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
|
693 |
u->build_year = v->build_year; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
694 |
if (building) v->value >>= 1; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
695 |
u->value = v->value; |
7048
06b931095b26
(svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents:
7046
diff
changeset
|
696 |
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
|
697 |
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
|
698 |
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
|
699 |
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
|
700 |
} |
e8c44e426175
(svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents:
1057
diff
changeset
|
701 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
702 |
/** 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
|
703 |
* @param tile tile of the depot where rail-vehicle is built |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
704 |
* @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
|
705 |
* @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
|
706 |
* @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
|
707 |
* bit 1 prevents any free cars from being added to the train |
0 | 708 |
*/ |
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
|
709 |
int32 CmdBuildRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 710 |
{ |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
711 |
/* Check if the engine-type is valid (for the player) */ |
6903
0b3d43bd1fdb
(svn r9543) -Fix [FS#691]: Split up STR_ENGINE_NOT_BUILDABLE into four strings so it can be
maedhros
parents:
6683
diff
changeset
|
712 |
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
|
713 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
714 |
/* 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
|
715 |
* 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
|
716 |
if (!(flags & DC_QUERY_COST)) { |
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
|
717 |
if (!IsTileDepotType(tile, TRANSPORT_RAIL)) 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
|
718 |
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
|
719 |
} |
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
|
720 |
|
0 | 721 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
722 |
||
6476 | 723 |
const RailVehicleInfo *rvi = RailVehInfo(p1); |
2244 | 724 |
|
725 |
/* Check if depot and new engine uses the same kind of tracks */ |
|
3411
0275a29a3f88
(svn r4226) -Fix: [elrails] it's no longer possible to build electric engines in non-electric depots
bjarni
parents:
3390
diff
changeset
|
726 |
/* We need to see if the engine got power on the tile to avoid eletric engines in non-electric depots */ |
6074 | 727 |
if (!HasPowerOnRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
728 |
|
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
|
729 |
if (rvi->railveh_type == RAILVEH_WAGON) return CmdBuildRailWagon(p1, tile, flags); |
0 | 730 |
|
6476 | 731 |
int32 value = EstimateTrainCost(rvi); |
732 |
||
733 |
uint num_vehicles = |
|
734 |
(rvi->railveh_type == RAILVEH_MULTIHEAD ? 2 : 1) + |
|
735 |
CountArticulatedParts(p1); |
|
0 | 736 |
|
737 |
if (!(flags & DC_QUERY_COST)) { |
|
2609
b2c7aa8ce0c4
(svn r3146) Avoid use of variable size declared arrays when allocating articulated rail vehicles.
peter1138
parents:
2608
diff
changeset
|
738 |
Vehicle *vl[12]; // Allow for upto 10 artic parts and dual-heads |
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
|
739 |
|
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
|
740 |
memset(&vl, 0, sizeof(vl)); |
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
|
741 |
|
6173 | 742 |
if (!AllocateVehicles(vl, num_vehicles)) |
0 | 743 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
744 |
||
6476 | 745 |
Vehicle *v = vl[0]; |
746 |
||
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
|
747 |
UnitID unit_num = HASBIT(p2, 0) ? 0 : GetFreeUnitNumber(VEH_TRAIN); |
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
|
748 |
if (unit_num > _patches.max_trains) |
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
|
749 |
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
|
750 |
|
0 | 751 |
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
|
752 |
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
|
753 |
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
|
754 |
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
|
755 |
|
0 | 756 |
v->unitnumber = unit_num; |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3140
diff
changeset
|
757 |
v->direction = DiagDirToDir(dir); |
1986
5dd3db2b86d7
(svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents:
1980
diff
changeset
|
758 |
v->tile = tile; |
0 | 759 |
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
|
760 |
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
|
761 |
v->y_pos = y; |
6476 | 762 |
v->z_pos = GetSlopeZ(x, y); |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
763 |
v->u.rail.track = TRACK_BIT_DEPOT; |
0 | 764 |
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL; |
765 |
v->spritenum = rvi->image_index; |
|
766 |
v->cargo_type = rvi->cargo_type; |
|
3870
766d7bd6c299
(svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents:
3836
diff
changeset
|
767 |
v->cargo_subtype = 0; |
0 | 768 |
v->cargo_cap = rvi->capacity; |
769 |
v->max_speed = rvi->max_speed; |
|
770 |
v->value = value; |
|
1266
9dc5638fe8cc
(svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents:
1247
diff
changeset
|
771 |
v->last_station_visited = INVALID_STATION; |
0 | 772 |
v->dest_tile = 0; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
773 |
|
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
|
774 |
v->engine_type = p1; |
0 | 775 |
|
6074 | 776 |
const Engine *e = GetEngine(p1); |
0 | 777 |
v->reliability = e->reliability; |
778 |
v->reliability_spd_dec = e->reliability_spd_dec; |
|
779 |
v->max_age = e->lifelength * 366; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
780 |
|
0 | 781 |
v->string_id = STR_SV_TRAIN_NAME; |
6074 | 782 |
v->u.rail.railtype = rvi->railtype; |
2564 | 783 |
_new_vehicle_id = v->index; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
784 |
|
0 | 785 |
v->service_interval = _patches.servint_trains; |
786 |
v->date_of_last_service = _date; |
|
4329
9759d5c52010
(svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents:
4293
diff
changeset
|
787 |
v->build_year = _cur_year; |
7048
06b931095b26
(svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents:
7046
diff
changeset
|
788 |
v = new (v) Train(); |
0 | 789 |
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
|
790 |
v->random_bits = VehicleRandomBits(); |
0 | 791 |
|
6502
7ca154aca2b7
(svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents:
6501
diff
changeset
|
792 |
v->vehicle_flags = 0; |
7ca154aca2b7
(svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents:
6501
diff
changeset
|
793 |
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SETBIT(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE); |
7ca154aca2b7
(svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents:
6501
diff
changeset
|
794 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
795 |
v->subtype = 0; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
796 |
SetFrontEngine(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
797 |
SetTrainEngine(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
798 |
|
0 | 799 |
VehiclePositionChanged(v); |
800 |
||
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
|
801 |
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
|
802 |
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
|
803 |
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
|
804 |
/* 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
|
805 |
* 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
|
806 |
* 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
|
807 |
*/ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
808 |
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
|
809 |
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
|
810 |
} else { |
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
|
811 |
AddArticulatedParts(vl); |
2244 | 812 |
} |
0 | 813 |
|
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
|
814 |
TrainConsistChanged(v); |
0 | 815 |
UpdateTrainAcceleration(v); |
2244 | 816 |
|
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
|
817 |
if (!HASBIT(p2, 1)) { // check if the cars should be added to the new vehicle |
2244 | 818 |
NormalizeTrainVehInDepot(v); |
819 |
} |
|
0 | 820 |
|
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
821 |
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); |
588 | 822 |
RebuildVehicleLists(); |
0 | 823 |
InvalidateWindow(WC_COMPANY, v->owner); |
6195
b90cf92697b9
(svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents:
6173
diff
changeset
|
824 |
if (IsLocalPlayer()) |
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
|
825 |
InvalidateAutoreplaceWindow(VEH_TRAIN); // updates the replace Train window |
6195
b90cf92697b9
(svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents:
6173
diff
changeset
|
826 |
|
b90cf92697b9
(svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents:
6173
diff
changeset
|
827 |
GetPlayer(_current_player)->num_engines[p1]++; |
0 | 828 |
} |
829 |
} |
|
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
|
830 |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
831 |
return value; |
0 | 832 |
} |
833 |
||
834 |
||
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
835 |
/* 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
|
836 |
* 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
|
837 |
int CheckTrainInDepot(const Vehicle *v, bool needs_to_be_stopped) |
0 | 838 |
{ |
839 |
TileIndex tile = v->tile; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
840 |
|
0 | 841 |
/* check if stopped in a 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
|
842 |
if (!IsTileDepotType(tile, TRANSPORT_RAIL) || v->cur_speed != 0) return -1; |
0 | 843 |
|
6476 | 844 |
int count = 0; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
845 |
for (; v != NULL; v = v->next) { |
2844 | 846 |
/* This count is used by the depot code to determine the number of engines |
847 |
* 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
|
848 |
* 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
|
849 |
* |
87cf4955787d
(svn r5175) -Fix: [autoreplace] FS#186 autoreplaced trains can leave all wagons in depot
bjarni
parents:
3977
diff
changeset
|
850 |
* Also skip counting rear ends of multiheaded engines */ |
87cf4955787d
(svn r5175) -Fix: [autoreplace] FS#186 autoreplaced trains can leave all wagons in depot
bjarni
parents:
3977
diff
changeset
|
851 |
if (!IsArticulatedPart(v) && !(!IsTrainEngine(v) && IsMultiheaded(v))) count++; |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
852 |
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
|
853 |
(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
|
854 |
return -1; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
855 |
} |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
856 |
} |
0 | 857 |
|
858 |
return count; |
|
859 |
} |
|
860 |
||
4529
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
861 |
/* 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
|
862 |
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
|
863 |
{ |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
864 |
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
|
865 |
} |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
866 |
|
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
867 |
/* 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
|
868 |
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
|
869 |
{ |
6476 | 870 |
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
|
871 |
} |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
872 |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
873 |
/** |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
874 |
* 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
|
875 |
* @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
|
876 |
* @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
|
877 |
* @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
|
878 |
*/ |
0 | 879 |
static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first) |
880 |
{ |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
881 |
/* unlinking the first vehicle of the chain? */ |
0 | 882 |
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
|
883 |
v = GetNextVehicle(v); |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
884 |
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
|
885 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
886 |
if (IsTrainWagon(v)) SetFreeWagon(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
887 |
|
0 | 888 |
return v; |
889 |
} |
|
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
|
890 |
|
6476 | 891 |
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
|
892 |
for (u = first; GetNextVehicle(u) != v; u = GetNextVehicle(u)) {} |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
893 |
GetLastEnginePart(u)->next = 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
|
894 |
return first; |
0 | 895 |
} |
896 |
||
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
897 |
static Vehicle *FindGoodVehiclePos(const Vehicle *src) |
0 | 898 |
{ |
899 |
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
|
900 |
EngineID eng = src->engine_type; |
0 | 901 |
TileIndex tile = src->tile; |
902 |
||
903 |
FOR_ALL_VEHICLES(dst) { |
|
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
|
904 |
if (dst->type == VEH_TRAIN && IsFreeWagon(dst) && dst->tile == tile) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
905 |
/* check so all vehicles in the line have the same engine. */ |
0 | 906 |
Vehicle *v = dst; |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
907 |
|
0 | 908 |
while (v->engine_type == eng) { |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
909 |
v = v->next; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
910 |
if (v == NULL) return dst; |
0 | 911 |
} |
912 |
} |
|
913 |
} |
|
914 |
||
915 |
return NULL; |
|
916 |
} |
|
917 |
||
2676
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 |
* 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
|
920 |
* 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
|
921 |
*/ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
922 |
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
|
923 |
{ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
924 |
UnlinkWagon(v, GetFirstVehicleInChain(v)); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
925 |
if (dest == NULL) return; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
926 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
927 |
v->next = dest->next; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
928 |
dest->next = v; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
929 |
ClearFreeWagon(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
930 |
ClearFrontEngine(v); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
931 |
} |
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 |
/* |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
934 |
* 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
|
935 |
* 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
|
936 |
*/ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
937 |
static void NormaliseTrainConsist(Vehicle *v) |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
938 |
{ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
939 |
if (IsFreeWagon(v)) return; |
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 |
assert(IsFrontEngine(v)); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
942 |
|
2952 | 943 |
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
|
944 |
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
|
945 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
946 |
/* make sure that there are no free cars before next engine */ |
6476 | 947 |
Vehicle *u; |
948 |
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
|
949 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
950 |
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
|
951 |
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
|
952 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
953 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
954 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
955 |
/** 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
|
956 |
* @param tile unused |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
957 |
* @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
|
958 |
* @param p1 various bitstuffed elements |
2639 | 959 |
* - p1 (bit 0 - 15) source vehicle index |
960 |
* - 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
|
961 |
* @param p2 (bit 0) move all vehicles following the source vehicle |
0 | 962 |
*/ |
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
|
963 |
int32 CmdMoveRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 964 |
{ |
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
|
965 |
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
|
966 |
VehicleID d = GB(p1, 16, 16); |
0 | 967 |
|
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
968 |
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
|
969 |
|
6476 | 970 |
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
|
971 |
|
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
|
972 |
if (src->type != VEH_TRAIN || !CheckOwnership(src->owner)) return CMD_ERROR; |
0 | 973 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
974 |
/* if nothing is selected as destination, try and find a matching vehicle to drag to. */ |
6476 | 975 |
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
|
976 |
if (d == INVALID_VEHICLE) { |
4198
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
977 |
dst = IsTrainEngine(src) ? NULL : FindGoodVehiclePos(src); |
0 | 978 |
} else { |
6462 | 979 |
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
|
980 |
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
|
981 |
if (dst->type != VEH_TRAIN || !CheckOwnership(dst->owner)) return CMD_ERROR; |
0 | 982 |
} |
983 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
984 |
/* if an articulated part is being handled, deal with its parent vehicle */ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
985 |
while (IsArticulatedPart(src)) src = GetPrevVehicleInChain(src); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
986 |
if (dst != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
987 |
while (IsArticulatedPart(dst)) dst = GetPrevVehicleInChain(dst); |
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
988 |
} |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
989 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
990 |
/* don't move the same vehicle.. */ |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
991 |
if (src == dst) return 0; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
992 |
|
0 | 993 |
/* locate the head of the two chains */ |
6476 | 994 |
Vehicle *src_head = GetFirstVehicleInChain(src); |
995 |
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
|
996 |
if (dst != NULL) { |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
997 |
dst_head = GetFirstVehicleInChain(dst); |
6462 | 998 |
if (dst_head->tile != src_head->tile) return CMD_ERROR; |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
999 |
/* 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
|
1000 |
dst = GetLastEnginePart(dst); |
6462 | 1001 |
} else { |
1002 |
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
|
1003 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1004 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1005 |
if (dst != NULL && IsMultiheaded(dst) && !IsTrainEngine(dst) && IsTrainWagon(src)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1006 |
/* We are moving a wagon to the rear part of a multiheaded engine */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1007 |
if (dst->next == NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1008 |
/* It's the last one, so we will add the wagon just before the rear engine */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1009 |
dst = GetPrevVehicleInChain(dst); |
2825
f81c0b099d69
(svn r3373) -Fix: don't link a wagon to itself, which resulted in a wagon disapearing, and a depot which was unable to remove
truelight
parents:
2819
diff
changeset
|
1010 |
/* Now if the vehicle we want to link to is the vehicle itself, drop out */ |
f81c0b099d69
(svn r3373) -Fix: don't link a wagon to itself, which resulted in a wagon disapearing, and a depot which was unable to remove
truelight
parents:
2819
diff
changeset
|
1011 |
if (dst == src) return CMD_ERROR; |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1012 |
/* if dst is NULL, it means that dst got a rear multiheaded engine as first engine. We can't use that */ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1013 |
if (dst == NULL) return CMD_ERROR; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1014 |
} else { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1015 |
/* there are more units on this train, so we will add the wagon after the next one*/ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1016 |
dst = dst->next; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1017 |
} |
1601
9fd461d00287
(svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents:
1554
diff
changeset
|
1018 |
} |
9fd461d00287
(svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents:
1554
diff
changeset
|
1019 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1020 |
if (IsTrainEngine(src) && dst_head != NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1021 |
/* we need to make sure that we didn't place it between a pair of multiheaded engines */ |
6476 | 1022 |
Vehicle *engine = NULL; |
1023 |
||
1024 |
for (Vehicle *u = dst_head; u != NULL; u = u->next) { |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1025 |
if (IsTrainEngine(u) && IsMultiheaded(u) && u->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
|
1026 |
engine = u; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1027 |
} |
4198
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
1028 |
if (engine != NULL && engine->u.rail.other_multiheaded_part == u) { |
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
1029 |
engine = NULL; |
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
1030 |
} |
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
1031 |
if (u == dst) { |
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
1032 |
if (engine != NULL) dst = engine->u.rail.other_multiheaded_part; |
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
1033 |
break; |
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
1034 |
} |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1035 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1036 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1037 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1038 |
if (IsMultiheaded(src) && !IsTrainEngine(src)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1039 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1040 |
/* when moving all wagons, we can't have the same src_head and dst_head */ |
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1041 |
if (HASBIT(p2, 0) && src_head == dst_head) return 0; |
0 | 1042 |
|
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1043 |
{ |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1044 |
int max_len = _patches.mammoth_trains ? 100 : 9; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1045 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1046 |
/* check if all vehicles in the source train are stopped inside a depot. */ |
6476 | 1047 |
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
|
1048 |
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
|
1049 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1050 |
/* 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
|
1051 |
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
|
1052 |
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
|
1053 |
|
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1054 |
if (dst_head != NULL) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1055 |
/* 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
|
1056 |
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
|
1057 |
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
|
1058 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1059 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1060 |
/* We are moving between rows, so only count the wagons from the source |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1061 |
* row that are being moved. */ |
2883
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1062 |
if (HASBIT(p2, 0)) { |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1063 |
const Vehicle *u; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1064 |
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
|
1065 |
src_len--; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1066 |
} else { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1067 |
/* 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
|
1068 |
src_len = 1; |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1069 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1070 |
|
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1071 |
if (src_len + dst_len > max_len) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1072 |
/* 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
|
1073 |
if (dst_head != NULL && IsFrontEngine(dst_head)) return_cmd_error(STR_8819_TRAIN_TOO_LONG); |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1074 |
/* 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
|
1075 |
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
|
1076 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1077 |
} else { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1078 |
/* 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
|
1079 |
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
|
1080 |
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
|
1081 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1082 |
} |
e1efe385e89c
(svn r3431) - Fix crash when moving vehicles around in a train depot under certain conditions.
peter1138
parents:
2874
diff
changeset
|
1083 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1084 |
/* 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
|
1085 |
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
|
1086 |
UnitID unit_num = GetFreeUnitNumber(VEH_TRAIN); |
0 | 1087 |
if (unit_num > _patches.max_trains) |
1088 |
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME); |
|
1089 |
||
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
|
1090 |
if (flags & DC_EXEC) src->unitnumber = unit_num; |
0 | 1091 |
} |
1092 |
||
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
|
1093 |
if (dst_head != NULL) { |
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
|
1094 |
/* Check NewGRF Callback 0x1D */ |
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
|
1095 |
uint16 callback = GetVehicleCallbackParent(CBID_TRAIN_ALLOW_WAGON_ATTACH, 0, 0, dst_head->engine_type, src, dst_head); |
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
|
1096 |
if (callback != CALLBACK_FAILED) { |
3729
c4ef8b67998f
(svn r4706) - NewGRF: add the correct default error message for the wagon attach callback.
peter1138
parents:
3727
diff
changeset
|
1097 |
if (callback == 0xFD) return_cmd_error(STR_INCOMPATIBLE_RAIL_TYPES); |
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
|
1098 |
if (callback < 0xFD) { |
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
|
1099 |
StringID error = GetGRFStringID(GetEngineGRFID(dst_head->engine_type), 0xD000 + callback); |
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
|
1100 |
return_cmd_error(error); |
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
|
1101 |
} |
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
|
1102 |
} |
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
|
1103 |
} |
0 | 1104 |
|
1105 |
/* do it? */ |
|
1106 |
if (flags & DC_EXEC) { |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1107 |
/* clear the ->first cache */ |
6476 | 1108 |
for (Vehicle *u = src_head; u != NULL; u = u->next) u->first = NULL; |
1109 |
for (Vehicle *u = dst_head; u != NULL; u = u->next) u->first = NULL; |
|
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
|
1110 |
|
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1111 |
if (HASBIT(p2, 0)) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1112 |
/* unlink ALL wagons */ |
0 | 1113 |
if (src != src_head) { |
1114 |
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
|
1115 |
while (GetNextVehicle(v) != src) v = GetNextVehicle(v); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
1116 |
GetLastEnginePart(v)->next = 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
|
1117 |
} else { |
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1118 |
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
|
1119 |
src_head = NULL; |
0 | 1120 |
} |
1121 |
} else { |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1122 |
/* 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
|
1123 |
if (src_head == dst_head) dst_head = NULL; |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1124 |
/* 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
|
1125 |
src_head = UnlinkWagon(src, 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
|
1126 |
GetLastEnginePart(src)->next = NULL; |
0 | 1127 |
} |
1128 |
||
1129 |
if (dst == NULL) { |
|
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1130 |
/* 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
|
1131 |
InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile); |
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1132 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1133 |
/* 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
|
1134 |
if (IsTrainEngine(src)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1135 |
if (!IsFrontEngine(src)) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1136 |
/* 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
|
1137 |
SetFrontEngine(src); |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
1138 |
assert(src->orders == NULL); |
0 | 1139 |
src->num_orders = 0; |
1140 |
} |
|
1141 |
} else { |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1142 |
SetFreeWagon(src); |
0 | 1143 |
} |
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
|
1144 |
dst_head = src; |
0 | 1145 |
} else { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1146 |
if (IsFrontEngine(src)) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1147 |
/* the vehicle was previously a loco. need to free the order list and delete vehicle windows etc. */ |
0 | 1148 |
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
|
1149 |
DeleteVehicleOrders(src); |
0 | 1150 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1151 |
|
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1152 |
if (IsFrontEngine(src) || IsFreeWagon(src)) { |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1153 |
InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile); |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1154 |
ClearFrontEngine(src); |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1155 |
ClearFreeWagon(src); |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1156 |
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
|
1157 |
} |
0 | 1158 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1159 |
/* link in the wagon(s) in the chain. */ |
0 | 1160 |
{ |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
1161 |
Vehicle *v; |
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
1162 |
|
2602
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
1163 |
for (v = src; GetNextVehicle(v) != NULL; v = GetNextVehicle(v)); |
f0e2dcce3695
(svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents:
2599
diff
changeset
|
1164 |
GetLastEnginePart(v)->next = dst->next; |
0 | 1165 |
} |
1166 |
dst->next = src; |
|
1167 |
} |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1168 |
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
|
1169 |
if (src->u.rail.other_multiheaded_part == src_head) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1170 |
src_head = src_head->next; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1171 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1172 |
AddWagonToConsist(src->u.rail.other_multiheaded_part, src); |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1173 |
/* previous line set the front engine to the old front. We need to clear that */ |
4197
84acfde415e0
(svn r5664) -Fix: [SF 1518090 ] moving train engines in depot - crash (svn r5463)
bjarni
parents:
4182
diff
changeset
|
1174 |
src->u.rail.other_multiheaded_part->first = NULL; |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1175 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1176 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1177 |
if (HASBIT(p2, 0) && src_head != NULL && src_head != src) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1178 |
/* if we stole a rear multiheaded engine, we better give it back to the front end */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1179 |
Vehicle *engine = NULL, *u; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1180 |
for (u = src_head; u != NULL; u = u->next) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1181 |
if (IsMultiheaded(u)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1182 |
if (IsTrainEngine(u)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1183 |
engine = u; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1184 |
continue; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1185 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1186 |
/* we got the rear engine to match with the front one */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1187 |
engine = NULL; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1188 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1189 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1190 |
if (engine != NULL && engine->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
|
1191 |
AddWagonToConsist(engine->u.rail.other_multiheaded_part, engine); |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1192 |
/* previous line set the front engine to the old front. We need to clear that */ |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1193 |
engine->u.rail.other_multiheaded_part->first = NULL; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1194 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1195 |
} |
0 | 1196 |
|
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
|
1197 |
/* 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
|
1198 |
* 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
|
1199 |
* 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
|
1200 |
if (src_head != NULL && !IsFrontEngine(src_head) && IsTrainEngine(src_head)) { |
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
|
1201 |
CmdMoveRailVehicle(0, flags, src_head->index | (INVALID_VEHICLE << 16), 1); |
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
|
1202 |
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
|
1203 |
} |
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
|
1204 |
|
4198
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
1205 |
if (src_head != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1206 |
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
|
1207 |
TrainConsistChanged(src_head); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1208 |
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
|
1209 |
UpdateTrainAcceleration(src_head); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1210 |
InvalidateWindow(WC_VEHICLE_DETAILS, src_head->index); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1211 |
/* 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
|
1212 |
InvalidateWindow(WC_VEHICLE_REFIT, src_head->index); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1213 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, src_head->index, 12); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1214 |
} |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1215 |
/* 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
|
1216 |
InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile); |
6476 | 1217 |
} |
0 | 1218 |
|
4198
fcb0808731db
(svn r5665) - Codechange: check pointers against NULL, coding style, tabulation; nothing serious.
Darkvater
parents:
4197
diff
changeset
|
1219 |
if (dst_head != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1220 |
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
|
1221 |
TrainConsistChanged(dst_head); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1222 |
if (IsFrontEngine(dst_head)) { |
0 | 1223 |
UpdateTrainAcceleration(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
|
1224 |
InvalidateWindow(WC_VEHICLE_DETAILS, dst_head->index); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1225 |
/* 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
|
1226 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, dst_head->index, 12); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1227 |
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
|
1228 |
} |
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
|
1229 |
/* 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
|
1230 |
InvalidateWindow(WC_VEHICLE_DEPOT, dst_head->tile); |
0 | 1231 |
} |
1232 |
||
737
ceb3dce50a37
(svn r1189) Fix vehicle list update glitch when moving waggons in depots
tron
parents:
715
diff
changeset
|
1233 |
RebuildVehicleLists(); |
0 | 1234 |
} |
1235 |
||
1236 |
return 0; |
|
1237 |
} |
|
1238 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1239 |
/** 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
|
1240 |
* @param tile unused |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1241 |
* @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
|
1242 |
* @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
|
1243 |
* @param p2 unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1244 |
*/ |
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
|
1245 |
int32 CmdStartStopTrain(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 1246 |
{ |
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
1247 |
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
|
1248 |
|
6476 | 1249 |
Vehicle *v = GetVehicle(p1); |
0 | 1250 |
|
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
|
1251 |
if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1252 |
|
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
|
1253 |
/* 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
|
1254 |
* return 0xFF if it can. */ |
6476 | 1255 |
uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v); |
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
|
1256 |
if (callback != CALLBACK_FAILED && callback != 0xFF) { |
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
|
1257 |
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
|
1258 |
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
|
1259 |
} |
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
|
1260 |
|
4251
85befcd5f435
(svn r5853) -Fix: [elrails] FS#178 Electric Trains can leave Conventional Depot
bjarni
parents:
4244
diff
changeset
|
1261 |
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
|
1262 |
|
0 | 1263 |
if (flags & DC_EXEC) { |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
1264 |
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
|
1265 |
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
|
1266 |
} |
4c950c7ec5c9
(svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents:
3022
diff
changeset
|
1267 |
|
0 | 1268 |
v->u.rail.days_since_order_progr = 0; |
1269 |
v->vehstatus ^= VS_STOPPED; |
|
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
1270 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1271 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
1272 |
} |
|
1273 |
return 0; |
|
1274 |
} |
|
1275 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1276 |
/** 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
|
1277 |
* @param tile unused |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1278 |
* @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
|
1279 |
* @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
|
1280 |
* @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
|
1281 |
* - 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
|
1282 |
* - p2 = 1: sell the vehicle and all vehicles following it in the chain |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1283 |
if the wagon is dragged, don't delete the possibly belonging rear-engine to some front |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1284 |
* - 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
|
1285 |
* 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
|
1286 |
*/ |
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
|
1287 |
int32 CmdSellRailWagon(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) || p2 > 2) return CMD_ERROR; |
0 | 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 |
|
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
|
1295 |
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1296 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1297 |
while (IsArticulatedPart(v)) v = GetPrevVehicleInChain(v); |
6476 | 1298 |
Vehicle *first = GetFirstVehicleInChain(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1299 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1300 |
/* 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
|
1301 |
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
|
1302 |
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
|
1303 |
} |
0 | 1304 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1305 |
if (IsMultiheaded(v) && !IsTrainEngine(v)) return_cmd_error(STR_REAR_ENGINE_FOLLOW_FRONT_ERROR); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1306 |
|
1842
7efaede8ac8e
(svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents:
1802
diff
changeset
|
1307 |
if (flags & DC_EXEC) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1308 |
if (v == first && IsFrontEngine(first)) { |
1842
7efaede8ac8e
(svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents:
1802
diff
changeset
|
1309 |
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
|
1310 |
} |
0 | 1311 |
InvalidateWindow(WC_VEHICLE_DEPOT, first->tile); |
1779
598bf208ad44
(svn r2283) - Fix (regression): [ 1197493 ] train_gui.c:1341: failed assertion ..., somehow RebuildVehicleList() got lost during the rewrite :P
Darkvater
parents:
1777
diff
changeset
|
1312 |
RebuildVehicleLists(); |
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
|
1313 |
} |
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
|
1314 |
|
6476 | 1315 |
int32 cost = 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
|
1316 |
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
|
1317 |
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
|
1318 |
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
|
1319 |
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
|
1320 |
|
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 |
/* 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
|
1322 |
* 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
|
1323 |
Vehicle *rear = (IsMultiheaded(v) && |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1324 |
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
|
1325 |
|
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
|
1326 |
if (rear != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1327 |
cost -= 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
|
1328 |
if (flags & DC_EXEC) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1329 |
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
|
1330 |
DeleteDepotHighlightOfVehicle(rear); |
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
|
1331 |
DeleteVehicle(rear); |
0 | 1332 |
} |
1333 |
} |
|
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
|
1334 |
|
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
|
1335 |
/* 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
|
1336 |
* here, so take attention */ |
1770
b6bda4dc6da2
(svn r2274) - Codechange: some comments, parentheses and EngineID typedef for engine_type
Darkvater
parents:
1766
diff
changeset
|
1337 |
if ((flags & DC_EXEC) && v == first) { |
6476 | 1338 |
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
|
1339 |
|
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
|
1340 |
/* 2.1 If the first wagon is sold, update the first-> pointers to NULL */ |
6476 | 1341 |
for (Vehicle *tmp = first; tmp != NULL; tmp = tmp->next) tmp->first = 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
|
1342 |
|
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
|
1343 |
/* 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
|
1344 |
* 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
|
1345 |
* 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
|
1346 |
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
|
1347 |
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
|
1348 |
/* 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
|
1349 |
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
|
1350 |
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
|
1351 |
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
|
1352 |
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
|
1353 |
new_f->num_orders = first->num_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
|
1354 |
|
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
|
1355 |
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
|
1356 |
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
|
1357 |
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
|
1358 |
} |
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
|
1359 |
|
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
|
1360 |
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
|
1361 |
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
|
1362 |
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
|
1363 |
} |
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
|
1364 |
|
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
|
1365 |
/* |
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
|
1366 |
* 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
|
1367 |
* 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
|
1368 |
* 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
|
1369 |
*/ |
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
|
1370 |
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
|
1371 |
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
|
1372 |
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
|
1373 |
|
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
|
1374 |
if (IsLocalPlayer()) ShowTrainViewWindow(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
|
1375 |
} |
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
|
1376 |
} |
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
|
1377 |
|
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 |
/* 3. Delete the requested wagon */ |
1472
043304846688
(svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents:
1461
diff
changeset
|
1379 |
cost -= 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
|
1380 |
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
|
1381 |
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
|
1382 |
DeleteDepotHighlightOfVehicle(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
|
1383 |
DeleteVehicle(v); |
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 |
|
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
|
1385 |
/* 4 If the second wagon was an engine, update it to front_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
|
1386 |
* 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
|
1387 |
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
|
1388 |
|
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
|
1389 |
/* 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
|
1390 |
if (first != NULL) { |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1391 |
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
|
1392 |
TrainConsistChanged(first); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1393 |
if (IsFrontEngine(first)) { |
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
|
1394 |
InvalidateWindow(WC_VEHICLE_DETAILS, first->index); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1395 |
InvalidateWindow(WC_VEHICLE_REFIT, first->index); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1396 |
UpdateTrainAcceleration(first); |
74cab7e464d1
(svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents:
1909
diff
changeset
|
1397 |
} |
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
|
1398 |
} |
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
|
1399 |
|
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 |
|
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
|
1401 |
/* (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
|
1402 |
* 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
|
1403 |
* 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
|
1404 |
* engines to its train anyways */ |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1405 |
if (p2 == 2 && HASBIT(ori_subtype, Train_Front)) { |
6476 | 1406 |
Vehicle *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
|
1407 |
for (v = first; 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
|
1408 |
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
|
1409 |
DoCommand(v->tile, v->index | INVALID_VEHICLE << 16, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE); |
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
|
1410 |
} |
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
|
1411 |
} |
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
|
1412 |
} |
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
|
1413 |
} 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
|
1414 |
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
|
1415 |
/* 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
|
1416 |
* 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
|
1417 |
* earlier in the chain (before deletion), leave it alone */ |
6476 | 1418 |
Vehicle *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
|
1419 |
for (; 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
|
1420 |
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
|
1421 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1422 |
if (IsMultiheaded(v)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1423 |
if (IsTrainEngine(v)) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1424 |
/* 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
|
1425 |
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
|
1426 |
|
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1427 |
if (rear != NULL) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1428 |
cost -= rear->value; |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1429 |
if (flags & DC_EXEC) { |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1430 |
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
|
1431 |
DeleteDepotHighlightOfVehicle(rear); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1432 |
DeleteVehicle(rear); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1433 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1434 |
} |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1435 |
} 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
|
1436 |
/* The front to this engine is earlier in this train. Do nothing */ |
2549 | 1437 |
continue; |
1438 |
} |
|
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 |
cost -= v->value; |
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 |
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
|
1443 |
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
|
1444 |
DeleteDepotHighlightOfVehicle(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
|
1445 |
DeleteVehicle(v); |
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
|
1446 |
} |
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
|
1447 |
} |
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
|
1448 |
|
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
|
1449 |
/* 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
|
1450 |
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
|
1451 |
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
|
1452 |
TrainConsistChanged(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
|
1453 |
if (IsFrontEngine(first)) UpdateTrainAcceleration(first); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1454 |
InvalidateWindow(WC_VEHICLE_DETAILS, first->index); |
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
1455 |
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
|
1456 |
} |
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
|
1457 |
} break; |
0 | 1458 |
} |
1459 |
return cost; |
|
1460 |
} |
|
1461 |
||
7054
edbb4d7765f2
(svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents:
7050
diff
changeset
|
1462 |
void Train::UpdateDeltaXY(Direction direction) |
0 | 1463 |
{ |
7054
edbb4d7765f2
(svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents:
7050
diff
changeset
|
1464 |
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0) |
0 | 1465 |
static const uint32 _delta_xy_table[8] = { |
1466 |
MKIT(3, 3, -1, -1), |
|
1467 |
MKIT(3, 7, -1, -3), |
|
1468 |
MKIT(3, 3, -1, -1), |
|
1469 |
MKIT(7, 3, -3, -1), |
|
1470 |
MKIT(3, 3, -1, -1), |
|
1471 |
MKIT(3, 7, -1, -3), |
|
1472 |
MKIT(3, 3, -1, -1), |
|
1473 |
MKIT(7, 3, -3, -1), |
|
1474 |
}; |
|
1475 |
#undef MKIT |
|
1476 |
||
1477 |
uint32 x = _delta_xy_table[direction]; |
|
7054
edbb4d7765f2
(svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents:
7050
diff
changeset
|
1478 |
this->x_offs = GB(x, 0, 8); |
edbb4d7765f2
(svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents:
7050
diff
changeset
|
1479 |
this->y_offs = GB(x, 8, 8); |
edbb4d7765f2
(svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents:
7050
diff
changeset
|
1480 |
this->sprite_width = GB(x, 16, 8); |
edbb4d7765f2
(svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents:
7050
diff
changeset
|
1481 |
this->sprite_height = GB(x, 24, 8); |
edbb4d7765f2
(svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents:
7050
diff
changeset
|
1482 |
this->z_height = 6; |
0 | 1483 |
} |
1484 |
||
1485 |
static void UpdateVarsAfterSwap(Vehicle *v) |
|
1486 |
{ |
|
7054
edbb4d7765f2
(svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents:
7050
diff
changeset
|
1487 |
v->UpdateDeltaXY(v->direction); |
0 | 1488 |
v->cur_image = GetTrainImage(v, v->direction); |
1489 |
BeginVehicleMove(v); |
|
1490 |
VehiclePositionChanged(v); |
|
1491 |
EndVehicleMove(v); |
|
1492 |
} |
|
1493 |
||
2639 | 1494 |
static void SetLastSpeed(Vehicle* v, int spd) |
1495 |
{ |
|
0 | 1496 |
int old = v->u.rail.last_speed; |
1497 |
if (spd != old) { |
|
1498 |
v->u.rail.last_speed = spd; |
|
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 (_patches.vehicle_speed || (old == 0) != (spd == 0)) |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
1500 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1501 |
} |
1502 |
} |
|
1503 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1504 |
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
|
1505 |
{ |
6476 | 1506 |
byte flag1 = *swap_flag1; |
1507 |
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
|
1508 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1509 |
/* Clear the flags */ |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1510 |
CLRBIT(*swap_flag1, VRF_GOINGUP); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1511 |
CLRBIT(*swap_flag1, VRF_GOINGDOWN); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1512 |
CLRBIT(*swap_flag2, VRF_GOINGUP); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1513 |
CLRBIT(*swap_flag2, VRF_GOINGDOWN); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1514 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1515 |
/* Reverse the rail-flags (if needed) */ |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1516 |
if (HASBIT(flag1, VRF_GOINGUP)) { |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1517 |
SETBIT(*swap_flag2, VRF_GOINGDOWN); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1518 |
} else if (HASBIT(flag1, VRF_GOINGDOWN)) { |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1519 |
SETBIT(*swap_flag2, VRF_GOINGUP); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1520 |
} |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1521 |
if (HASBIT(flag2, VRF_GOINGUP)) { |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1522 |
SETBIT(*swap_flag1, VRF_GOINGDOWN); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1523 |
} else if (HASBIT(flag2, VRF_GOINGDOWN)) { |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1524 |
SETBIT(*swap_flag1, VRF_GOINGUP); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1525 |
} |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1526 |
} |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1527 |
|
0 | 1528 |
static void ReverseTrainSwapVeh(Vehicle *v, int l, int r) |
1529 |
{ |
|
1530 |
Vehicle *a, *b; |
|
1531 |
||
1532 |
/* locate vehicles to swap */ |
|
2549 | 1533 |
for (a = v; l != 0; l--) a = a->next; |
1534 |
for (b = v; r != 0; r--) b = b->next; |
|
0 | 1535 |
|
1536 |
if (a != b) { |
|
1537 |
/* swap the hidden bits */ |
|
1538 |
{ |
|
1539 |
uint16 tmp = (a->vehstatus & ~VS_HIDDEN) | (b->vehstatus&VS_HIDDEN); |
|
1540 |
b->vehstatus = (b->vehstatus & ~VS_HIDDEN) | (a->vehstatus&VS_HIDDEN); |
|
1541 |
a->vehstatus = tmp; |
|
1542 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1543 |
|
5984 | 1544 |
Swap(a->u.rail.track, b->u.rail.track); |
1545 |
Swap(a->direction, b->direction); |
|
0 | 1546 |
|
1547 |
/* toggle direction */ |
|
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
1548 |
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
|
1549 |
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
|
1550 |
|
5984 | 1551 |
Swap(a->x_pos, b->x_pos); |
1552 |
Swap(a->y_pos, b->y_pos); |
|
1553 |
Swap(a->tile, b->tile); |
|
1554 |
Swap(a->z_pos, b->z_pos); |
|
0 | 1555 |
|
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1556 |
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
|
1557 |
|
0 | 1558 |
/* update other vars */ |
1559 |
UpdateVarsAfterSwap(a); |
|
1560 |
UpdateVarsAfterSwap(b); |
|
1554
c0b2287254f4
(svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents:
1552
diff
changeset
|
1561 |
|
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
1562 |
/* 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
|
1563 |
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
|
1564 |
if (b->u.rail.track != TRACK_BIT_WORMHOLE) VehicleEnterTile(b, b->tile, b->x_pos, b->y_pos); |
0 | 1565 |
} else { |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
1566 |
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
|
1567 |
UpdateVarsAfterSwap(a); |
1554
c0b2287254f4
(svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents:
1552
diff
changeset
|
1568 |
|
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
1569 |
if (a->u.rail.track != TRACK_BIT_WORMHOLE) VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos); |
0 | 1570 |
} |
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
|
1571 |
|
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
|
1572 |
/* 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
|
1573 |
TrainPowerChanged(v); |
0 | 1574 |
} |
1575 |
||
744
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1576 |
/* Check if the vehicle is a train and is on the tile we are testing */ |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1577 |
static void *TestTrainOnCrossing(Vehicle *v, void *data) |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1578 |
{ |
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
|
1579 |
if (v->tile != *(const TileIndex*)data || v->type != VEH_TRAIN) return NULL; |
744
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1580 |
return v; |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1581 |
} |
41293b73ebbb
(svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents:
743
diff
changeset
|
1582 |
|
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1583 |
static void DisableTrainCrossing(TileIndex tile) |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1584 |
{ |
3560
4389ae5ad967
(svn r4435) - Fix: an assertion triggered when trying to remove a bridge with the remove-tool (r4348 surfaced this). In CmdRemoveRoad tiletype was not checked for ownership. Intorudce IsLevelCrossingTile() which checks if a tile is a crossing without knowing the type. Suggested by peter1138 and Tron.
Darkvater
parents:
3546
diff
changeset
|
1585 |
if (IsLevelCrossingTile(tile) && |
2639 | 1586 |
VehicleFromPos(tile, &tile, TestTrainOnCrossing) == NULL && // empty? |
3322
fa5d46929db9
(svn r4088) -Codechange: Introduce {Unb,B}arCrossing and IsCrossingBarred to put and get the status of a level crossing
celestar
parents:
3315
diff
changeset
|
1587 |
IsCrossingBarred(tile)) { |
fa5d46929db9
(svn r4088) -Codechange: Introduce {Unb,B}arCrossing and IsCrossingBarred to put and get the status of a level crossing
celestar
parents:
3315
diff
changeset
|
1588 |
UnbarCrossing(tile); |
2639 | 1589 |
MarkTileDirtyByTile(tile); |
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1590 |
} |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1591 |
} |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1592 |
|
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1593 |
/** |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1594 |
* Advances wagons for train reversing, needed for variable length wagons. |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1595 |
* Needs to be called once before the train is reversed, and once after it. |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1596 |
* @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
|
1597 |
* @param before Set to true for the call before reversing, false otherwise |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1598 |
*/ |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1599 |
static void AdvanceWagons(Vehicle *v, bool before) |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1600 |
{ |
6476 | 1601 |
Vehicle *base = v; |
1602 |
Vehicle *first = base->next; |
|
1603 |
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
|
1604 |
|
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1605 |
while (length > 2) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1606 |
/* find pairwise matching wagon |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1607 |
* start<>end, start+1<>end-1, ... */ |
6476 | 1608 |
Vehicle *last = first; |
1609 |
for (uint i = length - 3; i > 0; i--) last = last->next; |
|
1610 |
||
1611 |
int differential = last->u.rail.cached_veh_length - base->u.rail.cached_veh_length; |
|
2639 | 1612 |
if (before) differential *= -1; |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1613 |
|
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1614 |
if (differential > 0) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1615 |
/* disconnect last car to make sure only this subset moves */ |
6476 | 1616 |
Vehicle *tempnext = last->next; |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1617 |
last->next = NULL; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1618 |
|
6476 | 1619 |
for (int i = 0; i < differential; i++) TrainController(first, false); |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1620 |
|
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1621 |
last->next = tempnext; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1622 |
} |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1623 |
|
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1624 |
base = first; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1625 |
first = first->next; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1626 |
length -= 2; |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1627 |
} |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1628 |
} |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1629 |
|
2951 | 1630 |
|
0 | 1631 |
static void ReverseTrainDirection(Vehicle *v) |
1632 |
{ |
|
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1633 |
if (IsTileDepotType(v->tile, TRANSPORT_RAIL)) { |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1634 |
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1635 |
} |
0 | 1636 |
|
743
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1637 |
/* Check if we were approaching a rail/road-crossing */ |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1638 |
{ |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1639 |
TileIndex tile = v->tile; |
3163
129a91e147b8
(svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents:
3161
diff
changeset
|
1640 |
DiagDirection dir = DirToDiagDir(v->direction); |
129a91e147b8
(svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents:
3161
diff
changeset
|
1641 |
|
1459
6c1f01803928
(svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents:
1455
diff
changeset
|
1642 |
/* Determine the diagonal direction in which we will exit this tile */ |
3163
129a91e147b8
(svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents:
3161
diff
changeset
|
1643 |
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[dir]) { |
129a91e147b8
(svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents:
3161
diff
changeset
|
1644 |
dir = ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT); |
743
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1645 |
} |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1646 |
/* Calculate next tile */ |
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4556
diff
changeset
|
1647 |
tile += TileOffsByDiagDir(dir); |
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1648 |
|
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1649 |
/* Check if the train left a rail/road-crossing */ |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
1650 |
DisableTrainCrossing(tile); |
743
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1651 |
} |
8161509f1864
(svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents:
742
diff
changeset
|
1652 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1653 |
/* count number of vehicles */ |
6476 | 1654 |
int r = -1; |
1655 |
const Vehicle *u = v; |
|
0 | 1656 |
do r++; while ( (u = u->next) != NULL ); |
1657 |
||
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1658 |
AdvanceWagons(v, true); |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1659 |
|
0 | 1660 |
/* swap start<>end, start+1<>end-1, ... */ |
6476 | 1661 |
int l = 0; |
0 | 1662 |
do { |
1663 |
ReverseTrainSwapVeh(v, l++, r--); |
|
1664 |
} while (l <= r); |
|
1665 |
||
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1666 |
AdvanceWagons(v, false); |
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
1667 |
|
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1668 |
if (IsTileDepotType(v->tile, TRANSPORT_RAIL)) { |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1669 |
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); |
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
1670 |
} |
0 | 1671 |
|
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
1672 |
CLRBIT(v->u.rail.flags, VRF_REVERSING); |
0 | 1673 |
} |
1674 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1675 |
/** 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
|
1676 |
* @param tile unused |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1677 |
* @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
|
1678 |
* @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
|
1679 |
* @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
|
1680 |
*/ |
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
|
1681 |
int32 CmdReverseTrainDirection(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 1682 |
{ |
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
1683 |
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
|
1684 |
|
6476 | 1685 |
Vehicle *v = GetVehicle(p1); |
0 | 1686 |
|
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
|
1687 |
if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1688 |
|
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1689 |
if (p2) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1690 |
/* 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
|
1691 |
|
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
|
1692 |
if (IsMultiheaded(v) || HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_ARTIC_ENGINE)) { |
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1693 |
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
|
1694 |
} |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1695 |
|
6476 | 1696 |
Vehicle *front = GetFirstVehicleInChain(v); |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1697 |
/* 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
|
1698 |
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
|
1699 |
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
|
1700 |
} |
3257
aed8705e3847
(svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents:
3256
diff
changeset
|
1701 |
|
aed8705e3847
(svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents:
3256
diff
changeset
|
1702 |
if (flags & DC_EXEC) { |
3259
e4992824d715
(svn r3947) use TOGGLEBIT() instead of manual bit toggling in CmdReverseTrainDirection (pointed out by glx)
bjarni
parents:
3257
diff
changeset
|
1703 |
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
|
1704 |
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
|
1705 |
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
|
1706 |
} |
aed8705e3847
(svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents:
3256
diff
changeset
|
1707 |
} else { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1708 |
/*turn the whole train around */ |
3257
aed8705e3847
(svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents:
3256
diff
changeset
|
1709 |
if (v->u.rail.crash_anim_pos != 0 || v->breakdown_ctr != 0) return CMD_ERROR; |
aed8705e3847
(svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents:
3256
diff
changeset
|
1710 |
|
aed8705e3847
(svn r3945) cleaned the layout in CmdReverseTrainDirection a bit (made a bit messy in last commit)
bjarni
parents:
3256
diff
changeset
|
1711 |
if (flags & DC_EXEC) { |
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1712 |
if (_patches.realistic_acceleration && 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
|
1713 |
TOGGLEBIT(v->u.rail.flags, VRF_REVERSING); |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1714 |
} else { |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1715 |
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
|
1716 |
SetLastSpeed(v, 0); |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1717 |
ReverseTrainDirection(v); |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
1718 |
} |
0 | 1719 |
} |
1720 |
} |
|
1721 |
return 0; |
|
1722 |
} |
|
1723 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1724 |
/** 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
|
1725 |
* @param tile unused |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1726 |
* @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
|
1727 |
* @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
|
1728 |
* @param p2 unused |
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1729 |
*/ |
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
|
1730 |
int32 CmdForceTrainProceed(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 1731 |
{ |
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
1732 |
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
|
1733 |
|
6476 | 1734 |
Vehicle *v = GetVehicle(p1); |
0 | 1735 |
|
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
|
1736 |
if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; |
0 | 1737 |
|
2639 | 1738 |
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
|
1739 |
|
0 | 1740 |
return 0; |
1741 |
} |
|
1742 |
||
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
|
1743 |
/** 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
|
1744 |
* @param tile unused |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1745 |
* @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
|
1746 |
* @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
|
1747 |
* param p2 various bitstuffed elements |
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
1748 |
* - 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
|
1749 |
* - p2 = (bit 8-15) - the new cargo subtype to refit to |
7042
d841fb0d8e9a
(svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents:
6998
diff
changeset
|
1750 |
* - p2 = (bit 16) - refit only this vehicle |
d841fb0d8e9a
(svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents:
6998
diff
changeset
|
1751 |
* @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
|
1752 |
*/ |
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
|
1753 |
int32 CmdRefitRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 1754 |
{ |
2635 | 1755 |
CargoID new_cid = GB(p2, 0, 8); |
3954
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
1756 |
byte new_subtype = GB(p2, 8, 8); |
7042
d841fb0d8e9a
(svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents:
6998
diff
changeset
|
1757 |
bool only_this = HASBIT(p2, 16); |
842 | 1758 |
|
4352
8ddb01bc6075
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents:
4351
diff
changeset
|
1759 |
if (!IsValidVehicleID(p1)) return CMD_ERROR; |
915 | 1760 |
|
6476 | 1761 |
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
|
1762 |
|
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
|
1763 |
if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; |
2244 | 1764 |
if (CheckTrainStoppedInDepot(v) < 0) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED); |
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
|
1765 |
|
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
|
1766 |
/* Check cargo */ |
6642
2e690e6fdc03
(svn r9259) -Codechange: NUM_CARGO isn't a valid cargo type...
peter1138
parents:
6629
diff
changeset
|
1767 |
if (new_cid >= NUM_CARGO) return CMD_ERROR; |
0 | 1768 |
|
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
|
1769 |
SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN); |
0a1ce05c3d45
(svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents:
1236
diff
changeset
|
1770 |
|
6476 | 1771 |
int32 cost = 0; |
1772 |
uint num = 0; |
|
915 | 1773 |
|
0 | 1774 |
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
|
1775 |
/* 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
|
1776 |
* 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
|
1777 |
* 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
|
1778 |
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
|
1779 |
|
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
|
1780 |
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
|
1781 |
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
|
1782 |
|
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
|
1783 |
if (HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_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
|
1784 |
/* 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
|
1785 |
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
|
1786 |
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
|
1787 |
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
|
1788 |
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
|
1789 |
/* 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
|
1790 |
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
|
1791 |
/* 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
|
1792 |
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
|
1793 |
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
|
1794 |
} |
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1795 |
|
398f156479c8
(svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents:
1891
diff
changeset
|
1796 |
if (amount == CALLBACK_FAILED) { // callback failed or not used, use default |
6476 | 1797 |
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
|
1798 |
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
|
1799 |
/* 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
|
1800 |
* 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
|
1801 |
* 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
|
1802 |
*/ |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1803 |
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
|
1804 |
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
|
1805 |
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
|
1806 |
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
|
1807 |
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
|
1808 |
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
|
1809 |
} |
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
|
1810 |
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
|
1811 |
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
|
1812 |
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
|
1813 |
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
|
1814 |
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
|
1815 |
} |
6476 | 1816 |
} |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1817 |
|
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1818 |
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
|
1819 |
if (new_cid != v->cargo_type) { |
4544
b2d5ad597e4d
(svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents:
4529
diff
changeset
|
1820 |
cost += 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
|
1821 |
} |
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
|
1822 |
|
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1823 |
num += amount; |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1824 |
if (flags & DC_EXEC) { |
3955
81e7e25e4599
(svn r5104) - When refitting a vehicle to its existing cargo type, don't lose the cargo onboard (useful when adding wagons to a train)
peter1138
parents:
3954
diff
changeset
|
1825 |
v->cargo_count = (v->cargo_type == new_cid) ? min(amount, v->cargo_count) : 0; |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1826 |
v->cargo_type = new_cid; |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1827 |
v->cargo_cap = amount; |
3954
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
1828 |
v->cargo_subtype = new_subtype; |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1829 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1830 |
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile); |
3954
634cd5e35803
(svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents:
3948
diff
changeset
|
1831 |
RebuildVehicleLists(); |
1883
3b02000cfbe0
(svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents:
1882
diff
changeset
|
1832 |
} |
0 | 1833 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1834 |
} |
7042
d841fb0d8e9a
(svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents:
6998
diff
changeset
|
1835 |
} while ((v = v->next) != NULL && !only_this); |
0 | 1836 |
|
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
|
1837 |
_returned_refit_capacity = num; |
0 | 1838 |
|
4708
5129e3add448
(svn r6618) - After refitting a train, update its cached variables as they may change.
peter1138
parents:
4656
diff
changeset
|
1839 |
/* Update the train's cached variables */ |
5303
5a3fa9dfe40b
(svn r7459) -Fix: make sure TrainConsistChanged() is called for the first vehicle of a chain after reffiting
glx
parents:
5279
diff
changeset
|
1840 |
if (flags & DC_EXEC) TrainConsistChanged(GetFirstVehicleInChain(GetVehicle(p1))); |
4708
5129e3add448
(svn r6618) - After refitting a train, update its cached variables as they may change.
peter1138
parents:
4656
diff
changeset
|
1841 |
|
0 | 1842 |
return cost; |
1843 |
} |
|
1844 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
1845 |
struct TrainFindDepotData { |
0 | 1846 |
uint best_length; |
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1969
diff
changeset
|
1847 |
TileIndex tile; |
2475 | 1848 |
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
|
1849 |
/** |
2639 | 1850 |
* true if reversing is necessary for the train to get to this depot |
1851 |
* 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
|
1852 |
*/ |
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
|
1853 |
bool reverse; |
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
1854 |
}; |
0 | 1855 |
|
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
1856 |
static bool NtpCallbFindDepot(TileIndex tile, TrainFindDepotData *tfdd, int track, uint length) |
0 | 1857 |
{ |
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3267
diff
changeset
|
1858 |
if (IsTileType(tile, MP_RAILWAY) && |
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3267
diff
changeset
|
1859 |
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
|
1860 |
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
|
1861 |
/* 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
|
1862 |
tfdd->best_length = length / DIAG_FACTOR; |
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3267
diff
changeset
|
1863 |
tfdd->tile = tile; |
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3267
diff
changeset
|
1864 |
return true; |
0 | 1865 |
} |
1866 |
||
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2115
diff
changeset
|
1867 |
return false; |
0 | 1868 |
} |
1869 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1870 |
/** returns the tile of a depot to goto to. The given vehicle must not be |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1871 |
* 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
|
1872 |
static TrainFindDepotData FindClosestTrainDepot(Vehicle *v, int max_distance) |
0 | 1873 |
{ |
6476 | 1874 |
assert(!(v->vehstatus & VS_CRASHED)); |
1875 |
||
0 | 1876 |
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
|
1877 |
tfdd.owner = v->owner; |
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
1878 |
tfdd.best_length = (uint)-1; |
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
|
1879 |
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
|
1880 |
|
6476 | 1881 |
TileIndex tile = v->tile; |
1882 |
if (IsTileDepotType(tile, TRANSPORT_RAIL)) { |
|
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
|
1883 |
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
|
1884 |
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
|
1885 |
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
|
1886 |
} |
0 | 1887 |
|
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
1888 |
if (_patches.yapf.rail_use_yapf) { |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
1889 |
bool found = YapfFindNearestRailDepotTwoWay(v, max_distance, NPF_INFINITE_PENALTY, &tfdd.tile, &tfdd.reverse); |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
1890 |
tfdd.best_length = found ? max_distance / 2 : -1; // some fake distance or NOT_FOUND |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
1891 |
} else if (_patches.new_pathfinding_all) { |
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
|
1892 |
Vehicle* last = GetLastVehicleInChain(v); |
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
1893 |
Trackdir trackdir = GetVehicleTrackdir(v); |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
1894 |
Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last)); |
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
1895 |
|
6476 | 1896 |
assert(trackdir != INVALID_TRACKDIR); |
1897 |
NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, last->tile, trackdir_rev, TRANSPORT_RAIL, v->owner, v->u.rail.compatible_railtypes, NPF_INFINITE_PENALTY); |
|
1247 | 1898 |
if (ftd.best_bird_dist == 0) { |
1899 |
/* Found target */ |
|
1900 |
tfdd.tile = ftd.node.tile; |
|
1675
d004a4d0f6d4
(svn r2179) - Fix: [ 1121437, 1114228 ] Vehicles not entering depots for auto servicing.
matthijs
parents:
1615
diff
changeset
|
1901 |
/* Our caller expects a number of tiles, so we just approximate that |
4549
60410aa1aa88
(svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents:
4546
diff
changeset
|
1902 |
* number by this. It might not be completely what we want, but it will |
60410aa1aa88
(svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents:
4546
diff
changeset
|
1903 |
* work for now :-) We can possibly change this when the old pathfinder |
60410aa1aa88
(svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents:
4546
diff
changeset
|
1904 |
* is removed. */ |
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
|
1905 |
tfdd.best_length = ftd.best_path_dist / NPF_TILE_LENGTH; |
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
|
1906 |
if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) tfdd.reverse = true; |
1247 | 1907 |
} |
0 | 1908 |
} else { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1909 |
/* search in the forward direction first. */ |
6476 | 1910 |
DiagDirection i = DirToDiagDir(v->direction); |
3163
129a91e147b8
(svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents:
3161
diff
changeset
|
1911 |
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { |
129a91e147b8
(svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents:
3161
diff
changeset
|
1912 |
i = ChangeDiagDir(i, DIAGDIRDIFF_90LEFT); |
129a91e147b8
(svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents:
3161
diff
changeset
|
1913 |
} |
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
|
1914 |
NewTrainPathfind(tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &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
|
1915 |
if (tfdd.best_length == (uint)-1){ |
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
|
1916 |
tfdd.reverse = true; |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1917 |
/* search in backwards direction */ |
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3140
diff
changeset
|
1918 |
i = ReverseDiagDir(DirToDiagDir(v->direction)); |
3163
129a91e147b8
(svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents:
3161
diff
changeset
|
1919 |
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { |
129a91e147b8
(svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents:
3161
diff
changeset
|
1920 |
i = ChangeDiagDir(i, DIAGDIRDIFF_90LEFT); |
129a91e147b8
(svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents:
3161
diff
changeset
|
1921 |
} |
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
|
1922 |
NewTrainPathfind(tile, 0, v->u.rail.compatible_railtypes, i, (NTPEnumProc*)NtpCallbFindDepot, &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
|
1923 |
} |
0 | 1924 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1925 |
|
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
|
1926 |
return tfdd; |
0 | 1927 |
} |
1928 |
||
1784
6eb3ab1bc33c
(svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents:
1779
diff
changeset
|
1929 |
/** 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
|
1930 |
* @param tile unused |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
1931 |
* @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
|
1932 |
* @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
|
1933 |
* @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
|
1934 |
* - 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
|
1935 |
* - 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
|
1936 |
*/ |
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
|
1937 |
int32 CmdSendTrainToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) |
0 | 1938 |
{ |
4506
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
1939 |
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
|
1940 |
/* 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
|
1941 |
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
|
1942 |
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
|
1943 |
} |
3a70624c40eb
(svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents:
4451
diff
changeset
|
1944 |
|
4506
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
1945 |
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
|
1946 |
|
6476 | 1947 |
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
|
1948 |
|
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
|
1949 |
if (v->type != VEH_TRAIN || !CheckOwnership(v->owner)) return CMD_ERROR; |
4506
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
1950 |
|
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
1951 |
if (v->vehstatus & VS_CRASHED) return CMD_ERROR; |
1757
b179d50241dc
(svn r2261) - Fix: When crashed vehicles try to find a depot for servicing, openttd asserts.
matthijs
parents:
1752
diff
changeset
|
1952 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1953 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
4519
1437b1e7416c
(svn r6304) -Codechange r6295: Use !! instead of (bool) in commands to send vehicles to depots
bjarni
parents:
4510
diff
changeset
|
1954 |
if (!!(p2 & DEPOT_SERVICE) == HASBIT(v->current_order.flags, OFB_HALT_IN_DEPOT)) { |
4510
071bca98c577
(svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents:
4506
diff
changeset
|
1955 |
/* We called with a different DEPOT_SERVICE setting. |
4549
60410aa1aa88
(svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents:
4546
diff
changeset
|
1956 |
* Now we change the setting to apply the new one and let the vehicle head for the same depot. |
60410aa1aa88
(svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents:
4546
diff
changeset
|
1957 |
* Note: the if is (true for requesting service == true for ordered to stop in depot) */ |
4510
071bca98c577
(svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents:
4506
diff
changeset
|
1958 |
if (flags & DC_EXEC) { |
071bca98c577
(svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents:
4506
diff
changeset
|
1959 |
TOGGLEBIT(v->current_order.flags, OFB_HALT_IN_DEPOT); |
071bca98c577
(svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents:
4506
diff
changeset
|
1960 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
071bca98c577
(svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents:
4506
diff
changeset
|
1961 |
} |
071bca98c577
(svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents:
4506
diff
changeset
|
1962 |
return 0; |
071bca98c577
(svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents:
4506
diff
changeset
|
1963 |
} |
071bca98c577
(svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents:
4506
diff
changeset
|
1964 |
|
4506
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
1965 |
if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders |
0 | 1966 |
if (flags & DC_EXEC) { |
1615
cee18da24b41
(svn r2119) - Fix: [ 1172878 ] Trains "Go to depot" button: click twice skip to next order (patch by Loic GUILLOUX). I also added short explanation of OF_/OFB_ difference to order.h.
pasky
parents:
1601
diff
changeset
|
1967 |
if (HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) { |
0 | 1968 |
v->u.rail.days_since_order_progr = 0; |
1969 |
v->cur_order_index++; |
|
1970 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1971 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1972 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1973 |
v->current_order.flags = 0; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
1974 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 1975 |
} |
1976 |
return 0; |
|
1977 |
} |
|
1978 |
||
4526 | 1979 |
/* check if at a standstill (not stopped only) in a depot |
1980 |
* the check is down here to make it possible to alter stop/service for trains entering the depot */ |
|
1981 |
if (IsTileDepotType(v->tile, TRANSPORT_RAIL) && v->cur_speed == 0) return CMD_ERROR; |
|
1982 |
||
6476 | 1983 |
TrainFindDepotData tfdd = FindClosestTrainDepot(v, 0); |
4506
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
1984 |
if (tfdd.best_length == (uint)-1) return_cmd_error(STR_883A_UNABLE_TO_FIND_ROUTE_TO); |
0 | 1985 |
|
1986 |
if (flags & DC_EXEC) { |
|
6998
403314de0154
(svn r9685) -Fix (r9683): Call v->LeaveStation() when a vehicle in a station is sent to a depot.
maedhros
parents:
6987
diff
changeset
|
1987 |
if (v->current_order.type == OT_LOADING) v->LeaveStation(); |
403314de0154
(svn r9685) -Fix (r9683): Call v->LeaveStation() when a vehicle in a station is sent to a depot.
maedhros
parents:
6987
diff
changeset
|
1988 |
|
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
|
1989 |
v->dest_tile = tfdd.tile; |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
1990 |
v->current_order.type = OT_GOTO_DEPOT; |
4412
945e8a9bb89a
(svn r6165) -Feature: control click Goto Depot will now make the vehicle service
bjarni
parents:
4406
diff
changeset
|
1991 |
v->current_order.flags = OF_NON_STOP; |
4506
0d8fcc0a4e49
(svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents:
4463
diff
changeset
|
1992 |
if (!(p2 & DEPOT_SERVICE)) SETBIT(v->current_order.flags, OFB_HALT_IN_DEPOT); |
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
1993 |
v->current_order.dest = GetDepotByTile(tfdd.tile)->index; |
5259
08336337aae0
(svn r7389) -Fix: when forcing a vehicle to go to a depot, clear refit orders from current orders (caused refit error popup when not having no orders)
bjarni
parents:
5256
diff
changeset
|
1994 |
v->current_order.refit_cargo = CT_INVALID; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
1995 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
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
|
1996 |
/* If there is no depot in front, reverse automatically */ |
6998
403314de0154
(svn r9685) -Fix (r9683): Call v->LeaveStation() when a vehicle in a station is sent to a depot.
maedhros
parents:
6987
diff
changeset
|
1997 |
if (tfdd.reverse) DoCommand(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION); |
0 | 1998 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
1999 |
|
0 | 2000 |
return 0; |
2001 |
} |
|
2002 |
||
2003 |
||
6573 | 2004 |
void OnTick_Train() |
0 | 2005 |
{ |
2006 |
_age_cargo_skip_counter = (_age_cargo_skip_counter == 0) ? 184 : (_age_cargo_skip_counter - 1); |
|
2007 |
} |
|
2008 |
||
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2009 |
static const int8 _vehicle_smoke_pos[8] = { |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2010 |
1, 1, 1, 0, -1, -1, -1, 0 |
0 | 2011 |
}; |
2012 |
||
2630 | 2013 |
static void HandleLocomotiveSmokeCloud(const Vehicle* v) |
0 | 2014 |
{ |
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
2015 |
bool sound = false; |
0 | 2016 |
|
2017 |
if (v->vehstatus & VS_TRAIN_SLOWING || v->load_unload_time_rem != 0 || v->cur_speed < 2) |
|
2018 |
return; |
|
2019 |
||
6476 | 2020 |
const Vehicle* u = v; |
0 | 2021 |
|
2022 |
do { |
|
6074 | 2023 |
const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2024 |
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
|
2025 |
byte effect_type = GB(v->u.rail.cached_vis_effect, 4, 2); |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2026 |
bool disable_effect = HASBIT(v->u.rail.cached_vis_effect, 6); |
0 | 2027 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2028 |
/* 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
|
2029 |
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
|
2030 |
disable_effect || |
6074 | 2031 |
rvi->railtype > RAILTYPE_ELECTRIC || |
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
|
2032 |
v->vehstatus & VS_HIDDEN) { |
0 | 2033 |
continue; |
2639 | 2034 |
} |
0 | 2035 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2036 |
/* No smoke in depots or tunnels */ |
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
|
2037 |
if (IsTileDepotType(v->tile, TRANSPORT_RAIL) || IsTunnelTile(v->tile)) continue; |
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
|
2038 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2039 |
/* No sparks for electric vehicles on nonelectrified tracks */ |
6480 | 2040 |
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
|
2041 |
|
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2042 |
if (effect_type == 0) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2043 |
/* Use default effect type for engine class. */ |
6074 | 2044 |
effect_type = rvi->engclass; |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2045 |
} else { |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2046 |
effect_type--; |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2047 |
} |
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2048 |
|
6476 | 2049 |
int x = _vehicle_smoke_pos[v->direction] * effect_offset; |
2050 |
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
|
2051 |
|
3256
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
2052 |
if (HASBIT(v->u.rail.flags, VRF_REVERSE_DIRECTION)) { |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
2053 |
x = -x; |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
2054 |
y = -y; |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
2055 |
} |
a23d94f568b7
(svn r3944) -Feature: it's now possible to turn a single unit in a train
bjarni
parents:
3242
diff
changeset
|
2056 |
|
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2057 |
switch (effect_type) { |
0 | 2058 |
case 0: |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2059 |
/* steam smoke. */ |
2612
744aef3af72f
(svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents:
2610
diff
changeset
|
2060 |
if (GB(v->tick_counter, 0, 4) == 0) { |
2595
0bab25144829
(svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents:
2587
diff
changeset
|
2061 |
CreateEffectVehicleRel(v, x, y, 10, EV_STEAM_SMOKE); |
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
2062 |
sound = true; |
0 | 2063 |
} |
2064 |
break; |
|
2065 |
||
2066 |
case 1: |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2067 |
/* diesel smoke */ |
2637
8807e6a63f89
(svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents:
2635
diff
changeset
|
2068 |
if (u->cur_speed <= 40 && CHANCE16(15, 128)) { |
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
|
2069 |
CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE); |
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
2070 |
sound = true; |
0 | 2071 |
} |
2072 |
break; |
|
2073 |
||
2074 |
case 2: |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2075 |
/* blue spark */ |
2637
8807e6a63f89
(svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents:
2635
diff
changeset
|
2076 |
if (GB(v->tick_counter, 0, 2) == 0 && CHANCE16(1, 45)) { |
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
|
2077 |
CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK); |
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
2078 |
sound = true; |
0 | 2079 |
} |
2080 |
break; |
|
2081 |
} |
|
2637
8807e6a63f89
(svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents:
2635
diff
changeset
|
2082 |
} 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
|
2083 |
|
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
2084 |
if (sound) PlayVehicleSound(u, VSE_TRAIN_EFFECT); |
0 | 2085 |
} |
2086 |
||
2549 | 2087 |
static void TrainPlayLeaveStationSound(const Vehicle* v) |
0 | 2088 |
{ |
541 | 2089 |
static const SoundFx sfx[] = { |
2090 |
SND_04_TRAIN, |
|
2091 |
SND_0A_TRAIN_HORN, |
|
2092 |
SND_0A_TRAIN_HORN |
|
2093 |
}; |
|
2094 |
||
6476 | 2095 |
if (PlayVehicleSound(v, VSE_START)) return; |
2096 |
||
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
|
2097 |
EngineID engtype = v->engine_type; |
6074 | 2098 |
switch (RailVehInfo(engtype)->railtype) { |
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
2099 |
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
|
2100 |
case RAILTYPE_ELECTRIC: |
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
2101 |
SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], v); |
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
308
diff
changeset
|
2102 |
break; |
2519
67a2d670c17e
(svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents:
2510
diff
changeset
|
2103 |
|
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
|
2104 |
case RAILTYPE_MONO: SndPlayVehicleFx(SND_47_MAGLEV_2, v); 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
|
2105 |
case RAILTYPE_MAGLEV: SndPlayVehicleFx(SND_41_MAGLEV, v); break; |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2106 |
default: NOT_REACHED(); |
0 | 2107 |
} |
2108 |
} |
|
2109 |
||
2110 |
static bool CheckTrainStayInDepot(Vehicle *v) |
|
2111 |
{ |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2112 |
/* bail out if not all wagons are in the same depot or not in a depot at all */ |
6476 | 2113 |
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
|
2114 |
if (u->u.rail.track != TRACK_BIT_DEPOT || u->tile != v->tile) return false; |
2639 | 2115 |
} |
0 | 2116 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2117 |
/* 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
|
2118 |
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
|
2119 |
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
|
2120 |
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
|
2121 |
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
|
2122 |
} |
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
|
2123 |
|
0 | 2124 |
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
|
2125 |
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
|
2126 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 2127 |
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
|
2128 |
} |
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
2129 |
|
0 | 2130 |
v->load_unload_time_rem = 0; |
2131 |
||
3172
1605cb59ccd9
(svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents:
3163
diff
changeset
|
2132 |
if (UpdateSignalsOnSegment(v->tile, DirToDiagDir(v->direction))) { |
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
|
2133 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 2134 |
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
|
2135 |
} |
0 | 2136 |
} |
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
|
2137 |
|
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
|
2138 |
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
|
2139 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 2140 |
TrainPlayLeaveStationSound(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2141 |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2142 |
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
|
2143 |
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
|
2144 |
|
0 | 2145 |
v->vehstatus &= ~VS_HIDDEN; |
2146 |
v->cur_speed = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2147 |
|
7054
edbb4d7765f2
(svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents:
7050
diff
changeset
|
2148 |
v->UpdateDeltaXY(v->direction); |
0 | 2149 |
v->cur_image = GetTrainImage(v, v->direction); |
2150 |
VehiclePositionChanged(v); |
|
3172
1605cb59ccd9
(svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents:
3163
diff
changeset
|
2151 |
UpdateSignalsOnSegment(v->tile, DirToDiagDir(v->direction)); |
0 | 2152 |
UpdateTrainAcceleration(v); |
4739
bd535b408617
(svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents:
4737
diff
changeset
|
2153 |
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile); |
0 | 2154 |
|
2155 |
return false; |
|
2156 |
} |
|
2157 |
||
1247 | 2158 |
/* Check for station tiles */ |
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2159 |
struct TrainTrackFollowerData { |
0 | 2160 |
TileIndex dest_coords; |
1551
eb05e6a90e67
(svn r2055) -CodeChange: Begun introducting StationID
celestar
parents:
1542
diff
changeset
|
2161 |
StationID station_index; // station index we're heading for |
0 | 2162 |
uint best_bird_dist; |
2163 |
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
|
2164 |
TrackdirByte best_track; |
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2165 |
}; |
0 | 2166 |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2167 |
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
|
2168 |
{ |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2169 |
/* heading for nowhere? */ |
2951 | 2170 |
if (ttfd->dest_coords == 0) return false; |
0 | 2171 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2172 |
/* 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
|
2173 |
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
|
2174 |
IsTileType(tile, MP_STATION) && |
3334
b72ac8637a30
(svn r4113) Moved all relevant map functions for stations to station_map.[ch]
celestar
parents:
3322
diff
changeset
|
2175 |
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
|
2176 |
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
|
2177 |
)) { |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2178 |
/* 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
|
2179 |
* 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
|
2180 |
* approximation of where the station is */ |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2181 |
|
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2182 |
/* 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
|
2183 |
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
|
2184 |
ttfd->best_bird_dist = 0; |
0 | 2185 |
return true; |
2186 |
} else { |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2187 |
/* didn't find station, keep track of the best path so far. */ |
6476 | 2188 |
uint dist = DistanceManhattan(tile, ttfd->dest_coords); |
0 | 2189 |
if (dist < ttfd->best_bird_dist) { |
2190 |
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
|
2191 |
ttfd->best_track = track; |
0 | 2192 |
} |
2193 |
return false; |
|
2194 |
} |
|
2195 |
} |
|
2196 |
||
2630 | 2197 |
static void FillWithStationData(TrainTrackFollowerData* fd, const Vehicle* v) |
0 | 2198 |
{ |
2639 | 2199 |
fd->dest_coords = v->dest_tile; |
2200 |
if (v->current_order.type == OT_GOTO_STATION) { |
|
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
2201 |
fd->station_index = v->current_order.dest; |
2639 | 2202 |
} else { |
2203 |
fd->station_index = INVALID_STATION; |
|
2204 |
} |
|
0 | 2205 |
} |
2206 |
||
2207 |
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
|
2208 |
{{ 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
|
2209 |
{{ 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
|
2210 |
{{ 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
|
2211 |
{{ 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
|
2212 |
{{ 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
|
2213 |
{{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 8, 4 }, { 7, 15, 0 }}, |
0 | 2214 |
}; |
2215 |
||
2216 |
static const uint32 _reachable_tracks[4] = { |
|
2217 |
0x10091009, |
|
2218 |
0x00160016, |
|
2219 |
0x05200520, |
|
2220 |
0x2A002A00, |
|
2221 |
}; |
|
2222 |
||
2223 |
static const byte _search_directions[6][4] = { |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2224 |
{ 0, 9, 2, 9 }, ///< track 1 |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2225 |
{ 9, 1, 9, 3 }, ///< track 2 |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2226 |
{ 9, 0, 3, 9 }, ///< track upper |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2227 |
{ 1, 9, 9, 2 }, ///< track lower |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2228 |
{ 3, 2, 9, 9 }, ///< track left |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2229 |
{ 9, 9, 1, 0 }, ///< track right |
0 | 2230 |
}; |
2231 |
||
2232 |
static const byte _pick_track_table[6] = {1, 3, 2, 2, 0, 0}; |
|
2233 |
||
2234 |
/* choose a track */ |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2235 |
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
|
2236 |
{ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2237 |
Track best_track; |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2238 |
/* 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
|
2239 |
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
|
2240 |
|
2758
9cd8a21e9a7b
(svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents:
2752
diff
changeset
|
2241 |
#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
|
2242 |
TIC() |
0 | 2243 |
#endif |
2244 |
||
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2245 |
assert((tracks & ~0x3F) == 0); |
0 | 2246 |
|
1247 | 2247 |
/* quick return in case only one possible track is available */ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2248 |
if (KILL_FIRST_BIT(tracks) == 0) return FindFirstTrack(tracks); |
0 | 2249 |
|
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2250 |
if (_patches.yapf.rail_use_yapf) { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2251 |
Trackdir trackdir = YapfChooseRailTrack(v, tile, enterdir, tracks, &path_not_found); |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2252 |
if (trackdir != INVALID_TRACKDIR) { |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2253 |
best_track = TrackdirToTrack(trackdir); |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2254 |
} else { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2255 |
best_track = FindFirstTrack(tracks); |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2256 |
} |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2257 |
} else if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */ |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2258 |
void* perf = NpfBeginInterval(); |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2259 |
|
1247 | 2260 |
NPFFindStationOrTileData fstd; |
2261 |
NPFFillWithOrderData(&fstd, v); |
|
2262 |
/* The enterdir for the new tile, is the exitdir for the old tile */ |
|
6476 | 2263 |
Trackdir trackdir = GetVehicleTrackdir(v); |
1247 | 2264 |
assert(trackdir != 0xff); |
2265 |
||
6476 | 2266 |
NPFFoundTargetData ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.compatible_railtypes); |
1698
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2267 |
|
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2268 |
if (ftd.best_trackdir == 0xff) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2269 |
/* We are already at our target. Just do something |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2270 |
* @todo maybe display error? |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2271 |
* @todo: go straight ahead if possible? */ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2272 |
best_track = FindFirstTrack(tracks); |
0 | 2273 |
} else { |
1698
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2274 |
/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains |
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2275 |
the direction we need to take to get there, if ftd.best_bird_dist is not 0, |
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2276 |
we did not find our target, but ftd.best_trackdir contains the direction leading |
bf364c664a60
(svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents:
1685
diff
changeset
|
2277 |
to the tile closest to our target. */ |
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
|
2278 |
if (ftd.best_bird_dist != 0) path_not_found = true; |
1247 | 2279 |
/* Discard enterdir information, making it a normal track */ |
2008
c9d6585c96c8
(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents:
2006
diff
changeset
|
2280 |
best_track = TrackdirToTrack(ftd.best_trackdir); |
0 | 2281 |
} |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2282 |
|
6476 | 2283 |
int time = NpfEndInterval(perf); |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5316
diff
changeset
|
2284 |
DEBUG(yapf, 4, "[NPFT] %d us - %d rounds - %d open - %d closed -- ", time, 0, _aystar_stats_open_size, _aystar_stats_closed_size); |
0 | 2285 |
} else { |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2286 |
void* perf = NpfBeginInterval(); |
6476 | 2287 |
|
2288 |
TrainTrackFollowerData fd; |
|
1247 | 2289 |
FillWithStationData(&fd, v); |
2290 |
||
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2291 |
/* New train pathfinding */ |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2292 |
fd.best_bird_dist = (uint)-1; |
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2293 |
fd.best_track_dist = (uint)-1; |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2294 |
fd.best_track = INVALID_TRACKDIR; |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2295 |
|
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4556
diff
changeset
|
2296 |
NewTrainPathfind(tile - TileOffsByDiagDir(enterdir), v->dest_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
|
2297 |
v->u.rail.compatible_railtypes, enterdir, (NTPEnumProc*)NtpCallbFindStation, &fd); |
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2298 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2299 |
/* check whether the path was found or 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
|
2300 |
if (fd.best_bird_dist != 0) path_not_found = true; |
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
|
2301 |
|
2044
68ec4a2f2d79
(svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents:
2032
diff
changeset
|
2302 |
if (fd.best_track == 0xff) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2303 |
/* blaha */ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2304 |
best_track = FindFirstTrack(tracks); |
1247 | 2305 |
} else { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2306 |
best_track = TrackdirToTrack(fd.best_track); |
1247 | 2307 |
} |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2308 |
|
6476 | 2309 |
int time = NpfEndInterval(perf); |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5316
diff
changeset
|
2310 |
DEBUG(yapf, 4, "[NTPT] %d us - %d rounds - %d open - %d closed -- ", time, 0, 0, 0); |
0 | 2311 |
} |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2312 |
/* 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
|
2313 |
if (path_not_found) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2314 |
/* PF didn't find the route */ |
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
|
2315 |
if (!HASBIT(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION)) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2316 |
/* it is first time the problem occurred, set the "path not found" flag */ |
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
|
2317 |
SETBIT(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION); |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2318 |
/* and notify user about the event */ |
4996
c20caeeb1aed
(svn r7003) -Fix: only the appropriate train owner can now see the "Train is lost" message (peter1138)
KUDr
parents:
4870
diff
changeset
|
2319 |
if (_patches.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
|
2320 |
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
|
2321 |
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
|
2322 |
STR_TRAIN_IS_LOST, |
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
|
2323 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 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
|
2324 |
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
|
2325 |
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
|
2326 |
} |
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
|
2327 |
} |
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
|
2328 |
} else { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2329 |
/* route found, is the train marked with "path not found" flag? */ |
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
|
2330 |
if (HASBIT(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION)) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2331 |
/* clear the flag as the PF's problem was solved */ |
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
|
2332 |
CLRBIT(v->u.rail.flags, VRF_NO_PATH_TO_DESTINATION); |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2333 |
/* 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
|
2334 |
} |
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
|
2335 |
} |
0 | 2336 |
|
2758
9cd8a21e9a7b
(svn r3303) Change #if PF_BENCHMARK to #ifdef PF_BENCHMARK
peter1138
parents:
2752
diff
changeset
|
2337 |
#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
|
2338 |
TOC("PF time = ", 1) |
0 | 2339 |
#endif |
2340 |
||
2341 |
return best_track; |
|
2342 |
} |
|
2343 |
||
2344 |
||
2345 |
static bool CheckReverseTrain(Vehicle *v) |
|
2346 |
{ |
|
2347 |
if (_opt.diff.line_reverse_mode != 0 || |
|
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2348 |
v->u.rail.track == TRACK_BIT_DEPOT || v->u.rail.track == TRACK_BIT_WORMHOLE || |
0 | 2349 |
!(v->direction & 1)) |
2350 |
return false; |
|
2351 |
||
6476 | 2352 |
TrainTrackFollowerData fd; |
0 | 2353 |
FillWithStationData(&fd, v); |
2354 |
||
6476 | 2355 |
uint reverse_best = 0; |
0 | 2356 |
|
2357 |
assert(v->u.rail.track); |
|
2358 |
||
6476 | 2359 |
int i = _search_directions[FIND_FIRST_BIT(v->u.rail.track)][DirToDiagDir(v->direction)]; |
0 | 2360 |
|
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2361 |
if (_patches.yapf.rail_use_yapf) { |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2362 |
reverse_best = YapfCheckReverseTrain(v); |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2363 |
} else if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */ |
1247 | 2364 |
NPFFindStationOrTileData fstd; |
2365 |
NPFFoundTargetData ftd; |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2366 |
Trackdir trackdir, trackdir_rev; |
1247 | 2367 |
Vehicle* last = GetLastVehicleInChain(v); |
2368 |
||
2369 |
NPFFillWithOrderData(&fstd, v); |
|
2370 |
||
1752
cdbfb2f23e72
(svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents:
1698
diff
changeset
|
2371 |
trackdir = GetVehicleTrackdir(v); |
1942
634961366cdc
(svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents:
1935
diff
changeset
|
2372 |
trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last)); |
1247 | 2373 |
assert(trackdir != 0xff); |
2374 |
assert(trackdir_rev != 0xff); |
|
2375 |
||
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
|
2376 |
ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, last->tile, trackdir_rev, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.compatible_railtypes); |
1247 | 2377 |
if (ftd.best_bird_dist != 0) { |
2378 |
/* We didn't find anything, just keep on going straight ahead */ |
|
2379 |
reverse_best = false; |
|
2380 |
} else { |
|
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
|
2381 |
if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) { |
1247 | 2382 |
reverse_best = true; |
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
|
2383 |
} else { |
1247 | 2384 |
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
|
2385 |
} |
1247 | 2386 |
} |
2387 |
} else { |
|
6476 | 2388 |
int best_track = -1; |
2389 |
uint reverse = 0; |
|
2390 |
uint best_bird_dist = 0; |
|
2391 |
uint best_track_dist = 0; |
|
2392 |
||
2952 | 2393 |
for (;;) { |
1247 | 2394 |
fd.best_bird_dist = (uint)-1; |
2395 |
fd.best_track_dist = (uint)-1; |
|
2396 |
||
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2397 |
NewTrainPathfind(v->tile, v->dest_tile, v->u.rail.compatible_railtypes, (DiagDirection)(reverse ^ i), (NTPEnumProc*)NtpCallbFindStation, &fd); |
1247 | 2398 |
|
2399 |
if (best_track != -1) { |
|
2400 |
if (best_bird_dist != 0) { |
|
2401 |
if (fd.best_bird_dist != 0) { |
|
2402 |
/* neither reached the destination, pick the one with the smallest bird dist */ |
|
2403 |
if (fd.best_bird_dist > best_bird_dist) goto bad; |
|
2404 |
if (fd.best_bird_dist < best_bird_dist) goto good; |
|
2405 |
} else { |
|
2406 |
/* we found the destination for the first time */ |
|
2407 |
goto good; |
|
2408 |
} |
|
0 | 2409 |
} else { |
1247 | 2410 |
if (fd.best_bird_dist != 0) { |
2411 |
/* didn't find destination, but we've found the destination previously */ |
|
2412 |
goto bad; |
|
2413 |
} else { |
|
2414 |
/* both old & new reached the destination, compare track length */ |
|
2415 |
if (fd.best_track_dist > best_track_dist) goto bad; |
|
2416 |
if (fd.best_track_dist < best_track_dist) goto good; |
|
2417 |
} |
|
0 | 2418 |
} |
1247 | 2419 |
|
2420 |
/* if we reach this position, there's two paths of equal value so far. |
|
2421 |
* pick one randomly. */ |
|
6476 | 2422 |
int r = GB(Random(), 0, 8); |
1247 | 2423 |
if (_pick_track_table[i] == (v->direction & 3)) r += 80; |
2424 |
if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80; |
|
2425 |
if (r <= 127) goto bad; |
|
0 | 2426 |
} |
1247 | 2427 |
good:; |
2428 |
best_track = i; |
|
2429 |
best_bird_dist = fd.best_bird_dist; |
|
2430 |
best_track_dist = fd.best_track_dist; |
|
2431 |
reverse_best = reverse; |
|
2432 |
bad:; |
|
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
|
2433 |
if (reverse != 0) break; |
1247 | 2434 |
reverse = 2; |
0 | 2435 |
} |
2436 |
} |
|
2437 |
||
2438 |
return reverse_best != 0; |
|
2439 |
} |
|
2440 |
||
2441 |
static bool ProcessTrainOrder(Vehicle *v) |
|
2442 |
{ |
|
3005
db4007baa0ab
(svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents:
2999
diff
changeset
|
2443 |
switch (v->current_order.type) { |
db4007baa0ab
(svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents:
2999
diff
changeset
|
2444 |
case OT_GOTO_DEPOT: |
db4007baa0ab
(svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents:
2999
diff
changeset
|
2445 |
if (!(v->current_order.flags & OF_PART_OF_ORDERS)) return false; |
db4007baa0ab
(svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents:
2999
diff
changeset
|
2446 |
if ((v->current_order.flags & OF_SERVICE_IF_NEEDED) && |
db4007baa0ab
(svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents:
2999
diff
changeset
|
2447 |
!VehicleNeedsService(v)) { |
db4007baa0ab
(svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents:
2999
diff
changeset
|
2448 |
v->cur_order_index++; |
db4007baa0ab
(svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents:
2999
diff
changeset
|
2449 |
} |
db4007baa0ab
(svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents:
2999
diff
changeset
|
2450 |
break; |
db4007baa0ab
(svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents:
2999
diff
changeset
|
2451 |
|
db4007baa0ab
(svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents:
2999
diff
changeset
|
2452 |
case OT_LOADING: |
db4007baa0ab
(svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents:
2999
diff
changeset
|
2453 |
case OT_LEAVESTATION: |
0 | 2454 |
return false; |
4351
63ae31104f07
(svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents:
4344
diff
changeset
|
2455 |
|
63ae31104f07
(svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents:
4344
diff
changeset
|
2456 |
default: break; |
0 | 2457 |
} |
2458 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2459 |
/* check if we've reached the waypoint? */ |
6476 | 2460 |
bool at_waypoint = false; |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2461 |
if (v->current_order.type == OT_GOTO_WAYPOINT && v->tile == v->dest_tile) { |
0 | 2462 |
v->cur_order_index++; |
3934
6d178534fbaf
(svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents:
3933
diff
changeset
|
2463 |
at_waypoint = true; |
0 | 2464 |
} |
2465 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2466 |
/* check if we've reached a non-stop station while TTDPatch nonstop is enabled.. */ |
2549 | 2467 |
if (_patches.new_nonstop && |
2468 |
v->current_order.flags & OF_NON_STOP && |
|
2469 |
IsTileType(v->tile, MP_STATION) && |
|
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
2470 |
v->current_order.dest == GetStationIndex(v->tile)) { |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2471 |
v->cur_order_index++; |
0 | 2472 |
} |
2473 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2474 |
/* Get the current order */ |
2639 | 2475 |
if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0; |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2476 |
|
6476 | 2477 |
const Order *order = GetVehicleOrder(v, v->cur_order_index); |
0 | 2478 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2479 |
/* If no order, do nothing. */ |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2480 |
if (order == NULL) { |
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
|
2481 |
v->current_order.Free(); |
0 | 2482 |
v->dest_tile = 0; |
2483 |
return false; |
|
2484 |
} |
|
2485 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2486 |
/* If it is unchanged, keep it. */ |
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
2487 |
if (order->type == v->current_order.type && |
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
2488 |
order->flags == v->current_order.flags && |
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
2489 |
order->dest == v->current_order.dest) |
0 | 2490 |
return false; |
2491 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2492 |
/* Otherwise set it, and determine the destination tile. */ |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2493 |
v->current_order = *order; |
0 | 2494 |
|
2495 |
v->dest_tile = 0; |
|
2496 |
||
3934
6d178534fbaf
(svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents:
3933
diff
changeset
|
2497 |
InvalidateVehicleOrder(v); |
6d178534fbaf
(svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents:
3933
diff
changeset
|
2498 |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2499 |
switch (order->type) { |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2500 |
case OT_GOTO_STATION: |
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
2501 |
if (order->dest == v->last_station_visited) |
1266
9dc5638fe8cc
(svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents:
1247
diff
changeset
|
2502 |
v->last_station_visited = INVALID_STATION; |
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
2503 |
v->dest_tile = GetStation(order->dest)->xy; |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2504 |
break; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2505 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2506 |
case OT_GOTO_DEPOT: |
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
2507 |
v->dest_tile = GetDepot(order->dest)->xy; |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2508 |
break; |
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2509 |
|
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2510 |
case OT_GOTO_WAYPOINT: |
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
2511 |
v->dest_tile = GetWaypoint(order->dest)->xy; |
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2512 |
break; |
3934
6d178534fbaf
(svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents:
3933
diff
changeset
|
2513 |
|
6d178534fbaf
(svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents:
3933
diff
changeset
|
2514 |
default: |
6d178534fbaf
(svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents:
3933
diff
changeset
|
2515 |
return false; |
0 | 2516 |
} |
2517 |
||
3934
6d178534fbaf
(svn r5071) - Fix (FS#184): "Erroneous train reversal on waypoints". When processing the next train order, do not even consider reversing the train if the last order was to a waypoint.
peter1138
parents:
3933
diff
changeset
|
2518 |
return !at_waypoint && CheckReverseTrain(v); |
0 | 2519 |
} |
2520 |
||
7049
01825af2ce90
(svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents:
7048
diff
changeset
|
2521 |
void Train::MarkDirty() |
0 | 2522 |
{ |
7049
01825af2ce90
(svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents:
7048
diff
changeset
|
2523 |
Vehicle *v = this; |
0 | 2524 |
do { |
2525 |
v->cur_image = GetTrainImage(v, v->direction); |
|
2526 |
MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1); |
|
2639 | 2527 |
} while ((v = v->next) != NULL); |
7049
01825af2ce90
(svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents:
7048
diff
changeset
|
2528 |
|
01825af2ce90
(svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents:
7048
diff
changeset
|
2529 |
/* need to update acceleration and cached values since the goods on the train changed. */ |
01825af2ce90
(svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents:
7048
diff
changeset
|
2530 |
TrainCargoChanged(this); |
01825af2ce90
(svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents:
7048
diff
changeset
|
2531 |
UpdateTrainAcceleration(this); |
0 | 2532 |
} |
2533 |
||
2534 |
static void HandleTrainLoading(Vehicle *v, bool mode) |
|
2535 |
{ |
|
6107 | 2536 |
switch (v->current_order.type) { |
6109
2361013ddd68
(svn r8434) -Fix (r8430): Missing braces caused compilation failure for some (most?) compilers...
peter1138
parents:
6107
diff
changeset
|
2537 |
case OT_LOADING: { |
6107 | 2538 |
if (mode) return; |
2539 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2540 |
/* don't mark the train as lost if we're loading on the final station. */ |
6107 | 2541 |
if (v->current_order.flags & OF_NON_STOP) { |
2542 |
v->u.rail.days_since_order_progr = 0; |
|
0 | 2543 |
} |
6107 | 2544 |
|
2545 |
if (--v->load_unload_time_rem) return; |
|
2546 |
||
7061
81d3a68972b5
(svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents:
7054
diff
changeset
|
2547 |
if (CanFillVehicle(v)) { |
81d3a68972b5
(svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents:
7054
diff
changeset
|
2548 |
LoadUnloadVehicle(v); |
6107 | 2549 |
return; |
2550 |
} |
|
2551 |
||
2552 |
TrainPlayLeaveStationSound(v); |
|
2553 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2554 |
Order b = v->current_order; |
5902
906fea1535ab
(svn r8110) -Codechange: direct Vehicle::current_order.type changes (to OT_LOADING and OT_LEAVESTATION) replaced by v->BeginLoading() and v->LeaveStation() calls. This should allow easy hooking of those state transitions in order to maintain vehicle loading queue.
KUDr
parents:
5853
diff
changeset
|
2555 |
v->LeaveStation(); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2556 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2557 |
/* If this was not the final order, don't remove it from the list. */ |
2639 | 2558 |
if (!(b.flags & OF_NON_STOP)) return; |
6107 | 2559 |
break; |
6109
2361013ddd68
(svn r8434) -Fix (r8430): Missing braces caused compilation failure for some (most?) compilers...
peter1138
parents:
6107
diff
changeset
|
2560 |
} |
6107 | 2561 |
|
2562 |
case OT_DUMMY: break; |
|
2563 |
||
2564 |
default: return; |
|
0 | 2565 |
} |
2566 |
||
2567 |
v->u.rail.days_since_order_progr = 0; |
|
2568 |
v->cur_order_index++; |
|
1024
9b06b01490a4
(svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents:
954
diff
changeset
|
2569 |
InvalidateVehicleOrder(v); |
0 | 2570 |
} |
2571 |
||
2572 |
static int UpdateTrainSpeed(Vehicle *v) |
|
2573 |
{ |
|
2574 |
uint accel; |
|
2575 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2576 |
if (v->vehstatus & VS_STOPPED || HASBIT(v->u.rail.flags, VRF_REVERSING)) { |
2639 | 2577 |
if (_patches.realistic_acceleration) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2578 |
accel = GetTrainAcceleration(v, AM_BRAKE) * 2; |
2639 | 2579 |
} else { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2580 |
accel = v->acceleration * -2; |
2639 | 2581 |
} |
0 | 2582 |
} else { |
2639 | 2583 |
if (_patches.realistic_acceleration) { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2584 |
accel = GetTrainAcceleration(v, AM_ACCEL); |
2639 | 2585 |
} else { |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2586 |
accel = v->acceleration; |
2639 | 2587 |
} |
0 | 2588 |
} |
2589 |
||
6476 | 2590 |
uint spd = v->subspeed + accel * 2; |
0 | 2591 |
v->subspeed = (byte)spd; |
1179
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2592 |
{ |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2593 |
int tempmax = v->max_speed; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2594 |
if (v->cur_speed > v->max_speed) |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2595 |
tempmax = v->cur_speed - (v->cur_speed / 10) - 1; |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2596 |
v->cur_speed = spd = clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax); |
abea5b669f74
(svn r1681) -Feature: New realistic acceleration.
celestar
parents:
1174
diff
changeset
|
2597 |
} |
0 | 2598 |
|
2599 |
if (!(v->direction & 1)) spd = spd * 3 >> 2; |
|
2600 |
||
2601 |
spd += v->progress; |
|
2602 |
v->progress = (byte)spd; |
|
2603 |
return (spd >> 8); |
|
2604 |
} |
|
2605 |
||
1551
eb05e6a90e67
(svn r2055) -CodeChange: Begun introducting StationID
celestar
parents:
1542
diff
changeset
|
2606 |
static void TrainEnterStation(Vehicle *v, StationID station) |
0 | 2607 |
{ |
2608 |
v->last_station_visited = station; |
|
2609 |
||
2610 |
/* check if a train ever visited this station before */ |
|
6476 | 2611 |
Station *st = GetStation(station); |
0 | 2612 |
if (!(st->had_vehicle_of_type & HVOT_TRAIN)) { |
2613 |
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
|
2614 |
SetDParam(0, st->index); |
6476 | 2615 |
uint32 flags = v->owner == _local_player ? |
2616 |
NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : |
|
2617 |
NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_VEHICLE, NT_ARRIVAL_OTHER, 0); |
|
0 | 2618 |
AddNewsItem( |
2619 |
STR_8801_CITIZENS_CELEBRATE_FIRST, |
|
2620 |
flags, |
|
2621 |
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
|
2622 |
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
|
2623 |
); |
0 | 2624 |
} |
2625 |
||
7046
5f11f759f74d
(svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents:
7042
diff
changeset
|
2626 |
v->BeginLoading(); |
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
2627 |
v->current_order.dest = 0; |
0 | 2628 |
} |
2629 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2630 |
static byte AfterSetTrainPos(Vehicle *v, bool new_tile) |
0 | 2631 |
{ |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2632 |
/* need this hint so it returns the right z coordinate on bridges. */ |
6476 | 2633 |
byte new_z = GetSlopeZ(v->x_pos, v->y_pos); |
2634 |
||
2635 |
byte old_z = v->z_pos; |
|
0 | 2636 |
v->z_pos = new_z; |
2637 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2638 |
if (new_tile) { |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2639 |
CLRBIT(v->u.rail.flags, VRF_GOINGUP); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2640 |
CLRBIT(v->u.rail.flags, VRF_GOINGDOWN); |
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2641 |
|
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2642 |
if (new_z != old_z) { |
1980
9ea0c89fbb58
(svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents:
1977
diff
changeset
|
2643 |
TileIndex tile = TileVirtXY(v->x_pos, v->y_pos); |
1683
2248e6615517
(svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents:
1681
diff
changeset
|
2644 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2645 |
/* XXX workaround, whole UP/DOWN detection needs overhaul */ |
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
|
2646 |
if (!IsTunnelTile(tile)) { |
1683
2248e6615517
(svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents:
1681
diff
changeset
|
2647 |
SETBIT(v->u.rail.flags, (new_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
|
2648 |
} |
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
2649 |
} |
0 | 2650 |
} |
2651 |
||
2652 |
VehiclePositionChanged(v); |
|
2653 |
EndVehicleMove(v); |
|
2654 |
return old_z; |
|
2655 |
} |
|
2656 |
||
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3154
diff
changeset
|
2657 |
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
|
2658 |
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
|
2659 |
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
|
2660 |
DIR_E , DIR_SE, DIR_S |
0 | 2661 |
}; |
2662 |
||
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3140
diff
changeset
|
2663 |
static Direction GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile) |
0 | 2664 |
{ |
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
|
2665 |
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
|
2666 |
TileX(new_tile) - TileX(old_tile) + 1; |
0 | 2667 |
assert(offs < 11); |
2668 |
return _new_vehicle_direction_table[offs]; |
|
2669 |
} |
|
2670 |
||
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3140
diff
changeset
|
2671 |
static Direction GetNewVehicleDirection(const Vehicle *v, int x, int y) |
0 | 2672 |
{ |
2673 |
uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1); |
|
2674 |
assert(offs < 11); |
|
2675 |
return _new_vehicle_direction_table[offs]; |
|
2676 |
} |
|
2677 |
||
2436
177cb6a8339f
(svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents:
2425
diff
changeset
|
2678 |
static int GetDirectionToVehicle(const Vehicle *v, int x, int y) |
0 | 2679 |
{ |
2680 |
byte offs; |
|
2681 |
||
2682 |
x -= v->x_pos; |
|
2683 |
if (x >= 0) { |
|
2684 |
offs = (x > 2) ? 0 : 1; |
|
2685 |
} else { |
|
2686 |
offs = (x < -2) ? 2 : 1; |
|
2687 |
} |
|
2688 |
||
2689 |
y -= v->y_pos; |
|
2690 |
if (y >= 0) { |
|
2691 |
offs += ((y > 2) ? 0 : 1) * 4; |
|
2692 |
} else { |
|
2693 |
offs += ((y < -2) ? 2 : 1) * 4; |
|
2694 |
} |
|
2695 |
||
2696 |
assert(offs < 11); |
|
2697 |
return _new_vehicle_direction_table[offs]; |
|
2698 |
} |
|
2699 |
||
2700 |
/* Check if the vehicle is compatible with the specified tile */ |
|
1048 | 2701 |
static bool CheckCompatibleRail(const Vehicle *v, TileIndex tile) |
0 | 2702 |
{ |
1048 | 2703 |
return |
2549 | 2704 |
IsTileOwner(tile, v->owner) && ( |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
2705 |
!IsFrontEngine(v) || |
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
|
2706 |
HASBIT(v->u.rail.compatible_railtypes, GetRailType(tile)) |
2549 | 2707 |
); |
0 | 2708 |
} |
2709 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2710 |
struct RailtypeSlowdownParams { |
0 | 2711 |
byte small_turn, large_turn; |
2712 |
byte z_up; // fraction to remove when moving up |
|
2713 |
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
|
2714 |
}; |
0 | 2715 |
|
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
|
2716 |
static const RailtypeSlowdownParams _railtype_slowdown[] = { |
0 | 2717 |
// normal accel |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2718 |
{256 / 4, 256 / 2, 256 / 4, 2}, ///< normal |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2719 |
{256 / 4, 256 / 2, 256 / 4, 2}, ///< electrified |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2720 |
{256 / 4, 256 / 2, 256 / 4, 2}, ///< monorail |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2721 |
{0, 256 / 2, 256 / 4, 2}, ///< maglev |
0 | 2722 |
}; |
2723 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2724 |
/** Modify the speed of the vehicle due to a turn */ |
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3154
diff
changeset
|
2725 |
static void AffectSpeedByDirChange(Vehicle* v, Direction new_dir) |
0 | 2726 |
{ |
3158
a821c8fec829
(svn r3784) Add a type and functions to handle direction changes
tron
parents:
3157
diff
changeset
|
2727 |
if (_patches.realistic_acceleration) return; |
a821c8fec829
(svn r3784) Add a type and functions to handle direction changes
tron
parents:
3157
diff
changeset
|
2728 |
|
6476 | 2729 |
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
|
2730 |
if (diff == DIRDIFF_SAME) return; |
0 | 2731 |
|
6476 | 2732 |
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
|
2733 |
v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8; |
0 | 2734 |
} |
2735 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2736 |
/** Modify the speed of the vehicle due to a change in altitude */ |
0 | 2737 |
static void AffectSpeedByZChange(Vehicle *v, byte old_z) |
2738 |
{ |
|
2639 | 2739 |
if (old_z == v->z_pos || _patches.realistic_acceleration) return; |
0 | 2740 |
|
6476 | 2741 |
const RailtypeSlowdownParams *rsp = &_railtype_slowdown[v->u.rail.railtype]; |
0 | 2742 |
|
2743 |
if (old_z < v->z_pos) { |
|
2744 |
v->cur_speed -= (v->cur_speed * rsp->z_up >> 8); |
|
2745 |
} else { |
|
2746 |
uint16 spd = v->cur_speed + rsp->z_down; |
|
2639 | 2747 |
if (spd <= v->max_speed) v->cur_speed = spd; |
0 | 2748 |
} |
2749 |
} |
|
2750 |
||
3172
1605cb59ccd9
(svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents:
3163
diff
changeset
|
2751 |
static const DiagDirection _otherside_signal_directions[] = { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2752 |
DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, INVALID_DIAGDIR, INVALID_DIAGDIR, |
3172
1605cb59ccd9
(svn r3803) Change the second parameter of UpdateSignalsOnSegment() from Direction to DiagDirection as that's what it really operates on
tron
parents:
3163
diff
changeset
|
2753 |
DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE |
0 | 2754 |
}; |
2755 |
||
3153
301c1d71122b
(svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3140
diff
changeset
|
2756 |
static void TrainMovedChangeSignals(TileIndex tile, DiagDirection dir) |
0 | 2757 |
{ |
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
|
2758 |
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
|
2759 |
GetRailTileType(tile) == RAIL_TILE_SIGNALS) { |
3269
95e7bffcb3c5
(svn r3981) More work for the rail accessing functions and enums
tron
parents:
3267
diff
changeset
|
2760 |
uint i = FindFirstBit2x64(GetTrackBits(tile) * 0x101 & _reachable_tracks[dir]); |
0 | 2761 |
UpdateSignalsOnSegment(tile, _otherside_signal_directions[i]); |
2762 |
} |
|
2763 |
} |
|
2764 |
||
2765 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2766 |
struct TrainCollideChecker { |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2767 |
const Vehicle *v; |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2768 |
const Vehicle *v_skip; |
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2769 |
}; |
0 | 2770 |
|
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2771 |
static void *FindTrainCollideEnum(Vehicle *v, void *data) |
0 | 2772 |
{ |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2773 |
const TrainCollideChecker* tcc = (TrainCollideChecker*)data; |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2774 |
|
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2775 |
if (v != tcc->v && |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2776 |
v != tcc->v_skip && |
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
|
2777 |
v->type == VEH_TRAIN && |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2778 |
v->u.rail.track != TRACK_BIT_DEPOT && |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2779 |
myabs(v->z_pos - tcc->v->z_pos) <= 6 && |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2780 |
myabs(v->x_pos - tcc->v->x_pos) < 6 && |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2781 |
myabs(v->y_pos - tcc->v->y_pos) < 6) { |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2782 |
return v; |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2783 |
} else { |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2784 |
return NULL; |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2785 |
} |
0 | 2786 |
} |
2787 |
||
2788 |
static void SetVehicleCrashed(Vehicle *v) |
|
2789 |
{ |
|
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
|
2790 |
if (v->u.rail.crash_anim_pos != 0) return; |
0 | 2791 |
|
2792 |
v->u.rail.crash_anim_pos++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2793 |
|
6476 | 2794 |
Vehicle *u = v; |
0 | 2795 |
BEGIN_ENUM_WAGONS(v) |
2796 |
v->vehstatus |= VS_CRASHED; |
|
2797 |
END_ENUM_WAGONS(v) |
|
2798 |
||
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
2799 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, u->index, STATUS_BAR); |
0 | 2800 |
} |
2801 |
||
2549 | 2802 |
static uint CountPassengersInTrain(const Vehicle* v) |
0 | 2803 |
{ |
2549 | 2804 |
uint num = 0; |
0 | 2805 |
BEGIN_ENUM_WAGONS(v) |
6660
da97dfc6ef5f
(svn r9322) -Codechange: Use cargo class to count crash/flood victims
peter1138
parents:
6648
diff
changeset
|
2806 |
if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) num += v->cargo_count; |
0 | 2807 |
END_ENUM_WAGONS(v) |
2808 |
return num; |
|
2809 |
} |
|
2810 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2811 |
/** |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2812 |
* 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
|
2813 |
* so, destroys this vehicle, and the other vehicle if its subtype has TS_Front. |
22 | 2814 |
* Reports the incident in a flashy news item, modifies station ratings and |
2815 |
* plays a sound. |
|
2816 |
*/ |
|
0 | 2817 |
static void CheckTrainCollision(Vehicle *v) |
2818 |
{ |
|
2819 |
/* can't collide in depot */ |
|
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2820 |
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
|
2821 |
|
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2822 |
assert(v->u.rail.track == TRACK_BIT_WORMHOLE || TileVirtXY(v->x_pos, v->y_pos) == v->tile); |
0 | 2823 |
|
6476 | 2824 |
TrainCollideChecker tcc; |
0 | 2825 |
tcc.v = v; |
2826 |
tcc.v_skip = v->next; |
|
2827 |
||
2828 |
/* find colliding vehicle */ |
|
6476 | 2829 |
Vehicle *realcoll = (Vehicle*)VehicleFromPos(TileVirtXY(v->x_pos, v->y_pos), &tcc, FindTrainCollideEnum); |
2639 | 2830 |
if (realcoll == NULL) return; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2831 |
|
6476 | 2832 |
Vehicle *coll = GetFirstVehicleInChain(realcoll); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2833 |
|
0 | 2834 |
/* it can't collide with its own wagons */ |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2835 |
if (v == coll || |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2836 |
(v->u.rail.track == TRACK_BIT_WORMHOLE && (v->direction & 2) != (realcoll->direction & 2))) |
0 | 2837 |
return; |
2838 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2839 |
/* two drivers + passangers killed in train v */ |
6476 | 2840 |
uint num = 2 + CountPassengersInTrain(v); |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2841 |
if (!(coll->vehstatus & VS_CRASHED)) |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2842 |
/* two drivers + passangers killed in train coll (if it was not crashed already) */ |
0 | 2843 |
num += 2 + CountPassengersInTrain(coll); |
2844 |
||
2845 |
SetVehicleCrashed(v); |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
2846 |
if (IsFrontEngine(coll)) SetVehicleCrashed(coll); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2847 |
|
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
2848 |
SetDParam(0, num); |
0 | 2849 |
AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL, |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2850 |
NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_VEHICLE, NT_ACCIDENT, 0), |
0 | 2851 |
v->index, |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2852 |
0 |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
2853 |
); |
0 | 2854 |
|
2855 |
ModifyStationRatingAround(v->tile, v->owner, -160, 30); |
|
541 | 2856 |
SndPlayVehicleFx(SND_13_BIG_CRASH, v); |
0 | 2857 |
} |
2858 |
||
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2859 |
struct VehicleAtSignalData { |
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
|
2860 |
TileIndex tile; |
3157
40de8616c04c
(svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents:
3154
diff
changeset
|
2861 |
Direction direction; |
6574
e1d1a12faaf7
(svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents:
6573
diff
changeset
|
2862 |
}; |
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
|
2863 |
|
0 | 2864 |
static void *CheckVehicleAtSignal(Vehicle *v, void *data) |
2865 |
{ |
|
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
2866 |
const VehicleAtSignalData* vasd = (VehicleAtSignalData*)data; |
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
|
2867 |
|
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
|
2868 |
if (v->type == VEH_TRAIN && IsFrontEngine(v) && v->tile == vasd->tile) { |
3158
a821c8fec829
(svn r3784) Add a type and functions to handle direction changes
tron
parents:
3157
diff
changeset
|
2869 |
DirDiff diff = ChangeDirDiff(DirDifference(v->direction, vasd->direction), DIRDIFF_90RIGHT); |
a821c8fec829
(svn r3784) Add a type and functions to handle direction changes
tron
parents:
3157
diff
changeset
|
2870 |
|
a821c8fec829
(svn r3784) Add a type and functions to handle direction changes
tron
parents:
3157
diff
changeset
|
2871 |
if (diff == DIRDIFF_90RIGHT || (v->cur_speed <= 5 && diff <= DIRDIFF_REVERSE)) return v; |
0 | 2872 |
} |
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
|
2873 |
return NULL; |
0 | 2874 |
} |
2875 |
||
5252
6823da581c30
(svn r7378) -Fix (r2428): Don't update vehicle images when turning a train around.
peter1138
parents:
5215
diff
changeset
|
2876 |
static void TrainController(Vehicle *v, bool update_image) |
0 | 2877 |
{ |
1961
10ce7350f76b
(svn r2467) - Fix: [newgrf] Prevent trains with shorter wagons breaking apart when reversing in some cases. (Therax)
hackykid
parents:
1942
diff
changeset
|
2878 |
Vehicle *prev; |
0 | 2879 |
|
22 | 2880 |
/* For every vehicle after and including the given vehicle */ |
1961
10ce7350f76b
(svn r2467) - Fix: [newgrf] Prevent trains with shorter wagons breaking apart when reversing in some cases. (Therax)
hackykid
parents:
1942
diff
changeset
|
2881 |
for (prev = GetPrevVehicleInChain(v); v != NULL; prev = v, v = v->next) { |
0 | 2882 |
BeginVehicleMove(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2883 |
|
6479 | 2884 |
GetNewVehiclePosResult gp = GetNewVehiclePos(v); |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2885 |
if (v->u.rail.track != TRACK_BIT_WORMHOLE) { |
22 | 2886 |
/* Not inside tunnel */ |
6478 | 2887 |
if (gp.old_tile == gp.new_tile) { |
22 | 2888 |
/* Staying in the old tile */ |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
2889 |
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
|
2890 |
/* Inside depot */ |
0 | 2891 |
gp.x = v->x_pos; |
2892 |
gp.y = v->y_pos; |
|
2893 |
} 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
|
2894 |
/* Not inside depot */ |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
2895 |
|
5919
2b58160d667d
(svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents:
5902
diff
changeset
|
2896 |
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
|
2897 |
|
6476 | 2898 |
uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
6318
5e3cf8b18775
(svn r8700) -Fix (8698): replaced a magic number with the wrong enum :(
rubidium
parents:
6317
diff
changeset
|
2899 |
if (HASBIT(r, VETS_CANNOT_ENTER)) { |
0 | 2900 |
goto invalid_rail; |
1247 | 2901 |
} |
6317
c73bda71ac16
(svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents:
6195
diff
changeset
|
2902 |
if (HASBIT(r, VETS_ENTERED_STATION)) { |
c73bda71ac16
(svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents:
6195
diff
changeset
|
2903 |
TrainEnterStation(v, r >> VETS_STATION_ID_OFFSET); |
0 | 2904 |
return; |
2905 |
} |
|
2906 |
||
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
2907 |
if (v->current_order.type == 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
|
2908 |
v->current_order.Free(); |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
2909 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 2910 |
} |
2911 |
} |
|
2912 |
} else { |
|
2913 |
/* A new tile is about to be entered. */ |
|
2914 |
||
2915 |
/* Determine what direction we're entering the new tile from */ |
|
6477 | 2916 |
Direction dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile); |
2917 |
DiagDirection 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
|
2918 |
assert(IsValidDiagDirection(enterdir)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2919 |
|
0 | 2920 |
/* Get the status of the tracks in the new tile and mask |
2921 |
* away the bits that aren't reachable. */ |
|
6477 | 2922 |
uint32 ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL) & _reachable_tracks[enterdir]; |
0 | 2923 |
|
2924 |
/* Combine the from & to directions. |
|
2925 |
* Now, the lower byte contains the track status, and the byte at bit 16 contains |
|
2926 |
* the signal status. */ |
|
6476 | 2927 |
uint32 tracks = ts | (ts >> 8); |
6477 | 2928 |
TrackBits bits = (TrackBits)(tracks & TRACK_BIT_MASK); |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
2929 |
if ((_patches.new_pathfinding_all || _patches.yapf.rail_use_yapf) && _patches.forbid_90_deg && prev == NULL) { |
1247 | 2930 |
/* We allow wagons to make 90 deg turns, because forbid_90_deg |
2931 |
* 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
|
2932 |
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
|
2933 |
} |
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
|
2934 |
|
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 |
if (bits == TRACK_BIT_NONE) goto invalid_rail; |
0 | 2936 |
|
2937 |
/* Check if the new tile contrains tracks that are compatible |
|
2938 |
* 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
|
2939 |
if (!CheckCompatibleRail(v, gp.new_tile)) goto invalid_rail; |
0 | 2940 |
|
6476 | 2941 |
TrackBits chosen_track; |
0 | 2942 |
if (prev == NULL) { |
2943 |
/* Currently the locomotive is active. Determine which one of the |
|
2944 |
* available tracks to choose */ |
|
6481 | 2945 |
chosen_track = TrackToTrackBits(ChooseTrainTrack(v, gp.new_tile, enterdir, bits)); |
1247 | 2946 |
assert(chosen_track & tracks); |
0 | 2947 |
|
2948 |
/* Check if it's a red signal and that force proceed is not clicked. */ |
|
6477 | 2949 |
if ((tracks >> 16) & chosen_track && v->u.rail.force_proceed == 0) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2950 |
/* In front of a red signal |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
2951 |
* find the first set bit in ts. need to do it in 2 steps, since |
6477 | 2952 |
* FIND_FIRST_BIT only handles 6 bits at a time. */ |
2953 |
Trackdir i = FindFirstTrackdir((TrackdirBits)(uint16)ts); |
|
2954 |
||
2955 |
if (!HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(i))) { |
|
2956 |
v->cur_speed = 0; |
|
2957 |
v->subspeed = 0; |
|
2958 |
v->progress = 255 - 100; |
|
2959 |
if (++v->load_unload_time_rem < _patches.wait_oneway_signal * 20) return; |
|
2960 |
} else if (HasSignalOnTrackdir(gp.new_tile, i)) { |
|
2961 |
v->cur_speed = 0; |
|
2962 |
v->subspeed = 0; |
|
6987
b0f13039bda2
(svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents:
6986
diff
changeset
|
2963 |
v->progress = 255 - 10; |
6477 | 2964 |
if (++v->load_unload_time_rem < _patches.wait_twoway_signal * 73) { |
2965 |
TileIndex o_tile = gp.new_tile + TileOffsByDiagDir(enterdir); |
|
2966 |
VehicleAtSignalData vasd; |
|
2967 |
vasd.tile = o_tile; |
|
2968 |
vasd.direction = ReverseDir(dir); |
|
2969 |
||
2970 |
/* check if a train is waiting on the other side */ |
|
2971 |
if (VehicleFromPos(o_tile, &vasd, CheckVehicleAtSignal) == NULL) return; |
|
2972 |
} |
|
2973 |
} |
|
2974 |
goto reverse_train_direction; |
|
2975 |
} |
|
0 | 2976 |
} 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
|
2977 |
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
|
2978 |
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
|
2979 |
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
|
2980 |
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
|
2981 |
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
|
2982 |
}; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2983 |
|
0 | 2984 |
/* 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
|
2985 |
chosen_track = (TrackBits)(byte)(_matching_tracks[GetDirectionToVehicle(prev, gp.x, gp.y)] & bits); |
0 | 2986 |
} |
2987 |
||
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
|
2988 |
/* 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
|
2989 |
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
|
2990 |
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
|
2991 |
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
|
2992 |
chosen_track == TRACK_BIT_LEFT || chosen_track == TRACK_BIT_RIGHT); |
0 | 2993 |
|
2994 |
/* Update XY to reflect the entrance to the new tile, and select the direction to use */ |
|
6476 | 2995 |
const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir]; |
2996 |
gp.x = (gp.x & ~0xF) | b[0]; |
|
2997 |
gp.y = (gp.y & ~0xF) | b[1]; |
|
2998 |
Direction chosen_dir = (Direction)b[2]; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
2999 |
|
0 | 3000 |
/* Call the landscape function and tell it that the vehicle entered the tile */ |
6476 | 3001 |
uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y); |
6317
c73bda71ac16
(svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents:
6195
diff
changeset
|
3002 |
if (HASBIT(r, VETS_CANNOT_ENTER)) { |
0 | 3003 |
goto invalid_rail; |
1247 | 3004 |
} |
0 | 3005 |
|
3657 | 3006 |
if (IsLevelCrossingTile(v->tile) && v->next == NULL) { |
3007 |
UnbarCrossing(v->tile); |
|
3008 |
MarkTileDirtyByTile(v->tile); |
|
3009 |
} |
|
3010 |
||
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
3011 |
if (IsFrontEngine(v)) v->load_unload_time_rem = 0; |
0 | 3012 |
|
6317
c73bda71ac16
(svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents:
6195
diff
changeset
|
3013 |
if (!HASBIT(r, VETS_ENTERED_WORMHOLE)) { |
0 | 3014 |
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
|
3015 |
|
6480 | 3016 |
if (GetTileRailType(gp.new_tile) != GetTileRailType(gp.old_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
|
3017 |
TrainPowerChanged(GetFirstVehicleInChain(v)); |
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
|
3018 |
} |
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
|
3019 |
|
0 | 3020 |
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
|
3021 |
assert(v->u.rail.track); |
0 | 3022 |
} |
3023 |
||
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
|
3024 |
if (IsFrontEngine(v)) TrainMovedChangeSignals(gp.new_tile, enterdir); |
0 | 3025 |
|
22 | 3026 |
/* Signals can only change when the first |
3027 |
* (above) or the last vehicle moves. */ |
|
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
|
3028 |
if (v->next == NULL) TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3029 |
|
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
|
3030 |
if (prev == NULL) AffectSpeedByDirChange(v, chosen_dir); |
0 | 3031 |
|
3032 |
v->direction = chosen_dir; |
|
3033 |
} |
|
3034 |
} 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
|
3035 |
/* In tunnel or on a bridge */ |
6467 | 3036 |
if (!(v->vehstatus & VS_HIDDEN)) { |
3037 |
v->cur_speed = |
|
3038 |
min(v->cur_speed, GetBridge(GetBridgeType(v->tile))->speed); |
|
3039 |
} |
|
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
3040 |
|
6317
c73bda71ac16
(svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents:
6195
diff
changeset
|
3041 |
if (!(IsTunnelTile(gp.new_tile) || IsBridgeTile(gp.new_tile)) || !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
|
3042 |
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
|
3043 |
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
|
3044 |
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
|
3045 |
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
|
3046 |
continue; |
0 | 3047 |
} |
3048 |
} |
|
3049 |
||
3050 |
/* update image of train, as well as delta XY */ |
|
6476 | 3051 |
Direction newdir = GetNewVehicleDirection(v, gp.x, gp.y); |
7054
edbb4d7765f2
(svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents:
7050
diff
changeset
|
3052 |
v->UpdateDeltaXY(newdir); |
5252
6823da581c30
(svn r7378) -Fix (r2428): Don't update vehicle images when turning a train around.
peter1138
parents:
5215
diff
changeset
|
3053 |
if (update_image) v->cur_image = GetTrainImage(v, newdir); |
0 | 3054 |
|
3055 |
v->x_pos = gp.x; |
|
3056 |
v->y_pos = gp.y; |
|
3057 |
||
3058 |
/* update the Z position of the vehicle */ |
|
6476 | 3059 |
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
|
3060 |
|
0 | 3061 |
if (prev == NULL) { |
22 | 3062 |
/* This is the first vehicle in the train */ |
0 | 3063 |
AffectSpeedByZChange(v, old_z); |
3064 |
} |
|
3065 |
} |
|
1438 | 3066 |
return; |
0 | 3067 |
|
3068 |
invalid_rail: |
|
22 | 3069 |
/* We've reached end of line?? */ |
2639 | 3070 |
if (prev != NULL) error("!Disconnecting train"); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3071 |
|
0 | 3072 |
reverse_train_direction: |
3073 |
v->load_unload_time_rem = 0; |
|
3074 |
v->cur_speed = 0; |
|
3075 |
v->subspeed = 0; |
|
3076 |
ReverseTrainDirection(v); |
|
3077 |
} |
|
3078 |
||
1430
68847f67a412
(svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents:
1418
diff
changeset
|
3079 |
extern TileIndex CheckTunnelBusy(TileIndex tile, uint *length); |
98 | 3080 |
|
1418
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3081 |
/** |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3082 |
* 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
|
3083 |
* 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
|
3084 |
* will remove the last wagon of a crashed train. If this wagon was on a crossing, |
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3085 |
* or inside a tunnel, recalculate the signals as they might need updating |
6980
6b5dee376733
(svn r9665) -Documentation: Doxygen corrections,errors, corrections of corrections...
belugas
parents:
6949
diff
changeset
|
3086 |
* @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
|
3087 |
*/ |
0 | 3088 |
static void DeleteLastWagon(Vehicle *v) |
3089 |
{ |
|
1418
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3090 |
/* 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
|
3091 |
* *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
|
3092 |
* one which will physicially be removed */ |
6476 | 3093 |
Vehicle *u = v; |
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
3094 |
for (; v->next != NULL; v = v->next) u = v; |
0 | 3095 |
u->next = NULL; |
3096 |
||
3097 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
3098 |
DeleteWindowById(WC_VEHICLE_VIEW, v->index); |
|
588 | 3099 |
RebuildVehicleLists(); |
0 | 3100 |
InvalidateWindow(WC_COMPANY, v->owner); |
3101 |
||
3102 |
BeginVehicleMove(v); |
|
3103 |
EndVehicleMove(v); |
|
3104 |
DeleteVehicle(v); |
|
3105 |
||
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
3106 |
if (v->u.rail.track != TRACK_BIT_DEPOT && v->u.rail.track != TRACK_BIT_WORMHOLE) |
1418
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3107 |
SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(v->u.rail.track)); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3108 |
|
1434
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
3109 |
/* Check if the wagon was on a road/rail-crossing and disable it if no |
f219d7bf2eb6
(svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents:
1432
diff
changeset
|
3110 |
* others are on it */ |
1103
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
3111 |
DisableTrainCrossing(v->tile); |
d3b5e10c6a7e
(svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents:
1095
diff
changeset
|
3112 |
|
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
3113 |
if ((v->u.rail.track == TRACK_BIT_WORMHOLE && v->vehstatus & VS_HIDDEN)) { // inside a tunnel |
1430
68847f67a412
(svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents:
1418
diff
changeset
|
3114 |
TileIndex endtile = CheckTunnelBusy(v->tile, NULL); |
68847f67a412
(svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents:
1418
diff
changeset
|
3115 |
|
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
|
3116 |
if (endtile == INVALID_TILE) return; // tunnel is busy (error returned) |
1418
1c235eb4197e
(svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents:
1394
diff
changeset
|
3117 |
|
1431
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3118 |
switch (v->direction) { |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3119 |
case 1: |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3120 |
case 5: |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3121 |
SetSignalsOnBothDir(v->tile, 0); |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3122 |
SetSignalsOnBothDir(endtile, 0); |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3123 |
break; |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3124 |
|
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3125 |
case 3: |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3126 |
case 7: |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3127 |
SetSignalsOnBothDir(v->tile, 1); |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3128 |
SetSignalsOnBothDir(endtile, 1); |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3129 |
break; |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3130 |
|
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3131 |
default: |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3132 |
break; |
93ab62952062
(svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents:
1430
diff
changeset
|
3133 |
} |
98 | 3134 |
} |
0 | 3135 |
} |
3136 |
||
3137 |
static void ChangeTrainDirRandomly(Vehicle *v) |
|
3138 |
{ |
|
3160 | 3139 |
static const DirDiff delta[] = { |
3140 |
DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT |
|
3141 |
}; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3142 |
|
0 | 3143 |
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
|
3144 |
/* 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
|
3145 |
if (!(v->vehstatus & VS_HIDDEN)) { |
3977
edb5b94e2094
(svn r5155) - Remove the bridge branch merge (revision r5070)
tron
parents:
3956
diff
changeset
|
3146 |
v->direction = ChangeDir(v->direction, delta[GB(Random(), 0, 2)]); |
0 | 3147 |
BeginVehicleMove(v); |
7054
edbb4d7765f2
(svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents:
7050
diff
changeset
|
3148 |
v->UpdateDeltaXY(v->direction); |
0 | 3149 |
v->cur_image = GetTrainImage(v, 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
|
3150 |
/* Refrain from updating the z position of the vehicle when on |
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
3151 |
a bridge, because AfterSetTrainPos will put the vehicle under |
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
3152 |
the bridge in that case */ |
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
3153 |
if (v->u.rail.track != TRACK_BIT_WORMHOLE) AfterSetTrainPos(v, false); |
0 | 3154 |
} |
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
|
3155 |
} while ((v = v->next) != NULL); |
0 | 3156 |
} |
3157 |
||
3158 |
static void HandleCrashedTrain(Vehicle *v) |
|
3159 |
{ |
|
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
|
3160 |
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
|
3161 |
|
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
3162 |
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
|
3163 |
CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE); |
0 | 3164 |
} |
3165 |
||
6476 | 3166 |
uint32 r; |
2637
8807e6a63f89
(svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents:
2635
diff
changeset
|
3167 |
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
|
3168 |
int index = (r * 10 >> 16); |
0 | 3169 |
|
6476 | 3170 |
Vehicle *u = v; |
0 | 3171 |
do { |
3172 |
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
|
3173 |
r = Random(); |
0 | 3174 |
|
3175 |
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
|
3176 |
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
|
3177 |
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
|
3178 |
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
|
3179 |
EV_EXPLOSION_SMALL); |
0 | 3180 |
break; |
3181 |
} |
|
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
|
3182 |
} while ((u = u->next) != NULL); |
0 | 3183 |
} |
3184 |
||
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
|
3185 |
if (state <= 240 && !(v->tick_counter & 3)) ChangeTrainDirRandomly(v); |
0 | 3186 |
|
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
|
3187 |
if (state >= 4440 && !(v->tick_counter&0x1F)) { |
0 | 3188 |
DeleteLastWagon(v); |
1128
d3ffc98b92ad
(svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents:
1109
diff
changeset
|
3189 |
} |
0 | 3190 |
} |
3191 |
||
3192 |
static void HandleBrokenTrain(Vehicle *v) |
|
3193 |
{ |
|
3194 |
if (v->breakdown_ctr != 1) { |
|
3195 |
v->breakdown_ctr = 1; |
|
3196 |
v->cur_speed = 0; |
|
3197 |
||
3198 |
if (v->breakdowns_since_last_service != 255) |
|
3199 |
v->breakdowns_since_last_service++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3200 |
|
0 | 3201 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
3202 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3203 |
|
4656
acffecd6f484
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4648
diff
changeset
|
3204 |
if (!PlayVehicleSound(v, VSE_BREAKDOWN)) { |
6683
7ec558346172
(svn r9400) -Codechange: Use some more representative enum names for landscape types.
belugas
parents:
6660
diff
changeset
|
3205 |
SndPlayVehicleFx((_opt.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
|
3206 |
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
|
3207 |
} |
0 | 3208 |
|
3209 |
if (!(v->vehstatus & VS_HIDDEN)) { |
|
3210 |
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE); |
|
2549 | 3211 |
if (u != NULL) u->u.special.unk0 = v->breakdown_delay * 2; |
0 | 3212 |
} |
3213 |
} |
|
3214 |
||
3215 |
if (!(v->tick_counter & 3)) { |
|
3216 |
if (!--v->breakdown_delay) { |
|
3217 |
v->breakdown_ctr = 0; |
|
3218 |
InvalidateWindow(WC_VEHICLE_VIEW, v->index); |
|
3219 |
} |
|
3220 |
} |
|
3221 |
} |
|
3222 |
||
3223 |
static const byte _breakdown_speeds[16] = { |
|
3224 |
225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15 |
|
3225 |
}; |
|
3226 |
||
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3227 |
static bool TrainCheckIfLineEnds(Vehicle *v) |
0 | 3228 |
{ |
6476 | 3229 |
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
|
3230 |
if (t > 1) { |
0 | 3231 |
v->vehstatus |= VS_TRAIN_SLOWING; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3232 |
|
6476 | 3233 |
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
|
3234 |
if (break_speed < v->cur_speed) v->cur_speed = break_speed; |
0 | 3235 |
} else { |
3236 |
v->vehstatus &= ~VS_TRAIN_SLOWING; |
|
3237 |
} |
|
3238 |
||
6319
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
3239 |
if (v->u.rail.track == TRACK_BIT_WORMHOLE) return true; // exit if inside a tunnel |
27e68b914d3d
(svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents:
6318
diff
changeset
|
3240 |
if (v->u.rail.track == TRACK_BIT_DEPOT) return true; // exit if inside a depot |
2008
c9d6585c96c8
(svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents:
2006
diff
changeset
|
3241 |
|
6476 | 3242 |
TileIndex tile = v->tile; |
0 | 3243 |
|
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
3244 |
if (IsTileType(tile, MP_TUNNELBRIDGE)) { |
6476 | 3245 |
DiagDirection dir = IsTunnel(tile) ? GetTunnelDirection(tile) : GetBridgeRampDirection(tile); |
5573
afa6f92a71fd
(svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents:
5568
diff
changeset
|
3246 |
if (DiagDirToDir(dir) == v->direction) return true; |
3154 | 3247 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3248 |
|
0 | 3249 |
// depot? |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3250 |
/* XXX -- When enabled, this makes it possible to crash trains of others |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3251 |
(by building a depot right against a station) */ |
4632
d74da5d7683b
(svn r6496) -Codechange: removed direct map access in train_cmd.c (Rubidium)
glx
parents:
4574
diff
changeset
|
3252 |
/* if (IsTileType(tile, MP_RAILWAY) && GetRailTileType(tile) == RAIL_TILE_DEPOT_WAYPOINT) |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3253 |
return true;*/ |
0 | 3254 |
|
22 | 3255 |
/* Determine the non-diagonal direction in which we will exit this tile */ |
6476 | 3256 |
DiagDirection dir = DirToDiagDir(v->direction); |
3163
129a91e147b8
(svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents:
3161
diff
changeset
|
3257 |
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[dir]) { |
129a91e147b8
(svn r3789) Add an enum and function to handle DiagDirection changes
tron
parents:
3161
diff
changeset
|
3258 |
dir = ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT); |
0 | 3259 |
} |
22 | 3260 |
/* Calculate next tile */ |
4559
c853d2440065
(svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents:
4556
diff
changeset
|
3261 |
tile += TileOffsByDiagDir(dir); |
22 | 3262 |
// determine the track status on the next tile. |
6476 | 3263 |
uint32 ts = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _reachable_tracks[dir]; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3264 |
|
22 | 3265 |
/* Calc position within the current tile ?? */ |
6476 | 3266 |
uint x = v->x_pos & 0xF; |
3267 |
uint y = v->y_pos & 0xF; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3268 |
|
2639 | 3269 |
switch (v->direction) { |
3186
5798593c2d95
(svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents:
3185
diff
changeset
|
3270 |
case DIR_N : x = ~x + ~y + 24; break; |
5798593c2d95
(svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents:
3185
diff
changeset
|
3271 |
case DIR_NW: x = y; /* FALLTHROUGH */ |
5798593c2d95
(svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents:
3185
diff
changeset
|
3272 |
case DIR_NE: x = ~x + 16; break; |
5798593c2d95
(svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents:
3185
diff
changeset
|
3273 |
case DIR_E : x = ~x + y + 8; break; |
5798593c2d95
(svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents:
3185
diff
changeset
|
3274 |
case DIR_SE: x = y; break; |
5798593c2d95
(svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents:
3185
diff
changeset
|
3275 |
case DIR_S : x = x + y - 8; break; |
5798593c2d95
(svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents:
3185
diff
changeset
|
3276 |
case DIR_W : x = ~y + x + 8; break; |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
3277 |
default: break; |
0 | 3278 |
} |
3279 |
||
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
|
3280 |
if (GB(ts, 0, 16) != 0) { |
2874
30c79e6e1c3e
(svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents:
2855
diff
changeset
|
3281 |
/* If we approach a rail-piece which we can't enter, or the back of a depot, don't enter it! */ |
3645
86af43f87885
(svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents:
3636
diff
changeset
|
3282 |
if (x + 4 >= TILE_SIZE && |
2874
30c79e6e1c3e
(svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents:
2855
diff
changeset
|
3283 |
(!CheckCompatibleRail(v, tile) || |
30c79e6e1c3e
(svn r3422) - Fix: If an engine reaches the rear of a depot, make the engine turn around before it reaches the tile end (same as a normal end of line.) This fixes a bug whereby the engine could poke into the depot and crash with another engine just leaving it.
peter1138
parents:
2855
diff
changeset
|
3284 |
(IsTileDepotType(tile, TRANSPORT_RAIL) && |
3185 | 3285 |
GetRailDepotDirection(tile) == dir))) { |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3286 |
v->cur_speed = 0; |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3287 |
ReverseTrainDirection(v); |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3288 |
return false; |
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3289 |
} |
0 | 3290 |
if ((ts &= (ts >> 16)) == 0) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
3291 |
/* make a rail/road crossing red */ |
3560
4389ae5ad967
(svn r4435) - Fix: an assertion triggered when trying to remove a bridge with the remove-tool (r4348 surfaced this). In CmdRemoveRoad tiletype was not checked for ownership. Intorudce IsLevelCrossingTile() which checks if a tile is a crossing without knowing the type. Suggested by peter1138 and Tron.
Darkvater
parents:
3546
diff
changeset
|
3292 |
if (IsLevelCrossingTile(tile)) { |
3322
fa5d46929db9
(svn r4088) -Codechange: Introduce {Unb,B}arCrossing and IsCrossingBarred to put and get the status of a level crossing
celestar
parents:
3315
diff
changeset
|
3293 |
if (!IsCrossingBarred(tile)) { |
fa5d46929db9
(svn r4088) -Codechange: Introduce {Unb,B}arCrossing and IsCrossingBarred to put and get the status of a level crossing
celestar
parents:
3315
diff
changeset
|
3294 |
BarCrossing(tile); |
541 | 3295 |
SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v); |
0 | 3296 |
MarkTileDirtyByTile(tile); |
3297 |
} |
|
3298 |
} |
|
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3299 |
return true; |
0 | 3300 |
} |
3645
86af43f87885
(svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents:
3636
diff
changeset
|
3301 |
} else if (x + 4 >= TILE_SIZE) { |
0 | 3302 |
v->cur_speed = 0; |
3303 |
ReverseTrainDirection(v); |
|
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3304 |
return false; |
0 | 3305 |
} |
3306 |
||
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
3307 |
/* slow down */ |
0 | 3308 |
v->vehstatus |= VS_TRAIN_SLOWING; |
6476 | 3309 |
uint16 break_speed = _breakdown_speeds[x & 0xF]; |
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
|
3310 |
if (!(v->direction & 1)) break_speed >>= 1; |
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
|
3311 |
if (break_speed < v->cur_speed) v->cur_speed = break_speed; |
742
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3312 |
|
60511ced2372
(svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents:
737
diff
changeset
|
3313 |
return true; |
0 | 3314 |
} |
3315 |
||
3316 |
static void TrainLocoHandler(Vehicle *v, bool mode) |
|
3317 |
{ |
|
3318 |
/* train has crashed? */ |
|
3319 |
if (v->u.rail.crash_anim_pos != 0) { |
|
3320 |
if (!mode) HandleCrashedTrain(v); |
|
3321 |
return; |
|
3322 |
} |
|
3323 |
||
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
|
3324 |
if (v->u.rail.force_proceed != 0) v->u.rail.force_proceed--; |
0 | 3325 |
|
3326 |
/* train is broken down? */ |
|
3327 |
if (v->breakdown_ctr != 0) { |
|
3328 |
if (v->breakdown_ctr <= 2) { |
|
3329 |
HandleBrokenTrain(v); |
|
3330 |
return; |
|
3331 |
} |
|
3332 |
v->breakdown_ctr--; |
|
3333 |
} |
|
3334 |
||
954
4d052517a993
(svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents:
926
diff
changeset
|
3335 |
if (HASBIT(v->u.rail.flags, VRF_REVERSING) && v->cur_speed == 0) { |
0 | 3336 |
ReverseTrainDirection(v); |
3337 |
} |
|
3338 |
||
3339 |
/* 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
|
3340 |
if (v->vehstatus & VS_STOPPED && v->cur_speed == 0) return; |
0 | 3341 |
|
3342 |
if (ProcessTrainOrder(v)) { |
|
3343 |
v->load_unload_time_rem = 0; |
|
3344 |
v->cur_speed = 0; |
|
3345 |
v->subspeed = 0; |
|
3346 |
ReverseTrainDirection(v); |
|
3347 |
return; |
|
3348 |
} |
|
3349 |
||
3350 |
HandleTrainLoading(v, mode); |
|
3351 |
||
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
|
3352 |
if (v->current_order.type == OT_LOADING) return; |
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
|
3353 |
|
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
|
3354 |
if (CheckTrainStayInDepot(v)) return; |
0 | 3355 |
|
3356 |
if (!mode) HandleLocomotiveSmokeCloud(v); |
|
3357 |
||
6476 | 3358 |
int j = UpdateTrainSpeed(v); |
0 | 3359 |
if (j == 0) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
3360 |
/* 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
|
3361 |
if (v->cur_speed != 0) return; |
0 | 3362 |
} else { |
3363 |
TrainCheckIfLineEnds(v); |
|
3364 |
||
3365 |
do { |
|
5252
6823da581c30
(svn r7378) -Fix (r2428): Don't update vehicle images when turning a train around.
peter1138
parents:
5215
diff
changeset
|
3366 |
TrainController(v, true); |
1922
797081e56d13
(svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents:
1921
diff
changeset
|
3367 |
CheckTrainCollision(v); |
0 | 3368 |
if (v->cur_speed <= 0x100) |
3369 |
break; |
|
3370 |
} while (--j != 0); |
|
3371 |
} |
|
3372 |
||
3373 |
SetLastSpeed(v, v->cur_speed); |
|
3374 |
} |
|
3375 |
||
3376 |
||
3377 |
void Train_Tick(Vehicle *v) |
|
3378 |
{ |
|
3379 |
if (_age_cargo_skip_counter == 0 && v->cargo_days != 0xff) |
|
3380 |
v->cargo_days++; |
|
3381 |
||
3382 |
v->tick_counter++; |
|
3383 |
||
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
3384 |
if (IsFrontEngine(v)) { |
0 | 3385 |
TrainLocoHandler(v, false); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3386 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
3387 |
/* make sure vehicle wasn't deleted. */ |
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
|
3388 |
if (v->type == VEH_TRAIN && IsFrontEngine(v)) |
0 | 3389 |
TrainLocoHandler(v, true); |
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
3390 |
} else if (IsFreeWagon(v) && HASBITS(v->vehstatus, VS_CRASHED)) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
3391 |
/* Delete flooded standalone wagon */ |
1132
7378651941bf
(svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents:
1128
diff
changeset
|
3392 |
if (++v->u.rail.crash_anim_pos >= 4400) |
7378651941bf
(svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents:
1128
diff
changeset
|
3393 |
DeleteVehicle(v); |
0 | 3394 |
} |
3395 |
} |
|
3396 |
||
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
3397 |
#define MAX_ACCEPTABLE_DEPOT_DIST 16 |
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
3870
diff
changeset
|
3398 |
|
0 | 3399 |
static void CheckIfTrainNeedsService(Vehicle *v) |
3400 |
{ |
|
2639 | 3401 |
if (_patches.servint_trains == 0) return; |
3402 |
if (!VehicleNeedsService(v)) return; |
|
3403 |
if (v->vehstatus & VS_STOPPED) return; |
|
3404 |
if (_patches.gotodepot && VehicleHasDepotOrders(v)) return; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3405 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
3406 |
/* Don't interfere with a depot visit scheduled by the user, or a |
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
3407 |
* depot visit by the order list. */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3408 |
if (v->current_order.type == OT_GOTO_DEPOT && |
1530
caa16c506a22
(svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents:
1520
diff
changeset
|
3409 |
(v->current_order.flags & (OF_HALT_IN_DEPOT | OF_PART_OF_ORDERS)) != 0) |
0 | 3410 |
return; |
3411 |
||
4529
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
3412 |
if (CheckTrainIsInsideDepot(v)) { |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
3413 |
VehicleServiceInDepot(v); |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
3414 |
return; |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
3415 |
} |
2bfac24af02a
(svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents:
4527
diff
changeset
|
3416 |
|
6476 | 3417 |
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
|
3418 |
/* 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
|
3419 |
if (tfdd.best_length == (uint)-1 || tfdd.best_length > MAX_ACCEPTABLE_DEPOT_DIST) { |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3420 |
if (v->current_order.type == OT_GOTO_DEPOT) { |
308
1d39f4e3eab1
(svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents:
300
diff
changeset
|
3421 |
/* 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
|
3422 |
* 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
|
3423 |
* schedule? */ |
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3424 |
v->current_order.type = OT_DUMMY; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3425 |
v->current_order.flags = 0; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
3426 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 3427 |
} |
3428 |
return; |
|
3429 |
} |
|
3430 |
||
6476 | 3431 |
const Depot* depot = GetDepotByTile(tfdd.tile); |
0 | 3432 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3433 |
if (v->current_order.type == OT_GOTO_DEPOT && |
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
3434 |
v->current_order.dest != depot->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
|
3435 |
!CHANCE16(3, 16)) { |
0 | 3436 |
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
|
3437 |
} |
0 | 3438 |
|
7050
6120cd416583
(svn r9756) -Fix [FS#755]: LeaveStation wasn't always called when a station was left.
rubidium
parents:
7049
diff
changeset
|
3439 |
if (v->current_order.type == OT_LOADING) v->LeaveStation(); |
6120cd416583
(svn r9756) -Fix [FS#755]: LeaveStation wasn't always called when a station was left.
rubidium
parents:
7049
diff
changeset
|
3440 |
|
555
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3441 |
v->current_order.type = OT_GOTO_DEPOT; |
eec6c0294435
(svn r955) Replace uint16 for orders with struct Order
tron
parents:
541
diff
changeset
|
3442 |
v->current_order.flags = OF_NON_STOP; |
4527
b18634a31a4a
(svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents:
4526
diff
changeset
|
3443 |
v->current_order.dest = depot->index; |
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
|
3444 |
v->dest_tile = tfdd.tile; |
755
823358e83abf
(svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents:
744
diff
changeset
|
3445 |
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR); |
0 | 3446 |
} |
3447 |
||
1790
4afb4b4e4278
(svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents:
1786
diff
changeset
|
3448 |
int32 GetTrainRunningCost(const Vehicle *v) |
0 | 3449 |
{ |
3450 |
int32 cost = 0; |
|
3451 |
||
3452 |
do { |
|
540
fbed599dadbb
(svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents:
534
diff
changeset
|
3453 |
const RailVehicleInfo *rvi = RailVehInfo(v->engine_type); |
6986
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
3454 |
|
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
3455 |
byte cost_factor = GetVehicleProperty(v, 0x0D, rvi->running_cost_base); |
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
3456 |
if (cost_factor == 0) continue; |
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
3457 |
|
168d3add1f13
(svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents:
6980
diff
changeset
|
3458 |
cost += cost_factor * _price.running_rail[rvi->running_cost_class]; |
4839
a8b963413d15
(svn r6765) - Fix: Don't add up running cost of articulated engine parts.
peter1138
parents:
4831
diff
changeset
|
3459 |
} while ((v = GetNextVehicle(v)) != NULL); |
0 | 3460 |
|
3461 |
return cost; |
|
3462 |
} |
|
3463 |
||
3464 |
void OnNewDay_Train(Vehicle *v) |
|
3465 |
{ |
|
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
|
3466 |
if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v); |
0 | 3467 |
|
2676
2ba71e034d97
(svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents:
2666
diff
changeset
|
3468 |
if (IsFrontEngine(v)) { |
0 | 3469 |
CheckVehicleBreakdown(v); |
3470 |
AgeVehicle(v); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3471 |
|
0 | 3472 |
CheckIfTrainNeedsService(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3473 |
|
3140
69cb681c6d86
(svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents:
3139
diff
changeset
|
3474 |
CheckOrders(v); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
166
diff
changeset
|
3475 |
|
0 | 3476 |
/* update destination */ |
6476 | 3477 |
if (v->current_order.type == OT_GOTO_STATION) { |
3478 |
TileIndex tile = GetStation(v->current_order.dest)->train_tile; |
|
3479 |
if (tile != 0) v->dest_tile = tile; |
|
2639 | 3480 |
} |
0 | 3481 |
|
3482 |
if ((v->vehstatus & VS_STOPPED) == 0) { |
|
3483 |
/* running costs */ |
|
3484 |
int32 cost = GetTrainRunningCost(v) / 364; |
|
3485 |
||
3486 |
v->profit_this_year -= cost >> 8; |
|
3487 |
||
3488 |
SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN); |
|
3489 |
SubtractMoneyFromPlayerFract(v->owner, cost); |
|
3490 |
||
3491 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
1151
614cbcb5b9fe
(svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents:
1137
diff
changeset
|
3492 |
InvalidateWindowClasses(WC_TRAINS_LIST); |
0 | 3493 |
} |
3494 |
} |
|
3495 |
} |
|
3496 |
||
6573 | 3497 |
void TrainsYearlyLoop() |
0 | 3498 |
{ |
3499 |
Vehicle *v; |
|
3500 |
||
3501 |
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
|
3502 |
if (v->type == VEH_TRAIN && IsFrontEngine(v)) { |
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
3503 |
/* show warning if train is not generating enough income last 2 years (corresponds to a red icon in the vehicle list) */ |
0 | 3504 |
if (_patches.train_income_warn && v->owner == _local_player && v->age >= 730 && v->profit_this_year < 0) { |
534
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
3505 |
SetDParam(1, v->profit_this_year); |
17ab2f22ff74
(svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents:
507
diff
changeset
|
3506 |
SetDParam(0, v->unitnumber); |
0 | 3507 |
AddNewsItem( |
3508 |
STR_TRAIN_IS_UNPROFITABLE, |
|
3509 |
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), |
|
3510 |
v->index, |
|
3511 |
0); |
|
3512 |
} |
|
3513 |
||
3514 |
v->profit_last_year = v->profit_this_year; |
|
3515 |
v->profit_this_year = 0; |
|
3516 |
InvalidateWindow(WC_VEHICLE_DETAILS, v->index); |
|
3517 |
} |
|
3518 |
} |
|
3519 |
} |
|
3520 |
||
3521 |
||
6573 | 3522 |
void InitializeTrains() |
0 | 3523 |
{ |
3524 |
_age_cargo_skip_counter = 1; |
|
3525 |
} |
|
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3526 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3527 |
/* |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3528 |
* 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
|
3529 |
* 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
|
3530 |
*/ |
6573 | 3531 |
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
|
3532 |
{ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3533 |
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
|
3534 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3535 |
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
|
3536 |
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
|
3537 |
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
|
3538 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3539 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3540 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3541 |
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
|
3542 |
if (v->type == VEH_TRAIN && IsFrontEngine(v)) { |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3543 |
Vehicle *u = v; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3544 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3545 |
BEGIN_ENUM_WAGONS(u) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3546 |
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
|
3547 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3548 |
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
|
3549 |
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
|
3550 |
/* 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
|
3551 |
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
|
3552 |
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
|
3553 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3554 |
|
6476 | 3555 |
Vehicle *w; |
3556 |
for (w = u->next; w != NULL && (w->engine_type != u->engine_type || w->u.rail.other_multiheaded_part != NULL); w = GetNextVehicle(w)); |
|
3557 |
if (w != NULL) { |
|
3558 |
/* we found a car to partner with this engine. Now we will make sure it face the right way */ |
|
3559 |
if (IsTrainEngine(w)) { |
|
3560 |
ClearTrainEngine(w); |
|
3561 |
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
|
3562 |
} |
6476 | 3563 |
w->u.rail.other_multiheaded_part = u; |
3564 |
u->u.rail.other_multiheaded_part = w; |
|
3565 |
} else { |
|
3566 |
/* we got a front car and no rear cars. We will fake this one for forget that it should have been multiheaded */ |
|
3567 |
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
|
3568 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3569 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3570 |
} END_ENUM_WAGONS(u) |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3571 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3572 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3573 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3574 |
|
6918
5589c415e28f
(svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas
parents:
6903
diff
changeset
|
3575 |
/** |
2855
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3576 |
* 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
|
3577 |
* 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
|
3578 |
*/ |
6573 | 3579 |
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
|
3580 |
{ |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3581 |
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
|
3582 |
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
|
3583 |
if (v->type == VEH_TRAIN) { |
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
|
3584 |
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
|
3585 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3586 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3587 |
|
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3588 |
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
|
3589 |
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
|
3590 |
if (HASBIT(v->subtype, 7) && ((v->subtype & ~0x80) == 0 || (v->subtype & ~0x80) == 4)) { |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3591 |
Vehicle *u = v; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3592 |
|
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
|
3593 |
BEGIN_ENUM_WAGONS(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
|
3594 |
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
|
3595 |
|
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
|
3596 |
CLRBIT(u->subtype, 7); |
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
|
3597 |
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
|
3598 |
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
|
3599 |
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
|
3600 |
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
|
3601 |
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
|
3602 |
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
|
3603 |
|
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
|
3604 |
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
|
3605 |
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
|
3606 |
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
|
3607 |
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
|
3608 |
|
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
|
3609 |
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
|
3610 |
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
|
3611 |
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
|
3612 |
// 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
|
3613 |
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
|
3614 |
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
|
3615 |
} |
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
|
3616 |
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
|
3617 |
// 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
|
3618 |
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
|
3619 |
break; |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3620 |
} |
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
|
3621 |
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
|
3622 |
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
|
3623 |
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
|
3624 |
|
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
|
3625 |
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
|
3626 |
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
|
3627 |
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
|
3628 |
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
|
3629 |
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
|
3630 |
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
|
3631 |
} |
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
|
3632 |
} END_ENUM_WAGONS(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
|
3633 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3634 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3635 |
} |
950b5a56f9d5
(svn r3403) -Codechange: [multiheaded engines] the references between the front and rear engines are no longer saved
bjarni
parents:
2854
diff
changeset
|
3636 |
} |