annotate src/train_cmd.cpp @ 18692:90d395fc678d draft

(svn r23540) -Codechange: unify and document vehicle widgets
author rubidium <rubidium@openttd.org>
date Fri, 16 Dec 2011 16:58:55 +0000
parents 1972f6346144
children 1fa19d70aea9
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 "vehicle_gui.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_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
22 #include "newgrf_text.h"
6643
18d58b36b9b3 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium <rubidium@openttd.org>
parents: 6608
diff changeset
23 #include "group.h"
8114
2d6af5d7a142 (svn r11675) -Codechange: split the string types from the string functions.
rubidium <rubidium@openttd.org>
parents: 8108
diff changeset
24 #include "strings_func.h"
17277
5b204f51b151 (svn r22017) -Codechange: move MarkTileDirtyByTile to viewport_func.h
rubidium <rubidium@openttd.org>
parents: 17236
diff changeset
25 #include "viewport_func.h"
8131
e300ac8001ae (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium <rubidium@openttd.org>
parents: 8119
diff changeset
26 #include "window_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
27 #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
28 #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
29 #include "ai/ai.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
30 #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
31 #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
32 #include "gamelog.h"
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
33 #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
34 #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
35 #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
36 #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
37 #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
38 #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
39 #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
40
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
41 #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
42 #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
43
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
44 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
45 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
46 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
47 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
48 static void CheckIfTrainNeedsService(Train *v);
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
49 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
50
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
51 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
52 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
53
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 /**
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
56 * 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
57 *
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
58 * @param direction vehicle direction
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
59 * @param track vehicle track bits
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
60 * @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
61 */
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
62 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
63 {
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
64 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
65
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
66 DiagDirection diagdir = DirToDiagDir(direction);
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
67
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
68 /* 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
69 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
70 diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT);
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
71 }
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
72
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
73 return diagdir;
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
74 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
75
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
76
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
77 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
78 * 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
79 * @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
80 * @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
81 */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
82 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
83 {
12415
6a77d1df56e2 (svn r16849) -Codechange: replace GetCargo() by CargoSpec::Get()
smatz <smatz@openttd.org>
parents: 12351
diff changeset
84 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
85 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
86 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
87
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
88 /** 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
89 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
90 {
12035
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
91 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
92 bool first = true;
12035
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
93
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
94 FOR_ALL_TRAINS(v) {
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
95 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
96 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
97 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
98 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
99 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
100 (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
101 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
102 SetDParam(1, v->owner);
14645
b7a22979b84e (svn r19224) -Codechange: change parameters passed to ShowErrorMessage() a bit
smatz <smatz@openttd.org>
parents: 14520
diff changeset
103 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
104
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
105 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
106 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
107 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
108 }
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
109 /* 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
110 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
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 }
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
115 }
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
116 }
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
117
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
118 /**
15826
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
119 * Update visual effect, power and acceleration caches.
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
120 * Called when a vehicle in the consist enters a different railtype.
15825
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
121 */
15826
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
122 void Train::RailtypeChanged()
15825
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
123 {
15826
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
124 for (Train *u = this; u != NULL; u = u->Next()) {
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
125 /* The wagon-is-powered-state should not change, so the weight does not change. */
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
126 u->UpdateVisualEffect(false);
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
127 }
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
128 this->PowerChanged();
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
129 if (this->IsFrontEngine()) this->UpdateAcceleration();
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
130 }
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
131
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
132 /**
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
133 * 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
134 * 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
135 * 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
136 * @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
137 */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
138 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
139 {
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
140 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
141
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
142 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
143
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
144 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
145 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
146 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
147 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
148
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
149 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
150
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
151 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
152 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
153
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
154 /* 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
155 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
156
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
157 /* 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
158 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
159 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
160
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
161 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
162
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
163 /* 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
164 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
165 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
166 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
167 }
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
168
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
169 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
170 /* 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
171 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
172 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
173 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
174 }
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
175
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
176 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
177 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
178 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
179
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
180 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
181
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
182 /* 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
183 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
184
11085
8da1855e9f14 (svn r15428) -Codechange: consistently use colour instead of having both color and colour.
rubidium <rubidium@openttd.org>
parents: 11018
diff changeset
185 /* 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
186 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
187
15825
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
188 /* 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
189 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
190
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
191 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
192 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
193 /* 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
194 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
195 } 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
196 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
197 }
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
198
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
199 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
200 /* 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
201 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
202 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
203 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
204 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
205
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
206 /* 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
207 * 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
208 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
209 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
210 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
211 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
212
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
213 /* 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
214 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
215 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
216 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
217 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
218 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
219
18337
f3ce001bc1d1 (svn r23173) -Codechange: Rename GetVehicleCapacity() to Engine::DetermineCapacity().
frosch <frosch@openttd.org>
parents: 18335
diff changeset
220 u->cargo_cap = e_u->DetermineCapacity(u);
17909
4d6b4b6d6069 (svn r22713) -Feature: [NewGRF] Per vehicle custom cargo ageing period.
michi_cc <michi_cc@openttd.org>
parents: 17804
diff changeset
221 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
222
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
223 /* check the vehicle length (callback) */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
224 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
225 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
226 /* 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
227 veh_len = GetVehicleProperty(u, PROP_TRAIN_SHORTEN_FACTOR, CALLBACK_FAILED);
1f35799d5a8b (svn r23150) -Change: [NewGRF v8] Deprecate callback 11, and use callback 36 instead.
frosch <frosch@openttd.org>
parents: 18312
diff changeset
228 } 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
229 /* 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
230 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
231 }
1f35799d5a8b (svn r23150) -Change: [NewGRF v8] Deprecate callback 11, and use callback 36 instead.
frosch <frosch@openttd.org>
parents: 18312
diff changeset
232 if (veh_len != CALLBACK_FAILED && veh_len >= VEHICLE_LENGTH) {
1f35799d5a8b (svn r23150) -Change: [NewGRF v8] Deprecate callback 11, and use callback 36 instead.
frosch <frosch@openttd.org>
parents: 18312
diff changeset
233 ErrorUnknownCallbackResult(e_u->GetGRFID(), CBID_VEHICLE_LENGTH, veh_len);
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 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
235 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
236 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
237
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
238 /* 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
239 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
240
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
241 /* 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
242 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
243
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
244 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
245 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
246 u->InvalidateNewGRFCache();
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
247 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
248
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
249 /* 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
250 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
251 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
252 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
253
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
254 /* 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
255 this->CargoChanged();
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
256
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
257 if (this->IsFrontEngine()) {
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
258 this->UpdateAcceleration();
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
259 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
17489
7e48879a9608 (svn r22244) -Codechange: Make vehicle windows handle command-/GUI-scope invalidations themself (from autoreplace and refitting).
frosch <frosch@openttd.org>
parents: 17452
diff changeset
260 InvalidateWindowData(WC_VEHICLE_REFIT, this->index);
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
261 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
262 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
263
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 /**
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 * 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
266 * 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
267 * @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
268 * @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
269 * @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
270 * @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
271 * @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
272 * @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
273 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
274 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
275 {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11920
diff changeset
276 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
277 *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
278 *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
279
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 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
281 * 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
282 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
283 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
284 /* 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
285 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
286 } 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
287 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
288 }
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 /* 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
291 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
292 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
293 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
294
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
295 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
296 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
297 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
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 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
300 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
301 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
302
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
303 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
304 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
305 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
306 }
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
307
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
308 /* 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
309 * 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
310 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
311 }
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
312
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
313
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
314 /**
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
315 * 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
316 * @return imposed speed limit
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
317 */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
318 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
319 {
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
320 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
321
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
322 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
323 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
324
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
325 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
326
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
327 int curvecount[2] = {0, 0};
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
328
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
329 /* first find the curve speed limit */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
330 int numcurve = 0;
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
331 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
332 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
333 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
334 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
335 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
336 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
337
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 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
339 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
340
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
341 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
342 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
343 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
344 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
345 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
346 sum += pos - lastpos;
18064
0108a6ec2d68 (svn r22879) -Fix: Miscalculation of train curve speed limits. (monoid)
terkhen <terkhen@openttd.org>
parents: 18007
diff changeset
347 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
348 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
349 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
350 }
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
351 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
352 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
353
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
354 /* 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
355 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
356 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
357 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
358 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
359
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
360 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
361 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
362 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
363 } else {
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
364 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
365 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
366 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
367 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
368
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
369 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
370 /* 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
371 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
372 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
373
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
374 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
375 /* 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
376 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
377 }
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
378 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
379
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
380 return max_speed;
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
381 }
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
382
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
383 /**
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
384 * 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
385 * @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
386 */
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
387 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
388 {
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
389 int max_speed = this->tcache.cached_max_curve_speed;
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
390 assert(max_speed == this->GetCurveSpeedLimit());
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
391
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
392 if (IsRailStationTile(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
393 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
394 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
395 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
396 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
397 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
398
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
399 /* 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
400 * 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
401 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
402
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
403 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
404 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
405
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
406 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
407 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
408 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
409 }
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
410
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
411 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
412 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
413 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
414 }
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
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
417 for (const Train *u = this; u != NULL; u = u->Next()) {
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
418 if (u->track == TRACK_BIT_DEPOT) {
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 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
420 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
421 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
422 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
423
16785
775f357828e3 (svn r21518) -Codechange: Rename AccelerationCache to GroundVehicleCache.
terkhen <terkhen@openttd.org>
parents: 16776
diff changeset
424 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
425 }
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
426
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
427 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
428 {
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
429 assert(this->IsFrontEngine());
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
430
16785
775f357828e3 (svn r21518) -Codechange: Rename AccelerationCache to GroundVehicleCache.
terkhen <terkhen@openttd.org>
parents: 16776
diff changeset
431 uint power = this->gcache.cached_power;
775f357828e3 (svn r21518) -Codechange: Rename AccelerationCache to GroundVehicleCache.
terkhen <terkhen@openttd.org>
parents: 16776
diff changeset
432 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
433 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
434 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
435 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
436
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
437 /**
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 * 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
439 * @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
440 * @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
441 */
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
442 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
443 {
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
444 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
445 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
446
18238
3141f1ed78eb (svn r23074) -Codechange: Add Vehicle::GetEngine() to simplify code.
frosch <frosch@openttd.org>
parents: 18159
diff changeset
447 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
448 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
449 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
450 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
451 }
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
452
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 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
454 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
455 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
456 }
17451
2dbd537df54d (svn r22205) -Codechange: replace magic number with constant
rubidium <rubidium@openttd.org>
parents: 17399
diff changeset
457 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
458 }
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
459
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
460 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
461 {
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
462 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
463 }
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
464
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
465 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
466 {
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
467 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
468 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
469
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
470 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
471
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
472 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
473 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
474 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
475
18238
3141f1ed78eb (svn r23074) -Codechange: Add Vehicle::GetEngine() to simplify code.
frosch <frosch@openttd.org>
parents: 18159
diff changeset
476 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
477 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
478
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
479 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
480
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 if (this->cargo.Count() >= this->cargo_cap / 2U) sprite += _wagon_full_adder[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
482
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 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
484 }
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
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
486 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
487 {
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
488 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
489 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
490 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
491
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 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
493 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
494 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
495 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
496 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
497 }
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
498 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
499 }
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
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
501 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
502 }
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 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
505
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
506 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
507 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
508
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
509 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
510 {
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
511 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
512 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
513 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
514
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
515 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
516 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
517 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
518 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
519
18472
11637619aa76 (svn r23316) -Feature: Add ability to zoom in to 2x and 4x level.
peter1138 <peter1138@openttd.org>
parents: 18446
diff changeset
520 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
521
13597
706013b8e441 (svn r18121) -Codechange: pass the maximum left/right location to the Draw*Engine functions
rubidium <rubidium@openttd.org>
parents: 13492
diff changeset
522 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
523 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
524 } 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
525 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
526 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
527 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
528 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
529 }
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
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
532 /**
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
533 * 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
534 * @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
535 * @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
536 * @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
537 * @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
538 * @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
539 */
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
540 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
541 {
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
542 const RailVehicleInfo *rvi = &e->u.rail;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
543
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
544 /* 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
545 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
546
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
547 if (flags & DC_EXEC) {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
548 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
549 *ret = v;
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
550 v->spritenum = rvi->image_index;
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
551
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
552 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
553 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
554
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
555 DiagDirection dir = GetRailDepotDirection(tile);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
556
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
557 v->direction = DiagDirToDir(dir);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
558 v->tile = tile;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
559
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
560 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
561 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
562
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
563 v->x_pos = x;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
564 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
565 v->z_pos = GetSlopePixelZ(x, y);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
566 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
567 v->track = TRACK_BIT_DEPOT;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
568 v->vehstatus = VS_HIDDEN | VS_DEFPAL;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
569
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
570 v->SetWagon();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
571
13934
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
572 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
573 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
574
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
575 v->cargo_type = e->GetDefaultCargoType();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
576 v->cargo_cap = rvi->capacity;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
577
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
578 v->railtype = rvi->railtype;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
579
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
580 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
581 v->cur_image = SPR_IMG_QUERY;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
582 v->random_bits = VehicleRandomBits();
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
583
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
584 v->group_id = DEFAULT_GROUP;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
585
13219
39e3a60fd47f (svn r17726) -Cleanup: Remove some unneeded/unused parameters.
frosch <frosch@openttd.org>
parents: 13218
diff changeset
586 AddArticulatedParts(v);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
587
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
588 _new_vehicle_id = v->index;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
589
11326
557dd16a7c24 (svn r15676) -Codechange: remove the need for BeginVehicleMove and merge VehiclePositionChanged and EndVehicleMove.
rubidium <rubidium@openttd.org>
parents: 11268
diff changeset
590 VehicleMove(v, false);
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
591 v->First()->ConsistChanged(false);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
592 UpdateTrainGroupID(v->First());
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
593
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
594 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
595
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
596 /* 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
597 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
598 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
599 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
600 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
601 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
602 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
603 !(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
604 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
605 break;
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
606 }
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
607 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
608 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
609
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
610 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
611 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
612
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
613 /** 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
614 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
615 {
12035
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
616 const Train *v;
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
617 FOR_ALL_TRAINS(v) {
12301
218a41d3d557 (svn r16718) -Codechange: make IsFreeWagon() member of Train
smatz <smatz@openttd.org>
parents: 12300
diff changeset
618 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
619 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
620 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
621 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
622 break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
623 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
624 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
625 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
626
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
627 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
628 {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
629 Train *u = new Train();
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
630 v->value >>= 1;
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
631 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
632 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
633 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
634 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
635 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
636 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
637 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
638 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
639 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
640 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
641 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
642 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
643 u->cargo_cap = v->cargo_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
644 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
645 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
646 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
647 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
648 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
649 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
650 u->SetMultiheaded();
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
651 v->SetNext(u);
11326
557dd16a7c24 (svn r15676) -Codechange: remove the need for BeginVehicleMove and merge VehiclePositionChanged and EndVehicleMove.
rubidium <rubidium@openttd.org>
parents: 11268
diff changeset
652 VehicleMove(u, false);
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
653
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
654 /* 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
655 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
656 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
657 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
658
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
659 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
660 * 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
661 * @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
662 * @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
663 * @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
664 * @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
665 * @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
666 * @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
667 */
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
668 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
669 {
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
670 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
671
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
672 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
673
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
674 /* 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
675 * 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
676 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
677
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
678 if (flags & DC_EXEC) {
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
679 DiagDirection dir = GetRailDepotDirection(tile);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
680 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
681 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
682
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
683 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
684 *ret = v;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
685 v->direction = DiagDirToDir(dir);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
686 v->tile = tile;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
687 v->owner = _current_company;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
688 v->x_pos = x;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
689 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
690 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
691 v->track = TRACK_BIT_DEPOT;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
692 v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
693 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
694 v->cargo_type = e->GetDefaultCargoType();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
695 v->cargo_cap = rvi->capacity;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
696 v->last_station_visited = INVALID_STATION;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
697
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
698 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
699 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
700
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
701 v->reliability = e->reliability;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
702 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
703 v->max_age = e->GetLifeLengthInDays();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
704
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
705 v->railtype = rvi->railtype;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
706 _new_vehicle_id = v->index;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
707
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
708 v->service_interval = 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
709 v->date_of_last_service = _date;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
710 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
711 v->cur_image = SPR_IMG_QUERY;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
712 v->random_bits = VehicleRandomBits();
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 (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
715
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
716 v->group_id = DEFAULT_GROUP;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
717
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
718 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
719 v->SetEngine();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
720
11326
557dd16a7c24 (svn r15676) -Codechange: remove the need for BeginVehicleMove and merge VehiclePositionChanged and EndVehicleMove.
rubidium <rubidium@openttd.org>
parents: 11268
diff changeset
721 VehicleMove(v, false);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
722
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
723 if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
724 AddRearEngineToMultiheadedTrain(v);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
725 } else {
13219
39e3a60fd47f (svn r17726) -Cleanup: Remove some unneeded/unused parameters.
frosch <frosch@openttd.org>
parents: 13218
diff changeset
726 AddArticulatedParts(v);
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
727 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
728
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
729 v->ConsistChanged(false);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
730 UpdateTrainGroupID(v);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
731
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
732 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
733 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
734 }
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
735
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
736 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
737 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
738
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
739 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
740 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
741
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
742
13948
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
743 bool Train::IsInDepot() 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
744 {
13948
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
745 /* Is the front engine stationary in the depot? */
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
746 if (!IsRailDepotTile(this->tile) || this->cur_speed != 0) return false;
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
747
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
748 /* Check whether the rest is also already trying to enter the depot. */
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
749 for (const Train *v = this; v != NULL; v = v->Next()) {
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
750 if (v->track != TRACK_BIT_DEPOT || v->tile != this->tile) return false;
13933
a71da8c8c5e2 (svn r18465) -Codechange: simplify CheckTrainInDepot and remove some unneeded wrapper functions
rubidium <rubidium@openttd.org>
parents: 13931
diff changeset
751 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
752
13948
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
753 return true;
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
754 }
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
755
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
756 bool Train::IsStoppedInDepot() const
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
757 {
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
758 /* Are we stopped? Of course wagons don't really care... */
13948
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
759 if (this->IsFrontEngine() && !(this->vehstatus & VS_STOPPED)) return false;
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
760 return this->IsInDepot();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
761 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
762
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
763 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
764 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
765 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
766 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
767
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
768 Train *dst;
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
769 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
770 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
771 /* 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
772 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
773 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
774 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
775 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
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 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
778 }
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 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
781 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
782
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
783 /** 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
784 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
785
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
786 /**
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
787 * 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
788 * @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
789 * @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
790 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
791 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
792 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
793 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
794 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
795
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
796 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
797 * 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
798 * @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
799 */
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
800 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
801 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
803 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
804
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
806 /* 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
807 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
808 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
809 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
810 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
811 } 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
812 /* 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
813 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
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 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
816 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
817 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /**
15002
baf0e12ad2b4 (svn r19614) -Codechange: "it's" => "its" where appropriate
smatz <smatz@openttd.org>
parents: 14898
diff changeset
820 * 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
821 * @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
822 * @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
823 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
825 {
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
826 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
827
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
829 * 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
830 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
831
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
833 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
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 * 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
838 * @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
839 * @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
840 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
842 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
843 /* 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
844 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
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 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
847 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
848 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 * 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
852 * 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
853 * @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
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 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
856 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
858 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
859
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
861 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
862 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
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 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
865
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
867 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
868 /* 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
869 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
870 }
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
871 }
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
872
13926
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
873 /**
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
874 * 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
875 * @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
876 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
878 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
880 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
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 /* 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
883 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
884
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
885 /* 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
886 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
887 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
888 } 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
889 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
890 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
891 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
894 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
895 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
896 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
897 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 * 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
902 * @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
903 * @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
904 * @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
905 * @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
906 * @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
907 * @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
908 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
910 {
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
911 /* 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
912 * 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
913 * 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
914 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
915 (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
916 (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
917 (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
918 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
919 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
922 * 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
923 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
924
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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_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
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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
929 * 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
930 * @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
931 * @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
932 */
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
933 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
934 {
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
935 /* 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
936 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
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 /* 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
939 * 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
940 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
941
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
943 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
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 /* 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
946 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
947 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
948
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
949 /* 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
950 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
951
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
952 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
953 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
954
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
955 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
956
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
958 * 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
959 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
960
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
962 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
963 /* 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
964 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
965 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
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 /* 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
968 * 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
969 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
970
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
971 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
972
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
974 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
975
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
977 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
978 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
979
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 (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
981 /* 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
982 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
983
18306
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
984 if (t->GetGRF()->grf_version < 8) {
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
985 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
986 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
987 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
988 } else {
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
989 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
990 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
991 } else {
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
992 switch (callback) {
18335
f95316892277 (svn r23171) -Fix (r23142): Fix comment.
frosch <frosch@openttd.org>
parents: 18314
diff changeset
993 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
994 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
995 break;
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
996
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
997 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
998 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
999 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
1000 break;
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1001 }
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1002 }
21d2cc91c3ce (svn r23142) -Change: [NewGRF v8] Unify the return values of callbacks returning D0xx texts.
frosch <frosch@openttd.org>
parents: 18276
diff changeset
1003 }
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
1004
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1005 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
1006 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1007 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1008
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1009 /* 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
1010 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
1011 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
1012 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
1013 }
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
1014
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
1015 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
1016 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
1017 }
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
1018
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
1019 /**
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
1020 * 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
1021 * @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
1022 * @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
1023 * @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
1024 * @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
1025 * @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
1026 * @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
1027 * @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
1028 */
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
1029 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
1030 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1031 /* 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
1032 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
1033 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
1034 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
1035 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
1036
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1037 /* 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
1038 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
1039 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 * 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
1043 * @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
1044 * @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
1045 * @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
1046 * @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
1047 * @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
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 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
1050 {
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
1051 /* 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
1052 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
1053 /* 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
1054 * 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
1055 *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
1056 } 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
1057 /* 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
1058 * 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
1059 *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
1060 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
1063 /* 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
1064 * 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
1065 * 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
1066 * 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
1067 * 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
1068 * 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
1069 *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
1070 (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
1071 }
77e7637c04c8 (svn r18459) -Codechange: move the new train validity checking (attach callbacks) to a separate function
rubidium <rubidium@openttd.org>
parents: 13926
diff changeset
1072
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
1073 /* 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
1074 * 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
1075 * 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
1076 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
1077 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
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 /* 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
1080 * 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
1081 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
1082 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
1083 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 * 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
1087 * 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
1088 * @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
1089 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1090 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
1091 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
1093 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
1094
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
1096 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
1097 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
1098
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
1100 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
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 /* Update the refit button and window */
17489
7e48879a9608 (svn r22244) -Codechange: Make vehicle windows handle command-/GUI-scope invalidations themself (from autoreplace and refitting).
frosch <frosch@openttd.org>
parents: 17452
diff changeset
1103 InvalidateWindowData(WC_VEHICLE_REFIT, head->index);
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
1104 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
1105
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
1107 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
1108 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
1109 }
77e7637c04c8 (svn r18459) -Codechange: move the new train validity checking (attach callbacks) to a separate function
rubidium <rubidium@openttd.org>
parents: 13926
diff changeset
1110
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1111 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1112 * 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
1113 * @param tile unused
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1114 * @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
1115 * 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
1116 * @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
1117 * - 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
1118 * - 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
1119 * @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
1120 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1121 * @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
1122 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1123 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
1124 {
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
1125 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
1126 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
1127 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
1128
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1129 Train *src = Train::GetIfValid(s);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1130 if (src == NULL) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1131
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1132 CommandCost ret = CheckOwnership(src->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1133 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
1134
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
1135 /* 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
1136 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
1137
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1138 /* 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
1139 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
1140 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
1141 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
1142 } else {
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1143 dst = Train::GetIfValid(d);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1144 if (dst == NULL) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1145
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1146 CommandCost ret = CheckOwnership(dst->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1147 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
1148
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
1149 /* 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
1150 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
1151 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1152
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1153 /* 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
1154 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
1155 if (dst != NULL) {
12450
af266072d46c (svn r16884) -Codechange: Add Train::GetFirstEnginePart() and use it.
frosch <frosch@openttd.org>
parents: 12433
diff changeset
1156 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
1157 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1158
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1159 /* 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
1160 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
1161
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1162 /* 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
1163 Train *src_head = src->First();
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1164 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
1165 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
1166 dst_head = dst->First();
6136
f05e23ab634f (svn r8878) -Fix
tron <tron@openttd.org>
parents: 6114
diff changeset
1167 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
1168 /* 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
1169 dst = dst->GetLastEnginePart();
6136
f05e23ab634f (svn r8878) -Fix
tron <tron@openttd.org>
parents: 6114
diff changeset
1170 } else {
f05e23ab634f (svn r8878) -Fix
tron <tron@openttd.org>
parents: 6114
diff changeset
1171 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
1172 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1173
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
1174 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
1175
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
1176 /* 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
1177 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
1178
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
1179 /* 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
1180 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
1181
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
1182 /* 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
1183 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
1184
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1185 /* 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
1186 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
1187
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1188 /* 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
1189 * 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
1190 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
1191 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
1192
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1193 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
1194 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
1195
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1196 /* 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
1197 * 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
1198 * 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
1199 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
1200 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
1201
18159
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1202 if (flags & DC_EXEC) {
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1203 /* Remove old heads from the statistics */
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1204 if (original_src_head != NULL && original_src_head->IsFrontEngine()) GroupStatistics::CountVehicle(original_src_head, -1);
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1205 if (original_dst_head != NULL && original_dst_head->IsFrontEngine()) GroupStatistics::CountVehicle(original_dst_head, -1);
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1206 }
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1207
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
1208 /* (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
1209 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
1210
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
1211 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
1212 /* 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
1213 * 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
1214 * 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
1215 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
1216 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
1217 /* 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
1218 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
1219 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
1220 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
1221 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1222 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1223
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1224 /* 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
1225 if (flags & DC_EXEC) {
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
1226 /* 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
1227 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
1228 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
1229
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
1231 * 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
1232 * 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
1233 * 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
1234 * 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
1235 * 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
1236 * 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
1237 * 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
1238 * 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
1239 * 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
1240 * 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
1241 * 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
1242 * 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
1243 * 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
1244 * 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
1245 * 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
1246 * 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
1247 * 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
1248 * 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
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 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
1251 /* 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
1252 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
1253 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
1254 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
1255 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
1256 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
1257 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
1258
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1259 /* 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
1260 * 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
1261 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
1262 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
1263 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
1264 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1265
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
1266 /* 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
1267 * 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
1268 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
1269 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
1270 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
1271 }
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
1272
18159
de0749262352 (svn r22984) -Feature: Display profit icons for groups in the group GUI.
frosch <frosch@openttd.org>
parents: 18153
diff changeset
1273 /* 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
1274 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
1275 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
1276
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
1277 /* 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
1278 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
1279 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
1280
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
1282 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
1283 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
1284 } 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
1285 /* 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
1286 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
1287 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
1288 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1289
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
1290 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
1291 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1292
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1293 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1294 * 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
1295 * @param flags type of operation
15850
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1296 * @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
1297 * @param data the selling mode
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1298 * - 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
1299 * - 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
1300 * 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
1301 * @param user the user for the order backup.
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1302 * @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
1303 */
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
1304 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
1305 {
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
1306 /* 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
1307 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
1308
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
1309 /* 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
1310 bool sell_chain = HasBit(data, 0);
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1311
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1312 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
1313 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
1314
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
1315 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
1316
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
1317 /* 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
1318 * 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
1319 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
1320 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
1321
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
1322 /* 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
1323 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
1324 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
1325
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
1326 /* 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
1327 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
1328
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
1329 /* 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
1330 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
1331 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
1332 /* 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
1333 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
1334 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
1335 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1336
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
1337 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
1338 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
1339
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
1340 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
1341 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
1342 }
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
1343
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
1344 /* 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
1345 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
1346 /* 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
1347 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
1348
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 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
1350 /* 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
1351 * '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
1352 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
1353 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
1354 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
1355
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 /* 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
1357 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
1358 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
1359
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
1360 /* 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
1361 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
1362 } 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
1363 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
1364 }
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
1365
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
1366 /* 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
1367 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
1368
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 /* 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
1370 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
1371 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
1372
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 /* 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
1374 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
1375 } 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
1376 /* 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
1377 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
1378 }
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
1379
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1380 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
1381 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1382
6558
2fc024ee8217 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium <rubidium@openttd.org>
parents: 6554
diff changeset
1383 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
1384 {
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
1385 /* 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
1386 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
1387 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
1388 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
1389 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
1390 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
1391 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
1392 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
1393
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
1394 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
1395 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
1396 {
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
1397 // 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
1398 -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
1399 -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
1400 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
1401 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
1402 };
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
1403
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
1404 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
1405
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
1406 /* 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
1407 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
1408 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
1409 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
1410 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
1411 } 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
1412 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
1413 /* 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
1414 * 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
1415 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
1416 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
1417 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
1418 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
1419 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
1420
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 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
1422 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
1423 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
1424 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
1425 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
1426
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 /* 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
1428 * 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
1429 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
1430 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
1431 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
1432 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
1433 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
1434
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 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
1436 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
1437 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
1438 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
1439 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
1440
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 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
1442 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
1443 }
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 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1445 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1446
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
1447 /** Mark a train as stuck and stop it if it isn't stopped right now. */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1448 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
1449 {
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
1450 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
1451 /* 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
1452 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
1453
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
1454 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
1455
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
1456 /* 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
1457 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
1458 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
1459 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
1460
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
1461 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
1462 }
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
1463 }
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
1464
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
1465 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
1466 {
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
1467 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
1468 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
1469
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1470 /* Clear the flags */
14753
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1471 ClrBit(*swap_flag1, GVF_GOINGUP_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1472 ClrBit(*swap_flag1, GVF_GOINGDOWN_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1473 ClrBit(*swap_flag2, GVF_GOINGUP_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1474 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
1475
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1476 /* 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
1477 if (HasBit(flag1, GVF_GOINGUP_BIT)) {
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1478 SetBit(*swap_flag2, GVF_GOINGDOWN_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1479 } 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
1480 SetBit(*swap_flag2, GVF_GOINGUP_BIT);
14768
f31d8dc443fc (svn r19356) -Fix: Whitespace.
frosch <frosch@openttd.org>
parents: 14754
diff changeset
1481 }
14753
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1482 if (HasBit(flag2, GVF_GOINGUP_BIT)) {
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1483 SetBit(*swap_flag1, GVF_GOINGDOWN_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1484 } 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
1485 SetBit(*swap_flag1, GVF_GOINGUP_BIT);
14768
f31d8dc443fc (svn r19356) -Fix: Whitespace.
frosch <frosch@openttd.org>
parents: 14754
diff changeset
1486 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1487 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1488
17098
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1489 /**
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1490 * 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
1491 * @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
1492 */
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1493 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
1494 {
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1495 /* 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
1496 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
1497
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1498 /* 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
1499 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
1500 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
1501 } 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
1502 /* 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
1503 * 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
1504 * 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
1505 * 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
1506 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
1507 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
1508 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
1509 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
1510 /* 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
1511 * "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
1512 * because we are at the border of the 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
1513 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
1514 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
1515 }
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
1516 }
17098
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1517 }
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1518
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1519 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
1520 }
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1521
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
1522 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
1523 {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1524 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
1525
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1526 /* 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
1527 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
1528 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
1529
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1530 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
1531 /* 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
1532 {
14678
68f4a5f222ef (svn r19257) -Codechange: minor coding style fix
smatz <smatz@openttd.org>
parents: 14645
diff changeset
1533 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
1534 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
1535 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
1536 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1537
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
1538 Swap(a->track, b->track);
14680
f3dfa0c1c278 (svn r19260) -Codechange: another minor coding style fix
smatz <smatz@openttd.org>
parents: 14678
diff changeset
1539 Swap(a->direction, b->direction);
5733
0b9175cb534a (svn r8276) -Fix
tron <tron@openttd.org>
parents: 5668
diff changeset
1540 Swap(a->x_pos, b->x_pos);
0b9175cb534a (svn r8276) -Fix
tron <tron@openttd.org>
parents: 5668
diff changeset
1541 Swap(a->y_pos, b->y_pos);
0b9175cb534a (svn r8276) -Fix
tron <tron@openttd.org>
parents: 5668
diff changeset
1542 Swap(a->tile, b->tile);
0b9175cb534a (svn r8276) -Fix
tron <tron@openttd.org>
parents: 5668
diff changeset
1543 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
1544
14753
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1545 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
1546
17098
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1547 UpdateStatusAfterSwap(a);
5035955400bc (svn r21835) -Codechange: use UpdateStatusAfterSwap() instead of copying the same code three times
smatz <smatz@openttd.org>
parents: 17063
diff changeset
1548 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
1549 } 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
1550 /* 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
1551 * 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
1552 * 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
1553 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
1554 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
1555 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1556
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
1557 /* Update power of the train in case tiles were different rail type. */
15826
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
1558 v->RailtypeChanged();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1559 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1560
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
1561
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1562 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1563 * 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
1564 * @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
1565 * @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
1566 */
9775
c3caa51b9888 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium <rubidium@openttd.org>
parents: 9774
diff changeset
1567 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
1568 {
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
1569 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
1570 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1571
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1572
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1573 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1574 * 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
1575 * @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
1576 * @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
1577 * @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
1578 */
9775
c3caa51b9888 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium <rubidium@openttd.org>
parents: 9774
diff changeset
1579 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
1580 {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1581 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
1582
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1583 Train *t = Train::From(v);
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1584 if (!t->IsFrontEngine()) return NULL;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1585
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1586 TileIndex tile = *(TileIndex *)data;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1587
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1588 if (TrainApproachingCrossingTile(t) != tile) return NULL;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1589
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1590 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
1591 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1592
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
1593
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1594 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1595 * 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
1596 * @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
1597 * @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
1598 * @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
1599 */
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
1600 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
1601 {
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
1602 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
1603
8598
ef2c93a9589d (svn r12179) -Codechange: use GetCrossingRailTrack() and GetCrossingRailAxis() to improve code readability
smatz <smatz@openttd.org>
parents: 8597
diff changeset
1604 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
1605 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
1606
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
1607 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
1608
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 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
1610 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
1611
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
1612 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
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
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
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 /**
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 * 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
1618 * @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
1619 * @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
1620 * @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
1621 */
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
1622 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
1623 {
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1624 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
1625
9824
fe3562a51648 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9822
diff changeset
1626 /* 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
1627 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
1628
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
1629 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
1630 if (new_state && sound) {
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
1631 SndPlayTileFx(SND_0E_LEVEL_CROSSING, 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
1632 }
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
1633 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
1634 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
1635 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1636 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1637
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
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1639 /**
8356
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1640 * 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
1641 * @param tile tile with crossing
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1642 * @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
1643 */
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1644 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
1645 {
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1646 if (!IsCrossingBarred(tile)) {
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1647 BarCrossing(tile);
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1648 SndPlayTileFx(SND_0E_LEVEL_CROSSING, tile);
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1649 MarkTileDirtyByTile(tile);
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1650 }
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1651 }
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1652
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1653
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1654 /**
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1655 * 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
1656 * 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
1657 * @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
1658 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1659 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
1660 {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1661 Train *base = v;
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1662 Train *first = base; // first vehicle to move
12460
4acae7d6f5ed (svn r16894) -Codechange: Add [Specialised]Vehicle::Last().
frosch <frosch@openttd.org>
parents: 12450
diff changeset
1663 Train *last = v->Last(); // last vehicle to move
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1664 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
1665
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1666 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
1667 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
1668 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
1669
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
1670 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
1671
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
1672 /* 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
1673 * 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
1674 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
1675
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
1676 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
1677 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
1678 }
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
1679 }
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
1680
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
1681
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
1682 /**
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
1683 * 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
1684 * 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
1685 * @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
1686 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1687 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
1688 {
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
1689 /* 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
1690 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
1691 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
1692 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
1693 }
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
1694
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1695 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
1696
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
1697 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
1698 /* '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
1699 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
1700
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
1701 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
1702 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
1703 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
1704 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
1705 }
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
1706 } 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
1707 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
1708 }
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
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1710 Train *base = v;
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1711 Train *first = base; // first vehicle to move
12460
4acae7d6f5ed (svn r16894) -Codechange: Add [Specialised]Vehicle::Last().
frosch <frosch@openttd.org>
parents: 12450
diff changeset
1712 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
1713 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
1714
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
1715 /* 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
1716 * 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
1717 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
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 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
1720 /* 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
1721 * (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
1722 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
1723
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 /* 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
1725 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
1726
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
1727 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
1728 first = first->Next();
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1729
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
1730 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
1731
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 /* 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
1733 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
1734
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
1735 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
1736 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
1737 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1738 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1739
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1740
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
1741 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
1742 {
8961
b33bcd7fa037 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz <smatz@openttd.org>
parents: 8954
diff changeset
1743 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
1744 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
1745 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1746
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
1747 /* 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
1748 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
1749
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1750 /* 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
1751 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
1752
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1753 /* count number of vehicles */
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
1754 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
1755
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 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
1757
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1758 /* swap start<>end, start+1<>end-1, ... */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1759 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
1760 do {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1761 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
1762 } 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
1763
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
1764 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
1765
8961
b33bcd7fa037 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz <smatz@openttd.org>
parents: 8954
diff changeset
1766 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
1767 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
1768 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1769
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
1770 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
1771
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
1772 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
1773
8667
4e73621c3950 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138 <peter1138@openttd.org>
parents: 8666
diff changeset
1774 /* 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
1775 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
1776
4e73621c3950 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138 <peter1138@openttd.org>
parents: 8666
diff changeset
1777 /* 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
1778 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
1779
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
1780 /* 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
1781 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
1782
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1783 /* 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
1784 crossing = TrainApproachingCrossingTile(v);
8356
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1785 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
1786
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1787 /* 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
1788 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
1789 /* 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
1790 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
1791 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
1792 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
1793 }
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1794
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1795 /* 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
1796 * 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
1797 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
1798 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
1799
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1800 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
1801 /* 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
1802 * 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
1803 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
1804 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
1805 !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
1806
12502
9472b49c61a1 (svn r16939) -Codechange: s/RailwayStation/RailStation/ to unify the way it's written.
rubidium <rubidium@openttd.org>
parents: 12472
diff changeset
1807 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
1808 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
1809 /* 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
1810 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
1811 } 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
1812 /* 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
1813 MarkTrainAsStuck(v);
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1814 }
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
1815 } 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
1816 /* 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
1817 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
1818 v->wait_counter = 0;
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1819 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1820 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1821
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1822 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1823 * 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
1824 * @param tile unused
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1825 * @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
1826 * @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
1827 * @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
1828 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1829 * @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
1830 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1831 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
1832 {
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1833 Train *v = Train::GetIfValid(p1);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1834 if (v == NULL) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1835
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1836 CommandCost ret = CheckOwnership(v->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1837 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
1838
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
1839 if (p2 != 0) {
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1840 /* 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
1841
13036
8e2a0e8fa275 (svn r17534) -Codechange: unify the naming of callback masks/flags
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
1842 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
1843 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
1844 }
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
1845 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
1846
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1847 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
1848 /* 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
1849 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
1850 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
1851 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1852
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1853 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
1854 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
1855
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
1856 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
1857 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
1858 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
1859 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
1860 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
1861 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1862 } 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
1863 /* 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
1864 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
1865
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1866 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
1867 /* 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
1868 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
1869 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
1870 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
1871
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
1872 /* 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
1873 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
1874 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
1875 }
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
1876 }
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
1877
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
1878 /* We cancel any 'skip signal at dangers' here */
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
1879 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
1880 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
1881
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
1882 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
1883 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
1884 } else {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1885 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
1886 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
1887 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
1888 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
1889 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1890 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1891 }
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
1892 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
1893 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1894
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1895 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1896 * 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
1897 * @param tile unused
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1898 * @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
1899 * @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
1900 * @param p2 unused
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1901 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1902 * @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
1903 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1904 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
1905 {
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1906 Train *t = Train::GetIfValid(p1);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1907 if (t == NULL) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1908
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1909 CommandCost ret = CheckOwnership(t->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1910 if (ret.Failed()) return ret;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1911
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1912
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
1913 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
1914 /* 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
1915 * 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
1916 * 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
1917 * 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
1918 * next signal we encounter. */
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
1919 t->force_proceed = t->force_proceed == TFP_SIGNAL ? TFP_NONE : HasBit(t->flags, VRF_TRAIN_STUCK) || t->IsInDepot() ? 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
1920 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
1921 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1922
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
1923 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
1924 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1925
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1926 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1927 * returns the tile of a depot to goto to. The given vehicle must not be
15623
c62577640878 (svn r20286) -Codechange: Unify end of doxygen comments.
frosch <frosch@openttd.org>
parents: 15620
diff changeset
1928 * crashed!
c62577640878 (svn r20286) -Codechange: Unify end of doxygen comments.
frosch <frosch@openttd.org>
parents: 15620
diff changeset
1929 */
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1930 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
1931 {
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1932 assert(!(v->vehstatus & VS_CRASHED));
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1933
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1934 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
1935
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
1936 PBSTileInfo origin = FollowTrainReservation(v);
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1937 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
1938
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
1939 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
1940 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
1941 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
1942
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1943 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
1944 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1945 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1946
8890
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
1947 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
1948 {
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1949 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
1950 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
1951
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
1952 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
1953 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
1954 if (reverse != NULL) *reverse = tfdd.reverse;
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
1955
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
1956 return true;
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
1957 }
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
1958
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
1959 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
1960 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1961 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
1962 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
1963 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
1964 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
1965 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
1966 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
1967 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1968
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
1969 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
1970
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
1971 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
1972 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
1973 }
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
1974
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
1975 /** Check if the train is on the last reserved tile and try to extend the path then. */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1976 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
1977 {
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
1978 /* 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
1979 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
1980
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
1981 /* 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
1982 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
1983
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
1984 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
1985 /* 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
1986 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
1987 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
1988 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
1989
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
1990 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
1991 /* 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
1992 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
1993 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
1994
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
1995 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
1996 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
1997 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
1998 /* 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
1999 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
2000 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
2001
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
2002 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
2003 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
2004 }
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
2005 /* 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
2006 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
2007
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
2008 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
2009
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
2010 /* 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
2011 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
2012 !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
2013 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
2014
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
2015 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
2016 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
2017
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
2018 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
2019 /* 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
2020 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
2021 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
2022 /* 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
2023 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
2024 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
2025 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
2026 }
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
2027 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
2028 }
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
2029 }
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
2030 }
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
2031 }
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
2032
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2033 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
2034 {
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2035 /* 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
2036 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
2037 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
2038 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2039
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2040 /* 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
2041 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
2042 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
2043 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
2044 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
2045 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2046
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
2047 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
2048
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
2049 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
2050 /* 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
2051 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
2052 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
2053 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
2054 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2055
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
2056 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
2057
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
2058 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
2059 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
2060 /* 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
2061 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
2062 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
2063 }
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
2064 } 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
2065 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
2066 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2067
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
2068 /* 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
2069 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
2070 /* 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
2071 if (HasDepotReservation(v->tile)) return true;
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2072 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
2073 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
2074 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
2075 }
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
2076
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
2077 /* 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
2078 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
2079 /* 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
2080 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
2081 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
2082 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
2083 }
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
2084
12471
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2085 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
2086 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
2087
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2088 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
2089 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
2090 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
2091
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
2092 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
2093 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
2094
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2095 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
2096 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
2097
6558
2fc024ee8217 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium <rubidium@openttd.org>
parents: 6554
diff changeset
2098 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
2099 v->cur_image = v->GetImage(v->direction, EIT_ON_MAP);
11326
557dd16a7c24 (svn r15676) -Codechange: remove the need for BeginVehicleMove and merge VehiclePositionChanged and EndVehicleMove.
rubidium <rubidium@openttd.org>
parents: 11268
diff changeset
2100 VehicleMove(v, false);
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
2101 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
2102 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
2103 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
2104
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2105 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
2106 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2107
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
2108 /** Clear the reservation of a tile that was just left by a wagon on track_dir. */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2109 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
2110 {
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
2111 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
2112
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
2113 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
2114 /* 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
2115 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
2116 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
2117
14707
ed6ca7f84893 (svn r19291) -Codechange: Rename HasVehicleOnTunnelBridge() to TunnelBridgeIsFree() and return a CommandCost status.
alberth <alberth@openttd.org>
parents: 14702
diff changeset
2118 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
2119 /* 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
2120 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
2121 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
2122
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
2123 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
2124 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
2125 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
2126 }
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
2127 }
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
2128 }
12502
9472b49c61a1 (svn r16939) -Codechange: s/RailwayStation/RailStation/ to unify the way it's written.
rubidium <rubidium@openttd.org>
parents: 12472
diff changeset
2129 } 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
2130 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
2131 /* 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
2132 * 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
2133 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
2134 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
2135 }
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
2136 } 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
2137 /* 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
2138 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
2139 }
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
2140 }
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
2141
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
2142 /** Free the reserved path in front of a vehicle. */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2143 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
2144 {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
2145 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
2146
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
2147 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
2148 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
2149 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
2150 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
2151
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
2152 /* 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
2153 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
2154
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
2155 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
2156 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
2157 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
2158 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
2159 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
2160 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
2161
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
2162 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
2163
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
2164 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
2165 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
2166 /* 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
2167 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
2168 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
2169 }
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
2170 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
2171 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
2172 /* 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
2173 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
2174 } 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
2175 /* 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
2176 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
2177 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
2178 }
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
2179 } 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
2180 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
2181 }
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
2182 }
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
2183
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
2184 /* 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
2185 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
2186
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
2187 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
2188 }
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
2189 }
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
2190
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2191 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
2192 {{ 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
2193 {{ 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
2194 {{ 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
2195 {{ 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
2196 {{ 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
2197 {{ 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
2198 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2199
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
2200 /**
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
2201 * 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
2202 *
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
2203 * @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
2204 * @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
2205 * @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
2206 * @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
2207 * @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
2208 * @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
2209 * @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
2210 * @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
2211 */
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
2212 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
2213 {
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
2214 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
2215 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
2216 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
2217
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2218 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
2219 }
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 }
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
2221
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 /**
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
2223 * 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
2224 * 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
2225 * @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
2226 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2227 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
2228 {
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
2229 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
2230
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
2231 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
2232
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
2233 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
2234 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
2235 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
2236 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
2237 /* 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
2238 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
2239 }
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
2240
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
2241 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
2242 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
2243 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
2244 }
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
2245
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
2246 /* 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
2247 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
2248 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
2249 /* 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
2250 * 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
2251 * 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
2252 * 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
2253 * 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
2254 * 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
2255 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
2256
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
2257 /* 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
2258 * 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
2259 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
2260
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
2261 /* 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
2262 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
2263 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
2264 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
2265 }
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
2266
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
2267 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
2268 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
2269
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
2270 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
2271 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
2272 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
2273 /* 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
2274 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
2275 }
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
2276
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
2277 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
2278 }
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
2279
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
2280 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
2281 /* 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
2282 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
2283 }
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
2284
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
2285 /* 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
2286 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
2287 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
2288 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
2289 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
2290 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
2291 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
2292
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
2293 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
2294 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
2295 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
2296 }
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
2297 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
2298
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
2299 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
2300 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
2301
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
2302 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
2303 }
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
2304
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
2305 /* 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
2306 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
2307 }
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
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 /**
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
2310 * 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
2311 * 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
2312 *
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
2313 * @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
2314 * @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
2315 * @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
2316 * @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
2317 * @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
2318 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2319 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
2320 {
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2321 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
2322 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
2323 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
2324
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2325 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
2326 }
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
2327 }
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
2328
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
2329 /** This class will save the current order of a vehicle and restore it on destruction. */
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
2330 class VehicleOrderSaver
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
2331 {
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
2332 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
2333 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
2334 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
2335 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
2336 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
2337 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
2338 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
2339
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
2340 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
2341 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
2342 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
2343 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
2344 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
2345 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
2346 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
2347 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
2348 {
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
2349 }
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
2350
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
2351 ~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
2352 {
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
2353 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
2354 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
2355 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
2356 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
2357 }
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
2358
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
2359 /**
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
2360 * 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
2361 * @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
2362 * @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
2363 */
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
2364 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
2365 {
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
2366 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
2367
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
2368 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
2369
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
2370 int conditional_depth = 0;
c7206a6dabf0 (svn r14826) -Fix [FS#2494]: infinite loop in order lookahead when reserving a path (Swallow)
rubidium <rubidium@openttd.org>
parents: 10546
diff changeset
2371
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
2372 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
2373 /* 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
2374 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
2375
11991
a4d3838349e6 (svn r16397) -Codechange: move GetVehicleOrder/GetLastVehicleOrder into Vehicle
rubidium <rubidium@openttd.org>
parents: 11988
diff changeset
2376 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
2377 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
2378
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
2379 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
2380 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
2381 /* 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
2382 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
2383 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
2384 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
2385 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
2386 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
2387 case OT_CONDITIONAL: {
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
2388 if (conditional_depth > this->v->GetNumOrders()) return false;
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
2389 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
2390 if (next != INVALID_VEH_ORDER_ID) {
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
2391 conditional_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
2392 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
2393 /* 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
2394 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
2395 }
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
2396 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
2397 }
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
2398 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
2399 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
2400 }
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
2401 /* 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
2402 * 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
2403 ++this->index;
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
2404 } while (this->index != this->v->cur_real_order_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
2405
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
2406 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
2407 }
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
2408 };
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
2409
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 /* choose a track */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2411 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
2412 {
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
2413 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
2414 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
2415 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
2416
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
2417 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
2418
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
2419 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
2420
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
2421 /* 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
2422 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
2423 /* 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
2424 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
2425
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
2426 /* 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
2427 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
2428 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
2429 /* 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
2430 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
2431 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
2432 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
2433 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
2434 } 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
2435 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
2436 }
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
2437 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
2438 }
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
2439
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
2440 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
2441 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
2442 if (do_track_reservation) {
9822
d61801babe63 (svn r13964) -Codechange [YAPP]: Check if a train needs servicing when reserving the next path segment. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9821
diff changeset
2443 /* Check if the train needs service here, so it has a chance to always find a depot.
d61801babe63 (svn r13964) -Codechange [YAPP]: Check if a train needs servicing when reserving the next path segment. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9821
diff changeset
2444 * Also check if the current order is a service order so we don't reserve a path to
d61801babe63 (svn r13964) -Codechange [YAPP]: Check if a train needs servicing when reserving the next path segment. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9821
diff changeset
2445 * the destination but instead to the next one if service isn't needed. */
d61801babe63 (svn r13964) -Codechange [YAPP]: Check if a train needs servicing when reserving the next path segment. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9821
diff changeset
2446 CheckIfTrainNeedsService(v);
d61801babe63 (svn r13964) -Codechange [YAPP]: Check if a train needs servicing when reserving the next path segment. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9821
diff changeset
2447 if (v->current_order.IsType(OT_DUMMY) || v->current_order.IsType(OT_CONDITIONAL) || v->current_order.IsType(OT_GOTO_DEPOT)) ProcessOrders(v);
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
2448
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
2449 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
2450 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
2451 /* 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
2452 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
2453 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
2454 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
2455 }
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
2456 }
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
2457
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
2458 /* 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
2459 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
2460
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
2461 /* 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
2462 * 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
2463 * 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
2464 * 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
2465 * 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
2466 * 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
2467 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
2468 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
2469 } 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
2470 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
2471 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
2472 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
2473 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
2474 }
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
2475
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
2476 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
2477 /* 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
2478 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
2479 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
2480
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
2481 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
2482 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
2483 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
2484 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2485
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
2486 /* 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
2487 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
2488
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
2489 /* 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
2490 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
2491 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
2492 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
2493 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
2494 }
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 /* 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
2497 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
2498 /* 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
2499 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
2500 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
2501 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
2502 best_track = FindFirstTrack(res);
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
2503 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
2504 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
2505 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
2506 } 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
2507 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
2508 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
2509 }
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 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
2511 }
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
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 (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
2514
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
2515 /* 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
2516 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
2517 /* 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
2518 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
2519 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
2520 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
2521 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
2522 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
2523 }
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
2524
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
2525 /* 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
2526 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
2527 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
2528 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
2529 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
2530 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
2531 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
2532 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
2533 /* 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
2534 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
2535 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
2536 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
2537 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
2538 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
2539 }
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
2540 }
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
2541 /* 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
2542 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
2543 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
2544 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
2545 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
2546 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
2547 }
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
2548 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
2549 }
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
2550
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
2551 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
2552
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
2553 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
2554
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2555 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
2556 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2557
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
2558 /**
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
2559 * 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
2560 *
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
2561 * @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
2562 * @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
2563 * @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
2564 * @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
2565 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2566 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
2567 {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
2568 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
2569
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
2570 /* 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
2571 * 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
2572 * 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
2573 if (v->track == TRACK_BIT_DEPOT) {
12471
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2574 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
2575 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
2576 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
2577 } 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
2578 /* 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
2579 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
2580 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
2581 }
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
2582 }
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
2583
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
2584 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
2585 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
2586 /* 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
2587 * 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
2588 * 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
2589 * 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
2590 * 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
2591 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
2592 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
2593 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
2594 }
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
2595 /* 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
2596 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
2597 /* Can't be stuck then. */
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
2598 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
2599 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
2600 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
2601 }
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
2602
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
2603 /* 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
2604 if (v->track == TRACK_BIT_DEPOT) {
12471
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2605 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
2606 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
2607 }
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
2608
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
2609 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
2610 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
2611 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
2612
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
2613 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
2614
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
2615 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
2616 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
2617
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
2618 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
2619 /* Free the depot reservation as well. */
12471
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2620 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
2621 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
2622 }
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
2623
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
2624 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
2625 v->wait_counter = 0;
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
2626 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
2627 }
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
2628 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
2629 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
2630 }
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
2631
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2632
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2633 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
2634 {
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
2635 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
2636 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
2637 !(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
2638 return false;
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2639 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2640
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2641 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
2642
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
2643 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
2644 case VPF_NPF: return NPFTrainCheckReverse(v);
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2645 case VPF_YAPF: return YapfTrainCheckReverse(v);
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2646
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2647 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
2648 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2649 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2650
8827
095c429c5fa7 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium <rubidium@openttd.org>
parents: 8794
diff changeset
2651 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
2652 {
8830
981a95f3204d (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium <rubidium@openttd.org>
parents: 8827
diff changeset
2653 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
2654
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11920
diff changeset
2655 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
2656 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
2657 /* 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
2658 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
2659 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
2660 }
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
2661
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
2662 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
2663 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2664
6553
733ef9947fe2 (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium <rubidium@openttd.org>
parents: 6552
diff changeset
2665 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
2666 {
15127
c065afa82756 (svn r19756) -Codechange: move UpdateViewport() from Vehicle to SpecializedVehicle in order to improve performance
smatz <smatz@openttd.org>
parents: 15073
diff changeset
2667 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
2668 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
2669 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
2670 } 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
2671
733ef9947fe2 (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium <rubidium@openttd.org>
parents: 6552
diff changeset
2672 /* 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
2673 this->CargoChanged();
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
2674 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
2675 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2676
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
2677 /**
15002
baf0e12ad2b4 (svn r19614) -Codechange: "it's" => "its" where appropriate
smatz <smatz@openttd.org>
parents: 14898
diff changeset
2678 * 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
2679 * 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
2680 * 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
2681 * 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
2682 * @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
2683 */
14284
fc532d11eb85 (svn r18836) -Codechange: make TrainUpdateSpeed a class function and update some comments (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14261
diff changeset
2684 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
2685 {
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
2686 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
2687 default: NOT_REACHED();
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
2688 case AM_ORIGINAL:
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
2689 return this->DoUpdateSpeed(this->acceleration * (this->GetAccelerationStatus() == AS_BRAKE ? -4 : 2), 0, this->gcache.cached_max_track_speed);
b21a59ab8453 (svn r21914) -Codechange: move the algorithmic part of Train::UpdateSpeed to a function in GroundVehicle
rubidium <rubidium@openttd.org>
parents: 17172
diff changeset
2690
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
2691 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
2692 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
2693 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2694 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2695
16372
ea86953aa185 (svn r21088) -Doc: Additions/corrections of doxygen comments.
alberth <alberth@openttd.org>
parents: 16349
diff changeset
2696 /**
ea86953aa185 (svn r21088) -Doc: Additions/corrections of doxygen comments.
alberth <alberth@openttd.org>
parents: 16349
diff changeset
2697 * 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
2698 * @param v Train that entered the station.
ea86953aa185 (svn r21088) -Doc: Additions/corrections of doxygen comments.
alberth <alberth@openttd.org>
parents: 16349
diff changeset
2699 * @param station Station visited.
ea86953aa185 (svn r21088) -Doc: Additions/corrections of doxygen comments.
alberth <alberth@openttd.org>
parents: 16349
diff changeset
2700 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2701 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
2702 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2703 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
2704
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2705 /* 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
2706 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
2707 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
2708 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
2709 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
2710 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
2711 STR_NEWS_FIRST_TRAIN_ARRIVAL,
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
2712 v->owner == _local_company ? NS_ARRIVAL_COMPANY : NS_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
2713 v->index,
10123
57aa34285d05 (svn r14307) -Fix: when deleting a station, remove news items regarding it
smatz <smatz@openttd.org>
parents: 10103
diff changeset
2714 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
2715 );
18511
f1214da4c397 (svn r23355) -Codechange: rename all AI* to Script* (Rubidium)
truebrain <truebrain@openttd.org>
parents: 18472
diff changeset
2716 AI::NewEvent(v->owner, 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
2717 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2718
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
2719 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
2720 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
2721
6550
2fa52ce08b41 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium <rubidium@openttd.org>
parents: 6546
diff changeset
2722 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
2723
15935
a4fa352275f4 (svn r20623) -Codechange: unify the storing of animation related information
rubidium <rubidium@openttd.org>
parents: 15899
diff changeset
2724 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
2725 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2726
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2727 /* 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
2728 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
2729 {
16547
a11cd939c335 (svn r21273) -Codechange: Return values should start at the same line.
alberth <alberth@openttd.org>
parents: 16511
diff changeset
2730 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
2731 (!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
2732 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2733
6248
0789677a15a0 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium <rubidium@openttd.org>
parents: 6247
diff changeset
2734 struct RailtypeSlowdownParams {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2735 byte small_turn, large_turn;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2736 byte z_up; // fraction to remove when moving up
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2737 byte z_down; // fraction to remove when moving down
6248
0789677a15a0 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium <rubidium@openttd.org>
parents: 6247
diff changeset
2738 };
5584
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 static const RailtypeSlowdownParams _railtype_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
2741 /* normal accel */
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2742 {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
2743 {256 / 4, 256 / 2, 256 / 4, 2}, ///< electrified
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2744 {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
2745 {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
2746 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2747
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2748 /** Modify the speed of the vehicle due to a change in altitude */
18272
3ebab55c5495 (svn r23108) -Codechange: more uint -> int / byte -> int conversions for Z related variables
rubidium <rubidium@openttd.org>
parents: 18255
diff changeset
2749 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
2750 {
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
2751 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
2752
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
2753 const RailtypeSlowdownParams *rsp = &_railtype_slowdown[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
2754
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2755 if (old_z < v->z_pos) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2756 v->cur_speed -= (v->cur_speed * rsp->z_up >> 8);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2757 } else {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2758 uint16 spd = v->cur_speed + rsp->z_down;
16785
775f357828e3 (svn r21518) -Codechange: Rename AccelerationCache to GroundVehicleCache.
terkhen <terkhen@openttd.org>
parents: 16776
diff changeset
2759 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
2760 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2761 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2762
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
2763 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
2764 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2765 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
2766 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
2767 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
2768 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
2769 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
2770 /* 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
2771 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
2772 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2773 }
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
2774 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
2775 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2776
12423
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2777 /**
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2778 * Tries to reserve track under whole train consist
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2779 */
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2780 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
2781 {
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2782 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
2783 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
2784 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
2785 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
2786 break;
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2787 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
2788 break;
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2789 default:
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2790 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
2791 break;
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2792 }
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2793 }
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2794 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2795
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2796 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
2797 {
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2798 uint pass = 0;
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2799 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
2800 pass += 2; // driver
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2801
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
2802 /* 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
2803 * 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
2804 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
2805 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
2806 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
2807 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
2808 /* 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
2809 * 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
2810 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
2811 }
9811
84013100eac2 (svn r13953) -Add [YAPP]: Free track reservations of crashed trains. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9810
diff changeset
2812 }
13873
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2813
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2814 /* 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
2815 * 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
2816 TileIndex crossing = TrainApproachingCrossingTile(this);
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2817 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
2818
7015f43f6a77 (svn r18941) -Fix [FS#3575]: remove the loading indicators as soon as a train crashes
yexo <yexo@openttd.org>
parents: 14382
diff changeset
2819 /* 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
2820 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
2821 }
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
2822
17141
4ccee6c9442b (svn r21879) -Codechange: reset vehicle's GOINGUP/DOWN bits when it crashes
smatz <smatz@openttd.org>
parents: 17137
diff changeset
2823 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
2824
85b3d1a4faff (svn r18402) -Codechange: unify/centralise the code for crashing vehicles
rubidium <rubidium@openttd.org>
parents: 13842
diff changeset
2825 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
2826 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
2827 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2828
11150
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2829 /**
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2830 * 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
2831 * 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
2832 * @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
2833 * @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
2834 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2835 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
2836 {
12423
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2837 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
2838
11150
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2839 /* 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
2840 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
2841 num = v->Crash();
18511
f1214da4c397 (svn r23355) -Codechange: rename all AI* to Script* (Rubidium)
truebrain <truebrain@openttd.org>
parents: 18472
diff changeset
2842 AI::NewEvent(v->owner, 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
2843 }
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2844
698a97a3744a (svn r16857) -Fix [FS#3036](r16652): crash when train partially in depot crashed
smatz <smatz@openttd.org>
parents: 12415
diff changeset
2845 /* 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
2846 * 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
2847 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
2848
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2849 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
2850 }
7e9a1181206a (svn r15497) -Fix (r15027): when trains crashed, AIEvent was created only for one train
smatz <smatz@openttd.org>
parents: 11149
diff changeset
2851
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
2852 struct TrainCollideChecker {
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
2853 Train *v; ///< vehicle we are testing for 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
2854 uint num; ///< 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
2855 };
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
2856
9775
c3caa51b9888 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium <rubidium@openttd.org>
parents: 9774
diff changeset
2857 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
2858 {
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2859 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
2860
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
2861 /* 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
2862 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
2863
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
2864 /* 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
2865 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
2866
8281
a3736272d02a (svn r11845) -Codechange: simplify train collision detection a bit
smatz <smatz@openttd.org>
parents: 8271
diff changeset
2867 /* 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
2868 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
2869
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
2870 /* 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
2871 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
2872
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
2873 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
2874 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
2875
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
2876 /* 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
2877 * 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
2878 * 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
2879 * 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
2880 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
2881 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
2882
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
2883 /* 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
2884 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
2885 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
2886
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
2887 /* 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
2888 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
2889
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
2890 /* 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
2891 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
2892 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
2893
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
2894 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
2895 }
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
2896
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2897 /**
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2898 * 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
2899 * 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
2900 * 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
2901 * plays a sound.
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2902 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2903 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
2904 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2905 /* 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
2906 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
2907
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
2908 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
2909
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
2910 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
2911 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
2912 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
2913
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
2914 /* 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
2915 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
2916 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
2917 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
2918 } 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
2919 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
2920 }
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
2921
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
2922 /* 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
2923 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
2924
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
2925 SetDParam(0, tcc.num);
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
2926 AddVehicleNewsItem(STR_NEWS_TRAIN_CRASH,
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
2927 NS_ACCIDENT,
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
2928 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
2929 );
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2930
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2931 ModifyStationRatingAround(v->tile, v->owner, -160, 30);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2932 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
2933 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
2934 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2935
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
2936 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
2937 {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
2938 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
2939
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
2940 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
2941 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
2942
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
2943 /* 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
2944 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
2945
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
2946 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
2947
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
2948 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
2949 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2950
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
2951 /**
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
2952 * 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
2953 * @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
2954 * @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
2955 * @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
2956 * @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
2957 */
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
2958 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
2959 {
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
2960 Train *first = v->First();
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2961 Train *prev;
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
2962 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
2963
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2964 /* 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
2965 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
2966 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
2967 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
2968
6153
42ef9b98d898 (svn r8897) -Fix
tron <tron@openttd.org>
parents: 6152
diff changeset
2969 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
2970 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
2971 /* Not inside tunnel */
6152
16eaa24b383f (svn r8896) -Fix
tron <tron@openttd.org>
parents: 6151
diff changeset
2972 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
2973 /* 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
2974 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
2975 /* 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
2976 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
2977 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
2978 } 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
2979 /* 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
2980
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
2981 /* 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
2982 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
2983
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
2984 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
2985 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
2986 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
2987 }
7928
4e8dfd103163 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7923
diff changeset
2988 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
2989 /* 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
2990 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
2991 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2992 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2993 } else {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2994 /* 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
2995
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2996 /* 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
2997 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
2998 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
2999
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3000 /* 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
3001 * 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
3002 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
3003 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
3004
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
3005 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
3006 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
3007
9e46ac001a8c (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch <frosch@openttd.org>
parents: 8614
diff changeset
3008 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
3009 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
3010 /* 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
3011 * 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
3012 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
3013 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3014
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
3015 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
3016
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
3017 /* 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
3018 * 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
3019 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
3020
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3021 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
3022 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
3023 /* 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
3024 * 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
3025 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
3026 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
3027
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
3028 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
3029 /* 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
3030 * 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
3031 * 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
3032 * 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
3033 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
3034 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
3035 !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
3036 /* 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
3037 * entered via the back. */
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
3038 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
3039 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
3040 }
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
3041 }
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
3042
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3043 /* 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
3044 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
3045 /* 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
3046 Trackdir i = FindFirstTrackdir(trackdirbits);
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3047
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
3048 /* 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
3049 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
3050
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3051 if (!HasSignalOnTrackdir(gp.new_tile, ReverseTrackdir(i))) {
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3052 v->cur_speed = 0;
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3053 v->subspeed = 0;
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3054 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
3055 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
3056 } else if (HasSignalOnTrackdir(gp.new_tile, i)) {
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3057 v->cur_speed = 0;
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3058 v->subspeed = 0;
6491
696f0e1b046e (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium <rubidium@openttd.org>
parents: 6490
diff changeset
3059 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
3060 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
3061 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
3062 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
3063
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
3064 exitdir = ReverseDiagDir(exitdir);
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3065
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3066 /* 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
3067 if (!HasVehicleOnPos(o_tile, &exitdir, &CheckTrainAtSignal)) return false;
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3068 }
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3069 }
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
3070
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
3071 /* 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
3072 * 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
3073 * 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
3074 * 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
3075 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
3076 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
3077 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
3078 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
3079 }
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3080 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
3081 } else {
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
3082 TryReserveRailTrack(gp.new_tile, TrackBitsToTrack(chosen_track));
6151
dc514bd416ef (svn r8894) -Fix
tron <tron@openttd.org>
parents: 6150
diff changeset
3083 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3084 } else {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3085 /* 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
3086 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
3087 /* 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
3088 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
3089 /* 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
3090 * 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
3091 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
3092 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
3093 } 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
3094 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
3095 }
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
3096 } else {
11690
eb5d9897c030 (svn r16075) -Fix (r15792)[FS#2844]: Mixed up TRACK_BIT_X/Y.
frosch <frosch@openttd.org>
parents: 11677
diff changeset
3097 /* 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
3098 * 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
3099 * 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
3100 * 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
3101 * 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
3102 * 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
3103 */
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
3104 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
3105 {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
3106 {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
3107 {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
3108 {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
3109 };
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
3110 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
3111 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
3112 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
3113 }
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
3114 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
3115 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3116
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
3117 /* 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
3118 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
3119 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
3120 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
3121 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
3122
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3123 /* 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
3124 const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir];
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3125 gp.x = (gp.x & ~0xF) | b[0];
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3126 gp.y = (gp.y & ~0xF) | b[1];
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3127 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
3128
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3129 /* 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
3130 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
3131 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
3132 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
3133 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3134
7928
4e8dfd103163 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13 <skidd13@openttd.org>
parents: 7923
diff changeset
3135 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
3136 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
3137 Trackdir tdir = TrackDirectionToTrackdir(track, chosen_dir);
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3138 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
3139 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
3140 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
3141 }
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
3142
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
3143 /* 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
3144 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
3145
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3146 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
3147
6154
238db87ae59e (svn r8899) -Fix
tron <tron@openttd.org>
parents: 6153
diff changeset
3148 if (GetTileRailType(gp.new_tile) != GetTileRailType(gp.old_tile)) {
15826
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
3149 v->First()->RailtypeChanged();
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3150 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3151
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
3152 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
3153 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
3154 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3155
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
3156 /* 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
3157 * 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
3158 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
3159
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3160 if (chosen_dir != v->direction) {
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
3161 if (prev == NULL && _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) {
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3162 const RailtypeSlowdownParams *rsp = &_railtype_slowdown[v->railtype];
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3163 DirDiff diff = DirDifference(v->direction, chosen_dir);
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3164 v->cur_speed -= (diff == DIRDIFF_45RIGHT || diff == DIRDIFF_45LEFT ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8;
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3165 }
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3166 direction_changed = true;
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3167 v->direction = chosen_dir;
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3168 }
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
3169
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3170 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
3171 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
3172
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
3173 /* 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
3174 TileIndex crossing = TrainApproachingCrossingTile(v);
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
3175 if (crossing != INVALID_TILE && HasCrossingReservation(crossing)) 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
3176
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
3177 /* 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
3178 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
3179 }
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
3180
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
3181 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
3182 /* 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
3183 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
3184 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3185 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3186 } else {
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
3187 /* In a tunnel or on a bridge
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
3188 * - for tunnels, only the part when the vehicle is not visible (part of enter/exit tile too)
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
3189 * - for bridges, only the middle part - without the bridge heads */
6141
4be07d3ba23b (svn r8884) -Fix
tron <tron@openttd.org>
parents: 6136
diff changeset
3190 if (!(v->vehstatus & VS_HIDDEN)) {
16420
f74d1996f90c (svn r21136) -Fix [FS#4213]: bridge speed limits should apply to all wagons of a vehicle, not just the head of the vehicle
rubidium <rubidium@openttd.org>
parents: 16407
diff changeset
3191 Train *first = v->First();
f74d1996f90c (svn r21136) -Fix [FS#4213]: bridge speed limits should apply to all wagons of a vehicle, not just the head of the vehicle
rubidium <rubidium@openttd.org>
parents: 16407
diff changeset
3192 first->cur_speed = min(first->cur_speed, GetBridgeSpec(GetBridgeType(v->tile))->speed);
6141
4be07d3ba23b (svn r8884) -Fix
tron <tron@openttd.org>
parents: 6136
diff changeset
3193 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3194
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
3195 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
3196 /* Perform look-ahead on tunnel exit. */
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3197 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
3198 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
3199 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
3200 }
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
3201 /* 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
3202 * 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
3203 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
3204 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
3205 }
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
3206 } 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
3207 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
3208 v->y_pos = gp.y;
11326
557dd16a7c24 (svn r15676) -Codechange: remove the need for BeginVehicleMove and merge VehiclePositionChanged and EndVehicleMove.
rubidium <rubidium@openttd.org>
parents: 11268
diff changeset
3209 VehicleMove(v, !(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
3210 continue;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3211 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3212 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3213
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3214 /* 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
3215 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
3216
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3217 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
3218 v->y_pos = gp.y;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3219
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3220 /* 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
3221 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
3222
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3223 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
3224 /* 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
3225 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
3226 }
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
3227
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
3228 if (update_signals_crossing) {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3229 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
3230 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
3231 /* 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
3232 * 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
3233 * 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
3234 * 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
3235 * 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
3236 * 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
3237 * 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
3238 * 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
3239 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
3240 !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
3241 !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
3242 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
3243 }
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
3244 }
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
3245 }
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
3246
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
3247 /* 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
3248 * (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
3249 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
3250 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
3251 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
3252 }
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
3253 }
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
3254
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
3255 /* 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
3256 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
3257 }
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
3258
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
3259 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
3260
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
3261 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
3262
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3263 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
3264 /* 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
3265 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
3266
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3267 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
3268 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
3269 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
3270 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
3271 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
3272 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
3273 }
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
3274
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
3275 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
3276 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3277
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
3278 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
3279 * 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
3280 * @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
3281 * @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
3282 * @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
3283 */
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
3284 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
3285 {
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
3286 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
3287
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
3288 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
3289 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
3290 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
3291 /* 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
3292 *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
3293 } 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
3294 *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
3295 }
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
3296 }
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
3297
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
3298 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
3299 }
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
3300
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3301 /**
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3302 * 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
3303 * 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
3304 * 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
3305 * 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
3306 * @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
3307 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3308 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
3309 {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3310 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
3311
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3312 /* 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
3313 * *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
3314 * 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
3315 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
3316 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
3317 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
3318
10493
5531e81401ee (svn r14748) -Codechange: unduplicate window updating when deleting vehicles.
rubidium <rubidium@openttd.org>
parents: 10492
diff changeset
3319 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
3320 /* 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
3321 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
3322 /* 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
3323 * 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
3324 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
3325 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
3326 }
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
3327 }
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
3328
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
3329 /* '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
3330 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
3331 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
3332 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
3333
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
3334 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
3335 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
3336
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
3337 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
3338 /* 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
3339 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
3340 }
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
3341
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
3342 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
3343 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
3344 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
3345
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
3346 /* 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
3347 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
3348 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
3349
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
3350 /* 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
3351 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
3352 Track t;
5fa5a4d1f409 (svn r19792) -Change: use the typed FOR_EACH_SET_BIT for Tracks (adf88)
rubidium <rubidium@openttd.org>
parents: 15156
diff changeset
3353 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
3354 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3355
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
3356 /* 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
3357 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
3358
8257
38d56fa7b6f7 (svn r11821) -Fix (r11802): 'optimization assert' when removing crashed wagon in some cases
smatz <smatz@openttd.org>
parents: 8256
diff changeset
3359 /* 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
3360 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
3361 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
3362 } 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
3363 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
3364 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3365 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3366
17236
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3367 /**
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3368 * 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
3369 * @param v First crashed vehicle.
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3370 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3371 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
3372 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3373 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
3374 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
3375 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3376
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3377 do {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3378 /* 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
3379 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
3380 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
3381 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
3382 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
3383 /* 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
3384 * 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
3385 * the bridge in that case */
14754
691e61140880 (svn r19342) -Codechange: Move inclination update functions to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14753
diff changeset
3386 if (v->track != TRACK_BIT_WORMHOLE) v->UpdateInclination(false, 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
3387 }
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
3388 } 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
3389 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3390
17236
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3391 /**
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3392 * Handle a crashed train.
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3393 * @param v First train vehicle.
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3394 * @return %Vehicle chain still exists.
2401b5b905f3 (svn r21976) -Doc: Document several crash handling functions.
alberth <alberth@openttd.org>
parents: 17227
diff changeset
3395 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3396 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
3397 {
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
3398 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
3399
5993
2498be7c7e94 (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium <rubidium@openttd.org>
parents: 5992
diff changeset
3400 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
3401 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
3402 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3403
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3404 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
3405 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
3406 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
3407
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3408 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
3409 do {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3410 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
3411 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
3412
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3413 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
3414 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
3415 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
3416 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
3417 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
3418 break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3419 }
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
3420 } 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
3421 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3422
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3423 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
3424
8969
01cc3ee6ecb1 (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium <rubidium@openttd.org>
parents: 8962
diff changeset
3425 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
3426 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
3427 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
3428 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
3429 }
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3430
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3431 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
3432 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3433
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
3434 /** 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
3435 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
3436 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
3437 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3438
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
3439
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
3440 /**
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
3441 * 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
3442 *
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
3443 * @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
3444 * @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
3445 * @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
3446 * @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
3447 */
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
3448 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
3449 {
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
3450 /* Calc position within the current tile */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3451 uint x = v->x_pos & 0xF;
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3452 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
3453
8312
f6764781d2eb (svn r11877) -Fix: another way to crash competitors' train in a station
smatz <smatz@openttd.org>
parents: 8305
diff changeset
3454 /* 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
3455 * 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
3456 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
3457 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
3458 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
3459 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
3460 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
3461 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
3462 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
3463 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
3464 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
3465 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3466
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
3467 /* 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
3468 * 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
3469 * 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
3470 * 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
3471 * 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
3472 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
3473 /* 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
3474 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
3475 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
3476 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
3477 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3478
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
3479 /* 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
3480 v->vehstatus |= VS_TRAIN_SLOWING;
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
3481 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
3482 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
3483
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3484 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
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
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
3487
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
3488 /**
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
3489 * 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
3490 * @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
3491 * @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
3492 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3493 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
3494 {
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3495 /* 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
3496 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
3497
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3498 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
3499
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3500 /* 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
3501 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
3502 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
3503 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
3504 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3505
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3506 /* 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
3507 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
3508 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
3509 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
3510 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3511
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3512 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
3513 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3514
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3515
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3516 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3517 * 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
3518 * (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
3519 * @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
3520 * @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
3521 * @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
3522 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3523 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
3524 {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3525 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
3526 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
3527
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3528 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
3529
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
3530 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
3531 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
3532
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
3533 /* 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
3534 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
3535 !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
3536 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
3537 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3538
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3539 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
3540 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3541
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3542
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
3543 /**
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
3544 * 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
3545 *
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
3546 * @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
3547 * @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
3548 * @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
3549 */
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
3550 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
3551 {
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
3552 /* 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
3553
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
3554 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
3555 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
3556 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
3557
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 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
3559 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
3560 } 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
3561 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
3562 }
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
3563
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
3564 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
3565
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
3566 /* 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
3567 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
3568 /* 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
3569 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
3570
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
3571 /* 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
3572 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
3573 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
3574
0a524ed2b87b (svn r12532) -Cleanup: Replace two tables of magic values with already existing functions.
frosch <frosch@openttd.org>
parents: 8793
diff changeset
3575 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
3576 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
3577
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
3578 /* 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
3579
8482
dd2dbdff6d0d (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz <smatz@openttd.org>
parents: 8467
diff changeset
3580 /* 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
3581 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
3582 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
3583 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
3584 }
dd2dbdff6d0d (svn r12057) -Fix: slowdown train when approaching 90deg turn when 90deg turns are forbidden
smatz <smatz@openttd.org>
parents: 8467
diff changeset
3585
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
3586 /* 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
3587 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
3588 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
3589 }
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
3590
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
3591 /* 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
3592 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
3593
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
3594 /* 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
3595 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
3596
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
3597 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
3598 }
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
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3601 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
3602 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3603 /* 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
3604 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
3605 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
3606 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3607
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
3608 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
3609 ClrBit(v->flags, VRF_TRAIN_STUCK);
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
3610 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
3611 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3612
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3613 /* 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
3614 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
3615
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
3616 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
3617 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
3618 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3619
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3620 /* 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
3621 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
3622
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
3623 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
3624 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
3625 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
3626 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
3627 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
3628 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
3629 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
3630 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
3631 } 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
3632 /* 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
3633 * 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
3634 * 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
3635 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
3636 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
3637
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
3638 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
3639 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
3640 }
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
3641 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
3642 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3643
6594
7535dcf42683 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium <rubidium@openttd.org>
parents: 6593
diff changeset
3644 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
3645
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3646 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
3647
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3648 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
3649
16511
b9db78828e3d (svn r21237) -Codechange: Move HandleLocomotiveSmokeCloud to Vehicle::ShowVisualEffect (Hirundo)
rubidium <rubidium@openttd.org>
parents: 16510
diff changeset
3650 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
3651
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
3652 /* 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
3653 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
3654 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
3655 }
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
3656
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
3657 /* 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
3658 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
3659 ++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
3660
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
3661 /* 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
3662 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
3663
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
3664 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
3665 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
3666 /* 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
3667 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
3668
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
3669 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
3670 /* 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
3671 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
3672 SetDParam(0, v->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
3673 AddVehicleNewsItem(
12619
2f530108f787 (svn r17062) -Change: unify the naming of some 125 strings
rubidium <rubidium@openttd.org>
parents: 12510
diff changeset
3674 STR_NEWS_TRAIN_IS_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
3675 NS_ADVICE,
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
3676 v->index
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
3677 );
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
3678 }
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
3679 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
3680 }
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
3681 /* 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
3682 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
3683 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
3684 v->wait_counter = 0;
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
3685 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
3686 }
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
3687 }
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
3688
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
3689 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
3690 v->current_order.Free();
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
3691 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
3692 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
3693 }
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
3694
14284
fc532d11eb85 (svn r18836) -Codechange: make TrainUpdateSpeed a class function and update some comments (Terkhen)
rubidium <rubidium@openttd.org>
parents: 14261
diff changeset
3695 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
3696
916b061b84a0 (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz <smatz@openttd.org>
parents: 8390
diff changeset
3697 /* 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
3698 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
3699 /* 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
3700 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
3701 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
3702 }
916b061b84a0 (svn r11996) -Fix [FS#1706]: update train statusbar when stopping from zero speed
smatz <smatz@openttd.org>
parents: 8390
diff changeset
3703
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
3704 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
3705 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
3706 /* 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
3707 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
3708 } else {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3709 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
3710 /* 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
3711 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
3712 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
3713 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
3714 /* 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
3715 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
3716 /* 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
3717 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
3718
11701
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3719 /* 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
3720 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
3721
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3722 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
3723 /* 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
3724 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
3725 (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
3726 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
3727 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
3728 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
3729 }
11701
362680cbb585 (svn r16087) -Fix (r16079): slow trains wouldn't crash into eachother anymore
rubidium <rubidium@openttd.org>
parents: 11694
diff changeset
3730 }
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
3731 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
3732 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3733
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3734 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
3735 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
3736
12666
0275245c6c62 (svn r17121) -Fix [FS#3060]: Update vehicle position cache when the vehicle sprite changes.
frosch <frosch@openttd.org>
parents: 12635
diff changeset
3737 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
3738 }
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
3739
10641
94cbc9a0158b (svn r14943) -Cleanup: remove some rogue spaces/replace some rogue tabs with spaces
rubidium <rubidium@openttd.org>
parents: 10589
diff changeset
3740 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
3741
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;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3743 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3744
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3745
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
3746
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
3747 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
3748 {
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
3749 Money cost = 0;
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3750 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
3751
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
3752 do {
18238
3141f1ed78eb (svn r23074) -Codechange: Add Vehicle::GetEngine() to simplify code.
frosch <frosch@openttd.org>
parents: 18159
diff changeset
3753 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
3754 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
3755
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
3756 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
3757 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
3758
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
3759 /* 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
3760 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
3761
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
3762 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
3763 } 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
3764
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
3765 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
3766 }
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
3767
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
3768
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3769 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
3770 {
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
3771 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
3772
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3773 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
3774 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
3775
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
3776 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
3777
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3778 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
3779
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3780 return TrainLocoHandler(this, true);
12301
218a41d3d557 (svn r16718) -Codechange: make IsFreeWagon() member of Train
smatz <smatz@openttd.org>
parents: 12300
diff changeset
3781 } 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
3782 /* 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
3783 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
3784 delete this;
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3785 return false;
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3786 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3787 }
11970
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3788
2286db6ac0bb (svn r16376) -Codechange: Vehicle::Tick() now returns false if the vehicle was deleted
smatz <smatz@openttd.org>
parents: 11949
diff changeset
3789 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
3790 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3791
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
3792 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
3793 {
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
3794 if (Company::Get(v->owner)->settings.vehicle.servint_trains == 0 || !v->NeedsAutomaticServicing()) return;
7502
2250b8ea9466 (svn r11017) -Codechange: unify determining whether a vehicle needs/can be service a little more.
rubidium <rubidium@openttd.org>
parents: 7497
diff changeset
3795 if (v->IsInDepot()) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3796 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
3797 return;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3798 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3799
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
3800 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
3801 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
3802 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
3803 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
3804 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
3805 }
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
3806
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
3807 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
3808 /* 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
3809 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
3810 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
3811 /* 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
3812 * 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
3813 * schedule? */
8836
890a77315801 (svn r12584) -Codechange: do not access the order type directly.
rubidium <rubidium@openttd.org>
parents: 8832
diff changeset
3814 v->current_order.MakeDummy();
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
3815 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
3816 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3817 return;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3818 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3819
13001
c2298b1d3f83 (svn r17495) -Codechange: replace 'Depot::Get(GetDepotIndex(tile))->index' with GetDepotIndex(tile)
rubidium <rubidium@openttd.org>
parents: 12860
diff changeset
3820 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
3821
8836
890a77315801 (svn r12584) -Codechange: do not access the order type directly.
rubidium <rubidium@openttd.org>
parents: 8832
diff changeset
3822 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
3823 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
3824 !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
3825 return;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3826 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3827
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
3828 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
3829 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
3830 v->dest_tile = tfdd.tile;
18692
90d395fc678d (svn r23540) -Codechange: unify and document vehicle widgets
rubidium <rubidium@openttd.org>
parents: 18627
diff changeset
3831 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
3832 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3833
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
3834 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
3835 {
18007
c7a5373f1a03 (svn r22816) -Feature(ette): [NewGRF] Also age wagons and articulated parts.
frosch <frosch@openttd.org>
parents: 17909
diff changeset
3836 AgeVehicle(this);
c7a5373f1a03 (svn r22816) -Feature(ette): [NewGRF] Also age wagons and articulated parts.
frosch <frosch@openttd.org>
parents: 17909
diff changeset
3837
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
3838 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
3839
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
3840 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
3841 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
3842
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
3843 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
3844
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
3845 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
3846
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3847 /* update destination */
8836
890a77315801 (svn r12584) -Codechange: do not access the order type directly.
rubidium <rubidium@openttd.org>
parents: 8832
diff changeset
3848 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
3849 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
3850 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
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
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
3853 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
3854 /* running costs */
10744
0118ca646690 (svn r15077) -Codechange: enumify DAYS_IN_YEAR and DAYS_IN_LEAP_YEAR
smatz <smatz@openttd.org>
parents: 10696
diff changeset
3855 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
3856
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
3857 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
3858 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
3859
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
3860 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
3861
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
3862 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
3863 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
3864 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3865 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3866 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
3867
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3868 Trackdir Train::GetVehicleTrackdir() const
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3869 {
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3870 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
3871
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
3872 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
3873 /* 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
3874 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
3875 }
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3876
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
3877 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
3878 /* 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
3879 return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3880 }
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
3881
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
3882 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
3883 }