src/roadveh_cmd.cpp
author rubidium
Thu, 18 Dec 2008 12:23:08 +0000
changeset 10436 8d3a9fbe8f19
parent 10328 7500e2e4c8ab
permissions -rw-r--r--
(svn r14689) -Change: make configure die on commonly made user mistakes, like not having SDL development files or zlib headers installed; you can still compile a dedicated server or a binary without zlib, but you have to explicitly force it.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
9111
48ce04029fe4 (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
parents: 9070
diff changeset
     3
/** @file roadveh_cmd.cpp Handling of road vehicles. */
6393
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
     4
3508
b685e9157aef (svn r4359) MSV6 Release mode now compiles (problem of UINT_MAX. stdafx.h should be the first include). Thanks to KUDr for solution and testing
belugas
parents: 3491
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1793
diff changeset
     6
#include "openttd.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1295
diff changeset
     7
#include "debug.h"
8119
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8116
diff changeset
     8
#include "tile_cmd.h"
6453
226bcddeba32 (svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents: 6407
diff changeset
     9
#include "landscape.h"
3144
33e42feae531 (svn r3763) Adapt to the new 'map accessors go in foo_map.h'-scheme
tron
parents: 3140
diff changeset
    10
#include "road_map.h"
3959
48f178431701 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3958
diff changeset
    11
#include "roadveh.h"
3315
c9d989783c55 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3236
diff changeset
    12
#include "station_map.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
    13
#include "command_func.h"
8785
871586967963 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8784
diff changeset
    14
#include "station_base.h"
8763
81fadd257f67 (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 8716
diff changeset
    15
#include "news_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include "pathfind.h"
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    17
#include "npf.h"
10208
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    18
#include "company_func.h"
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    19
#include "company_base.h"
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
    20
#include "bridge.h"
3184
118a520164e4 (svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents: 3169
diff changeset
    21
#include "tunnel_map.h"
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
    22
#include "bridge_map.h"
1752
d65cd19f7117 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1745
diff changeset
    23
#include "vehicle_gui.h"
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
    24
#include "articulated_vehicles.h"
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
    25
#include "newgrf_callbacks.h"
2962
f0a49b646c48 (svn r3524) - Split newgrf features from engine.[ch] into newgrf_engine.[ch], and add the new files to project files.
peter1138
parents: 2954
diff changeset
    26
#include "newgrf_engine.h"
4244
d40c73c55357 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
    27
#include "newgrf_text.h"
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
    28
#include "newgrf_sound.h"
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
    29
#include "yapf/yapf.h"
6329
a492d5e61d7b (svn r9301) -Codechange: Use cargo class to determine if a road vehicle can stop in bus or truck stops.
peter1138
parents: 6316
diff changeset
    30
#include "cargotype.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8108
diff changeset
    31
#include "strings_func.h"
8083
ad22eade501f (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 8081
diff changeset
    32
#include "tunnelbridge_map.h"
8131
160939e24ed3 (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
parents: 8119
diff changeset
    33
#include "functions.h"
160939e24ed3 (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
parents: 8119
diff changeset
    34
#include "window_func.h"
8140
0d0d8c94f84b (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8139
diff changeset
    35
#include "date_func.h"
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
    36
#include "vehicle_func.h"
8157
1505def01945 (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium
parents: 8145
diff changeset
    37
#include "sound_func.h"
8211
29a8510dfd62 (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8207
diff changeset
    38
#include "variables.h"
8212
cf3fce5c7464 (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium
parents: 8211
diff changeset
    39
#include "autoreplace_gui.h"
8224
c5a64d87cc54 (svn r11787) -Codechange: more header rewrites. This time related to viewport.h.
rubidium
parents: 8212
diff changeset
    40
#include "gfx_func.h"
8270
e7c342f6b14c (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8266
diff changeset
    41
#include "settings_type.h"
8784
c2e9d649a9ce (svn r12488) -Codechange: split order.h into order_base.h and order_func.h.
rubidium
parents: 8763
diff changeset
    42
#include "order_func.h"
8962
1b263c69799d (svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents: 8961
diff changeset
    43
#include "depot_base.h"
1b263c69799d (svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents: 8961
diff changeset
    44
#include "depot_func.h"
9009
6684576ef32b (svn r12804) -Codechange: move the effect vehicle handling out of vehicle.cpp
rubidium
parents: 9008
diff changeset
    45
#include "effectvehicle_func.h"
10122
c524226103f1 (svn r14306) -Codechange: unify the code to draw the vehicle list.
rubidium
parents: 10083
diff changeset
    46
#include "core/alloc_func.hpp"
8083
ad22eade501f (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 8081
diff changeset
    47
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8261
diff changeset
    48
#include "table/strings.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
static const uint16 _roadveh_images[63] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
	0xCD4, 0xCDC, 0xCE4, 0xCEC, 0xCF4, 0xCFC, 0xD0C, 0xD14,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
	0xD24, 0xD1C, 0xD2C, 0xD04, 0xD1C, 0xD24, 0xD6C, 0xD74,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
	0xD7C, 0xC14, 0xC1C, 0xC24, 0xC2C, 0xC34, 0xC3C, 0xC4C,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
	0xC54, 0xC64, 0xC5C, 0xC6C, 0xC44, 0xC5C, 0xC64, 0xCAC,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
	0xCB4, 0xCBC, 0xD94, 0xD9C, 0xDA4, 0xDAC, 0xDB4, 0xDBC,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
	0xDCC, 0xDD4, 0xDE4, 0xDDC, 0xDEC, 0xDC4, 0xDDC, 0xDE4,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
	0xE2C, 0xE34, 0xE3C, 0xC14, 0xC1C, 0xC2C, 0xC3C, 0xC4C,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
	0xC5C, 0xC64, 0xC6C, 0xC74, 0xC84, 0xC94, 0xCA4
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
static const uint16 _roadveh_full_adder[63] = {
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    62
	 0,  88,   0,   0,   0,   0,  48,  48,
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    63
	48,  48,   0,   0,  64,  64,   0,  16,
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    64
	16,   0,  88,   0,   0,   0,   0,  48,
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    65
	48,  48,  48,   0,   0,  64,  64,   0,
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    66
	16,  16,   0,  88,   0,   0,   0,   0,
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    67
	48,  48,  48,  48,   0,   0,  64,  64,
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    68
	 0,  16,  16,   0,   8,   8,   8,   8,
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    69
	 0,   0,   0,   8,   8,   8,   8
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    72
/** 'Convert' the DiagDirection where a road vehicle enters to the trackdirs it can drive onto */
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    73
static const TrackdirBits _road_enter_dir_to_reachable_trackdirs[DIAGDIR_END] = {
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    74
	TRACKDIR_BIT_LEFT_N  | TRACKDIR_BIT_LOWER_E | TRACKDIR_BIT_X_NE,    // Enter from north east
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    75
	TRACKDIR_BIT_LEFT_S  | TRACKDIR_BIT_UPPER_E | TRACKDIR_BIT_Y_SE,    // Enter from south east
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    76
	TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_X_SW    | TRACKDIR_BIT_RIGHT_S, // Enter from south west
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    77
	TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_Y_NW     // Enter from north west
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    80
static const Trackdir _road_reverse_table[DIAGDIR_END] = {
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    81
	TRACKDIR_RVREV_NE, TRACKDIR_RVREV_SE, TRACKDIR_RVREV_SW, TRACKDIR_RVREV_NW
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    84
/** 'Convert' the DiagDirection where a road vehicle should exit to
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    85
 * the trackdirs it can use to drive to the exit direction*/
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    86
static const TrackdirBits _road_exit_dir_to_incoming_trackdirs[DIAGDIR_END] = {
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    87
	TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_X_SW    | TRACKDIR_BIT_LEFT_S,
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    88
	TRACKDIR_BIT_LEFT_N  | TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_Y_NW,
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    89
	TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_UPPER_E | TRACKDIR_BIT_X_NE,
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
    90
	TRACKDIR_BIT_RIGHT_S | TRACKDIR_BIT_LOWER_E | TRACKDIR_BIT_Y_SE
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
6000
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
    93
/** Converts the exit direction of a depot to trackdir the vehicle is going to drive to */
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
    94
static const Trackdir _roadveh_depot_exit_trackdir[DIAGDIR_END] = {
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
    95
	TRACKDIR_X_NE, TRACKDIR_Y_SE, TRACKDIR_X_SW, TRACKDIR_Y_NW
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
    96
};
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
    97
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
    98
static SpriteID GetRoadVehIcon(EngineID engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
{
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   100
	uint8 spritenum = RoadVehInfo(engine)->image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   102
	if (is_custom_sprite(spritenum)) {
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   103
		SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   104
		if (sprite != 0) return sprite;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   105
9070
dd0121143eba (svn r12924) -Feature: Introducing the so called 'engine pool' which primarily removes the fixed engine type limits and also happens to allow (with the patch option 'dynamic_engines') multiple NewGRF vehicle sets to coexist.
peter1138
parents: 9022
diff changeset
   106
		spritenum = GetEngine(engine)->image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   108
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   109
	return 6 + _roadveh_images[spritenum];
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   110
}
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   111
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   112
SpriteID RoadVehicle::GetImage(Direction direction) const
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   113
{
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   114
	uint8 spritenum = this->spritenum;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   115
	SpriteID sprite;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   116
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   117
	if (is_custom_sprite(spritenum)) {
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   118
		sprite = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(spritenum)));
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   119
		if (sprite != 0) return sprite;
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   120
9070
dd0121143eba (svn r12924) -Feature: Introducing the so called 'engine pool' which primarily removes the fixed engine type limits and also happens to allow (with the patch option 'dynamic_engines') multiple NewGRF vehicle sets to coexist.
peter1138
parents: 9022
diff changeset
   121
		spritenum = GetEngine(this->engine_type)->image_index;
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   122
	}
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   123
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   124
	sprite = direction + _roadveh_images[spritenum];
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   125
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   126
	if (this->cargo.Count() >= this->cargo_cap / 2U) sprite += _roadveh_full_adder[spritenum];
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   127
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   128
	return sprite;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
   131
void DrawRoadVehEngine(int x, int y, EngineID engine, SpriteID pal)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
{
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
   133
	DrawSprite(GetRoadVehIcon(engine), pal, x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6879
diff changeset
   136
static CommandCost EstimateRoadVehCost(EngineID engine_type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
{
9923
3056bf623139 (svn r14078) -Codechange: Rename *VehicleInfo::base_cost to cost_factor, indicating what it actually is.
peter1138
parents: 9921
diff changeset
   138
	return CommandCost(EXPENSES_NEW_VEHICLES, ((_price.roadveh_base >> 3) * GetEngineProperty(engine_type, 0x11, RoadVehInfo(engine_type)->cost_factor)) >> 5);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   141
byte GetRoadVehLength(const Vehicle *v)
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   142
{
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   143
	byte length = 8;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   144
7215
7f90af37655d (svn r10493) -Codechange: update some callback ID enums to reflect their changed usage, add a few and update the comments.
rubidium
parents: 7135
diff changeset
   145
	uint16 veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, v->engine_type, v);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   146
	if (veh_len != CALLBACK_FAILED) {
7922
a7e266f966d9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 7909
diff changeset
   147
		length -= Clamp(veh_len, 0, 7);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   148
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   149
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   150
	return length;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   151
}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   152
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   153
void RoadVehUpdateCache(Vehicle *v)
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   154
{
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   155
	assert(v->type == VEH_ROAD);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   156
	assert(IsRoadVehFront(v));
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   157
7492
35acee076719 (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
parents: 7490
diff changeset
   158
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7492
diff changeset
   159
		/* Check the v->first cache. */
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7492
diff changeset
   160
		assert(u->First() == v);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   161
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   162
		/* Update the 'first engine' */
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   163
		u->u.road.first_engine = (v == u) ? INVALID_ENGINE : v->engine_type;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   164
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   165
		/* Update the length of the vehicle. */
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   166
		u->u.road.cached_veh_length = GetRoadVehLength(u);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   167
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   168
}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   169
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   170
/** Build a road vehicle.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   171
 * @param tile tile of depot where road vehicle is built
6483
ae78fc3c6b20 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6453
diff changeset
   172
 * @param flags operation to perform
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   173
 * @param p1 bus/truck type being built (engine)
9628
981ba3800c35 (svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz
parents: 9611
diff changeset
   174
 * @param p2 unused
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   175
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6879
diff changeset
   176
CommandCost CmdBuildRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
{
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6879
diff changeset
   178
	CommandCost cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
	Vehicle *v;
1282
ea2ae881814c (svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents: 1266
diff changeset
   180
	UnitID unit_num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
	Engine *e;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   182
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
   183
	if (!IsEngineBuildable(p1, VEH_ROAD, _current_company)) return_cmd_error(STR_ROAD_VEHICLE_NOT_AVAILABLE);
1196
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1151
diff changeset
   184
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
	cost = EstimateRoadVehCost(p1);
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   186
	if (flags & DC_QUERY_COST) return cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
1442
a166ae688746 (svn r1946) Fix: [AInew] Allow DC_QUERY_COST even on tiles with no depot built. This is used by the ai_new for deciding if it can afford the vehicles before actually building the depot. Was broken in r1728. Signed-Off-By: TrueLight ;-)
pasky
parents: 1401
diff changeset
   188
	/* The ai_new queries the vehicle cost before building the route,
a166ae688746 (svn r1946) Fix: [AInew] Allow DC_QUERY_COST even on tiles with no depot built. This is used by the ai_new for deciding if it can afford the vehicles before actually building the depot. Was broken in r1728. Signed-Off-By: TrueLight ;-)
pasky
parents: 1401
diff changeset
   189
	 * so we must check against cheaters no sooner than now. --pasky */
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   190
	if (!IsRoadDepotTile(tile)) return CMD_ERROR;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
   191
	if (!IsTileOwner(tile, _current_company)) return CMD_ERROR;
1442
a166ae688746 (svn r1946) Fix: [AInew] Allow DC_QUERY_COST even on tiles with no depot built. This is used by the ai_new for deciding if it can afford the vehicles before actually building the depot. Was broken in r1728. Signed-Off-By: TrueLight ;-)
pasky
parents: 1401
diff changeset
   192
8563
13b1a41e46f1 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 8556
diff changeset
   193
	if (HasTileRoadType(tile, ROADTYPE_TRAM) != HasBit(EngInfo(p1)->misc_flags, EF_ROAD_TRAM)) return_cmd_error(STR_DEPOT_WRONG_DEPOT_TYPE);
6709
2de208267583 (svn r9941) -Fix: trams could be cloned to appear in normal road depots and vice versa (spotted by Desolator)
bjarni
parents: 6695
diff changeset
   194
7595
9d645518f001 (svn r11122) -Fix [FS#1234]: crash when building a NewGRF vehicle when the articulated build vehicle callback returneed a different value for the purchase window than the normal build. Thanks for Dalestan and _minime_ for pointers to possible causes.
rubidium
parents: 7593
diff changeset
   195
	uint num_vehicles = 1 + CountArticulatedParts(p1, false);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   196
7595
9d645518f001 (svn r11122) -Fix [FS#1234]: crash when building a NewGRF vehicle when the articulated build vehicle callback returneed a different value for the purchase window than the normal build. Thanks for Dalestan and _minime_ for pointers to possible causes.
rubidium
parents: 7593
diff changeset
   197
	/* Allow for the front and the articulated parts, plus one to "terminate" the list. */
9488
963e9e278125 (svn r13456) -Codechange: use AllocaM() macro instead of alloca() at most places
smatz
parents: 9470
diff changeset
   198
	Vehicle **vl = AllocaM(Vehicle*, num_vehicles + 1);
7595
9d645518f001 (svn r11122) -Fix [FS#1234]: crash when building a NewGRF vehicle when the articulated build vehicle callback returneed a different value for the purchase window than the normal build. Thanks for Dalestan and _minime_ for pointers to possible causes.
rubidium
parents: 7593
diff changeset
   199
	memset(vl, 0, sizeof(*vl) * (num_vehicles + 1));
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   200
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7370
diff changeset
   201
	if (!Vehicle::AllocateList(vl, num_vehicles)) {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   202
		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   203
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   204
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   205
	v = vl[0];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
	/* find the first free roadveh id */
9628
981ba3800c35 (svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz
parents: 9611
diff changeset
   208
	unit_num = (flags & DC_AUTOREPLACE) ? 0 : GetFreeUnitNumber(VEH_ROAD);
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
   209
	if (unit_num > _settings_game.vehicle.max_roadveh)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   212
	if (flags & DC_EXEC) {
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   213
		int x;
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   214
		int y;
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   215
538
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   216
		const RoadVehicleInfo *rvi = RoadVehInfo(p1);
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   217
7782
e2999b5fcfad (svn r11332) -Fix: vehicles getting a value of 0 on construction.
rubidium
parents: 7768
diff changeset
   218
		v = new (v) RoadVehicle();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
		v->unitnumber = unit_num;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   220
		v->direction = DiagDirToDir(GetRoadDepotDirection(tile));
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
   221
		v->owner = _current_company;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   222
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   223
		v->tile = tile;
3645
7f950533d510 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3630
diff changeset
   224
		x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
7f950533d510 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3630
diff changeset
   225
		y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
		v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
		v->y_pos = y;
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6483
diff changeset
   228
		v->z_pos = GetSlopeZ(x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   229
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
   230
		v->running_ticks = 0;
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
   231
6000
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
   232
		v->u.road.state = RVSB_IN_DEPOT;
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6483
diff changeset
   233
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
538
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   235
		v->spritenum = rvi->image_index;
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   236
		v->cargo_type = rvi->cargo_type;
3870
d9ebc4ea750a (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3816
diff changeset
   237
		v->cargo_subtype = 0;
538
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   238
		v->cargo_cap = rvi->capacity;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   239
//		v->cargo_count = 0;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   240
		v->value = cost.GetCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
//		v->day_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
//		v->next_order_param = v->next_order = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
//		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   244
//		v->progress = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
//	v->u.road.unk2 = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
//	v->u.road.overtaking = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
1266
eccd576e322f (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
   249
		v->last_station_visited = INVALID_STATION;
538
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   250
		v->max_speed = rvi->max_speed;
8207
59649e32e130 (svn r11770) -Codechange: An engine type's type is EngineID, not byte.
peter1138
parents: 8157
diff changeset
   251
		v->engine_type = (EngineID)p1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
1926
530480d14685 (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1901
diff changeset
   253
		e = GetEngine(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   254
		v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
		v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
		v->max_age = e->lifelength * 366;
2564
24533c2db978 (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2552
diff changeset
   257
		_new_vehicle_id = v->index;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   258
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   259
		v->name = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
   261
		v->service_interval = _settings_game.vehicle.servint_roadveh;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   263
		v->date_of_last_service = _date;
4329
0e6e689f66e7 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   264
		v->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   265
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   266
		v->cur_image = 0xC15;
2804
ea4080819175 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2767
diff changeset
   267
		v->random_bits = VehicleRandomBits();
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   268
		SetRoadVehFront(v);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   269
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
   270
		v->u.road.roadtype = HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   271
		v->u.road.compatible_roadtypes = RoadTypeToRoadTypes(v->u.road.roadtype);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   272
		v->u.road.cached_veh_length = GetRoadVehLength(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
6176
153f0f24dc1b (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6175
diff changeset
   274
		v->vehicle_flags = 0;
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
   275
		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
6176
153f0f24dc1b (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6175
diff changeset
   276
6608
dc0b009642a4 (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
parents: 6594
diff changeset
   277
		v->cargo_cap = GetVehicleProperty(v, 0x0F, rvi->capacity);
dc0b009642a4 (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
parents: 6594
diff changeset
   278
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   279
		AddArticulatedParts(vl, VEH_ROAD);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   280
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
		VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4725
diff changeset
   283
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9234
diff changeset
   284
		InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
		InvalidateWindow(WC_COMPANY, v->owner);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
   286
		if (IsLocalCompany())
7425
d9bc116f2f54 (svn r10823) -Fix r8610 [FS#1097]: the autoreplace window vehicle count didn't always update correctly) (Matthias)
bjarni
parents: 7421
diff changeset
   287
			InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Road window
5944
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5943
diff changeset
   288
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
   289
		GetCompany(_current_company)->num_engines[p1]++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   290
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   291
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8226
diff changeset
   292
	return cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   295
void ClearSlot(Vehicle *v)
1664
1f909247678d (svn r2168) - Fix: Various stages of invisible trains, and wrong v->u.rail.track combinations. When a vehicle was sold its (possibly) assigned road slot for multislot was not cleared, thus resulting in a bug confusion. This should fix [ 1178520 ] Assertion failure in ai.c (invalid v->u.rail.track). With big thanks to TrueLight for the demo-recording patch, BJH for the wonderful savegame, Hackykid for his brilliant suggestions of the problem and Celestar for... ehm, making it all possible ;p
Darkvater
parents: 1530
diff changeset
   296
{
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   297
	RoadStop *rs = v->u.road.slot;
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   298
	if (v->u.road.slot == NULL) return;
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   299
1664
1f909247678d (svn r2168) - Fix: Various stages of invisible trains, and wrong v->u.rail.track combinations. When a vehicle was sold its (possibly) assigned road slot for multislot was not cleared, thus resulting in a bug confusion. This should fix [ 1178520 ] Assertion failure in ai.c (invalid v->u.rail.track). With big thanks to TrueLight for the demo-recording patch, BJH for the wonderful savegame, Hackykid for his brilliant suggestions of the problem and Celestar for... ehm, making it all possible ;p
Darkvater
parents: 1530
diff changeset
   300
	v->u.road.slot = NULL;
1f909247678d (svn r2168) - Fix: Various stages of invisible trains, and wrong v->u.rail.track combinations. When a vehicle was sold its (possibly) assigned road slot for multislot was not cleared, thus resulting in a bug confusion. This should fix [ 1178520 ] Assertion failure in ai.c (invalid v->u.rail.track). With big thanks to TrueLight for the demo-recording patch, BJH for the wonderful savegame, Hackykid for his brilliant suggestions of the problem and Celestar for... ehm, making it all possible ;p
Darkvater
parents: 1530
diff changeset
   301
	v->u.road.slot_age = 0;
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   302
3431
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   303
	assert(rs->num_vehicles != 0);
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   304
	rs->num_vehicles--;
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   305
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
   306
	DEBUG(ms, 3, "Clearing slot at 0x%X", rs->xy);
1664
1f909247678d (svn r2168) - Fix: Various stages of invisible trains, and wrong v->u.rail.track combinations. When a vehicle was sold its (possibly) assigned road slot for multislot was not cleared, thus resulting in a bug confusion. This should fix [ 1178520 ] Assertion failure in ai.c (invalid v->u.rail.track). With big thanks to TrueLight for the demo-recording patch, BJH for the wonderful savegame, Hackykid for his brilliant suggestions of the problem and Celestar for... ehm, making it all possible ;p
Darkvater
parents: 1530
diff changeset
   307
}
1f909247678d (svn r2168) - Fix: Various stages of invisible trains, and wrong v->u.rail.track combinations. When a vehicle was sold its (possibly) assigned road slot for multislot was not cleared, thus resulting in a bug confusion. This should fix [ 1178520 ] Assertion failure in ai.c (invalid v->u.rail.track). With big thanks to TrueLight for the demo-recording patch, BJH for the wonderful savegame, Hackykid for his brilliant suggestions of the problem and Celestar for... ehm, making it all possible ;p
Darkvater
parents: 1530
diff changeset
   308
9935
abf91482d3af (svn r14090) -Fix: the vehicle window of articulated road vehicles would show the clone/refit button when the vehicle was not completely stopped in the depot.
rubidium
parents: 9923
diff changeset
   309
bool RoadVehicle::IsStoppedInDepot() const
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   310
{
9935
abf91482d3af (svn r14090) -Fix: the vehicle window of articulated road vehicles would show the clone/refit button when the vehicle was not completely stopped in the depot.
rubidium
parents: 9923
diff changeset
   311
	TileIndex tile = this->tile;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   312
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   313
	if (!IsRoadDepotTile(tile)) return false;
9935
abf91482d3af (svn r14090) -Fix: the vehicle window of articulated road vehicles would show the clone/refit button when the vehicle was not completely stopped in the depot.
rubidium
parents: 9923
diff changeset
   314
	if (IsRoadVehFront(this) && !(this->vehstatus & VS_STOPPED)) return false;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   315
9935
abf91482d3af (svn r14090) -Fix: the vehicle window of articulated road vehicles would show the clone/refit button when the vehicle was not completely stopped in the depot.
rubidium
parents: 9923
diff changeset
   316
	for (const Vehicle *v = this; v != NULL; v = v->Next()) {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   317
		if (v->u.road.state != RVSB_IN_DEPOT || v->tile != tile) return false;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   318
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   319
	return true;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   320
}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   321
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   322
/** Sell a road vehicle.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   323
 * @param tile unused
6483
ae78fc3c6b20 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6453
diff changeset
   324
 * @param flags operation to perform
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   325
 * @param p1 vehicle ID to be sold
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   326
 * @param p2 unused
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   327
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6879
diff changeset
   328
CommandCost CmdSellRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   330
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   332
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   333
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 904
diff changeset
   334
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   335
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   336
	if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   337
7695
4b27fd86b318 (svn r11229) -Fix [FS#1307]: one could sell vehicles that were crashed in a depot, which would still yield money.
rubidium
parents: 7669
diff changeset
   338
	if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
4b27fd86b318 (svn r11229) -Fix [FS#1307]: one could sell vehicles that were crashed in a depot, which would still yield money.
rubidium
parents: 7669
diff changeset
   339
9935
abf91482d3af (svn r14090) -Fix: the vehicle window of articulated road vehicles would show the clone/refit button when the vehicle was not completely stopped in the depot.
rubidium
parents: 9923
diff changeset
   340
	if (!v->IsStoppedInDepot()) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   341
		return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   342
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   343
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8226
diff changeset
   344
	CommandCost ret(EXPENSES_NEW_VEHICLES, -v->value);
7768
0ba14389a50e (svn r11317) -Fix [FS#1355]: don't read a variable of a destroyed vehicle as the value will be reset to 0.
rubidium
parents: 7695
diff changeset
   345
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   347
		// Invalidate depot
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9234
diff changeset
   349
		InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
		InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   351
		DeleteWindowById(WC_VEHICLE_VIEW, v->index);
5256
9056fd4b30f4 (svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents: 5215
diff changeset
   352
		DeleteDepotHighlightOfVehicle(v);
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7370
diff changeset
   353
		delete v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   355
7768
0ba14389a50e (svn r11317) -Fix [FS#1355]: don't read a variable of a destroyed vehicle as the value will be reset to 0.
rubidium
parents: 7695
diff changeset
   356
	return ret;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   359
struct RoadFindDepotData {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
	uint best_length;
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
   361
	TileIndex tile;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   362
	OwnerByte owner;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   363
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   364
3186
4317b5b295c4 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3184
diff changeset
   365
static const DiagDirection _road_pf_directions[] = {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   366
	DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, INVALID_DIAGDIR, INVALID_DIAGDIR,
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   367
	DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, INVALID_DIAGDIR, INVALID_DIAGDIR
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
8611
9037a4227d67 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 8609
diff changeset
   370
static bool EnumRoadSignalFindDepot(TileIndex tile, void* data, Trackdir trackdir, uint length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   371
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   372
	RoadFindDepotData* rfdd = (RoadFindDepotData*)data;
3137
f8db55911726 (svn r3752) FYOS avoid to cast function pointers
tron
parents: 3136
diff changeset
   373
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   374
	tile += TileOffsByDiagDir(_road_pf_directions[trackdir]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
8563
13b1a41e46f1 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 8556
diff changeset
   376
	if (IsRoadDepotTile(tile) &&
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   377
			IsTileOwner(tile, rfdd->owner) &&
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   378
			length < rfdd->best_length) {
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   379
		rfdd->best_length = length;
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   380
		rfdd->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   383
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   385
static const Depot* FindClosestRoadDepot(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
{
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
   387
	switch (_settings_game.pf.pathfinder_for_roadvehs) {
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   388
		case VPF_YAPF: /* YAPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   389
			return YapfFindNearestRoadDepot(v);
1745
1113e1272ca2 (svn r2249) - Fix: [ 1187703 ] [NPF] Road vehicles cannot find a depot when in a roadstop
matthijs
parents: 1709
diff changeset
   390
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   391
		case VPF_NPF: { /* NPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   392
			/* See where we are now */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   393
			Trackdir trackdir = GetVehicleTrackdir(v);
3153
e83501906eae (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   394
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   395
			NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, v->tile, ReverseTrackdir(trackdir), false, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPES, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   396
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   397
			if (ftd.best_bird_dist == 0) return GetDepotByTile(ftd.node.tile); /* Target found */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   398
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   400
		default:
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   401
		case VPF_OPF: { /* OPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   402
			RoadFindDepotData rfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   404
			rfdd.owner = v->owner;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   405
			rfdd.best_length = UINT_MAX;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   406
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   407
			/* search in all directions */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   408
			for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
8800
c30102fee110 (svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents: 8793
diff changeset
   409
				FollowTrack(v->tile, PATHFIND_FLAGS_NONE, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, d, EnumRoadSignalFindDepot, NULL, &rfdd);
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   410
			}
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   411
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   412
			if (rfdd.best_length != UINT_MAX) return GetDepotByTile(rfdd.tile);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   413
		} break;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   414
	}
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   415
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   416
	return NULL; /* Target not found */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
8890
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   419
bool RoadVehicle::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   420
{
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   421
	const Depot *depot = FindClosestRoadDepot(this);
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   422
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   423
	if (depot == NULL) return false;
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   424
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   425
	if (location    != NULL) *location    = depot->xy;
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   426
	if (destination != NULL) *destination = depot->index;
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   427
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   428
	return true;
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   429
}
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   430
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   431
/** Send a road vehicle to the depot.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   432
 * @param tile unused
6483
ae78fc3c6b20 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6453
diff changeset
   433
 * @param flags operation to perform
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   434
 * @param p1 vehicle ID to send to the depot
4451
2e86da0081b5 (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4445
diff changeset
   435
 * @param p2 various bitmasked elements
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   436
 * - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   437
 * - p2 bit 8-10 - VLW flag (for mass goto depot)
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   438
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6879
diff changeset
   439
CommandCost CmdSendRoadVehToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
{
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   441
	if (p2 & DEPOT_MASS_SEND) {
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   442
		/* Mass goto depot requested */
4546
507b7d9bd834 (svn r6378) -Codechange: Rename VLW_FLAGS to VLW_MASK as it is a mask
Darkvater
parents: 4544
diff changeset
   443
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
   444
		return SendAllVehiclesToDepot(VEH_ROAD, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
4463
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   445
	}
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   446
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   447
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   448
8891
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8890
diff changeset
   449
	Vehicle *v = GetVehicle(p1);
4510
54d30a2372c3 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   450
8891
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8890
diff changeset
   451
	if (v->type != VEH_ROAD) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
8891
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8890
diff changeset
   453
	return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   456
/** Turn a roadvehicle around.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   457
 * @param tile unused
6483
ae78fc3c6b20 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6453
diff changeset
   458
 * @param flags operation to perform
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   459
 * @param p1 vehicle ID to turn
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   460
 * @param p2 unused
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   461
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6879
diff changeset
   462
CommandCost CmdTurnRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   465
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   466
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   467
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 904
diff changeset
   468
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   469
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   470
	if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   471
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
   472
	if (v->vehstatus & VS_STOPPED ||
7476
f6eb56e8b8a7 (svn r10983) -Codechange: use vehstatus & VS_CRASHED instead of some other "methods" custom to each vehicle to determine whether the vehicle is crashed.
rubidium
parents: 7469
diff changeset
   473
			v->vehstatus & VS_CRASHED ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
			v->breakdown_ctr != 0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   475
			v->u.road.overtaking != 0 ||
6000
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
   476
			v->u.road.state == RVSB_WORMHOLE ||
7490
bb27d92565d3 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7476
diff changeset
   477
			v->IsInDepot() ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   478
			v->cur_speed < 5) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   479
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   481
8563
13b1a41e46f1 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 8556
diff changeset
   482
	if (IsNormalRoadTile(v->tile) && GetDisallowedRoadDirections(v->tile) != DRD_NONE) return CMD_ERROR;
6764
d09d8d618a07 (svn r9999) -Feature: make it possible to disallow busses and lorries to go a specific way on straight pieces of road.
rubidium
parents: 6754
diff changeset
   483
8088
92fca5b09665 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8083
diff changeset
   484
	if (IsTileType(v->tile, MP_TUNNELBRIDGE) && DirToDiagDir(v->direction) == GetTunnelBridgeDirection(v->tile)) return CMD_ERROR;
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
   485
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   486
	if (flags & DC_EXEC) v->u.road.reverse_ctr = 180;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   487
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   488
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   489
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
6553
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
   492
void RoadVehicle::MarkDirty()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   493
{
7492
35acee076719 (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
parents: 7490
diff changeset
   494
	for (Vehicle *v = this; v != NULL; v = v->Next()) {
7419
531c4851ba85 (svn r10812) -Fix (r10097) [FS#1093]: Make RoadVehicle::MarkDirty() mark all parts of articulated vehicles as dirty, not just the front.
maedhros
parents: 7418
diff changeset
   495
		v->cur_image = v->GetImage(v->direction);
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8302
diff changeset
   496
		MarkSingleVehicleDirty(v);
7419
531c4851ba85 (svn r10812) -Fix (r10097) [FS#1093]: Make RoadVehicle::MarkDirty() mark all parts of articulated vehicles as dirty, not just the front.
maedhros
parents: 7418
diff changeset
   497
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   500
void RoadVehicle::UpdateDeltaXY(Direction direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
{
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   502
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
	static const uint32 _delta_xy_table[8] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   504
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   505
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   510
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   512
	};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
#undef MKIT
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   514
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   515
	uint32 x = _delta_xy_table[direction];
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   516
	this->x_offs        = GB(x,  0, 8);
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   517
	this->y_offs        = GB(x,  8, 8);
8793
5e35232593c3 (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 8786
diff changeset
   518
	this->x_extent      = GB(x, 16, 8);
5e35232593c3 (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 8786
diff changeset
   519
	this->y_extent      = GB(x, 24, 8);
5e35232593c3 (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 8786
diff changeset
   520
	this->z_extent      = 6;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   523
static void ClearCrashedStation(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
{
2671
2c2edd17e9cc (svn r3213) - Codechange: Clean up handling of road stops, avoiding unnecessary use of pointers and using the *BIT() macros.
peter1138
parents: 2662
diff changeset
   525
	RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
5990
280fbef32d44 (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 5989
diff changeset
   527
	/* Mark the station entrance as not busy */
280fbef32d44 (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 5989
diff changeset
   528
	rs->SetEntranceBusy(false);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   529
5990
280fbef32d44 (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 5989
diff changeset
   530
	/* Free the parking bay */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
   531
	rs->FreeBay(HasBit(v->u.road.state, RVS_USING_SECOND_BAY));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   532
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   533
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   534
static void DeleteLastRoadVeh(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
{
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   536
	Vehicle *u = v;
7492
35acee076719 (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
parents: 7490
diff changeset
   537
	for (; v->Next() != NULL; v = v->Next()) u = v;
35acee076719 (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
parents: 7490
diff changeset
   538
	u->SetNext(NULL);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   539
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
	DeleteWindowById(WC_VEHICLE_VIEW, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   541
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9234
diff changeset
   542
	InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
	InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   545
	if (IsTileType(v->tile, MP_STATION)) ClearCrashedStation(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   546
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8302
diff changeset
   547
	MarkSingleVehicleDirty(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   548
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7370
diff changeset
   549
	delete v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
static byte SetRoadVehPosition(Vehicle *v, int x, int y)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
	byte new_z, old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
6393
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
   556
	/* need this hint so it returns the right z coordinate on bridges. */
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   557
	v->x_pos = x;
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   558
	v->y_pos = y;
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   559
	new_z = GetSlopeZ(x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
	old_z = v->z_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   562
	v->z_pos = new_z;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   563
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   564
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
	return old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   569
static void RoadVehSetRandomDirection(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
{
3160
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   571
	static const DirDiff delta[] = {
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   572
		DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   573
	};
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   574
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   575
	do {
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   576
		uint32 r = Random();
3160
14b27357d353 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   577
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   578
		v->direction = ChangeDir(v->direction, delta[r & 3]);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   579
		BeginVehicleMove(v);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   580
		v->UpdateDeltaXY(v->direction);
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7096
diff changeset
   581
		v->cur_image = v->GetImage(v->direction);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   582
		SetRoadVehPosition(v, v->x_pos, v->y_pos);
7492
35acee076719 (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
parents: 7490
diff changeset
   583
	} while ((v = v->Next()) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   584
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
static void RoadVehIsCrashed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
	v->u.road.crashed_ctr++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
	if (v->u.road.crashed_ctr == 2) {
1359
52782e5cf7c9 (svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents: 1330
diff changeset
   590
		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
	} else if (v->u.road.crashed_ctr <= 45) {
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   592
		if ((v->tick_counter & 7) == 0) RoadVehSetRandomDirection(v);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   593
	} else if (v->u.road.crashed_ctr >= 2220 && !(v->tick_counter & 0x1F)) {
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   594
		DeleteLastRoadVeh(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
9775
22e256c3bf46 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium
parents: 9628
diff changeset
   598
static Vehicle *EnumCheckRoadVehCrashTrain(Vehicle *v, void *data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
{
9775
22e256c3bf46 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium
parents: 9628
diff changeset
   600
	const Vehicle *u = (Vehicle*)data;
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   601
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   602
	return
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   603
		v->type == VEH_TRAIN &&
7923
91982bd3c549 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 7922
diff changeset
   604
		abs(v->z_pos - u->z_pos) <= 6 &&
91982bd3c549 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 7922
diff changeset
   605
		abs(v->x_pos - u->x_pos) <= 4 &&
91982bd3c549 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 7922
diff changeset
   606
		abs(v->y_pos - u->y_pos) <= 4 ?
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   607
			v : NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   608
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
static void RoadVehCrash(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
{
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   612
	uint16 pass = 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
	v->u.road.crashed_ctr++;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   615
7492
35acee076719 (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
parents: 7490
diff changeset
   616
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6980
diff changeset
   617
		if (IsCargoInClass(u->cargo_type, CC_PASSENGERS)) pass += u->cargo.Count();
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   618
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   619
		u->vehstatus |= VS_CRASHED;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   620
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8302
diff changeset
   621
		MarkSingleVehicleDirty(u);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   622
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   623
3236
004a4e2bf30c (svn r3909) [multistop]
celestar
parents: 3186
diff changeset
   624
	ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8317
diff changeset
   626
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   628
	SetDParam(0, pass);
2260
10154bc4c650 (svn r2780) Remove some more unused strings and make the use of a few strings more explicit
tron
parents: 2214
diff changeset
   629
	AddNewsItem(
10154bc4c650 (svn r2780) Remove some more unused strings and make the use of a few strings more explicit
tron
parents: 2214
diff changeset
   630
		(pass == 1) ?
10154bc4c650 (svn r2780) Remove some more unused strings and make the use of a few strings more explicit
tron
parents: 2214
diff changeset
   631
			STR_9031_ROAD_VEHICLE_CRASH_DRIVER : STR_9032_ROAD_VEHICLE_CRASH_DIE,
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9224
diff changeset
   632
		NS_ACCIDENT_VEHICLE,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
		v->index,
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   634
		0
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   635
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
	ModifyStationRatingAround(v->tile, v->owner, -160, 22);
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 538
diff changeset
   638
	SndPlayVehicleFx(SND_12_EXPLOSION, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   640
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
static void RoadVehCheckTrainCrash(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
{
7492
35acee076719 (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
parents: 7490
diff changeset
   643
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   644
		if (u->u.road.state == RVSB_WORMHOLE) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   646
		TileIndex tile = u->tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   647
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   648
		if (!IsLevelCrossingTile(tile)) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   649
10083
eee4e42aa15b (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
parents: 9935
diff changeset
   650
		if (HasVehicleOnPosXY(v->x_pos, v->y_pos, u, EnumCheckRoadVehCrashTrain)) {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   651
			RoadVehCrash(v);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   652
			return;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   653
		}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   654
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   657
static void HandleBrokenRoadVeh(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   658
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   660
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   661
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   662
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   663
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   664
			v->breakdowns_since_last_service++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   665
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   666
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   667
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   668
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   669
		if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
   670
			SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ?
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   671
				SND_0F_VEHICLE_BREAKDOWN : SND_35_COMEDY_BREAKDOWN, v);
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   672
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   673
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   674
		if (!(v->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   675
			Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
9008
de19c73e405f (svn r12803) -Cleanup: rename SpecialVehicle to EffectVehicle to have a uniform naming of the thing instead of using both names for the same thing.
rubidium
parents: 9000
diff changeset
   676
			if (u != NULL) u->u.effect.animation_state = v->breakdown_delay * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   677
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   678
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   679
2989
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   680
	if ((v->tick_counter & 1) == 0) {
916f9443345f (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   681
		if (--v->breakdown_delay == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   682
			v->breakdown_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   683
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   684
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   685
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   686
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   687
8827
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   688
TileIndex RoadVehicle::GetOrderStationLocation(StationID station)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   689
{
8830
b288359ab100 (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 8827
diff changeset
   690
	if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
b288359ab100 (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 8827
diff changeset
   691
8827
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   692
	TileIndex dest = INVALID_TILE;
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   693
	const RoadStop *rs = GetStation(station)->GetPrimaryRoadStop(this);
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   694
	if (rs != NULL) {
9605
5a1897abd925 (svn r13647) -Codechange: replace MAX_UVALUE() for std types with the equivalent constant
skidd13
parents: 9560
diff changeset
   695
		uint mindist = UINT_MAX;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   696
8827
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   697
		for (; rs != NULL; rs = rs->GetNextRoadStop(this)) {
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   698
			uint dist = DistanceManhattan(this->tile, rs->xy);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   699
8827
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   700
			if (dist < mindist) {
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   701
				mindist = dist;
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   702
				dest = rs->xy;
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   703
			}
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   704
		}
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   705
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   706
8827
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   707
	if (dest != INVALID_TILE) {
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   708
		return dest;
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   709
	} else {
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   710
		/* There is no stop left at the station, so don't even TRY to go there */
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   711
		this->cur_order_index++;
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   712
		return 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   713
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   714
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   716
static void StartRoadVehSound(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   717
{
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   718
	if (!PlayVehicleSound(v, VSE_START)) {
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   719
		SoundFx s = RoadVehInfo(v->engine_type)->sfx;
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   720
		if (s == SND_19_BUS_START_PULL_AWAY && (v->tick_counter & 3) == 0)
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   721
			s = SND_1A_BUS_START_PULL_AWAY_WITH_HORN;
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   722
		SndPlayVehicleFx(s, v);
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   723
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   726
struct RoadVehFindData {
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   727
	int x;
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   728
	int y;
10083
eee4e42aa15b (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
parents: 9935
diff changeset
   729
	const Vehicle *veh;
eee4e42aa15b (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
parents: 9935
diff changeset
   730
	Vehicle *best;
eee4e42aa15b (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
parents: 9935
diff changeset
   731
	uint best_diff;
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   732
	Direction dir;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   733
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
9775
22e256c3bf46 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium
parents: 9628
diff changeset
   735
static Vehicle *EnumCheckRoadVehClose(Vehicle *v, void *data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
{
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   737
	static const int8 dist_x[] = { -4, -8, -4, -1, 4, 8, 4, 1 };
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   738
	static const int8 dist_y[] = { -4, -1, 4, 8, 4, 1, -4, -8 };
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   739
10083
eee4e42aa15b (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
parents: 9935
diff changeset
   740
	RoadVehFindData *rvf = (RoadVehFindData*)data;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   741
63
53cc59601b3c (svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents: 19
diff changeset
   742
	short x_diff = v->x_pos - rvf->x;
53cc59601b3c (svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents: 19
diff changeset
   743
	short y_diff = v->y_pos - rvf->y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   744
10083
eee4e42aa15b (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
parents: 9935
diff changeset
   745
	if (v->type == VEH_ROAD &&
eee4e42aa15b (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
parents: 9935
diff changeset
   746
			!v->IsInDepot() &&
eee4e42aa15b (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
parents: 9935
diff changeset
   747
			abs(v->z_pos - rvf->veh->z_pos) < 6 &&
eee4e42aa15b (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
parents: 9935
diff changeset
   748
			v->direction == rvf->dir &&
eee4e42aa15b (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
parents: 9935
diff changeset
   749
			rvf->veh->First() != v->First() &&
eee4e42aa15b (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
parents: 9935
diff changeset
   750
			(dist_x[v->direction] >= 0 || (x_diff > dist_x[v->direction] && x_diff <= 0)) &&
eee4e42aa15b (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
parents: 9935
diff changeset
   751
			(dist_x[v->direction] <= 0 || (x_diff < dist_x[v->direction] && x_diff >= 0)) &&
eee4e42aa15b (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
parents: 9935
diff changeset
   752
			(dist_y[v->direction] >= 0 || (y_diff > dist_y[v->direction] && y_diff <= 0)) &&
eee4e42aa15b (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
parents: 9935
diff changeset
   753
			(dist_y[v->direction] <= 0 || (y_diff < dist_y[v->direction] && y_diff >= 0))) {
eee4e42aa15b (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
parents: 9935
diff changeset
   754
		uint diff = abs(x_diff) + abs(y_diff);
eee4e42aa15b (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
parents: 9935
diff changeset
   755
eee4e42aa15b (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
parents: 9935
diff changeset
   756
		if (diff < rvf->best_diff || (diff == rvf->best_diff && v->index < rvf->best->index)) {
eee4e42aa15b (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
parents: 9935
diff changeset
   757
			rvf->best = v;
eee4e42aa15b (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
parents: 9935
diff changeset
   758
			rvf->best_diff = diff;
eee4e42aa15b (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
parents: 9935
diff changeset
   759
		}
eee4e42aa15b (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
parents: 9935
diff changeset
   760
	}
eee4e42aa15b (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
parents: 9935
diff changeset
   761
eee4e42aa15b (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
parents: 9935
diff changeset
   762
	return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   763
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   764
10083
eee4e42aa15b (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
parents: 9935
diff changeset
   765
static Vehicle *RoadVehFindCloseTo(Vehicle *v, int x, int y, Direction dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   766
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   767
	RoadVehFindData rvf;
7816
997b23172406 (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 7782
diff changeset
   768
	Vehicle *front = v->First();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   769
7816
997b23172406 (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 7782
diff changeset
   770
	if (front->u.road.reverse_ctr != 0) return NULL;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   771
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   772
	rvf.x = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   773
	rvf.y = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   774
	rvf.dir = dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   775
	rvf.veh = v;
10083
eee4e42aa15b (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
parents: 9935
diff changeset
   776
	rvf.best_diff = UINT_MAX;
eee4e42aa15b (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
parents: 9935
diff changeset
   777
7816
997b23172406 (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 7782
diff changeset
   778
	if (front->u.road.state == RVSB_WORMHOLE) {
10083
eee4e42aa15b (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
parents: 9935
diff changeset
   779
		FindVehicleOnPos(v->tile, &rvf, EnumCheckRoadVehClose);
eee4e42aa15b (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
parents: 9935
diff changeset
   780
		FindVehicleOnPos(GetOtherTunnelBridgeEnd(v->tile), &rvf, EnumCheckRoadVehClose);
7816
997b23172406 (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 7782
diff changeset
   781
	} else {
10083
eee4e42aa15b (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
parents: 9935
diff changeset
   782
		FindVehicleOnPosXY(x, y, &rvf, EnumCheckRoadVehClose);
7816
997b23172406 (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 7782
diff changeset
   783
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   784
6393
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
   785
	/* This code protects a roadvehicle from being blocked for ever
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
   786
	 * If more than 1480 / 74 days a road vehicle is blocked, it will
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
   787
	 * drive just through it. The ultimate backup-code of TTD.
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
   788
	 * It can be disabled. */
10083
eee4e42aa15b (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
parents: 9935
diff changeset
   789
	if (rvf.best_diff == UINT_MAX) {
7816
997b23172406 (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 7782
diff changeset
   790
		front->u.road.blocked_ctr = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   791
		return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   793
7816
997b23172406 (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 7782
diff changeset
   794
	if (++front->u.road.blocked_ctr > 1480) return NULL;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   795
10083
eee4e42aa15b (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
parents: 9935
diff changeset
   796
	return rvf.best;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   797
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   798
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   799
static void RoadVehArrivesAt(const Vehicle* v, Station* st)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   800
{
6329
a492d5e61d7b (svn r9301) -Codechange: Use cargo class to determine if a road vehicle can stop in bus or truck stops.
peter1138
parents: 6316
diff changeset
   801
	if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   802
		/* Check if station was ever visited before */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   803
		if (!(st->had_vehicle_of_type & HVOT_BUS)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   804
			st->had_vehicle_of_type |= HVOT_BUS;
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   805
			SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   806
			AddNewsItem(
8424
c3477565c975 (svn r11994) -Codechange: Remove numbers from string names where the strings aren't present in TTD, since they don't correspond to either TTD's TextIDs or OpenTTD's StringIDs.
maedhros
parents: 8392
diff changeset
   807
				v->u.road.roadtype == ROADTYPE_ROAD ? STR_902F_CITIZENS_CELEBRATE_FIRST : STR_CITIZENS_CELEBRATE_FIRST_PASSENGER_TRAM,
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
   808
				(v->owner == _local_company) ? NS_ARRIVAL_COMPANY : NS_ARRIVAL_OTHER,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   809
				v->index,
10123
f73d96619303 (svn r14307) -Fix: when deleting a station, remove news items regarding it
smatz
parents: 10122
diff changeset
   810
				st->index
f73d96619303 (svn r14307) -Fix: when deleting a station, remove news items regarding it
smatz
parents: 10122
diff changeset
   811
			);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   812
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   813
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   814
		/* Check if station was ever visited before */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   815
		if (!(st->had_vehicle_of_type & HVOT_TRUCK)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
			st->had_vehicle_of_type |= HVOT_TRUCK;
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   817
			SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   818
			AddNewsItem(
8424
c3477565c975 (svn r11994) -Codechange: Remove numbers from string names where the strings aren't present in TTD, since they don't correspond to either TTD's TextIDs or OpenTTD's StringIDs.
maedhros
parents: 8392
diff changeset
   819
				v->u.road.roadtype == ROADTYPE_ROAD ? STR_9030_CITIZENS_CELEBRATE_FIRST : STR_CITIZENS_CELEBRATE_FIRST_CARGO_TRAM,
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
   820
				(v->owner == _local_company) ? NS_ARRIVAL_COMPANY : NS_ARRIVAL_OTHER,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
				v->index,
10123
f73d96619303 (svn r14307) -Fix: when deleting a station, remove news items regarding it
smatz
parents: 10122
diff changeset
   822
				st->index
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   823
			);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   824
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   825
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   827
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
static bool RoadVehAccelerate(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
{
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   830
	uint spd = v->cur_speed + 1 + (v->u.road.overtaking != 0 ? 1 : 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   831
	byte t;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   832
6393
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
   833
	/* Clamp */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
	spd = min(spd, v->max_speed);
6141
bd1637f172b4 (svn r8884) -Fix
tron
parents: 6131
diff changeset
   835
	if (v->u.road.state == RVSB_WORMHOLE && !(v->vehstatus & VS_HIDDEN)) {
8491
97fcf662c3c3 (svn r12066) -Codechange: Rename GetBridge for the more common GetBridgeSpec
belugas
parents: 8467
diff changeset
   836
		spd = min(spd, GetBridgeSpec(GetBridgeType(v->tile))->speed * 2);
6141
bd1637f172b4 (svn r8884) -Fix
tron
parents: 6131
diff changeset
   837
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   838
6393
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
   839
	/* updates statusbar only if speed have changed to save CPU time */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
	if (spd != v->cur_speed) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   841
		v->cur_speed = spd;
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
   842
		if (_settings_client.gui.vehicle_speed) {
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8317
diff changeset
   843
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   844
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   845
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   846
6393
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
   847
	/* Decrease somewhat when turning */
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   848
	if (!(v->direction & 1)) spd = spd * 3 >> 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   849
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   850
	if (spd == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   852
	if ((byte)++spd == 0) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   853
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   854
	v->progress = (t = v->progress) - (byte)spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   855
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   856
	return (t < v->progress);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   857
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   858
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   859
static Direction RoadVehGetNewDirection(const Vehicle* v, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   860
{
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   861
	static const Direction _roadveh_new_dir[] = {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   862
		DIR_N , DIR_NW, DIR_W , INVALID_DIR,
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   863
		DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   864
		DIR_E , DIR_SE, DIR_S
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   865
	};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   866
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   867
	x = x - v->x_pos + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   868
	y = y - v->y_pos + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   869
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   870
	if ((uint)x > 2 || (uint)y > 2) return v->direction;
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   871
	return _roadveh_new_dir[y * 4 + x];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   872
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   873
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   874
static Direction RoadVehGetSlidingDirection(const Vehicle* v, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   876
	Direction new_dir = RoadVehGetNewDirection(v, x, y);
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   877
	Direction old_dir = v->direction;
3159
b4a664574e8f (svn r3785) Replace some if-magic to determine the turning direction for road vehicles with the new DirDiff stuff
tron
parents: 3157
diff changeset
   878
	DirDiff delta;
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   879
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   880
	if (new_dir == old_dir) return old_dir;
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   881
	delta = (DirDifference(new_dir, old_dir) > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   882
	return ChangeDir(old_dir, delta);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   883
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   884
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   885
struct OvertakeData {
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   886
	const Vehicle* u;
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   887
	const Vehicle* v;
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
   888
	TileIndex tile;
8609
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   889
	Trackdir trackdir;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   890
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   891
9775
22e256c3bf46 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium
parents: 9628
diff changeset
   892
static Vehicle *EnumFindVehBlockingOvertake(Vehicle *v, void* data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   894
	const OvertakeData* od = (OvertakeData*)data;
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   895
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   896
	return
8081
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8060
diff changeset
   897
		v->type == VEH_ROAD && v->First() == v && v != od->u && v != od->v ?
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   898
			v : NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   899
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
8609
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   901
/**
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   902
 * Check if overtaking is possible on a piece of track
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   903
 *
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   904
 * @param od Information about the tile and the involved vehicles
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   905
 * @return true if we have to abort overtaking
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   906
 */
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   907
static bool CheckRoadBlockedForOvertaking(OvertakeData *od)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
{
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8611
diff changeset
   909
	TrackStatus ts = GetTileTrackStatus(od->tile, TRANSPORT_ROAD, od->v->u.road.compatible_roadtypes);
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8611
diff changeset
   910
	TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts);
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8611
diff changeset
   911
	TrackdirBits red_signals = TrackStatusToRedSignals(ts); // barred level crossing
8609
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   912
	TrackBits trackbits = TrackdirBitsToTrackBits(trackdirbits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   913
8609
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   914
	/* Track does not continue along overtaking direction || track has junction || levelcrossing is barred */
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   915
	if (!HasBit(trackdirbits, od->trackdir) || (trackbits & ~TRACK_BIT_CROSS) || (red_signals != TRACKDIR_BIT_NONE)) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   916
8609
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   917
	/* Are there more vehicles on the tile except the two vehicles involved in overtaking */
10083
eee4e42aa15b (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
parents: 9935
diff changeset
   918
	return HasVehicleOnPos(od->tile, od, EnumFindVehBlockingOvertake);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   919
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   920
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   921
static void RoadVehCheckOvertake(Vehicle *v, Vehicle *u)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   922
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
	OvertakeData od;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   925
	od.v = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   926
	od.u = u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   927
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   928
	if (u->max_speed >= v->max_speed &&
3431
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   929
			!(u->vehstatus & VS_STOPPED) &&
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   930
			u->cur_speed != 0) {
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   931
		return;
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   932
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
6687
c7b92f03d33f (svn r9919) -Codechange: prepare some more places for more road types.
rubidium
parents: 6685
diff changeset
   934
	/* Trams can't overtake other trams */
c7b92f03d33f (svn r9919) -Codechange: prepare some more places for more road types.
rubidium
parents: 6685
diff changeset
   935
	if (v->u.road.roadtype == ROADTYPE_TRAM) return;
c7b92f03d33f (svn r9919) -Codechange: prepare some more places for more road types.
rubidium
parents: 6685
diff changeset
   936
7669
37d063010a57 (svn r11200) -Fix [FS#1291]: road vehicles could not overtake on one way roads going to the east.
rubidium
parents: 7642
diff changeset
   937
	/* Don't overtake in stations */
37d063010a57 (svn r11200) -Fix [FS#1291]: road vehicles could not overtake on one way roads going to the east.
rubidium
parents: 7642
diff changeset
   938
	if (IsTileType(v->tile, MP_STATION)) return;
37d063010a57 (svn r11200) -Fix [FS#1291]: road vehicles could not overtake on one way roads going to the east.
rubidium
parents: 7642
diff changeset
   939
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   940
	/* For now, articulated road vehicles can't overtake anything. */
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   941
	if (RoadVehHasArticPart(v)) return;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
   942
8609
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   943
	/* Vehicles are not driving in same direction || direction is not a diagonal direction */
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   944
	if (v->direction != u->direction || !(v->direction & 1)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
6000
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
   946
	/* Check if vehicle is in a road stop, depot, tunnel or bridge or not on a straight road */
6131
eaeaa5da9989 (svn r8868) -Fix (r8715): road vehicles could not overtake anymore in some directions (mart3p).
rubidium
parents: 6012
diff changeset
   947
	if (v->u.road.state >= RVSB_IN_ROAD_STOP || !IsStraightRoadTrackdir((Trackdir)(v->u.road.state & RVSB_TRACKDIR_MASK))) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
9224
93528d9cb96d (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9111
diff changeset
   949
	od.trackdir = DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
7669
37d063010a57 (svn r11200) -Fix [FS#1291]: road vehicles could not overtake on one way roads going to the east.
rubidium
parents: 7642
diff changeset
   950
8609
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   951
	/* Are the current and the next tile suitable for overtaking?
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   952
	 *  - Does the track continue along od.trackdir
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   953
	 *  - No junctions
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   954
	 *  - No barred levelcrossing
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   955
	 *  - No other vehicles in the way
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   956
	 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   957
	od.tile = v->tile;
8609
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   958
	if (CheckRoadBlockedForOvertaking(&od)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   959
4559
aa0c13e39840 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4549
diff changeset
   960
	od.tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
8609
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   961
	if (CheckRoadBlockedForOvertaking(&od)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   962
3431
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   963
	if (od.u->cur_speed == 0 || od.u->vehstatus& VS_STOPPED) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   964
		v->u.road.overtaking_ctr = 0x11;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
		v->u.road.overtaking = 0x10;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
	} else {
8609
cda5867c7efe (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 8563
diff changeset
   967
//		if (CheckRoadBlockedForOvertaking(&od)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
		v->u.road.overtaking_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
		v->u.road.overtaking = 0x10;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   970
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
static void RoadZPosAffectSpeed(Vehicle *v, byte old_z)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   974
{
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   975
	if (old_z == v->z_pos) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   976
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
	if (old_z < v->z_pos) {
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   978
		v->cur_speed = v->cur_speed * 232 / 256; // slow down by ~10%
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
		uint16 spd = v->cur_speed + 2;
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   981
		if (spd <= v->max_speed) v->cur_speed = spd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
static int PickRandomBit(uint bits)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   986
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
	uint i;
7832
60035eb28f08 (svn r11382) -Codechange: renamed COUNTBITS to CountBits, as it is no longer a macro (skidd13)
truelight
parents: 7817
diff changeset
   988
	uint num = RandomRange(CountBits(bits));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   990
	for (i = 0; !(bits & 1) || (int)--num >= 0; bits >>= 1, i++) {}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
	return i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   992
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   994
struct FindRoadToChooseData {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   995
	TileIndex dest;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   996
	uint maxtracklen;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   997
	uint mindist;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   998
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
8611
9037a4227d67 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 8609
diff changeset
  1000
static bool EnumRoadTrackFindDist(TileIndex tile, void* data, Trackdir trackdir, uint length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1002
	FindRoadToChooseData* frd = (FindRoadToChooseData*)data;
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1239
diff changeset
  1003
	uint dist = DistanceManhattan(tile, frd->dest);
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1004
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1005
	if (dist <= frd->mindist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1006
		if (dist != frd->mindist || length < frd->maxtracklen) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1007
			frd->maxtracklen = length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1008
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
		frd->mindist = dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1010
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1011
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1012
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8506
diff changeset
  1014
static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1015
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1016
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1017
	void* perf = NpfBeginInterval();
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8506
diff changeset
  1018
	NPFFoundTargetData ret = NPFRouteToStationOrTile(tile, trackdir, ignore_start_tile, target, type, sub_type, owner, railtypes);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1019
	int t = NpfEndInterval(perf);
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1020
	DEBUG(yapf, 4, "[NPFR] %d us - %d rounds - %d open - %d closed -- ", t, 0, _aystar_stats_open_size, _aystar_stats_closed_size);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1021
	return ret;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1022
}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1023
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1024
/**
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1025
 * Returns direction to for a road vehicle to take or
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1026
 * INVALID_TRACKDIR if the direction is currently blocked
6483
ae78fc3c6b20 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6453
diff changeset
  1027
 * @param v        the Vehicle to do the pathfinding for
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1028
 * @param tile     the where to start the pathfinding
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1029
 * @param enterdir the direction the vehicle enters the tile from
6483
ae78fc3c6b20 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6453
diff changeset
  1030
 * @return the Trackdir to take
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1031
 */
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1032
static Trackdir RoadFindPathToDest(Vehicle* v, TileIndex tile, DiagDirection enterdir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1033
{
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1034
#define return_track(x) { best_track = (Trackdir)x; goto found_best_track; }
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1035
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
  1036
	TileIndex desttile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1037
	FindRoadToChooseData frd;
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1038
	Trackdir best_track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1039
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8611
diff changeset
  1040
	TrackStatus ts = GetTileTrackStatus(tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes);
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8611
diff changeset
  1041
	TrackdirBits red_signals = TrackStatusToRedSignals(ts); // crossing
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8611
diff changeset
  1042
	TrackdirBits trackdirs = TrackStatusToTrackdirBits(ts);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1043
7370
41adc721b1fa (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7334
diff changeset
  1044
	if (IsTileType(tile, MP_ROAD)) {
8563
13b1a41e46f1 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 8556
diff changeset
  1045
		if (IsRoadDepot(tile) && (!IsTileOwner(tile, v->owner) || GetRoadDepotDirection(tile) == enterdir || (GetRoadTypes(tile) & v->u.road.compatible_roadtypes) == 0)) {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
  1046
			/* Road depot owned by another company or with the wrong orientation */
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1047
			trackdirs = TRACKDIR_BIT_NONE;
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2493
diff changeset
  1048
		}
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1049
	} else if (IsTileType(tile, MP_STATION) && IsStandardRoadStopTile(tile)) {
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1050
		/* Standard road stop (drive-through stops are treated as normal road) */
7469
2c1dd5ce245d (svn r10974) -Fix [FS#1144, FS#1155]: road vehicles that could not (properly) use a road stop still tried to go to that road stop.
rubidium
parents: 7450
diff changeset
  1051
2c1dd5ce245d (svn r10974) -Fix [FS#1144, FS#1155]: road vehicles that could not (properly) use a road stop still tried to go to that road stop.
rubidium
parents: 7450
diff changeset
  1052
		if (!IsTileOwner(tile, v->owner) || GetRoadStopDir(tile) == enterdir || RoadVehHasArticPart(v)) {
2c1dd5ce245d (svn r10974) -Fix [FS#1144, FS#1155]: road vehicles that could not (properly) use a road stop still tried to go to that road stop.
rubidium
parents: 7450
diff changeset
  1053
			/* different station owner or wrong orientation or the vehicle has articulated parts */
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1054
			trackdirs = TRACKDIR_BIT_NONE;
3937
5728e502f942 (svn r5080) -Fix: RVs can enter other player's depot or roadstop (introduced by YAPF related change of GetTileTrackStatus() - r4419)
KUDr
parents: 3933
diff changeset
  1055
		} else {
241
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1056
			/* Our station */
8785
871586967963 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8784
diff changeset
  1057
			RoadStopType rstype = IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? ROADSTOP_BUS : ROADSTOP_TRUCK;
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1058
3937
5728e502f942 (svn r5080) -Fix: RVs can enter other player's depot or roadstop (introduced by YAPF related change of GetTileTrackStatus() - r4419)
KUDr
parents: 3933
diff changeset
  1059
			if (GetRoadStopType(tile) != rstype) {
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1060
				/* Wrong station type */
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1061
				trackdirs = TRACKDIR_BIT_NONE;
3937
5728e502f942 (svn r5080) -Fix: RVs can enter other player's depot or roadstop (introduced by YAPF related change of GetTileTrackStatus() - r4419)
KUDr
parents: 3933
diff changeset
  1062
			} else {
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1063
				/* Proper station type, check if there is free loading bay */
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
  1064
				if (!_settings_game.pf.roadveh_queue && IsStandardRoadStopTile(tile) &&
5990
280fbef32d44 (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 5989
diff changeset
  1065
						!GetRoadStopByTile(tile, rstype)->HasFreeBay()) {
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1066
					/* Station is full and RV queuing is off */
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1067
					trackdirs = TRACKDIR_BIT_NONE;
3092
34cfd4aa9cc5 (svn r3681) - [Multistop] Check the RoadStop type before check its status. This fixes an assert introduced in r3663. Also fix the return type of GetRoadStopType().
peter1138
parents: 3074
diff changeset
  1068
				}
241
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1069
			}
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1070
		}
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1071
	}
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1072
	/* The above lookups should be moved to GetTileTrackStatus in the
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1073
	 * future, but that requires more changes to the pathfinder and other
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1074
	 * stuff, probably even more arguments to GTTS.
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1075
	 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1076
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1077
	/* Remove tracks unreachable from the enter dir */
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1078
	trackdirs &= _road_enter_dir_to_reachable_trackdirs[enterdir];
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1079
	if (trackdirs == TRACKDIR_BIT_NONE) {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1080
		/* No reachable tracks, so we'll reverse */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1081
		return_track(_road_reverse_table[enterdir]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1082
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1083
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1084
	if (v->u.road.reverse_ctr != 0) {
10174
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1085
		bool reverse = true;
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1086
		if (v->u.road.roadtype == ROADTYPE_TRAM) {
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1087
			/* Trams may only reverse on a tile if it contains at least the straight
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1088
			 * trackbits or when it is a valid turning tile (i.e. one roadbit) */
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1089
			RoadBits rb = GetAnyRoadBits(tile, ROADTYPE_TRAM);
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1090
			RoadBits straight = AxisToRoadBits(DiagDirToAxis(enterdir));
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1091
			reverse = ((rb & straight) == straight) ||
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1092
			          (rb == DiagDirToRoadBits(enterdir));
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1093
		}
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1094
		if (reverse) {
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1095
			v->u.road.reverse_ctr = 0;
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1096
			if (v->tile != tile) {
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1097
				return_track(_road_reverse_table[enterdir]);
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1098
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1099
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1100
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1101
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1102
	desttile = v->dest_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1103
	if (desttile == 0) {
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1104
		/* We've got no destination, pick a random track */
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1105
		return_track(PickRandomBit(trackdirs));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1106
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1107
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1108
	/* Only one track to choose between? */
7835
f24480642344 (svn r11385) -Fix r11383: bool operation can be hard to translate for some people ;) (tnx fjb for noticing this bug ;))
truelight
parents: 7833
diff changeset
  1109
	if (KillFirstBit(trackdirs) == TRACKDIR_BIT_NONE) {
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1110
		return_track(FindFirstBit2x64(trackdirs));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1111
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1112
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
  1113
	switch (_settings_game.pf.pathfinder_for_roadvehs) {
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1114
		case VPF_YAPF: { /* YAPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1115
			Trackdir trackdir = YapfChooseRoadTrack(v, tile, enterdir);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1116
			if (trackdir != INVALID_TRACKDIR) return_track(trackdir);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1117
			return_track(PickRandomBit(trackdirs));
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1118
		} break;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1119
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1120
		case VPF_NPF: { /* NPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1121
			NPFFindStationOrTileData fstd;
3167
8323c2ccd029 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1122
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1123
			NPFFillWithOrderData(&fstd, v);
9224
93528d9cb96d (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9111
diff changeset
  1124
			Trackdir trackdir = DiagDirToDiagTrackdir(enterdir);
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1125
			//debug("Finding path. Enterdir: %d, Trackdir: %d", enterdir, trackdir);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1126
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1127
			NPFFoundTargetData ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPES);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1128
			if (ftd.best_trackdir == INVALID_TRACKDIR) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1129
				/* We are already at our target. Just do something
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1130
				 * @todo: maybe display error?
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1131
				 * @todo: go straight ahead if possible? */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1132
				return_track(FindFirstBit2x64(trackdirs));
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1133
			} else {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1134
				/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1135
				 * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1136
				 * we did not find our target, but ftd.best_trackdir contains the direction leading
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1137
				 * to the tile closest to our target. */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1138
				return_track(ftd.best_trackdir);
3167
8323c2ccd029 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1139
			}
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1140
		} break;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1141
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1142
		default:
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1143
		case VPF_OPF: { /* OPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1144
			DiagDirection dir;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1145
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1146
			if (IsTileType(desttile, MP_ROAD)) {
8563
13b1a41e46f1 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 8556
diff changeset
  1147
				if (IsRoadDepot(desttile)) {
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1148
					dir = GetRoadDepotDirection(desttile);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1149
					goto do_it;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1150
				}
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1151
			} else if (IsTileType(desttile, MP_STATION)) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1152
				/* For drive-through stops we can head for the actual station tile */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1153
				if (IsStandardRoadStopTile(desttile)) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1154
					dir = GetRoadStopDir(desttile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1155
do_it:;
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1156
					/* When we are heading for a depot or station, we just
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1157
					 * pretend we are heading for the tile in front, we'll
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1158
					 * see from there */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1159
					desttile += TileOffsByDiagDir(dir);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1160
					if (desttile == tile && trackdirs & _road_exit_dir_to_incoming_trackdirs[dir]) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1161
						/* If we are already in front of the
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1162
						 * station/depot and we can get in from here,
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1163
						 * we enter */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1164
						return_track(FindFirstBit2x64(trackdirs & _road_exit_dir_to_incoming_trackdirs[dir]));
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1165
					}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1166
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1167
			}
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1168
			/* Do some pathfinding */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1169
			frd.dest = desttile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1170
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1171
			best_track = INVALID_TRACKDIR;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1172
			uint best_dist = UINT_MAX;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1173
			uint best_maxlen = UINT_MAX;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1174
			uint bitmask = (uint)trackdirs;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1175
			uint i;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1176
			FOR_EACH_SET_BIT(i, bitmask) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1177
				if (best_track == INVALID_TRACKDIR) best_track = (Trackdir)i; // in case we don't find the path, just pick a track
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1178
				frd.maxtracklen = UINT_MAX;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1179
				frd.mindist = UINT_MAX;
8800
c30102fee110 (svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents: 8793
diff changeset
  1180
				FollowTrack(tile, PATHFIND_FLAGS_NONE, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, _road_pf_directions[i], EnumRoadTrackFindDist, NULL, &frd);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1182
				if (frd.mindist < best_dist || (frd.mindist == best_dist && frd.maxtracklen < best_maxlen)) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1183
					best_dist = frd.mindist;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1184
					best_maxlen = frd.maxtracklen;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1185
					best_track = (Trackdir)i;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1186
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1187
			}
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1188
		} break;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1189
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1190
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1191
found_best_track:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1192
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8611
diff changeset
  1193
	if (HasBit(red_signals, best_track)) return INVALID_TRACKDIR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1194
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1195
	return best_track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1196
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1197
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1198
static uint RoadFindPathToStop(const Vehicle *v, TileIndex tile)
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1199
{
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
  1200
	if (_settings_game.pf.pathfinder_for_roadvehs == VPF_YAPF) {
6393
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
  1201
		/* use YAPF */
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1202
		return YapfRoadVehDistanceToTile(v, tile);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1203
	}
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1204
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1205
	/* use NPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1206
	Trackdir trackdir = GetVehicleTrackdir(v);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1207
	assert(trackdir != INVALID_TRACKDIR);
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1208
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1209
	NPFFindStationOrTileData fstd;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1210
	fstd.dest_coords = tile;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1211
	fstd.station_index = INVALID_STATION; // indicates that the destination is a tile, not a station
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1212
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1213
	uint dist = NPFRouteToStationOrTile(v->tile, trackdir, false, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPES).best_path_dist;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1214
	/* change units from NPF_TILE_LENGTH to # of tiles */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1215
	if (dist != UINT_MAX) dist = (dist + NPF_TILE_LENGTH - 1) / NPF_TILE_LENGTH;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
  1216
3915
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1217
	return dist;
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1218
}
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1219
6003
fca15ed49286 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6002
diff changeset
  1220
enum {
fca15ed49286 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6002
diff changeset
  1221
	RDE_NEXT_TILE = 0x80,
fca15ed49286 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6002
diff changeset
  1222
	RDE_TURNED    = 0x40,
6009
bbe756070082 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6005
diff changeset
  1223
bbe756070082 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6005
diff changeset
  1224
	/* Start frames for when a vehicle enters a tile/changes its state.
bbe756070082 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6005
diff changeset
  1225
	 * The start frame is different for vehicles that turned around or
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1226
	 * are leaving the depot as the do not start at the edge of the tile.
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1227
	 * For trams there are a few different start frames as there are two
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1228
	 * places where trams can turn. */
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1229
	RVC_DEFAULT_START_FRAME                =  0,
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1230
	RVC_TURN_AROUND_START_FRAME            =  1,
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1231
	RVC_DEPOT_START_FRAME                  =  6,
10174
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1232
	RVC_START_FRAME_AFTER_LONG_TRAM        = 21,
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1233
	RVC_TURN_AROUND_START_FRAME_SHORT_TRAM = 16,
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1234
	/* Stop frame for a vehicle in a drive-through stop */
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1235
	RVC_DRIVE_THROUGH_STOP_FRAME           =  7
6003
fca15ed49286 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6002
diff changeset
  1236
};
fca15ed49286 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6002
diff changeset
  1237
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  1238
struct RoadDriveEntry {
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  1239
	byte x, y;
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
  1240
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1241
8506
0e1e0f7c4989 (svn r12081) -Codechange: Rename table/roadveh.h to table/roadveh_movment.h
belugas
parents: 8491
diff changeset
  1242
#include "table/roadveh_movement.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1243
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1244
static const byte _road_veh_data_1[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1245
	20, 20, 16, 16, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1246
	19, 19, 15, 15, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1247
	16, 16, 12, 12, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1248
	15, 15, 11, 11
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1249
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1250
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1251
static bool RoadVehLeaveDepot(Vehicle *v, bool first)
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1252
{
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1253
	/* Don't leave if not all the wagons are in the depot. */
7492
35acee076719 (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
parents: 7490
diff changeset
  1254
	for (const Vehicle *u = v; u != NULL; u = u->Next()) {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1255
		if (u->u.road.state != RVSB_IN_DEPOT || u->tile != v->tile) return false;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1256
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1257
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1258
	DiagDirection dir = GetRoadDepotDirection(v->tile);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1259
	v->direction = DiagDirToDir(dir);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1260
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1261
	Trackdir tdir = _roadveh_depot_exit_trackdir[dir];
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
  1262
	const RoadDriveEntry *rdp = _road_drive_data[v->u.road.roadtype][(_settings_game.vehicle.road_side << RVS_DRIVE_SIDE) + tdir];
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1263
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1264
	int x = TileX(v->tile) * TILE_SIZE + (rdp[RVC_DEPOT_START_FRAME].x & 0xF);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1265
	int y = TileY(v->tile) * TILE_SIZE + (rdp[RVC_DEPOT_START_FRAME].y & 0xF);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1266
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1267
	if (first) {
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1268
		if (RoadVehFindCloseTo(v, x, y, v->direction) != NULL) return true;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1269
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1270
		VehicleServiceInDepot(v);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1271
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1272
		StartRoadVehSound(v);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1273
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1274
		/* Vehicle is about to leave a depot */
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1275
		v->cur_speed = 0;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1276
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1277
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1278
	BeginVehicleMove(v);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1279
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1280
	v->vehstatus &= ~VS_HIDDEN;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1281
	v->u.road.state = tdir;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1282
	v->u.road.frame = RVC_DEPOT_START_FRAME;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1283
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7096
diff changeset
  1284
	v->cur_image = v->GetImage(v->direction);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1285
	v->UpdateDeltaXY(v->direction);
8969
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8962
diff changeset
  1286
	SetRoadVehPosition(v, x, y);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1287
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1288
	InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1289
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1290
	return true;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1291
}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1292
7416
cced8adef086 (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7398
diff changeset
  1293
static Trackdir FollowPreviousRoadVehicle(const Vehicle *v, const Vehicle *prev, TileIndex tile, DiagDirection entry_dir, bool already_reversed)
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1294
{
7416
cced8adef086 (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7398
diff changeset
  1295
	if (prev->tile == v->tile && !already_reversed) {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1296
		/* If the previous vehicle is on the same tile as this vehicle is
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1297
		 * then it must have reversed. */
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1298
		return _road_reverse_table[entry_dir];
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1299
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1300
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1301
	byte prev_state = prev->u.road.state;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1302
	Trackdir dir;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1303
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1304
	if (prev_state == RVSB_WORMHOLE || prev_state == RVSB_IN_DEPOT) {
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1305
		DiagDirection diag_dir = INVALID_DIAGDIR;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1306
8088
92fca5b09665 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8083
diff changeset
  1307
		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
8083
ad22eade501f (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 8081
diff changeset
  1308
			diag_dir = GetTunnelBridgeDirection(tile);
8563
13b1a41e46f1 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 8556
diff changeset
  1309
		} else if (IsRoadDepotTile(tile)) {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1310
			diag_dir = ReverseDiagDir(GetRoadDepotDirection(tile));
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1311
		}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1312
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1313
		if (diag_dir == INVALID_DIAGDIR) return INVALID_TRACKDIR;
9224
93528d9cb96d (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9111
diff changeset
  1314
		dir = DiagDirToDiagTrackdir(diag_dir);
8261
43c552519240 (svn r11825) -Fix (FS#1627): an articulated road vehicle could split up when it turned around at a corner and then would enter a drive through station at the next tile.
rubidium
parents: 8258
diff changeset
  1315
	} else {
7869
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1316
		if (already_reversed && prev->tile != tile) {
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1317
			/*
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1318
			 * The vehicle has reversed, but did not go straight back.
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1319
			 * It immediatelly turn onto another tile. This means that
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1320
			 * the roadstate of the previous vehicle cannot be used
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1321
			 * as the direction we have to go with this vehicle.
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1322
			 *
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1323
			 * Next table is build in the following way:
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1324
			 *  - first row for when the vehicle in front went to the northern or
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1325
			 *    western tile, second for southern and eastern.
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1326
			 *  - columns represent the entry direction.
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1327
			 *  - cell values are determined by the Trackdir one has to take from
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1328
			 *    the entry dir (column) to the tile in north or south by only
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1329
			 *    going over the trackdirs used for turning 90 degrees, i.e.
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1330
			 *    TRACKDIR_{UPPER,RIGHT,LOWER,LEFT}_{N,E,S,W}.
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1331
			 */
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1332
			Trackdir reversed_turn_lookup[2][DIAGDIR_END] = {
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1333
				{ TRACKDIR_UPPER_W, TRACKDIR_RIGHT_N, TRACKDIR_LEFT_N,  TRACKDIR_UPPER_E },
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1334
				{ TRACKDIR_RIGHT_S, TRACKDIR_LOWER_W, TRACKDIR_LOWER_E, TRACKDIR_LEFT_S  }};
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1335
			dir = reversed_turn_lookup[prev->tile < tile ? 0 : 1][ReverseDiagDir(entry_dir)];
8261
43c552519240 (svn r11825) -Fix (FS#1627): an articulated road vehicle could split up when it turned around at a corner and then would enter a drive through station at the next tile.
rubidium
parents: 8258
diff changeset
  1336
		} else if (HasBit(prev_state, RVS_IN_DT_ROAD_STOP)) {
43c552519240 (svn r11825) -Fix (FS#1627): an articulated road vehicle could split up when it turned around at a corner and then would enter a drive through station at the next tile.
rubidium
parents: 8258
diff changeset
  1337
			dir = (Trackdir)(prev_state & RVSB_ROAD_STOP_TRACKDIR_MASK);
43c552519240 (svn r11825) -Fix (FS#1627): an articulated road vehicle could split up when it turned around at a corner and then would enter a drive through station at the next tile.
rubidium
parents: 8258
diff changeset
  1338
		} else if (prev_state < TRACKDIR_END) {
7869
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1339
			dir = (Trackdir)prev_state;
8261
43c552519240 (svn r11825) -Fix (FS#1627): an articulated road vehicle could split up when it turned around at a corner and then would enter a drive through station at the next tile.
rubidium
parents: 8258
diff changeset
  1340
		} else {
43c552519240 (svn r11825) -Fix (FS#1627): an articulated road vehicle could split up when it turned around at a corner and then would enter a drive through station at the next tile.
rubidium
parents: 8258
diff changeset
  1341
			return INVALID_TRACKDIR;
7869
9552440af591 (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 7835
diff changeset
  1342
		}
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1343
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1344
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1345
	/* Do some sanity checking. */
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1346
	static const RoadBits required_roadbits[] = {
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1347
		ROAD_X,            ROAD_Y,            ROAD_NW | ROAD_NE, ROAD_SW | ROAD_SE,
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1348
		ROAD_NW | ROAD_SW, ROAD_NE | ROAD_SE, ROAD_X,            ROAD_Y
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1349
	};
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1350
	RoadBits required = required_roadbits[dir & 0x07];
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1351
8716
360342cfd3fe (svn r12390) -Fix [FS#1851]: trams failing to turn on bridge heads/tunnel entrances.
rubidium
parents: 8626
diff changeset
  1352
	if ((required & GetAnyRoadBits(tile, v->u.road.roadtype, true)) == ROAD_NONE) {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1353
		dir = INVALID_TRACKDIR;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1354
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1355
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1356
	return dir;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1357
}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1358
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1359
/**
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1360
 * Can a tram track build without destruction on the given tile?
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
  1361
 * @param c the company that would be building the tram tracks
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1362
 * @param t the tile to build on.
8060
c839513049bd (svn r11621) -Fix [FS#1525]: there were still some cases where one could not build a tram track, but the tram could become blocked.
rubidium
parents: 8011
diff changeset
  1363
 * @param r the road bits needed.
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1364
 * @return true when a track track can be build on 't'
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1365
 */
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
  1366
static bool CanBuildTramTrackOnTile(CompanyID c, TileIndex t, RoadBits r)
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1367
{
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
  1368
	/* The 'current' company is not necessarily the owner of the vehicle. */
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
  1369
	CompanyID original_company = _current_company;
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
  1370
	_current_company = c;
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1371
8060
c839513049bd (svn r11621) -Fix [FS#1525]: there were still some cases where one could not build a tram track, but the tram could become blocked.
rubidium
parents: 8011
diff changeset
  1372
	CommandCost ret = DoCommand(t, ROADTYPE_TRAM << 4 | r, 0, 0, CMD_BUILD_ROAD);
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1373
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
  1374
	_current_company = original_company;
8060
c839513049bd (svn r11621) -Fix [FS#1525]: there were still some cases where one could not build a tram track, but the tram could become blocked.
rubidium
parents: 8011
diff changeset
  1375
	return CmdSucceeded(ret);
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1376
}
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1377
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1378
static bool IndividualRoadVehicleController(Vehicle *v, const Vehicle *prev)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1379
{
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1380
	Direction new_dir;
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1381
	Direction old_dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1382
	RoadDriveEntry rd;
8969
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8962
diff changeset
  1383
	int x, y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1384
	uint32 r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1385
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1386
	if (v->u.road.overtaking != 0)  {
8011
b9f348e95d25 (svn r11571) -Fix [FS#1493]: road vehicle getting to the wrong side of a station when trying to overtake in there.
rubidium
parents: 8005
diff changeset
  1387
		if (IsTileType(v->tile, MP_STATION)) {
b9f348e95d25 (svn r11571) -Fix [FS#1493]: road vehicle getting to the wrong side of a station when trying to overtake in there.
rubidium
parents: 8005
diff changeset
  1388
			/* Force us to be not overtaking! */
b9f348e95d25 (svn r11571) -Fix [FS#1493]: road vehicle getting to the wrong side of a station when trying to overtake in there.
rubidium
parents: 8005
diff changeset
  1389
			v->u.road.overtaking = 0;
b9f348e95d25 (svn r11571) -Fix [FS#1493]: road vehicle getting to the wrong side of a station when trying to overtake in there.
rubidium
parents: 8005
diff changeset
  1390
		} else if (++v->u.road.overtaking_ctr >= 35) {
2823
d4be8420b41c (svn r3371) -Fix: don't allow road-vehicles to perform an out-of-bound operation while overtaking in a curve (tnx to guru3, Tron and peter1138)
truelight
parents: 2819
diff changeset
  1391
			/* If overtaking just aborts at a random moment, we can have a out-of-bound problem,
d4be8420b41c (svn r3371) -Fix: don't allow road-vehicles to perform an out-of-bound operation while overtaking in a curve (tnx to guru3, Tron and peter1138)
truelight
parents: 2819
diff changeset
  1392
			 *  if the vehicle started a corner. To protect that, only allow an abort of
6000
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
  1393
			 *  overtake if we are on straight roads */
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
  1394
			if (v->u.road.state < RVSB_IN_ROAD_STOP && IsStraightRoadTrackdir((Trackdir)v->u.road.state)) {
2823
d4be8420b41c (svn r3371) -Fix: don't allow road-vehicles to perform an out-of-bound operation while overtaking in a curve (tnx to guru3, Tron and peter1138)
truelight
parents: 2819
diff changeset
  1395
				v->u.road.overtaking = 0;
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1396
			}
8011
b9f348e95d25 (svn r11571) -Fix [FS#1493]: road vehicle getting to the wrong side of a station when trying to overtake in there.
rubidium
parents: 8005
diff changeset
  1397
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1398
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1399
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1400
	/* If this vehicle is in a depot and we've reached this point it must be
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1401
	 * one of the articulated parts. It will stay in the depot until activated
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1402
	 * by the previous vehicle in the chain when it gets to the right place. */
7490
bb27d92565d3 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7476
diff changeset
  1403
	if (v->IsInDepot()) return true;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1404
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1405
	/* Save old vehicle position to use at end of move to set viewport area dirty */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1406
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1407
6000
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
  1408
	if (v->u.road.state == RVSB_WORMHOLE) {
6009
bbe756070082 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6005
diff changeset
  1409
		/* Vehicle is entering a depot or is on a bridge or in a tunnel */
6153
eb35b73b300a (svn r8897) -Fix
tron
parents: 6141
diff changeset
  1410
		GetNewVehiclePosResult gp = GetNewVehiclePos(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1411
8266
08d1ff0b6018 (svn r11830) -Fix [FS#1625]: road vehicles would not wait in line, but at a single point on bridges. Based on a patch by SmatZ.
rubidium
parents: 8264
diff changeset
  1412
		if (IsRoadVehFront(v)) {
08d1ff0b6018 (svn r11830) -Fix [FS#1625]: road vehicles would not wait in line, but at a single point on bridges. Based on a patch by SmatZ.
rubidium
parents: 8264
diff changeset
  1413
			const Vehicle *u = RoadVehFindCloseTo(v, gp.x, gp.y, v->direction);
08d1ff0b6018 (svn r11830) -Fix [FS#1625]: road vehicles would not wait in line, but at a single point on bridges. Based on a patch by SmatZ.
rubidium
parents: 8264
diff changeset
  1414
			if (u != NULL) {
08d1ff0b6018 (svn r11830) -Fix [FS#1625]: road vehicles would not wait in line, but at a single point on bridges. Based on a patch by SmatZ.
rubidium
parents: 8264
diff changeset
  1415
				v->cur_speed = u->First()->cur_speed;
08d1ff0b6018 (svn r11830) -Fix [FS#1625]: road vehicles would not wait in line, but at a single point on bridges. Based on a patch by SmatZ.
rubidium
parents: 8264
diff changeset
  1416
				return false;
08d1ff0b6018 (svn r11830) -Fix [FS#1625]: road vehicles would not wait in line, but at a single point on bridges. Based on a patch by SmatZ.
rubidium
parents: 8264
diff changeset
  1417
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1418
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1419
8390
f88f515e6557 (svn r11960) -Cleanup: simplify some IsTunnel(Tile) / IsBridge(Tile) conditions
smatz
parents: 8350
diff changeset
  1420
		if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1421
			/* Vehicle has just entered a bridge or tunnel */
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7096
diff changeset
  1422
			v->cur_image = v->GetImage(v->direction);
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
  1423
			v->UpdateDeltaXY(v->direction);
8969
6d1c74e0e2cd (svn r12761) -Codechange: lots of minor whitespace coding style fixes around operators.
rubidium
parents: 8962
diff changeset
  1424
			SetRoadVehPosition(v, gp.x, gp.y);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1425
			return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1426
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1427
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1428
		v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1429
		v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1430
		VehiclePositionChanged(v);
5385
3868f2e6db9b (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5380
diff changeset
  1431
		if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1432
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1433
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1434
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1435
	/* Get move position data for next frame.
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1436
	 * For a drive-through road stop use 'straight road' move data.
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1437
	 * In this case v->u.road.state is masked to give the road stop entry direction. */
6691
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1438
	rd = _road_drive_data[v->u.road.roadtype][(
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
  1439
		(HasBit(v->u.road.state, RVS_IN_DT_ROAD_STOP) ? v->u.road.state & RVSB_ROAD_STOP_TRACKDIR_MASK : v->u.road.state) +
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
  1440
		(_settings_game.vehicle.road_side << RVS_DRIVE_SIDE)) ^ v->u.road.overtaking][v->u.road.frame + 1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1441
6003
fca15ed49286 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6002
diff changeset
  1442
	if (rd.x & RDE_NEXT_TILE) {
7317
e61af9d8a2b3 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7266
diff changeset
  1443
		TileIndex tile = v->tile + TileOffsByDiagDir((DiagDirection)(rd.x & 3));
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1444
		Trackdir dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1445
		uint32 r;
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1446
		Direction newdir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1447
		const RoadDriveEntry *rdp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1448
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1449
		if (IsRoadVehFront(v)) {
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1450
			/* If this is the front engine, look for the right path. */
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1451
			dir = RoadFindPathToDest(v, tile, (DiagDirection)(rd.x & 3));
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1452
		} else {
7416
cced8adef086 (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7398
diff changeset
  1453
			dir = FollowPreviousRoadVehicle(v, prev, tile, (DiagDirection)(rd.x & 3), false);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1454
		}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1455
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1456
		if (dir == INVALID_TRACKDIR) {
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9413
diff changeset
  1457
			if (!IsRoadVehFront(v)) error("Disconnecting road vehicle.");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1458
			v->cur_speed = 0;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1459
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1460
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1461
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1462
again:
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1463
		uint start_frame = RVC_DEFAULT_START_FRAME;
6000
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
  1464
		if (IsReversingRoadTrackdir(dir)) {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1465
			/* Turning around */
6691
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1466
			if (v->u.road.roadtype == ROADTYPE_TRAM) {
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1467
				/* Determine the road bits the tram needs to be able to turn around
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1468
				 * using the 'big' corner loop. */
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1469
				RoadBits needed;
6691
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1470
				switch (dir) {
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1471
					default: NOT_REACHED();
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1472
					case TRACKDIR_RVREV_NE: needed = ROAD_SW; break;
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1473
					case TRACKDIR_RVREV_SE: needed = ROAD_NW; break;
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1474
					case TRACKDIR_RVREV_SW: needed = ROAD_NE; break;
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1475
					case TRACKDIR_RVREV_NW: needed = ROAD_SE; break;
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1476
				}
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1477
				if ((v->Previous() != NULL && v->Previous()->tile == tile) ||
8563
13b1a41e46f1 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 8556
diff changeset
  1478
						(IsRoadVehFront(v) && IsNormalRoadTile(tile) && !HasRoadWorks(tile) &&
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1479
							(needed & GetRoadBits(tile, ROADTYPE_TRAM)) != ROAD_NONE)) {
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1480
					/*
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1481
					 * Taking the 'big' corner for trams only happens when:
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1482
					 * - The previous vehicle in this (articulated) tram chain is
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1483
					 *   already on the 'next' tile, we just follow them regardless of
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1484
					 *   anything. When it is NOT on the 'next' tile, the tram started
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1485
					 *   doing a reversing turn when the piece of tram track on the next
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1486
					 *   tile did not exist yet. Do not use the big tram loop as that is
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1487
					 *   going to cause the tram to split up.
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1488
					 * - Or the front of the tram can drive over the next tile.
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1489
					 */
10230
74440463e61e (svn r14458) -Fix [FS#2341](r14368): crash when there was a tram dead end after a station/tunnel/bridge (frosch123 and Rubidium)
smatz
parents: 10208
diff changeset
  1490
				} else if (!IsRoadVehFront(v) || !CanBuildTramTrackOnTile(v->owner, tile, needed) || ((~needed & GetAnyRoadBits(v->tile, ROADTYPE_TRAM, false)) == ROAD_NONE)) {
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1491
					/*
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1492
					 * Taking the 'small' corner for trams only happens when:
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1493
					 * - We are not the from vehicle of an articulated tram.
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
  1494
					 * - Or when the company cannot build on the next tile.
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1495
					 *
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1496
					 * The 'small' corner means that the vehicle is on the end of a
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1497
					 * tram track and needs to start turning there. To do this properly
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1498
					 * the tram needs to start at an offset in the tram turning 'code'
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1499
					 * for 'big' corners. It furthermore does not go to the next tile,
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1500
					 * so that needs to be fixed too.
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1501
					 */
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1502
					tile = v->tile;
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1503
					start_frame = RVC_TURN_AROUND_START_FRAME_SHORT_TRAM;
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1504
				} else {
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
  1505
					/* The company can build on the next tile, so wait till (s)he does. */
6691
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1506
					v->cur_speed = 0;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1507
					return false;
6691
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1508
				}
8563
13b1a41e46f1 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 8556
diff changeset
  1509
			} else if (IsNormalRoadTile(v->tile) && GetDisallowedRoadDirections(v->tile) != DRD_NONE) {
6764
d09d8d618a07 (svn r9999) -Feature: make it possible to disallow busses and lorries to go a specific way on straight pieces of road.
rubidium
parents: 6754
diff changeset
  1510
				v->cur_speed = 0;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1511
				return false;
6691
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1512
			} else {
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1513
				tile = v->tile;
f9adbb0be102 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 6687
diff changeset
  1514
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1515
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1516
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1517
		/* Get position data for first frame on the new tile */
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
  1518
		rdp = _road_drive_data[v->u.road.roadtype][(dir + (_settings_game.vehicle.road_side << RVS_DRIVE_SIDE)) ^ v->u.road.overtaking];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1519
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1520
		x = TileX(tile) * TILE_SIZE + rdp[start_frame].x;
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1521
		y = TileY(tile) * TILE_SIZE + rdp[start_frame].y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1522
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1523
		newdir = RoadVehGetSlidingDirection(v, x, y);
7924
7ef93d919eba (svn r11477) -Fix [FS#1451]: a road vehicle must not show that it is driving max speed when it is standing still waiting for the vehicle in from of it.
rubidium
parents: 7923
diff changeset
  1524
		if (IsRoadVehFront(v)) {
7ef93d919eba (svn r11477) -Fix [FS#1451]: a road vehicle must not show that it is driving max speed when it is standing still waiting for the vehicle in from of it.
rubidium
parents: 7923
diff changeset
  1525
			Vehicle *u = RoadVehFindCloseTo(v, x, y, newdir);
7ef93d919eba (svn r11477) -Fix [FS#1451]: a road vehicle must not show that it is driving max speed when it is standing still waiting for the vehicle in from of it.
rubidium
parents: 7923
diff changeset
  1526
			if (u != NULL) {
7ef93d919eba (svn r11477) -Fix [FS#1451]: a road vehicle must not show that it is driving max speed when it is standing still waiting for the vehicle in from of it.
rubidium
parents: 7923
diff changeset
  1527
				v->cur_speed = u->First()->cur_speed;
7ef93d919eba (svn r11477) -Fix [FS#1451]: a road vehicle must not show that it is driving max speed when it is standing still waiting for the vehicle in from of it.
rubidium
parents: 7923
diff changeset
  1528
				return false;
7ef93d919eba (svn r11477) -Fix [FS#1451]: a road vehicle must not show that it is driving max speed when it is standing still waiting for the vehicle in from of it.
rubidium
parents: 7923
diff changeset
  1529
			}
7ef93d919eba (svn r11477) -Fix [FS#1451]: a road vehicle must not show that it is driving max speed when it is standing still waiting for the vehicle in from of it.
rubidium
parents: 7923
diff changeset
  1530
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1531
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1532
		r = VehicleEnterTile(v, tile, x, y);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
  1533
		if (HasBit(r, VETS_CANNOT_ENTER)) {
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  1534
			if (!IsTileType(tile, MP_TUNNELBRIDGE)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1535
				v->cur_speed = 0;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1536
				return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1537
			}
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1538
			/* Try an about turn to re-enter the previous tile */
6000
181ad2c1ab14 (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 5999
diff changeset
  1539
			dir = _road_reverse_table[rd.x & 3];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1540
			goto again;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1541
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1542
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7932
diff changeset
  1543
		if (IsInsideMM(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) && IsTileType(v->tile, MP_STATION)) {
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7932
diff changeset
  1544
			if (IsReversingRoadTrackdir(dir) && IsInsideMM(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END)) {
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1545
				/* New direction is trying to turn vehicle around.
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1546
				 * We can't turn at the exit of a road stop so wait.*/
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1547
				v->cur_speed = 0;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1548
				return false;
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1549
			}
3338
7cff208f0446 (svn r4120) Use the new station functions where appropriate
tron
parents: 3315
diff changeset
  1550
			if (IsRoadStop(v->tile)) {
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1551
				RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1552
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1553
				/* Vehicle is leaving a road stop tile, mark bay as free
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1554
				 * For drive-through stops, only do it if the vehicle stopped here */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
  1555
				if (IsStandardRoadStopTile(v->tile) || HasBit(v->u.road.state, RVS_IS_STOPPING)) {
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
  1556
					rs->FreeBay(HasBit(v->u.road.state, RVS_USING_SECOND_BAY));
7929
6c9b25842b0f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  1557
					ClrBit(v->u.road.state, RVS_IS_STOPPING);
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1558
				}
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1559
				if (IsStandardRoadStopTile(v->tile)) rs->SetEntranceBusy(false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1560
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1561
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1562
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
  1563
		if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1564
			v->tile = tile;
2823
d4be8420b41c (svn r3371) -Fix: don't allow road-vehicles to perform an out-of-bound operation while overtaking in a curve (tnx to guru3, Tron and peter1138)
truelight
parents: 2819
diff changeset
  1565
			v->u.road.state = (byte)dir;
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1566
			v->u.road.frame = start_frame;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1567
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1568
		if (newdir != v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1569
			v->direction = newdir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1570
			v->cur_speed -= v->cur_speed >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1571
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1572
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7096
diff changeset
  1573
		v->cur_image = v->GetImage(newdir);
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
  1574
		v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1575
		RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1576
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1577
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1578
6003
fca15ed49286 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6002
diff changeset
  1579
	if (rd.x & RDE_TURNED) {
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1580
		/* Vehicle has finished turning around, it will now head back onto the same tile */
7416
cced8adef086 (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7398
diff changeset
  1581
		Trackdir dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1582
		uint32 r;
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1583
		Direction newdir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1584
		const RoadDriveEntry *rdp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1585
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1586
		uint turn_around_start_frame = RVC_TURN_AROUND_START_FRAME;
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1587
7909
19cec50eb148 (svn r11461) -Fix [FS#1449] (r11449): a bus turning around in a station could case an assertion error. Patch by divide.
rubidium
parents: 7898
diff changeset
  1588
		RoadBits tram;
8716
360342cfd3fe (svn r12390) -Fix [FS#1851]: trams failing to turn on bridge heads/tunnel entrances.
rubidium
parents: 8626
diff changeset
  1589
		if (v->u.road.roadtype == ROADTYPE_TRAM && CountBits(tram = GetAnyRoadBits(v->tile, ROADTYPE_TRAM, true)) == 1) {
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1590
			/*
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1591
			 * The tram is turning around with one tram 'roadbit'. This means that
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1592
			 * it is using the 'big' corner 'drive data'. However, to support the
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1593
			 * trams to take a small corner, there is a 'turned' marker in the middle
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1594
			 * of the turning 'drive data'. When the tram took the long corner, we
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1595
			 * will still use the 'big' corner drive data, but we advance it one
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1596
			 * frame. We furthermore set the driving direction so the turning is
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1597
			 * going to be properly shown.
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1598
			 */
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1599
			turn_around_start_frame = RVC_START_FRAME_AFTER_LONG_TRAM;
10174
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1600
			switch (rd.x & 0x3) {
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1601
				default: NOT_REACHED();
10174
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1602
				case DIAGDIR_NW: dir = TRACKDIR_RVREV_SE; break;
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1603
				case DIAGDIR_NE: dir = TRACKDIR_RVREV_SW; break;
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1604
				case DIAGDIR_SE: dir = TRACKDIR_RVREV_NW; break;
b3116362cc09 (svn r14368) -Fix [FS#1852]: trams jumping when reversing on a single trambit (like caused during road construction reworks) or when (manually) reversing in a corner.
rubidium
parents: 10123
diff changeset
  1605
				case DIAGDIR_SW: dir = TRACKDIR_RVREV_NE; break;
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1606
			}
7416
cced8adef086 (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7398
diff changeset
  1607
		} else {
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1608
			if (IsRoadVehFront(v)) {
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1609
				/* If this is the front engine, look for the right path. */
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1610
				dir = RoadFindPathToDest(v, v->tile, (DiagDirection)(rd.x & 3));
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1611
			} else {
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1612
				dir = FollowPreviousRoadVehicle(v, prev, v->tile, (DiagDirection)(rd.x & 3), true);
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1613
			}
7416
cced8adef086 (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7398
diff changeset
  1614
		}
cced8adef086 (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7398
diff changeset
  1615
5999
d300c792fc66 (svn r8712) -Codechange/cleanup: replace 'magic' constants with enums, use proper types instead of byte, uint etc., give variables more descriptive names and add some comments.
rubidium
parents: 5991
diff changeset
  1616
		if (dir == INVALID_TRACKDIR) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1617
			v->cur_speed = 0;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1618
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1619
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1620
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
  1621
		rdp = _road_drive_data[v->u.road.roadtype][(_settings_game.vehicle.road_side << RVS_DRIVE_SIDE) + dir];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1622
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1623
		x = TileX(v->tile) * TILE_SIZE + rdp[turn_around_start_frame].x;
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1624
		y = TileY(v->tile) * TILE_SIZE + rdp[turn_around_start_frame].y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1625
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1626
		newdir = RoadVehGetSlidingDirection(v, x, y);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1627
		if (IsRoadVehFront(v) && RoadVehFindCloseTo(v, x, y, newdir) != NULL) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1628
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1629
		r = VehicleEnterTile(v, v->tile, x, y);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
  1630
		if (HasBit(r, VETS_CANNOT_ENTER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1631
			v->cur_speed = 0;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1632
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1633
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1634
6009
bbe756070082 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6005
diff changeset
  1635
		v->u.road.state = dir;
7898
7c6a9c9030b5 (svn r11449) -Fix [FS#1160]: trams could deadlock themselves. As of now trams will turn as roadvehicles do when the player cannot build a tram track piece on the next tile without destroying anything. It will not turn when the player can build the before mentioned track piece on the 'next' tile.
rubidium
parents: 7869
diff changeset
  1636
		v->u.road.frame = turn_around_start_frame;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1637
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1638
		if (newdir != v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1639
			v->direction = newdir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1640
			v->cur_speed -= v->cur_speed >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1641
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1642
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7096
diff changeset
  1643
		v->cur_image = v->GetImage(newdir);
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
  1644
		v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1645
		RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1646
		return true;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1647
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1648
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1649
	/* This vehicle is not in a wormhole and it hasn't entered a new tile. If
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1650
	 * it's on a depot tile, check if it's time to activate the next vehicle in
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1651
	 * the chain yet. */
8563
13b1a41e46f1 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 8556
diff changeset
  1652
	if (v->Next() != NULL && IsRoadDepotTile(v->tile)) {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1653
		if (v->u.road.frame == v->u.road.cached_veh_length + RVC_DEPOT_START_FRAME) {
7492
35acee076719 (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
parents: 7490
diff changeset
  1654
			RoadVehLeaveDepot(v->Next(), false);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1655
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1656
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1657
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1658
	/* Calculate new position for the vehicle */
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1659
	x = (v->x_pos & ~15) + (rd.x & 15);
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1660
	y = (v->y_pos & ~15) + (rd.y & 15);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1661
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1662
	new_dir = RoadVehGetSlidingDirection(v, x, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1663
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7932
diff changeset
  1664
	if (IsRoadVehFront(v) && !IsInsideMM(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END)) {
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1665
		/* Vehicle is not in a road stop.
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1666
		 * Check for another vehicle to overtake */
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1667
		Vehicle* u = RoadVehFindCloseTo(v, x, y, new_dir);
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1668
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1669
		if (u != NULL) {
7817
f5fd95f8bb9f (svn r11367) -Fix [FS#1258]: non-articulated road vehicles should be able to overtake (not-too-long) articulated vehicles, i.e. overtaking of articulated vehicles that are several (3+) tiles long is not supported.
rubidium
parents: 7816
diff changeset
  1670
			u = u->First();
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1671
			/* There is a vehicle in front overtake it if possible */
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1672
			if (v->u.road.overtaking == 0) RoadVehCheckOvertake(v, u);
7669
37d063010a57 (svn r11200) -Fix [FS#1291]: road vehicles could not overtake on one way roads going to the east.
rubidium
parents: 7642
diff changeset
  1673
			if (v->u.road.overtaking == 0) v->cur_speed = u->cur_speed;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1674
			return false;
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1675
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1676
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1677
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1678
	old_dir = v->direction;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1679
	if (new_dir != old_dir) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1680
		v->direction = new_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1681
		v->cur_speed -= (v->cur_speed >> 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1682
		if (old_dir != v->u.road.state) {
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1683
			/* The vehicle is in a road stop */
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7096
diff changeset
  1684
			v->cur_image = v->GetImage(new_dir);
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
  1685
			v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1686
			SetRoadVehPosition(v, v->x_pos, v->y_pos);
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1687
			/* Note, return here means that the frame counter is not incremented
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1688
			 * for vehicles changing direction in a road stop. This causes frames to
6009
bbe756070082 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6005
diff changeset
  1689
			 * be repeated. (XXX) Is this intended? */
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1690
			return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1691
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1692
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1693
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1694
	/* If the vehicle is in a normal road stop and the frame equals the stop frame OR
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1695
	 * if the vehicle is in a drive-through road stop and this is the destination station
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1696
	 * and it's the correct type of stop (bus or truck) and the frame equals the stop frame...
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1697
	 * (the station test and stop type test ensure that other vehicles, using the road stop as
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1698
	 * a through route, do not stop) */
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7932
diff changeset
  1699
	if (IsRoadVehFront(v) && ((IsInsideMM(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END) &&
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
  1700
			_road_veh_data_1[v->u.road.state - RVSB_IN_ROAD_STOP + (_settings_game.vehicle.road_side << RVS_DRIVE_SIDE)] == v->u.road.frame) ||
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7932
diff changeset
  1701
			(IsInsideMM(v->u.road.state, RVSB_IN_DT_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) &&
8919
403ddceeb879 (svn r12689) -Feature: non-stop(or rather no non-stop) and via orders for road vehicles.
rubidium
parents: 8891
diff changeset
  1702
			v->current_order.ShouldStopAtStation(v, GetStationIndex(v->tile)) &&
9500
12e1f7e8add9 (svn r13480) -Fix [FS#2050]: RVs stoppping at drive through stations of other companies.
rubidium
parents: 9488
diff changeset
  1703
			v->owner == GetRoadOwner(v->tile, v->u.road.roadtype) &&
8785
871586967963 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8784
diff changeset
  1704
			GetRoadStopType(v->tile) == (IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? ROADSTOP_BUS : ROADSTOP_TRUCK) &&
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1705
			v->u.road.frame == RVC_DRIVE_THROUGH_STOP_FRAME))) {
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1706
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1707
		RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
9611
60e02c65ee73 (svn r13664) -Fix [FS#2117]: bus/truck forgetting go-to-depot order when entering a non-drivethrough road stop.
rubidium
parents: 9605
diff changeset
  1708
		Station *st = GetStationByTile(v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1709
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1710
		/* Vehicle is at the stop position (at a bay) in a road stop.
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1711
		 * Note, if vehicle is loading/unloading it has already been handled,
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1712
		 * so if we get here the vehicle has just arrived or is just ready to leave. */
8919
403ddceeb879 (svn r12689) -Feature: non-stop(or rather no non-stop) and via orders for road vehicles.
rubidium
parents: 8891
diff changeset
  1713
		if (!v->current_order.IsType(OT_LEAVESTATION)) {
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1714
			/* Vehicle has arrived at a bay in a road stop */
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1715
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1716
			if (IsDriveThroughStopTile(v->tile)) {
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1717
				TileIndex next_tile = TILE_ADD(v->tile, TileOffsByDir(v->direction));
8785
871586967963 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8784
diff changeset
  1718
				RoadStopType type = IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? ROADSTOP_BUS : ROADSTOP_TRUCK;
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1719
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1720
				/* Check if next inline bay is free */
9560
e4bfca78db29 (svn r13581) -Fix [FS#2040]: RVs continueing onto next DT station when they are build adjacent to them.
rubidium
parents: 9500
diff changeset
  1721
				if (IsDriveThroughStopTile(next_tile) && (GetRoadStopType(next_tile) == type) && GetStationIndex(v->tile) == GetStationIndex(next_tile)) {
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1722
					RoadStop *rs_n = GetRoadStopByTile(next_tile, type);
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1723
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
  1724
					if (rs_n->IsFreeBay(HasBit(v->u.road.state, RVS_USING_SECOND_BAY))) {
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1725
						/* Bay in next stop along is free - use it */
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1726
						ClearSlot(v);
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1727
						rs_n->num_vehicles++;
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1728
						v->u.road.slot = rs_n;
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1729
						v->dest_tile = rs_n->xy;
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1730
						v->u.road.slot_age = 14;
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1731
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1732
						v->u.road.frame++;
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1733
						RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1734
						return true;
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1735
					}
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1736
				}
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1737
			}
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1738
5990
280fbef32d44 (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 5989
diff changeset
  1739
			rs->SetEntranceBusy(false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1740
8919
403ddceeb879 (svn r12689) -Feature: non-stop(or rather no non-stop) and via orders for road vehicles.
rubidium
parents: 8891
diff changeset
  1741
			v->last_station_visited = st->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1742
9611
60e02c65ee73 (svn r13664) -Fix [FS#2117]: bus/truck forgetting go-to-depot order when entering a non-drivethrough road stop.
rubidium
parents: 9605
diff changeset
  1743
			if (IsDriveThroughStopTile(v->tile) || (v->current_order.IsType(OT_GOTO_STATION) && v->current_order.GetDestination() == st->index)) {
8919
403ddceeb879 (svn r12689) -Feature: non-stop(or rather no non-stop) and via orders for road vehicles.
rubidium
parents: 8891
diff changeset
  1744
				RoadVehArrivesAt(v, st);
403ddceeb879 (svn r12689) -Feature: non-stop(or rather no non-stop) and via orders for road vehicles.
rubidium
parents: 8891
diff changeset
  1745
				v->BeginLoading();
9611
60e02c65ee73 (svn r13664) -Fix [FS#2117]: bus/truck forgetting go-to-depot order when entering a non-drivethrough road stop.
rubidium
parents: 9605
diff changeset
  1746
				return false;
8919
403ddceeb879 (svn r12689) -Feature: non-stop(or rather no non-stop) and via orders for road vehicles.
rubidium
parents: 8891
diff changeset
  1747
			}
9611
60e02c65ee73 (svn r13664) -Fix [FS#2117]: bus/truck forgetting go-to-depot order when entering a non-drivethrough road stop.
rubidium
parents: 9605
diff changeset
  1748
		} else {
60e02c65ee73 (svn r13664) -Fix [FS#2117]: bus/truck forgetting go-to-depot order when entering a non-drivethrough road stop.
rubidium
parents: 9605
diff changeset
  1749
			/* Vehicle is ready to leave a bay in a road stop */
5990
280fbef32d44 (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 5989
diff changeset
  1750
			if (rs->IsEntranceBusy()) {
6009
bbe756070082 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6005
diff changeset
  1751
				/* Road stop entrance is busy, so wait as there is nowhere else to go */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1752
				v->cur_speed = 0;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1753
				return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1754
			}
6263
6bd0726c74e2 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6259
diff changeset
  1755
			v->current_order.Free();
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1756
			ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1757
		}
6009
bbe756070082 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6005
diff changeset
  1758
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1759
		if (IsStandardRoadStopTile(v->tile)) rs->SetEntranceBusy(true);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1760
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1761
		if (rs == v->u.road.slot) {
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1762
			/* We are leaving the correct station */
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1763
			ClearSlot(v);
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1764
		} else if (v->u.road.slot != NULL) {
6393
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
  1765
			/* We are leaving the wrong station
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
  1766
			 * XXX The question is .. what to do? Actually we shouldn't be here
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
  1767
			 * but I guess we need to clear the slot */
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1768
			DEBUG(ms, 0, "Vehicle %d (index %d) arrived at wrong stop", v->unitnumber, v->index);
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1769
			if (v->tile != v->dest_tile) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1770
				DEBUG(ms, 2, " current tile 0x%X is not destination tile 0x%X. Route problem", v->tile, v->dest_tile);
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1771
			}
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1772
			if (v->dest_tile != v->u.road.slot->xy) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1773
				DEBUG(ms, 2, " stop tile 0x%X is not destination tile 0x%X. Multistop desync", v->u.road.slot->xy, v->dest_tile);
3138
7e6c85698ee5 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1774
			}
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8830
diff changeset
  1775
			if (!v->current_order.IsType(OT_GOTO_STATION)) {
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8830
diff changeset
  1776
				DEBUG(ms, 2, " current order type (%d) is not OT_GOTO_STATION", v->current_order.GetType());
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1777
			} else {
8840
332412c2e9c1 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 8836
diff changeset
  1778
				if (v->current_order.GetDestination() != st->index)
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1779
					DEBUG(ms, 2, " current station %d is not target station in current_order.station (%d)",
8840
332412c2e9c1 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 8836
diff changeset
  1780
							st->index, v->current_order.GetDestination());
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1781
			}
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1782
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1783
			DEBUG(ms, 2, " force a slot clearing");
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1784
			ClearSlot(v);
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1785
		}
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1786
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1787
		StartRoadVehSound(v);
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8317
diff changeset
  1788
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1789
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1790
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1791
	/* Check tile position conditions - i.e. stop position in depot,
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1792
	 * entry onto bridge or into tunnel */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1793
	r = VehicleEnterTile(v, v->tile, x, y);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
  1794
	if (HasBit(r, VETS_CANNOT_ENTER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1795
		v->cur_speed = 0;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1796
		return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1797
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1798
10302
8d4bdf14adb4 (svn r14545) -Fix [FS#2386]: road vehicles sometimes never got their "slots" deallocated causing RVs not going to depot for service.
rubidium
parents: 10230
diff changeset
  1799
	if (v->current_order.IsType(OT_LEAVESTATION) && IsDriveThroughStopTile(v->tile)) {
8d4bdf14adb4 (svn r14545) -Fix [FS#2386]: road vehicles sometimes never got their "slots" deallocated causing RVs not going to depot for service.
rubidium
parents: 10230
diff changeset
  1800
		v->current_order.Free();
8d4bdf14adb4 (svn r14545) -Fix [FS#2386]: road vehicles sometimes never got their "slots" deallocated causing RVs not going to depot for service.
rubidium
parents: 10230
diff changeset
  1801
		ClearSlot(v);
8d4bdf14adb4 (svn r14545) -Fix [FS#2386]: road vehicles sometimes never got their "slots" deallocated causing RVs not going to depot for service.
rubidium
parents: 10230
diff changeset
  1802
	}
8919
403ddceeb879 (svn r12689) -Feature: non-stop(or rather no non-stop) and via orders for road vehicles.
rubidium
parents: 8891
diff changeset
  1803
5989
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1804
	/* Move to next frame unless vehicle arrived at a stop position
4236781ef448 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 5944
diff changeset
  1805
	 * in a depot or entered a tunnel/bridge */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
  1806
	if (!HasBit(r, VETS_ENTERED_WORMHOLE)) v->u.road.frame++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1807
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7096
diff changeset
  1808
	v->cur_image = v->GetImage(v->direction);
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
  1809
	v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1810
	RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1811
	return true;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1812
}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1813
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1814
static void RoadVehController(Vehicle *v)
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1815
{
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1816
	/* decrease counters */
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1817
	v->tick_counter++;
6980
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6953
diff changeset
  1818
	v->current_order_time++;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1819
	if (v->u.road.reverse_ctr != 0) v->u.road.reverse_ctr--;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1820
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1821
	/* handle crashed */
7476
f6eb56e8b8a7 (svn r10983) -Codechange: use vehstatus & VS_CRASHED instead of some other "methods" custom to each vehicle to determine whether the vehicle is crashed.
rubidium
parents: 7469
diff changeset
  1822
	if (v->vehstatus & VS_CRASHED) {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1823
		RoadVehIsCrashed(v);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1824
		return;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1825
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1826
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1827
	RoadVehCheckTrainCrash(v);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1828
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1829
	/* road vehicle has broken down? */
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1830
	if (v->breakdown_ctr != 0) {
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1831
		if (v->breakdown_ctr <= 2) {
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1832
			HandleBrokenRoadVeh(v);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1833
			return;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1834
		}
9000
0eee16b3074d (svn r12795) -Fix [FS#1938]: vehicles could break down during loading and keep loading. The intention of the break down code is not to break down when having zero speed, therefor break downs now do not happen when loading.
rubidium
parents: 8969
diff changeset
  1835
		if (!v->current_order.IsType(OT_LOADING)) v->breakdown_ctr--;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1836
	}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1837
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1838
	if (v->vehstatus & VS_STOPPED) return;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1839
8827
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
  1840
	ProcessOrders(v);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1841
	v->HandleLoading();
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1842
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8830
diff changeset
  1843
	if (v->current_order.IsType(OT_LOADING)) return;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1844
7490
bb27d92565d3 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7476
diff changeset
  1845
	if (v->IsInDepot() && RoadVehLeaveDepot(v, true)) return;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1846
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1847
	/* Check if vehicle needs to proceed, return if it doesn't */
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1848
	if (!RoadVehAccelerate(v)) return;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1849
7492
35acee076719 (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
parents: 7490
diff changeset
  1850
	for (Vehicle *prev = NULL; v != NULL; prev = v, v = v->Next()) {
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1851
		if (!IndividualRoadVehicleController(v, prev)) break;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1852
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1853
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1854
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1855
static void AgeRoadVehCargo(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1856
{
3017
a75caf4efa2d (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1857
	if (_age_cargo_skip_counter != 0) return;
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6980
diff changeset
  1858
	v->cargo.AgeCargo();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1859
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1860
7135
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  1861
void RoadVehicle::Tick()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1862
{
7135
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
  1863
	AgeRoadVehCargo(this);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1864
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  1865
	if (IsRoadVehFront(this)) {
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  1866
		if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  1867
		RoadVehController(this);
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  1868
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1869
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1870
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1871
static void CheckIfRoadVehNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1872
{
6393
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
  1873
	/* If we already got a slot at a stop, use that FIRST, and go to a depot later */
9413
7042a8ec3fa8 (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
parents: 9358
diff changeset
  1874
	if (v->u.road.slot != NULL || _settings_game.vehicle.servint_roadveh == 0 || !v->NeedsAutomaticServicing()) return;
7490
bb27d92565d3 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7476
diff changeset
  1875
	if (v->IsInDepot()) {
4529
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  1876
		VehicleServiceInDepot(v);
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  1877
		return;
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  1878
	}
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  1879
6393
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
  1880
	/* XXX If we already have a depot order, WHY do we search over and over? */
7502
f821f134ec5a (svn r11017) -Codechange: unify determining whether a vehicle needs/can be service a little more.
rubidium
parents: 7497
diff changeset
  1881
	const Depot *depot = FindClosestRoadDepot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1882
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1883
	if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) {
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8830
diff changeset
  1884
		if (v->current_order.IsType(OT_GOTO_DEPOT)) {
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8830
diff changeset
  1885
			v->current_order.MakeDummy();
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8317
diff changeset
  1886
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1887
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1888
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1889
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1890
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8830
diff changeset
  1891
	if (v->current_order.IsType(OT_GOTO_DEPOT) &&
8848
635cb164edd4 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 8843
diff changeset
  1892
			v->current_order.GetNonStopType() & ONSF_NO_STOP_AT_INTERMEDIATE_STATIONS &&
7967
a230c063a672 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 7954
diff changeset
  1893
			!Chance16(1, 20)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1894
		return;
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1895
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1896
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8830
diff changeset
  1897
	if (v->current_order.IsType(OT_LOADING)) v->LeaveStation();
6613
1f914b423535 (svn r9833) -Fix: also 'leave' the station when leaving for automatic servicing.
rubidium
parents: 6608
diff changeset
  1898
	ClearSlot(v);
1f914b423535 (svn r9833) -Fix: also 'leave' the station when leaving for automatic servicing.
rubidium
parents: 6608
diff changeset
  1899
8853
3266f0374302 (svn r12615) -Codechange: rename some enums related to depot orders to make it more clear that they are no loading/unloading flags. Also add more type strictness.
rubidium
parents: 8848
diff changeset
  1900
	v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1901
	v->dest_tile = depot->xy;
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8317
diff changeset
  1902
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1903
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1904
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1905
void RoadVehicle::OnNewDay()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1906
{
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1907
	if (!IsRoadVehFront(this)) return;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6790
diff changeset
  1908
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1909
	if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1910
	if (this->u.road.blocked_ctr == 0) CheckVehicleBreakdown(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1911
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1912
	AgeVehicle(this);
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1913
	CheckIfRoadVehNeedsService(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1914
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1915
	CheckOrders(this);
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 11
diff changeset
  1916
6393
e1e4939d19b5 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6357
diff changeset
  1917
	/* Current slot has expired */
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8830
diff changeset
  1918
	if (this->current_order.IsType(OT_GOTO_STATION) && this->u.road.slot != NULL && this->u.road.slot_age-- == 0) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1919
		DEBUG(ms, 3, "Slot expired for vehicle %d (index %d) at stop 0x%X",
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1920
			this->unitnumber, this->index, this->u.road.slot->xy);
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1921
		ClearSlot(this);
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1922
	}
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1923
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1924
	/* update destination */
10321
4a43d5022b7f (svn r14566) -Fix [FS#2397]: RV's go via order would reserve a slot at the 'via' station which it never uses, which makes it unlikely that it reserves a slot for the next station and that makes queueing fail (Aali)
rubidium
parents: 10302
diff changeset
  1925
	if (!(this->vehstatus & VS_STOPPED) && this->current_order.IsType(OT_GOTO_STATION) && !(this->current_order.GetNonStopType() & ONSF_NO_STOP_AT_DESTINATION_STATION) && this->u.road.slot == NULL && !(this->vehstatus & VS_CRASHED)) {
8840
332412c2e9c1 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 8836
diff changeset
  1926
		Station *st = GetStation(this->current_order.GetDestination());
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1927
		RoadStop *rs = st->GetPrimaryRoadStop(this);
7469
2c1dd5ce245d (svn r10974) -Fix [FS#1144, FS#1155]: road vehicles that could not (properly) use a road stop still tried to go to that road stop.
rubidium
parents: 7450
diff changeset
  1928
		RoadStop *best = NULL;
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1929
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1930
		if (rs != NULL) {
5910
f1409c7e8a87 (svn r8536) -Fix (FS#577): Road Vehicles now can obtain a slot even if the station is very spread out
celestar
parents: 5876
diff changeset
  1931
			/* We try to obtain a slot if:
f1409c7e8a87 (svn r8536) -Fix (FS#577): Road Vehicles now can obtain a slot even if the station is very spread out
celestar
parents: 5876
diff changeset
  1932
			 * 1) we're reasonably close to the primary road stop
f1409c7e8a87 (svn r8536) -Fix (FS#577): Road Vehicles now can obtain a slot even if the station is very spread out
celestar
parents: 5876
diff changeset
  1933
			 * or
f1409c7e8a87 (svn r8536) -Fix (FS#577): Road Vehicles now can obtain a slot even if the station is very spread out
celestar
parents: 5876
diff changeset
  1934
			 * 2) we're somewhere close to the station rectangle (to make sure we do assign
f1409c7e8a87 (svn r8536) -Fix (FS#577): Road Vehicles now can obtain a slot even if the station is very spread out
celestar
parents: 5876
diff changeset
  1935
			 *    slots even if the station and its road stops are incredibly spread out)
f1409c7e8a87 (svn r8536) -Fix (FS#577): Road Vehicles now can obtain a slot even if the station is very spread out
celestar
parents: 5876
diff changeset
  1936
			 */
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1937
			if (DistanceManhattan(this->tile, rs->xy) < 16 || st->rect.PtInExtendedRect(TileX(this->tile), TileY(this->tile), 2)) {
3431
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1938
				uint dist, badness;
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1939
				uint minbadness = UINT_MAX;
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1940
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1941
				DEBUG(ms, 2, "Attempting to obtain a slot for vehicle %d (index %d) at station %d (0x%X)",
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1942
					this->unitnumber, this->index, st->index, st->xy
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3990
diff changeset
  1943
				);
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1944
				/* Now we find the nearest road stop that has a free slot */
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1945
				for (; rs != NULL; rs = rs->GetNextRoadStop(this)) {
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1946
					dist = RoadFindPathToStop(this, rs->xy);
3431
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1947
					if (dist == UINT_MAX) {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1948
						DEBUG(ms, 4, " stop 0x%X is unreachable, not treating further", rs->xy);
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1949
						continue;
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1950
					}
3915
914d45c135c7 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1951
					badness = (rs->num_vehicles + 1) * (rs->num_vehicles + 1) + dist;
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1952
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1953
					DEBUG(ms, 4, " stop 0x%X has %d vehicle%s waiting", rs->xy, rs->num_vehicles, rs->num_vehicles == 1 ? "":"s");
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1954
					DEBUG(ms, 4, " distance is %u", dist);
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1955
					DEBUG(ms, 4, " badness %u", badness);
3431
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1956
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1957
					if (badness < minbadness) {
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1958
						best = rs;
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1959
						minbadness = badness;
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1960
					}
3033
9cba043eb38f (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1961
				}
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1962
3431
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1963
				if (best != NULL) {
e3b4bc9c4375 (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1964
					best->num_vehicles++;
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1965
					DEBUG(ms, 3, "Assigned to stop 0x%X", best->xy);
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1966
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1967
					this->u.road.slot = best;
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1968
					this->dest_tile = best->xy;
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1969
					this->u.road.slot_age = 14;
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1970
				} else {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1971
					DEBUG(ms, 3, "Could not find a suitable stop");
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1972
				}
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1973
			} else {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1974
				DEBUG(ms, 5, "Distance from station too far. Postponing slotting for vehicle %d (index %d) at station %d, (0x%X)",
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1975
						this->unitnumber, this->index, st->index, st->xy);
1295
ae28afd6267e (svn r1799) -Codechange: [ Multistop ] Added debug class "ms" and more debugging
celestar
parents: 1282
diff changeset
  1976
			}
3123
ad1ec737e974 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1977
		} else {
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1978
			DEBUG(ms, 4, "No road stop for vehicle %d (index %d) at station %d (0x%X)",
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1979
					this->unitnumber, this->index, st->index, st->xy);
1217
59c024cfaf54 (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1980
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1981
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1982
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  1983
	if (this->running_ticks == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1984
8626
9781464622be (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138
parents: 8616
diff changeset
  1985
	const RoadVehicleInfo *rvi = RoadVehInfo(this->engine_type);
9781464622be (svn r12209) -Fix: [NewGRF] Support using any base price for rail and road vehicles' running cost, and show running cost of wagons if they have it.
peter1138
parents: 8616
diff changeset
  1986
	CommandCost cost(EXPENSES_ROADVEH_RUN, rvi->running_cost * GetPriceByIndex(rvi->running_cost_class) * this->running_ticks / (364 * DAY_TICKS));
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  1987
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  1988
	this->profit_this_year -= cost.GetCost();
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
  1989
	this->running_ticks = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1990
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
  1991
	SubtractMoneyFromCompanyFract(this->owner, cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1992
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8424
diff changeset
  1993
	InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1128
diff changeset
  1994
	InvalidateWindowClasses(WC_ROADVEH_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1995
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1996
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1997
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6176
diff changeset
  1998
void RoadVehiclesYearlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1999
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2000
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2001
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2002
	FOR_ALL_VEHICLES(v) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  2003
		if (v->type == VEH_ROAD) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2004
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2005
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2006
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2007
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2008
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2009
}
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2010
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2011
/** Refit a road vehicle to the specified cargo type
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2012
 * @param tile unused
6483
ae78fc3c6b20 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6453
diff changeset
  2013
 * @param flags operation to perform
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2014
 * @param p1 Vehicle ID of the vehicle to refit
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2015
 * @param p2 Bitstuffed elements
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2016
 * - p2 = (bit 0-7) - the new cargo type to refit to
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2017
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
7418
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2018
 * - p2 = (bit 16) - refit only this vehicle
6546
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6544
diff changeset
  2019
 * @return cost of refit or error
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2020
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6879
diff changeset
  2021
CommandCost CmdRefitRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2022
{
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2023
	Vehicle *v;
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8226
diff changeset
  2024
	CommandCost cost(EXPENSES_ROADVEH_RUN);
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2025
	CargoID new_cid = GB(p2, 0, 8);
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2026
	byte new_subtype = GB(p2, 8, 8);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
  2027
	bool only_this = HasBit(p2, 16);
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2028
	uint16 capacity = CALLBACK_FAILED;
7418
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2029
	uint total_capacity = 0;
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2030
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  2031
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2032
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2033
	v = GetVehicle(p1);
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2034
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  2035
	if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
9935
abf91482d3af (svn r14090) -Fix: the vehicle window of articulated road vehicles would show the clone/refit button when the vehicle was not completely stopped in the depot.
rubidium
parents: 9923
diff changeset
  2036
	if (!v->IsStoppedInDepot()) return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);
8145
2116aa2c6263 (svn r11707) -Fix: do not allow refitting flooded (destroyed) vehicles
smatz
parents: 8144
diff changeset
  2037
	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE);
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2038
7418
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2039
	if (new_cid >= NUM_CARGO) return CMD_ERROR;
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2040
7492
35acee076719 (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
parents: 7490
diff changeset
  2041
	for (; v != NULL; v = v->Next()) {
7418
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2042
		/* XXX: We refit all the attached wagons en-masse if they can be
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2043
		 * refitted. This is how TTDPatch does it.  TODO: Have some nice
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2044
		 * [Refit] button near each wagon. */
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2045
		if (!CanRefitTo(v->engine_type, new_cid)) continue;
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2046
7418
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2047
		if (v->cargo_cap == 0) continue;
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2048
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7924
diff changeset
  2049
		if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_REFIT_CAPACITY)) {
7418
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2050
			/* Back up the cargo type */
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2051
			CargoID temp_cid = v->cargo_type;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2052
			byte temp_subtype = v->cargo_subtype;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2053
			v->cargo_type = new_cid;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2054
			v->cargo_subtype = new_subtype;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2055
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2056
			/* Check the refit capacity callback */
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2057
			capacity = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2058
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2059
			/* Restore the original cargo type */
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2060
			v->cargo_type = temp_cid;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2061
			v->cargo_subtype = temp_subtype;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2062
		}
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2063
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2064
		if (capacity == CALLBACK_FAILED) {
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2065
			/* callback failed or not used, use default capacity */
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2066
			const RoadVehicleInfo *rvi = RoadVehInfo(v->engine_type);
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2067
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2068
			CargoID old_cid = rvi->cargo_type;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2069
			/* normally, the capacity depends on the cargo type, a vehicle can
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2070
			 * carry twice as much mail/goods as normal cargo, and four times as
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2071
			 * many passengers
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2072
			 */
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2073
			capacity = GetVehicleProperty(v, 0x0F, rvi->capacity);
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2074
			switch (old_cid) {
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2075
				case CT_PASSENGERS: break;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2076
				case CT_MAIL:
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2077
				case CT_GOODS: capacity *= 2; break;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2078
				default:       capacity *= 4; break;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2079
			}
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2080
			switch (new_cid) {
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2081
				case CT_PASSENGERS: break;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2082
				case CT_MAIL:
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2083
				case CT_GOODS: capacity /= 2; break;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2084
				default:       capacity /= 4; break;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2085
			}
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2086
		}
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2087
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2088
		total_capacity += capacity;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2089
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10174
diff changeset
  2090
		if (IsHumanCompany(v->owner) && new_cid != v->cargo_type) {
7418
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2091
			cost.AddCost(GetRefitCost(v->engine_type));
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2092
		}
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2093
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2094
		if (flags & DC_EXEC) {
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2095
			v->cargo_cap = capacity;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2096
			v->cargo.Truncate((v->cargo_type == new_cid) ? capacity : 0);
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2097
			v->cargo_type = new_cid;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2098
			v->cargo_subtype = new_subtype;
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2099
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2100
			InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9234
diff changeset
  2101
			InvalidateWindowClassesData(WC_ROADVEH_LIST, 0);
7418
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2102
		}
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2103
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2104
		if (only_this) break;
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2105
	}
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2106
7418
ef667c8ccda0 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7416
diff changeset
  2107
	_returned_refit_capacity = total_capacity;
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2108
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2109
	return cost;
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2110
}