annotate src/train_cmd.cpp @ 20729:ea20edff1862 draft default tip master

(svn r25643) -Fix (r25480): [OSX] Define version constants before they're used
author planetmaker <planetmaker@openttd.org>
date Wed, 31 Jul 2013 18:50:01 +0000
parents cecc29b45313
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1 /* $Id$ */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2
12778
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12705
diff changeset
3 /*
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12705
diff changeset
4 * This file is part of OpenTTD.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12705
diff changeset
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12705
diff changeset
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12705
diff changeset
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12705
diff changeset
8 */
bc7926153e19 (svn r17248) -Fix: add GPL license notice where appropriate
rubidium <rubidium@openttd.org>
parents: 12705
diff changeset
9
9111
d48433370037 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium <rubidium@openttd.org>
parents: 9070
diff changeset
10 /** @file train_cmd.cpp Handling of trains. */
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
11
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
12 #include "stdafx.h"
18627
1972f6346144 (svn r23474) -Codechange: move the declaration error related functions to error.h
rubidium <rubidium@openttd.org>
parents: 18607
diff changeset
13 #include "error.h"
6772
5d92b1c91256 (svn r10008) -Codechange: Move a couple of functions related to articulated vehicles to a file of their own.
maedhros <maedhros@openttd.org>
parents: 6771
diff changeset
14 #include "articulated_vehicles.h"
8116
9cc845deddfe (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium <rubidium@openttd.org>
parents: 8114
diff changeset
15 #include "command_func.h"
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
16 #include "pathfinder/npf/npf_func.h"
13890
32e58ed16e0b (svn r18420) -Codechange: split YAPF's track follower from the actual YAPF code
rubidium <rubidium@openttd.org>
parents: 13873
diff changeset
17 #include "pathfinder/yapf/yapf.hpp"
8763
d6e363672edb (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium <rubidium@openttd.org>
parents: 8712
diff changeset
18 #include "news_func.h"
10208
39cf8eebfda5 (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium <rubidium@openttd.org>
parents: 10207
diff changeset
19 #include "company_func.h"
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
20 #include "newgrf_sound.h"
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
21 #include "newgrf_text.h"
8114
2d6af5d7a142 (svn r11675) -Codechange: split the string types from the string functions.
rubidium <rubidium@openttd.org>
parents: 8108
diff changeset
22 #include "strings_func.h"
17277
5b204f51b151 (svn r22017) -Codechange: move MarkTileDirtyByTile to viewport_func.h
rubidium <rubidium@openttd.org>
parents: 17236
diff changeset
23 #include "viewport_func.h"
8144
d18c8a0bb638 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium <rubidium@openttd.org>
parents: 8143
diff changeset
24 #include "vehicle_func.h"
8157
019833e42fda (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium <rubidium@openttd.org>
parents: 8145
diff changeset
25 #include "sound_func.h"
10696
7edccbb67398 (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
truebrain <truebrain@openttd.org>
parents: 10690
diff changeset
26 #include "ai/ai.hpp"
18774
1fa19d70aea9 (svn r23622) -Add: a set of events to trigger in a GameScript
truebrain <truebrain@openttd.org>
parents: 18692
diff changeset
27 #include "game/game.hpp"
9003
ac23e012c9d7 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138 <peter1138@openttd.org>
parents: 9000
diff changeset
28 #include "newgrf_station.h"
9009
870efbdb988d (svn r12804) -Codechange: move the effect vehicle handling out of vehicle.cpp
rubidium <rubidium@openttd.org>
parents: 9008
diff changeset
29 #include "effectvehicle_func.h"
9704
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
30 #include "network/network.h"
13599
28fe769d10b8 (svn r18123) -Codechange: try our best to keep the vehicles within the build vehicle list/autoreplace lists instead of overflowing.
rubidium <rubidium@openttd.org>
parents: 13597
diff changeset
31 #include "spritecache.h"
14258
a899d4e5ee1a (svn r18809) -Codechange/Cleanup: remove unneeded headers from some files, if a header require a header make it include that header
rubidium <rubidium@openttd.org>
parents: 14227
diff changeset
32 #include "core/random_func.hpp"
a899d4e5ee1a (svn r18809) -Codechange/Cleanup: remove unneeded headers from some files, if a header require a header make it include that header
rubidium <rubidium@openttd.org>
parents: 14227
diff changeset
33 #include "company_base.h"
a899d4e5ee1a (svn r18809) -Codechange/Cleanup: remove unneeded headers from some files, if a header require a header make it include that header
rubidium <rubidium@openttd.org>
parents: 14227
diff changeset
34 #include "newgrf.h"
15865
270f9b0689cc (svn r20547) -Change: the way order backups are performed. Now restoring an order doesn't require up to 765 commands.
rubidium <rubidium@openttd.org>
parents: 15854
diff changeset
35 #include "order_backup.h"
18472
11637619aa76 (svn r23316) -Feature: Add ability to zoom in to 2x and 4x level.
peter1138 <peter1138@openttd.org>
parents: 18446
diff changeset
36 #include "zoom_func.h"
8083
e02014b06c7f (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz <smatz@openttd.org>
parents: 8081
diff changeset
37
8264
2495310e220f (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium <rubidium@openttd.org>
parents: 8258
diff changeset
38 #include "table/strings.h"
2495310e220f (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium <rubidium@openttd.org>
parents: 8258
diff changeset
39 #include "table/train_cmd.h"
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
40
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
41 static Track ChooseTrainTrack(Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool force_res, bool *got_reservation, bool mark_stuck);
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
42 static bool TrainCheckIfLineEnds(Train *v, bool reverse = true);
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
43 bool TrainController(Train *v, Vehicle *nomove, bool reverse = true); // Also used in vehicle_sl.cpp.
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
44 static TileIndex TrainApproachingCrossingTile(const Train *v);
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
45 static void CheckIfTrainNeedsService(Train *v);
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
46 static void CheckNextTrainTile(Train *v);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
47
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
48 static const byte _vehicle_initial_x_fract[4] = {10, 8, 4, 8};
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
49 static const byte _vehicle_initial_y_fract[4] = { 8, 4, 8, 10};
8248
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
50
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
51
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
52 /**
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
53 * Determine the side in which the train will leave the tile
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
54 *
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
55 * @param direction vehicle direction
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
56 * @param track vehicle track bits
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
57 * @return side of tile the train will leave
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
58 */
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
59 static inline DiagDirection TrainExitDir(Direction direction, TrackBits track)
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
60 {
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
61 static const TrackBits state_dir_table[DIAGDIR_END] = { TRACK_BIT_RIGHT, TRACK_BIT_LOWER, TRACK_BIT_LEFT, TRACK_BIT_UPPER };
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
62
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
63 DiagDirection diagdir = DirToDiagDir(direction);
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
64
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
65 /* Determine the diagonal direction in which we will exit this tile */
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
66 if (!HasBit(direction, 0) && track != state_dir_table[diagdir]) {
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
67 diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT);
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
68 }
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
69
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
70 return diagdir;
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
71 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
72
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
73
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
74 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
75 * Return the cargo weight multiplier to use for a rail vehicle
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
76 * @param cargo Cargo type to get multiplier for
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
77 * @return Cargo weight multiplier
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
78 */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
79 byte FreightWagonMult(CargoID cargo)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
80 {
12415
6a77d1df56e2 (svn r16849) -Codechange: replace GetCargo() by CargoSpec::Get()
smatz <smatz@openttd.org>
parents: 12351
diff changeset
81 if (!CargoSpec::Get(cargo)->is_freight) return 1;
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9358
diff changeset
82 return _settings_game.vehicle.freight_trains;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
83 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
84
9704
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
85 /** Checks if lengths of all rail vehicles are valid. If not, shows an error message. */
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
86 void CheckTrainsLengths()
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
87 {
12035
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
88 const Train *v;
17165
3c9baf898829 (svn r21903) -Codechange: warn only once for a train that has invalid length, not for each its wagon with invalid length
smatz <smatz@openttd.org>
parents: 17142
diff changeset
89 bool first = true;
12035
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
90
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
91 FOR_ALL_TRAINS(v) {
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
92 if (v->First() == v && !(v->vehstatus & VS_CRASHED)) {
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
93 for (const Train *u = v, *w = v->Next(); w != NULL; u = w, w = w->Next()) {
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
94 if (u->track != TRACK_BIT_DEPOT) {
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
95 if ((w->track != TRACK_BIT_DEPOT &&
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
96 max(abs(u->x_pos - w->x_pos), abs(u->y_pos - w->y_pos)) != u->CalcNextVehicleOffset()) ||
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
97 (w->track == TRACK_BIT_DEPOT && TicksToLeaveDepot(u) <= 0)) {
9704
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
98 SetDParam(0, v->index);
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
99 SetDParam(1, v->owner);
14645
b7a22979b84e (svn r19224) -Codechange: change parameters passed to ShowErrorMessage() a bit
smatz <smatz@openttd.org>
parents: 14520
diff changeset
100 ShowErrorMessage(STR_BROKEN_VEHICLE_LENGTH, INVALID_STRING_ID, WL_CRITICAL);
9705
228505046e27 (svn r13817) -Cleanup (r13816): no need to check for ENABLE_NETWORK, _networking is defined anyway
smatz <smatz@openttd.org>
parents: 9704
diff changeset
101
17165
3c9baf898829 (svn r21903) -Codechange: warn only once for a train that has invalid length, not for each its wagon with invalid length
smatz <smatz@openttd.org>
parents: 17142
diff changeset
102 if (!_networking && first) {
3c9baf898829 (svn r21903) -Codechange: warn only once for a train that has invalid length, not for each its wagon with invalid length
smatz <smatz@openttd.org>
parents: 17142
diff changeset
103 first = false;
3c9baf898829 (svn r21903) -Codechange: warn only once for a train that has invalid length, not for each its wagon with invalid length
smatz <smatz@openttd.org>
parents: 17142
diff changeset
104 DoCommandP(0, PM_PAUSED_ERROR, 1, CMD_PAUSE);
3c9baf898829 (svn r21903) -Codechange: warn only once for a train that has invalid length, not for each its wagon with invalid length
smatz <smatz@openttd.org>
parents: 17142
diff changeset
105 }
3c9baf898829 (svn r21903) -Codechange: warn only once for a train that has invalid length, not for each its wagon with invalid length
smatz <smatz@openttd.org>
parents: 17142
diff changeset
106 /* Break so we warn only once for each train. */
3c9baf898829 (svn r21903) -Codechange: warn only once for a train that has invalid length, not for each its wagon with invalid length
smatz <smatz@openttd.org>
parents: 17142
diff changeset
107 break;
9704
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
108 }
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
109 }
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
110 }
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
111 }
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
112 }
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
113 }
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
114
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
115 /**
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
116 * Recalculates the cached stuff of a train. Should be called each time a vehicle is added
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
117 * to/removed from the chain, and when the game is loaded.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
118 * Note: this needs to be called too for 'wagon chains' (in the depot, without an engine)
9704
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
119 * @param same_length should length of vehicles stay the same?
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
120 */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
121 void Train::ConsistChanged(bool same_length)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
122 {
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
123 uint16 max_speed = UINT16_MAX;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
124
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
125 assert(this->IsFrontEngine() || this->IsFreeWagon());
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
126
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
127 const RailVehicleInfo *rvi_v = RailVehInfo(this->engine_type);
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
128 EngineID first_engine = this->IsFrontEngine() ? this->engine_type : INVALID_ENGINE;
16788
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
129 this->gcache.cached_total_length = 0;
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
130 this->compatible_railtypes = RAILTYPES_NONE;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
131
8178
682fb03eb247 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138 <peter1138@openttd.org>
parents: 8175
diff changeset
132 bool train_can_tilt = true;
682fb03eb247 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138 <peter1138@openttd.org>
parents: 8175
diff changeset
133
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
134 for (Train *u = this; u != NULL; u = u->Next()) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
135 const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
136
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
137 /* Check the this->first cache. */
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
138 assert(u->First() == this);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
139
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
140 /* update the 'first engine' */
16788
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
141 u->gcache.first_engine = this == u ? INVALID_ENGINE : first_engine;
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
142 u->railtype = rvi_u->railtype;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
143
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
144 if (u->IsEngine()) first_engine = u->engine_type;
9515
bcc765d85155 (svn r13507) -Fix (r12856): first engine change should've been moved too
peter1138 <peter1138@openttd.org>
parents: 9488
diff changeset
145
9037
8132939c7773 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138 <peter1138@openttd.org>
parents: 9022
diff changeset
146 /* Set user defined data to its default value */
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
147 u->tcache.user_def_data = rvi_u->user_def_data;
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
148 this->InvalidateNewGRFCache();
12073
02d3a628e6db (svn r16480) -Fix (r15617): Invalidate newgrf variable caches of more vehicles in more places. Esp. they were only invalidated for trains.
frosch <frosch@openttd.org>
parents: 12035
diff changeset
149 u->InvalidateNewGRFCache();
9037
8132939c7773 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138 <peter1138@openttd.org>
parents: 9022
diff changeset
150 }
8132939c7773 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138 <peter1138@openttd.org>
parents: 9022
diff changeset
151
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
152 for (Train *u = this; u != NULL; u = u->Next()) {
9037
8132939c7773 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138 <peter1138@openttd.org>
parents: 9022
diff changeset
153 /* Update user defined data (must be done before other properties) */
13114
940252f91c5f (svn r17616) -Codechange [FS#3222]: Enumerize properties used in callback 0x36. Based on Terkhen's work.
frosch <frosch@openttd.org>
parents: 13094
diff changeset
154 u->tcache.user_def_data = GetVehicleProperty(u, PROP_TRAIN_USER_DATA, u->tcache.user_def_data);
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
155 this->InvalidateNewGRFCache();
12073
02d3a628e6db (svn r16480) -Fix (r15617): Invalidate newgrf variable caches of more vehicles in more places. Esp. they were only invalidated for trains.
frosch <frosch@openttd.org>
parents: 12035
diff changeset
156 u->InvalidateNewGRFCache();
9037
8132939c7773 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138 <peter1138@openttd.org>
parents: 9022
diff changeset
157 }
8132939c7773 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138 <peter1138@openttd.org>
parents: 9022
diff changeset
158
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
159 for (Train *u = this; u != NULL; u = u->Next()) {
18238
3141f1ed78eb (svn r23074) -Codechange: Add Vehicle::GetEngine() to simplify code.
frosch <frosch@openttd.org>
parents: 18159
diff changeset
160 const Engine *e_u = u->GetEngine();
11188
9e451d497d94 (svn r15541) -Revert (r15399): 'v->cargo_type' is also used in other places, which cannot accept CT_INVALID.
frosch <frosch@openttd.org>
parents: 11150
diff changeset
161 const RailVehicleInfo *rvi_u = &e_u->u.rail;
9037
8132939c7773 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138 <peter1138@openttd.org>
parents: 9022
diff changeset
162
13218
03a409fa4c29 (svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
frosch <frosch@openttd.org>
parents: 13186
diff changeset
163 if (!HasBit(e_u->info.misc_flags, EF_RAIL_TILTS)) train_can_tilt = false;
9037
8132939c7773 (svn r12856) -Fix: Taking r12377 further, ensure that prop 25 is set for all vehicles in the consist before other properties.
peter1138 <peter1138@openttd.org>
parents: 9022
diff changeset
164
6603
2124b871a178 (svn r9818) -Codechange: Cache wagon override sprite set whenever consist is changed, instead of 'recalculating' it every time it is used or just tested.
peter1138 <peter1138@openttd.org>
parents: 6594
diff changeset
165 /* Cache wagon override sprite group. NULL is returned if there is none */
16788
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
166 u->tcache.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->gcache.first_engine);
6603
2124b871a178 (svn r9818) -Codechange: Cache wagon override sprite set whenever consist is changed, instead of 'recalculating' it every time it is used or just tested.
peter1138 <peter1138@openttd.org>
parents: 6594
diff changeset
167
11085
8da1855e9f14 (svn r15428) -Codechange: consistently use colour instead of having both color and colour.
rubidium <rubidium@openttd.org>
parents: 11018
diff changeset
168 /* Reset colour map */
8da1855e9f14 (svn r15428) -Codechange: consistently use colour instead of having both color and colour.
rubidium <rubidium@openttd.org>
parents: 11018
diff changeset
169 u->colourmap = PAL_NONE;
7802
bb4e6370dfdf (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx <glx@openttd.org>
parents: 7783
diff changeset
170
15825
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
171 /* Update powered-wagon-status and visual effect */
15826
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
172 u->UpdateVisualEffect(true);
9738
469d98bb080e (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138 <peter1138@openttd.org>
parents: 9705
diff changeset
173
469d98bb080e (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138 <peter1138@openttd.org>
parents: 9705
diff changeset
174 if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RAILVEH_WAGON &&
16507
e89b23c58633 (svn r21233) -Codechange: Introduce an enum to remove some magic numbers from the visual effect code (Hirundo)
rubidium <rubidium@openttd.org>
parents: 16506
diff changeset
175 UsesWagonOverride(u) && !HasBit(u->vcache.cached_vis_effect, VE_DISABLE_WAGON_POWER)) {
9738
469d98bb080e (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138 <peter1138@openttd.org>
parents: 9705
diff changeset
176 /* wagon is powered */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
177 SetBit(u->flags, VRF_POWEREDWAGON); // cache 'powered' status
9738
469d98bb080e (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138 <peter1138@openttd.org>
parents: 9705
diff changeset
178 } else {
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
179 ClrBit(u->flags, VRF_POWEREDWAGON);
9738
469d98bb080e (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138 <peter1138@openttd.org>
parents: 9705
diff changeset
180 }
469d98bb080e (svn r13870) -Fix [FS#2167]: Callback 10 (visual effect and powered wagons setting) and powered wagons operation were not performed for articulated wagons.
peter1138 <peter1138@openttd.org>
parents: 9705
diff changeset
181
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
182 if (!u->IsArticulatedPart()) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
183 /* Do not count powered wagons for the compatible railtypes, as wagons always
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
184 have railtype normal */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
185 if (rvi_u->power > 0) {
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
186 this->compatible_railtypes |= GetRailTypeInfo(u->railtype)->powered_railtypes;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
187 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
188
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
189 /* Some electric engines can be allowed to run on normal rail. It happens to all
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
190 * existing electric engines when elrails are disabled and then re-enabled */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
191 if (HasBit(u->flags, VRF_EL_ENGINE_ALLOWED_NORMAL_RAIL)) {
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
192 u->railtype = RAILTYPE_RAIL;
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
193 u->compatible_railtypes |= RAILTYPES_RAIL;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
194 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
195
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
196 /* max speed is the minimum of the speed limits of all vehicles in the consist */
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9358
diff changeset
197 if ((rvi_u->railveh_type != RAILVEH_WAGON || _settings_game.vehicle.wagon_speed_limits) && !UsesWagonOverride(u)) {
13114
940252f91c5f (svn r17616) -Codechange [FS#3222]: Enumerize properties used in callback 0x36. Based on Terkhen's work.
frosch <frosch@openttd.org>
parents: 13094
diff changeset
198 uint16 speed = GetVehicleProperty(u, PROP_TRAIN_SPEED, rvi_u->max_speed);
6490
fcc1843c68c6 (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 <peter1138@openttd.org>
parents: 6484
diff changeset
199 if (speed != 0) max_speed = min(speed, max_speed);
fcc1843c68c6 (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 <peter1138@openttd.org>
parents: 6484
diff changeset
200 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
201 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
202
20307
93d7e37bd666 (svn r25259) -Codechange: track capacities and usage of links
rubidium <rubidium@openttd.org>
parents: 20258
diff changeset
203 uint16 new_cap = e_u->DetermineCapacity(u);
93d7e37bd666 (svn r25259) -Codechange: track capacities and usage of links
rubidium <rubidium@openttd.org>
parents: 20258
diff changeset
204 u->refit_cap = min(new_cap, u->refit_cap);
93d7e37bd666 (svn r25259) -Codechange: track capacities and usage of links
rubidium <rubidium@openttd.org>
parents: 20258
diff changeset
205 u->cargo_cap = new_cap;
17909
4d6b4b6d6069 (svn r22713) -Feature: [NewGRF] Per vehicle custom cargo ageing period.
michi_cc <michi_cc@openttd.org>
parents: 17804
diff changeset
206 u->vcache.cached_cargo_age_period = GetVehicleProperty(u, PROP_TRAIN_CARGO_AGE_PERIOD, e_u->info.cargo_age_period);
6608
68e9e501d4d8 (svn r9828) -Codechange: [NewGRF] Add support for changing cargo capacity with callback 36. This is set on construction for ships and roadvehicles, and whenever carriages are attached for trains.
peter1138 <peter1138@openttd.org>
parents: 6603
diff changeset
207
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
208 /* check the vehicle length (callback) */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
209 uint16 veh_len = CALLBACK_FAILED;
18314
1f35799d5a8b (svn r23150) -Change: [NewGRF v8] Deprecate callback 11, and use callback 36 instead.
frosch <frosch@openttd.org>
parents: 18312
diff changeset
210 if (e_u->GetGRF() != NULL && e_u->GetGRF()->grf_version >= 8) {
1f35799d5a8b (svn r23150) -Change: [NewGRF v8] Deprecate callback 11, and use callback 36 instead.
frosch <frosch@openttd.org>
parents: 18312
diff changeset
211 /* Use callback 36 */
1f35799d5a8b (svn r23150) -Change: [NewGRF v8] Deprecate callback 11, and use callback 36 instead.
frosch <frosch@openttd.org>
parents: 18312
diff changeset
212 veh_len = GetVehicleProperty(u, PROP_TRAIN_SHORTEN_FACTOR, CALLBACK_FAILED);
19780
3e5e1e2af3ac (svn r24713) -Fix [FS#5262]: [NewGRF] Tolerate old NewGRFs returning invalid values via CB 11.
frosch <frosch@openttd.org>
parents: 19481
diff changeset
213
3e5e1e2af3ac (svn r24713) -Fix [FS#5262]: [NewGRF] Tolerate old NewGRFs returning invalid values via CB 11.
frosch <frosch@openttd.org>
parents: 19481
diff changeset
214 if (veh_len != CALLBACK_FAILED && veh_len >= VEHICLE_LENGTH) {
3e5e1e2af3ac (svn r24713) -Fix [FS#5262]: [NewGRF] Tolerate old NewGRFs returning invalid values via CB 11.
frosch <frosch@openttd.org>
parents: 19481
diff changeset
215 ErrorUnknownCallbackResult(e_u->GetGRFID(), CBID_VEHICLE_LENGTH, veh_len);
3e5e1e2af3ac (svn r24713) -Fix [FS#5262]: [NewGRF] Tolerate old NewGRFs returning invalid values via CB 11.
frosch <frosch@openttd.org>
parents: 19481
diff changeset
216 }
18314
1f35799d5a8b (svn r23150) -Change: [NewGRF v8] Deprecate callback 11, and use callback 36 instead.
frosch <frosch@openttd.org>
parents: 18312
diff changeset
217 } else if (HasBit(e_u->info.callback_mask, CBM_VEHICLE_LENGTH)) {
1f35799d5a8b (svn r23150) -Change: [NewGRF v8] Deprecate callback 11, and use callback 36 instead.
frosch <frosch@openttd.org>
parents: 18312
diff changeset
218 /* Use callback 11 */
7215
ba8faf180ec2 (svn r10493) -Codechange: update some callback ID enums to reflect their changed usage, add a few and update the comments.
rubidium <rubidium@openttd.org>
parents: 7196
diff changeset
219 veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, u->engine_type, u);
18314
1f35799d5a8b (svn r23150) -Change: [NewGRF v8] Deprecate callback 11, and use callback 36 instead.
frosch <frosch@openttd.org>
parents: 18312
diff changeset
220 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
221 if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor;
17451
2dbd537df54d (svn r22205) -Codechange: replace magic number with constant
rubidium <rubidium@openttd.org>
parents: 17399
diff changeset
222 veh_len = VEHICLE_LENGTH - Clamp(veh_len, 0, VEHICLE_LENGTH - 1);
9704
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
223
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
224 /* verify length hasn't changed */
18276
72e793b46dee (svn r23112) -Codechange: Check if vehicle chain lengths stays constant when auto-refitting.
michi_cc <michi_cc@openttd.org>
parents: 18272
diff changeset
225 if (same_length && veh_len != u->gcache.cached_veh_length) VehicleLengthChanged(u);
9704
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
226
e1476334067a (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz <smatz@openttd.org>
parents: 9699
diff changeset
227 /* update vehicle length? */
16788
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
228 if (!same_length) u->gcache.cached_veh_length = veh_len;
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
229
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
230 this->gcache.cached_total_length += u->gcache.cached_veh_length;
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
231 this->InvalidateNewGRFCache();
12073
02d3a628e6db (svn r16480) -Fix (r15617): Invalidate newgrf variable caches of more vehicles in more places. Esp. they were only invalidated for trains.
frosch <frosch@openttd.org>
parents: 12035
diff changeset
232 u->InvalidateNewGRFCache();
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
233 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
234
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
235 /* store consist weight/max speed in cache */
16381
6d81f8cd1497 (svn r21097) -Codechange: Unify cached max speed for all vehicle types.
terkhen <terkhen@openttd.org>
parents: 16372
diff changeset
236 this->vcache.cached_max_speed = max_speed;
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
237 this->tcache.cached_tilt = train_can_tilt;
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
238 this->tcache.cached_max_curve_speed = this->GetCurveSpeedLimit();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
239
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
240 /* 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) */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
241 this->CargoChanged();
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
242
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
243 if (this->IsFrontEngine()) {
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
244 this->UpdateAcceleration();
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
245 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
19900
b4c4f1c24136 (svn r24833) -Codechange: Replace magic numbers for invalidating vehicle-related windows with an enum.
michi_cc <michi_cc@openttd.org>
parents: 19899
diff changeset
246 InvalidateWindowData(WC_VEHICLE_REFIT, this->index, VIWD_CONSIST_CHANGED);
19901
9edbe277d872 (svn r24834) -Fix [FS#5396]: The autorefit dropdown in the order GUI wasn't always updated when modifying vehicle consists.
michi_cc <michi_cc@openttd.org>
parents: 19900
diff changeset
247 InvalidateWindowData(WC_VEHICLE_ORDERS, this->index, VIWD_CONSIST_CHANGED);
8706
20434584b743 (svn r12380) -Fix: update train acceleration and max speed after setting cached value to ensure the correct max speed is used with disabled real acceleration
glx <glx@openttd.org>
parents: 8705
diff changeset
248 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
249 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
250
11657
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
251 /**
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
252 * Get the stop location of (the center) of the front vehicle of a train at
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
253 * a platform of a station.
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
254 * @param station_id the ID of the station where we're stopping
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
255 * @param tile the tile where the vehicle currently is
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
256 * @param v the vehicle to get the stop location of
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
257 * @param station_ahead 'return' the amount of 1/16th tiles in front of the train
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
258 * @param station_length 'return' the station length in 1/16th tiles
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
259 * @return the location, calculated from the begin of the station to stop at.
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
260 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
261 int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, int *station_ahead, int *station_length)
11657
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
262 {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11920
diff changeset
263 const Station *st = Station::Get(station_id);
11657
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
264 *station_ahead = st->GetPlatformLength(tile, DirToDiagDir(v->direction)) * TILE_SIZE;
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
265 *station_length = st->GetPlatformLength(tile) * TILE_SIZE;
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
266
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
267 /* Default to the middle of the station for stations stops that are not in
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
268 * the order list like intermediate stations when non-stop is disabled */
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
269 OrderStopLocation osl = OSL_PLATFORM_MIDDLE;
16788
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
270 if (v->gcache.cached_total_length >= *station_length) {
11657
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
271 /* The train is longer than the station, make it stop at the far end of the platform */
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
272 osl = OSL_PLATFORM_FAR_END;
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
273 } else if (v->current_order.IsType(OT_GOTO_STATION) && v->current_order.GetDestination() == station_id) {
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
274 osl = v->current_order.GetStopLocation();
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
275 }
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
276
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
277 /* The stop location of the FRONT! of the train */
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
278 int stop;
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
279 switch (osl) {
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
280 default: NOT_REACHED();
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
281
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
282 case OSL_PLATFORM_NEAR_END:
16788
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
283 stop = v->gcache.cached_total_length;
11657
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
284 break;
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
285
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
286 case OSL_PLATFORM_MIDDLE:
16788
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
287 stop = *station_length - (*station_length - v->gcache.cached_total_length) / 2;
11657
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
288 break;
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
289
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
290 case OSL_PLATFORM_FAR_END:
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
291 stop = *station_length;
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
292 break;
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
293 }
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
294
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
295 /* Subtract half the front vehicle length of the train so we get the real
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
296 * stop location of the train. */
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
297 return stop - (v->gcache.cached_veh_length + 1) / 2;
11657
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
298 }
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
299
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
300
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
301 /**
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
302 * Computes train speed limit caused by curves
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
303 * @return imposed speed limit
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
304 */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
305 int Train::GetCurveSpeedLimit() const
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
306 {
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
307 assert(this->First() == this);
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
308
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
309 static const int absolute_max_speed = UINT16_MAX;
8169
7962907a27a1 (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138 <peter1138@openttd.org>
parents: 8168
diff changeset
310 int max_speed = absolute_max_speed;
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
311
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
312 if (_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) return max_speed;
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
313
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
314 int curvecount[2] = {0, 0};
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
315
11368
82c90cd591ab (svn r15718) -Cleanup: apply some comment coding style on the rest of the sources too
rubidium <rubidium@openttd.org>
parents: 11351
diff changeset
316 /* first find the curve speed limit */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
317 int numcurve = 0;
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
318 int sum = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
319 int pos = 0;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
320 int lastpos = -1;
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
321 for (const Vehicle *u = this; u->Next() != NULL; u = u->Next(), pos++) {
8175
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
322 Direction this_dir = u->direction;
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
323 Direction next_dir = u->Next()->direction;
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
324
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
325 DirDiff dirdiff = DirDifference(this_dir, next_dir);
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
326 if (dirdiff == DIRDIFF_SAME) continue;
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
327
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
328 if (dirdiff == DIRDIFF_45LEFT) curvecount[0]++;
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
329 if (dirdiff == DIRDIFF_45RIGHT) curvecount[1]++;
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
330 if (dirdiff == DIRDIFF_45LEFT || dirdiff == DIRDIFF_45RIGHT) {
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
331 if (lastpos != -1) {
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
332 numcurve++;
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
333 sum += pos - lastpos;
18064
0108a6ec2d68 (svn r22879) -Fix: Miscalculation of train curve speed limits. (monoid)
terkhen <terkhen@openttd.org>
parents: 18007
diff changeset
334 if (pos - lastpos == 1 && max_speed > 88) {
8175
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
335 max_speed = 88;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
336 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
337 }
8175
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
338 lastpos = pos;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
339 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
340
11368
82c90cd591ab (svn r15718) -Cleanup: apply some comment coding style on the rest of the sources too
rubidium <rubidium@openttd.org>
parents: 11351
diff changeset
341 /* if we have a 90 degree turn, fix the speed limit to 60 */
8175
b6a2bebbaaed (svn r11738) -Codechange: Refactor realistic acceleration's curve counting to use DirDiffs instead of local tables, and remove an unnecessary loop.
peter1138 <peter1138@openttd.org>
parents: 8170
diff changeset
342 if (dirdiff == DIRDIFF_90LEFT || dirdiff == DIRDIFF_90RIGHT) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
343 max_speed = 61;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
344 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
345 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
346
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
347 if (numcurve > 0 && max_speed > 88) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
348 if (curvecount[0] == 1 && curvecount[1] == 1) {
8169
7962907a27a1 (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138 <peter1138@openttd.org>
parents: 8168
diff changeset
349 max_speed = absolute_max_speed;
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
350 } else {
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
351 sum /= numcurve;
7922
ff1975ced735 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13 <skidd13@openttd.org>
parents: 7881
diff changeset
352 max_speed = 232 - (13 - Clamp(sum, 1, 12)) * (13 - Clamp(sum, 1, 12));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
353 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
354 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
355
8169
7962907a27a1 (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138 <peter1138@openttd.org>
parents: 8168
diff changeset
356 if (max_speed != absolute_max_speed) {
7962907a27a1 (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138 <peter1138@openttd.org>
parents: 8168
diff changeset
357 /* Apply the engine's rail type curve speed advantage, if it slowed by curves */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
358 const RailtypeInfo *rti = GetRailTypeInfo(this->railtype);
8169
7962907a27a1 (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138 <peter1138@openttd.org>
parents: 8168
diff changeset
359 max_speed += (max_speed / 2) * rti->curve_speed;
8178
682fb03eb247 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138 <peter1138@openttd.org>
parents: 8175
diff changeset
360
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
361 if (this->tcache.cached_tilt) {
8178
682fb03eb247 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138 <peter1138@openttd.org>
parents: 8175
diff changeset
362 /* Apply max_speed bonus of 20% for a tilting train */
682fb03eb247 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138 <peter1138@openttd.org>
parents: 8175
diff changeset
363 max_speed += max_speed / 5;
682fb03eb247 (svn r11741) -Feature: Add support for NewGRF's train 'tilt' flag. Trains with tilt capability (specific details are per NewGRF set) will be given a 20% speed limit bonus on curves.
peter1138 <peter1138@openttd.org>
parents: 8175
diff changeset
364 }
8169
7962907a27a1 (svn r11732) -Fix (r4150): elrail merge gave elrail, monorail & maglev unintended speed bonuses for curves, as the bonus was based on the railtype index. The bonus is now specified by a property of the railtype.
peter1138 <peter1138@openttd.org>
parents: 8168
diff changeset
365 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
366
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
367 return max_speed;
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
368 }
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
369
14286
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
370 /**
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
371 * Calculates the maximum speed of the vehicle under its current conditions.
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
372 * @return Maximum speed of the vehicle.
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
373 */
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
374 int Train::GetCurrentMaxSpeed() const
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
375 {
20206
c2446757542d (svn r25167) -Fix [FS#5523] (r24832): Original train and rv acceleration did no longer respect bridge speed limits.
frosch <frosch@openttd.org>
parents: 20065
diff changeset
376 int max_speed = _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL ?
c2446757542d (svn r25167) -Fix [FS#5523] (r24832): Original train and rv acceleration did no longer respect bridge speed limits.
frosch <frosch@openttd.org>
parents: 20065
diff changeset
377 this->gcache.cached_max_track_speed :
c2446757542d (svn r25167) -Fix [FS#5523] (r24832): Original train and rv acceleration did no longer respect bridge speed limits.
frosch <frosch@openttd.org>
parents: 20065
diff changeset
378 this->tcache.cached_max_curve_speed;
c2446757542d (svn r25167) -Fix [FS#5523] (r24832): Original train and rv acceleration did no longer respect bridge speed limits.
frosch <frosch@openttd.org>
parents: 20065
diff changeset
379
c2446757542d (svn r25167) -Fix [FS#5523] (r24832): Original train and rv acceleration did no longer respect bridge speed limits.
frosch <frosch@openttd.org>
parents: 20065
diff changeset
380 if (_settings_game.vehicle.train_acceleration_model == AM_REALISTIC && IsRailStationTile(this->tile)) {
14286
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
381 StationID sid = GetStationIndex(this->tile);
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
382 if (this->current_order.ShouldStopAtStation(this, sid)) {
11657
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
383 int station_ahead;
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
384 int station_length;
14286
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
385 int stop_at = GetTrainStopLocation(sid, this->tile, this, &station_ahead, &station_length);
11657
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
386
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
387 /* The distance to go is whatever is still ahead of the train minus the
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
388 * distance from the train's stop location to the end of the platform */
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
389 int distance_to_go = station_ahead / TILE_SIZE - (station_length - stop_at) / TILE_SIZE;
11677
8d40b1680957 (svn r16057) -Fix [FS#2834] (r16037): division by zero when having an order with only one station that has either middle or near end stop location and where the platform is (significantly) longer than the train.
rubidium <rubidium@openttd.org>
parents: 11657
diff changeset
390
8d40b1680957 (svn r16057) -Fix [FS#2834] (r16037): division by zero when having an order with only one station that has either middle or near end stop location and where the platform is (significantly) longer than the train.
rubidium <rubidium@openttd.org>
parents: 11657
diff changeset
391 if (distance_to_go > 0) {
8d40b1680957 (svn r16057) -Fix [FS#2834] (r16037): division by zero when having an order with only one station that has either middle or near end stop location and where the platform is (significantly) longer than the train.
rubidium <rubidium@openttd.org>
parents: 11657
diff changeset
392 int st_max_speed = 120;
8d40b1680957 (svn r16057) -Fix [FS#2834] (r16037): division by zero when having an order with only one station that has either middle or near end stop location and where the platform is (significantly) longer than the train.
rubidium <rubidium@openttd.org>
parents: 11657
diff changeset
393
14286
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
394 int delta_v = this->cur_speed / (distance_to_go + 1);
15064
a34ec81b63b0 (svn r19687) -Fix: desync when joining the game because of using the wrong variable
rubidium <rubidium@openttd.org>
parents: 15009
diff changeset
395 if (max_speed > (this->cur_speed - delta_v)) {
14286
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
396 st_max_speed = this->cur_speed - (delta_v / 10);
11677
8d40b1680957 (svn r16057) -Fix [FS#2834] (r16037): division by zero when having an order with only one station that has either middle or near end stop location and where the platform is (significantly) longer than the train.
rubidium <rubidium@openttd.org>
parents: 11657
diff changeset
397 }
8d40b1680957 (svn r16057) -Fix [FS#2834] (r16037): division by zero when having an order with only one station that has either middle or near end stop location and where the platform is (significantly) longer than the train.
rubidium <rubidium@openttd.org>
parents: 11657
diff changeset
398
8d40b1680957 (svn r16057) -Fix [FS#2834] (r16037): division by zero when having an order with only one station that has either middle or near end stop location and where the platform is (significantly) longer than the train.
rubidium <rubidium@openttd.org>
parents: 11657
diff changeset
399 st_max_speed = max(st_max_speed, 25 * distance_to_go);
8d40b1680957 (svn r16057) -Fix [FS#2834] (r16037): division by zero when having an order with only one station that has either middle or near end stop location and where the platform is (significantly) longer than the train.
rubidium <rubidium@openttd.org>
parents: 11657
diff changeset
400 max_speed = min(max_speed, st_max_speed);
8170
a8be61a5213c (svn r11733) -Fix: Max speed for entering stations overrode the max speed of curves
peter1138 <peter1138@openttd.org>
parents: 8169
diff changeset
401 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
402 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
403 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
404
14286
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
405 for (const Train *u = this; u != NULL; u = u->Next()) {
20206
c2446757542d (svn r25167) -Fix [FS#5523] (r24832): Original train and rv acceleration did no longer respect bridge speed limits.
frosch <frosch@openttd.org>
parents: 20065
diff changeset
406 if (_settings_game.vehicle.train_acceleration_model == AM_REALISTIC && u->track == TRACK_BIT_DEPOT) {
14286
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
407 max_speed = min(max_speed, 61);
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
408 break;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
409 }
19899
4f44e1c86f9c (svn r24832) -Fix [FS#5397]: [NewGRF] Take bridge speed limits into account for vehicle variable 4C.
michi_cc <michi_cc@openttd.org>
parents: 19898
diff changeset
410
4f44e1c86f9c (svn r24832) -Fix [FS#5397]: [NewGRF] Take bridge speed limits into account for vehicle variable 4C.
michi_cc <michi_cc@openttd.org>
parents: 19898
diff changeset
411 /* Vehicle is on the middle part of a bridge. */
4f44e1c86f9c (svn r24832) -Fix [FS#5397]: [NewGRF] Take bridge speed limits into account for vehicle variable 4C.
michi_cc <michi_cc@openttd.org>
parents: 19898
diff changeset
412 if (u->track == TRACK_BIT_WORMHOLE && !(u->vehstatus & VS_HIDDEN)) {
4f44e1c86f9c (svn r24832) -Fix [FS#5397]: [NewGRF] Take bridge speed limits into account for vehicle variable 4C.
michi_cc <michi_cc@openttd.org>
parents: 19898
diff changeset
413 max_speed = min(max_speed, GetBridgeSpec(GetBridgeType(u->tile))->speed);
4f44e1c86f9c (svn r24832) -Fix [FS#5397]: [NewGRF] Take bridge speed limits into account for vehicle variable 4C.
michi_cc <michi_cc@openttd.org>
parents: 19898
diff changeset
414 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
415 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
416
19093
38652cadf0fe (svn r23947) -Feature: Timetabled maximum travel speeds for non-flying vehicles.
michi_cc <michi_cc@openttd.org>
parents: 18999
diff changeset
417 max_speed = min(max_speed, this->current_order.max_speed);
16785
775f357828e3 (svn r21518) -Codechange: Rename AccelerationCache to GroundVehicleCache.
terkhen <terkhen@openttd.org>
parents: 16776
diff changeset
418 return min(max_speed, this->gcache.cached_max_track_speed);
14286
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
419 }
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
420
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
421 /** Update acceleration of the train from the cached power and weight. */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
422 void Train::UpdateAcceleration()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
423 {
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
424 assert(this->IsFrontEngine());
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
425
16785
775f357828e3 (svn r21518) -Codechange: Rename AccelerationCache to GroundVehicleCache.
terkhen <terkhen@openttd.org>
parents: 16776
diff changeset
426 uint power = this->gcache.cached_power;
775f357828e3 (svn r21518) -Codechange: Rename AccelerationCache to GroundVehicleCache.
terkhen <terkhen@openttd.org>
parents: 16776
diff changeset
427 uint weight = this->gcache.cached_weight;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
428 assert(weight != 0);
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
429 this->acceleration = Clamp(power / weight * 4, 1, 255);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
430 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
431
12433
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
432 /**
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
433 * Get the width of a train vehicle image in the GUI.
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
434 * @param offset Additional offset for positioning the sprite; set to NULL if not needed
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
435 * @return Width in pixels
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
436 */
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
437 int Train::GetDisplayImageWidth(Point *offset) const
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
438 {
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
439 int reference_width = TRAININFO_DEFAULT_VEHICLE_WIDTH;
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
440 int vehicle_pitch = 0;
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
441
18238
3141f1ed78eb (svn r23074) -Codechange: Add Vehicle::GetEngine() to simplify code.
frosch <frosch@openttd.org>
parents: 18159
diff changeset
442 const Engine *e = this->GetEngine();
18239
e6cbcbf3780f (svn r23075) -Codechange: Add GetGRF() and GetGRFID() methods to Engine and Vehicle to simplify code.
frosch <frosch@openttd.org>
parents: 18238
diff changeset
443 if (e->GetGRF() != NULL && is_custom_sprite(e->u.rail.image_index)) {
e6cbcbf3780f (svn r23075) -Codechange: Add GetGRF() and GetGRFID() methods to Engine and Vehicle to simplify code.
frosch <frosch@openttd.org>
parents: 18238
diff changeset
444 reference_width = e->GetGRF()->traininfo_vehicle_width;
e6cbcbf3780f (svn r23075) -Codechange: Add GetGRF() and GetGRFID() methods to Engine and Vehicle to simplify code.
frosch <frosch@openttd.org>
parents: 18238
diff changeset
445 vehicle_pitch = e->GetGRF()->traininfo_vehicle_pitch;
12433
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
446 }
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
447
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
448 if (offset != NULL) {
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
449 offset->x = reference_width / 2;
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
450 offset->y = vehicle_pitch;
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
451 }
17451
2dbd537df54d (svn r22205) -Codechange: replace magic number with constant
rubidium <rubidium@openttd.org>
parents: 17399
diff changeset
452 return this->gcache.cached_veh_length * reference_width / VEHICLE_LENGTH;
12433
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
453 }
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
454
12020
e9a4f34044d8 (svn r16427) -Codechange: replace a few magic numbers with an existing constant and unduplicate a few lines.
rubidium <rubidium@openttd.org>
parents: 12015
diff changeset
455 static SpriteID GetDefaultTrainSprite(uint8 spritenum, Direction direction)
e9a4f34044d8 (svn r16427) -Codechange: replace a few magic numbers with an existing constant and unduplicate a few lines.
rubidium <rubidium@openttd.org>
parents: 12015
diff changeset
456 {
e9a4f34044d8 (svn r16427) -Codechange: replace a few magic numbers with an existing constant and unduplicate a few lines.
rubidium <rubidium@openttd.org>
parents: 12015
diff changeset
457 return ((direction + _engine_sprite_add[spritenum]) & _engine_sprite_and[spritenum]) + _engine_sprite_base[spritenum];
e9a4f34044d8 (svn r16427) -Codechange: replace a few magic numbers with an existing constant and unduplicate a few lines.
rubidium <rubidium@openttd.org>
parents: 12015
diff changeset
458 }
e9a4f34044d8 (svn r16427) -Codechange: replace a few magic numbers with an existing constant and unduplicate a few lines.
rubidium <rubidium@openttd.org>
parents: 12015
diff changeset
459
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
460 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
461 * Get the sprite to display the train.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
462 * @param direction Direction of view/travel.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
463 * @param image_type Visualisation context.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
464 * @return Sprite to display.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
465 */
18244
8f6022f41538 (svn r23080) -Feature: [NewGRF] Use variable 10 to enable vehicle GRFs to draw different sprites on the map and in various GUIs.
frosch <frosch@openttd.org>
parents: 18239
diff changeset
466 SpriteID Train::GetImage(Direction direction, EngineImageType image_type) const
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
467 {
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
468 uint8 spritenum = this->spritenum;
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
469 SpriteID sprite;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
470
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
471 if (HasBit(this->flags, VRF_REVERSE_DIRECTION)) direction = ReverseDir(direction);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
472
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
473 if (is_custom_sprite(spritenum)) {
18244
8f6022f41538 (svn r23080) -Feature: [NewGRF] Use variable 10 to enable vehicle GRFs to draw different sprites on the map and in various GUIs.
frosch <frosch@openttd.org>
parents: 18239
diff changeset
474 sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)), image_type);
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
475 if (sprite != 0) return sprite;
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
476
18238
3141f1ed78eb (svn r23074) -Codechange: Add Vehicle::GetEngine() to simplify code.
frosch <frosch@openttd.org>
parents: 18159
diff changeset
477 spritenum = this->GetEngine()->original_image_index;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
478 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
479
12020
e9a4f34044d8 (svn r16427) -Codechange: replace a few magic numbers with an existing constant and unduplicate a few lines.
rubidium <rubidium@openttd.org>
parents: 12015
diff changeset
480 sprite = GetDefaultTrainSprite(spritenum, direction);
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
481
20212
723239958dde (svn r25185) -Fix [FS#5508]: Remove ambivalent functions CargoList::Empty() and Count(), and replace them with VehicleCargoList::StoredCount(), TotalCount(), StationCargoList::AvailableCount() and TotalCount(). (fonsinchen)
frosch <frosch@openttd.org>
parents: 20206
diff changeset
482 if (this->cargo.StoredCount() >= this->cargo_cap / 2U) sprite += _wagon_full_adder[spritenum];
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
483
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
484 return sprite;
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
485 }
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
486
18244
8f6022f41538 (svn r23080) -Feature: [NewGRF] Use variable 10 to enable vehicle GRFs to draw different sprites on the map and in various GUIs.
frosch <frosch@openttd.org>
parents: 18239
diff changeset
487 static SpriteID GetRailIcon(EngineID engine, bool rear_head, int &y, EngineImageType image_type)
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
488 {
12433
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
489 const Engine *e = Engine::Get(engine);
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
490 Direction dir = rear_head ? DIR_E : DIR_W;
12433
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
491 uint8 spritenum = e->u.rail.image_index;
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
492
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
493 if (is_custom_sprite(spritenum)) {
18244
8f6022f41538 (svn r23080) -Feature: [NewGRF] Use variable 10 to enable vehicle GRFs to draw different sprites on the map and in various GUIs.
frosch <frosch@openttd.org>
parents: 18239
diff changeset
494 SpriteID sprite = GetCustomVehicleIcon(engine, dir, image_type);
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
495 if (sprite != 0) {
18239
e6cbcbf3780f (svn r23075) -Codechange: Add GetGRF() and GetGRFID() methods to Engine and Vehicle to simplify code.
frosch <frosch@openttd.org>
parents: 18238
diff changeset
496 if (e->GetGRF() != NULL) {
e6cbcbf3780f (svn r23075) -Codechange: Add GetGRF() and GetGRFID() methods to Engine and Vehicle to simplify code.
frosch <frosch@openttd.org>
parents: 18238
diff changeset
497 y += e->GetGRF()->traininfo_vehicle_pitch;
12433
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
498 }
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
499 return sprite;
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
500 }
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
501
12164
d1a94177c6ec (svn r16580) -Codechange: rename Engine::image_index to original_image_index to not confuse it with image_index from *VehInfo.
rubidium <rubidium@openttd.org>
parents: 12155
diff changeset
502 spritenum = Engine::Get(engine)->original_image_index;
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
503 }
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
504
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
505 if (rear_head) spritenum++;
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
506
12020
e9a4f34044d8 (svn r16427) -Codechange: replace a few magic numbers with an existing constant and unduplicate a few lines.
rubidium <rubidium@openttd.org>
parents: 12015
diff changeset
507 return GetDefaultTrainSprite(spritenum, DIR_W);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
508 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
509
18244
8f6022f41538 (svn r23080) -Feature: [NewGRF] Use variable 10 to enable vehicle GRFs to draw different sprites on the map and in various GUIs.
frosch <frosch@openttd.org>
parents: 18239
diff changeset
510 void DrawTrainEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
511 {
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
512 if (RailVehInfo(engine)->railveh_type == RAILVEH_MULTIHEAD) {
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
513 int yf = y;
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
514 int yr = y;
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
515
18244
8f6022f41538 (svn r23080) -Feature: [NewGRF] Use variable 10 to enable vehicle GRFs to draw different sprites on the map and in various GUIs.
frosch <frosch@openttd.org>
parents: 18239
diff changeset
516 SpriteID spritef = GetRailIcon(engine, false, yf, image_type);
8f6022f41538 (svn r23080) -Feature: [NewGRF] Use variable 10 to enable vehicle GRFs to draw different sprites on the map and in various GUIs.
frosch <frosch@openttd.org>
parents: 18239
diff changeset
517 SpriteID spriter = GetRailIcon(engine, true, yr, image_type);
13599
28fe769d10b8 (svn r18123) -Codechange: try our best to keep the vehicles within the build vehicle list/autoreplace lists instead of overflowing.
rubidium <rubidium@openttd.org>
parents: 13597
diff changeset
518 const Sprite *real_spritef = GetSprite(spritef, ST_NORMAL);
28fe769d10b8 (svn r18123) -Codechange: try our best to keep the vehicles within the build vehicle list/autoreplace lists instead of overflowing.
rubidium <rubidium@openttd.org>
parents: 13597
diff changeset
519 const Sprite *real_spriter = GetSprite(spriter, ST_NORMAL);
28fe769d10b8 (svn r18123) -Codechange: try our best to keep the vehicles within the build vehicle list/autoreplace lists instead of overflowing.
rubidium <rubidium@openttd.org>
parents: 13597
diff changeset
520
18472
11637619aa76 (svn r23316) -Feature: Add ability to zoom in to 2x and 4x level.
peter1138 <peter1138@openttd.org>
parents: 18446
diff changeset
521 preferred_x = Clamp(preferred_x, left - UnScaleByZoom(real_spritef->x_offs, ZOOM_LVL_GUI) + 14, right - UnScaleByZoom(real_spriter->width, ZOOM_LVL_GUI) - UnScaleByZoom(real_spriter->x_offs, ZOOM_LVL_GUI) - 15);
13599
28fe769d10b8 (svn r18123) -Codechange: try our best to keep the vehicles within the build vehicle list/autoreplace lists instead of overflowing.
rubidium <rubidium@openttd.org>
parents: 13597
diff changeset
522
13597
706013b8e441 (svn r18121) -Codechange: pass the maximum left/right location to the Draw*Engine functions
rubidium <rubidium@openttd.org>
parents: 13492
diff changeset
523 DrawSprite(spritef, pal, preferred_x - 14, yf);
706013b8e441 (svn r18121) -Codechange: pass the maximum left/right location to the Draw*Engine functions
rubidium <rubidium@openttd.org>
parents: 13492
diff changeset
524 DrawSprite(spriter, pal, preferred_x + 15, yr);
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
525 } else {
18244
8f6022f41538 (svn r23080) -Feature: [NewGRF] Use variable 10 to enable vehicle GRFs to draw different sprites on the map and in various GUIs.
frosch <frosch@openttd.org>
parents: 18239
diff changeset
526 SpriteID sprite = GetRailIcon(engine, false, y, image_type);
13599
28fe769d10b8 (svn r18123) -Codechange: try our best to keep the vehicles within the build vehicle list/autoreplace lists instead of overflowing.
rubidium <rubidium@openttd.org>
parents: 13597
diff changeset
527 const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
18472
11637619aa76 (svn r23316) -Feature: Add ability to zoom in to 2x and 4x level.
peter1138 <peter1138@openttd.org>
parents: 18446
diff changeset
528 preferred_x = Clamp(preferred_x, left - UnScaleByZoom(real_sprite->x_offs, ZOOM_LVL_GUI), right - UnScaleByZoom(real_sprite->width, ZOOM_LVL_GUI) - UnScaleByZoom(real_sprite->x_offs, ZOOM_LVL_GUI));
13597
706013b8e441 (svn r18121) -Codechange: pass the maximum left/right location to the Draw*Engine functions
rubidium <rubidium@openttd.org>
parents: 13492
diff changeset
529 DrawSprite(sprite, pal, preferred_x, y);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
530 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
531 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
532
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
533 /**
19906
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
534 * Get the size of the sprite of a train sprite heading west, or both heads (used for lists).
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
535 * @param engine The engine to get the sprite from.
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
536 * @param[out] width The width of the sprite.
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
537 * @param[out] height The height of the sprite.
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
538 * @param[out] xoffs Number of pixels to shift the sprite to the right.
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
539 * @param[out] yoffs Number of pixels to shift the sprite downwards.
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
540 * @param image_type Context the sprite is used in.
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
541 */
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
542 void GetTrainSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
543 {
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
544 int y = 0;
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
545
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
546 SpriteID sprite = GetRailIcon(engine, false, y, image_type);
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
547 const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
548
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
549 width = UnScaleByZoom(real_sprite->width, ZOOM_LVL_GUI);
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
550 height = UnScaleByZoom(real_sprite->height, ZOOM_LVL_GUI);
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
551 xoffs = UnScaleByZoom(real_sprite->x_offs, ZOOM_LVL_GUI);
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
552 yoffs = UnScaleByZoom(real_sprite->y_offs, ZOOM_LVL_GUI);
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
553
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
554 if (RailVehInfo(engine)->railveh_type == RAILVEH_MULTIHEAD) {
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
555 sprite = GetRailIcon(engine, true, y, image_type);
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
556 real_sprite = GetSprite(sprite, ST_NORMAL);
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
557
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
558 /* Calculate values relative to an imaginary center between the two sprites. */
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
559 width = TRAININFO_DEFAULT_VEHICLE_WIDTH + UnScaleByZoom(real_sprite->width, ZOOM_LVL_GUI) + UnScaleByZoom(real_sprite->x_offs, ZOOM_LVL_GUI) - xoffs;
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
560 height = max<uint>(height, UnScaleByZoom(real_sprite->height, ZOOM_LVL_GUI));
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
561 xoffs = xoffs - TRAININFO_DEFAULT_VEHICLE_WIDTH / 2;
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
562 yoffs = min(yoffs, UnScaleByZoom(real_sprite->y_offs, ZOOM_LVL_GUI));
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
563 }
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
564 }
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
565
4cdf512f07a1 (svn r24839) -Feature [FS#5271]: [NewGRF] Support oversized purchase list sprites. (Based on patch by Eddi)
michi_cc <michi_cc@openttd.org>
parents: 19901
diff changeset
566 /**
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
567 * Build a railroad wagon.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
568 * @param tile tile of the depot where rail-vehicle is built.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
569 * @param flags type of operation.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
570 * @param e the engine to build.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
571 * @param ret[out] the vehicle that has been built.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
572 * @return the cost of this operation or an error.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
573 */
15890
52d3e250c976 (svn r20572) -Codechange: free/reserve some bits in the wagon move command to increase the vehicle pool limit
rubidium <rubidium@openttd.org>
parents: 15865
diff changeset
574 static CommandCost CmdBuildRailWagon(TileIndex tile, DoCommandFlag flags, const Engine *e, Vehicle **ret)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
575 {
11188
9e451d497d94 (svn r15541) -Revert (r15399): 'v->cargo_type' is also used in other places, which cannot accept CT_INVALID.
frosch <frosch@openttd.org>
parents: 11150
diff changeset
576 const RailVehicleInfo *rvi = &e->u.rail;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
577
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
578 /* Check that the wagon can drive on the track in question */
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
579 if (!IsCompatibleRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
580
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
581 if (flags & DC_EXEC) {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
582 Train *v = new Train();
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
583 *ret = v;
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
584 v->spritenum = rvi->image_index;
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
585
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
586 v->engine_type = e->index;
16788
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
587 v->gcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
588
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
589 DiagDirection dir = GetRailDepotDirection(tile);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
590
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
591 v->direction = DiagDirToDir(dir);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
592 v->tile = tile;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
593
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
594 int x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
595 int y = TileY(tile) * TILE_SIZE | _vehicle_initial_y_fract[dir];
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
596
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
597 v->x_pos = x;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
598 v->y_pos = y;
18255
b3cb40aab91b (svn r23091) -Codechange: rename some Get*Z functions to Get*PixelZ functions if they return the Z in pixels (like TilePixelHeight)
rubidium <rubidium@openttd.org>
parents: 18244
diff changeset
599 v->z_pos = GetSlopePixelZ(x, y);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
600 v->owner = _current_company;
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
601 v->track = TRACK_BIT_DEPOT;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
602 v->vehstatus = VS_HIDDEN | VS_DEFPAL;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
603
12303
ca4bc7e70a4d (svn r16720) -Codechange: make Set/ClearFrontEngine(), Set/ClearArticulatedPart(), Set/ClearWagon(), Set/ClearEngine(), Set/ClearFreeWagon() and Set/ClearMultiheaded() members of Train
smatz <smatz@openttd.org>
parents: 12302
diff changeset
604 v->SetWagon();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
605
13934
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
606 v->SetFreeWagon();
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
607 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
608
11188
9e451d497d94 (svn r15541) -Revert (r15399): 'v->cargo_type' is also used in other places, which cannot accept CT_INVALID.
frosch <frosch@openttd.org>
parents: 11150
diff changeset
609 v->cargo_type = e->GetDefaultCargoType();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
610 v->cargo_cap = rvi->capacity;
20307
93d7e37bd666 (svn r25259) -Codechange: track capacities and usage of links
rubidium <rubidium@openttd.org>
parents: 20258
diff changeset
611 v->refit_cap = 0;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
612
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
613 v->railtype = rvi->railtype;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
614
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
615 v->build_year = _cur_year;
11903
c40e241e65ba (svn r16303) -Codechange: Use the question mark as default sprite when creating vehicles, instead of arbitrary sprites not related to the vehicle. (though that also applies to the question mark)
frosch <frosch@openttd.org>
parents: 11902
diff changeset
616 v->cur_image = SPR_IMG_QUERY;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
617 v->random_bits = VehicleRandomBits();
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
618
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
619 v->group_id = DEFAULT_GROUP;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
620
13219
39e3a60fd47f (svn r17726) -Cleanup: Remove some unneeded/unused parameters.
frosch <frosch@openttd.org>
parents: 13218
diff changeset
621 AddArticulatedParts(v);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
622
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
623 _new_vehicle_id = v->index;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
624
18864
c8717ac828d0 (svn r23713) -Codechange: split VehicleMove() into two functions, to minimalize the work done where possible
truebrain <truebrain@openttd.org>
parents: 18855
diff changeset
625 VehicleUpdatePosition(v);
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
626 v->First()->ConsistChanged(false);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
627 UpdateTrainGroupID(v->First());
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
628
11351
11acaad65229 (svn r15701) -Fix [FS#2595]: Blame NewGRFs returning inconsistent information in purchase-list/after building before users have a chance to blame OpenTTD for incorrectly autorenewing/-replacing.
frosch <frosch@openttd.org>
parents: 11327
diff changeset
629 CheckConsistencyOfArticulatedVehicle(v);
13934
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
630
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
631 /* Try to connect the vehicle to one of free chains of wagons. */
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
632 Train *w;
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
633 FOR_ALL_TRAINS(w) {
14106
4b9804ff406b (svn r18653) -Fix [FS#3442]: when trying to attach a wagon to an existing free wagon chain, don't attach it to itself
rubidium <rubidium@openttd.org>
parents: 14100
diff changeset
634 if (w->tile == tile && ///< Same depot
4b9804ff406b (svn r18653) -Fix [FS#3442]: when trying to attach a wagon to an existing free wagon chain, don't attach it to itself
rubidium <rubidium@openttd.org>
parents: 14100
diff changeset
635 w->IsFreeWagon() && ///< A free wagon chain
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
636 w->engine_type == e->index && ///< Same type
14106
4b9804ff406b (svn r18653) -Fix [FS#3442]: when trying to attach a wagon to an existing free wagon chain, don't attach it to itself
rubidium <rubidium@openttd.org>
parents: 14100
diff changeset
637 w->First() != v && ///< Don't connect to ourself
4b9804ff406b (svn r18653) -Fix [FS#3442]: when trying to attach a wagon to an existing free wagon chain, don't attach it to itself
rubidium <rubidium@openttd.org>
parents: 14100
diff changeset
638 !(w->vehstatus & VS_CRASHED)) { ///< Not crashed/flooded
15890
52d3e250c976 (svn r20572) -Codechange: free/reserve some bits in the wagon move command to increase the vehicle pool limit
rubidium <rubidium@openttd.org>
parents: 15865
diff changeset
639 DoCommand(0, v->index | 1 << 20, w->Last()->index, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
13934
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
640 break;
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
641 }
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
642 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
643 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
644
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
645 return CommandCost();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
646 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
647
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
648 /** Move all free vehicles in the depot to the train */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
649 static void NormalizeTrainVehInDepot(const Train *u)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
650 {
12035
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
651 const Train *v;
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
652 FOR_ALL_TRAINS(v) {
12301
218a41d3d557 (svn r16718) -Codechange: make IsFreeWagon() member of Train
smatz <smatz@openttd.org>
parents: 12300
diff changeset
653 if (v->IsFreeWagon() && v->tile == u->tile &&
12035
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
654 v->track == TRACK_BIT_DEPOT) {
15890
52d3e250c976 (svn r20572) -Codechange: free/reserve some bits in the wagon move command to increase the vehicle pool limit
rubidium <rubidium@openttd.org>
parents: 15865
diff changeset
655 if (DoCommand(0, v->index | 1 << 20, u->index, DC_EXEC,
14314
60d313b887b8 (svn r18866) -Codechange: remove the CmdFailed(ret)/CmdSucceeded(ret) wrapper functions
rubidium <rubidium@openttd.org>
parents: 14302
diff changeset
656 CMD_MOVE_RAIL_VEHICLE).Failed())
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
657 break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
658 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
659 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
660 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
661
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
662 static void AddRearEngineToMultiheadedTrain(Train *v)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
663 {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
664 Train *u = new Train();
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
665 v->value >>= 1;
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
666 u->value = v->value;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
667 u->direction = v->direction;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
668 u->owner = v->owner;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
669 u->tile = v->tile;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
670 u->x_pos = v->x_pos;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
671 u->y_pos = v->y_pos;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
672 u->z_pos = v->z_pos;
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
673 u->track = TRACK_BIT_DEPOT;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
674 u->vehstatus = v->vehstatus & ~VS_STOPPED;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
675 u->spritenum = v->spritenum + 1;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
676 u->cargo_type = v->cargo_type;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
677 u->cargo_subtype = v->cargo_subtype;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
678 u->cargo_cap = v->cargo_cap;
20307
93d7e37bd666 (svn r25259) -Codechange: track capacities and usage of links
rubidium <rubidium@openttd.org>
parents: 20258
diff changeset
679 u->refit_cap = v->refit_cap;
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
680 u->railtype = v->railtype;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
681 u->engine_type = v->engine_type;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
682 u->build_year = v->build_year;
11903
c40e241e65ba (svn r16303) -Codechange: Use the question mark as default sprite when creating vehicles, instead of arbitrary sprites not related to the vehicle. (though that also applies to the question mark)
frosch <frosch@openttd.org>
parents: 11902
diff changeset
683 u->cur_image = SPR_IMG_QUERY;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
684 u->random_bits = VehicleRandomBits();
12303
ca4bc7e70a4d (svn r16720) -Codechange: make Set/ClearFrontEngine(), Set/ClearArticulatedPart(), Set/ClearWagon(), Set/ClearEngine(), Set/ClearFreeWagon() and Set/ClearMultiheaded() members of Train
smatz <smatz@openttd.org>
parents: 12302
diff changeset
685 v->SetMultiheaded();
ca4bc7e70a4d (svn r16720) -Codechange: make Set/ClearFrontEngine(), Set/ClearArticulatedPart(), Set/ClearWagon(), Set/ClearEngine(), Set/ClearFreeWagon() and Set/ClearMultiheaded() members of Train
smatz <smatz@openttd.org>
parents: 12302
diff changeset
686 u->SetMultiheaded();
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
687 v->SetNext(u);
18864
c8717ac828d0 (svn r23713) -Codechange: split VehicleMove() into two functions, to minimalize the work done where possible
truebrain <truebrain@openttd.org>
parents: 18855
diff changeset
688 VehicleUpdatePosition(u);
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
689
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
690 /* Now we need to link the front and rear engines together */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
691 v->other_multiheaded_part = u;
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
692 u->other_multiheaded_part = v;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
693 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
694
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
695 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
696 * Build a railroad vehicle.
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
697 * @param tile tile of the depot where rail-vehicle is built.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
698 * @param flags type of operation.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
699 * @param e the engine to build.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
700 * @param data bit 0 prevents any free cars from being added to the train.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
701 * @param ret[out] the vehicle that has been built.
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
702 * @return the cost of this operation or an error.
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
703 */
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
704 CommandCost CmdBuildRailVehicle(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **ret)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
705 {
13218
03a409fa4c29 (svn r17725) -Codechange: Reduce usage of EngInfo and XxxVehInfo, esp. when a Engine * is already present.
frosch <frosch@openttd.org>
parents: 13186
diff changeset
706 const RailVehicleInfo *rvi = &e->u.rail;
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
707
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
708 if (rvi->railveh_type == RAILVEH_WAGON) return CmdBuildRailWagon(tile, flags, e, ret);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
709
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
710 /* Check if depot and new engine uses the same kind of tracks *
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
711 * We need to see if the engine got power on the tile to avoid electric engines in non-electric depots */
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
712 if (!HasPowerOnRail(rvi->railtype, GetRailType(tile))) return CMD_ERROR;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
713
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
714 if (flags & DC_EXEC) {
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
715 DiagDirection dir = GetRailDepotDirection(tile);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
716 int x = TileX(tile) * TILE_SIZE + _vehicle_initial_x_fract[dir];
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
717 int y = TileY(tile) * TILE_SIZE + _vehicle_initial_y_fract[dir];
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
718
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
719 Train *v = new Train();
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
720 *ret = v;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
721 v->direction = DiagDirToDir(dir);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
722 v->tile = tile;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
723 v->owner = _current_company;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
724 v->x_pos = x;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
725 v->y_pos = y;
18255
b3cb40aab91b (svn r23091) -Codechange: rename some Get*Z functions to Get*PixelZ functions if they return the Z in pixels (like TilePixelHeight)
rubidium <rubidium@openttd.org>
parents: 18244
diff changeset
726 v->z_pos = GetSlopePixelZ(x, y);
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
727 v->track = TRACK_BIT_DEPOT;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
728 v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
729 v->spritenum = rvi->image_index;
11188
9e451d497d94 (svn r15541) -Revert (r15399): 'v->cargo_type' is also used in other places, which cannot accept CT_INVALID.
frosch <frosch@openttd.org>
parents: 11150
diff changeset
730 v->cargo_type = e->GetDefaultCargoType();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
731 v->cargo_cap = rvi->capacity;
20307
93d7e37bd666 (svn r25259) -Codechange: track capacities and usage of links
rubidium <rubidium@openttd.org>
parents: 20258
diff changeset
732 v->refit_cap = 0;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
733 v->last_station_visited = INVALID_STATION;
20307
93d7e37bd666 (svn r25259) -Codechange: track capacities and usage of links
rubidium <rubidium@openttd.org>
parents: 20258
diff changeset
734 v->last_loading_station = INVALID_STATION;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
735
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
736 v->engine_type = e->index;
16788
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
737 v->gcache.first_engine = INVALID_ENGINE; // needs to be set before first callback
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
738
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
739 v->reliability = e->reliability;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
740 v->reliability_spd_dec = e->reliability_spd_dec;
12165
299a364a2abc (svn r16581) -Codechange: unify the access to Engine::lifelength.
rubidium <rubidium@openttd.org>
parents: 12164
diff changeset
741 v->max_age = e->GetLifeLengthInDays();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
742
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
743 v->railtype = rvi->railtype;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
744 _new_vehicle_id = v->index;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
745
20061
aad15ac081e0 (svn r24994) -Codechange: Replace access to service_interval with accessors (peter1138)
rubidium <rubidium@openttd.org>
parents: 19973
diff changeset
746 v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_trains);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
747 v->date_of_last_service = _date;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
748 v->build_year = _cur_year;
11903
c40e241e65ba (svn r16303) -Codechange: Use the question mark as default sprite when creating vehicles, instead of arbitrary sprites not related to the vehicle. (though that also applies to the question mark)
frosch <frosch@openttd.org>
parents: 11902
diff changeset
749 v->cur_image = SPR_IMG_QUERY;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
750 v->random_bits = VehicleRandomBits();
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
751
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
752 if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
20065
a7ddf82cf8e2 (svn r24998) -Fix [FS#5137]: Set vehicle's service interval is percent flag appropriately on creation (peter1138)
rubidium <rubidium@openttd.org>
parents: 20061
diff changeset
753 v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
754
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
755 v->group_id = DEFAULT_GROUP;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
756
12303
ca4bc7e70a4d (svn r16720) -Codechange: make Set/ClearFrontEngine(), Set/ClearArticulatedPart(), Set/ClearWagon(), Set/ClearEngine(), Set/ClearFreeWagon() and Set/ClearMultiheaded() members of Train
smatz <smatz@openttd.org>
parents: 12302
diff changeset
757 v->SetFrontEngine();
ca4bc7e70a4d (svn r16720) -Codechange: make Set/ClearFrontEngine(), Set/ClearArticulatedPart(), Set/ClearWagon(), Set/ClearEngine(), Set/ClearFreeWagon() and Set/ClearMultiheaded() members of Train
smatz <smatz@openttd.org>
parents: 12302
diff changeset
758 v->SetEngine();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
759
18864
c8717ac828d0 (svn r23713) -Codechange: split VehicleMove() into two functions, to minimalize the work done where possible
truebrain <truebrain@openttd.org>
parents: 18855
diff changeset
760 VehicleUpdatePosition(v);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
761
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
762 if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
763 AddRearEngineToMultiheadedTrain(v);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
764 } else {
13219
39e3a60fd47f (svn r17726) -Cleanup: Remove some unneeded/unused parameters.
frosch <frosch@openttd.org>
parents: 13218
diff changeset
765 AddArticulatedParts(v);
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
766 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
767
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
768 v->ConsistChanged(false);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
769 UpdateTrainGroupID(v);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
770
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
771 if (!HasBit(data, 0) && !(flags & DC_AUTOREPLACE)) { // check if the cars should be added to the new vehicle
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
772 NormalizeTrainVehInDepot(v);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
773 }
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
774
11351
11acaad65229 (svn r15701) -Fix [FS#2595]: Blame NewGRFs returning inconsistent information in purchase-list/after building before users have a chance to blame OpenTTD for incorrectly autorenewing/-replacing.
frosch <frosch@openttd.org>
parents: 11327
diff changeset
775 CheckConsistencyOfArticulatedVehicle(v);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
776 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
777
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
778 return CommandCost();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
779 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
780
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
781 static Train *FindGoodVehiclePos(const Train *src)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
782 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
783 EngineID eng = src->engine_type;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
784 TileIndex tile = src->tile;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
785
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
786 Train *dst;
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
787 FOR_ALL_TRAINS(dst) {
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
788 if (dst->IsFreeWagon() && dst->tile == tile && !(dst->vehstatus & VS_CRASHED)) {
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
789 /* check so all vehicles in the line have the same engine. */
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
790 Train *t = dst;
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
791 while (t->engine_type == eng) {
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
792 t = t->Next();
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
793 if (t == NULL) return dst;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
794 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
795 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
796 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
797
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
798 return NULL;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
799 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
800
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
801 /** Helper type for lists/vectors of trains */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
802 typedef SmallVector<Train *, 16> TrainList;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
803
13924
9a4225ccc64b (svn r18456) -Codechange: make the length checking functionality of CmdMoveRailVehicle a separate function and simplify the logic
rubidium <rubidium@openttd.org>
parents: 13890
diff changeset
804 /**
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
805 * Make a backup of a train into a train list.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
806 * @param list to make the backup in
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
807 * @param t the train to make the backup of
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
808 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
809 static void MakeTrainBackup(TrainList &list, Train *t)
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
810 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
811 for (; t != NULL; t = t->Next()) *list.Append() = t;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
812 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
813
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
814 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
815 * Restore the train from the backup list.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
816 * @param list the train to restore.
13924
9a4225ccc64b (svn r18456) -Codechange: make the length checking functionality of CmdMoveRailVehicle a separate function and simplify the logic
rubidium <rubidium@openttd.org>
parents: 13890
diff changeset
817 */
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
818 static void RestoreTrainBackup(TrainList &list)
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
819 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
820 /* No train, nothing to do. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
821 if (list.Length() == 0) return;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
822
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
823 Train *prev = NULL;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
824 /* Iterate over the list and rebuild it. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
825 for (Train **iter = list.Begin(); iter != list.End(); iter++) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
826 Train *t = *iter;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
827 if (prev != NULL) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
828 prev->SetNext(t);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
829 } else if (t->Previous() != NULL) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
830 /* Make sure the head of the train is always the first in the chain. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
831 t->Previous()->SetNext(NULL);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
832 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
833 prev = t;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
834 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
835 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
836
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
837 /**
15002
baf0e12ad2b4 (svn r19614) -Codechange: "it's" => "its" where appropriate
smatz <smatz@openttd.org>
parents: 14898
diff changeset
838 * Remove the given wagon from its consist.
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
839 * @param part the part of the train to remove.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
840 * @param chain whether to remove the whole chain.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
841 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
842 static void RemoveFromConsist(Train *part, bool chain = false)
13924
9a4225ccc64b (svn r18456) -Codechange: make the length checking functionality of CmdMoveRailVehicle a separate function and simplify the logic
rubidium <rubidium@openttd.org>
parents: 13890
diff changeset
843 {
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
844 Train *tail = chain ? part->Last() : part->GetLastEnginePart();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
845
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
846 /* Unlink at the front, but make it point to the next
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
847 * vehicle after the to be remove part. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
848 if (part->Previous() != NULL) part->Previous()->SetNext(tail->Next());
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
849
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
850 /* Unlink at the back */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
851 tail->SetNext(NULL);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
852 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
853
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
854 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
855 * Inserts a chain into the train at dst.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
856 * @param dst the place where to append after.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
857 * @param chain the chain to actually add.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
858 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
859 static void InsertInConsist(Train *dst, Train *chain)
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
860 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
861 /* We do not want to add something in the middle of an articulated part. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
862 assert(dst->Next() == NULL || !dst->Next()->IsArticulatedPart());
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
863
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
864 chain->Last()->SetNext(dst->Next());
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
865 dst->SetNext(chain);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
866 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
867
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
868 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
869 * Normalise the dual heads in the train, i.e. if one is
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
870 * missing move that one to this train.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
871 * @param t the train to normalise.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
872 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
873 static void NormaliseDualHeads(Train *t)
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
874 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
875 for (; t != NULL; t = t->GetNextVehicle()) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
876 if (!t->IsMultiheaded() || !t->IsEngine()) continue;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
877
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
878 /* Make sure that there are no free cars before next engine */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
879 Train *u;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
880 for (u = t; u->Next() != NULL && !u->Next()->IsEngine(); u = u->Next()) {}
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
881
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
882 if (u == t->other_multiheaded_part) continue;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
883
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
884 /* Remove the part from the 'wrong' train */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
885 RemoveFromConsist(t->other_multiheaded_part);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
886 /* And add it to the 'right' train */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
887 InsertInConsist(u, t->other_multiheaded_part);
13924
9a4225ccc64b (svn r18456) -Codechange: make the length checking functionality of CmdMoveRailVehicle a separate function and simplify the logic
rubidium <rubidium@openttd.org>
parents: 13890
diff changeset
888 }
9a4225ccc64b (svn r18456) -Codechange: make the length checking functionality of CmdMoveRailVehicle a separate function and simplify the logic
rubidium <rubidium@openttd.org>
parents: 13890
diff changeset
889 }
9a4225ccc64b (svn r18456) -Codechange: make the length checking functionality of CmdMoveRailVehicle a separate function and simplify the logic
rubidium <rubidium@openttd.org>
parents: 13890
diff changeset
890
13926
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
891 /**
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
892 * Normalise the sub types of the parts in this chain.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
893 * @param chain the chain to normalise.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
894 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
895 static void NormaliseSubtypes(Train *chain)
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
896 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
897 /* Nothing to do */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
898 if (chain == NULL) return;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
899
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
900 /* We must be the first in the chain. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
901 assert(chain->Previous() == NULL);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
902
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
903 /* Set the appropriate bits for the first in the chain. */
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
904 if (chain->IsWagon()) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
905 chain->SetFreeWagon();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
906 } else {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
907 assert(chain->IsEngine());
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
908 chain->SetFrontEngine();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
909 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
910
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
911 /* Now clear the bits for the rest of the chain */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
912 for (Train *t = chain->Next(); t != NULL; t = t->Next()) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
913 t->ClearFreeWagon();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
914 t->ClearFrontEngine();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
915 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
916 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
917
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
918 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
919 * Check/validate whether we may actually build a new train.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
920 * @note All vehicles are/were 'heads' of their chains.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
921 * @param original_dst The original destination chain.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
922 * @param dst The destination chain after constructing the train.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
923 * @param original_dst The original source chain.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
924 * @param dst The source chain after constructing the train.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
925 * @return possible error of this command.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
926 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
927 static CommandCost CheckNewTrain(Train *original_dst, Train *dst, Train *original_src, Train *src)
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
928 {
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
929 /* Just add 'new' engines and subtract the original ones.
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
930 * If that's less than or equal to 0 we can be sure we did
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
931 * not add any engines (read: trains) along the way. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
932 if ((src != NULL && src->IsEngine() ? 1 : 0) +
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
933 (dst != NULL && dst->IsEngine() ? 1 : 0) -
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
934 (original_src != NULL && original_src->IsEngine() ? 1 : 0) -
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
935 (original_dst != NULL && original_dst->IsEngine() ? 1 : 0) <= 0) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
936 return CommandCost();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
937 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
939 /* Get a free unit number and check whether it's within the bounds.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
940 * There will always be a maximum of one new train. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
941 if (GetFreeUnitNumber(VEH_TRAIN) <= _settings_game.vehicle.max_trains) return CommandCost();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
942
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
943 return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
944 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
945
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
946 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
947 * Check whether the train parts can be attached.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
948 * @param t the train to check
13926
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
949 * @return possible error of this command.
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
950 */
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
951 static CommandCost CheckTrainAttachment(Train *t)
13926
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
952 {
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
953 /* No multi-part train, no need to check. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
954 if (t == NULL || t->Next() == NULL || !t->IsEngine()) return CommandCost();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
955
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
956 /* The maximum length for a train. For each part we decrease this by one
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
957 * and if the result is negative the train is simply too long. */
17221
edf36d7f3f86 (svn r21960) -Change: show the length of vehicles in tiles, instead of half tiles in the depot
rubidium <rubidium@openttd.org>
parents: 17194
diff changeset
958 int allowed_len = _settings_game.vehicle.max_train_length * TILE_SIZE - t->gcache.cached_veh_length;
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
959
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
960 Train *head = t;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
961 Train *prev = t;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
962
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
963 /* Break the prev -> t link so it always holds within the loop. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
964 t = t->Next();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
965 prev->SetNext(NULL);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
966
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
967 /* Make sure the cache is cleared. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
968 head->InvalidateNewGRFCache();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
969
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
970 while (t != NULL) {
17221
edf36d7f3f86 (svn r21960) -Change: show the length of vehicles in tiles, instead of half tiles in the depot
rubidium <rubidium@openttd.org>
parents: 17194
diff changeset
971 allowed_len -= t->gcache.cached_veh_length;
edf36d7f3f86 (svn r21960) -Change: show the length of vehicles in tiles, instead of half tiles in the depot
rubidium <rubidium@openttd.org>
parents: 17194
diff changeset
972
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
973 Train *next = t->Next();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
974
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
975 /* Unlink the to-be-added piece; it is already unlinked from the previous
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
976 * part due to the fact that the prev -> t link is broken. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
977 t->SetNext(NULL);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
978
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
979 /* Don't check callback for articulated or rear dual headed parts */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
980 if (!t->IsArticulatedPart() && !t->IsRearDualheaded()) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
981 /* Back up and clear the first_engine data to avoid using wagon override group */
16788
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
982 EngineID first_engine = t->gcache.first_engine;
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
983 t->gcache.first_engine = INVALID_ENGINE;
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
984
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
985 /* We don't want the cache to interfere. head's cache is cleared before
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
986 * the loop and after each callback does not need to be cleared here. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
987 t->InvalidateNewGRFCache();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
988
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
989 uint16 callback = GetVehicleCallbackParent(CBID_TRAIN_ALLOW_WAGON_ATTACH, 0, 0, head->engine_type, t, head);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
990
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
991 /* Restore original first_engine data */
16788
efb0333e5aad (svn r21521) -Codechange: Unify some cached values that were present in both road vehicles and trains.
terkhen <terkhen@openttd.org>
parents: 16785
diff changeset
992 t->gcache.first_engine = first_engine;
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
993
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
994 /* We do not want to remember any cached variables from the test run */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
995 t->InvalidateNewGRFCache();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
996 head->InvalidateNewGRFCache();
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
997
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
998 if (callback != CALLBACK_FAILED) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
999 /* A failing callback means everything is okay */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1000 StringID error = STR_NULL;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1001
18895
d0ca24721a85 (svn r23744) -Fix (r23142) [FS#4923]: Check the version of the right GRF.
frosch <frosch@openttd.org>
parents: 18891
diff changeset
1002 if (head->GetGRF()->grf_version < 8) {
18306
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1003 if (callback == 0xFD) error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1004 if (callback < 0xFD) error = GetGRFStringID(head->GetGRFID(), 0xD000 + callback);
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1005 if (callback >= 0x100) ErrorUnknownCallbackResult(head->GetGRFID(), CBID_TRAIN_ALLOW_WAGON_ATTACH, callback);
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1006 } else {
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1007 if (callback < 0x400) {
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1008 error = GetGRFStringID(head->GetGRFID(), 0xD000 + callback);
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1009 } else {
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1010 switch (callback) {
18335
f95316892277 (svn r23171) -Fix (r23142): Fix comment.
frosch <frosch@openttd.org>
parents: 18314
diff changeset
1011 case 0x400: // allow if railtypes match (always the case for OpenTTD)
f95316892277 (svn r23171) -Fix (r23142): Fix comment.
frosch <frosch@openttd.org>
parents: 18314
diff changeset
1012 case 0x401: // allow
18306
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1013 break;
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1014
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1015 default: // unknown reason -> disallow
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1016 case 0x402: // disallow attaching
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1017 error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1018 break;
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1019 }
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1020 }
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1021 }
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1022
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1023 if (error != STR_NULL) return_cmd_error(error);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1024 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1025 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1026
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1027 /* And link it to the new part. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1028 prev->SetNext(t);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1029 prev = t;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1030 t = next;
13926
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
1031 }
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
1032
17221
edf36d7f3f86 (svn r21960) -Change: show the length of vehicles in tiles, instead of half tiles in the depot
rubidium <rubidium@openttd.org>
parents: 17194
diff changeset
1033 if (allowed_len < 0) return_cmd_error(STR_ERROR_TRAIN_TOO_LONG);
13926
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
1034 return CommandCost();
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
1035 }
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
1036
13927
77e7637c04c8 (svn r18459) -Codechange: move the new train validity checking (attach callbacks) to a separate function
rubidium <rubidium@openttd.org>
parents: 13926
diff changeset
1037 /**
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1038 * Validate whether we are going to create valid trains.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1039 * @note All vehicles are/were 'heads' of their chains.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1040 * @param original_dst The original destination chain.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1041 * @param dst The destination chain after constructing the train.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1042 * @param original_dst The original source chain.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1043 * @param dst The source chain after constructing the train.
15899
097904c36bde (svn r20582) -Fix [FS#4044]: Autoreplace can trigger an assertion when at the vehicle limit
rubidium <rubidium@openttd.org>
parents: 15891
diff changeset
1044 * @param check_limit Whether to check the vehicle limit.
13927
77e7637c04c8 (svn r18459) -Codechange: move the new train validity checking (attach callbacks) to a separate function
rubidium <rubidium@openttd.org>
parents: 13926
diff changeset
1045 * @return possible error of this command.
77e7637c04c8 (svn r18459) -Codechange: move the new train validity checking (attach callbacks) to a separate function
rubidium <rubidium@openttd.org>
parents: 13926
diff changeset
1046 */
15899
097904c36bde (svn r20582) -Fix [FS#4044]: Autoreplace can trigger an assertion when at the vehicle limit
rubidium <rubidium@openttd.org>
parents: 15891
diff changeset
1047 static CommandCost ValidateTrains(Train *original_dst, Train *dst, Train *original_src, Train *src, bool check_limit)
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1048 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1049 /* Check whether we may actually construct the trains. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1050 CommandCost ret = CheckTrainAttachment(src);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1051 if (ret.Failed()) return ret;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1052 ret = CheckTrainAttachment(dst);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1053 if (ret.Failed()) return ret;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1054
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1055 /* Check whether we need to build a new train. */
15899
097904c36bde (svn r20582) -Fix [FS#4044]: Autoreplace can trigger an assertion when at the vehicle limit
rubidium <rubidium@openttd.org>
parents: 15891
diff changeset
1056 return check_limit ? CheckNewTrain(original_dst, dst, original_src, src) : CommandCost();
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1057 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1058
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1059 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1060 * Arrange the trains in the wanted way.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1061 * @param dst_head The destination chain of the to be moved vehicle.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1062 * @param dst The destination for the to be moved vehicle.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1063 * @param src_head The source chain of the to be moved vehicle.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1064 * @param src The to be moved vehicle.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1065 * @param move_chain Whether to move all vehicles after src or not.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1066 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1067 static void ArrangeTrains(Train **dst_head, Train *dst, Train **src_head, Train *src, bool move_chain)
13927
77e7637c04c8 (svn r18459) -Codechange: move the new train validity checking (attach callbacks) to a separate function
rubidium <rubidium@openttd.org>
parents: 13926
diff changeset
1068 {
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1069 /* First determine the front of the two resulting trains */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1070 if (*src_head == *dst_head) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1071 /* If we aren't moving part(s) to a new train, we are just moving the
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1072 * front back and there is not destination head. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1073 *dst_head = NULL;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1074 } else if (*dst_head == NULL) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1075 /* If we are moving to a new train the head of the move train would become
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1076 * the head of the new vehicle. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1077 *dst_head = src;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1078 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1079
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1080 if (src == *src_head) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1081 /* If we are moving the front of a train then we are, in effect, creating
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1082 * a new head for the train. Point to that. Unless we are moving the whole
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1083 * train in which case there is not 'source' train anymore.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1084 * In case we are a multiheaded part we want the complete thing to come
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1085 * with us, so src->GetNextUnit(), however... when we are e.g. a wagon
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1086 * that is followed by a rear multihead we do not want to include that. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1087 *src_head = move_chain ? NULL :
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1088 (src->IsMultiheaded() ? src->GetNextUnit() : src->GetNextVehicle());
13927
77e7637c04c8 (svn r18459) -Codechange: move the new train validity checking (attach callbacks) to a separate function
rubidium <rubidium@openttd.org>
parents: 13926
diff changeset
1089 }
77e7637c04c8 (svn r18459) -Codechange: move the new train validity checking (attach callbacks) to a separate function
rubidium <rubidium@openttd.org>
parents: 13926
diff changeset
1090
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1091 /* Now it's just simply removing the part that we are going to move from the
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1092 * source train and *if* the destination is a not a new train add the chain
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1093 * at the destination location. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1094 RemoveFromConsist(src, move_chain);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1095 if (*dst_head != src) InsertInConsist(dst, src);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1096
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1097 /* Now normalise the dual heads, that is move the dual heads around in such
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1098 * a way that the head and rear of a dual head are in the same train */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1099 NormaliseDualHeads(*src_head);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1100 NormaliseDualHeads(*dst_head);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1101 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1102
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1103 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1104 * Normalise the head of the train again, i.e. that is tell the world that
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1105 * we have changed and update all kinds of variables.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1106 * @param head the train to update.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1107 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1108 static void NormaliseTrainHead(Train *head)
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1109 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1110 /* Not much to do! */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1111 if (head == NULL) return;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1112
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1113 /* Tell the 'world' the train changed. */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
1114 head->ConsistChanged(false);
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1115 UpdateTrainGroupID(head);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1116
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1117 /* Not a front engine, i.e. a free wagon chain. No need to do more. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1118 if (!head->IsFrontEngine()) return;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1119
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1120 /* Update the refit button and window */
19900
b4c4f1c24136 (svn r24833) -Codechange: Replace magic numbers for invalidating vehicle-related windows with an enum.
michi_cc <michi_cc@openttd.org>
parents: 19899
diff changeset
1121 InvalidateWindowData(WC_VEHICLE_REFIT, head->index, VIWD_CONSIST_CHANGED);
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
1122 SetWindowWidgetDirty(WC_VEHICLE_VIEW, head->index, WID_VV_REFIT);
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1123
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1124 /* If we don't have a unit number yet, set one. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1125 if (head->unitnumber != 0) return;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1126 head->unitnumber = GetFreeUnitNumber(VEH_TRAIN);
13927
77e7637c04c8 (svn r18459) -Codechange: move the new train validity checking (attach callbacks) to a separate function
rubidium <rubidium@openttd.org>
parents: 13926
diff changeset
1127 }
77e7637c04c8 (svn r18459) -Codechange: move the new train validity checking (attach callbacks) to a separate function
rubidium <rubidium@openttd.org>
parents: 13926
diff changeset
1128
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1129 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1130 * Move a rail vehicle around inside the depot.
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1131 * @param tile unused
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1132 * @param flags type of operation
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9923
diff changeset
1133 * Note: DC_AUTOREPLACE is set when autoreplace tries to undo its modifications or moves vehicles to temporary locations inside the depot.
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1134 * @param p1 various bitstuffed elements
15890
52d3e250c976 (svn r20572) -Codechange: free/reserve some bits in the wagon move command to increase the vehicle pool limit
rubidium <rubidium@openttd.org>
parents: 15865
diff changeset
1135 * - p1 (bit 0 - 19) source vehicle index
52d3e250c976 (svn r20572) -Codechange: free/reserve some bits in the wagon move command to increase the vehicle pool limit
rubidium <rubidium@openttd.org>
parents: 15865
diff changeset
1136 * - p1 (bit 20) move all vehicles following the source vehicle
52d3e250c976 (svn r20572) -Codechange: free/reserve some bits in the wagon move command to increase the vehicle pool limit
rubidium <rubidium@openttd.org>
parents: 15865
diff changeset
1137 * @param p2 what wagon to put the source wagon AFTER, XXX - INVALID_VEHICLE to make a new line
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1138 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1139 * @return the cost of this operation or an error
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1140 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1141 CommandCost CmdMoveRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1142 {
15890
52d3e250c976 (svn r20572) -Codechange: free/reserve some bits in the wagon move command to increase the vehicle pool limit
rubidium <rubidium@openttd.org>
parents: 15865
diff changeset
1143 VehicleID s = GB(p1, 0, 20);
52d3e250c976 (svn r20572) -Codechange: free/reserve some bits in the wagon move command to increase the vehicle pool limit
rubidium <rubidium@openttd.org>
parents: 15865
diff changeset
1144 VehicleID d = GB(p2, 0, 20);
52d3e250c976 (svn r20572) -Codechange: free/reserve some bits in the wagon move command to increase the vehicle pool limit
rubidium <rubidium@openttd.org>
parents: 15865
diff changeset
1145 bool move_chain = HasBit(p1, 20);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1146
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1147 Train *src = Train::GetIfValid(s);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1148 if (src == NULL) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1149
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1150 CommandCost ret = CheckOwnership(src->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1151 if (ret.Failed()) return ret;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1152
7696
7ae4d4720c11 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium <rubidium@openttd.org>
parents: 7695
diff changeset
1153 /* Do not allow moving crashed vehicles inside the depot, it is likely to cause asserts later */
12089
482bdd64bbe8 (svn r16498) -Codechange: Remove hardly used HASBITS.
frosch <frosch@openttd.org>
parents: 12085
diff changeset
1154 if (src->vehstatus & VS_CRASHED) return CMD_ERROR;
7696
7ae4d4720c11 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium <rubidium@openttd.org>
parents: 7695
diff changeset
1155
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1156 /* if nothing is selected as destination, try and find a matching vehicle to drag to. */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1157 Train *dst;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1158 if (d == INVALID_VEHICLE) {
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
1159 dst = src->IsEngine() ? NULL : FindGoodVehiclePos(src);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1160 } else {
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1161 dst = Train::GetIfValid(d);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1162 if (dst == NULL) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1163
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1164 CommandCost ret = CheckOwnership(dst->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1165 if (ret.Failed()) return ret;
7696
7ae4d4720c11 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium <rubidium@openttd.org>
parents: 7695
diff changeset
1166
7ae4d4720c11 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium <rubidium@openttd.org>
parents: 7695
diff changeset
1167 /* Do not allow appending to crashed vehicles, too */
12089
482bdd64bbe8 (svn r16498) -Codechange: Remove hardly used HASBITS.
frosch <frosch@openttd.org>
parents: 12085
diff changeset
1168 if (dst->vehstatus & VS_CRASHED) return CMD_ERROR;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1169 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1170
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1171 /* if an articulated part is being handled, deal with its parent vehicle */
12450
af266072d46c (svn r16884) -Codechange: Add Train::GetFirstEnginePart() and use it.
frosch <frosch@openttd.org>
parents: 12433
diff changeset
1172 src = src->GetFirstEnginePart();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1173 if (dst != NULL) {
12450
af266072d46c (svn r16884) -Codechange: Add Train::GetFirstEnginePart() and use it.
frosch <frosch@openttd.org>
parents: 12433
diff changeset
1174 dst = dst->GetFirstEnginePart();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1175 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1176
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1177 /* don't move the same vehicle.. */
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
1178 if (src == dst) return CommandCost();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1179
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1180 /* locate the head of the two chains */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1181 Train *src_head = src->First();
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1182 Train *dst_head;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1183 if (dst != NULL) {
7497
797ff0b0e0a5 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium <rubidium@openttd.org>
parents: 7493
diff changeset
1184 dst_head = dst->First();
6136
f05e23ab634f (svn r8878) -Fix
tron <tron@openttd.org>
parents: 6114
diff changeset
1185 if (dst_head->tile != src_head->tile) return CMD_ERROR;
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1186 /* Now deal with articulated part of destination wagon */
12306
d0ff55a976ad (svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents: 12303
diff changeset
1187 dst = dst->GetLastEnginePart();
6136
f05e23ab634f (svn r8878) -Fix
tron <tron@openttd.org>
parents: 6114
diff changeset
1188 } else {
f05e23ab634f (svn r8878) -Fix
tron <tron@openttd.org>
parents: 6114
diff changeset
1189 dst_head = NULL;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1190 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1191
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
1192 if (src->IsRearDualheaded()) return_cmd_error(STR_ERROR_REAR_ENGINE_FOLLOW_FRONT);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1193
13941
5a2efeabdd21 (svn r18473) -Fix (r18470): crash when moving a multiheaded part to be just after itself
rubidium <rubidium@openttd.org>
parents: 13940
diff changeset
1194 /* When moving all wagons, we can't have the same src_head and dst_head */
13926
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
1195 if (move_chain && src_head == dst_head) return CommandCost();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1196
13941
5a2efeabdd21 (svn r18473) -Fix (r18470): crash when moving a multiheaded part to be just after itself
rubidium <rubidium@openttd.org>
parents: 13940
diff changeset
1197 /* When moving a multiheaded part to be place after itself, bail out. */
13942
06db5e527f36 (svn r18474) -Fix (r18470): in some odd corner cases the group (vehicle) counts could be off
rubidium <rubidium@openttd.org>
parents: 13941
diff changeset
1198 if (!move_chain && dst != NULL && dst->IsRearDualheaded() && src == dst->other_multiheaded_part) return CommandCost();
13941
5a2efeabdd21 (svn r18473) -Fix (r18470): crash when moving a multiheaded part to be just after itself
rubidium <rubidium@openttd.org>
parents: 13940
diff changeset
1199
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1200 /* Check if all vehicles in the source train are stopped inside a depot. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1201 if (!src_head->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1202
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1203 /* Check if all vehicles in the destination train are stopped inside a depot. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1204 if (dst_head != NULL && !dst_head->IsStoppedInDepot()) return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1205
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1206 /* First make a backup of the order of the trains. That way we can do
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1207 * whatever we want with the order and later on easily revert. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1208 TrainList original_src;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1209 TrainList original_dst;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1210
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1211 MakeTrainBackup(original_src, src_head);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1212 MakeTrainBackup(original_dst, dst_head);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1213
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1214 /* Also make backup of the original heads as ArrangeTrains can change them.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1215 * For the destination head we do not care if it is the same as the source
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1216 * head because in that case it's just a copy. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1217 Train *original_src_head = src_head;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1218 Train *original_dst_head = (dst_head == src_head ? NULL : dst_head);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1219
18832
1d0a58216701 (svn r23681) -Fix [FS#4914]: vehicle numbers got misaccounted when autoreplacing failed due to length checks
rubidium <rubidium@openttd.org>
parents: 18774
diff changeset
1220 /* We want this information from before the rearrangement, but execute this after the validation. */
1d0a58216701 (svn r23681) -Fix [FS#4914]: vehicle numbers got misaccounted when autoreplacing failed due to length checks
rubidium <rubidium@openttd.org>
parents: 18774
diff changeset
1221 bool original_src_head_front_engine = original_src_head != NULL && original_src_head->IsFrontEngine();
1d0a58216701 (svn r23681) -Fix [FS#4914]: vehicle numbers got misaccounted when autoreplacing failed due to length checks
rubidium <rubidium@openttd.org>
parents: 18774
diff changeset
1222 bool original_dst_head_front_engine = original_dst_head != NULL && original_dst_head->IsFrontEngine();
18159
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1223
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1224 /* (Re)arrange the trains in the wanted arrangement. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1225 ArrangeTrains(&dst_head, dst, &src_head, src, move_chain);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1226
9928
0cc9c6c36c43 (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch <frosch@openttd.org>
parents: 9923
diff changeset
1227 if ((flags & DC_AUTOREPLACE) == 0) {
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1228 /* If the autoreplace flag is set we do not need to test for the validity
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1229 * because we are going to revert the train to its original state. As we
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1230 * assume the original state was correct autoreplace can skip this. */
15899
097904c36bde (svn r20582) -Fix [FS#4044]: Autoreplace can trigger an assertion when at the vehicle limit
rubidium <rubidium@openttd.org>
parents: 15891
diff changeset
1231 CommandCost ret = ValidateTrains(original_dst_head, dst_head, original_src_head, src_head, true);
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1232 if (ret.Failed()) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1233 /* Restore the train we had. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1234 RestoreTrainBackup(original_src);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1235 RestoreTrainBackup(original_dst);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1236 return ret;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1237 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1238 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1239
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1240 /* do it? */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1241 if (flags & DC_EXEC) {
18832
1d0a58216701 (svn r23681) -Fix [FS#4914]: vehicle numbers got misaccounted when autoreplacing failed due to length checks
rubidium <rubidium@openttd.org>
parents: 18774
diff changeset
1242 /* Remove old heads from the statistics */
1d0a58216701 (svn r23681) -Fix [FS#4914]: vehicle numbers got misaccounted when autoreplacing failed due to length checks
rubidium <rubidium@openttd.org>
parents: 18774
diff changeset
1243 if (original_src_head_front_engine) GroupStatistics::CountVehicle(original_src_head, -1);
1d0a58216701 (svn r23681) -Fix [FS#4914]: vehicle numbers got misaccounted when autoreplacing failed due to length checks
rubidium <rubidium@openttd.org>
parents: 18774
diff changeset
1244 if (original_dst_head_front_engine) GroupStatistics::CountVehicle(original_dst_head, -1);
1d0a58216701 (svn r23681) -Fix [FS#4914]: vehicle numbers got misaccounted when autoreplacing failed due to length checks
rubidium <rubidium@openttd.org>
parents: 18774
diff changeset
1245
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1246 /* First normalise the sub types of the chains. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1247 NormaliseSubtypes(src_head);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1248 NormaliseSubtypes(dst_head);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1249
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1250 /* There are 14 different cases:
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1251 * 1) front engine gets moved to a new train, it stays a front engine.
16349
b1fc0cfd2943 (svn r21060) -Doc: Improved wording of comments (mostly by __ln__)
alberth <alberth@openttd.org>
parents: 16306
diff changeset
1252 * a) the 'next' part is a wagon that becomes a free wagon chain.
b1fc0cfd2943 (svn r21060) -Doc: Improved wording of comments (mostly by __ln__)
alberth <alberth@openttd.org>
parents: 16306
diff changeset
1253 * b) the 'next' part is an engine that becomes a front engine.
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1254 * c) there is no 'next' part, nothing else happens
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1255 * 2) front engine gets moved to another train, it is not a front engine anymore
16349
b1fc0cfd2943 (svn r21060) -Doc: Improved wording of comments (mostly by __ln__)
alberth <alberth@openttd.org>
parents: 16306
diff changeset
1256 * a) the 'next' part is a wagon that becomes a free wagon chain.
b1fc0cfd2943 (svn r21060) -Doc: Improved wording of comments (mostly by __ln__)
alberth <alberth@openttd.org>
parents: 16306
diff changeset
1257 * b) the 'next' part is an engine that becomes a front engine.
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1258 * c) there is no 'next' part, nothing else happens
16481
89cd59264ee1 (svn r21205) -Fix [FS#4207]: Under some conditions, group count would be wrong after moving train engines in the depot window.
terkhen <terkhen@openttd.org>
parents: 16466
diff changeset
1259 * 3) front engine gets moved to later in the current train, it is not a front engine anymore.
16349
b1fc0cfd2943 (svn r21060) -Doc: Improved wording of comments (mostly by __ln__)
alberth <alberth@openttd.org>
parents: 16306
diff changeset
1260 * a) the 'next' part is a wagon that becomes a free wagon chain.
b1fc0cfd2943 (svn r21060) -Doc: Improved wording of comments (mostly by __ln__)
alberth <alberth@openttd.org>
parents: 16306
diff changeset
1261 * b) the 'next' part is an engine that becomes a front engine.
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1262 * 4) free wagon gets moved
16349
b1fc0cfd2943 (svn r21060) -Doc: Improved wording of comments (mostly by __ln__)
alberth <alberth@openttd.org>
parents: 16306
diff changeset
1263 * a) the 'next' part is a wagon that becomes a free wagon chain.
b1fc0cfd2943 (svn r21060) -Doc: Improved wording of comments (mostly by __ln__)
alberth <alberth@openttd.org>
parents: 16306
diff changeset
1264 * b) the 'next' part is an engine that becomes a front engine.
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1265 * c) there is no 'next' part, nothing else happens
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1266 * 5) non front engine gets moved and becomes a new train, nothing else happens
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1267 * 6) non front engine gets moved within a train / to another train, nothing hapens
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1268 * 7) wagon gets moved, nothing happens
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1269 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1270 if (src == original_src_head && src->IsEngine() && !src->IsFrontEngine()) {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1271 /* Cases #2 and #3: the front engine gets trashed. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1272 DeleteWindowById(WC_VEHICLE_VIEW, src->index);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1273 DeleteWindowById(WC_VEHICLE_ORDERS, src->index);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1274 DeleteWindowById(WC_VEHICLE_REFIT, src->index);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1275 DeleteWindowById(WC_VEHICLE_DETAILS, src->index);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1276 DeleteWindowById(WC_VEHICLE_TIMETABLE, src->index);
18607
b8fc708229a7 (svn r23454) -Change: Mark company window dirty when moving a rail engine creates or deletes a train.
peter1138 <peter1138@openttd.org>
parents: 18511
diff changeset
1277 SetWindowDirty(WC_COMPANY, _current_company);
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1278
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1279 /* Delete orders, group stuff and the unit number as we're not the
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1280 * front of any vehicle anymore. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1281 DeleteVehicleOrders(src);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1282 RemoveVehicleFromGroup(src);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1283 src->unitnumber = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1284 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1285
13942
06db5e527f36 (svn r18474) -Fix (r18470): in some odd corner cases the group (vehicle) counts could be off
rubidium <rubidium@openttd.org>
parents: 13941
diff changeset
1286 /* We weren't a front engine but are becoming one. So
06db5e527f36 (svn r18474) -Fix (r18470): in some odd corner cases the group (vehicle) counts could be off
rubidium <rubidium@openttd.org>
parents: 13941
diff changeset
1287 * we should be put in the default group. */
06db5e527f36 (svn r18474) -Fix (r18470): in some odd corner cases the group (vehicle) counts could be off
rubidium <rubidium@openttd.org>
parents: 13941
diff changeset
1288 if (original_src_head != src && dst_head == src) {
06db5e527f36 (svn r18474) -Fix (r18470): in some odd corner cases the group (vehicle) counts could be off
rubidium <rubidium@openttd.org>
parents: 13941
diff changeset
1289 SetTrainGroupID(src, DEFAULT_GROUP);
18607
b8fc708229a7 (svn r23454) -Change: Mark company window dirty when moving a rail engine creates or deletes a train.
peter1138 <peter1138@openttd.org>
parents: 18511
diff changeset
1290 SetWindowDirty(WC_COMPANY, _current_company);
13942
06db5e527f36 (svn r18474) -Fix (r18470): in some odd corner cases the group (vehicle) counts could be off
rubidium <rubidium@openttd.org>
parents: 13941
diff changeset
1291 }
06db5e527f36 (svn r18474) -Fix (r18470): in some odd corner cases the group (vehicle) counts could be off
rubidium <rubidium@openttd.org>
parents: 13941
diff changeset
1292
18159
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1293 /* Add new heads to statistics */
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1294 if (src_head != NULL && src_head->IsFrontEngine()) GroupStatistics::CountVehicle(src_head, 1);
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1295 if (dst_head != NULL && dst_head->IsFrontEngine()) GroupStatistics::CountVehicle(dst_head, 1);
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1296
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1297 /* Handle 'new engine' part of cases #1b, #2b, #3b, #4b and #5 in NormaliseTrainHead. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1298 NormaliseTrainHead(src_head);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1299 NormaliseTrainHead(dst_head);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1300
18834
573054818505 (svn r23683) -Fix [FS#4912]-ish: when fitting another engine the cargo capacity of wagons could become lower, causing them to contain more than they should. This caused the cargo transfer from the replaced parts to put even more stuff in the already full wagon. Prevent this from happening by reducing the amount of cargo in the vehicle to the capacity when moving vehicles/wagons around, or when autoreplacing
rubidium <rubidium@openttd.org>
parents: 18832
diff changeset
1301 if ((flags & DC_NO_CARGO_CAP_CHECK) == 0) {
573054818505 (svn r23683) -Fix [FS#4912]-ish: when fitting another engine the cargo capacity of wagons could become lower, causing them to contain more than they should. This caused the cargo transfer from the replaced parts to put even more stuff in the already full wagon. Prevent this from happening by reducing the amount of cargo in the vehicle to the capacity when moving vehicles/wagons around, or when autoreplacing
rubidium <rubidium@openttd.org>
parents: 18832
diff changeset
1302 CheckCargoCapacity(src_head);
573054818505 (svn r23683) -Fix [FS#4912]-ish: when fitting another engine the cargo capacity of wagons could become lower, causing them to contain more than they should. This caused the cargo transfer from the replaced parts to put even more stuff in the already full wagon. Prevent this from happening by reducing the amount of cargo in the vehicle to the capacity when moving vehicles/wagons around, or when autoreplacing
rubidium <rubidium@openttd.org>
parents: 18832
diff changeset
1303 CheckCargoCapacity(dst_head);
573054818505 (svn r23683) -Fix [FS#4912]-ish: when fitting another engine the cargo capacity of wagons could become lower, causing them to contain more than they should. This caused the cargo transfer from the replaced parts to put even more stuff in the already full wagon. Prevent this from happening by reducing the amount of cargo in the vehicle to the capacity when moving vehicles/wagons around, or when autoreplacing
rubidium <rubidium@openttd.org>
parents: 18832
diff changeset
1304 }
573054818505 (svn r23683) -Fix [FS#4912]-ish: when fitting another engine the cargo capacity of wagons could become lower, causing them to contain more than they should. This caused the cargo transfer from the replaced parts to put even more stuff in the already full wagon. Prevent this from happening by reducing the amount of cargo in the vehicle to the capacity when moving vehicles/wagons around, or when autoreplacing
rubidium <rubidium@openttd.org>
parents: 18832
diff changeset
1305
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1306 /* We are undoubtedly changing something in the depot and train list. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1307 InvalidateWindowData(WC_VEHICLE_DEPOT, src->tile);
9297
add307f2e6ea (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium <rubidium@openttd.org>
parents: 9274
diff changeset
1308 InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
13938
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1309 } else {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1310 /* We don't want to execute what we're just tried. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1311 RestoreTrainBackup(original_src);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1312 RestoreTrainBackup(original_dst);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1313 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1314
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
1315 return CommandCost();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1316 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1317
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1318 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1319 * Sell a (single) train wagon/engine.
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1320 * @param flags type of operation
15850
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1321 * @param t the train wagon to sell
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1322 * @param data the selling mode
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1323 * - data = 0: only sell the single dragged wagon/engine (and any belonging rear-engines)
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1324 * - data = 1: sell the vehicle and all vehicles following it in the chain
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1325 * if the wagon is dragged, don't delete the possibly belonging rear-engine to some front
15865
270f9b0689cc (svn r20547) -Change: the way order backups are performed. Now restoring an order doesn't require up to 765 commands.
rubidium <rubidium@openttd.org>
parents: 15854
diff changeset
1326 * @param user the user for the order backup.
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1327 * @return the cost of this operation or an error
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1328 */
15865
270f9b0689cc (svn r20547) -Change: the way order backups are performed. Now restoring an order doesn't require up to 765 commands.
rubidium <rubidium@openttd.org>
parents: 15854
diff changeset
1329 CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *t, uint16 data, uint32 user)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1330 {
6789
26a3c3985d67 (svn r10028) -Fix (r2270/r2951): When deleting the first engine of a train with multiple engines, only reopen the train window if the player had the original train window open. This fixes 'random' windows opening for multiple players of the same company.
peter1138 <peter1138@openttd.org>
parents: 6788
diff changeset
1331 /* Check if we deleted a vehicle window */
26a3c3985d67 (svn r10028) -Fix (r2270/r2951): When deleting the first engine of a train with multiple engines, only reopen the train window if the player had the original train window open. This fixes 'random' windows opening for multiple players of the same company.
peter1138 <peter1138@openttd.org>
parents: 6788
diff changeset
1332 Window *w = NULL;
26a3c3985d67 (svn r10028) -Fix (r2270/r2951): When deleting the first engine of a train with multiple engines, only reopen the train window if the player had the original train window open. This fixes 'random' windows opening for multiple players of the same company.
peter1138 <peter1138@openttd.org>
parents: 6788
diff changeset
1333
13940
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1334 /* Sell a chain of vehicles or not? */
15850
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1335 bool sell_chain = HasBit(data, 0);
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1336
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1337 Train *v = Train::From(t)->GetFirstEnginePart();
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1338 Train *first = v->First();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1339
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
1340 if (v->IsRearDualheaded()) return_cmd_error(STR_ERROR_REAR_ENGINE_FOLLOW_FRONT);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1341
13940
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1342 /* First make a backup of the order of the train. That way we can do
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1343 * whatever we want with the order and later on easily revert. */
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1344 TrainList original;
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1345 MakeTrainBackup(original, first);
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1346
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1347 /* We need to keep track of the new head and the head of what we're going to sell. */
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1348 Train *new_head = first;
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1349 Train *sell_head = NULL;
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1350
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1351 /* Split the train in the wanted way. */
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1352 ArrangeTrains(&sell_head, NULL, &new_head, v, sell_chain);
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1353
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1354 /* We don't need to validate the second train; it's going to be sold. */
15899
097904c36bde (svn r20582) -Fix [FS#4044]: Autoreplace can trigger an assertion when at the vehicle limit
rubidium <rubidium@openttd.org>
parents: 15891
diff changeset
1355 CommandCost ret = ValidateTrains(NULL, NULL, first, new_head, (flags & DC_AUTOREPLACE) == 0);
13940
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1356 if (ret.Failed()) {
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1357 /* Restore the train we had. */
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1358 RestoreTrainBackup(original);
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1359 return ret;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1360 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1361
8230
5b61305fcdd4 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium <rubidium@openttd.org>
parents: 8226
diff changeset
1362 CommandCost cost(EXPENSES_NEW_VEHICLES);
13940
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1363 for (Train *t = sell_head; t != NULL; t = t->Next()) cost.AddCost(-t->value);
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1364
17284
acb919c16a29 (svn r22024) -Fix [FS#4468]: verify we can allocate an OrderList before we actually try to do so (Rubidium)
smatz <smatz@openttd.org>
parents: 17277
diff changeset
1365 if (first->orders.list == NULL && !OrderList::CanAllocateItem()) {
acb919c16a29 (svn r22024) -Fix [FS#4468]: verify we can allocate an OrderList before we actually try to do so (Rubidium)
smatz <smatz@openttd.org>
parents: 17277
diff changeset
1366 return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
acb919c16a29 (svn r22024) -Fix [FS#4468]: verify we can allocate an OrderList before we actually try to do so (Rubidium)
smatz <smatz@openttd.org>
parents: 17277
diff changeset
1367 }
acb919c16a29 (svn r22024) -Fix [FS#4468]: verify we can allocate an OrderList before we actually try to do so (Rubidium)
smatz <smatz@openttd.org>
parents: 17277
diff changeset
1368
13940
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1369 /* do it? */
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1370 if (flags & DC_EXEC) {
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1371 /* First normalise the sub types of the chain. */
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1372 NormaliseSubtypes(new_head);
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1373
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1374 if (v == first && v->IsEngine() && !sell_chain && new_head != NULL && new_head->IsFrontEngine()) {
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1375 /* We are selling the front engine. In this case we want to
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
1376 * 'give' the order, unit number and such to the new head. */
13940
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1377 new_head->orders.list = first->orders.list;
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1378 new_head->AddToShared(first);
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1379 DeleteVehicleOrders(first);
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1380
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1381 /* Copy other important data from the front engine */
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1382 new_head->CopyVehicleConfigAndStatistics(first);
18159
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1383 GroupStatistics::CountVehicle(new_head, 1); // after copying over the profit
13940
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1384
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1385 /* If we deleted a window then open a new one for the 'new' train */
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1386 if (IsLocalCompany() && w != NULL) ShowVehicleViewWindow(new_head);
15891
67b39929d6a2 (svn r20573) -Codechange: free/reserve some bits in the sell vehicle command to increase the vehicle pool limit
rubidium <rubidium@openttd.org>
parents: 15890
diff changeset
1387 } else if (v->IsPrimaryVehicle() && data & (MAKE_ORDER_BACKUP_FLAG >> 20)) {
15865
270f9b0689cc (svn r20547) -Change: the way order backups are performed. Now restoring an order doesn't require up to 765 commands.
rubidium <rubidium@openttd.org>
parents: 15854
diff changeset
1388 OrderBackup::Backup(v, user);
13940
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1389 }
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1390
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1391 /* We need to update the information about the train. */
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1392 NormaliseTrainHead(new_head);
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1393
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1394 /* We are undoubtedly changing something in the depot and train list. */
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1395 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1396 InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1397
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1398 /* Actually delete the sold 'goods' */
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1399 delete sell_head;
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1400 } else {
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1401 /* We don't want to execute what we're just tried. */
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1402 RestoreTrainBackup(original);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1403 }
13940
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1404
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1405 return cost;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1406 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1407
6558
2fc024ee8217 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium <rubidium@openttd.org>
parents: 6554
diff changeset
1408 void Train::UpdateDeltaXY(Direction direction)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1409 {
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1410 /* Set common defaults. */
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1411 this->x_offs = -1;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1412 this->y_offs = -1;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1413 this->x_extent = 3;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1414 this->y_extent = 3;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1415 this->z_extent = 6;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1416 this->x_bb_offs = 0;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1417 this->y_bb_offs = 0;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1418
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1419 if (!IsDiagonalDirection(direction)) {
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1420 static const int _sign_table[] =
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1421 {
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1422 // x, y
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1423 -1, -1, // DIR_N
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1424 -1, 1, // DIR_E
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1425 1, 1, // DIR_S
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1426 1, -1, // DIR_W
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1427 };
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1428
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1429 int half_shorten = (VEHICLE_LENGTH - this->gcache.cached_veh_length) / 2;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1430
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1431 /* For all straight directions, move the bound box to the centre of the vehicle, but keep the size. */
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1432 this->x_offs -= half_shorten * _sign_table[direction];
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1433 this->y_offs -= half_shorten * _sign_table[direction + 1];
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1434 this->x_extent += this->x_bb_offs = half_shorten * _sign_table[direction];
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1435 this->y_extent += this->y_bb_offs = half_shorten * _sign_table[direction + 1];
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1436 } else {
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1437 switch (direction) {
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1438 /* Shorten southern corner of the bounding box according the vehicle length
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1439 * and center the bounding box on the vehicle. */
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1440 case DIR_NE:
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1441 this->x_offs = 1 - (this->gcache.cached_veh_length + 1) / 2;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1442 this->x_extent = this->gcache.cached_veh_length - 1;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1443 this->x_bb_offs = -1;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1444 break;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1445
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1446 case DIR_NW:
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1447 this->y_offs = 1 - (this->gcache.cached_veh_length + 1) / 2;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1448 this->y_extent = this->gcache.cached_veh_length - 1;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1449 this->y_bb_offs = -1;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1450 break;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1451
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1452 /* Move northern corner of the bounding box down according to vehicle length
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1453 * and center the bounding box on the vehicle. */
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1454 case DIR_SW:
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1455 this->x_offs = 1 + (this->gcache.cached_veh_length + 1) / 2 - VEHICLE_LENGTH;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1456 this->x_extent = VEHICLE_LENGTH - 1;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1457 this->x_bb_offs = VEHICLE_LENGTH - this->gcache.cached_veh_length - 1;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1458 break;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1459
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1460 case DIR_SE:
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1461 this->y_offs = 1 + (this->gcache.cached_veh_length + 1) / 2 - VEHICLE_LENGTH;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1462 this->y_extent = VEHICLE_LENGTH - 1;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1463 this->y_bb_offs = VEHICLE_LENGTH - this->gcache.cached_veh_length - 1;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1464 break;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1465
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1466 default:
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1467 NOT_REACHED();
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1468 }
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1469 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1470 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1471
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1472 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1473 * Mark a train as stuck and stop it if it isn't stopped right now.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1474 * @param v %Train to mark as being stuck.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1475 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1476 static void MarkTrainAsStuck(Train *v)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1477 {
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1478 if (!HasBit(v->flags, VRF_TRAIN_STUCK)) {
15604
ca959eb5d431 (svn r20266) -Doc: Mostly typo fixes, a few doxygen-related improvements.
alberth <alberth@openttd.org>
parents: 15579
diff changeset
1479 /* It is the first time the problem occurred, set the "train stuck" flag. */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1480 SetBit(v->flags, VRF_TRAIN_STUCK);
12467
cf0ccd158d37 (svn r16901) -Fix [FS#3046] (rpbs): when marking trains stuck don't reset the unload counter/stuck when the vehicle is unloading. It'll be automatically reset once the vehicle wants to leave the station
rubidium <rubidium@openttd.org>
parents: 12460
diff changeset
1481
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
1482 v->wait_counter = 0;
9808
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
1483
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
1484 /* Stop train */
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
1485 v->cur_speed = 0;
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
1486 v->subspeed = 0;
17172
679a25d71a9d (svn r21911) -Codechange: move tcache.last_speed to gcache.last_speed and make SetLastSpeed a function of GroundVehicle
rubidium <rubidium@openttd.org>
parents: 17165
diff changeset
1487 v->SetLastSpeed();
9808
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
1488
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
1489 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
9808
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
1490 }
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
1491 }
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
1492
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1493 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1494 * Swap the two up/down flags in two ways:
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1495 * - Swap values of \a swap_flag1 and \a swap_flag2, and
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1496 * - If going up previously (#GVF_GOINGUP_BIT set), the #GVF_GOINGDOWN_BIT is set, and vice versa.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1497 * @param swap_flag1 [inout] First train flag.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1498 * @param swap_flag2 [inout] Second train flag.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1499 */
9808
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
1500 static void SwapTrainFlags(uint16 *swap_flag1, uint16 *swap_flag2)
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
1501 {
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
1502 uint16 flag1 = *swap_flag1;
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
1503 uint16 flag2 = *swap_flag2;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1504
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1505 /* Clear the flags */
14753
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1506 ClrBit(*swap_flag1, GVF_GOINGUP_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1507 ClrBit(*swap_flag1, GVF_GOINGDOWN_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1508 ClrBit(*swap_flag2, GVF_GOINGUP_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1509 ClrBit(*swap_flag2, GVF_GOINGDOWN_BIT);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1510
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1511 /* Reverse the rail-flags (if needed) */
14753
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1512 if (HasBit(flag1, GVF_GOINGUP_BIT)) {
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1513 SetBit(*swap_flag2, GVF_GOINGDOWN_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1514 } else if (HasBit(flag1, GVF_GOINGDOWN_BIT)) {
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1515 SetBit(*swap_flag2, GVF_GOINGUP_BIT);
14768
f31d8dc443fc (svn r19356) -Fix: Whitespace.
frosch <frosch@openttd.org>
parents: 14754
diff changeset
1516 }
14753
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1517 if (HasBit(flag2, GVF_GOINGUP_BIT)) {
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1518 SetBit(*swap_flag1, GVF_GOINGDOWN_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1519 } else if (HasBit(flag2, GVF_GOINGDOWN_BIT)) {
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1520 SetBit(*swap_flag1, GVF_GOINGUP_BIT);
14768
f31d8dc443fc (svn r19356) -Fix: Whitespace.
frosch <frosch@openttd.org>
parents: 14754
diff changeset
1521 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1522 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1523
17098
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1524 /**
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1525 * Updates some variables after swapping the vehicle.
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1526 * @param v swapped vehicle
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1527 */
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1528 static void UpdateStatusAfterSwap(Train *v)
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1529 {
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1530 /* Reverse the direction. */
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1531 if (v->track != TRACK_BIT_DEPOT) v->direction = ReverseDir(v->direction);
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1532
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1533 /* Call the proper EnterTile function unless we are in a wormhole. */
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1534 if (v->track != TRACK_BIT_WORMHOLE) {
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1535 VehicleEnterTile(v, v->tile, v->x_pos, v->y_pos);
17142
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1536 } else {
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1537 /* VehicleEnter_TunnelBridge() sets TRACK_BIT_WORMHOLE when the vehicle
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1538 * is on the last bit of the bridge head (frame == TILE_SIZE - 1).
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1539 * If we were swapped with such a vehicle, we have set TRACK_BIT_WORMHOLE,
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1540 * when we shouldn't have. Check if this is the case. */
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1541 TileIndex vt = TileVirtXY(v->x_pos, v->y_pos);
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1542 if (IsTileType(vt, MP_TUNNELBRIDGE)) {
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1543 VehicleEnterTile(v, vt, v->x_pos, v->y_pos);
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1544 if (v->track != TRACK_BIT_WORMHOLE && IsBridgeTile(v->tile)) {
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1545 /* We have just left the wormhole, possibly set the
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1546 * "goingdown" bit. UpdateInclination() can be used
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1547 * because we are at the border of the tile. */
18865
ab93092e1cb2 (svn r23714) -Codechange: don't mix Viewport with non-viewport code
truebrain <truebrain@openttd.org>
parents: 18864
diff changeset
1548 VehicleUpdatePosition(v);
17142
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1549 v->UpdateInclination(true, true);
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1550 return;
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1551 }
958bcdb45229 (svn r21880) -Fix: when a train after reversing ended at the last bit of a bridge ramp and directed outside the bridge, it could still have track set to TRACK_BIT_WORMHOLE
smatz <smatz@openttd.org>
parents: 17141
diff changeset
1552 }
17098
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1553 }
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1554
18865
ab93092e1cb2 (svn r23714) -Codechange: don't mix Viewport with non-viewport code
truebrain <truebrain@openttd.org>
parents: 18864
diff changeset
1555 VehicleUpdatePosition(v);
17098
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1556 v->UpdateViewport(true, true);
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1557 }
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1558
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1559 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1560 * Swap vehicles \a l and \a r in consist \a v, and reverse their direction.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1561 * @param v Consist to change.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1562 * @param l %Vehicle index in the consist of the first vehicle.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1563 * @param r %Vehicle index in the consist of the second vehicle.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1564 */
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1565 void ReverseTrainSwapVeh(Train *v, int l, int r)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1566 {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1567 Train *a, *b;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1568
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1569 /* locate vehicles to swap */
7492
09743324277c (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium <rubidium@openttd.org>
parents: 7488
diff changeset
1570 for (a = v; l != 0; l--) a = a->Next();
09743324277c (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium <rubidium@openttd.org>
parents: 7488
diff changeset
1571 for (b = v; r != 0; r--) b = b->Next();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1572
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1573 if (a != b) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1574 /* swap the hidden bits */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1575 {
14678
68f4a5f222ef (svn r19257) -Codechange: minor coding style fix
smatz <smatz@openttd.org>
parents: 14645
diff changeset
1576 uint16 tmp = (a->vehstatus & ~VS_HIDDEN) | (b->vehstatus & VS_HIDDEN);
68f4a5f222ef (svn r19257) -Codechange: minor coding style fix
smatz <smatz@openttd.org>
parents: 14645
diff changeset
1577 b->vehstatus = (b->vehstatus & ~VS_HIDDEN) | (a->vehstatus & VS_HIDDEN);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1578 a->vehstatus = tmp;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1579 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1580
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1581 Swap(a->track, b->track);
14680
f3dfa0c1c278 (svn r19260) -Codechange: another minor coding style fix
smatz <smatz@openttd.org>
parents: 14678
diff changeset
1582 Swap(a->direction, b->direction);
5733
0b9175cb534a (svn r8276) -Fix
tron <tron@openttd.org>
parents: 5668
diff changeset
1583 Swap(a->x_pos, b->x_pos);
0b9175cb534a (svn r8276) -Fix
tron <tron@openttd.org>
parents: 5668
diff changeset
1584 Swap(a->y_pos, b->y_pos);
0b9175cb534a (svn r8276) -Fix
tron <tron@openttd.org>
parents: 5668
diff changeset
1585 Swap(a->tile, b->tile);
0b9175cb534a (svn r8276) -Fix
tron <tron@openttd.org>
parents: 5668
diff changeset
1586 Swap(a->z_pos, b->z_pos);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1587
14753
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1588 SwapTrainFlags(&a->gv_flags, &b->gv_flags);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1589
17098
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1590 UpdateStatusAfterSwap(a);
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1591 UpdateStatusAfterSwap(b);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1592 } else {
17136
85e9c2a2bdb2 (svn r21874) -Fix: when a single-vehicle train was reversed while on a slope, its GOINGUP/DOWN weren't swapped
smatz <smatz@openttd.org>
parents: 17110
diff changeset
1593 /* Swap GVF_GOINGUP_BIT/GVF_GOINGDOWN_BIT.
85e9c2a2bdb2 (svn r21874) -Fix: when a single-vehicle train was reversed while on a slope, its GOINGUP/DOWN weren't swapped
smatz <smatz@openttd.org>
parents: 17110
diff changeset
1594 * This is a little bit redundant way, a->gv_flags will
85e9c2a2bdb2 (svn r21874) -Fix: when a single-vehicle train was reversed while on a slope, its GOINGUP/DOWN weren't swapped
smatz <smatz@openttd.org>
parents: 17110
diff changeset
1595 * be (re)set twice, but it reduces code duplication */
85e9c2a2bdb2 (svn r21874) -Fix: when a single-vehicle train was reversed while on a slope, its GOINGUP/DOWN weren't swapped
smatz <smatz@openttd.org>
parents: 17110
diff changeset
1596 SwapTrainFlags(&a->gv_flags, &a->gv_flags);
17098
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1597 UpdateStatusAfterSwap(a);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1598 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1599 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1600
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1601
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1602 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1603 * Check if the vehicle is a train
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1604 * @param v vehicle on tile
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1605 * @return v if it is a train, NULL otherwise
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1606 */
9775
c3caa51b9888 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium <rubidium@openttd.org>
parents: 9774
diff changeset
1607 static Vehicle *TrainOnTileEnum(Vehicle *v, void *)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1608 {
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1609 return (v->type == VEH_TRAIN) ? v : NULL;
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1610 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1611
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1612
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1613 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1614 * Checks if a train is approaching a rail-road crossing
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1615 * @param v vehicle on tile
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1616 * @param data tile with crossing we are testing
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1617 * @return v if it is approaching a crossing, NULL otherwise
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1618 */
9775
c3caa51b9888 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium <rubidium@openttd.org>
parents: 9774
diff changeset
1619 static Vehicle *TrainApproachingCrossingEnum(Vehicle *v, void *data)
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1620 {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1621 if (v->type != VEH_TRAIN || (v->vehstatus & VS_CRASHED)) return NULL;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1622
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1623 Train *t = Train::From(v);
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1624 if (!t->IsFrontEngine()) return NULL;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1625
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1626 TileIndex tile = *(TileIndex *)data;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1627
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1628 if (TrainApproachingCrossingTile(t) != tile) return NULL;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1629
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1630 return t;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1631 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1632
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1633
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1634 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1635 * Finds a vehicle approaching rail-road crossing
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1636 * @param tile tile to test
10083
4923bdc50041 (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium <rubidium@openttd.org>
parents: 9992
diff changeset
1637 * @return true if a vehicle is approaching the crossing
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1638 * @pre tile is a rail-road crossing
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1639 */
10083
4923bdc50041 (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium <rubidium@openttd.org>
parents: 9992
diff changeset
1640 static bool TrainApproachingCrossing(TileIndex tile)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1641 {
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1642 assert(IsLevelCrossingTile(tile));
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1643
8598
ef2c93a9589d (svn r12179) -Codechange: use GetCrossingRailTrack() and GetCrossingRailAxis() to improve code readability
smatz <smatz@openttd.org>
parents: 8597
diff changeset
1644 DiagDirection dir = AxisToDiagDir(GetCrossingRailAxis(tile));
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1645 TileIndex tile_from = tile + TileOffsByDiagDir(dir);
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1646
10083
4923bdc50041 (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium <rubidium@openttd.org>
parents: 9992
diff changeset
1647 if (HasVehicleOnPos(tile_from, &tile, &TrainApproachingCrossingEnum)) return true;
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1648
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1649 dir = ReverseDiagDir(dir);
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1650 tile_from = tile + TileOffsByDiagDir(dir);
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1651
10083
4923bdc50041 (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium <rubidium@openttd.org>
parents: 9992
diff changeset
1652 return HasVehicleOnPos(tile_from, &tile, &TrainApproachingCrossingEnum);
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1653 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1654
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1655
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1656 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1657 * Sets correct crossing state
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1658 * @param tile tile to update
8344
6312fafcfe47 (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz <smatz@openttd.org>
parents: 8342
diff changeset
1659 * @param sound should we play sound?
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1660 * @pre tile is a rail-road crossing
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1661 */
8344
6312fafcfe47 (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz <smatz@openttd.org>
parents: 8342
diff changeset
1662 void UpdateLevelCrossing(TileIndex tile, bool sound)
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1663 {
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1664 assert(IsLevelCrossingTile(tile));
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1665
9824
fe3562a51648 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9822
diff changeset
1666 /* train on crossing || train approaching crossing || reserved */
12470
299da608443d (svn r16907) -Codechange: make a more clear distinction between reservation functions that return a bool and that return TrackBits; GetRailStationReservation vs GetRailwayStationReservation, which one returns the bool and which one the TrackBits?
rubidium <rubidium@openttd.org>
parents: 12467
diff changeset
1667 bool new_state = HasVehicleOnPos(tile, NULL, &TrainOnTileEnum) || TrainApproachingCrossing(tile) || HasCrossingReservation(tile);
8344
6312fafcfe47 (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz <smatz@openttd.org>
parents: 8342
diff changeset
1668
6312fafcfe47 (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz <smatz@openttd.org>
parents: 8342
diff changeset
1669 if (new_state != IsCrossingBarred(tile)) {
6312fafcfe47 (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz <smatz@openttd.org>
parents: 8342
diff changeset
1670 if (new_state && sound) {
19913
f4e2c852270b (svn r24846) -Add: Advanced settings to disable certain sound effects.
frosch <frosch@openttd.org>
parents: 19906
diff changeset
1671 if (_settings_client.sound.ambient) SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
8344
6312fafcfe47 (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz <smatz@openttd.org>
parents: 8342
diff changeset
1672 }
6312fafcfe47 (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz <smatz@openttd.org>
parents: 8342
diff changeset
1673 SetCrossingBarred(tile, new_state);
6312fafcfe47 (svn r11910) -Fix: play 'ding-ding' crossing sound in more cases (except gameload and crossing construction)
smatz <smatz@openttd.org>
parents: 8342
diff changeset
1674 MarkTileDirtyByTile(tile);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1675 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1676 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1677
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1678
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1679 /**
8356
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1680 * Bars crossing and plays ding-ding sound if not barred already
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1681 * @param tile tile with crossing
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1682 * @pre tile is a rail-road crossing
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1683 */
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1684 static inline void MaybeBarCrossingWithSound(TileIndex tile)
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1685 {
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1686 if (!IsCrossingBarred(tile)) {
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1687 BarCrossing(tile);
19913
f4e2c852270b (svn r24846) -Add: Advanced settings to disable certain sound effects.
frosch <frosch@openttd.org>
parents: 19906
diff changeset
1688 if (_settings_client.sound.ambient) SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
8356
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1689 MarkTileDirtyByTile(tile);
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1690 }
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1691 }
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1692
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1693
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1694 /**
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1695 * Advances wagons for train reversing, needed for variable length wagons.
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1696 * This one is called before the train is reversed.
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1697 * @param v First vehicle in chain
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1698 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1699 static void AdvanceWagonsBeforeSwap(Train *v)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1700 {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1701 Train *base = v;
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1702 Train *first = base; // first vehicle to move
12460
4acae7d6f5ed (svn r16894) -Codechange: Add [Specialised]Vehicle::Last().
frosch <frosch@openttd.org>
parents: 12450
diff changeset
1703 Train *last = v->Last(); // last vehicle to move
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1704 uint length = CountVehiclesInChain(v);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1705
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1706 while (length > 2) {
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1707 last = last->Previous();
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1708 first = first->Next();
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1709
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1710 int differential = base->CalcNextVehicleOffset() - last->CalcNextVehicleOffset();
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1711
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1712 /* do not update images now
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1713 * negative differential will be handled in AdvanceWagonsAfterSwap() */
11327
fc99317cb85b (svn r15677) -Fix [FS#2546]: vehicle images would be determined during the process of moving the vehicle which means that only the (orientation) data for the vehicles in front of it is valid. Now the data for the vehicles behind the vehicle are valid too.
rubidium <rubidium@openttd.org>
parents: 11326
diff changeset
1714 for (int i = 0; i < differential; i++) TrainController(first, last->Next());
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1715
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1716 base = first; // == base->Next()
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1717 length -= 2;
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1718 }
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1719 }
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1720
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1721
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1722 /**
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1723 * Advances wagons for train reversing, needed for variable length wagons.
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1724 * This one is called after the train is reversed.
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1725 * @param v First vehicle in chain
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1726 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1727 static void AdvanceWagonsAfterSwap(Train *v)
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1728 {
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1729 /* first of all, fix the situation when the train was entering a depot */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1730 Train *dep = v; // last vehicle in front of just left depot
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1731 while (dep->Next() != NULL && (dep->track == TRACK_BIT_DEPOT || dep->Next()->track != TRACK_BIT_DEPOT)) {
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1732 dep = dep->Next(); // find first vehicle outside of a depot, with next vehicle inside a depot
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1733 }
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1734
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1735 Train *leave = dep->Next(); // first vehicle in a depot we are leaving now
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1736
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1737 if (leave != NULL) {
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1738 /* 'pull' next wagon out of the depot, so we won't miss it (it could stay in depot forever) */
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1739 int d = TicksToLeaveDepot(dep);
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1740
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1741 if (d <= 0) {
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1742 leave->vehstatus &= ~VS_HIDDEN; // move it out of the depot
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1743 leave->track = TrackToTrackBits(GetRailDepotTrack(leave->tile));
11327
fc99317cb85b (svn r15677) -Fix [FS#2546]: vehicle images would be determined during the process of moving the vehicle which means that only the (orientation) data for the vehicles in front of it is valid. Now the data for the vehicles behind the vehicle are valid too.
rubidium <rubidium@openttd.org>
parents: 11326
diff changeset
1744 for (int i = 0; i >= d; i--) TrainController(leave, NULL); // maybe move it, and maybe let another wagon leave
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1745 }
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1746 } else {
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1747 dep = NULL; // no vehicle in a depot, so no vehicle leaving a depot
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1748 }
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1749
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1750 Train *base = v;
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1751 Train *first = base; // first vehicle to move
12460
4acae7d6f5ed (svn r16894) -Codechange: Add [Specialised]Vehicle::Last().
frosch <frosch@openttd.org>
parents: 12450
diff changeset
1752 Train *last = v->Last(); // last vehicle to move
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1753 uint length = CountVehiclesInChain(v);
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1754
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
1755 /* We have to make sure all wagons that leave a depot because of train reversing are moved correctly
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1756 * they have already correct spacing, so we have to make sure they are moved how they should */
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
1757 bool nomove = (dep == NULL); // If there is no vehicle leaving a depot, limit the number of wagons moved immediately.
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1758
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1759 while (length > 2) {
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1760 /* we reached vehicle (originally) in front of a depot, stop now
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
1761 * (we would move wagons that are already moved with new wagon length). */
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1762 if (base == dep) break;
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1763
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1764 /* the last wagon was that one leaving a depot, so do not move it anymore */
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1765 if (last == dep) nomove = true;
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1766
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1767 last = last->Previous();
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1768 first = first->Next();
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1769
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1770 int differential = last->CalcNextVehicleOffset() - base->CalcNextVehicleOffset();
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1771
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1772 /* do not update images now */
11327
fc99317cb85b (svn r15677) -Fix [FS#2546]: vehicle images would be determined during the process of moving the vehicle which means that only the (orientation) data for the vehicles in front of it is valid. Now the data for the vehicles behind the vehicle are valid too.
rubidium <rubidium@openttd.org>
parents: 11326
diff changeset
1773 for (int i = 0; i < differential; i++) TrainController(first, (nomove ? last->Next() : NULL));
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1774
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1775 base = first; // == base->Next()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1776 length -= 2;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1777 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1778 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1779
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1780 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1781 * Turn a train around.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1782 * @param v %Train to turn around.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1783 */
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
1784 void ReverseTrainDirection(Train *v)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1785 {
8961
b33bcd7fa037 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz <smatz@openttd.org>
parents: 8954
diff changeset
1786 if (IsRailDepotTile(v->tile)) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1787 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1788 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1789
12693
3916fe720543 (svn r17152) -Fix: A stuck train could free the reservation of another train if it was reversed or did crash.
michi_cc <michi_cc@openttd.org>
parents: 12692
diff changeset
1790 /* Clear path reservation in front if train is not stuck. */
3916fe720543 (svn r17152) -Fix: A stuck train could free the reservation of another train if it was reversed or did crash.
michi_cc <michi_cc@openttd.org>
parents: 12692
diff changeset
1791 if (!HasBit(v->flags, VRF_TRAIN_STUCK)) FreeTrainTrackReservation(v);
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1792
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1793 /* Check if we were approaching a rail/road-crossing */
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1794 TileIndex crossing = TrainApproachingCrossingTile(v);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1795
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1796 /* count number of vehicles */
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
1797 int r = CountVehiclesInChain(v) - 1; // number of vehicles - 1
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1798
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1799 AdvanceWagonsBeforeSwap(v);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1800
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1801 /* swap start<>end, start+1<>end-1, ... */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1802 int l = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1803 do {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1804 ReverseTrainSwapVeh(v, l++, r--);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1805 } while (l <= r);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1806
8712
b5b3d75eb7db (svn r12386) -Fix [FS#1841](r2428): train could break apart when reversed while partially in a depot
smatz <smatz@openttd.org>
parents: 8710
diff changeset
1807 AdvanceWagonsAfterSwap(v);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1808
8961
b33bcd7fa037 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz <smatz@openttd.org>
parents: 8954
diff changeset
1809 if (IsRailDepotTile(v->tile)) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1810 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1811 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1812
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1813 ToggleBit(v->flags, VRF_TOGGLE_REVERSE);
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1814
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1815 ClrBit(v->flags, VRF_REVERSING);
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1816
8667
4e73621c3950 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138 <peter1138@openttd.org>
parents: 8666
diff changeset
1817 /* recalculate cached data */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
1818 v->ConsistChanged(true);
8667
4e73621c3950 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138 <peter1138@openttd.org>
parents: 8666
diff changeset
1819
4e73621c3950 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138 <peter1138@openttd.org>
parents: 8666
diff changeset
1820 /* update all images */
15127
c065afa82756 (svn r19756) -Codechange: move UpdateViewport() from Vehicle to SpecializedVehicle in order to improve performance
smatz <smatz@openttd.org>
parents: 15073
diff changeset
1821 for (Train *u = v; u != NULL; u = u->Next()) u->UpdateViewport(false, false);
8667
4e73621c3950 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138 <peter1138@openttd.org>
parents: 8666
diff changeset
1822
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1823 /* update crossing we were approaching */
8342
441cda3cfbdb (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz <smatz@openttd.org>
parents: 8334
diff changeset
1824 if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1825
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1826 /* maybe we are approaching crossing now, after reversal */
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1827 crossing = TrainApproachingCrossingTile(v);
8356
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1828 if (crossing != INVALID_TILE) MaybeBarCrossingWithSound(crossing);
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1829
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1830 /* If we are inside a depot after reversing, don't bother with path reserving. */
12137
b3c5a6c9146a (svn r16553) -Codechange: don't use TRACK_BIT_WORMHOLE and TRACK_BIT_DEPOT as bitmasks
smatz <smatz@openttd.org>
parents: 12129
diff changeset
1831 if (v->track == TRACK_BIT_DEPOT) {
9973
ed2e764e5d6b (svn r14130) -Fix (r13961) [FS#2248]: Clear the stuck state of a train that is reversing with the first vehicle inside a depot. (michi_cc)
frosch <frosch@openttd.org>
parents: 9941
diff changeset
1832 /* Can't be stuck here as inside a depot is always a safe tile. */
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
1833 if (HasBit(v->flags, VRF_TRAIN_STUCK)) SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1834 ClrBit(v->flags, VRF_TRAIN_STUCK);
9973
ed2e764e5d6b (svn r14130) -Fix (r13961) [FS#2248]: Clear the stuck state of a train that is reversing with the first vehicle inside a depot. (michi_cc)
frosch <frosch@openttd.org>
parents: 9941
diff changeset
1835 return;
ed2e764e5d6b (svn r14130) -Fix (r13961) [FS#2248]: Clear the stuck state of a train that is reversing with the first vehicle inside a depot. (michi_cc)
frosch <frosch@openttd.org>
parents: 9941
diff changeset
1836 }
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1837
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1838 /* TrainExitDir does not always produce the desired dir for depots and
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1839 * tunnels/bridges that is needed for UpdateSignalsOnSegment. */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1840 DiagDirection dir = TrainExitDir(v->direction, v->track);
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1841 if (IsRailDepotTile(v->tile) || IsTileType(v->tile, MP_TUNNELBRIDGE)) dir = INVALID_DIAGDIR;
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1842
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1843 if (UpdateSignalsOnSegment(v->tile, dir, v->owner) == SIGSEG_PBS || _settings_game.pf.reserve_paths) {
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1844 /* If we are currently on a tile with conventional signals, we can't treat the
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1845 * current tile as a safe tile or we would enter a PBS block without a reservation. */
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1846 bool first_tile_okay = !(IsTileType(v->tile, MP_RAILWAY) &&
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
1847 HasSignalOnTrackdir(v->tile, v->GetVehicleTrackdir()) &&
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1848 !IsPbsSignal(GetSignalType(v->tile, FindFirstTrack(v->track))));
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1849
19201
a934b903e21e (svn r24078) -Fix [FS#5093,FS#5130] (r24071): A fix that breaks all other cases isn't really a fix. Redo it to make sure that reservations of trains entering or exiting depots are properly made and freed.
michi_cc <michi_cc@openttd.org>
parents: 19197
diff changeset
1850 /* If we are on a depot tile facing outwards, do not treat the current tile as safe. */
a934b903e21e (svn r24078) -Fix [FS#5093,FS#5130] (r24071): A fix that breaks all other cases isn't really a fix. Redo it to make sure that reservations of trains entering or exiting depots are properly made and freed.
michi_cc <michi_cc@openttd.org>
parents: 19197
diff changeset
1851 if (IsRailDepotTile(v->tile) && TrackdirToExitdir(v->GetVehicleTrackdir()) == GetRailDepotDirection(v->tile)) first_tile_okay = false;
a934b903e21e (svn r24078) -Fix [FS#5093,FS#5130] (r24071): A fix that breaks all other cases isn't really a fix. Redo it to make sure that reservations of trains entering or exiting depots are properly made and freed.
michi_cc <michi_cc@openttd.org>
parents: 19197
diff changeset
1852
12502
9472b49c61a1 (svn r16939) -Codechange: s/RailwayStation/RailStation/ to unify the way it's written.
rubidium <rubidium@openttd.org>
parents: 12472
diff changeset
1853 if (IsRailStationTile(v->tile)) SetRailStationPlatformReservation(v->tile, TrackdirToExitdir(v->GetVehicleTrackdir()), true);
9891
95fb811fb1d3 (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9888
diff changeset
1854 if (TryPathReserve(v, false, first_tile_okay)) {
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1855 /* Do a look-ahead now in case our current tile was already a safe tile. */
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1856 CheckNextTrainTile(v);
9891
95fb811fb1d3 (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9888
diff changeset
1857 } else if (v->current_order.GetType() != OT_LOADING) {
95fb811fb1d3 (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9888
diff changeset
1858 /* Do not wait for a way out when we're still loading */
95fb811fb1d3 (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9888
diff changeset
1859 MarkTrainAsStuck(v);
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1860 }
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1861 } else if (HasBit(v->flags, VRF_TRAIN_STUCK)) {
10588
1c8fb7b3873a (svn r14851) -Fix: A train reversing in a non-PBS block can't be stuck.
michi_cc <michi_cc@openttd.org>
parents: 10587
diff changeset
1862 /* A train not inside a PBS block can't be stuck. */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1863 ClrBit(v->flags, VRF_TRAIN_STUCK);
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
1864 v->wait_counter = 0;
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1865 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1866 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1867
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1868 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1869 * Reverse train.
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1870 * @param tile unused
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1871 * @param flags type of operation
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1872 * @param p1 train to reverse
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1873 * @param p2 if true, reverse a unit in a train (needs to be in a depot)
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1874 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1875 * @return the cost of this operation or an error
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1876 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1877 CommandCost CmdReverseTrainDirection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1878 {
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1879 Train *v = Train::GetIfValid(p1);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1880 if (v == NULL) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1881
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1882 CommandCost ret = CheckOwnership(v->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1883 if (ret.Failed()) return ret;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1884
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
1885 if (p2 != 0) {
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1886 /* turn a single unit around */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1887
13036
8e2a0e8fa275 (svn r17534) -Codechange: unify the naming of callback masks/flags
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
1888 if (v->IsMultiheaded() || HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_ARTIC_ENGINE)) {
12351
65732f6bed3f (svn r16772) -Fix [FS#3019]: don't use the same error message for turning around road vehicles and flipping parts of trains in the depot
rubidium <rubidium@openttd.org>
parents: 12306
diff changeset
1889 return_cmd_error(STR_ERROR_CAN_T_REVERSE_DIRECTION_RAIL_VEHICLE_MULTIPLE_UNITS);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1890 }
17227
7135c0129418 (svn r21966) -Change [FS#4462]: [NewGRF] Disable the flipping of train engines/wagons in the depot by default for NewGRFs
rubidium <rubidium@openttd.org>
parents: 17223
diff changeset
1891 if (!HasBit(EngInfo(v->engine_type)->misc_flags, EF_RAIL_FLIPS)) return CMD_ERROR;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1892
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1893 Train *front = v->First();
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1894 /* make sure the vehicle is stopped in the depot */
13933
a71da8c8c5e2 (svn r18465) -Codechange: simplify CheckTrainInDepot and remove some unneeded wrapper functions
rubidium <rubidium@openttd.org>
parents: 13931
diff changeset
1895 if (!front->IsStoppedInDepot()) {
11730
a39da1e4c1f5 (svn r16118) -Change/cleanup: remove the hexadecimal 'in TTD the string had this ID' from 'some' strings and replace the string name with something more sensible.
rubidium <rubidium@openttd.org>
parents: 11701
diff changeset
1896 return_cmd_error(STR_ERROR_TRAINS_CAN_ONLY_BE_ALTERED_INSIDE_A_DEPOT);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1897 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1898
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1899 if (flags & DC_EXEC) {
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1900 ToggleBit(v->flags, VRF_REVERSE_DIRECTION);
17303
9bb1535a33c0 (svn r22043) -Fix: invalidate the right windows when a part of a train is flipped in the depot
rubidium <rubidium@openttd.org>
parents: 17284
diff changeset
1901
17304
aea54f56915d (svn r22044) -Fix [FS#4493]: update the consist cache when a part of a train is flipped in the depot
rubidium <rubidium@openttd.org>
parents: 17303
diff changeset
1902 front->ConsistChanged(false);
17303
9bb1535a33c0 (svn r22043) -Fix: invalidate the right windows when a part of a train is flipped in the depot
rubidium <rubidium@openttd.org>
parents: 17284
diff changeset
1903 SetWindowDirty(WC_VEHICLE_DEPOT, front->tile);
9bb1535a33c0 (svn r22043) -Fix: invalidate the right windows when a part of a train is flipped in the depot
rubidium <rubidium@openttd.org>
parents: 17284
diff changeset
1904 SetWindowDirty(WC_VEHICLE_DETAILS, front->index);
9bb1535a33c0 (svn r22043) -Fix: invalidate the right windows when a part of a train is flipped in the depot
rubidium <rubidium@openttd.org>
parents: 17284
diff changeset
1905 SetWindowDirty(WC_VEHICLE_VIEW, front->index);
9bb1535a33c0 (svn r22043) -Fix: invalidate the right windows when a part of a train is flipped in the depot
rubidium <rubidium@openttd.org>
parents: 17284
diff changeset
1906 SetWindowClassesDirty(WC_TRAINS_LIST);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1907 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1908 } else {
7476
a24293df92f5 (svn r10983) -Codechange: use vehstatus & VS_CRASHED instead of some other "methods" custom to each vehicle to determine whether the vehicle is crashed.
rubidium <rubidium@openttd.org>
parents: 7425
diff changeset
1909 /* turn the whole train around */
12082
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 12073
diff changeset
1910 if ((v->vehstatus & VS_CRASHED) || v->breakdown_ctr != 0) return CMD_ERROR;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1911
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1912 if (flags & DC_EXEC) {
11094
6a6a59f20daf (svn r15439) -Fix [FS#2626]: call Vehicle::LeaveStation() before reversing overlength loading train (station triggers were missed, PBS reservation could cause crash)
smatz <smatz@openttd.org>
parents: 11090
diff changeset
1913 /* Properly leave the station if we are loading and won't be loading anymore */
6a6a59f20daf (svn r15439) -Fix [FS#2626]: call Vehicle::LeaveStation() before reversing overlength loading train (station triggers were missed, PBS reservation could cause crash)
smatz <smatz@openttd.org>
parents: 11090
diff changeset
1914 if (v->current_order.IsType(OT_LOADING)) {
6a6a59f20daf (svn r15439) -Fix [FS#2626]: call Vehicle::LeaveStation() before reversing overlength loading train (station triggers were missed, PBS reservation could cause crash)
smatz <smatz@openttd.org>
parents: 11090
diff changeset
1915 const Vehicle *last = v;
6a6a59f20daf (svn r15439) -Fix [FS#2626]: call Vehicle::LeaveStation() before reversing overlength loading train (station triggers were missed, PBS reservation could cause crash)
smatz <smatz@openttd.org>
parents: 11090
diff changeset
1916 while (last->Next() != NULL) last = last->Next();
6a6a59f20daf (svn r15439) -Fix [FS#2626]: call Vehicle::LeaveStation() before reversing overlength loading train (station triggers were missed, PBS reservation could cause crash)
smatz <smatz@openttd.org>
parents: 11090
diff changeset
1917
6a6a59f20daf (svn r15439) -Fix [FS#2626]: call Vehicle::LeaveStation() before reversing overlength loading train (station triggers were missed, PBS reservation could cause crash)
smatz <smatz@openttd.org>
parents: 11090
diff changeset
1918 /* not a station || different station --> leave the station */
6a6a59f20daf (svn r15439) -Fix [FS#2626]: call Vehicle::LeaveStation() before reversing overlength loading train (station triggers were missed, PBS reservation could cause crash)
smatz <smatz@openttd.org>
parents: 11090
diff changeset
1919 if (!IsTileType(last->tile, MP_STATION) || GetStationIndex(last->tile) != GetStationIndex(v->tile)) {
6a6a59f20daf (svn r15439) -Fix [FS#2626]: call Vehicle::LeaveStation() before reversing overlength loading train (station triggers were missed, PBS reservation could cause crash)
smatz <smatz@openttd.org>
parents: 11090
diff changeset
1920 v->LeaveStation();
6a6a59f20daf (svn r15439) -Fix [FS#2626]: call Vehicle::LeaveStation() before reversing overlength loading train (station triggers were missed, PBS reservation could cause crash)
smatz <smatz@openttd.org>
parents: 11090
diff changeset
1921 }
6a6a59f20daf (svn r15439) -Fix [FS#2626]: call Vehicle::LeaveStation() before reversing overlength loading train (station triggers were missed, PBS reservation could cause crash)
smatz <smatz@openttd.org>
parents: 11090
diff changeset
1922 }
6a6a59f20daf (svn r15439) -Fix [FS#2626]: call Vehicle::LeaveStation() before reversing overlength loading train (station triggers were missed, PBS reservation could cause crash)
smatz <smatz@openttd.org>
parents: 11090
diff changeset
1923
13977
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
1924 /* We cancel any 'skip signal at dangers' here */
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
1925 v->force_proceed = TFP_NONE;
13977
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
1926 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
1927
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
1928 if (_settings_game.vehicle.train_acceleration_model != AM_ORIGINAL && v->cur_speed != 0) {
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
1929 ToggleBit(v->flags, VRF_REVERSING);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1930 } else {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1931 v->cur_speed = 0;
17172
679a25d71a9d (svn r21911) -Codechange: move tcache.last_speed to gcache.last_speed and make SetLastSpeed a function of GroundVehicle
rubidium <rubidium@openttd.org>
parents: 17165
diff changeset
1932 v->SetLastSpeed();
10176
71daf486081d (svn r14371) -Fix [FS#2313]: loading indicator didn't stay with the front engine when turning a train in a station.
rubidium <rubidium@openttd.org>
parents: 10173
diff changeset
1933 HideFillingPercent(&v->fill_percent_te_id);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1934 ReverseTrainDirection(v);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1935 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1936 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1937 }
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
1938 return CommandCost();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1939 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1940
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1941 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1942 * Force a train through a red signal
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1943 * @param tile unused
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1944 * @param flags type of operation
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1945 * @param p1 train to ignore the red signal
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1946 * @param p2 unused
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1947 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1948 * @return the cost of this operation or an error
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1949 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1950 CommandCost CmdForceTrainProceed(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1951 {
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1952 Train *t = Train::GetIfValid(p1);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1953 if (t == NULL) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1954
19479
5c205cdec645 (svn r24382) -Fix: Call Vehicle::IsStoppedInDepot only for the first vehicle in a chain (i.e. primary vehicle or free wagon).
frosch <frosch@openttd.org>
parents: 19383
diff changeset
1955 if (!t->IsPrimaryVehicle()) return CMD_ERROR;
5c205cdec645 (svn r24382) -Fix: Call Vehicle::IsStoppedInDepot only for the first vehicle in a chain (i.e. primary vehicle or free wagon).
frosch <frosch@openttd.org>
parents: 19383
diff changeset
1956
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1957 CommandCost ret = CheckOwnership(t->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1958 if (ret.Failed()) return ret;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1959
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1960
13977
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
1961 if (flags & DC_EXEC) {
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
1962 /* If we are forced to proceed, cancel that order.
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
1963 * If we are marked stuck we would want to force the train
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
1964 * to proceed to the next signal. In the other cases we
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
1965 * would like to pass the signal at danger and run till the
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
1966 * next signal we encounter. */
19481
b271f4f060fa (svn r24384) -Fix [FS#5188-ish]: Make IsInDepot() functions behave consistent across vehicle types and add IsChainInDepot instead, if that is what shall be checked.
frosch <frosch@openttd.org>
parents: 19479
diff changeset
1967 t->force_proceed = t->force_proceed == TFP_SIGNAL ? TFP_NONE : HasBit(t->flags, VRF_TRAIN_STUCK) || t->IsChainInDepot() ? TFP_STUCK : TFP_SIGNAL;
13977
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
1968 SetWindowDirty(WC_VEHICLE_VIEW, t->index);
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
1969 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1970
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
1971 return CommandCost();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1972 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1973
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1974 /**
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1975 * Try to find a depot nearby.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1976 * @param v %Train that wants a depot.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1977 * @param max_distance Maximal search distance.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1978 * @return Information where the closest train depot is located.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1979 * @pre The given vehicle must not be crashed!
15623
c62577640878 (svn r20286) -Codechange: Unify end of doxygen comments.
frosch <frosch@openttd.org>
parents: 15620
diff changeset
1980 */
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1981 static FindDepotData FindClosestTrainDepot(Train *v, int max_distance)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1982 {
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1983 assert(!(v->vehstatus & VS_CRASHED));
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1984
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1985 if (IsRailDepotTile(v->tile)) return FindDepotData(v->tile, 0);
9934
26c9719c4ae0 (svn r14089) -Fix [FS#2219]: trains not being able to find a route to a depot when the front is already in the depot.
rubidium <rubidium@openttd.org>
parents: 9928
diff changeset
1986
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
1987 PBSTileInfo origin = FollowTrainReservation(v);
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1988 if (IsRailDepotTile(origin.tile)) return FindDepotData(origin.tile, 0);
9934
26c9719c4ae0 (svn r14089) -Fix [FS#2219]: trains not being able to find a route to a depot when the front is already in the depot.
rubidium <rubidium@openttd.org>
parents: 9928
diff changeset
1989
13833
ad09d18a3e34 (svn r18362) -Cleanup: remove OPF for RVs and NTP to clean up lots of code and simplify some things for the future
rubidium <rubidium@openttd.org>
parents: 13832
diff changeset
1990 switch (_settings_game.pf.pathfinder_for_trains) {
14031
c603550031d4 (svn r18574) -Fix [FS#3392] (r18481): manually sending trains and RVs to depots didn't quite work
rubidium <rubidium@openttd.org>
parents: 13977
diff changeset
1991 case VPF_NPF: return NPFTrainFindNearestDepot(v, max_distance);
c603550031d4 (svn r18574) -Fix [FS#3392] (r18481): manually sending trains and RVs to depots didn't quite work
rubidium <rubidium@openttd.org>
parents: 13977
diff changeset
1992 case VPF_YAPF: return YapfTrainFindNearestDepot(v, max_distance);
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1993
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1994 default: NOT_REACHED();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1995 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1996 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1997
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1998 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
1999 * Locate the closest depot for this consist, and return the information to the caller.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2000 * @param location [out] If not \c NULL and a depot is found, store its location in the given address.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2001 * @param destination [out] If not \c NULL and a depot is found, store its index in the given address.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2002 * @param reverse [out] If not \c NULL and a depot is found, store reversal information in the given address.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2003 * @return A depot has been found.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2004 */
8890
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
2005 bool Train::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
2006 {
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2007 FindDepotData tfdd = FindClosestTrainDepot(this, 0);
10236
59398a628f10 (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium <rubidium@openttd.org>
parents: 10214
diff changeset
2008 if (tfdd.best_length == UINT_MAX) return false;
8890
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
2009
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
2010 if (location != NULL) *location = tfdd.tile;
13001
c2298b1d3f83 (svn r17495) -Codechange: replace 'Depot::Get(GetDepotIndex(tile))->index' with GetDepotIndex(tile)
rubidium <rubidium@openttd.org>
parents: 12860
diff changeset
2011 if (destination != NULL) *destination = GetDepotIndex(tfdd.tile);
8890
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
2012 if (reverse != NULL) *reverse = tfdd.reverse;
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
2013
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
2014 return true;
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
2015 }
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
2016
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2017 /** Play a sound for a train leaving the station. */
8550
14ecde7f8abf (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138 <peter1138@openttd.org>
parents: 8510
diff changeset
2018 void Train::PlayLeaveStationSound() const
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2019 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2020 static const SoundFx sfx[] = {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2021 SND_04_TRAIN,
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2022 SND_0A_TRAIN_HORN,
6586
245129bf2b8d (svn r9800) -Codechange: play sound effects based on the engine class, not the rail type (mart3p).
rubidium <rubidium@openttd.org>
parents: 6580
diff changeset
2023 SND_0A_TRAIN_HORN,
245129bf2b8d (svn r9800) -Codechange: play sound effects based on the engine class, not the rail type (mart3p).
rubidium <rubidium@openttd.org>
parents: 6580
diff changeset
2024 SND_47_MAGLEV_2,
245129bf2b8d (svn r9800) -Codechange: play sound effects based on the engine class, not the rail type (mart3p).
rubidium <rubidium@openttd.org>
parents: 6580
diff changeset
2025 SND_41_MAGLEV
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2026 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2027
8550
14ecde7f8abf (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138 <peter1138@openttd.org>
parents: 8510
diff changeset
2028 if (PlayVehicleSound(this, VSE_START)) return;
14ecde7f8abf (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138 <peter1138@openttd.org>
parents: 8510
diff changeset
2029
14ecde7f8abf (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138 <peter1138@openttd.org>
parents: 8510
diff changeset
2030 EngineID engtype = this->engine_type;
14ecde7f8abf (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138 <peter1138@openttd.org>
parents: 8510
diff changeset
2031 SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], this);
6593
431c5039b1f1 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium <rubidium@openttd.org>
parents: 6592
diff changeset
2032 }
431c5039b1f1 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium <rubidium@openttd.org>
parents: 6592
diff changeset
2033
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2034 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2035 * Check if the train is on the last reserved tile and try to extend the path then.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2036 * @param v %Train that needs its path extended.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2037 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2038 static void CheckNextTrainTile(Train *v)
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2039 {
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2040 /* Don't do any look-ahead if path_backoff_interval is 255. */
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2041 if (_settings_game.pf.path_backoff_interval == 255) return;
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2042
15156
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2043 /* Exit if we are inside a depot. */
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2044 if (v->track == TRACK_BIT_DEPOT) return;
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2045
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2046 switch (v->current_order.GetType()) {
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2047 /* Exit if we reached our destination depot. */
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2048 case OT_GOTO_DEPOT:
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2049 if (v->tile == v->dest_tile) return;
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2050 break;
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2051
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2052 case OT_GOTO_WAYPOINT:
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2053 /* If we reached our waypoint, make sure we see that. */
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2054 if (IsRailWaypointTile(v->tile) && GetStationIndex(v->tile) == v->current_order.GetDestination()) ProcessOrders(v);
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2055 break;
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2056
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2057 case OT_NOTHING:
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2058 case OT_LEAVESTATION:
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2059 case OT_LOADING:
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2060 /* Exit if the current order doesn't have a destination, but the train has orders. */
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2061 if (v->GetNumOrders() > 0) return;
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2062 break;
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2063
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2064 default:
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2065 break;
46dfa3614ec1 (svn r19785) -Codechange: reorder/rework the checks for CheckNextTrainTile a bit, causing the whole function to be a few percent faster and hopefully better to understand
rubidium <rubidium@openttd.org>
parents: 15155
diff changeset
2066 }
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2067 /* Exit if we are on a station tile and are going to stop. */
12502
9472b49c61a1 (svn r16939) -Codechange: s/RailwayStation/RailStation/ to unify the way it's written.
rubidium <rubidium@openttd.org>
parents: 12472
diff changeset
2068 if (IsRailStationTile(v->tile) && v->current_order.ShouldStopAtStation(v, GetStationIndex(v->tile))) return;
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2069
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
2070 Trackdir td = v->GetVehicleTrackdir();
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2071
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2072 /* On a tile with a red non-pbs signal, don't look ahead. */
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2073 if (IsTileType(v->tile, MP_RAILWAY) && HasSignalOnTrackdir(v->tile, td) &&
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2074 !IsPbsSignal(GetSignalType(v->tile, TrackdirToTrack(td))) &&
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2075 GetSignalStateByTrackdir(v->tile, td) == SIGNAL_STATE_RED) return;
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2076
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2077 CFollowTrackRail ft(v);
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2078 if (!ft.Follow(v->tile, td)) return;
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2079
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2080 if (!HasReservedTracks(ft.m_new_tile, TrackdirBitsToTrackBits(ft.m_new_td_bits))) {
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2081 /* Next tile is not reserved. */
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2082 if (KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE) {
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2083 if (HasPbsSignalOnTrackdir(ft.m_new_tile, FindFirstTrackdir(ft.m_new_td_bits))) {
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2084 /* If the next tile is a PBS signal, try to make a reservation. */
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2085 TrackBits tracks = TrackdirBitsToTrackBits(ft.m_new_td_bits);
13833
ad09d18a3e34 (svn r18362) -Cleanup: remove OPF for RVs and NTP to clean up lots of code and simplify some things for the future
rubidium <rubidium@openttd.org>
parents: 13832
diff changeset
2086 if (_settings_game.pf.forbid_90_deg) {
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2087 tracks &= ~TrackCrossesTracks(TrackdirToTrack(ft.m_old_td));
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2088 }
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2089 ChooseTrainTrack(v, ft.m_new_tile, ft.m_exitdir, tracks, false, NULL, false);
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2090 }
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2091 }
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2092 }
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2093 }
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2094
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2095 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2096 * Will the train stay in the depot the next tick?
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2097 * @param v %Train to check.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2098 * @return True if it stays in the depot, false otherwise.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2099 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2100 static bool CheckTrainStayInDepot(Train *v)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2101 {
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2102 /* bail out if not all wagons are in the same depot or not in a depot at all */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2103 for (const Train *u = v; u != NULL; u = u->Next()) {
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
2104 if (u->track != TRACK_BIT_DEPOT || u->tile != v->tile) return false;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2105 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2106
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2107 /* if the train got no power, then keep it in the depot */
16785
775f357828e3 (svn r21518) -Codechange: Rename AccelerationCache to GroundVehicleCache.
terkhen <terkhen@openttd.org>
parents: 16776
diff changeset
2108 if (v->gcache.cached_power == 0) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2109 v->vehstatus |= VS_STOPPED;
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
2110 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2111 return true;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2112 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2113
9818
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2114 SigSegState seg_state;
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2115
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
2116 if (v->force_proceed == TFP_NONE) {
9818
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2117 /* force proceed was not pressed */
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
2118 if (++v->wait_counter < 37) {
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
2119 SetWindowClassesDirty(WC_TRAINS_LIST);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2120 return true;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2121 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2122
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
2123 v->wait_counter = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2124
9818
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2125 seg_state = _settings_game.pf.reserve_paths ? SIGSEG_PBS : UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
12471
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2126 if (seg_state == SIGSEG_FULL || HasDepotReservation(v->tile)) {
9818
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2127 /* Full and no PBS signal in block or depot reserved, can't exit. */
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
2128 SetWindowClassesDirty(WC_TRAINS_LIST);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2129 return true;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2130 }
9818
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2131 } else {
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2132 seg_state = _settings_game.pf.reserve_paths ? SIGSEG_PBS : UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2133 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2134
11920
06375a564163 (svn r16322) -Fix [FS#2873]: trains could get stuck in a depot when they wanted to go to the same depot again.
rubidium <rubidium@openttd.org>
parents: 11903
diff changeset
2135 /* We are leaving a depot, but have to go to the exact same one; re-enter */
12155
884d20bcc977 (svn r16571) -Fix [FS#2974](r16322): in some cases, train could be stuck in depot
smatz <smatz@openttd.org>
parents: 12137
diff changeset
2136 if (v->current_order.IsType(OT_GOTO_DEPOT) && v->tile == v->dest_tile) {
11920
06375a564163 (svn r16322) -Fix [FS#2873]: trains could get stuck in a depot when they wanted to go to the same depot again.
rubidium <rubidium@openttd.org>
parents: 11903
diff changeset
2137 /* We need to have a reservation for this to work. */
12471
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2138 if (HasDepotReservation(v->tile)) return true;
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2139 SetDepotReservation(v->tile, true);
11920
06375a564163 (svn r16322) -Fix [FS#2873]: trains could get stuck in a depot when they wanted to go to the same depot again.
rubidium <rubidium@openttd.org>
parents: 11903
diff changeset
2140 VehicleEnterDepot(v);
06375a564163 (svn r16322) -Fix [FS#2873]: trains could get stuck in a depot when they wanted to go to the same depot again.
rubidium <rubidium@openttd.org>
parents: 11903
diff changeset
2141 return true;
06375a564163 (svn r16322) -Fix [FS#2873]: trains could get stuck in a depot when they wanted to go to the same depot again.
rubidium <rubidium@openttd.org>
parents: 11903
diff changeset
2142 }
06375a564163 (svn r16322) -Fix [FS#2873]: trains could get stuck in a depot when they wanted to go to the same depot again.
rubidium <rubidium@openttd.org>
parents: 11903
diff changeset
2143
9818
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2144 /* Only leave when we can reserve a path to our destination. */
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
2145 if (seg_state == SIGSEG_PBS && !TryPathReserve(v) && v->force_proceed == TFP_NONE) {
9818
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2146 /* No path and no force proceed. */
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
2147 SetWindowClassesDirty(WC_TRAINS_LIST);
9818
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2148 MarkTrainAsStuck(v);
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2149 return true;
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2150 }
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2151
12471
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2152 SetDepotReservation(v->tile, true);
9818
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2153 if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(v->tile);
14edde00b131 (svn r13960) -Codechange [YAPP]: Reserve a path when exiting a depot into a PBS block. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9817
diff changeset
2154
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2155 VehicleServiceInDepot(v);
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
2156 SetWindowClassesDirty(WC_TRAINS_LIST);
8550
14ecde7f8abf (svn r12128) -Codechange: Replace last use of TrainPlayLeaveStationSound(v) with v->PlayLeaveStationSound(), and remove wrapper function.
peter1138 <peter1138@openttd.org>
parents: 8510
diff changeset
2157 v->PlayLeaveStationSound();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2158
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
2159 v->track = TRACK_BIT_X;
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
2160 if (v->direction & 2) v->track = TRACK_BIT_Y;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2161
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2162 v->vehstatus &= ~VS_HIDDEN;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2163 v->cur_speed = 0;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2164
18865
ab93092e1cb2 (svn r23714) -Codechange: don't mix Viewport with non-viewport code
truebrain <truebrain@openttd.org>
parents: 18864
diff changeset
2165 v->UpdateViewport(true, true);
18864
c8717ac828d0 (svn r23713) -Codechange: split VehicleMove() into two functions, to minimalize the work done where possible
truebrain <truebrain@openttd.org>
parents: 18855
diff changeset
2166 VehicleUpdatePosition(v);
8300
ae1baac75172 (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz <smatz@openttd.org>
parents: 8283
diff changeset
2167 UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
2168 v->UpdateAcceleration();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2169 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2170
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2171 return false;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2172 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2173
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2174 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2175 * Clear the reservation of \a tile that was just left by a wagon on \a track_dir.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2176 * @param v %Train owning the reservation.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2177 * @param tile Tile with reservation to clear.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2178 * @param track_dir Track direction to clear.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2179 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2180 static void ClearPathReservation(const Train *v, TileIndex tile, Trackdir track_dir)
9809
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2181 {
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2182 DiagDirection dir = TrackdirToExitdir(track_dir);
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2183
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2184 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2185 /* Are we just leaving a tunnel/bridge? */
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2186 if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(dir)) {
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2187 TileIndex end = GetOtherTunnelBridgeEnd(tile);
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2188
14707
ed6ca7f84893 (svn r19291) -Codechange: Rename HasVehicleOnTunnelBridge() to TunnelBridgeIsFree() and return a CommandCost status.
alberth <alberth@openttd.org>
parents: 14702
diff changeset
2189 if (TunnelBridgeIsFree(tile, end, v).Succeeded()) {
10103
9a884d182149 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc <michi_cc@openttd.org>
parents: 10102
diff changeset
2190 /* Free the reservation only if no other train is on the tiles. */
9a884d182149 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc <michi_cc@openttd.org>
parents: 10102
diff changeset
2191 SetTunnelBridgeReservation(tile, false);
9a884d182149 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc <michi_cc@openttd.org>
parents: 10102
diff changeset
2192 SetTunnelBridgeReservation(end, false);
9a884d182149 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc <michi_cc@openttd.org>
parents: 10102
diff changeset
2193
9a884d182149 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc <michi_cc@openttd.org>
parents: 10102
diff changeset
2194 if (_settings_client.gui.show_track_reservation) {
9a884d182149 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc <michi_cc@openttd.org>
parents: 10102
diff changeset
2195 MarkTileDirtyByTile(tile);
9a884d182149 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc <michi_cc@openttd.org>
parents: 10102
diff changeset
2196 MarkTileDirtyByTile(end);
9a884d182149 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc <michi_cc@openttd.org>
parents: 10102
diff changeset
2197 }
9809
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2198 }
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2199 }
12502
9472b49c61a1 (svn r16939) -Codechange: s/RailwayStation/RailStation/ to unify the way it's written.
rubidium <rubidium@openttd.org>
parents: 12472
diff changeset
2200 } else if (IsRailStationTile(tile)) {
9809
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2201 TileIndex new_tile = TileAddByDiagDir(tile, dir);
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2202 /* If the new tile is not a further tile of the same station, we
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2203 * clear the reservation for the whole platform. */
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2204 if (!IsCompatibleTrainStationTile(new_tile, tile)) {
12502
9472b49c61a1 (svn r16939) -Codechange: s/RailwayStation/RailStation/ to unify the way it's written.
rubidium <rubidium@openttd.org>
parents: 12472
diff changeset
2205 SetRailStationPlatformReservation(tile, ReverseDiagDir(dir), false);
9809
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2206 }
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2207 } else {
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2208 /* Any other tile */
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2209 UnreserveRailTrack(tile, TrackdirToTrack(track_dir));
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2210 }
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2211 }
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2212
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2213 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2214 * Free the reserved path in front of a vehicle.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2215 * @param v %Train owning the reserved path.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2216 * @param origin %Tile to start clearing (if #INVALID_TILE, use the current tile of \a v).
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2217 * @param orig_td Track direction (if #INVALID_TRACKDIR, use the track direction of \a v).
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2218 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2219 void FreeTrainTrackReservation(const Train *v, TileIndex origin, Trackdir orig_td)
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2220 {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
2221 assert(v->IsFrontEngine());
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2222
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2223 TileIndex tile = origin != INVALID_TILE ? origin : v->tile;
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
2224 Trackdir td = orig_td != INVALID_TRACKDIR ? orig_td : v->GetVehicleTrackdir();
12502
9472b49c61a1 (svn r16939) -Codechange: s/RailwayStation/RailStation/ to unify the way it's written.
rubidium <rubidium@openttd.org>
parents: 12472
diff changeset
2225 bool free_tile = tile != v->tile || !(IsRailStationTile(v->tile) || IsTileType(v->tile, MP_TUNNELBRIDGE));
9472b49c61a1 (svn r16939) -Codechange: s/RailwayStation/RailStation/ to unify the way it's written.
rubidium <rubidium@openttd.org>
parents: 12472
diff changeset
2226 StationID station_id = IsRailStationTile(v->tile) ? GetStationIndex(v->tile) : INVALID_STATION;
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2227
19201
a934b903e21e (svn r24078) -Fix [FS#5093,FS#5130] (r24071): A fix that breaks all other cases isn't really a fix. Redo it to make sure that reservations of trains entering or exiting depots are properly made and freed.
michi_cc <michi_cc@openttd.org>
parents: 19197
diff changeset
2228 /* Can't be holding a reservation if we enter a depot. */
a934b903e21e (svn r24078) -Fix [FS#5093,FS#5130] (r24071): A fix that breaks all other cases isn't really a fix. Redo it to make sure that reservations of trains entering or exiting depots are properly made and freed.
michi_cc <michi_cc@openttd.org>
parents: 19197
diff changeset
2229 if (IsRailDepotTile(tile) && TrackdirToExitdir(td) != GetRailDepotDirection(tile)) return;
a934b903e21e (svn r24078) -Fix [FS#5093,FS#5130] (r24071): A fix that breaks all other cases isn't really a fix. Redo it to make sure that reservations of trains entering or exiting depots are properly made and freed.
michi_cc <michi_cc@openttd.org>
parents: 19197
diff changeset
2230 if (v->track == TRACK_BIT_DEPOT) {
a934b903e21e (svn r24078) -Fix [FS#5093,FS#5130] (r24071): A fix that breaks all other cases isn't really a fix. Redo it to make sure that reservations of trains entering or exiting depots are properly made and freed.
michi_cc <michi_cc@openttd.org>
parents: 19197
diff changeset
2231 /* Front engine is in a depot. We enter if some part is not in the depot. */
a934b903e21e (svn r24078) -Fix [FS#5093,FS#5130] (r24071): A fix that breaks all other cases isn't really a fix. Redo it to make sure that reservations of trains entering or exiting depots are properly made and freed.
michi_cc <michi_cc@openttd.org>
parents: 19197
diff changeset
2232 for (const Train *u = v; u != NULL; u = u->Next()) {
a934b903e21e (svn r24078) -Fix [FS#5093,FS#5130] (r24071): A fix that breaks all other cases isn't really a fix. Redo it to make sure that reservations of trains entering or exiting depots are properly made and freed.
michi_cc <michi_cc@openttd.org>
parents: 19197
diff changeset
2233 if (u->track != TRACK_BIT_DEPOT || u->tile != v->tile) return;
a934b903e21e (svn r24078) -Fix [FS#5093,FS#5130] (r24071): A fix that breaks all other cases isn't really a fix. Redo it to make sure that reservations of trains entering or exiting depots are properly made and freed.
michi_cc <michi_cc@openttd.org>
parents: 19197
diff changeset
2234 }
a934b903e21e (svn r24078) -Fix [FS#5093,FS#5130] (r24071): A fix that breaks all other cases isn't really a fix. Redo it to make sure that reservations of trains entering or exiting depots are properly made and freed.
michi_cc <michi_cc@openttd.org>
parents: 19197
diff changeset
2235 }
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2236 /* Don't free reservation if it's not ours. */
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2237 if (TracksOverlap(GetReservedTrackbits(tile) | TrackToTrackBits(TrackdirToTrack(td)))) return;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2238
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
2239 CFollowTrackRail ft(v, GetRailTypeInfo(v->railtype)->compatible_railtypes);
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2240 while (ft.Follow(tile, td)) {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2241 tile = ft.m_new_tile;
10974
7bd3e2d0f151 (svn r15313) -Cleanup: Use helper functions we already have and add some documentation.
michi_cc <michi_cc@openttd.org>
parents: 10960
diff changeset
2242 TrackdirBits bits = ft.m_new_td_bits & TrackBitsToTrackdirBits(GetReservedTrackbits(tile));
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2243 td = RemoveFirstTrackdir(&bits);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2244 assert(bits == TRACKDIR_BIT_NONE);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2245
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2246 if (!IsValidTrackdir(td)) break;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2247
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2248 if (IsTileType(tile, MP_RAILWAY)) {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2249 if (HasSignalOnTrackdir(tile, td) && !IsPbsSignal(GetSignalType(tile, TrackdirToTrack(td)))) {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2250 /* Conventional signal along trackdir: remove reservation and stop. */
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2251 UnreserveRailTrack(tile, TrackdirToTrack(td));
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2252 break;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2253 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2254 if (HasPbsSignalOnTrackdir(tile, td)) {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2255 if (GetSignalStateByTrackdir(tile, td) == SIGNAL_STATE_RED) {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2256 /* Red PBS signal? Can't be our reservation, would be green then. */
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2257 break;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2258 } else {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2259 /* Turn the signal back to red. */
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2260 SetSignalStateByTrackdir(tile, td, SIGNAL_STATE_RED);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2261 MarkTileDirtyByTile(tile);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2262 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2263 } else if (HasSignalOnTrackdir(tile, ReverseTrackdir(td)) && IsOnewaySignal(tile, TrackdirToTrack(td))) {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2264 break;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2265 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2266 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2267
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2268 /* Don't free first station/bridge/tunnel if we are on it. */
10103
9a884d182149 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc <michi_cc@openttd.org>
parents: 10102
diff changeset
2269 if (free_tile || (!(ft.m_is_station && GetStationIndex(ft.m_new_tile) == station_id) && !ft.m_is_tunnel && !ft.m_is_bridge)) ClearPathReservation(v, tile, td);
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2270
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2271 free_tile = true;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2272 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2273 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2274
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2275 static const byte _initial_tile_subcoord[6][4][3] = {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2276 {{ 15, 8, 1 }, { 0, 0, 0 }, { 0, 8, 5 }, { 0, 0, 0 }},
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2277 {{ 0, 0, 0 }, { 8, 0, 3 }, { 0, 0, 0 }, { 8, 15, 7 }},
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2278 {{ 0, 0, 0 }, { 7, 0, 2 }, { 0, 7, 6 }, { 0, 0, 0 }},
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2279 {{ 15, 8, 2 }, { 0, 0, 0 }, { 0, 0, 0 }, { 8, 15, 6 }},
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2280 {{ 15, 7, 0 }, { 8, 0, 4 }, { 0, 0, 0 }, { 0, 0, 0 }},
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2281 {{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 8, 4 }, { 7, 15, 0 }},
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2282 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2283
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2284 /**
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2285 * Perform pathfinding for a train.
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2286 *
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2287 * @param v The train
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2288 * @param tile The tile the train is about to enter
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2289 * @param enterdir Diagonal direction the train is coming from
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2290 * @param tracks Usable tracks on the new tile
16776
93de8ffd68f7 (svn r21509) -Codechange: rename the rail pathfinders "path_not_found" parameter to "path_found" and remove the ! where the variables are set / read
rubidium <rubidium@openttd.org>
parents: 16775
diff changeset
2291 * @param path_found [out] Whether a path has been found or not.
10974
7bd3e2d0f151 (svn r15313) -Cleanup: Use helper functions we already have and add some documentation.
michi_cc <michi_cc@openttd.org>
parents: 10960
diff changeset
2292 * @param do_track_reservation Path reservation is requested
7bd3e2d0f151 (svn r15313) -Cleanup: Use helper functions we already have and add some documentation.
michi_cc <michi_cc@openttd.org>
parents: 10960
diff changeset
2293 * @param dest [out] State and destination of the requested path
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2294 * @return The best track the train should follow
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2295 */
16776
93de8ffd68f7 (svn r21509) -Codechange: rename the rail pathfinders "path_not_found" parameter to "path_found" and remove the ! where the variables are set / read
rubidium <rubidium@openttd.org>
parents: 16775
diff changeset
2296 static Track DoTrainPathfind(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool do_track_reservation, PBSTileInfo *dest)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2297 {
13833
ad09d18a3e34 (svn r18362) -Cleanup: remove OPF for RVs and NTP to clean up lots of code and simplify some things for the future
rubidium <rubidium@openttd.org>
parents: 13832
diff changeset
2298 switch (_settings_game.pf.pathfinder_for_trains) {
16776
93de8ffd68f7 (svn r21509) -Codechange: rename the rail pathfinders "path_not_found" parameter to "path_found" and remove the ! where the variables are set / read
rubidium <rubidium@openttd.org>
parents: 16775
diff changeset
2299 case VPF_NPF: return NPFTrainChooseTrack(v, tile, enterdir, tracks, path_found, do_track_reservation, dest);
93de8ffd68f7 (svn r21509) -Codechange: rename the rail pathfinders "path_not_found" parameter to "path_found" and remove the ! where the variables are set / read
rubidium <rubidium@openttd.org>
parents: 16775
diff changeset
2300 case VPF_YAPF: return YapfTrainChooseTrack(v, tile, enterdir, tracks, path_found, do_track_reservation, dest);
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2301
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2302 default: NOT_REACHED();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2303 }
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2304 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2305
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2306 /**
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2307 * Extend a train path as far as possible. Stops on encountering a safe tile,
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2308 * another reservation or a track choice.
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2309 * @return INVALID_TILE indicates that the reservation failed.
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2310 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2311 static PBSTileInfo ExtendTrainReservation(const Train *v, TrackBits *new_tracks, DiagDirection *enterdir)
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2312 {
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2313 PBSTileInfo origin = FollowTrainReservation(v);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2314
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2315 CFollowTrackRail ft(v);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2316
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2317 TileIndex tile = origin.tile;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2318 Trackdir cur_td = origin.trackdir;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2319 while (ft.Follow(tile, cur_td)) {
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2320 if (KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE) {
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2321 /* Possible signal tile. */
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2322 if (HasOnewaySignalBlockingTrackdir(ft.m_new_tile, FindFirstTrackdir(ft.m_new_td_bits))) break;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2323 }
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2324
13833
ad09d18a3e34 (svn r18362) -Cleanup: remove OPF for RVs and NTP to clean up lots of code and simplify some things for the future
rubidium <rubidium@openttd.org>
parents: 13832
diff changeset
2325 if (_settings_game.pf.forbid_90_deg) {
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2326 ft.m_new_td_bits &= ~TrackdirCrossesTrackdirs(ft.m_old_td);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2327 if (ft.m_new_td_bits == TRACKDIR_BIT_NONE) break;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2328 }
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2329
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2330 /* Station, depot or waypoint are a possible target. */
11946
d5716f5a9844 (svn r16349) -Codechange: rename IsPlainRailTile() to IsPlainRail(), introduce shiny new IsPlainRailTile()
smatz <smatz@openttd.org>
parents: 11924
diff changeset
2331 bool target_seen = ft.m_is_station || (IsTileType(ft.m_new_tile, MP_RAILWAY) && !IsPlainRail(ft.m_new_tile));
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2332 if (target_seen || KillFirstBit(ft.m_new_td_bits) != TRACKDIR_BIT_NONE) {
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2333 /* Choice found or possible target encountered.
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2334 * On finding a possible target, we need to stop and let the pathfinder handle the
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2335 * remaining path. This is because we don't know if this target is in one of our
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2336 * orders, so we might cause pathfinding to fail later on if we find a choice.
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2337 * This failure would cause a bogous call to TryReserveSafePath which might reserve
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2338 * a wrong path not leading to our next destination. */
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2339 if (HasReservedTracks(ft.m_new_tile, TrackdirBitsToTrackBits(TrackdirReachesTrackdirs(ft.m_old_td)))) break;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2340
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2341 /* If we did skip some tiles, backtrack to the first skipped tile so the pathfinder
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2342 * actually starts its search at the first unreserved tile. */
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2343 if (ft.m_tiles_skipped != 0) ft.m_new_tile -= TileOffsByDiagDir(ft.m_exitdir) * ft.m_tiles_skipped;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2344
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2345 /* Choice found, path valid but not okay. Save info about the choice tile as well. */
17399
c8b6698f2078 (svn r22145) -Codechange: Do explicit test for non-bool values.
alberth <alberth@openttd.org>
parents: 17394
diff changeset
2346 if (new_tracks != NULL) *new_tracks = TrackdirBitsToTrackBits(ft.m_new_td_bits);
c8b6698f2078 (svn r22145) -Codechange: Do explicit test for non-bool values.
alberth <alberth@openttd.org>
parents: 17394
diff changeset
2347 if (enterdir != NULL) *enterdir = ft.m_exitdir;
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2348 return PBSTileInfo(ft.m_new_tile, ft.m_old_td, false);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2349 }
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2350
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2351 tile = ft.m_new_tile;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2352 cur_td = FindFirstTrackdir(ft.m_new_td_bits);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2353
13833
ad09d18a3e34 (svn r18362) -Cleanup: remove OPF for RVs and NTP to clean up lots of code and simplify some things for the future
rubidium <rubidium@openttd.org>
parents: 13832
diff changeset
2354 if (IsSafeWaitingPosition(v, tile, cur_td, true, _settings_game.pf.forbid_90_deg)) {
ad09d18a3e34 (svn r18362) -Cleanup: remove OPF for RVs and NTP to clean up lots of code and simplify some things for the future
rubidium <rubidium@openttd.org>
parents: 13832
diff changeset
2355 bool wp_free = IsWaitingPositionFree(v, tile, cur_td, _settings_game.pf.forbid_90_deg);
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2356 if (!(wp_free && TryReserveRailTrack(tile, TrackdirToTrack(cur_td)))) break;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2357 /* Safe position is all good, path valid and okay. */
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2358 return PBSTileInfo(tile, cur_td, true);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2359 }
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2360
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2361 if (!TryReserveRailTrack(tile, TrackdirToTrack(cur_td))) break;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2362 }
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2363
10186
d5dea47b6172 (svn r14397) -Fix: possible buffer overrun, wrong parameter type passed to printf and && where || is meant. Found by MSVC's code analysis (Darkvater)
rubidium <rubidium@openttd.org>
parents: 10178
diff changeset
2364 if (ft.m_err == CFollowTrackRail::EC_OWNER || ft.m_err == CFollowTrackRail::EC_NO_WAY) {
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2365 /* End of line, path valid and okay. */
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2366 return PBSTileInfo(ft.m_old_tile, ft.m_old_td, true);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2367 }
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2368
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2369 /* Sorry, can't reserve path, back out. */
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2370 tile = origin.tile;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2371 cur_td = origin.trackdir;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2372 TileIndex stopped = ft.m_old_tile;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2373 Trackdir stopped_td = ft.m_old_td;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2374 while (tile != stopped || cur_td != stopped_td) {
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2375 if (!ft.Follow(tile, cur_td)) break;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2376
13833
ad09d18a3e34 (svn r18362) -Cleanup: remove OPF for RVs and NTP to clean up lots of code and simplify some things for the future
rubidium <rubidium@openttd.org>
parents: 13832
diff changeset
2377 if (_settings_game.pf.forbid_90_deg) {
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2378 ft.m_new_td_bits &= ~TrackdirCrossesTrackdirs(ft.m_old_td);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2379 assert(ft.m_new_td_bits != TRACKDIR_BIT_NONE);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2380 }
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2381 assert(KillFirstBit(ft.m_new_td_bits) == TRACKDIR_BIT_NONE);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2382
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2383 tile = ft.m_new_tile;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2384 cur_td = FindFirstTrackdir(ft.m_new_td_bits);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2385
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2386 UnreserveRailTrack(tile, TrackdirToTrack(cur_td));
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2387 }
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2388
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2389 /* Path invalid. */
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2390 return PBSTileInfo();
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2391 }
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2392
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2393 /**
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2394 * Try to reserve any path to a safe tile, ignoring the vehicle's destination.
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2395 * Safe tiles are tiles in front of a signal, depots and station tiles at end of line.
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2396 *
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2397 * @param v The vehicle.
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2398 * @param tile The tile the search should start from.
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2399 * @param td The trackdir the search should start from.
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2400 * @param override_railtype Whether all physically compatible railtypes should be followed.
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2401 * @return True if a path to a safe stopping tile could be reserved.
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2402 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2403 static bool TryReserveSafeTrack(const Train *v, TileIndex tile, Trackdir td, bool override_tailtype)
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2404 {
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2405 switch (_settings_game.pf.pathfinder_for_trains) {
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2406 case VPF_NPF: return NPFTrainFindNearestSafeTile(v, tile, td, override_tailtype);
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2407 case VPF_YAPF: return YapfTrainFindNearestSafeTile(v, tile, td, override_tailtype);
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2408
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2409 default: NOT_REACHED();
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2410 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2411 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2412
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2413 /** This class will save the current order of a vehicle and restore it on destruction. */
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2414 class VehicleOrderSaver {
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2415 private:
17564
d5ff867052a5 (svn r22328) -Add: a flag to GroundVehicles to disable insertion and removal of automatic orders until the next real order is reached.
frosch <frosch@openttd.org>
parents: 17489
diff changeset
2416 Train *v;
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2417 Order old_order;
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2418 TileIndex old_dest_tile;
10343
dd96353a63c7 (svn r14594) -Fix [FS#2412]: trains could get their their last visited station reset when still (un)loading causing an invalid state.
rubidium <rubidium@openttd.org>
parents: 10328
diff changeset
2419 StationID old_last_station_visited;
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2420 VehicleOrderID index;
17693
cdeed5a57cdb (svn r22473) -Codechange: Automatic orders are better called implicit orders as no real order influencing path finding is added
planetmaker <planetmaker@openttd.org>
parents: 17564
diff changeset
2421 bool suppress_implicit_orders;
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2422
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2423 public:
17564
d5ff867052a5 (svn r22328) -Add: a flag to GroundVehicles to disable insertion and removal of automatic orders until the next real order is reached.
frosch <frosch@openttd.org>
parents: 17489
diff changeset
2424 VehicleOrderSaver(Train *_v) :
10343
dd96353a63c7 (svn r14594) -Fix [FS#2412]: trains could get their their last visited station reset when still (un)loading causing an invalid state.
rubidium <rubidium@openttd.org>
parents: 10328
diff changeset
2425 v(_v),
dd96353a63c7 (svn r14594) -Fix [FS#2412]: trains could get their their last visited station reset when still (un)loading causing an invalid state.
rubidium <rubidium@openttd.org>
parents: 10328
diff changeset
2426 old_order(_v->current_order),
dd96353a63c7 (svn r14594) -Fix [FS#2412]: trains could get their their last visited station reset when still (un)loading causing an invalid state.
rubidium <rubidium@openttd.org>
parents: 10328
diff changeset
2427 old_dest_tile(_v->dest_tile),
dd96353a63c7 (svn r14594) -Fix [FS#2412]: trains could get their their last visited station reset when still (un)loading causing an invalid state.
rubidium <rubidium@openttd.org>
parents: 10328
diff changeset
2428 old_last_station_visited(_v->last_station_visited),
17564
d5ff867052a5 (svn r22328) -Add: a flag to GroundVehicles to disable insertion and removal of automatic orders until the next real order is reached.
frosch <frosch@openttd.org>
parents: 17489
diff changeset
2429 index(_v->cur_real_order_index),
17693
cdeed5a57cdb (svn r22473) -Codechange: Automatic orders are better called implicit orders as no real order influencing path finding is added
planetmaker <planetmaker@openttd.org>
parents: 17564
diff changeset
2430 suppress_implicit_orders(HasBit(_v->gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS))
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2431 {
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2432 }
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2433
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2434 ~VehicleOrderSaver()
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2435 {
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2436 this->v->current_order = this->old_order;
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2437 this->v->dest_tile = this->old_dest_tile;
10343
dd96353a63c7 (svn r14594) -Fix [FS#2412]: trains could get their their last visited station reset when still (un)loading causing an invalid state.
rubidium <rubidium@openttd.org>
parents: 10328
diff changeset
2438 this->v->last_station_visited = this->old_last_station_visited;
17693
cdeed5a57cdb (svn r22473) -Codechange: Automatic orders are better called implicit orders as no real order influencing path finding is added
planetmaker <planetmaker@openttd.org>
parents: 17564
diff changeset
2439 SB(this->v->gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS, 1, suppress_implicit_orders ? 1: 0);
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2440 }
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2441
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2442 /**
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2443 * Set the current vehicle order to the next order in the order list.
10683
4742228a9d1d (svn r14994) -Fix [FS#Aali]: Better handling of OT_LOADING and OT_LEAVESTATION in ChooseTrainTrack.
frosch <frosch@openttd.org>
parents: 10682
diff changeset
2444 * @param skip_first Shall the first (i.e. active) order be skipped?
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2445 * @return True if a suitable next order could be found.
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2446 */
10683
4742228a9d1d (svn r14994) -Fix [FS#Aali]: Better handling of OT_LOADING and OT_LEAVESTATION in ChooseTrainTrack.
frosch <frosch@openttd.org>
parents: 10682
diff changeset
2447 bool SwitchToNextOrder(bool skip_first)
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2448 {
10792
dcc9b146a749 (svn r15125) -Fix [FS#2544]: Do not try to determine the next order destination, if there are no orders.
frosch <frosch@openttd.org>
parents: 10744
diff changeset
2449 if (this->v->GetNumOrders() == 0) return false;
dcc9b146a749 (svn r15125) -Fix [FS#2544]: Do not try to determine the next order destination, if there are no orders.
frosch <frosch@openttd.org>
parents: 10744
diff changeset
2450
10683
4742228a9d1d (svn r14994) -Fix [FS#Aali]: Better handling of OT_LOADING and OT_LEAVESTATION in ChooseTrainTrack.
frosch <frosch@openttd.org>
parents: 10682
diff changeset
2451 if (skip_first) ++this->index;
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2452
18931
f3fa67f5398e (svn r23781) -Fix [FS#4964]: under certain circumstances, e.g. a single invalid order, trying to determine the next order state could end up in an infinite loop
rubidium <rubidium@openttd.org>
parents: 18923
diff changeset
2453 int depth = 0;
10569
c7206a6dabf0 (svn r14826) -Fix [FS#2494]: infinite loop in order lookahead when reserving a path (Swallow)
rubidium <rubidium@openttd.org>
parents: 10546
diff changeset
2454
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2455 do {
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2456 /* Wrap around. */
10544
836dfaca2a32 (svn r14801) -Codechange: don't reference Vehicle::num_orders directly but through a method GetNumOrders() (PhilSophus)
rubidium <rubidium@openttd.org>
parents: 10528
diff changeset
2457 if (this->index >= this->v->GetNumOrders()) this->index = 0;
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2458
11991
a4d3838349e6 (svn r16397) -Codechange: move GetVehicleOrder/GetLastVehicleOrder into Vehicle
rubidium <rubidium@openttd.org>
parents: 11988
diff changeset
2459 Order *order = this->v->GetOrder(this->index);
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2460 assert(order != NULL);
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2461
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2462 switch (order->GetType()) {
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2463 case OT_GOTO_DEPOT:
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2464 /* Skip service in depot orders when the train doesn't need service. */
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2465 if ((order->GetDepotOrderType() & ODTFB_SERVICE) && !this->v->NeedsServicing()) break;
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2466 case OT_GOTO_STATION:
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2467 case OT_GOTO_WAYPOINT:
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2468 this->v->current_order = *order;
17804
7fc728e847d1 (svn r22589) -Fix [FS#4641]: PBS order forecasting modified the current order index in case of a goto-nearest-depot order and no depot could be found.
frosch <frosch@openttd.org>
parents: 17693
diff changeset
2469 return UpdateOrderDest(this->v, order, 0, true);
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2470 case OT_CONDITIONAL: {
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2471 VehicleOrderID next = ProcessConditionalOrder(order, this->v);
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2472 if (next != INVALID_VEH_ORDER_ID) {
18931
f3fa67f5398e (svn r23781) -Fix [FS#4964]: under certain circumstances, e.g. a single invalid order, trying to determine the next order state could end up in an infinite loop
rubidium <rubidium@openttd.org>
parents: 18923
diff changeset
2473 depth++;
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2474 this->index = next;
10261
486e807fb404 (svn r14492) -Fix [FS#2366] (r14482): Conditional orders could lead to an infinite loop on path look-ahead.
michi_cc <michi_cc@openttd.org>
parents: 10252
diff changeset
2475 /* Don't increment next, so no break here. */
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2476 continue;
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2477 }
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2478 break;
9812
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2479 }
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2480 default:
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2481 break;
9812
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2482 }
10261
486e807fb404 (svn r14492) -Fix [FS#2366] (r14482): Conditional orders could lead to an infinite loop on path look-ahead.
michi_cc <michi_cc@openttd.org>
parents: 10252
diff changeset
2483 /* Don't increment inside the while because otherwise conditional
486e807fb404 (svn r14492) -Fix [FS#2366] (r14482): Conditional orders could lead to an infinite loop on path look-ahead.
michi_cc <michi_cc@openttd.org>
parents: 10252
diff changeset
2484 * orders can lead to an infinite loop. */
486e807fb404 (svn r14492) -Fix [FS#2366] (r14482): Conditional orders could lead to an infinite loop on path look-ahead.
michi_cc <michi_cc@openttd.org>
parents: 10252
diff changeset
2485 ++this->index;
18931
f3fa67f5398e (svn r23781) -Fix [FS#4964]: under certain circumstances, e.g. a single invalid order, trying to determine the next order state could end up in an infinite loop
rubidium <rubidium@openttd.org>
parents: 18923
diff changeset
2486 depth++;
f3fa67f5398e (svn r23781) -Fix [FS#4964]: under certain circumstances, e.g. a single invalid order, trying to determine the next order state could end up in an infinite loop
rubidium <rubidium@openttd.org>
parents: 18923
diff changeset
2487 } while (this->index != this->v->cur_real_order_index && depth < this->v->GetNumOrders());
10251
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2488
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2489 return false;
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2490 }
b799a50c1992 (svn r14482) -Codechange: Use a class for saving the current train order when extending the reserved path beyond the current destination during pathfinding.
michi_cc <michi_cc@openttd.org>
parents: 10236
diff changeset
2491 };
9812
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2492
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2493 /* choose a track */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2494 static Track ChooseTrainTrack(Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool force_res, bool *got_reservation, bool mark_stuck)
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2495 {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2496 Track best_track = INVALID_TRACK;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2497 bool do_track_reservation = _settings_game.pf.reserve_paths || force_res;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2498 bool changed_signal = false;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2499
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2500 assert((tracks & ~TRACK_BIT_MASK) == 0);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2501
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2502 if (got_reservation != NULL) *got_reservation = false;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2503
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2504 /* Don't use tracks here as the setting to forbid 90 deg turns might have been switched between reservation and now. */
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2505 TrackBits res_tracks = (TrackBits)(GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir));
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2506 /* Do we have a suitable reserved track? */
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2507 if (res_tracks != TRACK_BIT_NONE) return FindFirstTrack(res_tracks);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2508
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2509 /* Quick return in case only one possible track is available */
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2510 if (KillFirstBit(tracks) == TRACK_BIT_NONE) {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2511 Track track = FindFirstTrack(tracks);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2512 /* We need to check for signals only here, as a junction tile can't have signals. */
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2513 if (track != INVALID_TRACK && HasPbsSignalOnTrackdir(tile, TrackEnterdirToTrackdir(track, enterdir))) {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2514 do_track_reservation = true;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2515 changed_signal = true;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2516 SetSignalStateByTrackdir(tile, TrackEnterdirToTrackdir(track, enterdir), SIGNAL_STATE_GREEN);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2517 } else if (!do_track_reservation) {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2518 return track;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2519 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2520 best_track = track;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2521 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2522
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2523 PBSTileInfo res_dest(tile, INVALID_TRACKDIR, false);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2524 DiagDirection dest_enterdir = enterdir;
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2525 if (do_track_reservation) {
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2526 res_dest = ExtendTrainReservation(v, &tracks, &dest_enterdir);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2527 if (res_dest.tile == INVALID_TILE) {
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2528 /* Reservation failed? */
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2529 if (mark_stuck) MarkTrainAsStuck(v);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2530 if (changed_signal) SetSignalStateByTrackdir(tile, TrackEnterdirToTrackdir(best_track, enterdir), SIGNAL_STATE_RED);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2531 return FindFirstTrack(tracks);
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2532 }
18839
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2533 if (res_dest.okay) {
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2534 /* Got a valid reservation that ends at a safe target, quick exit. */
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2535 if (got_reservation != NULL) *got_reservation = true;
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2536 if (changed_signal) MarkTileDirtyByTile(tile);
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2537 TryReserveRailTrack(v->tile, TrackdirToTrack(v->GetVehicleTrackdir()));
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2538 return best_track;
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2539 }
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2540
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2541 /* Check if the train needs service here, so it has a chance to always find a depot.
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2542 * Also check if the current order is a service order so we don't reserve a path to
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2543 * the destination but instead to the next one if service isn't needed. */
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2544 CheckIfTrainNeedsService(v);
1d05c702134c (svn r23688) -Codechange: No need to check if a train needs servicing if we only extend a train's path without actually calling the pathfinder. The path extension will stop when hitting a junction tile, so it is impossible to miss a depot this way.
michi_cc <michi_cc@openttd.org>
parents: 18834
diff changeset
2545 if (v->current_order.IsType(OT_DUMMY) || v->current_order.IsType(OT_CONDITIONAL) || v->current_order.IsType(OT_GOTO_DEPOT)) ProcessOrders(v);
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2546 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2547
10252
d0582ee8991e (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc <michi_cc@openttd.org>
parents: 10251
diff changeset
2548 /* Save the current train order. The destructor will restore the old order on function exit. */
d0582ee8991e (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc <michi_cc@openttd.org>
parents: 10251
diff changeset
2549 VehicleOrderSaver orders(v);
d0582ee8991e (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc <michi_cc@openttd.org>
parents: 10251
diff changeset
2550
d0582ee8991e (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc <michi_cc@openttd.org>
parents: 10251
diff changeset
2551 /* If the current tile is the destination of the current order and
10307
534319441220 (svn r14550) -Fix [FS#2391]: Don't assert on path look-ahead when processing a not-part-of-orders depot-order.
michi_cc <michi_cc@openttd.org>
parents: 10287
diff changeset
2552 * a reservation was requested, advance to the next order.
534319441220 (svn r14550) -Fix [FS#2391]: Don't assert on path look-ahead when processing a not-part-of-orders depot-order.
michi_cc <michi_cc@openttd.org>
parents: 10287
diff changeset
2553 * Don't advance on a depot order as depots are always safe end points
534319441220 (svn r14550) -Fix [FS#2391]: Don't assert on path look-ahead when processing a not-part-of-orders depot-order.
michi_cc <michi_cc@openttd.org>
parents: 10287
diff changeset
2554 * for a path and no look-ahead is necessary. This also avoids a
534319441220 (svn r14550) -Fix [FS#2391]: Don't assert on path look-ahead when processing a not-part-of-orders depot-order.
michi_cc <michi_cc@openttd.org>
parents: 10287
diff changeset
2555 * problem with depot orders not part of the order list when the
534319441220 (svn r14550) -Fix [FS#2391]: Don't assert on path look-ahead when processing a not-part-of-orders depot-order.
michi_cc <michi_cc@openttd.org>
parents: 10287
diff changeset
2556 * order list itself is empty. */
10683
4742228a9d1d (svn r14994) -Fix [FS#Aali]: Better handling of OT_LOADING and OT_LEAVESTATION in ChooseTrainTrack.
frosch <frosch@openttd.org>
parents: 10682
diff changeset
2557 if (v->current_order.IsType(OT_LEAVESTATION)) {
4742228a9d1d (svn r14994) -Fix [FS#Aali]: Better handling of OT_LOADING and OT_LEAVESTATION in ChooseTrainTrack.
frosch <frosch@openttd.org>
parents: 10682
diff changeset
2558 orders.SwitchToNextOrder(false);
4742228a9d1d (svn r14994) -Fix [FS#Aali]: Better handling of OT_LOADING and OT_LEAVESTATION in ChooseTrainTrack.
frosch <frosch@openttd.org>
parents: 10682
diff changeset
2559 } else if (v->current_order.IsType(OT_LOADING) || (!v->current_order.IsType(OT_GOTO_DEPOT) && (
10444
368a9eb9207e (svn r14699) -Fix (r14483): Do not use v->dest_tile for trains heading to a station. Reaching that tile does not mean reaching the station.
frosch <frosch@openttd.org>
parents: 10441
diff changeset
2560 v->current_order.IsType(OT_GOTO_STATION) ?
12502
9472b49c61a1 (svn r16939) -Codechange: s/RailwayStation/RailStation/ to unify the way it's written.
rubidium <rubidium@openttd.org>
parents: 12472
diff changeset
2561 IsRailStationTile(v->tile) && v->current_order.GetDestination() == GetStationIndex(v->tile) :
10683
4742228a9d1d (svn r14994) -Fix [FS#Aali]: Better handling of OT_LOADING and OT_LEAVESTATION in ChooseTrainTrack.
frosch <frosch@openttd.org>
parents: 10682
diff changeset
2562 v->tile == v->dest_tile))) {
4742228a9d1d (svn r14994) -Fix [FS#Aali]: Better handling of OT_LOADING and OT_LEAVESTATION in ChooseTrainTrack.
frosch <frosch@openttd.org>
parents: 10682
diff changeset
2563 orders.SwitchToNextOrder(true);
10252
d0582ee8991e (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc <michi_cc@openttd.org>
parents: 10251
diff changeset
2564 }
d0582ee8991e (svn r14483) -Fix [FS#2340]: On reaching a waypoint a train could falsely be reported as lost when doing the look ahead for a path.
michi_cc <michi_cc@openttd.org>
parents: 10251
diff changeset
2565
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2566 if (res_dest.tile != INVALID_TILE && !res_dest.okay) {
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2567 /* Pathfinders are able to tell that route was only 'guessed'. */
16776
93de8ffd68f7 (svn r21509) -Codechange: rename the rail pathfinders "path_not_found" parameter to "path_found" and remove the ! where the variables are set / read
rubidium <rubidium@openttd.org>
parents: 16775
diff changeset
2568 bool path_found = true;
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2569 TileIndex new_tile = res_dest.tile;
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2570
16776
93de8ffd68f7 (svn r21509) -Codechange: rename the rail pathfinders "path_not_found" parameter to "path_found" and remove the ! where the variables are set / read
rubidium <rubidium@openttd.org>
parents: 16775
diff changeset
2571 Track next_track = DoTrainPathfind(v, new_tile, dest_enterdir, tracks, path_found, do_track_reservation, &res_dest);
9817
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2572 if (new_tile == tile) best_track = next_track;
16776
93de8ffd68f7 (svn r21509) -Codechange: rename the rail pathfinders "path_not_found" parameter to "path_found" and remove the ! where the variables are set / read
rubidium <rubidium@openttd.org>
parents: 16775
diff changeset
2573 v->HandlePathfindingResult(path_found);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2574 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2575
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2576 /* No track reservation requested -> finished. */
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2577 if (!do_track_reservation) return best_track;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2578
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2579 /* A path was found, but could not be reserved. */
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2580 if (res_dest.tile != INVALID_TILE && !res_dest.okay) {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2581 if (mark_stuck) MarkTrainAsStuck(v);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2582 FreeTrainTrackReservation(v);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2583 return best_track;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2584 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2585
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2586 /* No possible reservation target found, we are probably lost. */
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2587 if (res_dest.tile == INVALID_TILE) {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2588 /* Try to find any safe destination. */
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2589 PBSTileInfo origin = FollowTrainReservation(v);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2590 if (TryReserveSafeTrack(v, origin.tile, origin.trackdir, false)) {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2591 TrackBits res = GetReservedTrackbits(tile) & DiagdirReachesTracks(enterdir);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2592 best_track = FindFirstTrack(res);
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
2593 TryReserveRailTrack(v->tile, TrackdirToTrack(v->GetVehicleTrackdir()));
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2594 if (got_reservation != NULL) *got_reservation = true;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2595 if (changed_signal) MarkTileDirtyByTile(tile);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2596 } else {
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2597 FreeTrainTrackReservation(v);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2598 if (mark_stuck) MarkTrainAsStuck(v);
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2599 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2600 return best_track;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2601 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2602
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2603 if (got_reservation != NULL) *got_reservation = true;
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2604
9812
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2605 /* Reservation target found and free, check if it is safe. */
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2606 while (!IsSafeWaitingPosition(v, res_dest.tile, res_dest.trackdir, true, _settings_game.pf.forbid_90_deg)) {
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2607 /* Extend reservation until we have found a safe position. */
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2608 DiagDirection exitdir = TrackdirToExitdir(res_dest.trackdir);
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2609 TileIndex next_tile = TileAddByDiagDir(res_dest.tile, exitdir);
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2610 TrackBits reachable = TrackdirBitsToTrackBits((TrackdirBits)(GetTileTrackStatus(next_tile, TRANSPORT_RAIL, 0))) & DiagdirReachesTracks(exitdir);
13833
ad09d18a3e34 (svn r18362) -Cleanup: remove OPF for RVs and NTP to clean up lots of code and simplify some things for the future
rubidium <rubidium@openttd.org>
parents: 13832
diff changeset
2611 if (_settings_game.pf.forbid_90_deg) {
9812
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2612 reachable &= ~TrackCrossesTracks(TrackdirToTrack(res_dest.trackdir));
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2613 }
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2614
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2615 /* Get next order with destination. */
10683
4742228a9d1d (svn r14994) -Fix [FS#Aali]: Better handling of OT_LOADING and OT_LEAVESTATION in ChooseTrainTrack.
frosch <frosch@openttd.org>
parents: 10682
diff changeset
2616 if (orders.SwitchToNextOrder(true)) {
9812
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2617 PBSTileInfo cur_dest;
16776
93de8ffd68f7 (svn r21509) -Codechange: rename the rail pathfinders "path_not_found" parameter to "path_found" and remove the ! where the variables are set / read
rubidium <rubidium@openttd.org>
parents: 16775
diff changeset
2618 bool path_found;
93de8ffd68f7 (svn r21509) -Codechange: rename the rail pathfinders "path_not_found" parameter to "path_found" and remove the ! where the variables are set / read
rubidium <rubidium@openttd.org>
parents: 16775
diff changeset
2619 DoTrainPathfind(v, next_tile, exitdir, reachable, path_found, true, &cur_dest);
9812
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2620 if (cur_dest.tile != INVALID_TILE) {
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2621 res_dest = cur_dest;
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2622 if (res_dest.okay) continue;
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2623 /* Path found, but could not be reserved. */
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2624 FreeTrainTrackReservation(v);
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2625 if (mark_stuck) MarkTrainAsStuck(v);
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2626 if (got_reservation != NULL) *got_reservation = false;
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2627 changed_signal = false;
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2628 break;
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2629 }
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2630 }
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2631 /* No order or no safe position found, try any position. */
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2632 if (!TryReserveSafeTrack(v, res_dest.tile, res_dest.trackdir, true)) {
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2633 FreeTrainTrackReservation(v);
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2634 if (mark_stuck) MarkTrainAsStuck(v);
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2635 if (got_reservation != NULL) *got_reservation = false;
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2636 changed_signal = false;
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2637 }
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2638 break;
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2639 }
861a72996945 (svn r13954) -Codechange [YAPP]: On reserving a path that ends at the destination, the path could end at a non-safe tile. In this case, extend the reservation based on the next vehicle orders. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9811
diff changeset
2640
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
2641 TryReserveRailTrack(v->tile, TrackdirToTrack(v->GetVehicleTrackdir()));
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2642
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2643 if (changed_signal) MarkTileDirtyByTile(tile);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2644
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2645 return best_track;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2646 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2647
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2648 /**
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2649 * Try to reserve a path to a safe position.
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2650 *
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2651 * @param v The vehicle
10974
7bd3e2d0f151 (svn r15313) -Cleanup: Use helper functions we already have and add some documentation.
michi_cc <michi_cc@openttd.org>
parents: 10960
diff changeset
2652 * @param mark_as_stuck Should the train be marked as stuck on a failed reservation?
7bd3e2d0f151 (svn r15313) -Cleanup: Use helper functions we already have and add some documentation.
michi_cc <michi_cc@openttd.org>
parents: 10960
diff changeset
2653 * @param first_tile_okay True if no path should be reserved if the current tile is a safe position.
7bd3e2d0f151 (svn r15313) -Cleanup: Use helper functions we already have and add some documentation.
michi_cc <michi_cc@openttd.org>
parents: 10960
diff changeset
2654 * @return True if a path could be reserved.
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2655 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2656 bool TryPathReserve(Train *v, bool mark_as_stuck, bool first_tile_okay)
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2657 {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
2658 assert(v->IsFrontEngine());
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2659
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2660 /* We have to handle depots specially as the track follower won't look
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2661 * at the depot tile itself but starts from the next tile. If we are still
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2662 * inside the depot, a depot reservation can never be ours. */
12137
b3c5a6c9146a (svn r16553) -Codechange: don't use TRACK_BIT_WORMHOLE and TRACK_BIT_DEPOT as bitmasks
smatz <smatz@openttd.org>
parents: 12129
diff changeset
2663 if (v->track == TRACK_BIT_DEPOT) {
12471
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2664 if (HasDepotReservation(v->tile)) {
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2665 if (mark_as_stuck) MarkTrainAsStuck(v);
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2666 return false;
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2667 } else {
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2668 /* Depot not reserved, but the next tile might be. */
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2669 TileIndex next_tile = TileAddByDiagDir(v->tile, GetRailDepotDirection(v->tile));
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2670 if (HasReservedTracks(next_tile, DiagdirReachesTracks(GetRailDepotDirection(v->tile)))) return false;
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2671 }
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2672 }
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2673
14100
e4f2ba58a34f (svn r18647) -Fix: [YAPP] A train inside a station was not always found when checking for trains on a reserved path.
michi_cc <michi_cc@openttd.org>
parents: 14081
diff changeset
2674 Vehicle *other_train = NULL;
9831
7c2cb1b0402d (svn r13974) -Fix [YAPP]: A train crash could lead to stale reservations. (michi_cc)
frosch <frosch@openttd.org>
parents: 9828
diff changeset
2675 PBSTileInfo origin = FollowTrainReservation(v, &other_train);
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
2676 /* The path we are driving on is already blocked by some other train.
12692
09fa66ffeadd (svn r17151) -Fix [FS#3104] (r13974): A train entering a PBS section through a block signal could cause a train crash if another reservation ending at a safe tile was already present in the section.
michi_cc <michi_cc@openttd.org>
parents: 12666
diff changeset
2677 * This can only happen in certain situations when mixing path and
09fa66ffeadd (svn r17151) -Fix [FS#3104] (r13974): A train entering a PBS section through a block signal could cause a train crash if another reservation ending at a safe tile was already present in the section.
michi_cc <michi_cc@openttd.org>
parents: 12666
diff changeset
2678 * block signals or when changing tracks and/or signals.
09fa66ffeadd (svn r17151) -Fix [FS#3104] (r13974): A train entering a PBS section through a block signal could cause a train crash if another reservation ending at a safe tile was already present in the section.
michi_cc <michi_cc@openttd.org>
parents: 12666
diff changeset
2679 * Exit here as doing any further reservations will probably just
09fa66ffeadd (svn r17151) -Fix [FS#3104] (r13974): A train entering a PBS section through a block signal could cause a train crash if another reservation ending at a safe tile was already present in the section.
michi_cc <michi_cc@openttd.org>
parents: 12666
diff changeset
2680 * make matters worse. */
14100
e4f2ba58a34f (svn r18647) -Fix: [YAPP] A train inside a station was not always found when checking for trains on a reserved path.
michi_cc <michi_cc@openttd.org>
parents: 14081
diff changeset
2681 if (other_train != NULL && other_train->index != v->index) {
12692
09fa66ffeadd (svn r17151) -Fix [FS#3104] (r13974): A train entering a PBS section through a block signal could cause a train crash if another reservation ending at a safe tile was already present in the section.
michi_cc <michi_cc@openttd.org>
parents: 12666
diff changeset
2682 if (mark_as_stuck) MarkTrainAsStuck(v);
09fa66ffeadd (svn r17151) -Fix [FS#3104] (r13974): A train entering a PBS section through a block signal could cause a train crash if another reservation ending at a safe tile was already present in the section.
michi_cc <michi_cc@openttd.org>
parents: 12666
diff changeset
2683 return false;
09fa66ffeadd (svn r17151) -Fix [FS#3104] (r13974): A train entering a PBS section through a block signal could cause a train crash if another reservation ending at a safe tile was already present in the section.
michi_cc <michi_cc@openttd.org>
parents: 12666
diff changeset
2684 }
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2685 /* If we have a reserved path and the path ends at a safe tile, we are finished already. */
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2686 if (origin.okay && (v->tile != origin.tile || first_tile_okay)) {
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2687 /* Can't be stuck then. */
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
2688 if (HasBit(v->flags, VRF_TRAIN_STUCK)) SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
2689 ClrBit(v->flags, VRF_TRAIN_STUCK);
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2690 return true;
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2691 }
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2692
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
2693 /* If we are in a depot, tentatively reserve the depot. */
12137
b3c5a6c9146a (svn r16553) -Codechange: don't use TRACK_BIT_WORMHOLE and TRACK_BIT_DEPOT as bitmasks
smatz <smatz@openttd.org>
parents: 12129
diff changeset
2694 if (v->track == TRACK_BIT_DEPOT) {
12471
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2695 SetDepotReservation(v->tile, true);
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2696 if (_settings_client.gui.show_track_reservation) MarkTileDirtyByTile(v->tile);
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2697 }
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2698
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2699 DiagDirection exitdir = TrackdirToExitdir(origin.trackdir);
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2700 TileIndex new_tile = TileAddByDiagDir(origin.tile, exitdir);
10974
7bd3e2d0f151 (svn r15313) -Cleanup: Use helper functions we already have and add some documentation.
michi_cc <michi_cc@openttd.org>
parents: 10960
diff changeset
2701 TrackBits reachable = TrackdirBitsToTrackBits(TrackStatusToTrackdirBits(GetTileTrackStatus(new_tile, TRANSPORT_RAIL, 0)) & DiagdirReachesTrackdirs(exitdir));
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2702
13833
ad09d18a3e34 (svn r18362) -Cleanup: remove OPF for RVs and NTP to clean up lots of code and simplify some things for the future
rubidium <rubidium@openttd.org>
parents: 13832
diff changeset
2703 if (_settings_game.pf.forbid_90_deg) reachable &= ~TrackCrossesTracks(TrackdirToTrack(origin.trackdir));
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2704
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2705 bool res_made = false;
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2706 ChooseTrainTrack(v, new_tile, exitdir, reachable, true, &res_made, mark_as_stuck);
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2707
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2708 if (!res_made) {
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2709 /* Free the depot reservation as well. */
12471
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2710 if (v->track == TRACK_BIT_DEPOT) SetDepotReservation(v->tile, false);
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2711 return false;
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2712 }
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2713
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
2714 if (HasBit(v->flags, VRF_TRAIN_STUCK)) {
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
2715 v->wait_counter = 0;
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
2716 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
10587
f03dcfd93ae5 (svn r14850) -Fix [FS#2503]: Reversing a stuck train that is then not stuck anymore did not always reset the waiting timer.
michi_cc <michi_cc@openttd.org>
parents: 10577
diff changeset
2717 }
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
2718 ClrBit(v->flags, VRF_TRAIN_STUCK);
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2719 return true;
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2720 }
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
2721
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2722
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2723 static bool CheckReverseTrain(const Train *v)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2724 {
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9358
diff changeset
2725 if (_settings_game.difficulty.line_reverse_mode != 0 ||
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
2726 v->track == TRACK_BIT_DEPOT || v->track == TRACK_BIT_WORMHOLE ||
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2727 !(v->direction & 1)) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2728 return false;
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2729 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2730
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2731 assert(v->track != TRACK_BIT_NONE);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2732
9413
512ea64da840 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium <rubidium@openttd.org>
parents: 9358
diff changeset
2733 switch (_settings_game.pf.pathfinder_for_trains) {
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2734 case VPF_NPF: return NPFTrainCheckReverse(v);
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2735 case VPF_YAPF: return YapfTrainCheckReverse(v);
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2736
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2737 default: NOT_REACHED();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2738 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2739 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2740
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2741 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2742 * Get the location of the next station to visit.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2743 * @param station Next station to visit.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2744 * @return Location of the new station.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2745 */
8827
095c429c5fa7 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium <rubidium@openttd.org>
parents: 8794
diff changeset
2746 TileIndex Train::GetOrderStationLocation(StationID station)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2747 {
8830
981a95f3204d (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium <rubidium@openttd.org>
parents: 8827
diff changeset
2748 if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
981a95f3204d (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium <rubidium@openttd.org>
parents: 8827
diff changeset
2749
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11920
diff changeset
2750 const Station *st = Station::Get(station);
8928
f256fabe6441 (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium <rubidium@openttd.org>
parents: 8891
diff changeset
2751 if (!(st->facilities & FACIL_TRAIN)) {
f256fabe6441 (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium <rubidium@openttd.org>
parents: 8891
diff changeset
2752 /* The destination station has no trainstation tiles. */
17194
b590eca5d805 (svn r21933) -Codechange: Split cur_order_index into cur_auto_order_index and cur_real_order_index to keep track of the current real order in an unambiguous way.
frosch <frosch@openttd.org>
parents: 17175
diff changeset
2753 this->IncrementRealOrderIndex();
8928
f256fabe6441 (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium <rubidium@openttd.org>
parents: 8891
diff changeset
2754 return 0;
f256fabe6441 (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium <rubidium@openttd.org>
parents: 8891
diff changeset
2755 }
f256fabe6441 (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium <rubidium@openttd.org>
parents: 8891
diff changeset
2756
f256fabe6441 (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium <rubidium@openttd.org>
parents: 8891
diff changeset
2757 return st->xy;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2758 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2759
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2760 /** Goods at the consist have changed, update the graphics, cargo, and acceleration. */
6553
733ef9947fe2 (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium <rubidium@openttd.org>
parents: 6552
diff changeset
2761 void Train::MarkDirty()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2762 {
15127
c065afa82756 (svn r19756) -Codechange: move UpdateViewport() from Vehicle to SpecializedVehicle in order to improve performance
smatz <smatz@openttd.org>
parents: 15073
diff changeset
2763 Train *v = this;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2764 do {
12666
0275245c6c62 (svn r17121) -Fix [FS#3060]: Update vehicle position cache when the vehicle sprite changes.
frosch <frosch@openttd.org>
parents: 12635
diff changeset
2765 v->UpdateViewport(false, false);
7492
09743324277c (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium <rubidium@openttd.org>
parents: 7488
diff changeset
2766 } while ((v = v->Next()) != NULL);
6553
733ef9947fe2 (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium <rubidium@openttd.org>
parents: 6552
diff changeset
2767
733ef9947fe2 (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium <rubidium@openttd.org>
parents: 6552
diff changeset
2768 /* need to update acceleration and cached values since the goods on the train changed. */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
2769 this->CargoChanged();
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
2770 this->UpdateAcceleration();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2771 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2772
10214
44a5ab175885 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium <rubidium@openttd.org>
parents: 10208
diff changeset
2773 /**
15002
baf0e12ad2b4 (svn r19614) -Codechange: "it's" => "its" where appropriate
smatz <smatz@openttd.org>
parents: 14898
diff changeset
2774 * This function looks at the vehicle and updates its speed (cur_speed
10214
44a5ab175885 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium <rubidium@openttd.org>
parents: 10208
diff changeset
2775 * and subspeed) variables. Furthermore, it returns the distance that
15436
a90f03aeb026 (svn r20079) -Codechange [FS#3922]: Add helper functions to deal with the 192-256-magic of vehicle movement.
frosch <frosch@openttd.org>
parents: 15363
diff changeset
2776 * the train can drive this tick. #Vehicle::GetAdvanceDistance() determines
a90f03aeb026 (svn r20079) -Codechange [FS#3922]: Add helper functions to deal with the 192-256-magic of vehicle movement.
frosch <frosch@openttd.org>
parents: 15363
diff changeset
2777 * the distance to drive before moving a step on the map.
10214
44a5ab175885 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium <rubidium@openttd.org>
parents: 10208
diff changeset
2778 * @return distance to drive.
44a5ab175885 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium <rubidium@openttd.org>
parents: 10208
diff changeset
2779 */
14284
fc532d11eb85 (svn r18836) -Codechange: make TrainUpdateSpeed a class function and update some comments (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14261
diff changeset
2780 int Train::UpdateSpeed()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2781 {
14286
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
2782 switch (_settings_game.vehicle.train_acceleration_model) {
f96af1b6a264 (svn r18838) -Codechange [FS#3524]: reorganisation of the train acceleration code plus some minor optimisations (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14284
diff changeset
2783 default: NOT_REACHED();
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
2784 case AM_ORIGINAL:
20206
c2446757542d (svn r25167) -Fix [FS#5523] (r24832): Original train and rv acceleration did no longer respect bridge speed limits.
frosch <frosch@openttd.org>
parents: 20065
diff changeset
2785 return this->DoUpdateSpeed(this->acceleration * (this->GetAccelerationStatus() == AS_BRAKE ? -4 : 2), 0, this->GetCurrentMaxSpeed());
17175
b21a59ab8453 (svn r21914) -Codechange: move the algorithmic part of Train::UpdateSpeed to a function in GroundVehicle
rubidium <rubidium@openttd.org>
parents: 17172
diff changeset
2786
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
2787 case AM_REALISTIC:
17175
b21a59ab8453 (svn r21914) -Codechange: move the algorithmic part of Train::UpdateSpeed to a function in GroundVehicle
rubidium <rubidium@openttd.org>
parents: 17172
diff changeset
2788 return this->DoUpdateSpeed(this->GetAcceleration(), this->GetAccelerationStatus() == AS_BRAKE ? 0 : 2, this->GetCurrentMaxSpeed());
14289
d4cfa529dcec (svn r18841) -Fix (r16869): Default-waypoint was drawn incorrectly for monorail and maglev in the waypoint picker.
frosch <frosch@openttd.org>
parents: 14286
diff changeset
2789 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2790 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2791
16372
ea86953aa185 (svn r21088) -Doc: Additions/corrections of doxygen comments.
alberth <alberth@openttd.org>
parents: 16349
diff changeset
2792 /**
ea86953aa185 (svn r21088) -Doc: Additions/corrections of doxygen comments.
alberth <alberth@openttd.org>
parents: 16349
diff changeset
2793 * Trains enters a station, send out a news item if it is the first train, and start loading.
ea86953aa185 (svn r21088) -Doc: Additions/corrections of doxygen comments.
alberth <alberth@openttd.org>
parents: 16349
diff changeset
2794 * @param v Train that entered the station.
ea86953aa185 (svn r21088) -Doc: Additions/corrections of doxygen comments.
alberth <alberth@openttd.org>
parents: 16349
diff changeset
2795 * @param station Station visited.
ea86953aa185 (svn r21088) -Doc: Additions/corrections of doxygen comments.
alberth <alberth@openttd.org>
parents: 16349
diff changeset
2796 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2797 static void TrainEnterStation(Train *v, StationID station)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2798 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2799 v->last_station_visited = station;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2800
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2801 /* check if a train ever visited this station before */
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11920
diff changeset
2802 Station *st = Station::Get(station);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2803 if (!(st->had_vehicle_of_type & HVOT_TRAIN)) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2804 st->had_vehicle_of_type |= HVOT_TRAIN;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2805 SetDParam(0, st->index);
12010
d0bf08a7c260 (svn r16416) -Fix [FS#2912]: Rework deleting of news when referenced vehicles/stations/industries are deleted.
frosch <frosch@openttd.org>
parents: 12008
diff changeset
2806 AddVehicleNewsItem(
11730
a39da1e4c1f5 (svn r16118) -Change/cleanup: remove the hexadecimal 'in TTD the string had this ID' from 'some' strings and replace the string name with something more sensible.
rubidium <rubidium@openttd.org>
parents: 11701
diff changeset
2807 STR_NEWS_FIRST_TRAIN_ARRIVAL,
19383
0c6420024e22 (svn r24284) -Codechange: Remove NewsSubtypes and directly use NewsTypes and NewsFlag instead.
frosch <frosch@openttd.org>
parents: 19381
diff changeset
2808 v->owner == _local_company ? NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2809 v->index,
10123
57aa34285d05 (svn r14307) -Fix: when deleting a station, remove news items regarding it
smatz <smatz@openttd.org>
parents: 10103
diff changeset
2810 st->index
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2811 );
18511
f1214da4c397 (svn r23355) -Codechange: rename all AI* to Script* (Rubidium)
truebrain <truebrain@openttd.org>
parents: 18472
diff changeset
2812 AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
18774
1fa19d70aea9 (svn r23622) -Add: a set of events to trigger in a GameScript
truebrain <truebrain@openttd.org>
parents: 18692
diff changeset
2813 Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2814 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2815
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
2816 v->force_proceed = TFP_NONE;
15351
6adf1d7d80cd (svn r19992) -Fix [FS#3878]: Clear force_proceed when entering depots and when loading.
frosch <frosch@openttd.org>
parents: 15320
diff changeset
2817 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
6adf1d7d80cd (svn r19992) -Fix [FS#3878]: Clear force_proceed when entering depots and when loading.
frosch <frosch@openttd.org>
parents: 15320
diff changeset
2818
6550
2fa52ce08b41 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium <rubidium@openttd.org>
parents: 6546
diff changeset
2819 v->BeginLoading();
9003
ac23e012c9d7 (svn r12798) -Feature: Add some support for NewGRF station animation. (Thanks to mart3p for samples and fixes)
peter1138 <peter1138@openttd.org>
parents: 9000
diff changeset
2820
19973
f8c0e108128c (svn r24905) -Feature(ish): Implement station randomisation triggers.
peter1138 <peter1138@openttd.org>
parents: 19968
diff changeset
2821 TriggerStationRandomisation(st, v->tile, SRT_TRAIN_ARRIVES);
15935
a4fa352275f4 (svn r20623) -Codechange: unify the storing of animation related information
rubidium <rubidium@openttd.org>
parents: 15899
diff changeset
2822 TriggerStationAnimation(st, v->tile, SAT_TRAIN_ARRIVES);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2823 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2824
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2825 /* Check if the vehicle is compatible with the specified tile */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2826 static inline bool CheckCompatibleRail(const Train *v, TileIndex tile)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2827 {
16547
a11cd939c335 (svn r21273) -Codechange: Return values should start at the same line.
alberth <alberth@openttd.org>
parents: 16511
diff changeset
2828 return IsTileOwner(tile, v->owner) &&
a11cd939c335 (svn r21273) -Codechange: Return values should start at the same line.
alberth <alberth@openttd.org>
parents: 16511
diff changeset
2829 (!v->IsFrontEngine() || HasBit(v->compatible_railtypes, GetRailType(tile)));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2830 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2831
18999
081eb943326c (svn r23855) -Fix [FS#5007]: out of bounds read for slowdown parameter caused desync when railtype >= 4, vehicles were fast, and the original acceleration model was used
rubidium <rubidium@openttd.org>
parents: 18931
diff changeset
2832 /** Data structure for storing engine speed changes of an acceleration type. */
081eb943326c (svn r23855) -Fix [FS#5007]: out of bounds read for slowdown parameter caused desync when railtype >= 4, vehicles were fast, and the original acceleration model was used
rubidium <rubidium@openttd.org>
parents: 18931
diff changeset
2833 struct AccelerationSlowdownParams {
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2834 byte small_turn; ///< Speed change due to a small turn.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2835 byte large_turn; ///< Speed change due to a large turn.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2836 byte z_up; ///< Fraction to remove when moving up.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2837 byte z_down; ///< Fraction to add when moving down.
6248
0789677a15a0 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium <rubidium@openttd.org>
parents: 6247
diff changeset
2838 };
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2839
18999
081eb943326c (svn r23855) -Fix [FS#5007]: out of bounds read for slowdown parameter caused desync when railtype >= 4, vehicles were fast, and the original acceleration model was used
rubidium <rubidium@openttd.org>
parents: 18931
diff changeset
2840 /** Speed update fractions for each acceleration type. */
081eb943326c (svn r23855) -Fix [FS#5007]: out of bounds read for slowdown parameter caused desync when railtype >= 4, vehicles were fast, and the original acceleration model was used
rubidium <rubidium@openttd.org>
parents: 18931
diff changeset
2841 static const AccelerationSlowdownParams _accel_slowdown[] = {
11368
82c90cd591ab (svn r15718) -Cleanup: apply some comment coding style on the rest of the sources too
rubidium <rubidium@openttd.org>
parents: 11351
diff changeset
2842 /* normal accel */
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2843 {256 / 4, 256 / 2, 256 / 4, 2}, ///< normal
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2844 {256 / 4, 256 / 2, 256 / 4, 2}, ///< monorail
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2845 {0, 256 / 2, 256 / 4, 2}, ///< maglev
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2846 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2847
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2848 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2849 * Modify the speed of the vehicle due to a change in altitude.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2850 * @param v %Train to update.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2851 * @param old_z Previous height.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2852 */
18272
3ebab55c5495 (svn r23108) -Codechange: more uint -> int / byte -> int conversions for Z related variables
rubidium <rubidium@openttd.org>
parents: 18255
diff changeset
2853 static inline void AffectSpeedByZChange(Train *v, int old_z)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2854 {
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
2855 if (old_z == v->z_pos || _settings_game.vehicle.train_acceleration_model != AM_ORIGINAL) return;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2856
18999
081eb943326c (svn r23855) -Fix [FS#5007]: out of bounds read for slowdown parameter caused desync when railtype >= 4, vehicles were fast, and the original acceleration model was used
rubidium <rubidium@openttd.org>
parents: 18931
diff changeset
2857 const AccelerationSlowdownParams *asp = &_accel_slowdown[GetRailTypeInfo(v->railtype)->acceleration_type];
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2858
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2859 if (old_z < v->z_pos) {
18999
081eb943326c (svn r23855) -Fix [FS#5007]: out of bounds read for slowdown parameter caused desync when railtype >= 4, vehicles were fast, and the original acceleration model was used
rubidium <rubidium@openttd.org>
parents: 18931
diff changeset
2860 v->cur_speed -= (v->cur_speed * asp->z_up >> 8);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2861 } else {
18999
081eb943326c (svn r23855) -Fix [FS#5007]: out of bounds read for slowdown parameter caused desync when railtype >= 4, vehicles were fast, and the original acceleration model was used
rubidium <rubidium@openttd.org>
parents: 18931
diff changeset
2862 uint16 spd = v->cur_speed + asp->z_down;
16785
775f357828e3 (svn r21518) -Codechange: Rename AccelerationCache to GroundVehicleCache.
terkhen <terkhen@openttd.org>
parents: 16776
diff changeset
2863 if (spd <= v->gcache.cached_max_track_speed) v->cur_speed = spd;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2864 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2865 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2866
9821
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
2867 static bool TrainMovedChangeSignals(TileIndex tile, DiagDirection dir)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2868 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2869 if (IsTileType(tile, MP_RAILWAY) &&
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2870 GetRailTileType(tile) == RAIL_TILE_SIGNALS) {
8794
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
2871 TrackdirBits tracks = TrackBitsToTrackdirBits(GetTrackBits(tile)) & DiagdirReachesTrackdirs(dir);
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
2872 Trackdir trackdir = FindFirstTrackdir(tracks);
9821
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
2873 if (UpdateSignalsOnSegment(tile, TrackdirToExitdir(trackdir), GetTileOwner(tile)) == SIGSEG_PBS && HasSignalOnTrackdir(tile, trackdir)) {
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
2874 /* A PBS block with a non-PBS signal facing us? */
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
2875 if (!IsPbsSignal(GetSignalType(tile, TrackdirToTrack(trackdir)))) return true;
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
2876 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2877 }
9821
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
2878 return false;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2879 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2880
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2881 /** Tries to reserve track under whole train consist. */
12423
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2882 void Train::ReserveTrackUnderConsist() const
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2883 {
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2884 for (const Train *u = this; u != NULL; u = u->Next()) {
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2885 switch (u->track) {
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2886 case TRACK_BIT_WORMHOLE:
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2887 TryReserveRailTrack(u->tile, DiagDirToDiagTrack(GetTunnelBridgeDirection(u->tile)));
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2888 break;
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2889 case TRACK_BIT_DEPOT:
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2890 break;
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2891 default:
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2892 TryReserveRailTrack(u->tile, TrackBitsToTrack(u->track));
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2893 break;
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2894 }
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2895 }
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2896 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2897
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2898 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2899 * The train vehicle crashed!
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2900 * Update its status and other parts around it.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2901 * @param flooded Crash was caused by flooding.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2902 * @return Number of people killed.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2903 */
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2904 uint Train::Crash(bool flooded)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2905 {
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2906 uint pass = 0;
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2907 if (this->IsFrontEngine()) {
16769
1a7dbc448da5 (svn r21502) -Change/Fix [FS#3991] (r150, r18402): before r18402 a train crash caused 2 "driver" deaths and a flooding 4 (added in r150). In r18402 the counting was merged and the flooding code was taken for counting drivers. Given those numbers were inconsistent (unlike for other vehicles) we better use the real original amount of driver deaths instead of the erroneous amount.
rubidium <rubidium@openttd.org>
parents: 16547
diff changeset
2908 pass += 2; // driver
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2909
12693
3916fe720543 (svn r17152) -Fix: A stuck train could free the reservation of another train if it was reversed or did crash.
michi_cc <michi_cc@openttd.org>
parents: 12692
diff changeset
2910 /* Remove the reserved path in front of the train if it is not stuck.
3916fe720543 (svn r17152) -Fix: A stuck train could free the reservation of another train if it was reversed or did crash.
michi_cc <michi_cc@openttd.org>
parents: 12692
diff changeset
2911 * Also clear all reserved tracks the train is currently on. */
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2912 if (!HasBit(this->flags, VRF_TRAIN_STUCK)) FreeTrainTrackReservation(this);
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2913 for (const Train *v = this; v != NULL; v = v->Next()) {
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2914 ClearPathReservation(v, v->tile, v->GetVehicleTrackdir());
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2915 if (IsTileType(v->tile, MP_TUNNELBRIDGE)) {
9845
ee10319a40b6 (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium <rubidium@openttd.org>
parents: 9840
diff changeset
2916 /* ClearPathReservation will not free the wormhole exit
ee10319a40b6 (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium <rubidium@openttd.org>
parents: 9840
diff changeset
2917 * if the train has just entered the wormhole. */
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2918 SetTunnelBridgeReservation(GetOtherTunnelBridgeEnd(v->tile), false);
9845
ee10319a40b6 (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium <rubidium@openttd.org>
parents: 9840
diff changeset
2919 }
9811
84013100eac2 (svn r13953) -Add [YAPP]: Free track reservations of crashed trains. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9810
diff changeset
2920 }
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2921
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2922 /* we may need to update crossing we were approaching,
17137
0e25103841ef (svn r21875) -Codechange: indentation of some comments was wrong
smatz <smatz@openttd.org>
parents: 17136
diff changeset
2923 * but must be updated after the train has been marked crashed */
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2924 TileIndex crossing = TrainApproachingCrossingTile(this);
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2925 if (crossing != INVALID_TILE) UpdateLevelCrossing(crossing);
14384
7015f43f6a77 (svn r18941) -Fix [FS#3575]: remove the loading indicators as soon as a train crashes
yexo <yexo@openttd.org>
parents: 14382
diff changeset
2926
7015f43f6a77 (svn r18941) -Fix [FS#3575]: remove the loading indicators as soon as a train crashes
yexo <yexo@openttd.org>
parents: 14382
diff changeset
2927 /* Remove the loading indicators (if any) */
7015f43f6a77 (svn r18941) -Fix [FS#3575]: remove the loading indicators as soon as a train crashes
yexo <yexo@openttd.org>
parents: 14382
diff changeset
2928 HideFillingPercent(&this->fill_percent_te_id);
8271
359d451c24ef (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz <smatz@openttd.org>
parents: 8270
diff changeset
2929 }
359d451c24ef (svn r11835) -Fix: mark depot, viewports, vehicle lists and vehicle details dirty when a train crashes
smatz <smatz@openttd.org>
parents: 8270
diff changeset
2930
17141
4ccee6c9442b (svn r21879) -Codechange: reset vehicle's GOINGUP/DOWN bits when it crashes
smatz <smatz@openttd.org>
parents: 17137
diff changeset
2931 pass += this->GroundVehicleBase::Crash(flooded);
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2932
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2933 this->crash_anim_pos = flooded ? 4000 : 1; // max 4440, disappear pretty fast when flooded
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2934 return pass;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2935 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2936
11150
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2937 /**
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2938 * Marks train as crashed and creates an AI event.
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2939 * Doesn't do anything if the train is crashed already.
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2940 * @param v first vehicle of chain
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2941 * @return number of victims (including 2 drivers; zero if train was already crashed)
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2942 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2943 static uint TrainCrashed(Train *v)
11150
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2944 {
12423
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2945 uint num = 0;
12237
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2946
11150
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2947 /* do not crash train twice */
12423
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2948 if (!(v->vehstatus & VS_CRASHED)) {
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2949 num = v->Crash();
18511
f1214da4c397 (svn r23355) -Codechange: rename all AI* to Script* (Rubidium)
truebrain <truebrain@openttd.org>
parents: 18472
diff changeset
2950 AI::NewEvent(v->owner, new ScriptEventVehicleCrashed(v->index, v->tile, ScriptEventVehicleCrashed::CRASH_TRAIN));
18774
1fa19d70aea9 (svn r23622) -Add: a set of events to trigger in a GameScript
truebrain <truebrain@openttd.org>
parents: 18692
diff changeset
2951 Game::NewEvent(new ScriptEventVehicleCrashed(v->index, v->tile, ScriptEventVehicleCrashed::CRASH_TRAIN));
12423
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2952 }
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2953
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2954 /* Try to re-reserve track under already crashed train too.
16796
9d03c51b3f0a (svn r21529) -Doc: update comment after function name change (Krille)
rubidium <rubidium@openttd.org>
parents: 16788
diff changeset
2955 * Crash() clears the reservation! */
12423
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2956 v->ReserveTrackUnderConsist();
11150
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2957
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2958 return num;
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2959 }
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2960
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2961 /** Temporary data storage for testing collisions. */
6966
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
2962 struct TrainCollideChecker {
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2963 Train *v; ///< %Vehicle we are testing for collision.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2964 uint num; ///< Total number of victims if train collided.
6966
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
2965 };
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
2966
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2967 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2968 * Collision test function.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2969 * @param v %Train vehicle to test collision with.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2970 * @param data %Train being examined.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2971 * @return \c NULL (always continue search)
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
2972 */
9775
c3caa51b9888 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium <rubidium@openttd.org>
parents: 9774
diff changeset
2973 static Vehicle *FindTrainCollideEnum(Vehicle *v, void *data)
6966
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
2974 {
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2975 TrainCollideChecker *tcc = (TrainCollideChecker*)data;
6966
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
2976
12237
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2977 /* not a train or in depot */
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2978 if (v->type != VEH_TRAIN || Train::From(v)->track == TRACK_BIT_DEPOT) return NULL;
8281
a3736272d02a (svn r11845) -Codechange: simplify train collision detection a bit
smatz <smatz@openttd.org>
parents: 8271
diff changeset
2979
17063
9f041188fc49 (svn r21800) -Fix [FS#3569]: under certain circumstances one could crash a competitor's train; take the lazy non-future proof version of the fix from the 1.0 branch as fixing the real bug is significantly more complex and might even break some backwards compatability if not done perfectly
rubidium <rubidium@openttd.org>
parents: 16796
diff changeset
2980 /* do not crash into trains of another company. */
9f041188fc49 (svn r21800) -Fix [FS#3569]: under certain circumstances one could crash a competitor's train; take the lazy non-future proof version of the fix from the 1.0 branch as fixing the real bug is significantly more complex and might even break some backwards compatability if not done perfectly
rubidium <rubidium@openttd.org>
parents: 16796
diff changeset
2981 if (v->owner != tcc->v->owner) return NULL;
9f041188fc49 (svn r21800) -Fix [FS#3569]: under certain circumstances one could crash a competitor's train; take the lazy non-future proof version of the fix from the 1.0 branch as fixing the real bug is significantly more complex and might even break some backwards compatability if not done perfectly
rubidium <rubidium@openttd.org>
parents: 16796
diff changeset
2982
8281
a3736272d02a (svn r11845) -Codechange: simplify train collision detection a bit
smatz <smatz@openttd.org>
parents: 8271
diff changeset
2983 /* get first vehicle now to make most usual checks faster */
12237
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2984 Train *coll = Train::From(v)->First();
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2985
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2986 /* can't collide with own wagons */
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2987 if (coll == tcc->v) return NULL;
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2988
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2989 int x_diff = v->x_pos - tcc->v->x_pos;
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2990 int y_diff = v->y_pos - tcc->v->y_pos;
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2991
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2992 /* Do fast calculation to check whether trains are not in close vicinity
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2993 * and quickly reject trains distant enough for any collision.
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2994 * Differences are shifted by 7, mapping range [-7 .. 8] into [0 .. 15]
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2995 * Differences are then ORed and then we check for any higher bits */
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2996 uint hash = (y_diff + 7) | (x_diff + 7);
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2997 if (hash & ~15) return NULL;
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2998
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
2999 /* Slower check using multiplication */
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3000 int min_diff = (Train::From(v)->gcache.cached_veh_length + 1) / 2 + (tcc->v->gcache.cached_veh_length + 1) / 2 - 1;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3001 if (x_diff * x_diff + y_diff * y_diff > min_diff * min_diff) return NULL;
12237
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
3002
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
3003 /* Happens when there is a train under bridge next to bridge head */
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
3004 if (abs(v->z_pos - tcc->v->z_pos) > 5) return NULL;
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
3005
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
3006 /* crash both trains */
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
3007 tcc->num += TrainCrashed(tcc->v);
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
3008 tcc->num += TrainCrashed(coll);
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
3009
52ee820c1243 (svn r16652) -Codechange: use less strict, but faster check for quickly bailing out in FindTrainCollideEnum() (Bilbo)
smatz <smatz@openttd.org>
parents: 12231
diff changeset
3010 return NULL; // continue searching
6966
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
3011 }
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
3012
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
3013 /**
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3014 * Checks whether the specified train has a collision with another vehicle. If
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3015 * so, destroys this vehicle, and the other vehicle if its subtype has TS_Front.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3016 * Reports the incident in a flashy news item, modifies station ratings and
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3017 * plays a sound.
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3018 * @param v %Train to test.
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3019 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3020 static bool CheckTrainCollision(Train *v)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3021 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3022 /* can't collide in depot */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3023 if (v->track == TRACK_BIT_DEPOT) return false;
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3024
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3025 assert(v->track == TRACK_BIT_WORMHOLE || TileVirtXY(v->x_pos, v->y_pos) == v->tile);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3026
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3027 TrainCollideChecker tcc;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3028 tcc.v = v;
6966
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
3029 tcc.num = 0;
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
3030
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
3031 /* find colliding vehicles */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3032 if (v->track == TRACK_BIT_WORMHOLE) {
10083
4923bdc50041 (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium <rubidium@openttd.org>
parents: 9992
diff changeset
3033 FindVehicleOnPos(v->tile, &tcc, FindTrainCollideEnum);
4923bdc50041 (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium <rubidium@openttd.org>
parents: 9992
diff changeset
3034 FindVehicleOnPos(GetOtherTunnelBridgeEnd(v->tile), &tcc, FindTrainCollideEnum);
7371
e03ba4271be4 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138 <peter1138@openttd.org>
parents: 7334
diff changeset
3035 } else {
10083
4923bdc50041 (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium <rubidium@openttd.org>
parents: 9992
diff changeset
3036 FindVehicleOnPosXY(v->x_pos, v->y_pos, &tcc, FindTrainCollideEnum);
7371
e03ba4271be4 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138 <peter1138@openttd.org>
parents: 7334
diff changeset
3037 }
6966
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
3038
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
3039 /* any dead -> no crash */
10282
9f1f7dad1803 (svn r14521) -Fix [FS#2378]: fast trains could continue to move after a crash
glx <glx@openttd.org>
parents: 10261
diff changeset
3040 if (tcc.num == 0) return false;
6966
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
3041
83a9857fde12 (svn r10222) -Fix [FS#892]: Only 2 trains could crash at one time as collision checking stopped on the first hit. This could cause desyncs in network games as the collision hash order is not guaranteed. (patch by B. N. Smatz)
peter1138 <peter1138@openttd.org>
parents: 6953
diff changeset
3042 SetDParam(0, tcc.num);
19383
0c6420024e22 (svn r24284) -Codechange: Remove NewsSubtypes and directly use NewsTypes and NewsFlag instead.
frosch <frosch@openttd.org>
parents: 19381
diff changeset
3043 AddVehicleNewsItem(STR_NEWS_TRAIN_CRASH, NT_ACCIDENT, v->index);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3044
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3045 ModifyStationRatingAround(v->tile, v->owner, -160, 30);
19913
f4e2c852270b (svn r24846) -Add: Advanced settings to disable certain sound effects.
frosch <frosch@openttd.org>
parents: 19906
diff changeset
3046 if (_settings_client.sound.disaster) SndPlayVehicleFx(SND_13_BIG_CRASH, v);
10282
9f1f7dad1803 (svn r14521) -Fix [FS#2378]: fast trains could continue to move after a crash
glx <glx@openttd.org>
parents: 10261
diff changeset
3047 return true;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3048 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3049
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3050 static Vehicle *CheckTrainAtSignal(Vehicle *v, void *data)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3051 {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3052 if (v->type != VEH_TRAIN || (v->vehstatus & VS_CRASHED)) return NULL;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3053
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3054 Train *t = Train::From(v);
9699
128a1a7ade68 (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz <smatz@openttd.org>
parents: 9628
diff changeset
3055 DiagDirection exitdir = *(DiagDirection *)data;
128a1a7ade68 (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz <smatz@openttd.org>
parents: 9628
diff changeset
3056
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3057 /* not front engine of a train, inside wormhole or depot, crashed */
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3058 if (!t->IsFrontEngine() || !(t->track & TRACK_BIT_MASK)) return NULL;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3059
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3060 if (t->cur_speed > 5 || TrainExitDir(t->direction, t->track) != exitdir) return NULL;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3061
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3062 return t;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3063 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3064
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3065 /**
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3066 * Move a vehicle chain one movement stop forwards.
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3067 * @param v First vehicle to move.
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3068 * @param nomove Stop moving this and all following vehicles.
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3069 * @param reverse Set to false to not execute the vehicle reversing. This does not change any other logic.
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3070 * @return True if the vehicle could be moved forward, false otherwise.
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3071 */
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3072 bool TrainController(Train *v, Vehicle *nomove, bool reverse)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3073 {
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3074 Train *first = v->First();
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3075 Train *prev;
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3076 bool direction_changed = false; // has direction of any part changed?
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3077
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3078 /* For every vehicle after and including the given vehicle */
8710
d087f1604050 (svn r12384) -Fix (r2428): do not disconnect train when reversing, it may 'think' it is whole in a depot
smatz <smatz@openttd.org>
parents: 8706
diff changeset
3079 for (prev = v->Previous(); v != nomove; prev = v, v = v->Next()) {
6871
7ef94b059aec (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138 <peter1138@openttd.org>
parents: 6857
diff changeset
3080 DiagDirection enterdir = DIAGDIR_BEGIN;
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3081 bool update_signals_crossing = false; // will we update signals or crossing state?
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3082
6153
42ef9b98d898 (svn r8897) -Fix
tron <tron@openttd.org>
parents: 6152
diff changeset
3083 GetNewVehiclePosResult gp = GetNewVehiclePos(v);
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3084 if (v->track != TRACK_BIT_WORMHOLE) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3085 /* Not inside tunnel */
6152
16eaa24b383f (svn r8896) -Fix
tron <tron@openttd.org>
parents: 6151
diff changeset
3086 if (gp.old_tile == gp.new_tile) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3087 /* Staying in the old tile */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3088 if (v->track == TRACK_BIT_DEPOT) {
5994
f7885ece4c67 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium <rubidium@openttd.org>
parents: 5993
diff changeset
3089 /* Inside depot */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3090 gp.x = v->x_pos;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3091 gp.y = v->y_pos;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3092 } else {
5994
f7885ece4c67 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium <rubidium@openttd.org>
parents: 5993
diff changeset
3093 /* Not inside depot */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3094
10655
66da07b05801 (svn r14957) -Fix [FS#2516]: Do not abort train movement, when we are just about to reach the end of the platform.
frosch <frosch@openttd.org>
parents: 10647
diff changeset
3095 /* Reverse when we are at the end of the track already, do not move to the new position */
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3096 if (v->IsFrontEngine() && !TrainCheckIfLineEnds(v, reverse)) return false;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3097
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3098 uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
7928
4e8dfd103163 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7923
diff changeset
3099 if (HasBit(r, VETS_CANNOT_ENTER)) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3100 goto invalid_rail;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3101 }
7928
4e8dfd103163 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7923
diff changeset
3102 if (HasBit(r, VETS_ENTERED_STATION)) {
10655
66da07b05801 (svn r14957) -Fix [FS#2516]: Do not abort train movement, when we are just about to reach the end of the platform.
frosch <frosch@openttd.org>
parents: 10647
diff changeset
3103 /* The new position is the end of the platform */
5991
1b793d99b5ba (svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium <rubidium@openttd.org>
parents: 5944
diff changeset
3104 TrainEnterStation(v, r >> VETS_STATION_ID_OFFSET);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3105 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3106 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3107 } else {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3108 /* A new tile is about to be entered. */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3109
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3110 /* Determine what direction we're entering the new tile from */
11431
936773e993af (svn r15789) -Codechange: Add DiagdirBetweenTiles() and use it.
frosch <frosch@openttd.org>
parents: 11368
diff changeset
3111 enterdir = DiagdirBetweenTiles(gp.old_tile, gp.new_tile);
5994
f7885ece4c67 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium <rubidium@openttd.org>
parents: 5993
diff changeset
3112 assert(IsValidDiagDirection(enterdir));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3113
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3114 /* Get the status of the tracks in the new tile and mask
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3115 * away the bits that aren't reachable. */
8794
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
3116 TrackStatus ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL, 0, ReverseDiagDir(enterdir));
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
3117 TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(enterdir);
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
3118
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
3119 TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs;
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
3120 TrackBits red_signals = TrackdirBitsToTrackBits(TrackStatusToRedSignals(ts) & reachable_trackdirs);
8616
9e46ac001a8c (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch <frosch@openttd.org>
parents: 8614
diff changeset
3121
9e46ac001a8c (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch <frosch@openttd.org>
parents: 8614
diff changeset
3122 TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
13833
ad09d18a3e34 (svn r18362) -Cleanup: remove OPF for RVs and NTP to clean up lots of code and simplify some things for the future
rubidium <rubidium@openttd.org>
parents: 13832
diff changeset
3123 if (_settings_game.pf.forbid_90_deg && prev == NULL) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3124 /* We allow wagons to make 90 deg turns, because forbid_90_deg
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3125 * can be switched on halfway a turn */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3126 bits &= ~TrackCrossesTracks(FindFirstTrack(v->track));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3127 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3128
5994
f7885ece4c67 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium <rubidium@openttd.org>
parents: 5993
diff changeset
3129 if (bits == TRACK_BIT_NONE) goto invalid_rail;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3130
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
3131 /* Check if the new tile constrains tracks that are compatible
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3132 * with the current train, if not, bail out. */
5994
f7885ece4c67 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium <rubidium@openttd.org>
parents: 5993
diff changeset
3133 if (!CheckCompatibleRail(v, gp.new_tile)) goto invalid_rail;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3134
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3135 TrackBits chosen_track;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3136 if (prev == NULL) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3137 /* Currently the locomotive is active. Determine which one of the
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3138 * available tracks to choose */
9810
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
3139 chosen_track = TrackToTrackBits(ChooseTrainTrack(v, gp.new_tile, enterdir, bits, false, NULL, true));
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
3140 assert(chosen_track & (bits | GetReservedTrackbits(gp.new_tile)));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3141
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
3142 if (v->force_proceed != TFP_NONE && IsPlainRailTile(gp.new_tile) && HasSignals(gp.new_tile)) {
13977
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3143 /* For each signal we find decrease the counter by one.
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3144 * We start at two, so the first signal we pass decreases
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3145 * this to one, then if we reach the next signal it is
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3146 * decreased to zero and we won't pass that new signal. */
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3147 Trackdir dir = FindFirstTrackdir(trackdirbits);
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3148 if (GetSignalType(gp.new_tile, TrackdirToTrack(dir)) != SIGTYPE_PBS ||
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3149 !HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(dir))) {
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3150 /* However, we do not want to be stopped by PBS signals
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3151 * entered via the back. */
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
3152 v->force_proceed = (v->force_proceed == TFP_SIGNAL) ? TFP_STUCK : TFP_NONE;
13977
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3153 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3154 }
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3155 }
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3156
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3157 /* Check if it's a red signal and that force proceed is not clicked. */
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
3158 if ((red_signals & chosen_track) && v->force_proceed == TFP_NONE) {
8616
9e46ac001a8c (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch <frosch@openttd.org>
parents: 8614
diff changeset
3159 /* In front of a red signal */
9e46ac001a8c (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch <frosch@openttd.org>
parents: 8614
diff changeset
3160 Trackdir i = FindFirstTrackdir(trackdirbits);
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3161
9808
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
3162 /* Don't handle stuck trains here. */
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3163 if (HasBit(v->flags, VRF_TRAIN_STUCK)) return false;
9808
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
3164
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3165 if (!HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(i))) {
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3166 v->cur_speed = 0;
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3167 v->subspeed = 0;
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3168 v->progress = 255 - 100;
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3169 if (!_settings_game.pf.reverse_at_signals || ++v->wait_counter < _settings_game.pf.wait_oneway_signal * 20) return false;
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3170 } else if (HasSignalOnTrackdir(gp.new_tile, i)) {
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3171 v->cur_speed = 0;
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3172 v->subspeed = 0;
6491
696f0e1b046e (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium <rubidium@openttd.org>
parents: 6490
diff changeset
3173 v->progress = 255 - 10;
17223
a1dda316ecbe (svn r21962) -Feature-ish: GUI setting to disable reversing at signals
rubidium <rubidium@openttd.org>
parents: 17221
diff changeset
3174 if (!_settings_game.pf.reverse_at_signals || ++v->wait_counter < _settings_game.pf.wait_twoway_signal * 73) {
9699
128a1a7ade68 (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz <smatz@openttd.org>
parents: 9628
diff changeset
3175 DiagDirection exitdir = TrackdirToExitdir(i);
128a1a7ade68 (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz <smatz@openttd.org>
parents: 9628
diff changeset
3176 TileIndex o_tile = TileAddByDiagDir(gp.new_tile, exitdir);
128a1a7ade68 (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz <smatz@openttd.org>
parents: 9628
diff changeset
3177
128a1a7ade68 (svn r13806) -Fix [FS#2162]: checking for train waiting at other side of two-way signal was broken
smatz <smatz@openttd.org>
parents: 9628
diff changeset
3178 exitdir = ReverseDiagDir(exitdir);
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3179
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3180 /* check if a train is waiting on the other side */
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3181 if (!HasVehicleOnPos(o_tile, &exitdir, &CheckTrainAtSignal)) return false;
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3182 }
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3183 }
9820
7ad7c8e6ca13 (svn r13962) -Codechange [YAPP]: Do not reverse in front of red signals when inside a PBS block and reversing of stuck trains is disabled. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9819
diff changeset
3184
7ad7c8e6ca13 (svn r13962) -Codechange [YAPP]: Do not reverse in front of red signals when inside a PBS block and reversing of stuck trains is disabled. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9819
diff changeset
3185 /* If we would reverse but are currently in a PBS block and
14702
66e6b13805cb (svn r19286) -Fix (r18648): [YAPP] If reversing at path signals was disabled, a train would not reverse when hitting the back of an one-way signal.
michi_cc <michi_cc@openttd.org>
parents: 14680
diff changeset
3186 * reversing of stuck trains is disabled, don't reverse.
66e6b13805cb (svn r19286) -Fix (r18648): [YAPP] If reversing at path signals was disabled, a train would not reverse when hitting the back of an one-way signal.
michi_cc <michi_cc@openttd.org>
parents: 14680
diff changeset
3187 * This does not apply if the reason for reversing is a one-way
66e6b13805cb (svn r19286) -Fix (r18648): [YAPP] If reversing at path signals was disabled, a train would not reverse when hitting the back of an one-way signal.
michi_cc <michi_cc@openttd.org>
parents: 14680
diff changeset
3188 * signal blocking us, because a train would then be stuck forever. */
17223
a1dda316ecbe (svn r21962) -Feature-ish: GUI setting to disable reversing at signals
rubidium <rubidium@openttd.org>
parents: 17221
diff changeset
3189 if (!_settings_game.pf.reverse_at_signals && !HasOnewaySignalBlockingTrackdir(gp.new_tile, i) &&
14702
66e6b13805cb (svn r19286) -Fix (r18648): [YAPP] If reversing at path signals was disabled, a train would not reverse when hitting the back of an one-way signal.
michi_cc <michi_cc@openttd.org>
parents: 14680
diff changeset
3190 UpdateSignalsOnSegment(v->tile, enterdir, v->owner) == SIGSEG_PBS) {
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
3191 v->wait_counter = 0;
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3192 return false;
9820
7ad7c8e6ca13 (svn r13962) -Codechange [YAPP]: Do not reverse in front of red signals when inside a PBS block and reversing of stuck trains is disabled. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9819
diff changeset
3193 }
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3194 goto reverse_train_direction;
9888
b849464ebe27 (svn r14036) -Fix [FS#2197,FS#2198]: trains crashing into eachother when signals are changed (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9874
diff changeset
3195 } else {
19973
f8c0e108128c (svn r24905) -Feature(ish): Implement station randomisation triggers.
peter1138 <peter1138@openttd.org>
parents: 19968
diff changeset
3196 TryReserveRailTrack(gp.new_tile, TrackBitsToTrack(chosen_track), false);
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3197 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3198 } else {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3199 /* The wagon is active, simply follow the prev vehicle. */
11434
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3200 if (prev->tile == gp.new_tile) {
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3201 /* Choose the same track as prev */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3202 if (prev->track == TRACK_BIT_WORMHOLE) {
11434
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3203 /* Vehicles entering tunnels enter the wormhole earlier than for bridges.
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3204 * However, just choose the track into the wormhole. */
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3205 assert(IsTunnel(prev->tile));
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3206 chosen_track = bits;
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3207 } else {
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3208 chosen_track = prev->track;
11434
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3209 }
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3210 } else {
11690
eb5d9897c030 (svn r16075) -Fix (r15792)[FS#2844]: Mixed up TRACK_BIT_X/Y.
frosch <frosch@openttd.org>
parents: 11677
diff changeset
3211 /* Choose the track that leads to the tile where prev is.
eb5d9897c030 (svn r16075) -Fix (r15792)[FS#2844]: Mixed up TRACK_BIT_X/Y.
frosch <frosch@openttd.org>
parents: 11677
diff changeset
3212 * This case is active if 'prev' is already on the second next tile, when 'v' just enters the next tile.
eb5d9897c030 (svn r16075) -Fix (r15792)[FS#2844]: Mixed up TRACK_BIT_X/Y.
frosch <frosch@openttd.org>
parents: 11677
diff changeset
3213 * I.e. when the tile between them has only space for a single vehicle like
eb5d9897c030 (svn r16075) -Fix (r15792)[FS#2844]: Mixed up TRACK_BIT_X/Y.
frosch <frosch@openttd.org>
parents: 11677
diff changeset
3214 * 1) horizontal/vertical track tiles and
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
3215 * 2) some orientations of tunnel entries, where the vehicle is already inside the wormhole at 8/16 from the tile edge.
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
3216 * Is also the train just reversing, the wagon inside the tunnel is 'on' the tile of the opposite tunnel entry.
11690
eb5d9897c030 (svn r16075) -Fix (r15792)[FS#2844]: Mixed up TRACK_BIT_X/Y.
frosch <frosch@openttd.org>
parents: 11677
diff changeset
3217 */
11434
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3218 static const TrackBits _connecting_track[DIAGDIR_END][DIAGDIR_END] = {
11690
eb5d9897c030 (svn r16075) -Fix (r15792)[FS#2844]: Mixed up TRACK_BIT_X/Y.
frosch <frosch@openttd.org>
parents: 11677
diff changeset
3219 {TRACK_BIT_X, TRACK_BIT_LOWER, TRACK_BIT_NONE, TRACK_BIT_LEFT },
eb5d9897c030 (svn r16075) -Fix (r15792)[FS#2844]: Mixed up TRACK_BIT_X/Y.
frosch <frosch@openttd.org>
parents: 11677
diff changeset
3220 {TRACK_BIT_UPPER, TRACK_BIT_Y, TRACK_BIT_LEFT, TRACK_BIT_NONE },
eb5d9897c030 (svn r16075) -Fix (r15792)[FS#2844]: Mixed up TRACK_BIT_X/Y.
frosch <frosch@openttd.org>
parents: 11677
diff changeset
3221 {TRACK_BIT_NONE, TRACK_BIT_RIGHT, TRACK_BIT_X, TRACK_BIT_UPPER},
eb5d9897c030 (svn r16075) -Fix (r15792)[FS#2844]: Mixed up TRACK_BIT_X/Y.
frosch <frosch@openttd.org>
parents: 11677
diff changeset
3222 {TRACK_BIT_RIGHT, TRACK_BIT_NONE, TRACK_BIT_LOWER, TRACK_BIT_Y }
11434
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3223 };
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3224 DiagDirection exitdir = DiagdirBetweenTiles(gp.new_tile, prev->tile);
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3225 assert(IsValidDiagDirection(exitdir));
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3226 chosen_track = _connecting_track[enterdir][exitdir];
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3227 }
2535211ea182 (svn r15792) -Codechange: Use a different algorithm for train vehicles to follow the vehicle in front of them.
frosch <frosch@openttd.org>
parents: 11431
diff changeset
3228 chosen_track &= bits;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3229 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3230
5994
f7885ece4c67 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium <rubidium@openttd.org>
parents: 5993
diff changeset
3231 /* Make sure chosen track is a valid track */
f7885ece4c67 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium <rubidium@openttd.org>
parents: 5993
diff changeset
3232 assert(
f7885ece4c67 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium <rubidium@openttd.org>
parents: 5993
diff changeset
3233 chosen_track == TRACK_BIT_X || chosen_track == TRACK_BIT_Y ||
f7885ece4c67 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium <rubidium@openttd.org>
parents: 5993
diff changeset
3234 chosen_track == TRACK_BIT_UPPER || chosen_track == TRACK_BIT_LOWER ||
f7885ece4c67 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium <rubidium@openttd.org>
parents: 5993
diff changeset
3235 chosen_track == TRACK_BIT_LEFT || chosen_track == TRACK_BIT_RIGHT);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3236
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3237 /* Update XY to reflect the entrance to the new tile, and select the direction to use */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3238 const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir];
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3239 gp.x = (gp.x & ~0xF) | b[0];
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3240 gp.y = (gp.y & ~0xF) | b[1];
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3241 Direction chosen_dir = (Direction)b[2];
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3242
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3243 /* Call the landscape function and tell it that the vehicle entered the tile */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3244 uint32 r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
7928
4e8dfd103163 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7923
diff changeset
3245 if (HasBit(r, VETS_CANNOT_ENTER)) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3246 goto invalid_rail;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3247 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3248
7928
4e8dfd103163 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7923
diff changeset
3249 if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
9809
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
3250 Track track = FindFirstTrack(chosen_track);
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
3251 Trackdir tdir = TrackDirectionToTrackdir(track, chosen_dir);
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3252 if (v->IsFrontEngine() && HasPbsSignalOnTrackdir(gp.new_tile, tdir)) {
9809
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
3253 SetSignalStateByTrackdir(gp.new_tile, tdir, SIGNAL_STATE_RED);
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
3254 MarkTileDirtyByTile(gp.new_tile);
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
3255 }
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
3256
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
3257 /* Clear any track reservation when the last vehicle leaves the tile */
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3258 if (v->Next() == NULL) ClearPathReservation(v, v->tile, v->GetVehicleTrackdir());
9809
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
3259
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3260 v->tile = gp.new_tile;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3261
6154
238db87ae59e (svn r8899) -Fix
tron <tron@openttd.org>
parents: 6153
diff changeset
3262 if (GetTileRailType(gp.new_tile) != GetTileRailType(gp.old_tile)) {
18923
429c5d53bda8 (svn r23773) -Change: [NewGRF] Update all cached train properties if a train vehicle enters a new railtype.
michi_cc <michi_cc@openttd.org>
parents: 18895
diff changeset
3263 v->First()->ConsistChanged(true);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3264 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3265
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3266 v->track = chosen_track;
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3267 assert(v->track);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3268 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3269
6871
7ef94b059aec (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138 <peter1138@openttd.org>
parents: 6857
diff changeset
3270 /* We need to update signal status, but after the vehicle position hash
14754
691e61140880 (svn r19342) -Codechange: Move inclination update functions to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14753
diff changeset
3271 * has been updated by UpdateInclination() */
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3272 update_signals_crossing = true;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3273
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3274 if (chosen_dir != v->direction) {
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
3275 if (prev == NULL && _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) {
18999
081eb943326c (svn r23855) -Fix [FS#5007]: out of bounds read for slowdown parameter caused desync when railtype >= 4, vehicles were fast, and the original acceleration model was used
rubidium <rubidium@openttd.org>
parents: 18931
diff changeset
3276 const AccelerationSlowdownParams *asp = &_accel_slowdown[GetRailTypeInfo(v->railtype)->acceleration_type];
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3277 DirDiff diff = DirDifference(v->direction, chosen_dir);
18999
081eb943326c (svn r23855) -Fix [FS#5007]: out of bounds read for slowdown parameter caused desync when railtype >= 4, vehicles were fast, and the original acceleration model was used
rubidium <rubidium@openttd.org>
parents: 18931
diff changeset
3278 v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? asp->small_turn : asp->large_turn) * v->cur_speed >> 8;
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3279 }
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3280 direction_changed = true;
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3281 v->direction = chosen_dir;
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3282 }
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3283
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3284 if (v->IsFrontEngine()) {
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
3285 v->wait_counter = 0;
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3286
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
3287 /* If we are approaching a crossing that is reserved, play the sound now. */
9824
fe3562a51648 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9822
diff changeset
3288 TileIndex crossing = TrainApproachingCrossingTile(v);
20258
b961719a03d6 (svn r25200) -Fix (r24846): The level crossing sound is an ambient sound and not a new year sound.
michi_cc <michi_cc@openttd.org>
parents: 20212
diff changeset
3289 if (crossing != INVALID_TILE && HasCrossingReservation(crossing) && _settings_client.sound.ambient) SndPlayTileFx(SND_0E_LEVEL_CROSSING, crossing);
9824
fe3562a51648 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9822
diff changeset
3290
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3291 /* Always try to extend the reservation when entering a tile. */
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3292 CheckNextTrainTile(v);
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3293 }
11657
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
3294
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
3295 if (HasBit(r, VETS_ENTERED_STATION)) {
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
3296 /* The new position is the location where we want to stop */
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
3297 TrainEnterStation(v, r >> VETS_STATION_ID_OFFSET);
ff1084d0d8a0 (svn r16037) -Feature: allow (per order) to let a train stop at the near end, middle or far end of a platform from the point of view of the driver of the train that enters the station.
rubidium <rubidium@openttd.org>
parents: 11435
diff changeset
3298 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3299 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3300 } else {
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3301 if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3302 /* Perform look-ahead on tunnel exit. */
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3303 if (v->IsFrontEngine()) {
9888
b849464ebe27 (svn r14036) -Fix [FS#2197,FS#2198]: trains crashing into eachother when signals are changed (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9874
diff changeset
3304 TryReserveRailTrack(gp.new_tile, DiagDirToDiagTrack(GetTunnelBridgeDirection(gp.new_tile)));
b849464ebe27 (svn r14036) -Fix [FS#2197,FS#2198]: trains crashing into eachother when signals are changed (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9874
diff changeset
3305 CheckNextTrainTile(v);
b849464ebe27 (svn r14036) -Fix [FS#2197,FS#2198]: trains crashing into eachother when signals are changed (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9874
diff changeset
3306 }
17099
b23e23e3904a (svn r21836) -Fix: when a train was reversed while inside a tunnel/bridge, it wouldn't have (re)set the GOINGUP/DOWN bits after leaving the tunnel/bridge
smatz <smatz@openttd.org>
parents: 17098
diff changeset
3307 /* Prevent v->UpdateInclination() being called with wrong parameters.
b23e23e3904a (svn r21836) -Fix: when a train was reversed while inside a tunnel/bridge, it wouldn't have (re)set the GOINGUP/DOWN bits after leaving the tunnel/bridge
smatz <smatz@openttd.org>
parents: 17098
diff changeset
3308 * This could happen if the train was reversed inside the tunnel/bridge. */
b23e23e3904a (svn r21836) -Fix: when a train was reversed while inside a tunnel/bridge, it wouldn't have (re)set the GOINGUP/DOWN bits after leaving the tunnel/bridge
smatz <smatz@openttd.org>
parents: 17098
diff changeset
3309 if (gp.old_tile == gp.new_tile) {
b23e23e3904a (svn r21836) -Fix: when a train was reversed while inside a tunnel/bridge, it wouldn't have (re)set the GOINGUP/DOWN bits after leaving the tunnel/bridge
smatz <smatz@openttd.org>
parents: 17098
diff changeset
3310 gp.old_tile = GetOtherTunnelBridgeEnd(gp.old_tile);
b23e23e3904a (svn r21836) -Fix: when a train was reversed while inside a tunnel/bridge, it wouldn't have (re)set the GOINGUP/DOWN bits after leaving the tunnel/bridge
smatz <smatz@openttd.org>
parents: 17098
diff changeset
3311 }
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3312 } else {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3313 v->x_pos = gp.x;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3314 v->y_pos = gp.y;
18864
c8717ac828d0 (svn r23713) -Codechange: split VehicleMove() into two functions, to minimalize the work done where possible
truebrain <truebrain@openttd.org>
parents: 18855
diff changeset
3315 VehicleUpdatePosition(v);
c8717ac828d0 (svn r23713) -Codechange: split VehicleMove() into two functions, to minimalize the work done where possible
truebrain <truebrain@openttd.org>
parents: 18855
diff changeset
3316 if ((v->vehstatus & VS_HIDDEN) == 0) VehicleUpdateViewport(v, true);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3317 continue;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3318 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3319 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3320
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3321 /* update image of train, as well as delta XY */
8143
1c7033494d92 (svn r11705) -Fix [FS#1557]: trains could have sprites with wrong direction when reversing, also was inconsistent with save/load process (possible desyncs)
smatz <smatz@openttd.org>
parents: 8140
diff changeset
3322 v->UpdateDeltaXY(v->direction);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3323
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3324 v->x_pos = gp.x;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3325 v->y_pos = gp.y;
18865
ab93092e1cb2 (svn r23714) -Codechange: don't mix Viewport with non-viewport code
truebrain <truebrain@openttd.org>
parents: 18864
diff changeset
3326 VehicleUpdatePosition(v);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3327
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3328 /* update the Z position of the vehicle */
18272
3ebab55c5495 (svn r23108) -Codechange: more uint -> int / byte -> int conversions for Z related variables
rubidium <rubidium@openttd.org>
parents: 18255
diff changeset
3329 int old_z = v->UpdateInclination(gp.new_tile != gp.old_tile, false);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3330
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3331 if (prev == NULL) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3332 /* This is the first vehicle in the train */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3333 AffectSpeedByZChange(v, old_z);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3334 }
6871
7ef94b059aec (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138 <peter1138@openttd.org>
parents: 6857
diff changeset
3335
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3336 if (update_signals_crossing) {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3337 if (v->IsFrontEngine()) {
9821
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3338 if (TrainMovedChangeSignals(gp.new_tile, enterdir)) {
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3339 /* We are entering a block with PBS signals right now, but
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3340 * not through a PBS signal. This means we don't have a
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3341 * reservation right now. As a conventional signal will only
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3342 * ever be green if no other train is in the block, getting
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3343 * a path should always be possible. If the player built
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3344 * such a strange network that it is not possible, the train
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3345 * will be marked as stuck and the player has to deal with
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3346 * the problem. */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3347 if ((!HasReservedTracks(gp.new_tile, v->track) &&
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3348 !TryReserveRailTrack(gp.new_tile, FindFirstTrack(v->track))) ||
9821
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3349 !TryPathReserve(v)) {
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3350 MarkTrainAsStuck(v);
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3351 }
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3352 }
2b151a3dc570 (svn r13963) -Codechange [YAPP]: Reserve a track when entering a PBS block through a conventional signal. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9820
diff changeset
3353 }
6871
7ef94b059aec (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138 <peter1138@openttd.org>
parents: 6857
diff changeset
3354
7ef94b059aec (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138 <peter1138@openttd.org>
parents: 6857
diff changeset
3355 /* Signals can only change when the first
7ef94b059aec (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138 <peter1138@openttd.org>
parents: 6857
diff changeset
3356 * (above) or the last vehicle moves. */
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3357 if (v->Next() == NULL) {
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3358 TrainMovedChangeSignals(gp.old_tile, ReverseDiagDir(enterdir));
8342
441cda3cfbdb (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz <smatz@openttd.org>
parents: 8334
diff changeset
3359 if (IsLevelCrossingTile(gp.old_tile)) UpdateLevelCrossing(gp.old_tile);
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3360 }
6871
7ef94b059aec (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138 <peter1138@openttd.org>
parents: 6857
diff changeset
3361 }
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3362
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3363 /* Do not check on every tick to save some computing time. */
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3364 if (v->IsFrontEngine() && v->tick_counter % _settings_game.pf.path_backoff_interval == 0) CheckNextTrainTile(v);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3365 }
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3366
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
3367 if (direction_changed) first->tcache.cached_max_curve_speed = first->GetCurveSpeedLimit();
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3368
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3369 return true;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3370
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3371 invalid_rail:
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3372 /* We've reached end of line?? */
9470
47924a4b2ad4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx <glx@openttd.org>
parents: 9413
diff changeset
3373 if (prev != NULL) error("Disconnecting train");
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3374
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3375 reverse_train_direction:
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3376 if (reverse) {
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3377 v->wait_counter = 0;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3378 v->cur_speed = 0;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3379 v->subspeed = 0;
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3380 ReverseTrainDirection(v);
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3381 }
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3382
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3383 return false;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3384 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3385
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
3386 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
3387 * Collect trackbits of all crashed train vehicles on a tile
10083
4923bdc50041 (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium <rubidium@openttd.org>
parents: 9992
diff changeset
3388 * @param v Vehicle passed from Find/HasVehicleOnPos()
9840
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3389 * @param data trackdirbits for the result
10083
4923bdc50041 (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium <rubidium@openttd.org>
parents: 9992
diff changeset
3390 * @return NULL to iterate over all vehicles on the tile.
9840
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3391 */
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3392 static Vehicle *CollectTrackbitsFromCrashedVehiclesEnum(Vehicle *v, void *data)
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3393 {
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3394 TrackBits *trackbits = (TrackBits *)data;
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3395
9845
ee10319a40b6 (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium <rubidium@openttd.org>
parents: 9840
diff changeset
3396 if (v->type == VEH_TRAIN && (v->vehstatus & VS_CRASHED) != 0) {
15257
25f7ec98dc69 (svn r19892) -Fix [FS#3856] (r19792): crash when trying to reserve 'in depot' track on clearing a crashed train
rubidium <rubidium@openttd.org>
parents: 15163
diff changeset
3397 TrackBits train_tbits = Train::From(v)->track;
25f7ec98dc69 (svn r19892) -Fix [FS#3856] (r19792): crash when trying to reserve 'in depot' track on clearing a crashed train
rubidium <rubidium@openttd.org>
parents: 15163
diff changeset
3398 if (train_tbits == TRACK_BIT_WORMHOLE) {
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3399 /* Vehicle is inside a wormhole, v->track contains no useful value then. */
9845
ee10319a40b6 (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium <rubidium@openttd.org>
parents: 9840
diff changeset
3400 *trackbits |= DiagDirToDiagTrackBits(GetTunnelBridgeDirection(v->tile));
15257
25f7ec98dc69 (svn r19892) -Fix [FS#3856] (r19792): crash when trying to reserve 'in depot' track on clearing a crashed train
rubidium <rubidium@openttd.org>
parents: 15163
diff changeset
3401 } else if (train_tbits != TRACK_BIT_DEPOT) {
25f7ec98dc69 (svn r19892) -Fix [FS#3856] (r19792): crash when trying to reserve 'in depot' track on clearing a crashed train
rubidium <rubidium@openttd.org>
parents: 15163
diff changeset
3402 *trackbits |= train_tbits;
9845
ee10319a40b6 (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium <rubidium@openttd.org>
parents: 9840
diff changeset
3403 }
ee10319a40b6 (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium <rubidium@openttd.org>
parents: 9840
diff changeset
3404 }
9840
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3405
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3406 return NULL;
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3407 }
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3408
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3409 /**
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3410 * Deletes/Clears the last wagon of a crashed train. It takes the engine of the
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3411 * train, then goes to the last wagon and deletes that. Each call to this function
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3412 * will remove the last wagon of a crashed train. If this wagon was on a crossing,
8073
ab953ea59f2e (svn r11634) -Fix: update signals when deleting crashed train on a bridge, update even when train is rotated
smatz <smatz@openttd.org>
parents: 8072
diff changeset
3413 * or inside a tunnel/bridge, recalculate the signals as they might need updating
6484
17fdaec13730 (svn r9665) -Documentation: Doxygen corrections,errors, corrections of corrections...
belugas <belugas@openttd.org>
parents: 6453
diff changeset
3414 * @param v the Vehicle of which last wagon is to be removed
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3415 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3416 static void DeleteLastWagon(Train *v)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3417 {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3418 Train *first = v->First();
8267
e377a682e7a4 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz <smatz@openttd.org>
parents: 8264
diff changeset
3419
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3420 /* Go to the last wagon and delete the link pointing there
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3421 * *u is then the one-before-last wagon, and *v the last
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
3422 * one which will physically be removed */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3423 Train *u = v;
7492
09743324277c (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium <rubidium@openttd.org>
parents: 7488
diff changeset
3424 for (; v->Next() != NULL; v = v->Next()) u = v;
09743324277c (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium <rubidium@openttd.org>
parents: 7488
diff changeset
3425 u->SetNext(NULL);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3426
10493
5531e81401ee (svn r14748) -Codechange: unduplicate window updating when deleting vehicles.
rubidium <rubidium@openttd.org>
parents: 10492
diff changeset
3427 if (first != v) {
8267
e377a682e7a4 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz <smatz@openttd.org>
parents: 8264
diff changeset
3428 /* Recalculate cached train properties */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
3429 first->ConsistChanged(false);
8267
e377a682e7a4 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz <smatz@openttd.org>
parents: 8264
diff changeset
3430 /* Update the depot window if the first vehicle is in depot -
e377a682e7a4 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz <smatz@openttd.org>
parents: 8264
diff changeset
3431 * if v == first, then it is updated in PreDestructor() */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3432 if (first->track == TRACK_BIT_DEPOT) {
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
3433 SetWindowDirty(WC_VEHICLE_DEPOT, first->tile);
8267
e377a682e7a4 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz <smatz@openttd.org>
parents: 8264
diff changeset
3434 }
20364
cecc29b45313 (svn r25319) -Fix [FS#5570]: crash when train with reserved cargo vanishes
rubidium <rubidium@openttd.org>
parents: 20307
diff changeset
3435 v->last_station_visited = first->last_station_visited; // for PreDestructor
8267
e377a682e7a4 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz <smatz@openttd.org>
parents: 8264
diff changeset
3436 }
e377a682e7a4 (svn r11831) -Fix: call TrainConsistChanged() when removing crashed train part to be consistent with saveload process
smatz <smatz@openttd.org>
parents: 8264
diff changeset
3437
8255
dc16e054fd8f (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz <smatz@openttd.org>
parents: 8254
diff changeset
3438 /* 'v' shouldn't be accessed after it has been deleted */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3439 TrackBits trackbits = v->track;
8255
dc16e054fd8f (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz <smatz@openttd.org>
parents: 8254
diff changeset
3440 TileIndex tile = v->tile;
8300
ae1baac75172 (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz <smatz@openttd.org>
parents: 8283
diff changeset
3441 Owner owner = v->owner;
8255
dc16e054fd8f (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz <smatz@openttd.org>
parents: 8254
diff changeset
3442
7398
6324d82f421d (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium <rubidium@openttd.org>
parents: 7371
diff changeset
3443 delete v;
9840
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3444 v = NULL; // make sure nobody will try to read 'v' anymore
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3445
12137
b3c5a6c9146a (svn r16553) -Codechange: don't use TRACK_BIT_WORMHOLE and TRACK_BIT_DEPOT as bitmasks
smatz <smatz@openttd.org>
parents: 12129
diff changeset
3446 if (trackbits == TRACK_BIT_WORMHOLE) {
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3447 /* Vehicle is inside a wormhole, v->track contains no useful value then. */
12137
b3c5a6c9146a (svn r16553) -Codechange: don't use TRACK_BIT_WORMHOLE and TRACK_BIT_DEPOT as bitmasks
smatz <smatz@openttd.org>
parents: 12129
diff changeset
3448 trackbits = DiagDirToDiagTrackBits(GetTunnelBridgeDirection(tile));
9845
ee10319a40b6 (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium <rubidium@openttd.org>
parents: 9840
diff changeset
3449 }
ee10319a40b6 (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium <rubidium@openttd.org>
parents: 9840
diff changeset
3450
ee10319a40b6 (svn r13989) -Fix [FS#2186] (r13953): YAPP track reservation for crashed trains was broken for depots and wormholes (michi_cc).
rubidium <rubidium@openttd.org>
parents: 9840
diff changeset
3451 Track track = TrackBitsToTrack(trackbits);
9840
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3452 if (HasReservedTracks(tile, trackbits)) {
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3453 UnreserveRailTrack(tile, track);
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3454
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3455 /* If there are still crashed vehicles on the tile, give the track reservation to them */
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3456 TrackBits remaining_trackbits = TRACK_BIT_NONE;
10083
4923bdc50041 (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium <rubidium@openttd.org>
parents: 9992
diff changeset
3457 FindVehicleOnPos(tile, &remaining_trackbits, CollectTrackbitsFromCrashedVehiclesEnum);
9840
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3458
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3459 /* It is important that these two are the first in the loop, as reservation cannot deal with every trackbit combination */
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3460 assert(TRACK_BEGIN == TRACK_X && TRACK_Y == TRACK_BEGIN + 1);
15163
5fa5a4d1f409 (svn r19792) -Change: use the typed FOR_EACH_SET_BIT for Tracks (adf88)
rubidium <rubidium@openttd.org>
parents: 15156
diff changeset
3461 Track t;
5fa5a4d1f409 (svn r19792) -Change: use the typed FOR_EACH_SET_BIT for Tracks (adf88)
rubidium <rubidium@openttd.org>
parents: 15156
diff changeset
3462 FOR_EACH_SET_TRACK(t, remaining_trackbits) TryReserveRailTrack(tile, t);
9840
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3463 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3464
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3465 /* check if the wagon was on a road/rail-crossing */
8342
441cda3cfbdb (svn r11908) -Fix: update crossing when merging a company, when building a crossing and after loading older savegame
smatz <smatz@openttd.org>
parents: 8334
diff changeset
3466 if (IsLevelCrossingTile(tile)) UpdateLevelCrossing(tile);
8255
dc16e054fd8f (svn r11819) -Fix: do not access the Vehicle struct that has been already deleted when removing crashed train
smatz <smatz@openttd.org>
parents: 8254
diff changeset
3467
8257
38d56fa7b6f7 (svn r11821) -Fix (r11802): 'optimization assert' when removing crashed wagon in some cases
smatz <smatz@openttd.org>
parents: 8256
diff changeset
3468 /* Update signals */
8961
b33bcd7fa037 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz <smatz@openttd.org>
parents: 8954
diff changeset
3469 if (IsTileType(tile, MP_TUNNELBRIDGE) || IsRailDepotTile(tile)) {
8300
ae1baac75172 (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz <smatz@openttd.org>
parents: 8283
diff changeset
3470 UpdateSignalsOnSegment(tile, INVALID_DIAGDIR, owner);
8257
38d56fa7b6f7 (svn r11821) -Fix (r11802): 'optimization assert' when removing crashed wagon in some cases
smatz <smatz@openttd.org>
parents: 8256
diff changeset
3471 } else {
9840
b2082b3c8224 (svn r13983) -Fix (r13953) [YAPP]: Crashed trains can be on different trackbits. Make sure there is still a track reservation when one vehicle is cleaned up.
frosch <frosch@openttd.org>
parents: 9833
diff changeset
3472 SetSignalsOnBothDir(tile, track, owner);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3473 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3474 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3475
17236
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3476 /**
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3477 * Rotate all vehicles of a (crashed) train chain randomly to animate the crash.
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3478 * @param v First crashed vehicle.
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3479 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3480 static void ChangeTrainDirRandomly(Train *v)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3481 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3482 static const DirDiff delta[] = {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3483 DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3484 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3485
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3486 do {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3487 /* We don't need to twist around vehicles if they're not visible */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3488 if (!(v->vehstatus & VS_HIDDEN)) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3489 v->direction = ChangeDir(v->direction, delta[GB(Random(), 0, 2)]);
6558
2fc024ee8217 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium <rubidium@openttd.org>
parents: 6554
diff changeset
3490 v->UpdateDeltaXY(v->direction);
18244
8f6022f41538 (svn r23080) -Feature: [NewGRF] Use variable 10 to enable vehicle GRFs to draw different sprites on the map and in various GUIs.
frosch <frosch@openttd.org>
parents: 18239
diff changeset
3491 v->cur_image = v->GetImage(v->direction, EIT_ON_MAP);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3492 /* Refrain from updating the z position of the vehicle when on
14754
691e61140880 (svn r19342) -Codechange: Move inclination update functions to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14753
diff changeset
3493 * a bridge, because UpdateInclination() will put the vehicle under
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
3494 * the bridge in that case */
18865
ab93092e1cb2 (svn r23714) -Codechange: don't mix Viewport with non-viewport code
truebrain <truebrain@openttd.org>
parents: 18864
diff changeset
3495 if (v->track != TRACK_BIT_WORMHOLE) {
ab93092e1cb2 (svn r23714) -Codechange: don't mix Viewport with non-viewport code
truebrain <truebrain@openttd.org>
parents: 18864
diff changeset
3496 VehicleUpdatePosition(v);
ab93092e1cb2 (svn r23714) -Codechange: don't mix Viewport with non-viewport code
truebrain <truebrain@openttd.org>
parents: 18864
diff changeset
3497 v->UpdateInclination(false, false);
ab93092e1cb2 (svn r23714) -Codechange: don't mix Viewport with non-viewport code
truebrain <truebrain@openttd.org>
parents: 18864
diff changeset
3498 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3499 }
7492
09743324277c (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium <rubidium@openttd.org>
parents: 7488
diff changeset
3500 } while ((v = v->Next()) != NULL);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3501 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3502
17236
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3503 /**
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3504 * Handle a crashed train.
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3505 * @param v First train vehicle.
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3506 * @return %Vehicle chain still exists.
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3507 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3508 static bool HandleCrashedTrain(Train *v)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3509 {
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3510 int state = ++v->crash_anim_pos;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3511
5993
2498be7c7e94 (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium <rubidium@openttd.org>
parents: 5992
diff changeset
3512 if (state == 4 && !(v->vehstatus & VS_HIDDEN)) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3513 CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3514 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3515
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3516 uint32 r;
7967
e30c5ec2d272 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13 <skidd13@openttd.org>
parents: 7932
diff changeset
3517 if (state <= 200 && Chance16R(1, 7, r)) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3518 int index = (r * 10 >> 16);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3519
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3520 Vehicle *u = v;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3521 do {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3522 if (--index < 0) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3523 r = Random();
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3524
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3525 CreateEffectVehicleRel(u,
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3526 GB(r, 8, 3) + 2,
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3527 GB(r, 16, 3) + 2,
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3528 GB(r, 0, 3) + 5,
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3529 EV_EXPLOSION_SMALL);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3530 break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3531 }
7492
09743324277c (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium <rubidium@openttd.org>
parents: 7488
diff changeset
3532 } while ((u = u->Next()) != NULL);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3533 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3534
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3535 if (state <= 240 && !(v->tick_counter & 3)) ChangeTrainDirRandomly(v);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3536
8969
01cc3ee6ecb1 (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium <rubidium@openttd.org>
parents: 8962
diff changeset
3537 if (state >= 4440 && !(v->tick_counter & 0x1F)) {
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3538 bool ret = v->Next() != NULL;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3539 DeleteLastWagon(v);
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3540 return ret;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3541 }
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3542
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3543 return true;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3544 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3545
8283
dca081896c52 (svn r11847) -Fix: do not affect the speed a train is entering a depot by trackbits behind it
smatz <smatz@openttd.org>
parents: 8281
diff changeset
3546 /** Maximum speeds for train that is broken down or approaching line end */
8312
f6764781d2eb (svn r11877) -Fix: another way to crash competitors' train in a station
smatz <smatz@openttd.org>
parents: 8305
diff changeset
3547 static const uint16 _breakdown_speeds[16] = {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3548 225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3549 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3550
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3551
8283
dca081896c52 (svn r11847) -Fix: do not affect the speed a train is entering a depot by trackbits behind it
smatz <smatz@openttd.org>
parents: 8281
diff changeset
3552 /**
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3553 * Train is approaching line end, slow down and possibly reverse
8283
dca081896c52 (svn r11847) -Fix: do not affect the speed a train is entering a depot by trackbits behind it
smatz <smatz@openttd.org>
parents: 8281
diff changeset
3554 *
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3555 * @param v front train engine
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3556 * @param signal not line end, just a red signal
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3557 * @param reverse Set to false to not execute the vehicle reversing. This does not change any other logic.
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3558 * @return true iff we did NOT have to reverse
8283
dca081896c52 (svn r11847) -Fix: do not affect the speed a train is entering a depot by trackbits behind it
smatz <smatz@openttd.org>
parents: 8281
diff changeset
3559 */
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3560 static bool TrainApproachingLineEnd(Train *v, bool signal, bool reverse)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3561 {
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3562 /* Calc position within the current tile */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3563 uint x = v->x_pos & 0xF;
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3564 uint y = v->y_pos & 0xF;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3565
8312
f6764781d2eb (svn r11877) -Fix: another way to crash competitors' train in a station
smatz <smatz@openttd.org>
parents: 8305
diff changeset
3566 /* for diagonal directions, 'x' will be 0..15 -
f6764781d2eb (svn r11877) -Fix: another way to crash competitors' train in a station
smatz <smatz@openttd.org>
parents: 8305
diff changeset
3567 * for other directions, it will be 1, 3, 5, ..., 15 */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3568 switch (v->direction) {
8312
f6764781d2eb (svn r11877) -Fix: another way to crash competitors' train in a station
smatz <smatz@openttd.org>
parents: 8305
diff changeset
3569 case DIR_N : x = ~x + ~y + 25; break;
15579
5f128be91f59 (svn r20239) -Doc: Give a common name to all fall throughs (FALL THROUGH).
terkhen <terkhen@openttd.org>
parents: 15552
diff changeset
3570 case DIR_NW: x = y; // FALL THROUGH
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3571 case DIR_NE: x = ~x + 16; break;
8312
f6764781d2eb (svn r11877) -Fix: another way to crash competitors' train in a station
smatz <smatz@openttd.org>
parents: 8305
diff changeset
3572 case DIR_E : x = ~x + y + 9; break;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3573 case DIR_SE: x = y; break;
8312
f6764781d2eb (svn r11877) -Fix: another way to crash competitors' train in a station
smatz <smatz@openttd.org>
parents: 8305
diff changeset
3574 case DIR_S : x = x + y - 7; break;
f6764781d2eb (svn r11877) -Fix: another way to crash competitors' train in a station
smatz <smatz@openttd.org>
parents: 8305
diff changeset
3575 case DIR_W : x = ~y + x + 9; break;
5587
c44c070c5032 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium <rubidium@openttd.org>
parents: 5584
diff changeset
3576 default: break;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3577 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3578
17452
aad8b54a8fa6 (svn r22206) -Revert-ish (r14526) / Unfix-ish [FS#3569] / Fix [FS#4545]: the "center" (for movement) of vehicles is (currently still) always at 4/8th original vehicle length from the front, so trains should stop at the same location regardless of the length of the front engine
rubidium <rubidium@openttd.org>
parents: 17451
diff changeset
3579 /* Do not reverse when approaching red signal. Make sure the vehicle's front
aad8b54a8fa6 (svn r22206) -Revert-ish (r14526) / Unfix-ish [FS#3569] / Fix [FS#4545]: the "center" (for movement) of vehicles is (currently still) always at 4/8th original vehicle length from the front, so trains should stop at the same location regardless of the length of the front engine
rubidium <rubidium@openttd.org>
parents: 17451
diff changeset
3580 * does not cross the tile boundary when we do reverse, but as the vehicle's
aad8b54a8fa6 (svn r22206) -Revert-ish (r14526) / Unfix-ish [FS#3569] / Fix [FS#4545]: the "center" (for movement) of vehicles is (currently still) always at 4/8th original vehicle length from the front, so trains should stop at the same location regardless of the length of the front engine
rubidium <rubidium@openttd.org>
parents: 17451
diff changeset
3581 * location is based on their center, use half a vehicle's length as offset.
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3582 * Multiply the half-length by two for straight directions to compensate that
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3583 * we only get odd x offsets there. */
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3584 if (!signal && x + (v->gcache.cached_veh_length + 1) / 2 * (IsDiagonalDirection(v->direction) ? 1 : 2) >= TILE_SIZE) {
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3585 /* we are too near the tile end, reverse now */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3586 v->cur_speed = 0;
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3587 if (reverse) ReverseTrainDirection(v);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3588 return false;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3589 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3590
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
3591 /* slow down */
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3592 v->vehstatus |= VS_TRAIN_SLOWING;
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3593 uint16 break_speed = _breakdown_speeds[x & 0xF];
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3594 if (break_speed < v->cur_speed) v->cur_speed = break_speed;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3595
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3596 return true;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3597 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3598
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3599
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3600 /**
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3601 * Determines whether train would like to leave the tile
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3602 * @param v train to test
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3603 * @return true iff vehicle is NOT entering or inside a depot or tunnel/bridge
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3604 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3605 static bool TrainCanLeaveTile(const Train *v)
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3606 {
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3607 /* Exit if inside a tunnel/bridge or a depot */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3608 if (v->track == TRACK_BIT_WORMHOLE || v->track == TRACK_BIT_DEPOT) return false;
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3609
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3610 TileIndex tile = v->tile;
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3611
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3612 /* entering a tunnel/bridge? */
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3613 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3614 DiagDirection dir = GetTunnelBridgeDirection(tile);
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3615 if (DiagDirToDir(dir) == v->direction) return false;
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3616 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3617
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3618 /* entering a depot? */
8961
b33bcd7fa037 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz <smatz@openttd.org>
parents: 8954
diff changeset
3619 if (IsRailDepotTile(tile)) {
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3620 DiagDirection dir = ReverseDiagDir(GetRailDepotDirection(tile));
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3621 if (DiagDirToDir(dir) == v->direction) return false;
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3622 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3623
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3624 return true;
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3625 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3626
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3627
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3628 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3629 * Determines whether train is approaching a rail-road crossing
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3630 * (thus making it barred)
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3631 * @param v front engine of train
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3632 * @return TileIndex of crossing the train is approaching, else INVALID_TILE
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3633 * @pre v in non-crashed front engine
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3634 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3635 static TileIndex TrainApproachingCrossingTile(const Train *v)
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3636 {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3637 assert(v->IsFrontEngine());
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3638 assert(!(v->vehstatus & VS_CRASHED));
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3639
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3640 if (!TrainCanLeaveTile(v)) return INVALID_TILE;
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3641
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3642 DiagDirection dir = TrainExitDir(v->direction, v->track);
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3643 TileIndex tile = v->tile + TileOffsByDiagDir(dir);
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3644
8606
86c438420c53 (svn r12188) -Codechange [FS#1782]: do not check twice for correct rail owner (patch by Vikthor)
smatz <smatz@openttd.org>
parents: 8598
diff changeset
3645 /* not a crossing || wrong axis || unusable rail (wrong type or owner) */
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3646 if (!IsLevelCrossingTile(tile) || DiagDirToAxis(dir) == GetCrossingRoadAxis(tile) ||
8606
86c438420c53 (svn r12188) -Codechange [FS#1782]: do not check twice for correct rail owner (patch by Vikthor)
smatz <smatz@openttd.org>
parents: 8598
diff changeset
3647 !CheckCompatibleRail(v, tile)) {
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3648 return INVALID_TILE;
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3649 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3650
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3651 return tile;
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3652 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3653
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3654
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3655 /**
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3656 * Checks for line end. Also, bars crossing at next tile if needed
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3657 *
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3658 * @param v vehicle we are checking
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3659 * @param reverse Set to false to not execute the vehicle reversing. This does not change any other logic.
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3660 * @return true iff we did NOT have to reverse
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3661 */
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3662 static bool TrainCheckIfLineEnds(Train *v, bool reverse)
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3663 {
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3664 /* First, handle broken down train */
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3665
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3666 int t = v->breakdown_ctr;
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3667 if (t > 1) {
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3668 v->vehstatus |= VS_TRAIN_SLOWING;
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3669
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3670 uint16 break_speed = _breakdown_speeds[GB(~t, 4, 4)];
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3671 if (break_speed < v->cur_speed) v->cur_speed = break_speed;
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3672 } else {
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3673 v->vehstatus &= ~VS_TRAIN_SLOWING;
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3674 }
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3675
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3676 if (!TrainCanLeaveTile(v)) return true;
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3677
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3678 /* Determine the non-diagonal direction in which we will exit this tile */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3679 DiagDirection dir = TrainExitDir(v->direction, v->track);
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3680 /* Calculate next tile */
8334
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3681 TileIndex tile = v->tile + TileOffsByDiagDir(dir);
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3682
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3683 /* Determine the track status on the next tile */
8794
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
3684 TrackStatus ts = GetTileTrackStatus(tile, TRANSPORT_RAIL, 0, ReverseDiagDir(dir));
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
3685 TrackdirBits reachable_trackdirs = DiagdirReachesTrackdirs(dir);
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
3686
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
3687 TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts) & reachable_trackdirs;
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
3688 TrackdirBits red_signals = TrackStatusToRedSignals(ts) & reachable_trackdirs;
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3689
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3690 /* We are sure the train is not entering a depot, it is detected above */
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3691
8482
dd2dbdff6d0d (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz <smatz@openttd.org>
parents: 8467
diff changeset
3692 /* mask unreachable track bits if we are forbidden to do 90deg turns */
8616
9e46ac001a8c (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch <frosch@openttd.org>
parents: 8614
diff changeset
3693 TrackBits bits = TrackdirBitsToTrackBits(trackdirbits);
13833
ad09d18a3e34 (svn r18362) -Cleanup: remove OPF for RVs and NTP to clean up lots of code and simplify some things for the future
rubidium <rubidium@openttd.org>
parents: 13832
diff changeset
3694 if (_settings_game.pf.forbid_90_deg) {
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3695 bits &= ~TrackCrossesTracks(FindFirstTrack(v->track));
8482
dd2dbdff6d0d (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz <smatz@openttd.org>
parents: 8467
diff changeset
3696 }
dd2dbdff6d0d (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz <smatz@openttd.org>
parents: 8467
diff changeset
3697
8606
86c438420c53 (svn r12188) -Codechange [FS#1782]: do not check twice for correct rail owner (patch by Vikthor)
smatz <smatz@openttd.org>
parents: 8598
diff changeset
3698 /* no suitable trackbits at all || unusable rail (wrong type or owner) */
86c438420c53 (svn r12188) -Codechange [FS#1782]: do not check twice for correct rail owner (patch by Vikthor)
smatz <smatz@openttd.org>
parents: 8598
diff changeset
3699 if (bits == TRACK_BIT_NONE || !CheckCompatibleRail(v, tile)) {
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3700 return TrainApproachingLineEnd(v, false, reverse);
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3701 }
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3702
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3703 /* approaching red signal */
18446
83cd95a883a3 (svn r23290) -Fix [FS#2379,FS#3569]: Change the centre of train vehicles to depend on the vehicle length instead of being fixed at 4/8th of the original vehicle length to make sure shortened vehicles don't block tiles they shouldn't block.
michi_cc <michi_cc@openttd.org>
parents: 18337
diff changeset
3704 if ((trackdirbits & red_signals) != 0) return TrainApproachingLineEnd(v, true, reverse);
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3705
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3706 /* approaching a rail/road crossing? then make it red */
8356
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
3707 if (IsLevelCrossingTile(tile)) MaybeBarCrossingWithSound(tile);
8305
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3708
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3709 return true;
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3710 }
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3711
1e1e16a1129c (svn r11870) -Fix: slow down train when approaching tile we can't enter in more cases
smatz <smatz@openttd.org>
parents: 8302
diff changeset
3712
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3713 static bool TrainLocoHandler(Train *v, bool mode)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3714 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3715 /* train has crashed? */
7476
a24293df92f5 (svn r10983) -Codechange: use vehstatus & VS_CRASHED instead of some other "methods" custom to each vehicle to determine whether the vehicle is crashed.
rubidium <rubidium@openttd.org>
parents: 7425
diff changeset
3716 if (v->vehstatus & VS_CRASHED) {
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3717 return mode ? true : HandleCrashedTrain(v); // 'this' can be deleted here
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3718 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3719
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
3720 if (v->force_proceed != TFP_NONE) {
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3721 ClrBit(v->flags, VRF_TRAIN_STUCK);
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
3722 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
9808
fa8743b489ff (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9775
diff changeset
3723 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3724
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3725 /* train is broken down? */
15957
1452fca58b4f (svn r20645) -Codechange [FS#4086]: unify the code for checking for breakdown handling as well (Hirundo)
rubidium <rubidium@openttd.org>
parents: 15956
diff changeset
3726 if (v->HandleBreakdown()) return true;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3727
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3728 if (HasBit(v->flags, VRF_REVERSING) && v->cur_speed == 0) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3729 ReverseTrainDirection(v);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3730 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3731
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3732 /* exit if train is stopped */
12082
d092f17a921d (svn r16491) -Codechange: Added parentheses around bitwise operators for code style.
alberth <alberth@openttd.org>
parents: 12073
diff changeset
3733 if ((v->vehstatus & VS_STOPPED) && v->cur_speed == 0) return true;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3734
11972
fd10870d74d4 (svn r16378) -Codechange: replace OldPool with simpler Pool. Compilation time, binary size and run time (with asserts disabled) should be improved
smatz <smatz@openttd.org>
parents: 11970
diff changeset
3735 bool valid_order = !v->current_order.IsType(OT_NOTHING) && v->current_order.GetType() != OT_CONDITIONAL;
8827
095c429c5fa7 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium <rubidium@openttd.org>
parents: 8794
diff changeset
3736 if (ProcessOrders(v) && CheckReverseTrain(v)) {
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
3737 v->wait_counter = 0;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3738 v->cur_speed = 0;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3739 v->subspeed = 0;
15449
f6ee1b7fde96 (svn r20098) -Fix [FS#3898]: A train reversing in a station would sometimes fail to release its reserved path.
michi_cc <michi_cc@openttd.org>
parents: 15436
diff changeset
3740 ClrBit(v->flags, VRF_LEAVING_STATION);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3741 ReverseTrainDirection(v);
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3742 return true;
15449
f6ee1b7fde96 (svn r20098) -Fix [FS#3898]: A train reversing in a station would sometimes fail to release its reserved path.
michi_cc <michi_cc@openttd.org>
parents: 15436
diff changeset
3743 } else if (HasBit(v->flags, VRF_LEAVING_STATION)) {
f6ee1b7fde96 (svn r20098) -Fix [FS#3898]: A train reversing in a station would sometimes fail to release its reserved path.
michi_cc <michi_cc@openttd.org>
parents: 15436
diff changeset
3744 /* Try to reserve a path when leaving the station as we
f6ee1b7fde96 (svn r20098) -Fix [FS#3898]: A train reversing in a station would sometimes fail to release its reserved path.
michi_cc <michi_cc@openttd.org>
parents: 15436
diff changeset
3745 * might not be marked as wanting a reservation, e.g.
f6ee1b7fde96 (svn r20098) -Fix [FS#3898]: A train reversing in a station would sometimes fail to release its reserved path.
michi_cc <michi_cc@openttd.org>
parents: 15436
diff changeset
3746 * when an overlength train gets turned around in a station. */
16306
091fcb86fcba (svn r21014) -Fix [FS#4179](r20098): Reversing of trains in stations while last wagon was in a depot or on a bridge-/tunnelhead caused trouble.
frosch <frosch@openttd.org>
parents: 16075
diff changeset
3747 DiagDirection dir = TrainExitDir(v->direction, v->track);
091fcb86fcba (svn r21014) -Fix [FS#4179](r20098): Reversing of trains in stations while last wagon was in a depot or on a bridge-/tunnelhead caused trouble.
frosch <frosch@openttd.org>
parents: 16075
diff changeset
3748 if (IsRailDepotTile(v->tile) || IsTileType(v->tile, MP_TUNNELBRIDGE)) dir = INVALID_DIAGDIR;
091fcb86fcba (svn r21014) -Fix [FS#4179](r20098): Reversing of trains in stations while last wagon was in a depot or on a bridge-/tunnelhead caused trouble.
frosch <frosch@openttd.org>
parents: 16075
diff changeset
3749
091fcb86fcba (svn r21014) -Fix [FS#4179](r20098): Reversing of trains in stations while last wagon was in a depot or on a bridge-/tunnelhead caused trouble.
frosch <frosch@openttd.org>
parents: 16075
diff changeset
3750 if (UpdateSignalsOnSegment(v->tile, dir, v->owner) == SIGSEG_PBS || _settings_game.pf.reserve_paths) {
15449
f6ee1b7fde96 (svn r20098) -Fix [FS#3898]: A train reversing in a station would sometimes fail to release its reserved path.
michi_cc <michi_cc@openttd.org>
parents: 15436
diff changeset
3751 TryPathReserve(v, true, true);
f6ee1b7fde96 (svn r20098) -Fix [FS#3898]: A train reversing in a station would sometimes fail to release its reserved path.
michi_cc <michi_cc@openttd.org>
parents: 15436
diff changeset
3752 }
f6ee1b7fde96 (svn r20098) -Fix [FS#3898]: A train reversing in a station would sometimes fail to release its reserved path.
michi_cc <michi_cc@openttd.org>
parents: 15436
diff changeset
3753 ClrBit(v->flags, VRF_LEAVING_STATION);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3754 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3755
6594
7535dcf42683 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium <rubidium@openttd.org>
parents: 6593
diff changeset
3756 v->HandleLoading(mode);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3757
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3758 if (v->current_order.IsType(OT_LOADING)) return true;
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3759
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3760 if (CheckTrainStayInDepot(v)) return true;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3761
16511
b9db78828e3d (svn r21237) -Codechange: Move HandleLocomotiveSmokeCloud to Vehicle::ShowVisualEffect (Hirundo)
rubidium <rubidium@openttd.org>
parents: 16510
diff changeset
3762 if (!mode) v->ShowVisualEffect();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3763
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3764 /* We had no order but have an order now, do look ahead. */
11972
fd10870d74d4 (svn r16378) -Codechange: replace OldPool with simpler Pool. Compilation time, binary size and run time (with asserts disabled) should be improved
smatz <smatz@openttd.org>
parents: 11970
diff changeset
3765 if (!valid_order && !v->current_order.IsType(OT_NOTHING)) {
9816
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3766 CheckNextTrainTile(v);
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3767 }
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
3768
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3769 /* Handle stuck trains. */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3770 if (!mode && HasBit(v->flags, VRF_TRAIN_STUCK)) {
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
3771 ++v->wait_counter;
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3772
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3773 /* Should we try reversing this tick if still stuck? */
17223
a1dda316ecbe (svn r21962) -Feature-ish: GUI setting to disable reversing at signals
rubidium <rubidium@openttd.org>
parents: 17221
diff changeset
3774 bool turn_around = v->wait_counter % (_settings_game.pf.wait_for_pbs_path * DAY_TICKS) == 0 && _settings_game.pf.reverse_at_signals;
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
3775
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
3776 if (!turn_around && v->wait_counter % _settings_game.pf.path_backoff_interval != 0 && v->force_proceed == TFP_NONE) return true;
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3777 if (!TryPathReserve(v)) {
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3778 /* Still stuck. */
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3779 if (turn_around) ReverseTrainDirection(v);
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3780
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
3781 if (HasBit(v->flags, VRF_TRAIN_STUCK) && v->wait_counter > 2 * _settings_game.pf.wait_for_pbs_path * DAY_TICKS) {
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3782 /* Show message to player. */
16772
975815b08336 (svn r21505) -Change: generalise the lost_train_warning setting to lost_vehicle_warning
rubidium <rubidium@openttd.org>
parents: 16771
diff changeset
3783 if (_settings_client.gui.lost_vehicle_warn && v->owner == _local_company) {
10574
bf4424d086f0 (svn r14831) -Codechange: use {VEHICLE} instead of Train/Ship/Road veh/Aircraft/{STRING} {COMMA} in lang files (part by Swallow)
smatz <smatz@openttd.org>
parents: 10571
diff changeset
3784 SetDParam(0, v->index);
19381
f37c5813696b (svn r24282) -Codechange: Add AddVehicleAdviceNewsItem function to preemptively deduplicate code.
frosch <frosch@openttd.org>
parents: 19349
diff changeset
3785 AddVehicleAdviceNewsItem(STR_NEWS_TRAIN_IS_STUCK, v->index);
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3786 }
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
3787 v->wait_counter = 0;
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3788 }
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3789 /* Exit if force proceed not pressed, else reset stuck flag anyway. */
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
3790 if (v->force_proceed == TFP_NONE) return true;
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3791 ClrBit(v->flags, VRF_TRAIN_STUCK);
14213
41b2e7bf03da (svn r18764) -Fix [FS#3422]: split the (un)load ticks counter and signal wait counter; sometimes they might get into eachother's way
rubidium <rubidium@openttd.org>
parents: 14127
diff changeset
3792 v->wait_counter = 0;
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
3793 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
9813
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3794 }
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3795 }
77ed5f6edd46 (svn r13955) -Codechange [YAPP]: Try to extend the path of a stuck train so it is able to continue. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9812
diff changeset
3796
10682
2d192d93d11c (svn r14993) -Fix (r14957, r14975): Do not reverse the train with OT_LEAVESTATION as that breaks PBS prediction.
frosch <frosch@openttd.org>
parents: 10672
diff changeset
3797 if (v->current_order.IsType(OT_LEAVESTATION)) {
2d192d93d11c (svn r14993) -Fix (r14957, r14975): Do not reverse the train with OT_LEAVESTATION as that breaks PBS prediction.
frosch <frosch@openttd.org>
parents: 10672
diff changeset
3798 v->current_order.Free();
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
3799 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3800 return true;
10682
2d192d93d11c (svn r14993) -Fix (r14957, r14975): Do not reverse the train with OT_LEAVESTATION as that breaks PBS prediction.
frosch <frosch@openttd.org>
parents: 10672
diff changeset
3801 }
2d192d93d11c (svn r14993) -Fix (r14957, r14975): Do not reverse the train with OT_LEAVESTATION as that breaks PBS prediction.
frosch <frosch@openttd.org>
parents: 10672
diff changeset
3802
14284
fc532d11eb85 (svn r18836) -Codechange: make TrainUpdateSpeed a class function and update some comments (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14261
diff changeset
3803 int j = v->UpdateSpeed();
8426
916b061b84a0 (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz <smatz@openttd.org>
parents: 8390
diff changeset
3804
916b061b84a0 (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz <smatz@openttd.org>
parents: 8390
diff changeset
3805 /* we need to invalidate the widget if we are stopping from 'Stopping 0 km/h' to 'Stopped' */
15351
6adf1d7d80cd (svn r19992) -Fix [FS#3878]: Clear force_proceed when entering depots and when loading.
frosch <frosch@openttd.org>
parents: 15320
diff changeset
3806 if (v->cur_speed == 0 && (v->vehstatus & VS_STOPPED)) {
13977
bb2258bdc43d (svn r18515) -Change: make it visible when you're to pass the next signal on danger and possible to cancel it
rubidium <rubidium@openttd.org>
parents: 13958
diff changeset
3807 /* If we manually stopped, we're not force-proceeding anymore. */
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
3808 v->force_proceed = TFP_NONE;
15351
6adf1d7d80cd (svn r19992) -Fix [FS#3878]: Clear force_proceed when entering depots and when loading.
frosch <frosch@openttd.org>
parents: 15320
diff changeset
3809 SetWindowDirty(WC_VEHICLE_VIEW, v->index);
8426
916b061b84a0 (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz <smatz@openttd.org>
parents: 8390
diff changeset
3810 }
916b061b84a0 (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz <smatz@openttd.org>
parents: 8390
diff changeset
3811
15436
a90f03aeb026 (svn r20079) -Codechange [FS#3922]: Add helper functions to deal with the 192-256-magic of vehicle movement.
frosch <frosch@openttd.org>
parents: 15363
diff changeset
3812 int adv_spd = v->GetAdvanceDistance();
10214
44a5ab175885 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium <rubidium@openttd.org>
parents: 10208
diff changeset
3813 if (j < adv_spd) {
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
3814 /* if the vehicle has speed 0, update the last_speed field. */
17172
679a25d71a9d (svn r21911) -Codechange: move tcache.last_speed to gcache.last_speed and make SetLastSpeed a function of GroundVehicle
rubidium <rubidium@openttd.org>
parents: 17165
diff changeset
3815 if (v->cur_speed == 0) v->SetLastSpeed();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3816 } else {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3817 TrainCheckIfLineEnds(v);
10214
44a5ab175885 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium <rubidium@openttd.org>
parents: 10208
diff changeset
3818 /* Loop until the train has finished moving. */
11701
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3819 for (;;) {
10214
44a5ab175885 (svn r14436) -Fix [FS#1793]: (fast) trains would sometimes move one time too often/little when moving from diagonal<->non-diagonal tracks (iguannab)
rubidium <rubidium@openttd.org>
parents: 10208
diff changeset
3820 j -= adv_spd;
11327
fc99317cb85b (svn r15677) -Fix [FS#2546]: vehicle images would be determined during the process of moving the vehicle which means that only the (orientation) data for the vehicles in front of it is valid. Now the data for the vehicles behind the vehicle are valid too.
rubidium <rubidium@openttd.org>
parents: 11326
diff changeset
3821 TrainController(v, NULL);
11701
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3822 /* Don't continue to move if the train crashed. */
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3823 if (CheckTrainCollision(v)) break;
15436
a90f03aeb026 (svn r20079) -Codechange [FS#3922]: Add helper functions to deal with the 192-256-magic of vehicle movement.
frosch <frosch@openttd.org>
parents: 15363
diff changeset
3824 /* Determine distance to next map position */
a90f03aeb026 (svn r20079) -Codechange [FS#3922]: Add helper functions to deal with the 192-256-magic of vehicle movement.
frosch <frosch@openttd.org>
parents: 15363
diff changeset
3825 adv_spd = v->GetAdvanceDistance();
11694
50291ef7d6e6 (svn r16079) -Fix [FS#2824]: insanely fast trains would not stop in time for stations
rubidium <rubidium@openttd.org>
parents: 11690
diff changeset
3826
11701
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3827 /* No more moving this tick */
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3828 if (j < adv_spd || v->cur_speed == 0) break;
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3829
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3830 OrderType order_type = v->current_order.GetType();
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3831 /* Do not skip waypoints (incl. 'via' stations) when passing through at full speed. */
12472
604a2cfc77ab (svn r16909) -Fix [FS#2996]: NewGRF stations would be triggering assertions all over the place when using the more advanced station types.
rubidium <rubidium@openttd.org>
parents: 12471
diff changeset
3832 if ((order_type == OT_GOTO_WAYPOINT || order_type == OT_GOTO_STATION) &&
11701
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3833 (v->current_order.GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) &&
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3834 IsTileType(v->tile, MP_STATION) &&
12472
604a2cfc77ab (svn r16909) -Fix [FS#2996]: NewGRF stations would be triggering assertions all over the place when using the more advanced station types.
rubidium <rubidium@openttd.org>
parents: 12471
diff changeset
3835 v->current_order.GetDestination() == GetStationIndex(v->tile)) {
11701
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3836 ProcessOrders(v);
11694
50291ef7d6e6 (svn r16079) -Fix [FS#2824]: insanely fast trains would not stop in time for stations
rubidium <rubidium@openttd.org>
parents: 11690
diff changeset
3837 }
11701
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3838 }
17172
679a25d71a9d (svn r21911) -Codechange: move tcache.last_speed to gcache.last_speed and make SetLastSpeed a function of GroundVehicle
rubidium <rubidium@openttd.org>
parents: 17165
diff changeset
3839 v->SetLastSpeed();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3840 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3841
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3842 for (Train *u = v; u != NULL; u = u->Next()) {
11327
fc99317cb85b (svn r15677) -Fix [FS#2546]: vehicle images would be determined during the process of moving the vehicle which means that only the (orientation) data for the vehicles in front of it is valid. Now the data for the vehicles behind the vehicle are valid too.
rubidium <rubidium@openttd.org>
parents: 11326
diff changeset
3843 if ((u->vehstatus & VS_HIDDEN) != 0) continue;
fc99317cb85b (svn r15677) -Fix [FS#2546]: vehicle images would be determined during the process of moving the vehicle which means that only the (orientation) data for the vehicles in front of it is valid. Now the data for the vehicles behind the vehicle are valid too.
rubidium <rubidium@openttd.org>
parents: 11326
diff changeset
3844
12666
0275245c6c62 (svn r17121) -Fix [FS#3060]: Update vehicle position cache when the vehicle sprite changes.
frosch <frosch@openttd.org>
parents: 12635
diff changeset
3845 u->UpdateViewport(false, false);
11327
fc99317cb85b (svn r15677) -Fix [FS#2546]: vehicle images would be determined during the process of moving the vehicle which means that only the (orientation) data for the vehicles in front of it is valid. Now the data for the vehicles behind the vehicle are valid too.
rubidium <rubidium@openttd.org>
parents: 11326
diff changeset
3846 }
fc99317cb85b (svn r15677) -Fix [FS#2546]: vehicle images would be determined during the process of moving the vehicle which means that only the (orientation) data for the vehicles in front of it is valid. Now the data for the vehicles behind the vehicle are valid too.
rubidium <rubidium@openttd.org>
parents: 11326
diff changeset
3847
10641
94cbc9a0158b (svn r14943) -Cleanup: remove some rogue spaces/replace some rogue tabs with spaces
rubidium <rubidium@openttd.org>
parents: 10589
diff changeset
3848 if (v->progress == 0) v->progress = j; // Save unused spd for next time, if TrainController didn't set progress
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3849
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3850 return true;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3851 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3852
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3853 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3854 * Get running cost for the train consist.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3855 * @return Yearly running costs.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3856 */
7488
307a03e3f031 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium <rubidium@openttd.org>
parents: 7486
diff changeset
3857 Money Train::GetRunningCost() const
307a03e3f031 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium <rubidium@openttd.org>
parents: 7486
diff changeset
3858 {
307a03e3f031 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium <rubidium@openttd.org>
parents: 7486
diff changeset
3859 Money cost = 0;
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3860 const Train *v = this;
7488
307a03e3f031 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium <rubidium@openttd.org>
parents: 7486
diff changeset
3861
307a03e3f031 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium <rubidium@openttd.org>
parents: 7486
diff changeset
3862 do {
18238
3141f1ed78eb (svn r23074) -Codechange: Add Vehicle::GetEngine() to simplify code.
frosch <frosch@openttd.org>
parents: 18159
diff changeset
3863 const Engine *e = v->GetEngine();
13741
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13599
diff changeset
3864 if (e->u.rail.running_cost_class == INVALID_PRICE) continue;
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13599
diff changeset
3865
775d368aa2b0 (svn r18266) -Codechange: Add a function to compute prices from price base and cost factor and use it consistently for vehicle purchase, running cost, and refit cost.
frosch <frosch@openttd.org>
parents: 13599
diff changeset
3866 uint cost_factor = GetVehicleProperty(v, PROP_TRAIN_RUNNING_COST_FACTOR, e->u.rail.running_cost);
7488
307a03e3f031 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium <rubidium@openttd.org>
parents: 7486
diff changeset
3867 if (cost_factor == 0) continue;
307a03e3f031 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium <rubidium@openttd.org>
parents: 7486
diff changeset
3868
9208
aa4576e5111a (svn r13074) -Fix: For multiheaded engines, halve power and running cost when used instead of when loading, to allow callback values to work properly.
peter1138 <peter1138@openttd.org>
parents: 9115
diff changeset
3869 /* Halve running cost for multiheaded parts */
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
3870 if (v->IsMultiheaded()) cost_factor /= 2;
9208
aa4576e5111a (svn r13074) -Fix: For multiheaded engines, halve power and running cost when used instead of when loading, to allow callback values to work properly.
peter1138 <peter1138@openttd.org>
parents: 9115
diff changeset
3871
18239
e6cbcbf3780f (svn r23075) -Codechange: Add GetGRF() and GetGRFID() methods to Engine and Vehicle to simplify code.
frosch <frosch@openttd.org>
parents: 18238
diff changeset
3872 cost += GetPrice(e->u.rail.running_cost_class, cost_factor, e->GetGRF());
12306
d0ff55a976ad (svn r16723) -Codechange: make GetNextArticPart(), GetLastEnginePart(), GetNextVehicle(), GetPrevVehicle(), GetNextUnit(), GetPrevUnit() members of Train
smatz <smatz@openttd.org>
parents: 12303
diff changeset
3873 } while ((v = v->GetNextVehicle()) != NULL);
7488
307a03e3f031 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium <rubidium@openttd.org>
parents: 7486
diff changeset
3874
307a03e3f031 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium <rubidium@openttd.org>
parents: 7486
diff changeset
3875 return cost;
307a03e3f031 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium <rubidium@openttd.org>
parents: 7486
diff changeset
3876 }
307a03e3f031 (svn r10999) -Codechange: unify the way the running cost of a vehicle is determined. Patch by nycom.
rubidium <rubidium@openttd.org>
parents: 7486
diff changeset
3877
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3878 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3879 * Update train vehicle data for a tick.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3880 * @return True if the vehicle still exists, false if it has ceased to exist (front of consists only).
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3881 */
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3882 bool Train::Tick()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3883 {
7135
45123abbc930 (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium <rubidium@openttd.org>
parents: 7134
diff changeset
3884 this->tick_counter++;
45123abbc930 (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium <rubidium@openttd.org>
parents: 7134
diff changeset
3885
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3886 if (this->IsFrontEngine()) {
15363
5ea4f4fa8ea2 (svn r20006) -Fix: Trains should also have running cost while slowing down for stop.
frosch <frosch@openttd.org>
parents: 15362
diff changeset
3887 if (!(this->vehstatus & VS_STOPPED) || this->cur_speed > 0) this->running_ticks++;
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3888
7135
45123abbc930 (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium <rubidium@openttd.org>
parents: 7134
diff changeset
3889 this->current_order_time++;
45123abbc930 (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium <rubidium@openttd.org>
parents: 7134
diff changeset
3890
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3891 if (!TrainLocoHandler(this, false)) return false;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3892
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3893 return TrainLocoHandler(this, true);
12301
218a41d3d557 (svn r16718) -Codechange: make IsFreeWagon() member of Train
smatz <smatz@openttd.org>
parents: 12300
diff changeset
3894 } else if (this->IsFreeWagon() && (this->vehstatus & VS_CRASHED)) {
7696
7ae4d4720c11 (svn r11230) -Fix [FS#1228]: one could construct trains out of crashed wagons and engines. Based on a patch by SmatZ.
rubidium <rubidium@openttd.org>
parents: 7695
diff changeset
3895 /* Delete flooded standalone wagon chain */
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3896 if (++this->crash_anim_pos >= 4400) {
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3897 delete this;
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3898 return false;
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3899 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3900 }
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3901
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3902 return true;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3903 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3904
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3905 /**
19968
0dba7f49118c (svn r24900) -Fix [FS#5389]: Comments with typos (most fixes supplied by Eagle_rainbow)
planetmaker <planetmaker@openttd.org>
parents: 19913
diff changeset
3906 * Check whether a train needs service, and if so, find a depot or service it.
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3907 * @return v %Train to check.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3908 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3909 static void CheckIfTrainNeedsService(Train *v)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3910 {
12032
2afeb10ae6ba (svn r16439) -Change: Make the default vehicle servicing settings company-based settings, so in a multiplayer game everyone can change them.
yexo <yexo@openttd.org>
parents: 12020
diff changeset
3911 if (Company::Get(v->owner)->settings.vehicle.servint_trains == 0 || !v->NeedsAutomaticServicing()) return;
19481
b271f4f060fa (svn r24384) -Fix [FS#5188-ish]: Make IsInDepot() functions behave consistent across vehicle types and add IsChainInDepot instead, if that is what shall be checked.
frosch <frosch@openttd.org>
parents: 19479
diff changeset
3912 if (v->IsChainInDepot()) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3913 VehicleServiceInDepot(v);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3914 return;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3915 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3916
13945
d725b850c967 (svn r18481) -Codechange: unify the curve pathfinder penalty defaults; 0.01 tile won't make a dent, 3 tiles might be a bit too much
rubidium <rubidium@openttd.org>
parents: 13942
diff changeset
3917 uint max_penalty;
d725b850c967 (svn r18481) -Codechange: unify the curve pathfinder penalty defaults; 0.01 tile won't make a dent, 3 tiles might be a bit too much
rubidium <rubidium@openttd.org>
parents: 13942
diff changeset
3918 switch (_settings_game.pf.pathfinder_for_trains) {
d725b850c967 (svn r18481) -Codechange: unify the curve pathfinder penalty defaults; 0.01 tile won't make a dent, 3 tiles might be a bit too much
rubidium <rubidium@openttd.org>
parents: 13942
diff changeset
3919 case VPF_NPF: max_penalty = _settings_game.pf.npf.maximum_go_to_depot_penalty; break;
d725b850c967 (svn r18481) -Codechange: unify the curve pathfinder penalty defaults; 0.01 tile won't make a dent, 3 tiles might be a bit too much
rubidium <rubidium@openttd.org>
parents: 13942
diff changeset
3920 case VPF_YAPF: max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty; break;
d725b850c967 (svn r18481) -Codechange: unify the curve pathfinder penalty defaults; 0.01 tile won't make a dent, 3 tiles might be a bit too much
rubidium <rubidium@openttd.org>
parents: 13942
diff changeset
3921 default: NOT_REACHED();
d725b850c967 (svn r18481) -Codechange: unify the curve pathfinder penalty defaults; 0.01 tile won't make a dent, 3 tiles might be a bit too much
rubidium <rubidium@openttd.org>
parents: 13942
diff changeset
3922 }
d725b850c967 (svn r18481) -Codechange: unify the curve pathfinder penalty defaults; 0.01 tile won't make a dent, 3 tiles might be a bit too much
rubidium <rubidium@openttd.org>
parents: 13942
diff changeset
3923
d725b850c967 (svn r18481) -Codechange: unify the curve pathfinder penalty defaults; 0.01 tile won't make a dent, 3 tiles might be a bit too much
rubidium <rubidium@openttd.org>
parents: 13942
diff changeset
3924 FindDepotData tfdd = FindClosestTrainDepot(v, max_penalty);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3925 /* Only go to the depot if it is not too far out of our way. */
13945
d725b850c967 (svn r18481) -Codechange: unify the curve pathfinder penalty defaults; 0.01 tile won't make a dent, 3 tiles might be a bit too much
rubidium <rubidium@openttd.org>
parents: 13942
diff changeset
3926 if (tfdd.best_length == UINT_MAX || tfdd.best_length > max_penalty) {
8836
890a77315801 (svn r12584) -Codechange: do not access the order type directly.
rubidium <rubidium@openttd.org>
parents: 8832
diff changeset
3927 if (v->current_order.IsType(OT_GOTO_DEPOT)) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3928 /* If we were already heading for a depot but it has
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3929 * suddenly moved farther away, we continue our normal
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3930 * schedule? */
8836
890a77315801 (svn r12584) -Codechange: do not access the order type directly.
rubidium <rubidium@openttd.org>
parents: 8832
diff changeset
3931 v->current_order.MakeDummy();
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
3932 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3933 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3934 return;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3935 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3936
13001
c2298b1d3f83 (svn r17495) -Codechange: replace 'Depot::Get(GetDepotIndex(tile))->index' with GetDepotIndex(tile)
rubidium <rubidium@openttd.org>
parents: 12860
diff changeset
3937 DepotID depot = GetDepotIndex(tfdd.tile);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3938
8836
890a77315801 (svn r12584) -Codechange: do not access the order type directly.
rubidium <rubidium@openttd.org>
parents: 8832
diff changeset
3939 if (v->current_order.IsType(OT_GOTO_DEPOT) &&
13001
c2298b1d3f83 (svn r17495) -Codechange: replace 'Depot::Get(GetDepotIndex(tile))->index' with GetDepotIndex(tile)
rubidium <rubidium@openttd.org>
parents: 12860
diff changeset
3940 v->current_order.GetDestination() != depot &&
7967
e30c5ec2d272 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13 <skidd13@openttd.org>
parents: 7932
diff changeset
3941 !Chance16(3, 16)) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3942 return;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3943 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3944
17693
cdeed5a57cdb (svn r22473) -Codechange: Automatic orders are better called implicit orders as no real order influencing path finding is added
planetmaker <planetmaker@openttd.org>
parents: 17564
diff changeset
3945 SetBit(v->gv_flags, GVF_SUPPRESS_IMPLICIT_ORDERS);
13001
c2298b1d3f83 (svn r17495) -Codechange: replace 'Depot::Get(GetDepotIndex(tile))->index' with GetDepotIndex(tile)
rubidium <rubidium@openttd.org>
parents: 12860
diff changeset
3946 v->current_order.MakeGoToDepot(depot, ODTFB_SERVICE);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3947 v->dest_tile = tfdd.tile;
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
3948 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3949 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3950
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3951 /** Update day counters of the train vehicle. */
8467
284a76aa2868 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx <glx@openttd.org>
parents: 8426
diff changeset
3952 void Train::OnNewDay()
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3953 {
18007
c7a5373f1a03 (svn r22816) -Feature(ette): [NewGRF] Also age wagons and articulated parts.
frosch <frosch@openttd.org>
parents: 17909
diff changeset
3954 AgeVehicle(this);
c7a5373f1a03 (svn r22816) -Feature(ette): [NewGRF] Also age wagons and articulated parts.
frosch <frosch@openttd.org>
parents: 17909
diff changeset
3955
8467
284a76aa2868 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx <glx@openttd.org>
parents: 8426
diff changeset
3956 if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
284a76aa2868 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx <glx@openttd.org>
parents: 8426
diff changeset
3957
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3958 if (this->IsFrontEngine()) {
8467
284a76aa2868 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx <glx@openttd.org>
parents: 8426
diff changeset
3959 CheckVehicleBreakdown(this);
284a76aa2868 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx <glx@openttd.org>
parents: 8426
diff changeset
3960
284a76aa2868 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx <glx@openttd.org>
parents: 8426
diff changeset
3961 CheckIfTrainNeedsService(this);
284a76aa2868 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx <glx@openttd.org>
parents: 8426
diff changeset
3962
284a76aa2868 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx <glx@openttd.org>
parents: 8426
diff changeset
3963 CheckOrders(this);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3964
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3965 /* update destination */
8836
890a77315801 (svn r12584) -Codechange: do not access the order type directly.
rubidium <rubidium@openttd.org>
parents: 8832
diff changeset
3966 if (this->current_order.IsType(OT_GOTO_STATION)) {
12510
3e976121ced2 (svn r16947) -Codechange: use TileArea instead of train_tile, trainst_w and trainst_h.
rubidium <rubidium@openttd.org>
parents: 12502
diff changeset
3967 TileIndex tile = Station::Get(this->current_order.GetDestination())->train_station.tile;
10488
a9ecb9be9814 (svn r14743) -Codechange: use INVALID_TILE to indicate station doesn't have queried facility (or station/roadstop is invalid) instead of 0 (Yexo)
smatz <smatz@openttd.org>
parents: 10444
diff changeset
3968 if (tile != INVALID_TILE) this->dest_tile = tile;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3969 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3970
8556
8aae08d562cc (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz <smatz@openttd.org>
parents: 8554
diff changeset
3971 if (this->running_ticks != 0) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3972 /* running costs */
10744
0118ca646690 (svn r15077) -Codechange: enumify DAYS_IN_YEAR and DAYS_IN_LEAP_YEAR
smatz <smatz@openttd.org>
parents: 10696
diff changeset
3973 CommandCost cost(EXPENSES_TRAIN_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
8556
8aae08d562cc (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz <smatz@openttd.org>
parents: 8554
diff changeset
3974
8aae08d562cc (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz <smatz@openttd.org>
parents: 8554
diff changeset
3975 this->profit_this_year -= cost.GetCost();
8aae08d562cc (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz <smatz@openttd.org>
parents: 8554
diff changeset
3976 this->running_ticks = 0;
8467
284a76aa2868 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx <glx@openttd.org>
parents: 8426
diff changeset
3977
10207
c54d140df948 (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium <rubidium@openttd.org>
parents: 10192
diff changeset
3978 SubtractMoneyFromCompanyFract(this->owner, cost);
8467
284a76aa2868 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx <glx@openttd.org>
parents: 8426
diff changeset
3979
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
3980 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
3981 SetWindowClassesDirty(WC_TRAINS_LIST);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3982 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3983 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3984 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3985
18855
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3986 /**
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3987 * Get the tracks of the train vehicle.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3988 * @return Current tracks of the vehicle.
a0d7e20ebafe (svn r23704) -Doc: Doxygen comment fixes and additions.
alberth <alberth@openttd.org>
parents: 18839
diff changeset
3989 */
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3990 Trackdir Train::GetVehicleTrackdir() const
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3991 {
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3992 if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3993
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3994 if (this->track == TRACK_BIT_DEPOT) {
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3995 /* We'll assume the train is facing outwards */
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3996 return DiagDirToDiagTrackdir(GetRailDepotDirection(this->tile)); // Train in depot
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3997 }
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3998
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
3999 if (this->track == TRACK_BIT_WORMHOLE) {
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
4000 /* train in tunnel or on bridge, so just use his direction and assume a diagonal track */
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
4001 return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
4002 }
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
4003
11986
78abb48a4fba (svn r16392) -Codechange: move some variables (the ones that aren't caches) from VehicleRail to Train
rubidium <rubidium@openttd.org>
parents: 11985
diff changeset
4004 return TrackDirectionToTrackdir(FindFirstTrack(this->track), this->direction);
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
4005 }