annotate src/train_cmd.cpp @ 15956:cc4575d99aa1 draft

(svn r20644) -Codechange [FS#4086]: unify the vehicle breakdown code (Hirundo)
author rubidium <rubidium@openttd.org>
date Sat, 28 Aug 2010 14:01:50 +0000
parents d1aa55b7ebcf
children 1452fca58b4f
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"
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
13 #include "gui.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"
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
25 #include "functions.h"
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"
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
39
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
40 #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
41 #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
42
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
43 static Track ChooseTrainTrack(Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool force_res, bool *got_reservation, bool mark_stuck);
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
44 static bool TrainCheckIfLineEnds(Train *v);
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
45 static void TrainController(Train *v, Vehicle *nomove);
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
46 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
47 static void CheckIfTrainNeedsService(Train *v);
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
48 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
49
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
50 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
51 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
52
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 * 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
56 *
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
57 * @param direction vehicle direction
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
58 * @param track vehicle track bits
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
59 * @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
60 */
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
61 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
62 {
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
63 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
64
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
65 DiagDirection diagdir = DirToDiagDir(direction);
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
66
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
67 /* 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
68 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
69 diagdir = ChangeDiagDir(diagdir, DIAGDIRDIFF_90LEFT);
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
70 }
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 return diagdir;
836273f128b5 (svn r11812) -Codechange: introduce TrainExitDir() to simplify some functions
smatz <smatz@openttd.org>
parents: 8238
diff changeset
73 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
74
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
75
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
76 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
77 * 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
78 * @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
79 * @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
80 */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
81 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
82 {
12415
6a77d1df56e2 (svn r16849) -Codechange: replace GetCargo() by CargoSpec::Get()
smatz <smatz@openttd.org>
parents: 12351
diff changeset
83 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
84 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
85 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
86
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
87 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
88 * Logs a bug in GRF and shows a warning message if this
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
89 * is for the first time this happened.
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 * @param u first vehicle of chain
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
91 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
92 static void RailVehicleLengthChanged(const Train *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
93 {
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
94 /* show a warning once for each engine in whole game and once for each GRF after each game load */
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11920
diff changeset
95 const Engine *engine = Engine::Get(u->engine_type);
15763
a60a50f29f64 (svn r20437) -Codechange: lets Engines use GRFFilePropsBase as well
rubidium <rubidium@openttd.org>
parents: 15710
diff changeset
96 uint32 grfid = engine->grf_prop.grffile->grfid;
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
97 GRFConfig *grfconfig = GetGRFConfig(grfid);
15763
a60a50f29f64 (svn r20437) -Codechange: lets Engines use GRFFilePropsBase as well
rubidium <rubidium@openttd.org>
parents: 15710
diff changeset
98 if (GamelogGRFBugReverse(grfid, engine->grf_prop.local_id) || !HasBit(grfconfig->grf_bugs, GBUG_VEH_LENGTH)) {
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
99 ShowNewGrfVehicleError(u->engine_type, STR_NEWGRF_BROKEN, STR_NEWGRF_BROKEN_VEHICLE_LENGTH, GBUG_VEH_LENGTH, true);
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
100 }
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 }
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
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
103 /** 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
104 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
105 {
12035
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
106 const Train *v;
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
107
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
108 FOR_ALL_TRAINS(v) {
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
109 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
110 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
111 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
112 if ((w->track != TRACK_BIT_DEPOT &&
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
113 max(abs(u->x_pos - w->x_pos), abs(u->y_pos - w->y_pos)) != u->tcache.cached_veh_length) ||
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
114 (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
115 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
116 SetDParam(1, v->owner);
14645
b7a22979b84e (svn r19224) -Codechange: change parameters passed to ShowErrorMessage() a bit
smatz <smatz@openttd.org>
parents: 14520
diff changeset
117 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
118
11852
862dd1024fe7 (svn r16242) -Codechange: rework pausing
rubidium <rubidium@openttd.org>
parents: 11730
diff changeset
119 if (!_networking) DoCommandP(0, PM_PAUSED_ERROR, 1, CMD_PAUSE);
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
120 }
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
121 }
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
122 }
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
123 }
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
124 }
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
125 }
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
126
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
127 /**
15826
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
128 * 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
129 * 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
130 */
15826
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
131 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
132 {
15826
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
133 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
134 /* 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
135 u->UpdateVisualEffect(false);
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
136 }
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
137 this->PowerChanged();
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
138 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
139 }
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
140
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
141 /**
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
142 * Update the cached visual effect.
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
143 * @param allow_power_change true if the wagon-is-powered-state may change.
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
144 */
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
145 void Train::UpdateVisualEffect(bool allow_power_change)
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
146 {
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
147 byte powered_before = this->tcache.cached_vis_effect & 0x80;
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
148
15825
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
149 const Engine *e = Engine::Get(this->engine_type);
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
150 if (e->u.rail.visual_effect != 0) {
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
151 this->tcache.cached_vis_effect = e->u.rail.visual_effect;
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
152 } else {
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
153 if (this->IsWagon() || this->IsArticulatedPart()) {
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
154 /* Wagons and articulated parts have no effect by default */
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
155 this->tcache.cached_vis_effect = 0x40;
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
156 } else if (e->u.rail.engclass == 0) {
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
157 /* Steam is offset by -4 units */
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
158 this->tcache.cached_vis_effect = 4;
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
159 } else {
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
160 /* Diesel fumes and sparks come from the centre */
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
161 this->tcache.cached_vis_effect = 8;
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
162 }
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
163 }
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
164
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
165 /* Check powered wagon / visual effect callback */
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
166 if (HasBit(e->info.callback_mask, CBM_TRAIN_WAGON_POWER)) {
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
167 uint16 callback = GetVehicleCallback(CBID_TRAIN_WAGON_POWER, 0, 0, this->engine_type, this);
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
168
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
169 if (callback != CALLBACK_FAILED) this->tcache.cached_vis_effect = GB(callback, 0, 8);
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
170 }
15826
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
171
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
172 if (!allow_power_change && powered_before != (this->tcache.cached_vis_effect & 0x80)) {
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
173 this->tcache.cached_vis_effect ^= 0x80;
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
174 ShowNewGrfVehicleError(this->engine_type, STR_NEWGRF_BROKEN, STR_NEWGRF_BROKEN_POWERED_WAGON, GBUG_VEH_POWERED_WAGON, false);
7f692dcca929 (svn r20505) -Feature [FS#3978]: Allow changing visual effect when changing railtype.
frosch <frosch@openttd.org>
parents: 15825
diff changeset
175 }
15825
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
176 }
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
177
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
178 /**
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
179 * 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
180 * 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
181 * 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
182 * @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
183 */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
184 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
185 {
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
186 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
187
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
188 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
189
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
190 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
191 EngineID first_engine = this->IsFrontEngine() ? this->engine_type : INVALID_ENGINE;
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
192 this->tcache.cached_total_length = 0;
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
193 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
194
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
195 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
196
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
197 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
198 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
199
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
200 /* 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
201 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
202
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
203 /* update the 'first engine' */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
204 u->tcache.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
205 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
206
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
207 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
208
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
209 /* 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
210 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
211 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
212 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
213 }
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
214
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
215 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
216 /* 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
217 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
218 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
219 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
220 }
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
221
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
222 for (Train *u = this; u != NULL; u = u->Next()) {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11920
diff changeset
223 const Engine *e_u = Engine::Get(u->engine_type);
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
224 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
225
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
226 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
227
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
228 /* Cache wagon override sprite group. NULL is returned if there is none */
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
229 u->tcache.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->tcache.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
230
11085
8da1855e9f14 (svn r15428) -Codechange: consistently use colour instead of having both color and colour.
rubidium <rubidium@openttd.org>
parents: 11018
diff changeset
231 /* 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
232 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
233
15825
5a7d7a33055b (svn r20504) -Codechange: Move updating of train's visual effect to separate function.
frosch <frosch@openttd.org>
parents: 15820
diff changeset
234 /* 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
235 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
236
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
237 if (rvi_v->pow_wag_power != 0 && rvi_u->railveh_type == RAILVEH_WAGON &&
15552
683f2dbe75bb (svn r20211) -Codechange: Indented code should have curly braces around it.
alberth <alberth@openttd.org>
parents: 15449
diff changeset
238 UsesWagonOverride(u) && !HasBit(u->tcache.cached_vis_effect, 7)) {
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
239 /* 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
240 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
241 } 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
242 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
243 }
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
244
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
245 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
246 /* 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
247 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
248 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
249 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
250 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
251
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
252 /* 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
253 * 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
254 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
255 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
256 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
257 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
258
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
259 /* 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
260 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
261 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
262 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
263 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
264 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
265
13388
96253b319c10 (svn r17897) -Fix [FS#3255]: CB15 and CB36 (capacity) were not always called when they should.
frosch <frosch@openttd.org>
parents: 13385
diff changeset
266 u->cargo_cap = GetVehicleCapacity(u);
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
267
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
268 /* check the vehicle length (callback) */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
269 uint16 veh_len = CALLBACK_FAILED;
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
270 if (HasBit(e_u->info.callback_mask, CBM_VEHICLE_LENGTH)) {
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
271 veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, u->engine_type, 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
272 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
273 if (veh_len == CALLBACK_FAILED) veh_len = rvi_u->shorten_factor;
11435
5dc6443c46dd (svn r15793) -Feature: Allow train vehicles to be shorten to 1/8 length, even if not at the end of the train.
frosch <frosch@openttd.org>
parents: 11434
diff changeset
274 veh_len = 8 - Clamp(veh_len, 0, 7);
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
275
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
276 /* verify length hasn't changed */
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
277 if (same_length && veh_len != u->tcache.cached_veh_length) RailVehicleLengthChanged(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
278
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
279 /* update vehicle length? */
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
280 if (!same_length) u->tcache.cached_veh_length = veh_len;
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
281
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
282 this->tcache.cached_total_length += u->tcache.cached_veh_length;
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
283 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
284 u->InvalidateNewGRFCache();
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
285 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
286
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
287 /* store consist weight/max speed in cache */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
288 this->tcache.cached_max_speed = max_speed;
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
289 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
290 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
291
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
292 /* 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
293 this->CargoChanged();
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
294
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
295 if (this->IsFrontEngine()) {
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
296 this->UpdateAcceleration();
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
297 SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
15073
63aa13fe4f2c (svn r19699) -Codechange: Use InvalidateData at the refit window.
terkhen <terkhen@openttd.org>
parents: 15064
diff changeset
298 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
299 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
300 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
301
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
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 * 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
304 * 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
305 * @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
306 * @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
307 * @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
308 * @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
309 * @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
310 * @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
311 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
312 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
313 {
11922
0a4b63f3f3c3 (svn r16325) -Codechange: replace GetPoolItem(index) by PoolItem::Get(index)
smatz <smatz@openttd.org>
parents: 11920
diff changeset
314 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
315 *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
316 *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
317
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
318 /* 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
319 * 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
320 OrderStopLocation osl = OSL_PLATFORM_MIDDLE;
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
321 if (v->tcache.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
322 /* 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
323 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
324 } 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
325 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
326 }
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
327
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
328 /* 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
329 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
330 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
331 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
332
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
333 case OSL_PLATFORM_NEAR_END:
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
334 stop = v->tcache.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
335 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
336
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
337 case OSL_PLATFORM_MIDDLE:
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
338 stop = *station_length - (*station_length - v->tcache.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
339 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
340
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
341 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
342 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
343 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
344 }
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
345
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
346 /* Subtract half the front vehicle length of the train so we get the real
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
347 * stop location of the train. */
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
348 return stop - (v->tcache.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
349 }
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
350
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
351
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
352 /**
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
353 * 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
354 * @return imposed speed limit
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
355 */
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
356 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
357 {
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
358 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
359
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
360 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
361 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
362
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
363 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
364
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
365 int curvecount[2] = {0, 0};
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
366
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
367 /* first find the curve speed limit */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
368 int numcurve = 0;
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
369 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
370 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
371 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
372 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
373 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
374 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
375
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
376 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
377 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
378
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
379 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
380 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
381 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
382 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
383 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
384 sum += pos - lastpos;
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
385 if (pos - 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
386 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
387 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
388 }
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
389 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
390 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
391
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
392 /* 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
393 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
394 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
395 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
396 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
397
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
398 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
399 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
400 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
401 } else {
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
402 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
403 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
404 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
405 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
406
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
407 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
408 /* 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
409 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
410 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
411
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
412 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
413 /* 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
414 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
415 }
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
416 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
417
12085
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
418 return max_speed;
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
419 }
067bb705b7bc (svn r16494) -Codechange: cache train's speed limit caused by curves
smatz <smatz@openttd.org>
parents: 12082
diff changeset
420
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
421 /**
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
422 * 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
423 * @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
424 */
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 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
426 {
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
427 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
428 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
429
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
430 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
431 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
432 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
433 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
434 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
435 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
436
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
437 /* 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
438 * 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
439 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
440
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
441 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
442 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
443
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
444 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
445 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
446 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
447 }
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
448
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
449 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
450 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
451 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
452 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
453 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
454
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
455 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
456 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
457 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
458 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
459 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
460 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
461
14750
bf6990330311 (svn r19338) -Codechange: Move the acceleration cache to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14748
diff changeset
462 return min(max_speed, this->acc_cache.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
463 }
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
464
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
465 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
466 {
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
467 assert(this->IsFrontEngine());
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
468
14750
bf6990330311 (svn r19338) -Codechange: Move the acceleration cache to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14748
diff changeset
469 this->max_speed = this->acc_cache.cached_max_track_speed;
bf6990330311 (svn r19338) -Codechange: Move the acceleration cache to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14748
diff changeset
470
bf6990330311 (svn r19338) -Codechange: Move the acceleration cache to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14748
diff changeset
471 uint power = this->acc_cache.cached_power;
bf6990330311 (svn r19338) -Codechange: Move the acceleration cache to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14748
diff changeset
472 uint weight = this->acc_cache.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
473 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
474 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
475 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
476
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
477 /**
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
478 * 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
479 * @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
480 * @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
481 */
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
482 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
483 {
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
484 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
485 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
486
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
487 const Engine *e = Engine::Get(this->engine_type);
15763
a60a50f29f64 (svn r20437) -Codechange: lets Engines use GRFFilePropsBase as well
rubidium <rubidium@openttd.org>
parents: 15710
diff changeset
488 if (e->grf_prop.grffile != NULL && is_custom_sprite(e->u.rail.image_index)) {
a60a50f29f64 (svn r20437) -Codechange: lets Engines use GRFFilePropsBase as well
rubidium <rubidium@openttd.org>
parents: 15710
diff changeset
489 reference_width = e->grf_prop.grffile->traininfo_vehicle_width;
a60a50f29f64 (svn r20437) -Codechange: lets Engines use GRFFilePropsBase as well
rubidium <rubidium@openttd.org>
parents: 15710
diff changeset
490 vehicle_pitch = e->grf_prop.grffile->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
491 }
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
492
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
493 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
494 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
495 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
496 }
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 return this->tcache.cached_veh_length * reference_width / 8;
c3588e7cb8f6 (svn r16867) -Feature(ette): Turn variable 0E/8E (vertical offset for trains in depot) and variable 1E/9E bit 3 (wagon width in depot) into grf-local variables.
frosch <frosch@openttd.org>
parents: 12423
diff changeset
498 }
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
499
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
500 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
501 {
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
502 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
503 }
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
504
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
505 SpriteID Train::GetImage(Direction direction) 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
506 {
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
507 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
508 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
509
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
510 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
511
9022
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
512 if (is_custom_sprite(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
513 sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(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
514 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
515
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
516 spritenum = Engine::Get(this->engine_type)->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
517 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
518
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
519 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
520
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
521 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
522
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
523 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
524 }
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
525
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
526 static SpriteID GetRailIcon(EngineID engine, bool rear_head, int &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
527 {
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
528 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
529 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
530 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
531
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
532 if (is_custom_sprite(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
533 SpriteID sprite = GetCustomVehicleIcon(engine, dir);
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
534 if (sprite != 0) {
15763
a60a50f29f64 (svn r20437) -Codechange: lets Engines use GRFFilePropsBase as well
rubidium <rubidium@openttd.org>
parents: 15710
diff changeset
535 if (e->grf_prop.grffile != NULL) {
a60a50f29f64 (svn r20437) -Codechange: lets Engines use GRFFilePropsBase as well
rubidium <rubidium@openttd.org>
parents: 15710
diff changeset
536 y += e->grf_prop.grffile->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
537 }
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
538 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
539 }
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
540
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
541 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
542 }
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
543
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
544 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
545
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
546 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
547 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
548
14319
72cb3c72d619 (svn r18872) -Codechange: introduce PaletteID and use it
rubidium <rubidium@openttd.org>
parents: 14314
diff changeset
549 void DrawTrainEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal)
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
550 {
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
551 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
552 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
553 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
554
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
555 SpriteID spritef = GetRailIcon(engine, false, yf);
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
556 SpriteID spriter = GetRailIcon(engine, true, yr);
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
557 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
558 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
559
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
560 preferred_x = Clamp(preferred_x, left - real_spritef->x_offs + 14, right - real_spriter->width - real_spriter->x_offs - 15);
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
561
13597
706013b8e441 (svn r18121) -Codechange: pass the maximum left/right location to the Draw*Engine functions
rubidium <rubidium@openttd.org>
parents: 13492
diff changeset
562 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
563 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
564 } else {
db43607c4798 (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138 <peter1138@openttd.org>
parents: 9020
diff changeset
565 SpriteID sprite = GetRailIcon(engine, false, y);
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
566 const Sprite *real_sprite = GetSprite(sprite, 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
567 preferred_x = Clamp(preferred_x, left - real_sprite->x_offs, right - real_sprite->width - real_sprite->x_offs);
13597
706013b8e441 (svn r18121) -Codechange: pass the maximum left/right location to the Draw*Engine functions
rubidium <rubidium@openttd.org>
parents: 13492
diff changeset
568 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
569 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
570 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
571
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
572 /**
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
573 * 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
574 * @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
575 * @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
576 * @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
577 * @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
578 * @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
579 */
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
580 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
581 {
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
582 const RailVehicleInfo *rvi = &e->u.rail;
10930
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 /* 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
585 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
586
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
587 if (flags & DC_EXEC) {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
588 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
589 *ret = v;
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
590 v->spritenum = rvi->image_index;
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
591
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
592 v->engine_type = e->index;
13177
5d3ff7c33daf (svn r17684) -Fix: tcache.first_engine and rcache.first_engine need to be set before first callback/sprite-resolving. For RV fronts it was missing at all, causing livery selection to fail.
frosch <frosch@openttd.org>
parents: 13114
diff changeset
593 v->tcache.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
594
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
595 DiagDirection dir = GetRailDepotDirection(tile);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
596
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
597 v->direction = DiagDirToDir(dir);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
598 v->tile = tile;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
599
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
600 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
601 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
602
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
603 v->x_pos = x;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
604 v->y_pos = y;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
605 v->z_pos = GetSlopeZ(x, y);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
606 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
607 v->track = TRACK_BIT_DEPOT;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
608 v->vehstatus = VS_HIDDEN | VS_DEFPAL;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
609
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
610 v->SetWagon();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
611
13934
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
612 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
613 InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
614
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
615 v->cargo_type = e->GetDefaultCargoType();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
616 v->cargo_cap = rvi->capacity;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
617
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
618 v->railtype = rvi->railtype;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
619
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
620 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
621 v->cur_image = SPR_IMG_QUERY;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
622 v->random_bits = VehicleRandomBits();
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
623
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
624 v->group_id = DEFAULT_GROUP;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
625
13219
39e3a60fd47f (svn r17726) -Cleanup: Remove some unneeded/unused parameters.
frosch <frosch@openttd.org>
parents: 13218
diff changeset
626 AddArticulatedParts(v);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
627
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
628 _new_vehicle_id = v->index;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
629
11326
557dd16a7c24 (svn r15676) -Codechange: remove the need for BeginVehicleMove and merge VehiclePositionChanged and EndVehicleMove.
rubidium <rubidium@openttd.org>
parents: 11268
diff changeset
630 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
631 v->First()->ConsistChanged(false);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
632 UpdateTrainGroupID(v->First());
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
633
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
634 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
635
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
636 /* 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
637 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
638 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
639 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
640 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
641 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
642 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
643 !(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
644 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
645 break;
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
646 }
6c8c9b6e2b7b (svn r18466) -Codechange: use CmdMoveRailWagon to attach a just build wagon to a chain
rubidium <rubidium@openttd.org>
parents: 13933
diff changeset
647 }
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 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
649
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
650 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
651 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
652
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
653 /** 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
654 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
655 {
12035
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
656 const Train *v;
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
657 FOR_ALL_TRAINS(v) {
12301
218a41d3d557 (svn r16718) -Codechange: make IsFreeWagon() member of Train
smatz <smatz@openttd.org>
parents: 12300
diff changeset
658 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
659 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
660 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
661 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
662 break;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
663 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
664 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
665 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
666
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
667 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
668 {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
669 Train *u = new Train();
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
670 v->value >>= 1;
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
671 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
672 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
673 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
674 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
675 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
676 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
677 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
678 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
679 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
680 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
681 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
682 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
683 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
684 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
685 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
686 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
687 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
688 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
689 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
690 u->SetMultiheaded();
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
691 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
692 VehicleMove(u, false);
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
693
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
694 /* 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
695 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
696 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
697 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
698
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
699 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
700 * 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
701 * @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
702 * @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
703 * @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
704 * @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
705 * @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
706 * @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
707 */
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
708 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
709 {
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
710 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
711
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
712 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
713
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
714 /* 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
715 * 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
716 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
717
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
718 if (flags & DC_EXEC) {
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
719 DiagDirection dir = GetRailDepotDirection(tile);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
720 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
721 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
722
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
723 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
724 *ret = v;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
725 v->direction = DiagDirToDir(dir);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
726 v->tile = tile;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
727 v->owner = _current_company;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
728 v->x_pos = x;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
729 v->y_pos = y;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
730 v->z_pos = GetSlopeZ(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
731 v->track = TRACK_BIT_DEPOT;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
732 v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
733 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
734 v->cargo_type = e->GetDefaultCargoType();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
735 v->cargo_cap = rvi->capacity;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
736 v->max_speed = rvi->max_speed;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
737 v->last_station_visited = INVALID_STATION;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
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 v->engine_type = e->index;
13177
5d3ff7c33daf (svn r17684) -Fix: tcache.first_engine and rcache.first_engine need to be set before first callback/sprite-resolving. For RV fronts it was missing at all, causing livery selection to fail.
frosch <frosch@openttd.org>
parents: 13114
diff changeset
740 v->tcache.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
741
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
742 v->reliability = e->reliability;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
743 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
744 v->max_age = e->GetLifeLengthInDays();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
745
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
746 v->railtype = rvi->railtype;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
747 _new_vehicle_id = v->index;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
748
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
749 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
750 v->date_of_last_service = _date;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
751 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
752 v->cur_image = SPR_IMG_QUERY;
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
753 v->random_bits = VehicleRandomBits();
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
754
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
755 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
756
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
757 v->group_id = DEFAULT_GROUP;
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
758
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
759 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
760 v->SetEngine();
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
761
11326
557dd16a7c24 (svn r15676) -Codechange: remove the need for BeginVehicleMove and merge VehiclePositionChanged and EndVehicleMove.
rubidium <rubidium@openttd.org>
parents: 11268
diff changeset
762 VehicleMove(v, false);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
763
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
764 if (rvi->railveh_type == RAILVEH_MULTIHEAD) {
11902
5b2be5f84fe8 (svn r16302) -Codechange: remove Vehicle::AllocateList
smatz <smatz@openttd.org>
parents: 11901
diff changeset
765 AddRearEngineToMultiheadedTrain(v);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
766 } else {
13219
39e3a60fd47f (svn r17726) -Cleanup: Remove some unneeded/unused parameters.
frosch <frosch@openttd.org>
parents: 13218
diff changeset
767 AddArticulatedParts(v);
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
768 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
769
14261
2e1e5f37e2fb (svn r18812) -Codechange: make some functions in train.h functions of Train.
rubidium <rubidium@openttd.org>
parents: 14258
diff changeset
770 v->ConsistChanged(false);
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
771 UpdateTrainGroupID(v);
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
772
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
773 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
774 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
775 }
10930
34cbf99eb82c (svn r15269) -Codechange: reduced indentation in CmdBuildRail*
glx <glx@openttd.org>
parents: 10929
diff changeset
776
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
777 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
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
15849
f4ed8e905fee (svn r20531) -Codechange: unify quite a bit of the vehicle building commands
rubidium <rubidium@openttd.org>
parents: 15826
diff changeset
780 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
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
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
783
13948
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
784 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
785 {
13948
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
786 /* 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
787 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
788
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
789 /* 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
790 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
791 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
792 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
793
13948
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
794 return true;
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
795 }
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
796
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
797 bool Train::IsStoppedInDepot() const
1a7862d4528d (svn r18484) -Codechange: simplify the Is(Stopped)InDepot functions for trains
rubidium <rubidium@openttd.org>
parents: 13945
diff changeset
798 {
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
799 /* 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
800 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
801 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
802 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
803
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
804 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
805 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
806 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
807 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
808
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
809 Train *dst;
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
810 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
811 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
812 /* 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
813 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
814 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
815 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
816 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
817 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
818 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
819 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
820
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
821 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
822 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
823
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
824 /** 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
825 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
826
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
827 /**
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
828 * 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
829 * @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
830 * @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
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 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
833 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
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 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 * 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
839 * @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
840 */
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
841 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
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 /* 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
844 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
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 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
847 /* 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
848 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
849 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
850 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
851 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
852 } 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
853 /* 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
854 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
855 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
857 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
858 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
859
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /**
15002
baf0e12ad2b4 (svn r19614) -Codechange: "it's" => "its" where appropriate
smatz <smatz@openttd.org>
parents: 14898
diff changeset
861 * 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
862 * @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
863 * @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
864 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
866 {
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
867 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
868
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
869 /* 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
870 * 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
871 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
872
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
873 /* 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
874 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
875 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 * 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
879 * @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
880 * @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
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 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
883 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
884 /* 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
885 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
886
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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->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
888 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
889 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 * 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
893 * 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
894 * @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
895 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
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 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
899 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
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 /* 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
902 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
903 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
904
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
906
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
908 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
909 /* 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
910 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
911 }
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
912 }
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
913
13926
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
914 /**
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
915 * 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
916 * @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
917 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
918 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
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 /* 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
921 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
922
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
924 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
925
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
926 /* 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
927 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
928 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
929 } 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
930 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
931 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
932 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
934 /* 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
935 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
936 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
937 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
938 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
939 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
940
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 * 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
943 * @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
944 * @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
945 * @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
946 * @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
947 * @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
948 * @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
949 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
951 {
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
952 /* 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
953 * 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
954 * 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
955 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
956 (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
957 (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
958 (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
959 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
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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
963 * 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
964 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
965
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
967 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
969 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
970 * 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
971 * @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
972 * @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
973 */
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
974 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
975 {
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
976 /* 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
977 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
978
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
979 /* 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
980 * and if the result is negative the train is simply too long. */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 int allowed_len = _settings_game.vehicle.mammoth_trains ? 100 : 10;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
984 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
985
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
986 /* 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
987 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
988 allowed_len--;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
989 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
990
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
991 /* 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
992 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
993
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
994 while (t != 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
995 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
996
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
997 /* 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
998 * 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
999 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
1000
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1001 /* 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
1002 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
1003 allowed_len--; // We do not count articulated parts and rear heads either.
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* Back up and clear the first_engine data to avoid using wagon override group */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 EngineID first_engine = t->tcache.first_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
1007 t->tcache.first_engine = INVALID_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
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 /* 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
1010 * 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
1011 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
1012
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1013 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
1014
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1015 /* Restore original first_engine data */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1016 t->tcache.first_engine = first_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
1017
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1018 /* 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
1019 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
1020 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
1021
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
1023 /* 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
1024 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
1025
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1026 if (callback == 0xFD) error = STR_ERROR_INCOMPATIBLE_RAIL_TYPES;
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1027 if (callback < 0xFD) error = GetGRFStringID(GetEngineGRFID(head->engine_type), 0xD000 + callback);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1028
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1029 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
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 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
1034 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
1035 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
1036 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
1037 }
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
1038
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 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
1040 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
1041 }
693d82515df6 (svn r18458) -Codechange: make the 'check train length' code of CmdMoveRailVehicle a separate function
rubidium <rubidium@openttd.org>
parents: 13924
diff changeset
1042
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
1043 /**
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
1044 * 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
1045 * @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
1046 * @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
1047 * @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
1048 * @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
1049 * @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
1050 * @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
1051 * @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
1052 */
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
1053 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
1054 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1055 /* Check whether we 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
1056 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
1057 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
1058 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
1059 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
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 /* 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
1062 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
1063 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 * 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
1067 * @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
1068 * @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
1069 * @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
1070 * @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
1071 * @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
1072 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
1074 {
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
1075 /* 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
1076 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
1077 /* 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
1078 * 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
1079 *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
1080 } 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
1081 /* 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
1082 * 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
1083 *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
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 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
1087 /* 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
1088 * 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
1089 * 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
1090 * 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
1091 * 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
1092 * 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
1093 *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
1094 (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
1095 }
77e7637c04c8 (svn r18459) -Codechange: move the new train validity checking (attach callbacks) to a separate function
rubidium <rubidium@openttd.org>
parents: 13926
diff changeset
1096
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 /* 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
1098 * 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
1099 * 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
1100 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
1101 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
1102
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1103 /* 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
1104 * 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
1105 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
1106 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
1107 }
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1108
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1109 /**
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1110 * 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
1111 * 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
1112 * @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
1113 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1114 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
1115 {
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1116 /* 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
1117 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
1118
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1119 /* 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
1120 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
1121 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
1122
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1123 /* 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
1124 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
1125
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1126 /* Update the refit button and window */
15073
63aa13fe4f2c (svn r19699) -Codechange: Use InvalidateData at the refit window.
terkhen <terkhen@openttd.org>
parents: 15064
diff changeset
1127 InvalidateWindowData(WC_VEHICLE_REFIT, head->index);
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
1128 SetWindowWidgetDirty(WC_VEHICLE_VIEW, head->index, VVW_WIDGET_REFIT_VEH);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1129
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1130 /* 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
1131 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
1132 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
1133 }
77e7637c04c8 (svn r18459) -Codechange: move the new train validity checking (attach callbacks) to a separate function
rubidium <rubidium@openttd.org>
parents: 13926
diff changeset
1134
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1135 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1136 * 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
1137 * @param tile unused
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1138 * @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
1139 * 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
1140 * @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
1141 * - 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
1142 * - 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
1143 * @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
1144 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1145 * @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
1146 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1147 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
1148 {
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
1149 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
1150 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
1151 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
1152
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1153 Train *src = Train::GetIfValid(s);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1154 if (src == NULL) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1155
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1156 CommandCost ret = CheckOwnership(src->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1157 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
1158
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
1159 /* 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
1160 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
1161
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1162 /* 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
1163 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
1164 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
1165 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
1166 } else {
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1167 dst = Train::GetIfValid(d);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1168 if (dst == NULL) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1169
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1170 CommandCost ret = CheckOwnership(dst->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1171 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
1172
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
1173 /* 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
1174 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
1175 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1176
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1177 /* 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
1178 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
1179 if (dst != NULL) {
12450
af266072d46c (svn r16884) -Codechange: Add Train::GetFirstEnginePart() and use it.
frosch <frosch@openttd.org>
parents: 12433
diff changeset
1180 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
1181 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1182
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1183 /* 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
1184 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
1185
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1186 /* 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
1187 Train *src_head = src->First();
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1188 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
1189 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
1190 dst_head = dst->First();
6136
f05e23ab634f (svn r8878) -Fix
tron <tron@openttd.org>
parents: 6114
diff changeset
1191 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
1192 /* 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
1193 dst = dst->GetLastEnginePart();
6136
f05e23ab634f (svn r8878) -Fix
tron <tron@openttd.org>
parents: 6114
diff changeset
1194 } else {
f05e23ab634f (svn r8878) -Fix
tron <tron@openttd.org>
parents: 6114
diff changeset
1195 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
1196 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1197
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
1198 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
1199
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
1200 /* 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
1201 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
1202
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
1203 /* 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
1204 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
1205
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
1206 /* 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
1207 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
1208
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
1210 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
1211
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
1213 * 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
1214 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
1215 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
1216
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 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
1218 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
1219
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
1221 * 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
1222 * 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
1223 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
1224 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
1225
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* (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
1227 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
1228
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
1229 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
1230 /* 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
1231 * 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
1232 * 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
1233 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
1234 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
1235 /* 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
1236 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
1237 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
1238 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
1239 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1240 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1241
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1242 /* 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
1243 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
1244 /* 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
1245 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
1246 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
1247
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 /* 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
1249 * 1) front engine gets moved to a new train, it stays a front 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
1250 * a) the 'next' part is a wagon, that becomes a free wagon 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
1251 * b) the 'next' part is an engine, that becomes a front 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
1252 * 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
1253 * 2) front engine gets moved to another train, it is not a front engine 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
1254 * a) the 'next' part is a wagon, that becomes a free wagon 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
1255 * b) the 'next' part is an engine, that becomes a front 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
1256 * 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
1257 * 3) front engine gets moved to later in the current train, it is not an engine 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
1258 * a) the 'next' part is a wagon, that becomes a free wagon 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
1259 * b) the 'next' part is an engine, that becomes a front 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
1260 * 4) free wagon gets moved
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles 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 * a) the 'next' part is a wagon, that becomes a free wagon 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
1262 * b) the 'next' part is an engine, that becomes a front 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
1263 * 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
1264 * 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
1265 * 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
1266 * 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
1267 */
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1268 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
1269 /* 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
1270 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
1271 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
1272 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
1273 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
1274 DeleteWindowById(WC_VEHICLE_TIMETABLE, src->index);
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1275
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
1276 /* We are going to be move to another train. So we
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
1277 * are no part of this group anymore. In case we
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
1278 * are not moving group... well, then we do not need
13958
158476c4d72c (svn r18494) -Fix (r18474): crash when moving non-engine to new free wagon chain and group count was off when moving a front engine within the same train and the new front isn't an engine.
rubidium <rubidium@openttd.org>
parents: 13948
diff changeset
1279 * to move.
158476c4d72c (svn r18494) -Fix (r18474): crash when moving non-engine to new free wagon chain and group count was off when moving a front engine within the same train and the new front isn't an engine.
rubidium <rubidium@openttd.org>
parents: 13948
diff changeset
1280 * Or we are moving to later in the train and our
158476c4d72c (svn r18494) -Fix (r18474): crash when moving non-engine to new free wagon chain and group count was off when moving a front engine within the same train and the new front isn't an engine.
rubidium <rubidium@openttd.org>
parents: 13948
diff changeset
1281 * new head isn't a front engine anymore.
158476c4d72c (svn r18494) -Fix (r18474): crash when moving non-engine to new free wagon chain and group count was off when moving a front engine within the same train and the new front isn't an engine.
rubidium <rubidium@openttd.org>
parents: 13948
diff changeset
1282 */
158476c4d72c (svn r18494) -Fix (r18474): crash when moving non-engine to new free wagon chain and group count was off when moving a front engine within the same train and the new front isn't an engine.
rubidium <rubidium@openttd.org>
parents: 13948
diff changeset
1283 if (dst_head != NULL ? dst_head != src : !src_head->IsFrontEngine()) {
158476c4d72c (svn r18494) -Fix (r18474): crash when moving non-engine to new free wagon chain and group count was off when moving a front engine within the same train and the new front isn't an engine.
rubidium <rubidium@openttd.org>
parents: 13948
diff changeset
1284 DecreaseGroupNumVehicle(src->group_id);
158476c4d72c (svn r18494) -Fix (r18474): crash when moving non-engine to new free wagon chain and group count was off when moving a front engine within the same train and the new front isn't an engine.
rubidium <rubidium@openttd.org>
parents: 13948
diff changeset
1285 }
13942
06db5e527f36 (svn r18474) -Fix (r18470): in some odd corner cases the group (vehicle) counts could be off
rubidium <rubidium@openttd.org>
parents: 13941
diff changeset
1286
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
1287 /* 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
1288 * 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
1289 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
1290 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
1291 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
1292 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1293
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
1294 /* 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
1295 * 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
1296 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
1297 SetTrainGroupID(src, 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
1298 }
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
1299
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
1300 /* 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
1301 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
1302 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
1303
4dce922c787f (svn r18470) -Codechange/Fix [part of FS#3146]: moving vehicles around in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13934
diff changeset
1304 /* 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
1305 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
1306 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
1307 } 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
1308 /* 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
1309 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
1310 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
1311 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1312
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
1313 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
1314 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1315
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1316 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1317 * 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
1318 * @param flags type of operation
15850
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1319 * @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
1320 * @param data the selling mode
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1321 * - 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
1322 * - 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
1323 * 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
1324 * @param user the user for the order backup.
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1325 * @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
1326 */
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
1327 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
1328 {
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
1329 /* 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
1330 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
1331
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
1332 /* 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
1333 bool sell_chain = HasBit(data, 0);
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1334
efc03d77eb93 (svn r20532) -Codechange: unify selling of vehicles a bit
rubidium <rubidium@openttd.org>
parents: 15849
diff changeset
1335 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
1336 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
1337
12302
fc8e41f34b8c (svn r16719) -Codechange: make IsArticulatedPart(), IsTrainEngine(), IsTrainWagon(), IsMultiheaded(), EngineHasArticPart() and IsRearDualheaded() members of Train
smatz <smatz@openttd.org>
parents: 12301
diff changeset
1338 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
1339
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
1340 /* 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
1341 * 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
1342 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
1343 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
1344
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 /* 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
1346 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
1347 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
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 /* 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
1350 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
1351
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 /* 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
1353 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
1354 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
1355 /* 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
1356 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
1357 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
1358 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1359
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
1360 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
1361 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
1362
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1363 /* 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
1364 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
1365 /* 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
1366 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
1367
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 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
1369 /* 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
1370 * '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
1371 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
1372 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
1373 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
1374
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 /* 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
1376 new_head->CopyVehicleConfigAndStatistics(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
1377 IncreaseGroupNumVehicle(new_head->group_id);
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1378
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 /* 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
1380 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
1381 } 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
1382 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
1383 }
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1384
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1385 /* 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
1386 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
1387
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
1388 /* 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
1389 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
1390 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
1391
b76c3fa5c59d (svn r18472) -Fix [FS#3146]: selling vehicles in the depot could create states that are not allowed by the NewGRF attach callback.
rubidium <rubidium@openttd.org>
parents: 13938
diff changeset
1392 /* 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
1393 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
1394 } 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
1395 /* 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
1396 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
1397 }
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
1398
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1399 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
1400 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1401
6558
2fc024ee8217 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium <rubidium@openttd.org>
parents: 6554
diff changeset
1402 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
1403 {
6558
2fc024ee8217 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium <rubidium@openttd.org>
parents: 6554
diff changeset
1404 #define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 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
1405 static const uint32 _delta_xy_table[8] = {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1406 MKIT(3, 3, -1, -1),
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1407 MKIT(3, 7, -1, -3),
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1408 MKIT(3, 3, -1, -1),
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1409 MKIT(7, 3, -3, -1),
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1410 MKIT(3, 3, -1, -1),
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1411 MKIT(3, 7, -1, -3),
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1412 MKIT(3, 3, -1, -1),
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1413 MKIT(7, 3, -3, -1),
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1414 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1415 #undef MKIT
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1416
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1417 uint32 x = _delta_xy_table[direction];
6558
2fc024ee8217 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium <rubidium@openttd.org>
parents: 6554
diff changeset
1418 this->x_offs = GB(x, 0, 8);
2fc024ee8217 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium <rubidium@openttd.org>
parents: 6554
diff changeset
1419 this->y_offs = GB(x, 8, 8);
8793
27556e5b9388 (svn r12531) -Codechange: Rename some variables for consistency.
frosch <frosch@openttd.org>
parents: 8786
diff changeset
1420 this->x_extent = GB(x, 16, 8);
27556e5b9388 (svn r12531) -Codechange: Rename some variables for consistency.
frosch <frosch@openttd.org>
parents: 8786
diff changeset
1421 this->y_extent = GB(x, 24, 8);
27556e5b9388 (svn r12531) -Codechange: Rename some variables for consistency.
frosch <frosch@openttd.org>
parents: 8786
diff changeset
1422 this->z_extent = 6;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1423 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1424
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1425 static inline void SetLastSpeed(Train *v, int 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
1426 {
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
1427 int old = v->tcache.last_speed;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1428 if (spd != old) {
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
1429 v->tcache.last_speed = spd;
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
1430 if (_settings_client.gui.vehicle_speed || (old == 0) != (spd == 0)) {
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
1431 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
1432 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1433 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1434 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1435
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
1436 /** 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
1437 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
1438 {
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
1439 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
1440 /* 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
1441 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
1442
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
1443 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
1444
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
1445 /* 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
1446 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
1447 v->subspeed = 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
1448 SetLastSpeed(v, 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
1449
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
1450 SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
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
1451 }
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
1452 }
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
1453
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
1454 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
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 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
1457 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
1458
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1459 /* Clear the flags */
14753
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1460 ClrBit(*swap_flag1, GVF_GOINGUP_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1461 ClrBit(*swap_flag1, GVF_GOINGDOWN_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1462 ClrBit(*swap_flag2, GVF_GOINGUP_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1463 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
1464
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1465 /* 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
1466 if (HasBit(flag1, GVF_GOINGUP_BIT)) {
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1467 SetBit(*swap_flag2, GVF_GOINGDOWN_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1468 } 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
1469 SetBit(*swap_flag2, GVF_GOINGUP_BIT);
14768
f31d8dc443fc (svn r19356) -Fix: Whitespace.
frosch <frosch@openttd.org>
parents: 14754
diff changeset
1470 }
14753
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1471 if (HasBit(flag2, GVF_GOINGUP_BIT)) {
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1472 SetBit(*swap_flag1, GVF_GOINGDOWN_BIT);
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1473 } 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
1474 SetBit(*swap_flag1, GVF_GOINGUP_BIT);
14768
f31d8dc443fc (svn r19356) -Fix: Whitespace.
frosch <frosch@openttd.org>
parents: 14754
diff changeset
1475 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1476 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1477
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1478 static 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
1479 {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1480 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
1481
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1482 /* 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
1483 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
1484 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
1485
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1486 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
1487 /* 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
1488 {
14678
68f4a5f222ef (svn r19257) -Codechange: minor coding style fix
smatz <smatz@openttd.org>
parents: 14645
diff changeset
1489 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
1490 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
1491 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
1492 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1493
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
1494 Swap(a->track, b->track);
14680
f3dfa0c1c278 (svn r19260) -Codechange: another minor coding style fix
smatz <smatz@openttd.org>
parents: 14678
diff changeset
1495 Swap(a->direction, b->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
1496
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1497 /* toggle direction */
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
1498 if (a->track != TRACK_BIT_DEPOT) a->direction = ReverseDir(a->direction);
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
1499 if (b->track != TRACK_BIT_DEPOT) b->direction = ReverseDir(b->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
1500
5733
0b9175cb534a (svn r8276) -Fix
tron <tron@openttd.org>
parents: 5668
diff changeset
1501 Swap(a->x_pos, b->x_pos);
0b9175cb534a (svn r8276) -Fix
tron <tron@openttd.org>
parents: 5668
diff changeset
1502 Swap(a->y_pos, b->y_pos);
0b9175cb534a (svn r8276) -Fix
tron <tron@openttd.org>
parents: 5668
diff changeset
1503 Swap(a->tile, b->tile);
0b9175cb534a (svn r8276) -Fix
tron <tron@openttd.org>
parents: 5668
diff changeset
1504 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
1505
14753
3e27a2798ca0 (svn r19341) -Codechange: Move GOINGUP/GOINGDOWN flags to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14751
diff changeset
1506 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
1507
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1508 /* update other vars */
12666
0275245c6c62 (svn r17121) -Fix [FS#3060]: Update vehicle position cache when the vehicle sprite changes.
frosch <frosch@openttd.org>
parents: 12635
diff changeset
1509 a->UpdateViewport(true, true);
0275245c6c62 (svn r17121) -Fix [FS#3060]: Update vehicle position cache when the vehicle sprite changes.
frosch <frosch@openttd.org>
parents: 12635
diff changeset
1510 b->UpdateViewport(true, 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
1511
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1512 /* call the proper EnterTile function unless we are in a 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
1513 if (a->track != TRACK_BIT_WORMHOLE) VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
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
1514 if (b->track != TRACK_BIT_WORMHOLE) VehicleEnterTile(b, b->tile, b->x_pos, b->y_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
1515 } 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
1516 if (a->track != TRACK_BIT_DEPOT) a->direction = ReverseDir(a->direction);
12666
0275245c6c62 (svn r17121) -Fix [FS#3060]: Update vehicle position cache when the vehicle sprite changes.
frosch <frosch@openttd.org>
parents: 12635
diff changeset
1517 a->UpdateViewport(true, 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
1518
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
1519 if (a->track != TRACK_BIT_WORMHOLE) VehicleEnterTile(a, a->tile, a->x_pos, a->y_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
1520 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1521
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
1522 /* 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
1523 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
1524 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1525
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
1526
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1527 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1528 * 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
1529 * @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
1530 * @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
1531 */
9775
c3caa51b9888 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium <rubidium@openttd.org>
parents: 9774
diff changeset
1532 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
1533 {
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
1534 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
1535 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1536
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1537
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1538 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1539 * 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
1540 * @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
1541 * @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
1542 * @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
1543 */
9775
c3caa51b9888 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium <rubidium@openttd.org>
parents: 9774
diff changeset
1544 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
1545 {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1546 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
1547
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1548 Train *t = Train::From(v);
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1549 if (!t->IsFrontEngine()) return NULL;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1550
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1551 TileIndex tile = *(TileIndex *)data;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1552
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1553 if (TrainApproachingCrossingTile(t) != tile) return NULL;
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1554
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
1555 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
1556 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1557
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
1558
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1559 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1560 * 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
1561 * @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
1562 * @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
1563 * @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
1564 */
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
1565 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
1566 {
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
1567 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
1568
8598
ef2c93a9589d (svn r12179) -Codechange: use GetCrossingRailTrack() and GetCrossingRailAxis() to improve code readability
smatz <smatz@openttd.org>
parents: 8597
diff changeset
1569 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
1570 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
1571
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
1572 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
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 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
1575 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
1576
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
1577 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
1578 }
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1579
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
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1581 /**
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1582 * 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
1583 * @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
1584 * @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
1585 * @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
1586 */
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
1587 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
1588 {
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1589 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
1590
9824
fe3562a51648 (svn r13966) -Codechange [YAPP]: Bar level crossings upon path reservation. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9822
diff changeset
1591 /* 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
1592 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
1593
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
1594 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
1595 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
1596 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
1597 }
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
1598 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
1599 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
1600 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1601 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1602
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
1603
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1604 /**
8356
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1605 * 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
1606 * @param tile tile with crossing
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1607 * @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
1608 */
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1609 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
1610 {
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1611 if (!IsCrossingBarred(tile)) {
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1612 BarCrossing(tile);
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1613 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
1614 MarkTileDirtyByTile(tile);
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1615 }
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1616 }
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1617
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1618
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1619 /**
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1620 * 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
1621 * 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
1622 * @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
1623 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1624 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
1625 {
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1626 Train *base = v;
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1627 Train *first = base; // first vehicle to move
12460
4acae7d6f5ed (svn r16894) -Codechange: Add [Specialised]Vehicle::Last().
frosch <frosch@openttd.org>
parents: 12450
diff changeset
1628 Train *last = v->Last(); // last vehicle to move
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1629 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
1630
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1631 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
1632 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
1633 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
1634
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
1635 int differential = base->tcache.cached_veh_length - last->tcache.cached_veh_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
1636
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
1637 /* 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
1638 * 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
1639 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
1640
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
1641 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
1642 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
1643 }
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
1644 }
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
1645
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
1646
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
1647 /**
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
1648 * 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
1649 * 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
1650 * @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
1651 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1652 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
1653 {
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
1654 /* 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
1655 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
1656 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
1657 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
1658 }
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
1659
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1660 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
1661
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
1662 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
1663 /* '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
1664 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
1665
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
1666 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
1667 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
1668 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
1669 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
1670 }
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 } 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
1672 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
1673 }
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
1674
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1675 Train *base = v;
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1676 Train *first = base; // first vehicle to move
12460
4acae7d6f5ed (svn r16894) -Codechange: Add [Specialised]Vehicle::Last().
frosch <frosch@openttd.org>
parents: 12450
diff changeset
1677 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
1678 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
1679
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
1680 /* 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
1681 * 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
1682 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
1683
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 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
1685 /* 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
1686 * (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
1687 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
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 /* 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
1690 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
1691
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 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
1693 first = first->Next();
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1694
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
1695 int differential = last->tcache.cached_veh_length - base->tcache.cached_veh_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
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 /* 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
1698 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
1699
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 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
1701 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
1702 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1703 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1704
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1705
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1706 static 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
1707 {
8961
b33bcd7fa037 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz <smatz@openttd.org>
parents: 8954
diff changeset
1708 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
1709 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
1710 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1711
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
1712 /* 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
1713 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
1714
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1715 /* 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
1716 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
1717
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1718 /* count number of vehicles */
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
1719 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
1720
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
1721 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
1722
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1723 /* swap start<>end, start+1<>end-1, ... */
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1724 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
1725 do {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1726 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
1727 } 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
1728
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
1729 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
1730
8961
b33bcd7fa037 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz <smatz@openttd.org>
parents: 8954
diff changeset
1731 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
1732 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
1733 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1734
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
1735 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
1736
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
1737 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
1738
8667
4e73621c3950 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138 <peter1138@openttd.org>
parents: 8666
diff changeset
1739 /* 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
1740 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
1741
4e73621c3950 (svn r12332) -Fix (r12331): Invalidate cached data and update image after setting flag.
peter1138 <peter1138@openttd.org>
parents: 8666
diff changeset
1742 /* 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
1743 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
1744
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
1745 /* 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
1746 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
1747
6ed81efdeff6 (svn r11900) -Fix: set correctly crossing state after train reversal, train leaving crossing, train crash
smatz <smatz@openttd.org>
parents: 8317
diff changeset
1748 /* 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
1749 crossing = TrainApproachingCrossingTile(v);
8356
68587b4684d2 (svn r11922) -Codechange: use MaybeBarCrossingWithSound() to bar crossing with sound
smatz <smatz@openttd.org>
parents: 8350
diff changeset
1750 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
1751
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1752 /* 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
1753 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
1754 /* Can't be stuck here as inside a depot is always a safe tile. */
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
1755 if (HasBit(v->flags, VRF_TRAIN_STUCK)) SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
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
1756 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
1757 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
1758 }
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1759
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1760 /* 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
1761 * 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
1762 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
1763 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
1764
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1765 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
1766 /* 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
1767 * 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
1768 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
1769 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
1770 !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
1771
12502
9472b49c61a1 (svn r16939) -Codechange: s/RailwayStation/RailStation/ to unify the way it's written.
rubidium <rubidium@openttd.org>
parents: 12472
diff changeset
1772 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
1773 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
1774 /* 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
1775 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
1776 } 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
1777 /* 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
1778 MarkTrainAsStuck(v);
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1779 }
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
1780 } 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
1781 /* 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
1782 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
1783 v->wait_counter = 0;
9819
ec8a4c799a44 (svn r13961) -Add [YAPP]: Handle train reversing. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9818
diff changeset
1784 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1785 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1786
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1787 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1788 * 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
1789 * @param tile unused
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1790 * @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
1791 * @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
1792 * @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
1793 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1794 * @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
1795 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1796 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
1797 {
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1798 Train *v = Train::GetIfValid(p1);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1799 if (v == NULL) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1800
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1801 CommandCost ret = CheckOwnership(v->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1802 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
1803
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
1804 if (p2 != 0) {
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1805 /* 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
1806
13036
8e2a0e8fa275 (svn r17534) -Codechange: unify the naming of callback masks/flags
rubidium <rubidium@openttd.org>
parents: 13034
diff changeset
1807 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
1808 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
1809 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1810
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1811 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
1812 /* 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
1813 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
1814 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
1815 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1816
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1817 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
1818 ToggleBit(v->flags, VRF_REVERSE_DIRECTION);
13034
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
1819 SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
6eb3f749890a (svn r17532) -Codechange: Rename several Invalidate functions to SetDirty for more consistency and distinguishability.
frosch <frosch@openttd.org>
parents: 13001
diff changeset
1820 SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
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
1821 /* We cancel any 'skip signal at dangers' here */
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
1822 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
1823 SetWindowDirty(WC_VEHICLE_VIEW, 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
1824 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1825 } 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
1826 /* 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
1827 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
1828
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1829 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
1830 /* 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
1831 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
1832 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
1833 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
1834
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
1835 /* 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
1836 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
1837 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
1838 }
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
1839 }
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
1840
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
1841 /* We cancel any 'skip signal at dangers' here */
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
1842 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
1843 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
1844
14406
5f93639271ee (svn r18963) -Codechange: Give AccelerationModel a generical name.
terkhen <terkhen@openttd.org>
parents: 14384
diff changeset
1845 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
1846 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
1847 } else {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1848 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
1849 SetLastSpeed(v, 0);
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
1850 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
1851 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
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 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1854 }
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
1855 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
1856 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1857
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1858 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1859 * 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
1860 * @param tile unused
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1861 * @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
1862 * @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
1863 * @param p2 unused
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1864 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1865 * @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
1866 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1867 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
1868 {
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1869 Train *t = Train::GetIfValid(p1);
14815
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1870 if (t == NULL) return CMD_ERROR;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1871
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1872 CommandCost ret = CheckOwnership(t->owner);
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1873 if (ret.Failed()) return ret;
ad5c3430b2b2 (svn r19405) -Codechange: CheckOwnership() returns a CommandCost.
alberth <alberth@openttd.org>
parents: 14768
diff changeset
1874
12034
aa140a9c419c (svn r16441) -Codechange: new class SpecializedVehicle used as superclass for all vehicle types
smatz <smatz@openttd.org>
parents: 12032
diff changeset
1875
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
1876 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
1877 /* 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
1878 * 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
1879 * 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
1880 * 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
1881 * next signal we encounter. */
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
1882 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
1883 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
1884 }
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1885
6950
d2846442a133 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium <rubidium@openttd.org>
parents: 6943
diff changeset
1886 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
1887 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1888
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1889 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1890 * 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
1891 * crashed!
c62577640878 (svn r20286) -Codechange: Unify end of doxygen comments.
frosch <frosch@openttd.org>
parents: 15620
diff changeset
1892 */
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1893 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
1894 {
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1895 assert(!(v->vehstatus & VS_CRASHED));
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1896
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1897 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
1898
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
1899 PBSTileInfo origin = FollowTrainReservation(v);
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1900 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
1901
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
1902 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
1903 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
1904 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
1905
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1906 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
1907 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1908 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1909
8890
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
1910 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
1911 {
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
1912 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
1913 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
1914
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
1915 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
1916 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
1917 if (reverse != NULL) *reverse = tfdd.reverse;
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
1918
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
1919 return true;
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
1920 }
b1d45af72e04 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium <rubidium@openttd.org>
parents: 8862
diff changeset
1921
15620
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1922 /**
f8e9a3f0c140 (svn r20283) -Codechange: Unify start of doygen comments.
frosch <frosch@openttd.org>
parents: 15604
diff changeset
1923 * Send a train to 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
1924 * @param tile unused
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1925 * @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
1926 * @param p1 train to send to the depot
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1927 * @param p2 various bitmasked elements
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1928 * - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1929 * - p2 bit 8-10 - VLW flag (for mass goto depot)
13067
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1930 * @param text unused
082cdb4504ac (svn r17567) -Fix: some doxygen warnings
rubidium <rubidium@openttd.org>
parents: 13054
diff changeset
1931 * @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
1932 */
11090
df23c4e04638 (svn r15434) -Codechange: bit of type safety for the DC_xxx flags.
rubidium <rubidium@openttd.org>
parents: 11085
diff changeset
1933 CommandCost CmdSendTrainToDepot(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
1934 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1935 if (p2 & DEPOT_MASS_SEND) {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1936 /* Mass goto depot requested */
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1937 if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
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
1938 return SendAllVehiclesToDepot(VEH_TRAIN, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1939 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1940
12035
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
1941 Train *v = Train::GetIfValid(p1);
dcc31c1d758a (svn r16442) -Codechange: use new Vehicle accessors at more places
smatz <smatz@openttd.org>
parents: 12034
diff changeset
1942 if (v == NULL) return CMD_ERROR;
8891
384f81a7ba31 (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium <rubidium@openttd.org>
parents: 8890
diff changeset
1943
384f81a7ba31 (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium <rubidium@openttd.org>
parents: 8890
diff changeset
1944 return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1945 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1946
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1947 static const int8 _vehicle_smoke_pos[8] = {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1948 1, 1, 1, 0, -1, -1, -1, 0
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1949 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1950
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1951 static void HandleLocomotiveSmokeCloud(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
1952 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1953 bool sound = false;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1954
15710
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
1955 /* Do not show any locomotive smoke/sparks when smoke_amount is set to none (0) or train is:
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
1956 * slowing down or stopped (by the player) or
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
1957 * it is ordered to reverse direction (by player) so it is slowing down to do it or
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
1958 * its current speed is less than 2 km-ish/h or
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
1959 * it is entering station with an order to stop there and its speed is equal to maximum station entering speed. */
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
1960 if (_settings_game.vehicle.smoke_amount == 0 ||
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
1961 v->vehstatus & (VS_TRAIN_SLOWING | VS_STOPPED) ||
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
1962 HasBit(v->flags, VRF_REVERSING) ||
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
1963 v->cur_speed < 2 ||
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
1964 (IsRailStationTile(v->tile) && v->IsFrontEngine() && v->current_order.ShouldStopAtStation(v, GetStationIndex(v->tile)) &&
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
1965 v->cur_speed >= v->Train::GetCurrentMaxSpeed())) {
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1966 return;
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
1967 }
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
1968
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
1969 const Train *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
1970
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1971 do {
5823
4f8ecdfccbd3 (svn r8385) -Fix
tron <tron@openttd.org>
parents: 5769
diff changeset
1972 const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
11987
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
1973 int effect_offset = GB(v->tcache.cached_vis_effect, 0, 4) - 8;
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
1974 byte effect_type = GB(v->tcache.cached_vis_effect, 4, 2);
81554d4e6d76 (svn r16393) -Codechange: move VehicleRail to Train.
rubidium <rubidium@openttd.org>
parents: 11986
diff changeset
1975 bool disable_effect = HasBit(v->tcache.cached_vis_effect, 6);
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1976
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1977 /* no smoke? */
5868
941f6c5a4668 (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas <belugas@openttd.org>
parents: 5858
diff changeset
1978 if ((rvi->railveh_type == RAILVEH_WAGON && effect_type == 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
1979 disable_effect ||
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1980 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
1981 continue;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1982 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1983
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1984 /* No smoke in depots or tunnels */
8961
b33bcd7fa037 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz <smatz@openttd.org>
parents: 8954
diff changeset
1985 if (IsRailDepotTile(v->tile) || IsTunnelTile(v->tile)) continue;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1986
15820
59fd8293b2ee (svn r20499) -Doc: Spelling fixes, and one doxygen comment addition.
alberth <alberth@openttd.org>
parents: 15763
diff changeset
1987 /* No sparks for electric vehicles on non-electrified tracks. */
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
1988 if (!HasPowerOnRail(v->railtype, GetTileRailType(v->tile))) continue;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1989
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1990 if (effect_type == 0) {
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
1991 /* Use default effect type for engine class. */
5823
4f8ecdfccbd3 (svn r8385) -Fix
tron <tron@openttd.org>
parents: 5769
diff changeset
1992 effect_type = rvi->engclass;
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1993 } else {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1994 effect_type--;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1995 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
1996
6150
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1997 int x = _vehicle_smoke_pos[v->direction] * effect_offset;
a11ec555c68f (svn r8893) -Fix
tron <tron@openttd.org>
parents: 6141
diff changeset
1998 int y = _vehicle_smoke_pos[(v->direction + 2) % 8] * effect_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
1999
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
2000 if (HasBit(v->flags, VRF_REVERSE_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
2001 x = -x;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2002 y = -y;
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2003 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2004
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2005 switch (effect_type) {
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2006 case 0:
15710
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2007 /* Steam smoke - amount is gradually falling until train reaches its maximum speed, after that it's normal.
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2008 * Details: while train's current speed is gradually increasing, steam plumes' density decreases by one third each
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2009 * third of its maximum speed spectrum. Steam emission finally normalises at very close to train's maximum speed.
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2010 * REGULATION:
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2011 * - instead of 1, 4 / 2^smoke_amount (max. 2) is used to provide sufficient regulation to steam puffs' amount. */
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2012 if (GB(v->tick_counter, 0, ((4 >> _settings_game.vehicle.smoke_amount) + ((u->cur_speed * 3) / u->tcache.cached_max_speed))) == 0) {
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2013 CreateEffectVehicleRel(v, x, y, 10, EV_STEAM_SMOKE);
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2014 sound = true;
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2015 }
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2016 break;
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2017
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2018 case 1:
15710
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2019 /* Diesel smoke - thicker when train is starting, gradually subsiding till locomotive reaches its maximum speed
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2020 * when it stops.
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2021 * Details: Train's (max.) speed spectrum is divided into 32 parts. When max. speed is reached, chance for smoke
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2022 * emission erodes by 32 (1/4). Power and train's weight come in handy too to either increase smoke emission in
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2023 * 6 steps (1000HP each) if the power is low or decrease smoke emission in 6 steps (512 tonnes each) if the train
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2024 * isn't overweight. Power and weight contributions are expressed in a way that neither extreme power, nor
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2025 * extreme weight can ruin the balance (e.g. FreightWagonMultiplier) in the formula. When the train reaches
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2026 * maximum speed no diesel_smoke is emitted as train has enough traction to keep locomotive running optimally.
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2027 * REGULATION:
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2028 * - up to which speed a diesel train is emitting smoke (with reduced/small setting only until 1/2 of max_speed),
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2029 * - in Chance16 - the last value is 512 / 2^smoke_amount (max. smoke when 128 = smoke_amount of 2). */
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2030 if (u->cur_speed < (u->tcache.cached_max_speed >> (2 >> _settings_game.vehicle.smoke_amount)) &&
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2031 Chance16((64 - ((u->cur_speed << 5) / u->tcache.cached_max_speed) + (32 >> (u->acc_cache.cached_power >> 10)) - (32 >> (u->acc_cache.cached_weight >> 9))), (512 >> _settings_game.vehicle.smoke_amount))) {
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2032 CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE);
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2033 sound = true;
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2034 }
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2035 break;
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2036
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2037 case 2:
15710
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2038 /* Electric train's spark - more often occurs when train is departing (more load)
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2039 * Details: Electric locomotives are usually at least twice as powerful as their diesel counterparts, so spark
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2040 * emissions are kept simple. Only when starting, creating huge force are sparks more likely to happen, but when
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2041 * reaching its max. speed, quarter by quarter of it, chance decreases untill the usuall 2,22% at train's top speed.
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2042 * REGULATION:
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2043 * - in Chance16 the last value is 360 / 2^smoke_amount (max. sparks when 90 = smoke_amount of 2). */
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2044 if (GB(v->tick_counter, 0, 2) == 0 &&
642a03a3d91e (svn r20376) -Feature [FS#3093]: add setting for none/original/more smoke (SirkoZ)
yexo <yexo@openttd.org>
parents: 15623
diff changeset
2045 Chance16((6 - ((u->cur_speed << 2) / u->tcache.cached_max_speed)), (360 >> _settings_game.vehicle.smoke_amount))) {
8850
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2046 CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK);
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2047 sound = true;
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2048 }
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2049 break;
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2050
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2051 default:
4859a9c43de3 (svn r12605) -Cleanup: variable scope and coding style in train*
smatz <smatz@openttd.org>
parents: 8843
diff changeset
2052 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
2053 }
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
2054 } 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
2055
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2056 if (sound) PlayVehicleSound(u, VSE_TRAIN_EFFECT);
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2057 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2058
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
2059 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
2060 {
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2061 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
2062 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
2063 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
2064 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
2065 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
2066 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
2067 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2068
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
2069 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
2070
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
2071 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
2072 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
2073 }
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
2074
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
2075 /** 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
2076 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
2077 {
72806df62429 (svn r13958) -Add [YAPP]: Implement look-ahead for trains so they extend their reservation before reaching the end. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9813
diff changeset
2078 /* 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
2079 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
2080
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
2081 /* 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
2082 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
2083
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
2084 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
2085 /* 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
2086 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
2087 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
2088 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
2089
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
2090 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
2091 /* 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
2092 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
2093 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
2094
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
2095 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
2096 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
2097 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
2098 /* 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
2099 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
2100 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
2101
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
2102 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
2103 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
2104 }
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
2105 /* 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
2106 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
2107
11976
7f11f2a71193 (svn r16382) -Codechange: make GetVehicleTrackdir a member function of Vehicle.
rubidium <rubidium@openttd.org>
parents: 11972
diff changeset
2108 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
2109
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
2110 /* 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
2111 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
2112 !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
2113 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
2114
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
2115 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
2116 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
2117
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
2118 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
2119 /* 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
2120 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
2121 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
2122 /* 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
2123 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
2124 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
2125 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
2126 }
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
2127 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
2128 }
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
2129 }
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
2130 }
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
2131 }
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
2132
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2133 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
2134 {
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2135 /* 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
2136 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
2137 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
2138 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2139
6422
fb10eafe2a26 (svn r9558) -Documentation: doxygen and comment changes: 'T' now. Almost done
belugas <belugas@openttd.org>
parents: 6407
diff changeset
2140 /* if the train got no power, then keep it in the depot */
14750
bf6990330311 (svn r19338) -Codechange: Move the acceleration cache to GroundVehicle.
terkhen <terkhen@openttd.org>
parents: 14748
diff changeset
2141 if (v->acc_cache.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
2142 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
2143 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
2144 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
2145 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2146
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
2147 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
2148
15362
834b4f9842c2 (svn r20005) -Codechange: Enumify force_proceed.
frosch <frosch@openttd.org>
parents: 15351
diff changeset
2149 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
2150 /* 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
2151 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
2152 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
2153 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
2154 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2155
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
2156 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
2157
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
2158 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
2159 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
2160 /* 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
2161 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
2162 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
2163 }
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
2164 } 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
2165 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
2166 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2167
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
2168 /* 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
2169 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
2170 /* 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
2171 if (HasDepotReservation(v->tile)) return true;
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2172 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
2173 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
2174 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
2175 }
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
2176
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
2177 /* 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
2178 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
2179 /* 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
2180 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
2181 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
2182 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
2183 }
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
2184
12471
c0587ce9da72 (svn r16908) -Codechange: s/DepotWaypointReservation/DepotReservation/
rubidium <rubidium@openttd.org>
parents: 12470
diff changeset
2185 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
2186 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
2187
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2188 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
2189 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
2190 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
2191
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
2192 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
2193 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
2194
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2195 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
2196 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
2197
6558
2fc024ee8217 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium <rubidium@openttd.org>
parents: 6554
diff changeset
2198 v->UpdateDeltaXY(v->direction);
7134
e1c4f52f82cd (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium <rubidium@openttd.org>
parents: 7109
diff changeset
2199 v->cur_image = v->GetImage(v->direction);
11326
557dd16a7c24 (svn r15676) -Codechange: remove the need for BeginVehicleMove and merge VehiclePositionChanged and EndVehicleMove.
rubidium <rubidium@openttd.org>
parents: 11268
diff changeset
2200 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
2201 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
2202 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
2203 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
2204
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2205 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
2206 }
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2207
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
2208 /** 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
2209 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
2210 {
7a8655114c79 (svn r13951) -Codechange [YAPP]: A train leaving a tile should unreserve the track it came from if it was reserved. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9808
diff changeset
2211 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
2212
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
2213 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
2214 /* 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
2215 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
2216 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
2217
14707
ed6ca7f84893 (svn r19291) -Codechange: Rename HasVehicleOnTunnelBridge() to TunnelBridgeIsFree() and return a CommandCost status.
alberth <alberth@openttd.org>
parents: 14702
diff changeset
2218 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
2219 /* 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
2220 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
2221 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
2222
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
2223 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
2224 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
2225 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
2226 }
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
2227 }
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
2228 }
12502
9472b49c61a1 (svn r16939) -Codechange: s/RailwayStation/RailStation/ to unify the way it's written.
rubidium <rubidium@openttd.org>
parents: 12472
diff changeset
2229 } 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
2230 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
2231 /* 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
2232 * 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
2233 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
2234 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
2235 }
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
2236 } 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
2237 /* 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
2238 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
2239 }
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
2240 }
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
2241
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
2242 /** 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
2243 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
2244 {
12300
7ec79afe8c18 (svn r16717) -Codechange: make IsFrontEngine() member of Train
smatz <smatz@openttd.org>
parents: 12240
diff changeset
2245 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
2246
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2247 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
2248 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
2249 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
2250 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
2251
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2252 /* 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
2253 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
2254
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
2255 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
2256 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
2257 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
2258 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
2259 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
2260 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
2261
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2262 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
2263
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2264 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
2265 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
2266 /* 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
2267 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
2268 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
2269 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2270 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
2271 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
2272 /* 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
2273 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
2274 } 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
2275 /* 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
2276 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
2277 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
2278 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2279 } 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
2280 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
2281 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2282 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2283
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2284 /* 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
2285 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
2286
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2287 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
2288 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2289 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2290
5584
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2291 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
2292 {{ 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
2293 {{ 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
2294 {{ 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
2295 {{ 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
2296 {{ 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
2297 {{ 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
2298 };
4b26bd55bd24 (svn r8033) [cpp] - Prepare for merge from branches/cpp (all .c files renamed to .cpp)
KUDr <KUDr@openttd.org>
parents:
diff changeset
2299
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
2300 /**
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2301 * 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
2302 *
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2303 * @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
2304 * @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
2305 * @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
2306 * @param tracks Usable tracks on the new 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
2307 * @param path_not_found [out] Set to false if the pathfinder couldn't find a way to the destination
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
2308 * @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
2309 * @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
2310 * @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
2311 */
13842
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2312 static Track DoTrainPathfind(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool *path_not_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
2313 {
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
2314 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
2315 case VPF_NPF: return NPFTrainChooseTrack(v, tile, enterdir, tracks, path_not_found, do_track_reservation, dest);
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2316 case VPF_YAPF: return YapfTrainChooseTrack(v, tile, enterdir, tracks, path_not_found, do_track_reservation, dest);
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2317
72b2245c3b0d (svn r18371) -Codechange: unify calling of the train pathfinders
rubidium <rubidium@openttd.org>
parents: 13835
diff changeset
2318 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
2319 }
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 }
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2321
0e5451814e50 (svn r13952) -Add [YAPP]: Trains now reserve paths trough PBS signals. Bump savegame version. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9809
diff changeset
2322 /**
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
2323 * 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
2324 * 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
2325 * @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
2326 */
11985
e05790b0a6c6 (svn r16391) -Codechange: use Train instead of Vehicle where appropriate.
rubidium <rubidium@openttd.org>
parents: 11978
diff changeset
2327 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
2328 {
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2329 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
2330
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
2331 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
2332
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2333 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
2334 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
2335 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
2336 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
2337 /* 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
2338 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
2339 }
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2340
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
2341 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
2342 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
2343 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
2344 }
c7c20be56cee (svn r13959) -Codechange [YAPP]: Try to extend a train reservation as far as possible and only call into the pathfinder on track choices. (michi_cc)
rubidium <rubidium@openttd.org>
parents: 9816
diff changeset
2345
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
2346 /* 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
2347 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
2348 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
2349 /* 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
2350 * 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
2351 * 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
2352 * 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
2353 * 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
2354 * 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
2355 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)