src/roadveh_cmd.cpp
author rubidium
Sat, 26 Apr 2008 20:54:56 +0000
branch0.6
changeset 10366 960aae8c20e2
parent 10351 4f1563682453
child 10474 80c90b755053
permissions -rw-r--r--
(svn r12907) [0.6] -Backport from trunk r12906:
- Fix: Vehicle groups, engine replacement rules and player/company names were not properly reset/freed after bankrupt (r12906)
[0.6] -Prepare: for 0.6.1-RC1.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
     3
/** @file roadveh_cmd.cpp */
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
     4
3508
99974a14a81d (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
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1793
diff changeset
     6
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1295
diff changeset
     7
#include "debug.h"
8615
6b91ca653bad (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8612
diff changeset
     8
#include "tile_cmd.h"
6949
72d11a1e1e60 (svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents: 6903
diff changeset
     9
#include "landscape.h"
3144
426b825578f9 (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
e2bbaa8d4978 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3958
diff changeset
    11
#include "roadveh.h"
3315
1f65f8260092 (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"
7476
7f086e4b2a76 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 7449
diff changeset
    13
#include "timetable.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "engine.h"
8612
6414fc21c2f3 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8610
diff changeset
    15
#include "command_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include "station.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#include "news.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
#include "pathfind.h"
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    19
#include "npf.h"
8750
fdd6054e7bae (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8732
diff changeset
    20
#include "player_func.h"
fdd6054e7bae (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8732
diff changeset
    21
#include "player_base.h"
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
    22
#include "depot.h"
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
    23
#include "bridge.h"
3184
7405329343ce (svn r3830) Move IsTunnelTile() from tile.h to tunnel_map.h and add IsTunnel(), which just checks for a tunnel, but not the tile type as IsTunnelTile() does
tron
parents: 3169
diff changeset
    24
#include "tunnel_map.h"
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
    25
#include "bridge_map.h"
1752
cdbfb2f23e72 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1745
diff changeset
    26
#include "vehicle_gui.h"
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
    27
#include "articulated_vehicles.h"
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
    28
#include "newgrf_callbacks.h"
2962
dbd168a4703a (svn r3524) - Split newgrf features from engine.[ch] into newgrf_engine.[ch], and add the new files to project files.
peter1138
parents: 2954
diff changeset
    29
#include "newgrf_engine.h"
4244
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
    30
#include "newgrf_text.h"
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
    31
#include "newgrf_sound.h"
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
    32
#include "yapf/yapf.h"
6655
951b0ba32eb1 (svn r9301) -Codechange: Use cargo class to determine if a road vehicle can stop in bus or truck stops.
peter1138
parents: 6642
diff changeset
    33
#include "cargotype.h"
8610
17cc343a23dd (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8604
diff changeset
    34
#include "strings_func.h"
8579
3efbb430092e (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 8577
diff changeset
    35
#include "tunnelbridge_map.h"
8627
448ebf3a8291 (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: 8615
diff changeset
    36
#include "functions.h"
448ebf3a8291 (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: 8615
diff changeset
    37
#include "window_func.h"
8636
2b158acb649c (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8635
diff changeset
    38
#include "date_func.h"
8640
1e93b81e96d2 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8636
diff changeset
    39
#include "vehicle_func.h"
8653
a83f7a536919 (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium
parents: 8641
diff changeset
    40
#include "sound_func.h"
8707
55835d8fbfcd (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8703
diff changeset
    41
#include "variables.h"
8708
0c29fbc79be4 (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium
parents: 8707
diff changeset
    42
#include "autoreplace_gui.h"
8720
4e60c30e2006 (svn r11787) -Codechange: more header rewrites. This time related to viewport.h.
rubidium
parents: 8708
diff changeset
    43
#include "gfx_func.h"
8766
c86cfa3a7580 (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8762
diff changeset
    44
#include "settings_type.h"
8579
3efbb430092e (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 8577
diff changeset
    45
8760
ce0891c412ce (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8757
diff changeset
    46
#include "table/strings.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
static const uint16 _roadveh_images[63] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
	0xCD4, 0xCDC, 0xCE4, 0xCEC, 0xCF4, 0xCFC, 0xD0C, 0xD14,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
	0xD24, 0xD1C, 0xD2C, 0xD04, 0xD1C, 0xD24, 0xD6C, 0xD74,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
	0xD7C, 0xC14, 0xC1C, 0xC24, 0xC2C, 0xC34, 0xC3C, 0xC4C,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
	0xC54, 0xC64, 0xC5C, 0xC6C, 0xC44, 0xC5C, 0xC64, 0xCAC,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
	0xCB4, 0xCBC, 0xD94, 0xD9C, 0xDA4, 0xDAC, 0xDB4, 0xDBC,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
	0xDCC, 0xDD4, 0xDE4, 0xDDC, 0xDEC, 0xDC4, 0xDDC, 0xDE4,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
	0xE2C, 0xE34, 0xE3C, 0xC14, 0xC1C, 0xC2C, 0xC3C, 0xC4C,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
	0xC5C, 0xC64, 0xC6C, 0xC74, 0xC84, 0xC94, 0xCA4
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
static const uint16 _roadveh_full_adder[63] = {
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    60
	 0,  88,   0,   0,   0,   0,  48,  48,
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    61
	48,  48,   0,   0,  64,  64,   0,  16,
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    62
	16,   0,  88,   0,   0,   0,   0,  48,
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    63
	48,  48,  48,   0,   0,  64,  64,   0,
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    64
	16,  16,   0,  88,   0,   0,   0,   0,
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    65
	48,  48,  48,  48,   0,   0,  64,  64,
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    66
	 0,  16,  16,   0,   8,   8,   8,   8,
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
    67
	 0,   0,   0,   8,   8,   8,   8
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
6325
f03f94f4d8fc (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: 6317
diff changeset
    70
/** 'Convert' the DiagDirection where a road vehicle enters to the trackdirs it can drive onto */
f03f94f4d8fc (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: 6317
diff changeset
    71
static const TrackdirBits _road_enter_dir_to_reachable_trackdirs[DIAGDIR_END] = {
f03f94f4d8fc (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: 6317
diff changeset
    72
	TRACKDIR_BIT_LEFT_N  | TRACKDIR_BIT_LOWER_E | TRACKDIR_BIT_X_NE,    // Enter from north east
f03f94f4d8fc (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: 6317
diff changeset
    73
	TRACKDIR_BIT_LEFT_S  | TRACKDIR_BIT_UPPER_E | TRACKDIR_BIT_Y_SE,    // Enter from south east
f03f94f4d8fc (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: 6317
diff changeset
    74
	TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_X_SW    | TRACKDIR_BIT_RIGHT_S, // Enter from south west
f03f94f4d8fc (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: 6317
diff changeset
    75
	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
    76
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
6325
f03f94f4d8fc (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: 6317
diff changeset
    78
static const Trackdir _road_reverse_table[DIAGDIR_END] = {
f03f94f4d8fc (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: 6317
diff changeset
    79
	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
    80
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
6325
f03f94f4d8fc (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: 6317
diff changeset
    82
/** 'Convert' the DiagDirection where a road vehicle should exit to
f03f94f4d8fc (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: 6317
diff changeset
    83
 * the trackdirs it can use to drive to the exit direction*/
f03f94f4d8fc (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: 6317
diff changeset
    84
static const TrackdirBits _road_exit_dir_to_incoming_trackdirs[DIAGDIR_END] = {
f03f94f4d8fc (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: 6317
diff changeset
    85
	TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_X_SW    | TRACKDIR_BIT_LEFT_S,
f03f94f4d8fc (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: 6317
diff changeset
    86
	TRACKDIR_BIT_LEFT_N  | TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_Y_NW,
f03f94f4d8fc (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: 6317
diff changeset
    87
	TRACKDIR_BIT_RIGHT_N | TRACKDIR_BIT_UPPER_E | TRACKDIR_BIT_X_NE,
f03f94f4d8fc (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: 6317
diff changeset
    88
	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
    89
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
6326
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
    91
/** Converts the exit direction of a depot to trackdir the vehicle is going to drive to */
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
    92
static const Trackdir _roadveh_depot_exit_trackdir[DIAGDIR_END] = {
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
    93
	TRACKDIR_X_NE, TRACKDIR_Y_SE, TRACKDIR_X_SW, TRACKDIR_Y_NW
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
    94
};
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
    95
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7592
diff changeset
    96
int RoadVehicle::GetImage(Direction direction) const
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
{
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7592
diff changeset
    98
	int img = this->spritenum;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
	int image;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
	if (is_custom_sprite(img)) {
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7592
diff changeset
   102
		image = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   103
		if (image != 0) return image;
8722
30cffd0c4e40 (svn r11789) -Codechange: coding style for global variable names
peter1138
parents: 8720
diff changeset
   104
		img = _orig_road_vehicle_info[this->engine_type - ROAD_ENGINES_INDEX].image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   106
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
	image = direction + _roadveh_images[img];
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7592
diff changeset
   108
	if (this->cargo.Count() >= this->cargo_cap / 2U) image += _roadveh_full_adder[img];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
	return image;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5902
diff changeset
   112
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
   113
{
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   114
	int spritenum = RoadVehInfo(engine)->image_index;
378
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
   115
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
   116
	if (is_custom_sprite(spritenum)) {
3186
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3184
diff changeset
   117
		int sprite = GetCustomVehicleIcon(engine, DIR_W);
378
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
   118
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   119
		if (sprite != 0) {
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5902
diff changeset
   120
			DrawSprite(sprite, pal, x, y);
378
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
   121
			return;
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
   122
		}
8722
30cffd0c4e40 (svn r11789) -Codechange: coding style for global variable names
peter1138
parents: 8720
diff changeset
   123
		spritenum = _orig_road_vehicle_info[engine - ROAD_ENGINES_INDEX].image_index;
378
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
   124
	}
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5902
diff changeset
   125
	DrawSprite(6 + _roadveh_images[spritenum], pal, x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7375
diff changeset
   128
static CommandCost EstimateRoadVehCost(EngineID engine_type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
{
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8722
diff changeset
   130
	return CommandCost(EXPENSES_NEW_VEHICLES, ((_price.roadveh_base >> 3) * GetEngineProperty(engine_type, 0x11, RoadVehInfo(engine_type)->base_cost)) >> 5);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   133
byte GetRoadVehLength(const Vehicle *v)
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   134
{
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   135
	byte length = 8;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   136
7711
66dffbd3b136 (svn r10493) -Codechange: update some callback ID enums to reflect their changed usage, add a few and update the comments.
rubidium
parents: 7631
diff changeset
   137
	uint16 veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, v->engine_type, v);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   138
	if (veh_len != CALLBACK_FAILED) {
8418
b49fc6be1ab9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 8405
diff changeset
   139
		length -= Clamp(veh_len, 0, 7);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   140
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   141
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   142
	return length;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   143
}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   144
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   145
void RoadVehUpdateCache(Vehicle *v)
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   146
{
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   147
	assert(v->type == VEH_ROAD);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   148
	assert(IsRoadVehFront(v));
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   149
7988
6075538f6111 (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: 7986
diff changeset
   150
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7988
diff changeset
   151
		/* Check the v->first cache. */
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7988
diff changeset
   152
		assert(u->First() == v);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   153
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   154
		/* Update the 'first engine' */
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   155
		u->u.road.first_engine = (v == u) ? INVALID_ENGINE : v->engine_type;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   156
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   157
		/* Update the length of the vehicle. */
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   158
		u->u.road.cached_veh_length = GetRoadVehLength(u);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   159
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   160
}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   161
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   162
/** Build a road vehicle.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   163
 * @param tile tile of depot where road vehicle is built
6979
d194d25020e7 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6949
diff changeset
   164
 * @param flags operation to perform
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   165
 * @param p1 bus/truck type being built (engine)
3816
2cdb29b57c1f (svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents: 3793
diff changeset
   166
 * @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   167
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7375
diff changeset
   168
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
   169
{
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7375
diff changeset
   170
	CommandCost cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
	Vehicle *v;
1282
e7a73ee62d2f (svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents: 1266
diff changeset
   172
	UnitID unit_num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
	Engine *e;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   174
6903
0b3d43bd1fdb (svn r9543) -Fix [FS#691]: Split up STR_ENGINE_NOT_BUILDABLE into four strings so it can be
maedhros
parents: 6889
diff changeset
   175
	if (!IsEngineBuildable(p1, VEH_ROAD, _current_player)) return_cmd_error(STR_ROAD_VEHICLE_NOT_AVAILABLE);
1196
115f46e3807d (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1151
diff changeset
   176
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
	cost = EstimateRoadVehCost(p1);
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   178
	if (flags & DC_QUERY_COST) return cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
1442
3e0ac5f13ee2 (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
   180
	/* The ai_new queries the vehicle cost before building the route,
3e0ac5f13ee2 (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
   181
	 * so we must check against cheaters no sooner than now. --pasky */
3e0ac5f13ee2 (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
   182
	if (!IsTileDepotType(tile, TRANSPORT_ROAD)) return CMD_ERROR;
1901
fb05044cf5c3 (svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents: 1891
diff changeset
   183
	if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
1442
3e0ac5f13ee2 (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
   184
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
   185
	if (HasTileRoadType(tile, ROADTYPE_TRAM) != HasBit(EngInfo(p1)->misc_flags, EF_ROAD_TRAM)) return_cmd_error(STR_DEPOT_WRONG_DEPOT_TYPE);
7205
e3d6da6d1e0b (svn r9941) -Fix: trams could be cloned to appear in normal road depots and vice versa (spotted by Desolator)
bjarni
parents: 7191
diff changeset
   186
8091
f24d360b6ba1 (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: 8089
diff changeset
   187
	uint num_vehicles = 1 + CountArticulatedParts(p1, false);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   188
8091
f24d360b6ba1 (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: 8089
diff changeset
   189
	/* Allow for the front and the articulated parts, plus one to "terminate" the list. */
f24d360b6ba1 (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: 8089
diff changeset
   190
	Vehicle **vl = (Vehicle**)alloca(sizeof(*vl) * (num_vehicles + 1));
f24d360b6ba1 (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: 8089
diff changeset
   191
	memset(vl, 0, sizeof(*vl) * (num_vehicles + 1));
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   192
7894
dbe6e9295ec5 (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7866
diff changeset
   193
	if (!Vehicle::AllocateList(vl, num_vehicles)) {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   194
		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   195
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   196
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   197
	v = vl[0];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
	/* find the first free roadveh id */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
   200
	unit_num = HasBit(p2, 0) ? 0 : GetFreeUnitNumber(VEH_ROAD);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
	if (unit_num > _patches.max_roadveh)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
		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
   203
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
	if (flags & DC_EXEC) {
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   205
		int x;
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   206
		int y;
4c8427796c64 (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
   207
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   208
		const RoadVehicleInfo *rvi = RoadVehInfo(p1);
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   209
8278
c10ecabf001d (svn r11332) -Fix: vehicles getting a value of 0 on construction.
rubidium
parents: 8264
diff changeset
   210
		v = new (v) RoadVehicle();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
		v->unitnumber = unit_num;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   212
		v->direction = DiagDirToDir(GetRoadDepotDirection(tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
		v->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
		v->tile = tile;
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3630
diff changeset
   216
		x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3630
diff changeset
   217
		y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
		v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
		v->y_pos = y;
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6979
diff changeset
   220
		v->z_pos = GetSlopeZ(x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
9052
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
   222
		v->running_ticks = 0;
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
   223
6326
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
   224
		v->u.road.state = RVSB_IN_DEPOT;
6987
b0f13039bda2 (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6979
diff changeset
   225
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   227
		v->spritenum = rvi->image_index;
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   228
		v->cargo_type = rvi->cargo_type;
3870
766d7bd6c299 (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3816
diff changeset
   229
		v->cargo_subtype = 0;
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   230
		v->cargo_cap = rvi->capacity;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
//		v->cargo_count = 0;
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   232
		v->value = cost.GetCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
//		v->day_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
//		v->next_order_param = v->next_order = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
//		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
//		v->progress = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   238
//	v->u.road.unk2 = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   239
//	v->u.road.overtaking = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   240
1266
9dc5638fe8cc (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
   241
		v->last_station_visited = INVALID_STATION;
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   242
		v->max_speed = rvi->max_speed;
8703
62b5536f70c2 (svn r11770) -Codechange: An engine type's type is EngineID, not byte.
peter1138
parents: 8653
diff changeset
   243
		v->engine_type = (EngineID)p1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   244
1926
68d60188a22f (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1901
diff changeset
   245
		e = GetEngine(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
		v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
		v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
		v->max_age = e->lifelength * 366;
2564
371e0fa97e1e (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2552
diff changeset
   249
		_new_vehicle_id = v->index;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   250
8754
5dae65402806 (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8750
diff changeset
   251
		v->name = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
		v->service_interval = _patches.servint_roadveh;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   254
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
		v->date_of_last_service = _date;
4329
9759d5c52010 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   256
		v->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   257
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
		v->cur_image = 0xC15;
2804
b932fe148d36 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2767
diff changeset
   259
		v->random_bits = VehicleRandomBits();
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   260
		SetRoadVehFront(v);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   261
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
   262
		v->u.road.roadtype = HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   263
		v->u.road.compatible_roadtypes = RoadTypeToRoadTypes(v->u.road.roadtype);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   264
		v->u.road.cached_veh_length = GetRoadVehLength(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   265
6502
7ca154aca2b7 (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6501
diff changeset
   266
		v->vehicle_flags = 0;
8427
143b0be22af1 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 8425
diff changeset
   267
		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
6502
7ca154aca2b7 (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6501
diff changeset
   268
7104
59f24cd8470b (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: 7090
diff changeset
   269
		v->cargo_cap = GetVehicleProperty(v, 0x0F, rvi->capacity);
59f24cd8470b (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: 7090
diff changeset
   270
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   271
		AddArticulatedParts(vl, VEH_ROAD);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   272
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
		VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4725
diff changeset
   275
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   276
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   277
		InvalidateWindow(WC_COMPANY, v->owner);
2618
7546b838230b (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2574
diff changeset
   278
		if (IsLocalPlayer())
7921
f99d5c351e75 (svn r10823) -Fix r8610 [FS#1097]: the autoreplace window vehicle count didn't always update correctly) (Matthias)
bjarni
parents: 7917
diff changeset
   279
			InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Road window
6195
b90cf92697b9 (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 6194
diff changeset
   280
b90cf92697b9 (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 6194
diff changeset
   281
		GetPlayer(_current_player)->num_engines[p1]++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8722
diff changeset
   284
	return cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   287
/** Start/Stop a road vehicle.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   288
 * @param tile unused
6979
d194d25020e7 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6949
diff changeset
   289
 * @param flags operation to perform
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   290
 * @param p1 road vehicle ID to start/stop
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   291
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   292
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7375
diff changeset
   293
CommandCost CmdStartStopRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
{
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   295
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   296
10330
700e4a785535 (svn r12871) [0.6] -Backport from trunk r12819, r12818, r12759:
rubidium
parents: 10327
diff changeset
   297
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   299
	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
   300
4244
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
   301
	/* Check if this road veh can be started/stopped. The callback will fail or
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
   302
	 * return 0xFF if it can. */
10330
700e4a785535 (svn r12871) [0.6] -Backport from trunk r12819, r12818, r12759:
rubidium
parents: 10327
diff changeset
   303
	uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
700e4a785535 (svn r12871) [0.6] -Backport from trunk r12819, r12818, r12759:
rubidium
parents: 10327
diff changeset
   304
	if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF) {
4244
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
   305
		StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
   306
		return_cmd_error(error);
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
   307
	}
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
   308
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
	if (flags & DC_EXEC) {
7986
881998b115c2 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7972
diff changeset
   310
		if (v->IsStoppedInDepot()) {
3139
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3138
diff changeset
   311
			DeleteVehicleNews(p1, STR_9016_ROAD_VEHICLE_IS_WAITING);
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3138
diff changeset
   312
		}
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3138
diff changeset
   313
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
		v->vehstatus ^= VS_STOPPED;
7188
78055acf1785 (svn r9924) -Fix: update the road vehicle speeds when stopped and/or running just behind another vehicle.
rubidium
parents: 7187
diff changeset
   315
		v->cur_speed = 0;
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8813
diff changeset
   316
		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
   317
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   320
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   322
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   323
void ClearSlot(Vehicle *v)
1664
000099fbae6e (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
   324
{
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   325
	RoadStop *rs = v->u.road.slot;
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   326
	if (v->u.road.slot == NULL) return;
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   327
1664
000099fbae6e (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
   328
	v->u.road.slot = NULL;
000099fbae6e (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
   329
	v->u.road.slot_age = 0;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   330
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   331
	assert(rs->num_vehicles != 0);
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   332
	rs->num_vehicles--;
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   333
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
   334
	DEBUG(ms, 3, "Clearing slot at 0x%X", rs->xy);
1664
000099fbae6e (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
   335
}
000099fbae6e (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
   336
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   337
static bool CheckRoadVehInDepotStopped(const Vehicle *v)
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   338
{
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   339
	TileIndex tile = v->tile;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   340
7946
e53b2f085489 (svn r10924) -Fix [FS#1120]: VS_STOPPED is only set for the front part of an articulated
maedhros
parents: 7921
diff changeset
   341
	if (!IsTileDepotType(tile, TRANSPORT_ROAD)) return false;
e53b2f085489 (svn r10924) -Fix [FS#1120]: VS_STOPPED is only set for the front part of an articulated
maedhros
parents: 7921
diff changeset
   342
	if (IsRoadVehFront(v) && !(v->vehstatus & VS_STOPPED)) return false;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   343
7988
6075538f6111 (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: 7986
diff changeset
   344
	for (; v != NULL; v = v->Next()) {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   345
		if (v->u.road.state != RVSB_IN_DEPOT || v->tile != tile) return false;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   346
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   347
	return true;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   348
}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   349
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   350
/** Sell a road vehicle.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   351
 * @param tile unused
6979
d194d25020e7 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6949
diff changeset
   352
 * @param flags operation to perform
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   353
 * @param p1 vehicle ID to be sold
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   354
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   355
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7375
diff changeset
   356
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
   357
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   359
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   360
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   361
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 904
diff changeset
   362
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   363
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   364
	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
   365
8191
3ffc17ef4ff5 (svn r11229) -Fix [FS#1307]: one could sell vehicles that were crashed in a depot, which would still yield money.
rubidium
parents: 8165
diff changeset
   366
	if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
3ffc17ef4ff5 (svn r11229) -Fix [FS#1307]: one could sell vehicles that were crashed in a depot, which would still yield money.
rubidium
parents: 8165
diff changeset
   367
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   368
	if (!CheckRoadVehInDepotStopped(v)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
		return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   370
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   371
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8722
diff changeset
   372
	CommandCost ret(EXPENSES_NEW_VEHICLES, -v->value);
8264
670152de17cb (svn r11317) -Fix [FS#1355]: don't read a variable of a destroyed vehicle as the value will be reset to 0.
rubidium
parents: 8191
diff changeset
   373
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
		// Invalidate depot
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   377
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   378
		InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
		DeleteWindowById(WC_VEHICLE_VIEW, v->index);
5256
90226d8da8fb (svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents: 5215
diff changeset
   380
		DeleteDepotHighlightOfVehicle(v);
7894
dbe6e9295ec5 (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7866
diff changeset
   381
		delete v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   383
8264
670152de17cb (svn r11317) -Fix [FS#1355]: don't read a variable of a destroyed vehicle as the value will be reset to 0.
rubidium
parents: 8191
diff changeset
   384
	return ret;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   387
struct RoadFindDepotData {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
	uint best_length;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
   389
	TileIndex tile;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   390
	OwnerByte owner;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   391
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
3186
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3184
diff changeset
   393
static const DiagDirection _road_pf_directions[] = {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   394
	DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, INVALID_DIAGDIR, INVALID_DIAGDIR,
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   395
	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
   396
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   397
9107
3e57b96e98f2 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 9105
diff changeset
   398
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
   399
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   400
	RoadFindDepotData* rfdd = (RoadFindDepotData*)data;
3137
1d321ced37f3 (svn r3752) FYOS avoid to cast function pointers
tron
parents: 3136
diff changeset
   401
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   402
	tile += TileOffsByDiagDir(_road_pf_directions[trackdir]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
   404
	if (IsRoadDepotTile(tile) &&
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   405
			IsTileOwner(tile, rfdd->owner) &&
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   406
			length < rfdd->best_length) {
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   407
		rfdd->best_length = length;
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   408
		rfdd->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   413
static const Depot* FindClosestRoadDepot(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
{
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   415
	switch (_patches.pathfinder_for_roadvehs) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   416
		case VPF_YAPF: /* YAPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   417
			return YapfFindNearestRoadDepot(v);
1745
2dcc0a66d91d (svn r2249) - Fix: [ 1187703 ] [NPF] Road vehicles cannot find a depot when in a roadstop
matthijs
parents: 1709
diff changeset
   418
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   419
		case VPF_NPF: { /* NPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   420
			/* See where we are now */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   421
			Trackdir trackdir = GetVehicleTrackdir(v);
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   422
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   423
			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
   424
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   425
			if (ftd.best_bird_dist == 0) return GetDepotByTile(ftd.node.tile); /* Target found */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   426
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   428
		default:
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   429
		case VPF_OPF: { /* OPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   430
			RoadFindDepotData rfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   432
			rfdd.owner = v->owner;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   433
			rfdd.best_length = UINT_MAX;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   434
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   435
			/* search in all directions */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   436
			for (DiagDirection d = DIAGDIR_BEGIN; d < DIAGDIR_END; d++) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   437
				FollowTrack(v->tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, d, EnumRoadSignalFindDepot, NULL, &rfdd);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   438
			}
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   439
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   440
			if (rfdd.best_length != UINT_MAX) return GetDepotByTile(rfdd.tile);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   441
		} break;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   442
	}
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   443
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
   444
	return NULL; /* Target not found */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   447
/** Send a road vehicle to the depot.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   448
 * @param tile unused
6979
d194d25020e7 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6949
diff changeset
   449
 * @param flags operation to perform
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   450
 * @param p1 vehicle ID to send to the depot
4451
66603f0f732e (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4445
diff changeset
   451
 * @param p2 various bitmasked elements
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   452
 * - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   453
 * - p2 bit 8-10 - VLW flag (for mass goto depot)
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   454
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7375
diff changeset
   455
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
   456
{
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   457
	Vehicle *v;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   458
	const Depot *dep;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   460
	if (p2 & DEPOT_MASS_SEND) {
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   461
		/* Mass goto depot requested */
4546
9345e0569655 (svn r6378) -Codechange: Rename VLW_FLAGS to VLW_MASK as it is a mask
Darkvater
parents: 4544
diff changeset
   462
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   463
		return SendAllVehiclesToDepot(VEH_ROAD, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_MASK), p1);
4463
3a70624c40eb (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   464
	}
3a70624c40eb (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   465
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   466
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   467
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   468
	v = GetVehicle(p1);
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   469
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   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
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   472
	if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
1757
b179d50241dc (svn r2261) - Fix: When crashed vehicles try to find a depot for servicing, openttd asserts.
matthijs
parents: 1752
diff changeset
   473
7986
881998b115c2 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7972
diff changeset
   474
	if (v->IsInDepot()) return CMD_ERROR;
4526
77ef74d28e76 (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
   475
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   476
	/* If the current orders are already goto-depot */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   477
	if (v->current_order.type == OT_GOTO_DEPOT) {
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
   478
		if (!!(p2 & DEPOT_SERVICE) == HasBit(v->current_order.flags, OF_HALT_IN_DEPOT)) {
4510
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   479
			/* We called with a different DEPOT_SERVICE setting.
4549
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
   480
			 * Now we change the setting to apply the new one and let the vehicle head for the same depot.
60410aa1aa88 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4546
diff changeset
   481
			 * Note: the if is (true for requesting service == true for ordered to stop in depot) */
4510
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   482
			if (flags & DC_EXEC) {
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
   483
				ClrBit(v->current_order.flags, OF_PART_OF_ORDERS);
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
   484
				ToggleBit(v->current_order.flags, OF_HALT_IN_DEPOT);
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8813
diff changeset
   485
				InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
4510
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   486
			}
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   487
			return CommandCost();
4510
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   488
		}
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   489
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   490
		if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
		if (flags & DC_EXEC) {
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   492
			/* If the orders to 'goto depot' are in the orders list (forced servicing),
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   493
			 * then skip to the next order; effectively cancelling this forced service */
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
   494
			if (HasBit(v->current_order.flags, OF_PART_OF_ORDERS))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
				v->cur_order_index++;
1530
caa16c506a22 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
   496
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   497
			v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   498
			v->current_order.flags = 0;
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8813
diff changeset
   499
			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
   500
		}
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   501
		return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   502
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   504
	dep = FindClosestRoadDepot(v);
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   505
	if (dep == NULL) return_cmd_error(STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
	if (flags & DC_EXEC) {
6998
403314de0154 (svn r9685) -Fix (r9683): Call v->LeaveStation() when a vehicle in a station is sent to a depot.
maedhros
parents: 6987
diff changeset
   508
		if (v->current_order.type == OT_LOADING) v->LeaveStation();
403314de0154 (svn r9685) -Fix (r9683): Call v->LeaveStation() when a vehicle in a station is sent to a depot.
maedhros
parents: 6987
diff changeset
   509
3236
36e8eb3ae55a (svn r3909) [multistop]
celestar
parents: 3186
diff changeset
   510
		ClearSlot(v);
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   511
		v->current_order.type = OT_GOTO_DEPOT;
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
   512
		v->current_order.flags = OFB_NON_STOP;
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
   513
		if (!(p2 & DEPOT_SERVICE)) SetBit(v->current_order.flags, OF_HALT_IN_DEPOT);
5259
08336337aae0 (svn r7389) -Fix: when forcing a vehicle to go to a depot, clear refit orders from current orders (caused refit error popup when not having no orders)
bjarni
parents: 5256
diff changeset
   514
		v->current_order.refit_cargo = CT_INVALID;
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
   515
		v->current_order.dest = dep->index;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   516
		v->dest_tile = dep->xy;
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8813
diff changeset
   517
		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
   518
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   519
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   520
	return CommandCost();
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
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   523
/** Turn a roadvehicle around.
3491
4c8427796c64 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3481
diff changeset
   524
 * @param tile unused
6979
d194d25020e7 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6949
diff changeset
   525
 * @param flags operation to perform
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   526
 * @param p1 vehicle ID to turn
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   527
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   528
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7375
diff changeset
   529
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
   530
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   531
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   532
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   533
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   534
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 904
diff changeset
   535
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   537
	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
   538
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   539
	if (v->vehstatus & VS_STOPPED ||
7972
dff055fff851 (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: 7965
diff changeset
   540
			v->vehstatus & VS_CRASHED ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
			v->breakdown_ctr != 0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
			v->u.road.overtaking != 0 ||
6326
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
   543
			v->u.road.state == RVSB_WORMHOLE ||
7986
881998b115c2 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7972
diff changeset
   544
			v->IsInDepot() ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
			v->cur_speed < 5) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   548
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
   549
	if (IsNormalRoadTile(v->tile) && GetDisallowedRoadDirections(v->tile) != DRD_NONE) return CMD_ERROR;
7260
23cd58b6bf51 (svn r9999) -Feature: make it possible to disallow busses and lorries to go a specific way on straight pieces of road.
rubidium
parents: 7250
diff changeset
   550
8584
a8b6dffead63 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8579
diff changeset
   551
	if (IsTileType(v->tile, MP_TUNNELBRIDGE) && DirToDiagDir(v->direction) == GetTunnelBridgeDirection(v->tile)) return CMD_ERROR;
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   552
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   553
	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
   554
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   555
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   556
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   558
7049
01825af2ce90 (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 7048
diff changeset
   559
void RoadVehicle::MarkDirty()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
{
7988
6075538f6111 (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: 7986
diff changeset
   561
	for (Vehicle *v = this; v != NULL; v = v->Next()) {
7915
8b20c0c2debf (svn r10812) -Fix (r10097) [FS#1093]: Make RoadVehicle::MarkDirty() mark all parts of articulated vehicles as dirty, not just the front.
maedhros
parents: 7914
diff changeset
   562
		v->cur_image = v->GetImage(v->direction);
8813
6d054db96ede (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8798
diff changeset
   563
		MarkSingleVehicleDirty(v);
7915
8b20c0c2debf (svn r10812) -Fix (r10097) [FS#1093]: Make RoadVehicle::MarkDirty() mark all parts of articulated vehicles as dirty, not just the front.
maedhros
parents: 7914
diff changeset
   564
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
   567
void RoadVehicle::UpdateDeltaXY(Direction direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
{
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
   569
#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
   570
	static const uint32 _delta_xy_table[8] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   573
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
	};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
#undef MKIT
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
   581
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
   582
	uint32 x = _delta_xy_table[direction];
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
   583
	this->x_offs        = GB(x,  0, 8);
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
   584
	this->y_offs        = GB(x,  8, 8);
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
   585
	this->sprite_width  = GB(x, 16, 8);
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
   586
	this->sprite_height = GB(x, 24, 8);
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
   587
	this->z_height      = 6;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   590
static void ClearCrashedStation(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
{
2671
457c6cdbf9e7 (svn r3213) - Codechange: Clean up handling of road stops, avoiding unnecessary use of pointers and using the *BIT() macros.
peter1138
parents: 2662
diff changeset
   592
	RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
6316
6ba1efc2b7fc (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 6315
diff changeset
   594
	/* Mark the station entrance as not busy */
6ba1efc2b7fc (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 6315
diff changeset
   595
	rs->SetEntranceBusy(false);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   596
6316
6ba1efc2b7fc (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 6315
diff changeset
   597
	/* Free the parking bay */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
   598
	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
   599
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   601
static void DeleteLastRoadVeh(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
{
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   603
	Vehicle *u = v;
7988
6075538f6111 (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: 7986
diff changeset
   604
	for (; v->Next() != NULL; v = v->Next()) u = v;
6075538f6111 (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: 7986
diff changeset
   605
	u->SetNext(NULL);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   606
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
	DeleteWindowById(WC_VEHICLE_VIEW, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   608
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   609
	RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
	InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   612
	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
   613
8813
6d054db96ede (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8798
diff changeset
   614
	MarkSingleVehicleDirty(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   615
7894
dbe6e9295ec5 (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7866
diff changeset
   616
	delete v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
static byte SetRoadVehPosition(Vehicle *v, int x, int y)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   620
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
	byte new_z, old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
   623
	/* need this hint so it returns the right z coordinate on bridges. */
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   624
	v->x_pos = x;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   625
	v->y_pos = y;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   626
	new_z = GetSlopeZ(x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
	old_z = v->z_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
	v->z_pos = new_z;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   630
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
	return old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
static void RoadVehSetRandomDirection(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
{
3160
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   638
	static const DirDiff delta[] = {
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   639
		DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   640
	};
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   641
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   642
	do {
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   643
		uint32 r = Random();
3160
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   644
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   645
		v->direction = ChangeDir(v->direction, delta[r & 3]);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   646
		BeginVehicleMove(v);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   647
		v->UpdateDeltaXY(v->direction);
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7592
diff changeset
   648
		v->cur_image = v->GetImage(v->direction);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   649
		SetRoadVehPosition(v, v->x_pos, v->y_pos);
7988
6075538f6111 (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: 7986
diff changeset
   650
	} while ((v = v->Next()) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   651
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   653
static void RoadVehIsCrashed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
	v->u.road.crashed_ctr++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
	if (v->u.road.crashed_ctr == 2) {
1359
8ba976aed634 (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
   657
		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   658
	} else if (v->u.road.crashed_ctr <= 45) {
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   659
		if ((v->tick_counter & 7) == 0) RoadVehSetRandomDirection(v);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   660
	} else if (v->u.road.crashed_ctr >= 2220 && !(v->tick_counter & 0x1F)) {
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   661
		DeleteLastRoadVeh(v);
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   664
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   665
static void* EnumCheckRoadVehCrashTrain(Vehicle* v, void* data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   666
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   667
	const Vehicle* u = (Vehicle*)data;
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   668
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   669
	return
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   670
		v->type == VEH_TRAIN &&
8419
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   671
		abs(v->z_pos - u->z_pos) <= 6 &&
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   672
		abs(v->x_pos - u->x_pos) <= 4 &&
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   673
		abs(v->y_pos - u->y_pos) <= 4 ?
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   674
			v : NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   675
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   676
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   677
static void RoadVehCrash(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   678
{
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   679
	uint16 pass = 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   680
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   681
	v->u.road.crashed_ctr++;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   682
7988
6075538f6111 (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: 7986
diff changeset
   683
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
7506
e52d89f5c7c1 (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: 7476
diff changeset
   684
		if (IsCargoInClass(u->cargo_type, CC_PASSENGERS)) pass += u->cargo.Count();
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   685
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   686
		u->vehstatus |= VS_CRASHED;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   687
8813
6d054db96ede (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8798
diff changeset
   688
		MarkSingleVehicleDirty(u);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   689
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   690
3236
36e8eb3ae55a (svn r3909) [multistop]
celestar
parents: 3186
diff changeset
   691
	ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   692
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8813
diff changeset
   693
	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
   694
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   695
	SetDParam(0, pass);
2260
3e97367f7cbc (svn r2780) Remove some more unused strings and make the use of a few strings more explicit
tron
parents: 2214
diff changeset
   696
	AddNewsItem(
3e97367f7cbc (svn r2780) Remove some more unused strings and make the use of a few strings more explicit
tron
parents: 2214
diff changeset
   697
		(pass == 1) ?
3e97367f7cbc (svn r2780) Remove some more unused strings and make the use of a few strings more explicit
tron
parents: 2214
diff changeset
   698
			STR_9031_ROAD_VEHICLE_CRASH_DRIVER : STR_9032_ROAD_VEHICLE_CRASH_DIE,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   699
		NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   700
		v->index,
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   701
		0
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   702
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   703
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   704
	ModifyStationRatingAround(v->tile, v->owner, -160, 22);
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 538
diff changeset
   705
	SndPlayVehicleFx(SND_12_EXPLOSION, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   706
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   708
static void RoadVehCheckTrainCrash(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   709
{
7988
6075538f6111 (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: 7986
diff changeset
   710
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   711
		if (u->u.road.state == RVSB_WORMHOLE) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   713
		TileIndex tile = u->tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   714
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   715
		if (!IsLevelCrossingTile(tile)) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
7367
a0499d5cb8e5 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 7353
diff changeset
   717
		if (VehicleFromPosXY(v->x_pos, v->y_pos, u, EnumCheckRoadVehCrashTrain) != NULL) {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   718
			RoadVehCrash(v);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   719
			return;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   720
		}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
   721
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   722
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   723
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
static void HandleBrokenRoadVeh(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   726
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   727
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   728
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   729
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   730
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
			v->breakdowns_since_last_service++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   732
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   735
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   736
		if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
6683
7ec558346172 (svn r9400) -Codechange: Use some more representative enum names for landscape types.
belugas
parents: 6657
diff changeset
   737
			SndPlayVehicleFx((_opt.landscape != LT_TOYLAND) ?
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   738
				SND_0F_VEHICLE_BREAKDOWN : SND_35_COMEDY_BREAKDOWN, v);
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   739
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   740
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   741
		if (!(v->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   742
			Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
7830
210381b46ae6 (svn r10697) -Codechange: give a more sensible names to some of the unkX variables.
rubidium
parents: 7813
diff changeset
   743
			if (u != NULL) u->u.special.animation_state = v->breakdown_delay * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   744
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   745
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   747
	if ((v->tick_counter & 1) == 0) {
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   748
		if (--v->breakdown_delay == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   749
			v->breakdown_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   750
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   751
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   752
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   753
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   754
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   755
static void ProcessRoadVehOrder(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   756
{
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   757
	const Order *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   758
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   759
	switch (v->current_order.type) {
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   760
		case OT_GOTO_DEPOT:
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
   761
			/* Let a depot order in the orderlist interrupt. */
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
   762
			if (!(v->current_order.flags & OFB_PART_OF_ORDERS)) return;
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
   763
			if (v->current_order.flags & OFB_SERVICE_IF_NEEDED &&
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   764
					!VehicleNeedsService(v)) {
7566
435e3c4dbf28 (svn r10335) -Fix (r10331): Increment the current order index *after* using it in UpdateVehicleTimetable.
maedhros
parents: 7519
diff changeset
   765
				UpdateVehicleTimetable(v, true);
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   766
				v->cur_order_index++;
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   767
			}
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   768
			break;
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   769
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   770
		case OT_LOADING:
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   771
		case OT_LEAVESTATION:
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   772
			return;
4351
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4344
diff changeset
   773
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4344
diff changeset
   774
		default: break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   775
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   776
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   777
	if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   778
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   779
	order = GetVehicleOrder(v, v->cur_order_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   780
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   781
	if (order == NULL) {
6589
addf4b6946d0 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6585
diff changeset
   782
		v->current_order.Free();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   783
		v->dest_tile = 0;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   784
		ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   785
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   786
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   787
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
   788
	if (order->type  == v->current_order.type &&
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
   789
			order->flags == v->current_order.flags &&
10327
2274772a4424 (svn r12868) [0.6] -Backport from trunk r12795, r12776, r12683, r12681, r12678:
rubidium
parents: 9212
diff changeset
   790
			order->dest  == v->current_order.dest &&
2274772a4424 (svn r12868) [0.6] -Backport from trunk r12795, r12776, r12683, r12681, r12678:
rubidium
parents: 9212
diff changeset
   791
			v->dest_tile != 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
		return;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   793
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   794
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   795
	v->current_order = *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   796
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   797
	switch (order->type) {
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   798
		case OT_GOTO_STATION: {
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
   799
			if (order->dest == v->last_station_visited) {
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   800
				v->last_station_visited = INVALID_STATION;
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   801
			}
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   802
7965
c1a170885a60 (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: 7946
diff changeset
   803
			const RoadStop *rs = GetStation(order->dest)->GetPrimaryRoadStop(v);
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   804
7250
49ca9a9e70f1 (svn r9989) -Fix [FS#817]: trams/road vehicles did go to the closest road stop regardless whether they could actually "drive" on there.
rubidium
parents: 7205
diff changeset
   805
			TileIndex dest = INVALID_TILE;
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   806
			if (rs != NULL) {
7250
49ca9a9e70f1 (svn r9989) -Fix [FS#817]: trams/road vehicles did go to the closest road stop regardless whether they could actually "drive" on there.
rubidium
parents: 7205
diff changeset
   807
				uint mindist = MAX_UVALUE(uint);
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   808
7965
c1a170885a60 (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: 7946
diff changeset
   809
				for (; rs != NULL; rs = rs->GetNextRoadStop(v)) {
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   810
					uint dist = DistanceManhattan(v->tile, rs->xy);
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   811
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   812
					if (dist < mindist) {
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   813
						mindist = dist;
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   814
						dest = rs->xy;
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   815
					}
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   816
				}
7250
49ca9a9e70f1 (svn r9989) -Fix [FS#817]: trams/road vehicles did go to the closest road stop regardless whether they could actually "drive" on there.
rubidium
parents: 7205
diff changeset
   817
			}
49ca9a9e70f1 (svn r9989) -Fix [FS#817]: trams/road vehicles did go to the closest road stop regardless whether they could actually "drive" on there.
rubidium
parents: 7205
diff changeset
   818
49ca9a9e70f1 (svn r9989) -Fix [FS#817]: trams/road vehicles did go to the closest road stop regardless whether they could actually "drive" on there.
rubidium
parents: 7205
diff changeset
   819
			if (dest != INVALID_TILE) {
49ca9a9e70f1 (svn r9989) -Fix [FS#817]: trams/road vehicles did go to the closest road stop regardless whether they could actually "drive" on there.
rubidium
parents: 7205
diff changeset
   820
					v->dest_tile = dest;
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   821
			} else {
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
   822
				/* There is no stop left at the station, so don't even TRY to go there */
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   823
				v->cur_order_index++;
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   824
				v->dest_tile = 0;
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   825
			}
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   826
			break;
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   827
		}
1231
64cc1f5c1c82 (svn r1735) -Fix: [ 1112285 ] Fixed an assertion error with stations where stops
celestar
parents: 1224
diff changeset
   828
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   829
		case OT_GOTO_DEPOT:
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
   830
			v->dest_tile = GetDepot(order->dest)->xy;
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   831
			break;
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
   832
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   833
		default:
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   834
			v->dest_tile = 0;
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   835
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   836
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   837
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   838
	InvalidateVehicleOrder(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   839
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   841
static void StartRoadVehSound(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   842
{
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   843
	if (!PlayVehicleSound(v, VSE_START)) {
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   844
		SoundFx s = RoadVehInfo(v->engine_type)->sfx;
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   845
		if (s == SND_19_BUS_START_PULL_AWAY && (v->tick_counter & 3) == 0)
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   846
			s = SND_1A_BUS_START_PULL_AWAY_WITH_HORN;
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   847
		SndPlayVehicleFx(s, v);
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   848
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   849
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   850
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   851
struct RoadVehFindData {
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   852
	int x;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   853
	int y;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   854
	const Vehicle* veh;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   855
	Direction dir;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   856
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   857
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   858
static void* EnumCheckRoadVehClose(Vehicle *v, void* data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   859
{
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   860
	static const int8 dist_x[] = { -4, -8, -4, -1, 4, 8, 4, 1 };
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   861
	static const int8 dist_y[] = { -4, -1, 4, 8, 4, 1, -4, -8 };
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   862
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   863
	const RoadVehFindData* rvf = (RoadVehFindData*)data;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   864
63
53cc59601b3c (svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents: 19
diff changeset
   865
	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
   866
	short y_diff = v->y_pos - rvf->y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   867
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   868
	return
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   869
		v->type == VEH_ROAD &&
7986
881998b115c2 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7972
diff changeset
   870
		!v->IsInDepot() &&
8419
de9999f762d0 (svn r11476) -Codechange: rename the function myabs to abs to get rid of an unneeded define
skidd13
parents: 8418
diff changeset
   871
		abs(v->z_pos - rvf->veh->z_pos) < 6 &&
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   872
		v->direction == rvf->dir &&
7993
76b0eb3e19c9 (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7988
diff changeset
   873
		rvf->veh->First() != v->First() &&
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   874
		(dist_x[v->direction] >= 0 || (x_diff > dist_x[v->direction] && x_diff <= 0)) &&
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   875
		(dist_x[v->direction] <= 0 || (x_diff < dist_x[v->direction] && x_diff >= 0)) &&
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   876
		(dist_y[v->direction] >= 0 || (y_diff > dist_y[v->direction] && y_diff <= 0)) &&
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   877
		(dist_y[v->direction] <= 0 || (y_diff < dist_y[v->direction] && y_diff >= 0)) ?
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   878
			v : NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   879
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   880
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   881
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
   882
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   883
	RoadVehFindData rvf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   884
	Vehicle *u;
8312
48bd7541f6ae (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 8278
diff changeset
   885
	Vehicle *front = v->First();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
8312
48bd7541f6ae (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 8278
diff changeset
   887
	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
   888
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
	rvf.x = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
	rvf.y = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   891
	rvf.dir = dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   892
	rvf.veh = v;
8312
48bd7541f6ae (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 8278
diff changeset
   893
	if (front->u.road.state == RVSB_WORMHOLE) {
48bd7541f6ae (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 8278
diff changeset
   894
		u = (Vehicle*)VehicleFromPos(v->tile, &rvf, EnumCheckRoadVehClose);
8762
e0d5cde5228f (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: 8760
diff changeset
   895
		if (u == NULL) u = (Vehicle*)VehicleFromPos(GetOtherTunnelBridgeEnd(v->tile), &rvf, EnumCheckRoadVehClose);
8312
48bd7541f6ae (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 8278
diff changeset
   896
	} else {
48bd7541f6ae (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 8278
diff changeset
   897
		u = (Vehicle*)VehicleFromPosXY(x, y, &rvf, EnumCheckRoadVehClose);
48bd7541f6ae (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 8278
diff changeset
   898
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   899
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
   900
	/* This code protects a roadvehicle from being blocked for ever
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
   901
	 * If more than 1480 / 74 days a road vehicle is blocked, it will
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
   902
	 * drive just through it. The ultimate backup-code of TTD.
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
   903
	 * It can be disabled. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   904
	if (u == NULL) {
8312
48bd7541f6ae (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 8278
diff changeset
   905
		front->u.road.blocked_ctr = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   906
		return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   907
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
8312
48bd7541f6ae (svn r11366) -Fix [FS#1258]: road vehicles must not drive through eachother on bridges/in tunnels.
rubidium
parents: 8278
diff changeset
   909
	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
   910
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   911
	return u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   912
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   913
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   914
static void RoadVehArrivesAt(const Vehicle* v, Station* st)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   915
{
6655
951b0ba32eb1 (svn r9301) -Codechange: Use cargo class to determine if a road vehicle can stop in bus or truck stops.
peter1138
parents: 6642
diff changeset
   916
	if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   917
		/* Check if station was ever visited before */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   918
		if (!(st->had_vehicle_of_type & HVOT_BUS)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   919
			uint32 flags;
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
			st->had_vehicle_of_type |= HVOT_BUS;
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   922
			SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
			flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
			AddNewsItem(
8920
04df97185061 (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: 8888
diff changeset
   925
				v->u.road.roadtype == ROADTYPE_ROAD ? STR_902F_CITIZENS_CELEBRATE_FIRST : STR_CITIZENS_CELEBRATE_FIRST_PASSENGER_TRAM,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   926
				flags,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   927
				v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   928
				0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   929
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   930
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   931
		/* Check if station was ever visited before */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   932
		if (!(st->had_vehicle_of_type & HVOT_TRUCK)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
			uint32 flags;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
			st->had_vehicle_of_type |= HVOT_TRUCK;
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   936
			SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
			flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   938
			AddNewsItem(
8920
04df97185061 (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: 8888
diff changeset
   939
				v->u.road.roadtype == ROADTYPE_ROAD ? STR_9030_CITIZENS_CELEBRATE_FIRST : STR_CITIZENS_CELEBRATE_FIRST_CARGO_TRAM,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
				flags,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
				v->index,
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   942
				0
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   943
			);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   946
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
static bool RoadVehAccelerate(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
{
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   950
	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
   951
	byte t;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   952
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
   953
	/* Clamp */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   954
	spd = min(spd, v->max_speed);
6467
6228b576422d (svn r8884) -Fix
tron
parents: 6457
diff changeset
   955
	if (v->u.road.state == RVSB_WORMHOLE && !(v->vehstatus & VS_HIDDEN)) {
8987
782be613237b (svn r12066) -Codechange: Rename GetBridge for the more common GetBridgeSpec
belugas
parents: 8963
diff changeset
   956
		spd = min(spd, GetBridgeSpec(GetBridgeType(v->tile))->speed * 2);
6467
6228b576422d (svn r8884) -Fix
tron
parents: 6457
diff changeset
   957
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
   959
	/* 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
   960
	if (spd != v->cur_speed) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   961
		v->cur_speed = spd;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   962
		if (_patches.vehicle_speed) {
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8813
diff changeset
   963
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   964
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
   967
	/* Decrease somewhat when turning */
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   968
	if (!(v->direction & 1)) spd = spd * 3 >> 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   970
	if (spd == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   972
	if ((byte)++spd == 0) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   974
	v->progress = (t = v->progress) - (byte)spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   975
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   976
	return (t < v->progress);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   979
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
   980
{
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   981
	static const Direction _roadveh_new_dir[] = {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   982
		DIR_N , DIR_NW, DIR_W , INVALID_DIR,
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   983
		DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   984
		DIR_E , DIR_SE, DIR_S
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
	};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   986
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
	x = x - v->x_pos + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   988
	y = y - v->y_pos + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   990
	if ((uint)x > 2 || (uint)y > 2) return v->direction;
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
   991
	return _roadveh_new_dir[y * 4 + x];
0
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
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   994
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
   995
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   996
	Direction new_dir = RoadVehGetNewDirection(v, x, y);
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   997
	Direction old_dir = v->direction;
3159
2abc21583259 (svn r3785) Replace some if-magic to determine the turning direction for road vehicles with the new DirDiff stuff
tron
parents: 3157
diff changeset
   998
	DirDiff delta;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   999
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1000
	if (new_dir == old_dir) return old_dir;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1001
	delta = (DirDifference(new_dir, old_dir) > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1002
	return ChangeDir(old_dir, delta);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1004
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1005
struct OvertakeData {
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1006
	const Vehicle* u;
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1007
	const Vehicle* v;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
  1008
	TileIndex tile;
9105
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1009
	Trackdir trackdir;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1010
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1011
9105
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1012
static void* EnumFindVehBlockingOvertake(Vehicle* v, void* data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1014
	const OvertakeData* od = (OvertakeData*)data;
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1015
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1016
	return
8577
fc4b7dd34058 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8556
diff changeset
  1017
		v->type == VEH_ROAD && v->First() == v && v != od->u && v != od->v ?
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1018
			v : NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1019
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1020
9105
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1021
/**
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1022
 * Check if overtaking is possible on a piece of track
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1023
 *
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1024
 * @param od Information about the tile and the involved vehicles
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1025
 * @return true if we have to abort overtaking
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1026
 */
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1027
static bool CheckRoadBlockedForOvertaking(OvertakeData *od)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1028
{
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9107
diff changeset
  1029
	TrackStatus ts = GetTileTrackStatus(od->tile, TRANSPORT_ROAD, od->v->u.road.compatible_roadtypes);
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9107
diff changeset
  1030
	TrackdirBits trackdirbits = TrackStatusToTrackdirBits(ts);
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9107
diff changeset
  1031
	TrackdirBits red_signals = TrackStatusToRedSignals(ts); // barred level crossing
9105
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1032
	TrackBits trackbits = TrackdirBitsToTrackBits(trackdirbits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1033
9105
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1034
	/* Track does not continue along overtaking direction || track has junction || levelcrossing is barred */
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1035
	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
  1036
9105
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1037
	/* Are there more vehicles on the tile except the two vehicles involved in overtaking */
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1038
	return VehicleFromPos(od->tile, od, EnumFindVehBlockingOvertake) != NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1039
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1040
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1041
static void RoadVehCheckOvertake(Vehicle *v, Vehicle *u)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1042
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1043
	OvertakeData od;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1044
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1045
	od.v = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1046
	od.u = u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1047
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1048
	if (u->max_speed >= v->max_speed &&
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1049
			!(u->vehstatus & VS_STOPPED) &&
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1050
			u->cur_speed != 0) {
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1051
		return;
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1052
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
7183
23b20a982228 (svn r9919) -Codechange: prepare some more places for more road types.
rubidium
parents: 7181
diff changeset
  1054
	/* Trams can't overtake other trams */
23b20a982228 (svn r9919) -Codechange: prepare some more places for more road types.
rubidium
parents: 7181
diff changeset
  1055
	if (v->u.road.roadtype == ROADTYPE_TRAM) return;
23b20a982228 (svn r9919) -Codechange: prepare some more places for more road types.
rubidium
parents: 7181
diff changeset
  1056
8165
606d97bce909 (svn r11200) -Fix [FS#1291]: road vehicles could not overtake on one way roads going to the east.
rubidium
parents: 8138
diff changeset
  1057
	/* Don't overtake in stations */
606d97bce909 (svn r11200) -Fix [FS#1291]: road vehicles could not overtake on one way roads going to the east.
rubidium
parents: 8138
diff changeset
  1058
	if (IsTileType(v->tile, MP_STATION)) return;
606d97bce909 (svn r11200) -Fix [FS#1291]: road vehicles could not overtake on one way roads going to the east.
rubidium
parents: 8138
diff changeset
  1059
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1060
	/* For now, articulated road vehicles can't overtake anything. */
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1061
	if (RoadVehHasArticPart(v)) return;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1062
9105
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1063
	/* Vehicles are not driving in same direction || direction is not a diagonal direction */
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1064
	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
  1065
6326
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
  1066
	/* Check if vehicle is in a road stop, depot, tunnel or bridge or not on a straight road */
6457
2c447a703ec6 (svn r8868) -Fix (r8715): road vehicles could not overtake anymore in some directions (mart3p).
rubidium
parents: 6338
diff changeset
  1067
	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
  1068
9105
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1069
	od.trackdir = DiagdirToDiagTrackdir(DirToDiagDir(v->direction));
8165
606d97bce909 (svn r11200) -Fix [FS#1291]: road vehicles could not overtake on one way roads going to the east.
rubidium
parents: 8138
diff changeset
  1070
9105
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1071
	/* Are the current and the next tile suitable for overtaking?
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1072
	 *  - Does the track continue along od.trackdir
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1073
	 *  - No junctions
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1074
	 *  - No barred levelcrossing
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1075
	 *  - No other vehicles in the way
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1076
	 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1077
	od.tile = v->tile;
9105
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1078
	if (CheckRoadBlockedForOvertaking(&od)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1079
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4549
diff changeset
  1080
	od.tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
9105
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1081
	if (CheckRoadBlockedForOvertaking(&od)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1082
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1083
	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
  1084
		v->u.road.overtaking_ctr = 0x11;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1085
		v->u.road.overtaking = 0x10;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1086
	} else {
9105
ecc85c6d9176 (svn r12191) -Fix: Do not start overtaking if the RV reaches wrong-way one-way-road in the next tiles.
frosch
parents: 9059
diff changeset
  1087
//		if (CheckRoadBlockedForOvertaking(&od)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1088
		v->u.road.overtaking_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1089
		v->u.road.overtaking = 0x10;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1090
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1091
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1092
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1093
static void RoadZPosAffectSpeed(Vehicle *v, byte old_z)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1094
{
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1095
	if (old_z == v->z_pos) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1096
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1097
	if (old_z < v->z_pos) {
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1098
		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
  1099
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1100
		uint16 spd = v->cur_speed + 2;
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1101
		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
  1102
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1103
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1104
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1105
static int PickRandomBit(uint bits)
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
	uint i;
8328
6909973c8359 (svn r11382) -Codechange: renamed COUNTBITS to CountBits, as it is no longer a macro (skidd13)
truelight
parents: 8313
diff changeset
  1108
	uint num = RandomRange(CountBits(bits));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1109
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1110
	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
  1111
	return i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1112
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1113
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1114
struct FindRoadToChooseData {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1115
	TileIndex dest;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1116
	uint maxtracklen;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1117
	uint mindist;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1118
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
9107
3e57b96e98f2 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 9105
diff changeset
  1120
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
  1121
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1122
	FindRoadToChooseData* frd = (FindRoadToChooseData*)data;
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1239
diff changeset
  1123
	uint dist = DistanceManhattan(tile, frd->dest);
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1124
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1125
	if (dist <= frd->mindist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1126
		if (dist != frd->mindist || length < frd->maxtracklen) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1127
			frd->maxtracklen = length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1128
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1129
		frd->mindist = dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1130
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1131
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1132
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1133
9006
f68ac871be5c (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 9002
diff changeset
  1134
static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypes railtypes)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1135
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1136
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1137
	void* perf = NpfBeginInterval();
9006
f68ac871be5c (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 9002
diff changeset
  1138
	NPFFoundTargetData ret = NPFRouteToStationOrTile(tile, trackdir, ignore_start_tile, target, type, sub_type, owner, railtypes);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1139
	int t = NpfEndInterval(perf);
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1140
	DEBUG(yapf, 4, "[NPFR] %d us - %d rounds - %d open - %d closed -- ", t, 0, _aystar_stats_open_size, _aystar_stats_closed_size);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1141
	return ret;
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1142
}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1143
6325
f03f94f4d8fc (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: 6317
diff changeset
  1144
/**
f03f94f4d8fc (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: 6317
diff changeset
  1145
 * Returns direction to for a road vehicle to take or
f03f94f4d8fc (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: 6317
diff changeset
  1146
 * INVALID_TRACKDIR if the direction is currently blocked
6979
d194d25020e7 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6949
diff changeset
  1147
 * @param v        the Vehicle to do the pathfinding for
6325
f03f94f4d8fc (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: 6317
diff changeset
  1148
 * @param tile     the where to start the pathfinding
f03f94f4d8fc (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: 6317
diff changeset
  1149
 * @param enterdir the direction the vehicle enters the tile from
6979
d194d25020e7 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6949
diff changeset
  1150
 * @return the Trackdir to take
6325
f03f94f4d8fc (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: 6317
diff changeset
  1151
 */
f03f94f4d8fc (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: 6317
diff changeset
  1152
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
  1153
{
6325
f03f94f4d8fc (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: 6317
diff changeset
  1154
#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
  1155
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
  1156
	TileIndex desttile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1157
	FindRoadToChooseData frd;
6325
f03f94f4d8fc (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: 6317
diff changeset
  1158
	Trackdir best_track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1159
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9107
diff changeset
  1160
	TrackStatus ts = GetTileTrackStatus(tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes);
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9107
diff changeset
  1161
	TrackdirBits red_signals = TrackStatusToRedSignals(ts); // crossing
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9107
diff changeset
  1162
	TrackdirBits trackdirs = TrackStatusToTrackdirBits(ts);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1163
7866
e19fda04e8d3 (svn r10733) -Codechange: change MP_STREET into MP_ROAD as we use the word "road" everywhere except in the tile type.
rubidium
parents: 7830
diff changeset
  1164
	if (IsTileType(tile, MP_ROAD)) {
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
  1165
		if (IsRoadDepot(tile) && (!IsTileOwner(tile, v->owner) || GetRoadDepotDirection(tile) == enterdir || (GetRoadTypes(tile) & v->u.road.compatible_roadtypes) == 0)) {
4445
562232eef864 (svn r6223) -Fix: RVs with empty order list were able to enter the depot or road stop from the back. I introduced this bug when I "improved" GetTileTrackStatus(). (thanks glx for the report and fix).
KUDr
parents: 4434
diff changeset
  1166
			/* Road depot owned by another player or with the wrong orientation */
6325
f03f94f4d8fc (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: 6317
diff changeset
  1167
			trackdirs = TRACKDIR_BIT_NONE;
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2493
diff changeset
  1168
		}
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1169
	} else if (IsTileType(tile, MP_STATION) && IsStandardRoadStopTile(tile)) {
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1170
		/* Standard road stop (drive-through stops are treated as normal road) */
7965
c1a170885a60 (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: 7946
diff changeset
  1171
c1a170885a60 (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: 7946
diff changeset
  1172
		if (!IsTileOwner(tile, v->owner) || GetRoadStopDir(tile) == enterdir || RoadVehHasArticPart(v)) {
c1a170885a60 (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: 7946
diff changeset
  1173
			/* different station owner or wrong orientation or the vehicle has articulated parts */
6325
f03f94f4d8fc (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: 6317
diff changeset
  1174
			trackdirs = TRACKDIR_BIT_NONE;
3937
60b89172e88e (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
  1175
		} else {
241
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1176
			/* Our station */
6655
951b0ba32eb1 (svn r9301) -Codechange: Use cargo class to determine if a road vehicle can stop in bus or truck stops.
peter1138
parents: 6642
diff changeset
  1177
			RoadStop::Type rstype = IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? RoadStop::BUS : RoadStop::TRUCK;
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1178
3937
60b89172e88e (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
  1179
			if (GetRoadStopType(tile) != rstype) {
6325
f03f94f4d8fc (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: 6317
diff changeset
  1180
				/* Wrong station type */
f03f94f4d8fc (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: 6317
diff changeset
  1181
				trackdirs = TRACKDIR_BIT_NONE;
3937
60b89172e88e (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
  1182
			} else {
6325
f03f94f4d8fc (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: 6317
diff changeset
  1183
				/* Proper station type, check if there is free loading bay */
7965
c1a170885a60 (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: 7946
diff changeset
  1184
				if (!_patches.roadveh_queue && IsStandardRoadStopTile(tile) &&
6316
6ba1efc2b7fc (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 6315
diff changeset
  1185
						!GetRoadStopByTile(tile, rstype)->HasFreeBay()) {
6325
f03f94f4d8fc (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: 6317
diff changeset
  1186
					/* Station is full and RV queuing is off */
f03f94f4d8fc (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: 6317
diff changeset
  1187
					trackdirs = TRACKDIR_BIT_NONE;
3092
e790a2dbda83 (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
  1188
				}
241
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1189
			}
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1190
		}
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1191
	}
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1192
	/* 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
  1193
	 * 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
  1194
	 * 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
  1195
	 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1196
6325
f03f94f4d8fc (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: 6317
diff changeset
  1197
	/* Remove tracks unreachable from the enter dir */
f03f94f4d8fc (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: 6317
diff changeset
  1198
	trackdirs &= _road_enter_dir_to_reachable_trackdirs[enterdir];
f03f94f4d8fc (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: 6317
diff changeset
  1199
	if (trackdirs == TRACKDIR_BIT_NONE) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1200
		/* No reachable tracks, so we'll reverse */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1201
		return_track(_road_reverse_table[enterdir]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1202
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1203
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1204
	if (v->u.road.reverse_ctr != 0) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1205
		/* What happens here?? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1206
		v->u.road.reverse_ctr = 0;
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
  1207
		if (v->tile != tile) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1208
			return_track(_road_reverse_table[enterdir]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1209
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1210
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1211
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1212
	desttile = v->dest_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1213
	if (desttile == 0) {
6325
f03f94f4d8fc (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: 6317
diff changeset
  1214
		/* We've got no destination, pick a random track */
f03f94f4d8fc (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: 6317
diff changeset
  1215
		return_track(PickRandomBit(trackdirs));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1216
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1217
6325
f03f94f4d8fc (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: 6317
diff changeset
  1218
	/* Only one track to choose between? */
8331
bf1b4474e288 (svn r11385) -Fix r11383: bool operation can be hard to translate for some people ;) (tnx fjb for noticing this bug ;))
truelight
parents: 8329
diff changeset
  1219
	if (KillFirstBit(trackdirs) == TRACKDIR_BIT_NONE) {
6325
f03f94f4d8fc (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: 6317
diff changeset
  1220
		return_track(FindFirstBit2x64(trackdirs));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1221
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1222
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1223
	switch (_patches.pathfinder_for_roadvehs) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1224
		case VPF_YAPF: { /* YAPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1225
			Trackdir trackdir = YapfChooseRoadTrack(v, tile, enterdir);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1226
			if (trackdir != INVALID_TRACKDIR) return_track(trackdir);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1227
			return_track(PickRandomBit(trackdirs));
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1228
		} break;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1229
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1230
		case VPF_NPF: { /* NPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1231
			NPFFindStationOrTileData fstd;
3167
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1232
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1233
			NPFFillWithOrderData(&fstd, v);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1234
			Trackdir trackdir = DiagdirToDiagTrackdir(enterdir);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1235
			//debug("Finding path. Enterdir: %d, Trackdir: %d", enterdir, trackdir);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1236
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1237
			NPFFoundTargetData ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPES);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1238
			if (ftd.best_trackdir == INVALID_TRACKDIR) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1239
				/* We are already at our target. Just do something
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1240
				 * @todo: maybe display error?
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1241
				 * @todo: go straight ahead if possible? */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1242
				return_track(FindFirstBit2x64(trackdirs));
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1243
			} else {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1244
				/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1245
				 * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1246
				 * we did not find our target, but ftd.best_trackdir contains the direction leading
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1247
				 * to the tile closest to our target. */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1248
				return_track(ftd.best_trackdir);
3167
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1249
			}
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1250
		} break;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1251
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1252
		default:
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1253
		case VPF_OPF: { /* OPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1254
			DiagDirection dir;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1255
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1256
			if (IsTileType(desttile, MP_ROAD)) {
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
  1257
				if (IsRoadDepot(desttile)) {
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1258
					dir = GetRoadDepotDirection(desttile);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1259
					goto do_it;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1260
				}
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1261
			} else if (IsTileType(desttile, MP_STATION)) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1262
				/* For drive-through stops we can head for the actual station tile */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1263
				if (IsStandardRoadStopTile(desttile)) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1264
					dir = GetRoadStopDir(desttile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1265
do_it:;
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1266
					/* When we are heading for a depot or station, we just
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1267
					 * pretend we are heading for the tile in front, we'll
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1268
					 * see from there */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1269
					desttile += TileOffsByDiagDir(dir);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1270
					if (desttile == tile && trackdirs & _road_exit_dir_to_incoming_trackdirs[dir]) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1271
						/* If we are already in front of the
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1272
						 * station/depot and we can get in from here,
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1273
						 * we enter */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1274
						return_track(FindFirstBit2x64(trackdirs & _road_exit_dir_to_incoming_trackdirs[dir]));
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1275
					}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1276
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1277
			}
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1278
			/* Do some pathfinding */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1279
			frd.dest = desttile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1280
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1281
			best_track = INVALID_TRACKDIR;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1282
			uint best_dist = UINT_MAX;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1283
			uint best_maxlen = UINT_MAX;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1284
			uint bitmask = (uint)trackdirs;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1285
			uint i;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1286
			FOR_EACH_SET_BIT(i, bitmask) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1287
				if (best_track == INVALID_TRACKDIR) best_track = (Trackdir)i; // in case we don't find the path, just pick a track
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1288
				frd.maxtracklen = UINT_MAX;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1289
				frd.mindist = UINT_MAX;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1290
				FollowTrack(tile, 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
  1291
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1292
				if (frd.mindist < best_dist || (frd.mindist == best_dist && frd.maxtracklen < best_maxlen)) {
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1293
					best_dist = frd.mindist;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1294
					best_maxlen = frd.maxtracklen;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1295
					best_track = (Trackdir)i;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1296
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1297
			}
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1298
		} break;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1299
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1300
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1301
found_best_track:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1302
9112
ec6800eaa340 (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 9107
diff changeset
  1303
	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
  1304
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1305
	return best_track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1306
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1307
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1308
static uint RoadFindPathToStop(const Vehicle *v, TileIndex tile)
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1309
{
9047
4dce11366b62 (svn r12129) -Change [FS#1759]: simplified patch settings for pathfinders (Yorick)
smatz
parents: 9006
diff changeset
  1310
	if (_patches.pathfinder_for_roadvehs == VPF_YAPF) {
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
  1311
		/* use YAPF */
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1312
		return YapfRoadVehDistanceToTile(v, tile);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1313
	}
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1314
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1315
	/* use NPF */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1316
	Trackdir trackdir = GetVehicleTrackdir(v);
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1317
	assert(trackdir != INVALID_TRACKDIR);
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1318
9050
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1319
	NPFFindStationOrTileData fstd;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1320
	fstd.dest_coords = tile;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1321
	fstd.station_index = INVALID_STATION; // indicates that the destination is a tile, not a station
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1322
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1323
	uint dist = NPFRouteToStationOrTile(v->tile, trackdir, false, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPES).best_path_dist;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1324
	/* change units from NPF_TILE_LENGTH to # of tiles */
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1325
	if (dist != UINT_MAX) dist = (dist + NPF_TILE_LENGTH - 1) / NPF_TILE_LENGTH;
c9b3ceb09413 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 9047
diff changeset
  1326
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1327
	return dist;
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1328
}
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1329
6329
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1330
enum {
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1331
	RDE_NEXT_TILE = 0x80,
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1332
	RDE_TURNED    = 0x40,
6335
0c3dc188f099 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6331
diff changeset
  1333
0c3dc188f099 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6331
diff changeset
  1334
	/* Start frames for when a vehicle enters a tile/changes its state.
0c3dc188f099 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6331
diff changeset
  1335
	 * The start frame is different for vehicles that turned around or
8394
8b0f485cebbe (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: 8365
diff changeset
  1336
	 * are leaving the depot as the do not start at the edge of the tile.
8b0f485cebbe (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: 8365
diff changeset
  1337
	 * For trams there are a few different start frames as there are two
8b0f485cebbe (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: 8365
diff changeset
  1338
	 * places where trams can turn. */
8b0f485cebbe (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: 8365
diff changeset
  1339
	RVC_DEFAULT_START_FRAME                =  0,
8b0f485cebbe (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: 8365
diff changeset
  1340
	RVC_TURN_AROUND_START_FRAME            =  1,
8b0f485cebbe (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: 8365
diff changeset
  1341
	RVC_DEPOT_START_FRAME                  =  6,
8b0f485cebbe (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: 8365
diff changeset
  1342
	RVC_START_FRAME_AFTER_LONG_TRAM        = 22,
8b0f485cebbe (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: 8365
diff changeset
  1343
	RVC_TURN_AROUND_START_FRAME_SHORT_TRAM = 16,
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1344
	/* Stop frame for a vehicle in a drive-through stop */
8394
8b0f485cebbe (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: 8365
diff changeset
  1345
	RVC_DRIVE_THROUGH_STOP_FRAME           =  7
6329
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1346
};
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1347
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1348
struct RoadDriveEntry {
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1349
	byte x, y;
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1350
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1351
9002
e7570592e22c (svn r12081) -Codechange: Rename table/roadveh.h to table/roadveh_movment.h
belugas
parents: 8987
diff changeset
  1352
#include "table/roadveh_movement.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1353
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1354
static const byte _road_veh_data_1[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1355
	20, 20, 16, 16, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1356
	19, 19, 15, 15, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1357
	16, 16, 12, 12, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1358
	15, 15, 11, 11
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1359
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1360
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1361
static bool RoadVehLeaveDepot(Vehicle *v, bool first)
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1362
{
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1363
	/* Don't leave if not all the wagons are in the depot. */
7988
6075538f6111 (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: 7986
diff changeset
  1364
	for (const Vehicle *u = v; u != NULL; u = u->Next()) {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1365
		if (u->u.road.state != RVSB_IN_DEPOT || u->tile != v->tile) return false;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1366
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1367
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1368
	DiagDirection dir = GetRoadDepotDirection(v->tile);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1369
	v->direction = DiagDirToDir(dir);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1370
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1371
	Trackdir tdir = _roadveh_depot_exit_trackdir[dir];
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1372
	const RoadDriveEntry *rdp = _road_drive_data[v->u.road.roadtype][(_opt.road_side << RVS_DRIVE_SIDE) + tdir];
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1373
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1374
	int x = TileX(v->tile) * TILE_SIZE + (rdp[RVC_DEPOT_START_FRAME].x & 0xF);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1375
	int y = TileY(v->tile) * TILE_SIZE + (rdp[RVC_DEPOT_START_FRAME].y & 0xF);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1376
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1377
	if (first) {
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1378
		if (RoadVehFindCloseTo(v, x, y, v->direction) != NULL) return true;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1379
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1380
		VehicleServiceInDepot(v);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1381
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1382
		StartRoadVehSound(v);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1383
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1384
		/* Vehicle is about to leave a depot */
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1385
		v->cur_speed = 0;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1386
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1387
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1388
	BeginVehicleMove(v);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1389
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1390
	v->vehstatus &= ~VS_HIDDEN;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1391
	v->u.road.state = tdir;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1392
	v->u.road.frame = RVC_DEPOT_START_FRAME;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1393
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7592
diff changeset
  1394
	v->cur_image = v->GetImage(v->direction);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1395
	v->UpdateDeltaXY(v->direction);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1396
	SetRoadVehPosition(v,x,y);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1397
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1398
	InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1399
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1400
	return true;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1401
}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1402
7912
fe352c500b3b (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7894
diff changeset
  1403
static Trackdir FollowPreviousRoadVehicle(const Vehicle *v, const Vehicle *prev, TileIndex tile, DiagDirection entry_dir, bool already_reversed)
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1404
{
7912
fe352c500b3b (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7894
diff changeset
  1405
	if (prev->tile == v->tile && !already_reversed) {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1406
		/* If the previous vehicle is on the same tile as this vehicle is
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1407
		 * then it must have reversed. */
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1408
		return _road_reverse_table[entry_dir];
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1409
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1410
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1411
	byte prev_state = prev->u.road.state;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1412
	Trackdir dir;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1413
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1414
	if (prev_state == RVSB_WORMHOLE || prev_state == RVSB_IN_DEPOT) {
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1415
		DiagDirection diag_dir = INVALID_DIAGDIR;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1416
8584
a8b6dffead63 (svn r11649) -Codechange: some code can be simplified thanks to changes in r11642
smatz
parents: 8579
diff changeset
  1417
		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
8579
3efbb430092e (svn r11644) -Codechange: merge some functions from tunnel_map.h and bridge_map.h into tunnelbridge_map.h
smatz
parents: 8577
diff changeset
  1418
			diag_dir = GetTunnelBridgeDirection(tile);
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
  1419
		} else if (IsRoadDepotTile(tile)) {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1420
			diag_dir = ReverseDiagDir(GetRoadDepotDirection(tile));
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1421
		}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1422
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1423
		if (diag_dir == INVALID_DIAGDIR) return INVALID_TRACKDIR;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1424
		dir = DiagdirToDiagTrackdir(diag_dir);
8757
b74c2fd3a0da (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: 8754
diff changeset
  1425
	} else {
8365
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1426
		if (already_reversed && prev->tile != tile) {
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1427
			/*
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1428
			 * The vehicle has reversed, but did not go straight back.
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1429
			 * It immediatelly turn onto another tile. This means that
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1430
			 * the roadstate of the previous vehicle cannot be used
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1431
			 * as the direction we have to go with this vehicle.
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1432
			 *
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1433
			 * Next table is build in the following way:
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1434
			 *  - first row for when the vehicle in front went to the northern or
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1435
			 *    western tile, second for southern and eastern.
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1436
			 *  - columns represent the entry direction.
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1437
			 *  - cell values are determined by the Trackdir one has to take from
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1438
			 *    the entry dir (column) to the tile in north or south by only
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1439
			 *    going over the trackdirs used for turning 90 degrees, i.e.
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1440
			 *    TRACKDIR_{UPPER,RIGHT,LOWER,LEFT}_{N,E,S,W}.
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1441
			 */
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1442
			Trackdir reversed_turn_lookup[2][DIAGDIR_END] = {
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1443
				{ TRACKDIR_UPPER_W, TRACKDIR_RIGHT_N, TRACKDIR_LEFT_N,  TRACKDIR_UPPER_E },
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1444
				{ TRACKDIR_RIGHT_S, TRACKDIR_LOWER_W, TRACKDIR_LOWER_E, TRACKDIR_LEFT_S  }};
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1445
			dir = reversed_turn_lookup[prev->tile < tile ? 0 : 1][ReverseDiagDir(entry_dir)];
8757
b74c2fd3a0da (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: 8754
diff changeset
  1446
		} else if (HasBit(prev_state, RVS_IN_DT_ROAD_STOP)) {
b74c2fd3a0da (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: 8754
diff changeset
  1447
			dir = (Trackdir)(prev_state & RVSB_ROAD_STOP_TRACKDIR_MASK);
b74c2fd3a0da (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: 8754
diff changeset
  1448
		} else if (prev_state < TRACKDIR_END) {
8365
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1449
			dir = (Trackdir)prev_state;
8757
b74c2fd3a0da (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: 8754
diff changeset
  1450
		} else {
b74c2fd3a0da (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: 8754
diff changeset
  1451
			return INVALID_TRACKDIR;
8365
542dc6f931ce (svn r11419) -Fix [FS#1388]: articulated vehicle breaking up when making a 90 degree turn immediately after a 180 degree turn.
rubidium
parents: 8331
diff changeset
  1452
		}
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1453
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1454
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1455
	/* Do some sanity checking. */
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1456
	static const RoadBits required_roadbits[] = {
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1457
		ROAD_X,            ROAD_Y,            ROAD_NW | ROAD_NE, ROAD_SW | ROAD_SE,
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1458
		ROAD_NW | ROAD_SW, ROAD_NE | ROAD_SE, ROAD_X,            ROAD_Y
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1459
	};
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1460
	RoadBits required = required_roadbits[dir & 0x07];
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1461
9212
3503ffa351b6 (svn r12390) -Fix [FS#1851]: trams failing to turn on bridge heads/tunnel entrances.
rubidium
parents: 9122
diff changeset
  1462
	if ((required & GetAnyRoadBits(tile, v->u.road.roadtype, true)) == ROAD_NONE) {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1463
		dir = INVALID_TRACKDIR;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1464
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1465
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1466
	return dir;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1467
}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1468
8394
8b0f485cebbe (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: 8365
diff changeset
  1469
/**
8b0f485cebbe (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: 8365
diff changeset
  1470
 * Can a tram track build without destruction on the given tile?
8556
50b175a55d6b (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: 8507
diff changeset
  1471
 * @param p the player that would be building the tram tracks
8394
8b0f485cebbe (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: 8365
diff changeset
  1472
 * @param t the tile to build on.
8556
50b175a55d6b (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: 8507
diff changeset
  1473
 * @param r the road bits needed.
8394
8b0f485cebbe (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: 8365
diff changeset
  1474
 * @return true when a track track can be build on 't'
8b0f485cebbe (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: 8365
diff changeset
  1475
 */
8556
50b175a55d6b (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: 8507
diff changeset
  1476
static bool CanBuildTramTrackOnTile(PlayerID p, TileIndex t, RoadBits r)
8394
8b0f485cebbe (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: 8365
diff changeset
  1477
{
8556
50b175a55d6b (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: 8507
diff changeset
  1478
	/* The 'current' player is not necessarily the owner of the vehicle. */
50b175a55d6b (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: 8507
diff changeset
  1479
	PlayerID original_player = _current_player;
50b175a55d6b (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: 8507
diff changeset
  1480
	_current_player = p;
8394
8b0f485cebbe (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: 8365
diff changeset
  1481
8556
50b175a55d6b (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: 8507
diff changeset
  1482
	CommandCost ret = DoCommand(t, ROADTYPE_TRAM << 4 | r, 0, 0, CMD_BUILD_ROAD);
8394
8b0f485cebbe (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: 8365
diff changeset
  1483
8556
50b175a55d6b (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: 8507
diff changeset
  1484
	_current_player = original_player;
50b175a55d6b (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: 8507
diff changeset
  1485
	return CmdSucceeded(ret);
8394
8b0f485cebbe (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: 8365
diff changeset
  1486
}
8b0f485cebbe (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: 8365
diff changeset
  1487
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1488
static bool IndividualRoadVehicleController(Vehicle *v, const Vehicle *prev)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1489
{
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1490
	Direction new_dir;
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1491
	Direction old_dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1492
	RoadDriveEntry rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1493
	int x,y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1494
	uint32 r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1495
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1496
	if (v->u.road.overtaking != 0)  {
8507
1d4060189029 (svn r11571) -Fix [FS#1493]: road vehicle getting to the wrong side of a station when trying to overtake in there.
rubidium
parents: 8501
diff changeset
  1497
		if (IsTileType(v->tile, MP_STATION)) {
1d4060189029 (svn r11571) -Fix [FS#1493]: road vehicle getting to the wrong side of a station when trying to overtake in there.
rubidium
parents: 8501
diff changeset
  1498
			/* Force us to be not overtaking! */
1d4060189029 (svn r11571) -Fix [FS#1493]: road vehicle getting to the wrong side of a station when trying to overtake in there.
rubidium
parents: 8501
diff changeset
  1499
			v->u.road.overtaking = 0;
1d4060189029 (svn r11571) -Fix [FS#1493]: road vehicle getting to the wrong side of a station when trying to overtake in there.
rubidium
parents: 8501
diff changeset
  1500
		} else if (++v->u.road.overtaking_ctr >= 35) {
2823
3aee7190192f (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
  1501
			/* If overtaking just aborts at a random moment, we can have a out-of-bound problem,
3aee7190192f (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
  1502
			 *  if the vehicle started a corner. To protect that, only allow an abort of
6326
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
  1503
			 *  overtake if we are on straight roads */
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
  1504
			if (v->u.road.state < RVSB_IN_ROAD_STOP && IsStraightRoadTrackdir((Trackdir)v->u.road.state)) {
2823
3aee7190192f (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
  1505
				v->u.road.overtaking = 0;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1506
			}
8507
1d4060189029 (svn r11571) -Fix [FS#1493]: road vehicle getting to the wrong side of a station when trying to overtake in there.
rubidium
parents: 8501
diff changeset
  1507
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1508
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1509
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1510
	/* If this vehicle is in a depot and we've reached this point it must be
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1511
	 * one of the articulated parts. It will stay in the depot until activated
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1512
	 * by the previous vehicle in the chain when it gets to the right place. */
7986
881998b115c2 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7972
diff changeset
  1513
	if (v->IsInDepot()) return true;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1514
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1515
	/* 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
  1516
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1517
6326
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
  1518
	if (v->u.road.state == RVSB_WORMHOLE) {
6335
0c3dc188f099 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6331
diff changeset
  1519
		/* Vehicle is entering a depot or is on a bridge or in a tunnel */
6479
b228a94a1832 (svn r8897) -Fix
tron
parents: 6467
diff changeset
  1520
		GetNewVehiclePosResult gp = GetNewVehiclePos(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1521
8762
e0d5cde5228f (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: 8760
diff changeset
  1522
		if (IsRoadVehFront(v)) {
e0d5cde5228f (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: 8760
diff changeset
  1523
			const Vehicle *u = RoadVehFindCloseTo(v, gp.x, gp.y, v->direction);
e0d5cde5228f (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: 8760
diff changeset
  1524
			if (u != NULL) {
e0d5cde5228f (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: 8760
diff changeset
  1525
				v->cur_speed = u->First()->cur_speed;
e0d5cde5228f (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: 8760
diff changeset
  1526
				return false;
e0d5cde5228f (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: 8760
diff changeset
  1527
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1528
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1529
8886
9f2c7ebc7fc9 (svn r11960) -Cleanup: simplify some IsTunnel(Tile) / IsBridge(Tile) conditions
smatz
parents: 8846
diff changeset
  1530
		if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) && HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1531
			/* Vehicle has just entered a bridge or tunnel */
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7592
diff changeset
  1532
			v->cur_image = v->GetImage(v->direction);
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
  1533
			v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1534
			SetRoadVehPosition(v,gp.x,gp.y);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1535
			return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1536
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1537
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1538
		v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1539
		v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1540
		VehiclePositionChanged(v);
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
  1541
		if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1542
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1543
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1544
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1545
	/* Get move position data for next frame.
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1546
	 * For a drive-through road stop use 'straight road' move data.
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1547
	 * In this case v->u.road.state is masked to give the road stop entry direction. */
7187
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1548
	rd = _road_drive_data[v->u.road.roadtype][(
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
  1549
		(HasBit(v->u.road.state, RVS_IN_DT_ROAD_STOP) ? v->u.road.state & RVSB_ROAD_STOP_TRACKDIR_MASK : v->u.road.state) +
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1550
		(_opt.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
  1551
6329
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1552
	if (rd.x & RDE_NEXT_TILE) {
7813
49fbe41f8a04 (svn r10672) -Codechange: typify some parameters/variables.
rubidium
parents: 7762
diff changeset
  1553
		TileIndex tile = v->tile + TileOffsByDiagDir((DiagDirection)(rd.x & 3));
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1554
		Trackdir dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1555
		uint32 r;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1556
		Direction newdir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1557
		const RoadDriveEntry *rdp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1558
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1559
		if (IsRoadVehFront(v)) {
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1560
			/* If this is the front engine, look for the right path. */
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1561
			dir = RoadFindPathToDest(v, tile, (DiagDirection)(rd.x & 3));
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1562
		} else {
7912
fe352c500b3b (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7894
diff changeset
  1563
			dir = FollowPreviousRoadVehicle(v, prev, tile, (DiagDirection)(rd.x & 3), false);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1564
		}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1565
6325
f03f94f4d8fc (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: 6317
diff changeset
  1566
		if (dir == INVALID_TRACKDIR) {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1567
			if (!IsRoadVehFront(v)) error("!Disconnecting road vehicle.");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1568
			v->cur_speed = 0;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1569
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1570
		}
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
again:
8394
8b0f485cebbe (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: 8365
diff changeset
  1573
		uint start_frame = RVC_DEFAULT_START_FRAME;
6326
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
  1574
		if (IsReversingRoadTrackdir(dir)) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1575
			/* Turning around */
7187
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1576
			if (v->u.road.roadtype == ROADTYPE_TRAM) {
8394
8b0f485cebbe (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: 8365
diff changeset
  1577
				/* Determine the road bits the tram needs to be able to turn around
8b0f485cebbe (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: 8365
diff changeset
  1578
				 * using the 'big' corner loop. */
8b0f485cebbe (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: 8365
diff changeset
  1579
				RoadBits needed;
7187
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1580
				switch (dir) {
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1581
					default: NOT_REACHED();
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1582
					case TRACKDIR_RVREV_NE: needed = ROAD_SW; break;
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1583
					case TRACKDIR_RVREV_SE: needed = ROAD_NW; break;
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1584
					case TRACKDIR_RVREV_SW: needed = ROAD_NE; break;
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1585
					case TRACKDIR_RVREV_NW: needed = ROAD_SE; break;
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1586
				}
8394
8b0f485cebbe (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: 8365
diff changeset
  1587
				if ((v->Previous() != NULL && v->Previous()->tile == tile) ||
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
  1588
						(IsRoadVehFront(v) && IsNormalRoadTile(tile) && !HasRoadWorks(tile) &&
8394
8b0f485cebbe (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: 8365
diff changeset
  1589
							(needed & GetRoadBits(tile, ROADTYPE_TRAM)) != ROAD_NONE)) {
8b0f485cebbe (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: 8365
diff changeset
  1590
					/*
8b0f485cebbe (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: 8365
diff changeset
  1591
					 * Taking the 'big' corner for trams only happens when:
8b0f485cebbe (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: 8365
diff changeset
  1592
					 * - The previous vehicle in this (articulated) tram chain is
8b0f485cebbe (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: 8365
diff changeset
  1593
					 *   already on the 'next' tile, we just follow them regardless of
8b0f485cebbe (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: 8365
diff changeset
  1594
					 *   anything. When it is NOT on the 'next' tile, the tram started
8b0f485cebbe (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: 8365
diff changeset
  1595
					 *   doing a reversing turn when the piece of tram track on the next
8b0f485cebbe (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: 8365
diff changeset
  1596
					 *   tile did not exist yet. Do not use the big tram loop as that is
8b0f485cebbe (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: 8365
diff changeset
  1597
					 *   going to cause the tram to split up.
8b0f485cebbe (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: 8365
diff changeset
  1598
					 * - Or the front of the tram can drive over the next tile.
8b0f485cebbe (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: 8365
diff changeset
  1599
					 */
8556
50b175a55d6b (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: 8507
diff changeset
  1600
				} else if (!IsRoadVehFront(v) || !CanBuildTramTrackOnTile(v->owner, tile, needed)) {
8394
8b0f485cebbe (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: 8365
diff changeset
  1601
					/*
8b0f485cebbe (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: 8365
diff changeset
  1602
					 * Taking the 'small' corner for trams only happens when:
8b0f485cebbe (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: 8365
diff changeset
  1603
					 * - We are not the from vehicle of an articulated tram.
8b0f485cebbe (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: 8365
diff changeset
  1604
					 * - Or when the player cannot build on the next tile.
8b0f485cebbe (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: 8365
diff changeset
  1605
					 *
8b0f485cebbe (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: 8365
diff changeset
  1606
					 * The 'small' corner means that the vehicle is on the end of a
8b0f485cebbe (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: 8365
diff changeset
  1607
					 * tram track and needs to start turning there. To do this properly
8b0f485cebbe (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: 8365
diff changeset
  1608
					 * the tram needs to start at an offset in the tram turning 'code'
8b0f485cebbe (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: 8365
diff changeset
  1609
					 * for 'big' corners. It furthermore does not go to the next tile,
8b0f485cebbe (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: 8365
diff changeset
  1610
					 * so that needs to be fixed too.
8b0f485cebbe (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: 8365
diff changeset
  1611
					 */
8b0f485cebbe (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: 8365
diff changeset
  1612
					tile = v->tile;
8b0f485cebbe (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: 8365
diff changeset
  1613
					start_frame = RVC_TURN_AROUND_START_FRAME_SHORT_TRAM;
8b0f485cebbe (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: 8365
diff changeset
  1614
				} else {
8b0f485cebbe (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: 8365
diff changeset
  1615
					/* The player can build on the next tile, so wait till (s)he does. */
7187
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1616
					v->cur_speed = 0;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1617
					return false;
7187
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1618
				}
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
  1619
			} else if (IsNormalRoadTile(v->tile) && GetDisallowedRoadDirections(v->tile) != DRD_NONE) {
7260
23cd58b6bf51 (svn r9999) -Feature: make it possible to disallow busses and lorries to go a specific way on straight pieces of road.
rubidium
parents: 7250
diff changeset
  1620
				v->cur_speed = 0;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1621
				return false;
7187
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1622
			} else {
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1623
				tile = v->tile;
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1624
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1625
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1626
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1627
		/* Get position data for first frame on the new tile */
7187
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1628
		rdp = _road_drive_data[v->u.road.roadtype][(dir + (_opt.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
  1629
8394
8b0f485cebbe (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: 8365
diff changeset
  1630
		x = TileX(tile) * TILE_SIZE + rdp[start_frame].x;
8b0f485cebbe (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: 8365
diff changeset
  1631
		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
  1632
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1633
		newdir = RoadVehGetSlidingDirection(v, x, y);
8420
e90a0306973f (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: 8419
diff changeset
  1634
		if (IsRoadVehFront(v)) {
e90a0306973f (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: 8419
diff changeset
  1635
			Vehicle *u = RoadVehFindCloseTo(v, x, y, newdir);
e90a0306973f (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: 8419
diff changeset
  1636
			if (u != NULL) {
e90a0306973f (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: 8419
diff changeset
  1637
				v->cur_speed = u->First()->cur_speed;
e90a0306973f (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: 8419
diff changeset
  1638
				return false;
e90a0306973f (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: 8419
diff changeset
  1639
			}
e90a0306973f (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: 8419
diff changeset
  1640
		}
0
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
		r = VehicleEnterTile(v, tile, x, y);
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
  1643
		if (HasBit(r, VETS_CANNOT_ENTER)) {
1035
0a170deb6e33 (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
  1644
			if (!IsTileType(tile, MP_TUNNELBRIDGE)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1645
				v->cur_speed = 0;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1646
				return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1647
			}
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1648
			/* Try an about turn to re-enter the previous tile */
6326
c904c7ea1c8d (svn r8715) -Codechange/cleanup: replace magic numbers related to state of road vehicles with enums. Original patch by mart3p.
rubidium
parents: 6325
diff changeset
  1649
			dir = _road_reverse_table[rd.x & 3];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1650
			goto again;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1651
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1652
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8428
diff changeset
  1653
		if (IsInsideMM(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) && IsTileType(v->tile, MP_STATION)) {
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8428
diff changeset
  1654
			if (IsReversingRoadTrackdir(dir) && IsInsideMM(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END)) {
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1655
				/* New direction is trying to turn vehicle around.
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1656
				 * We can't turn at the exit of a road stop so wait.*/
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1657
				v->cur_speed = 0;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1658
				return false;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1659
			}
3338
2c60ed0dcce3 (svn r4120) Use the new station functions where appropriate
tron
parents: 3315
diff changeset
  1660
			if (IsRoadStop(v->tile)) {
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1661
				RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1662
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1663
				/* Vehicle is leaving a road stop tile, mark bay as free
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1664
				 * For drive-through stops, only do it if the vehicle stopped here */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
  1665
				if (IsStandardRoadStopTile(v->tile) || HasBit(v->u.road.state, RVS_IS_STOPPING)) {
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
  1666
					rs->FreeBay(HasBit(v->u.road.state, RVS_USING_SECOND_BAY));
8425
72a71d480c5f (svn r11482) -Codechange: Remove the doubled function ClrBitT and rename the remaining to fit with the naming style
skidd13
parents: 8424
diff changeset
  1667
					ClrBit(v->u.road.state, RVS_IS_STOPPING);
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1668
				}
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1669
				if (IsStandardRoadStopTile(v->tile)) rs->SetEntranceBusy(false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1670
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1671
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1672
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
  1673
		if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1674
			v->tile = tile;
2823
3aee7190192f (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
  1675
			v->u.road.state = (byte)dir;
8394
8b0f485cebbe (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: 8365
diff changeset
  1676
			v->u.road.frame = start_frame;
0
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
		if (newdir != v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1679
			v->direction = newdir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1680
			v->cur_speed -= v->cur_speed >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1681
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1682
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7592
diff changeset
  1683
		v->cur_image = v->GetImage(newdir);
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
  1684
		v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1685
		RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1686
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1687
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1688
6329
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1689
	if (rd.x & RDE_TURNED) {
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1690
		/* Vehicle has finished turning around, it will now head back onto the same tile */
7912
fe352c500b3b (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7894
diff changeset
  1691
		Trackdir dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1692
		uint32 r;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1693
		Direction newdir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1694
		const RoadDriveEntry *rdp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1695
8394
8b0f485cebbe (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: 8365
diff changeset
  1696
		uint turn_around_start_frame = RVC_TURN_AROUND_START_FRAME;
8b0f485cebbe (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: 8365
diff changeset
  1697
8405
301456a669bd (svn r11461) -Fix [FS#1449] (r11449): a bus turning around in a station could case an assertion error. Patch by divide.
rubidium
parents: 8394
diff changeset
  1698
		RoadBits tram;
9212
3503ffa351b6 (svn r12390) -Fix [FS#1851]: trams failing to turn on bridge heads/tunnel entrances.
rubidium
parents: 9122
diff changeset
  1699
		if (v->u.road.roadtype == ROADTYPE_TRAM && CountBits(tram = GetAnyRoadBits(v->tile, ROADTYPE_TRAM, true)) == 1) {
8394
8b0f485cebbe (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: 8365
diff changeset
  1700
			/*
8b0f485cebbe (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: 8365
diff changeset
  1701
			 * The tram is turning around with one tram 'roadbit'. This means that
8b0f485cebbe (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: 8365
diff changeset
  1702
			 * it is using the 'big' corner 'drive data'. However, to support the
8b0f485cebbe (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: 8365
diff changeset
  1703
			 * trams to take a small corner, there is a 'turned' marker in the middle
8b0f485cebbe (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: 8365
diff changeset
  1704
			 * of the turning 'drive data'. When the tram took the long corner, we
8b0f485cebbe (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: 8365
diff changeset
  1705
			 * will still use the 'big' corner drive data, but we advance it one
8b0f485cebbe (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: 8365
diff changeset
  1706
			 * frame. We furthermore set the driving direction so the turning is
8b0f485cebbe (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: 8365
diff changeset
  1707
			 * going to be properly shown.
8b0f485cebbe (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: 8365
diff changeset
  1708
			 */
8b0f485cebbe (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: 8365
diff changeset
  1709
			turn_around_start_frame = RVC_START_FRAME_AFTER_LONG_TRAM;
8b0f485cebbe (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: 8365
diff changeset
  1710
			switch (tram) {
8b0f485cebbe (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: 8365
diff changeset
  1711
				default: NOT_REACHED();
8b0f485cebbe (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: 8365
diff changeset
  1712
				case ROAD_SW: dir = TRACKDIR_RVREV_NE; break;
8b0f485cebbe (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: 8365
diff changeset
  1713
				case ROAD_NW: dir = TRACKDIR_RVREV_SE; break;
8b0f485cebbe (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: 8365
diff changeset
  1714
				case ROAD_NE: dir = TRACKDIR_RVREV_SW; break;
8b0f485cebbe (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: 8365
diff changeset
  1715
				case ROAD_SE: dir = TRACKDIR_RVREV_NW; break;
8b0f485cebbe (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: 8365
diff changeset
  1716
			}
7912
fe352c500b3b (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7894
diff changeset
  1717
		} else {
8394
8b0f485cebbe (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: 8365
diff changeset
  1718
			if (IsRoadVehFront(v)) {
8b0f485cebbe (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: 8365
diff changeset
  1719
				/* If this is the front engine, look for the right path. */
8b0f485cebbe (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: 8365
diff changeset
  1720
				dir = RoadFindPathToDest(v, v->tile, (DiagDirection)(rd.x & 3));
8b0f485cebbe (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: 8365
diff changeset
  1721
			} else {
8b0f485cebbe (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: 8365
diff changeset
  1722
				dir = FollowPreviousRoadVehicle(v, prev, v->tile, (DiagDirection)(rd.x & 3), true);
8b0f485cebbe (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: 8365
diff changeset
  1723
			}
7912
fe352c500b3b (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7894
diff changeset
  1724
		}
fe352c500b3b (svn r10809) -Fix (r10097): When reversing, articulated parts of road vehicles should not attempt to do their own pathfinding.
maedhros
parents: 7894
diff changeset
  1725
6325
f03f94f4d8fc (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: 6317
diff changeset
  1726
		if (dir == INVALID_TRACKDIR) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1727
			v->cur_speed = 0;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1728
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1729
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1730
7187
f7dc3204d457 (svn r9923) -Add: support for Action 0 Road vehicles, property 1C, bit 0.
rubidium
parents: 7183
diff changeset
  1731
		rdp = _road_drive_data[v->u.road.roadtype][(_opt.road_side << RVS_DRIVE_SIDE) + dir];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1732
8394
8b0f485cebbe (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: 8365
diff changeset
  1733
		x = TileX(v->tile) * TILE_SIZE + rdp[turn_around_start_frame].x;
8b0f485cebbe (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: 8365
diff changeset
  1734
		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
  1735
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1736
		newdir = RoadVehGetSlidingDirection(v, x, y);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1737
		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
  1738
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1739
		r = VehicleEnterTile(v, v->tile, x, y);
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
  1740
		if (HasBit(r, VETS_CANNOT_ENTER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1741
			v->cur_speed = 0;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1742
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1743
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1744
6335
0c3dc188f099 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6331
diff changeset
  1745
		v->u.road.state = dir;
8394
8b0f485cebbe (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: 8365
diff changeset
  1746
		v->u.road.frame = turn_around_start_frame;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1747
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1748
		if (newdir != v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1749
			v->direction = newdir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1750
			v->cur_speed -= v->cur_speed >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1751
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1752
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7592
diff changeset
  1753
		v->cur_image = v->GetImage(newdir);
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
  1754
		v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1755
		RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1756
		return true;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1757
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1758
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1759
	/* This vehicle is not in a wormhole and it hasn't entered a new tile. If
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1760
	 * it's on a depot tile, check if it's time to activate the next vehicle in
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1761
	 * the chain yet. */
9059
04edde3eb0c6 (svn r12141) -Codechange: Introduce IsNormalRoad[Tile](), IsRoadDepot[Tile]() and HasTileRoadType(); and use them.
frosch
parents: 9052
diff changeset
  1762
	if (v->Next() != NULL && IsRoadDepotTile(v->tile)) {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1763
		if (v->u.road.frame == v->u.road.cached_veh_length + RVC_DEPOT_START_FRAME) {
7988
6075538f6111 (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: 7986
diff changeset
  1764
			RoadVehLeaveDepot(v->Next(), false);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1765
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1766
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1767
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1768
	/* Calculate new position for the vehicle */
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1769
	x = (v->x_pos & ~15) + (rd.x & 15);
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1770
	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
  1771
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1772
	new_dir = RoadVehGetSlidingDirection(v, x, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1773
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8428
diff changeset
  1774
	if (IsRoadVehFront(v) && !IsInsideMM(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END)) {
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1775
		/* Vehicle is not in a road stop.
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1776
		 * Check for another vehicle to overtake */
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1777
		Vehicle* u = RoadVehFindCloseTo(v, x, y, new_dir);
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1778
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1779
		if (u != NULL) {
8313
d92b90b5e9ed (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: 8312
diff changeset
  1780
			u = u->First();
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1781
			/* There is a vehicle in front overtake it if possible */
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1782
			if (v->u.road.overtaking == 0) RoadVehCheckOvertake(v, u);
8165
606d97bce909 (svn r11200) -Fix [FS#1291]: road vehicles could not overtake on one way roads going to the east.
rubidium
parents: 8138
diff changeset
  1783
			if (v->u.road.overtaking == 0) v->cur_speed = u->cur_speed;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1784
			return false;
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1785
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1786
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1787
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1788
	old_dir = v->direction;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1789
	if (new_dir != old_dir) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1790
		v->direction = new_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1791
		v->cur_speed -= (v->cur_speed >> 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1792
		if (old_dir != v->u.road.state) {
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1793
			/* The vehicle is in a road stop */
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7592
diff changeset
  1794
			v->cur_image = v->GetImage(new_dir);
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
  1795
			v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1796
			SetRoadVehPosition(v, v->x_pos, v->y_pos);
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1797
			/* Note, return here means that the frame counter is not incremented
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1798
			 * for vehicles changing direction in a road stop. This causes frames to
6335
0c3dc188f099 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6331
diff changeset
  1799
			 * be repeated. (XXX) Is this intended? */
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1800
			return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1801
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1802
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1803
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1804
	/* If the vehicle is in a normal road stop and the frame equals the stop frame OR
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1805
	 * if the vehicle is in a drive-through road stop and this is the destination station
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1806
	 * and it's the correct type of stop (bus or truck) and the frame equals the stop frame...
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1807
	 * (the station test and stop type test ensure that other vehicles, using the road stop as
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1808
	 * a through route, do not stop) */
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8428
diff changeset
  1809
	if (IsRoadVehFront(v) && ((IsInsideMM(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_ROAD_STOP_END) &&
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1810
			_road_veh_data_1[v->u.road.state - RVSB_IN_ROAD_STOP + (_opt.road_side << RVS_DRIVE_SIDE)] == v->u.road.frame) ||
8450
dce58137301f (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 8428
diff changeset
  1811
			(IsInsideMM(v->u.road.state, RVSB_IN_DT_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) &&
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1812
			v->current_order.dest == GetStationIndex(v->tile) &&
6655
951b0ba32eb1 (svn r9301) -Codechange: Use cargo class to determine if a road vehicle can stop in bus or truck stops.
peter1138
parents: 6642
diff changeset
  1813
			GetRoadStopType(v->tile) == (IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? RoadStop::BUS : RoadStop::TRUCK) &&
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1814
			v->u.road.frame == RVC_DRIVE_THROUGH_STOP_FRAME))) {
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1815
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1816
		RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3236
diff changeset
  1817
		Station* st = GetStationByTile(v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1818
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1819
		/* Vehicle is at the stop position (at a bay) in a road stop.
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1820
		 * Note, if vehicle is loading/unloading it has already been handled,
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1821
		 * so if we get here the vehicle has just arrived or is just ready to leave. */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1822
		if (v->current_order.type != OT_LEAVESTATION &&
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1823
				v->current_order.type != OT_GOTO_DEPOT) {
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1824
			/* 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
  1825
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1826
			if (IsDriveThroughStopTile(v->tile)) {
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1827
				TileIndex next_tile = TILE_ADD(v->tile, TileOffsByDir(v->direction));
6655
951b0ba32eb1 (svn r9301) -Codechange: Use cargo class to determine if a road vehicle can stop in bus or truck stops.
peter1138
parents: 6642
diff changeset
  1828
				RoadStop::Type type = IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? RoadStop::BUS : RoadStop::TRUCK;
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1829
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1830
				/* Check if next inline bay is free */
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1831
				if (IsDriveThroughStopTile(next_tile) && (GetRoadStopType(next_tile) == type)) {
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1832
					RoadStop *rs_n = GetRoadStopByTile(next_tile, type);
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1833
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
  1834
					if (rs_n->IsFreeBay(HasBit(v->u.road.state, RVS_USING_SECOND_BAY))) {
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1835
						/* Bay in next stop along is free - use it */
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1836
						ClearSlot(v);
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1837
						rs_n->num_vehicles++;
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1838
						v->u.road.slot = rs_n;
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1839
						v->dest_tile = rs_n->xy;
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1840
						v->u.road.slot_age = 14;
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1841
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1842
						v->u.road.frame++;
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1843
						RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1844
						return true;
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1845
					}
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1846
				}
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1847
			}
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1848
6316
6ba1efc2b7fc (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 6315
diff changeset
  1849
			rs->SetEntranceBusy(false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1850
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3236
diff changeset
  1851
			v->last_station_visited = GetStationIndex(v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1852
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1853
			RoadVehArrivesAt(v, st);
5902
906fea1535ab (svn r8110) -Codechange: direct Vehicle::current_order.type changes (to OT_LOADING and OT_LEAVESTATION) replaced by v->BeginLoading() and v->LeaveStation() calls. This should allow easy hooking of those state transitions in order to maintain vehicle loading queue.
KUDr
parents: 5838
diff changeset
  1854
			v->BeginLoading();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1855
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1856
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1857
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1858
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1859
		/* Vehicle is ready to leave a bay in a road stop */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1860
		if (v->current_order.type != OT_GOTO_DEPOT) {
6316
6ba1efc2b7fc (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 6315
diff changeset
  1861
			if (rs->IsEntranceBusy()) {
6335
0c3dc188f099 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6331
diff changeset
  1862
				/* 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
  1863
				v->cur_speed = 0;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1864
				return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1865
			}
6589
addf4b6946d0 (svn r9072) -Codechange: [Orders] added methods to orders to free them and check if they are in use
bjarni
parents: 6585
diff changeset
  1866
			v->current_order.Free();
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1867
			ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1868
		}
6335
0c3dc188f099 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6331
diff changeset
  1869
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1870
		if (IsStandardRoadStopTile(v->tile)) rs->SetEntranceBusy(true);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1871
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1872
		if (rs == v->u.road.slot) {
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1873
			/* We are leaving the correct station */
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1874
			ClearSlot(v);
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1875
		} else if (v->u.road.slot != NULL) {
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
  1876
			/* We are leaving the wrong station
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
  1877
			 * XXX The question is .. what to do? Actually we shouldn't be here
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
  1878
			 * but I guess we need to clear the slot */
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1879
			DEBUG(ms, 0, "Vehicle %d (index %d) arrived at wrong stop", v->unitnumber, v->index);
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1880
			if (v->tile != v->dest_tile) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1881
				DEBUG(ms, 2, " current tile 0x%X is not destination tile 0x%X. Route problem", v->tile, v->dest_tile);
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1882
			}
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1883
			if (v->dest_tile != v->u.road.slot->xy) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1884
				DEBUG(ms, 2, " stop tile 0x%X is not destination tile 0x%X. Multistop desync", v->u.road.slot->xy, v->dest_tile);
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1885
			}
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1886
			if (v->current_order.type != OT_GOTO_STATION) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1887
				DEBUG(ms, 2, " current order type (%d) is not OT_GOTO_STATION", v->current_order.type);
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1888
			} else {
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  1889
				if (v->current_order.dest != st->index)
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1890
					DEBUG(ms, 2, " current station %d is not target station in current_order.station (%d)",
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  1891
							st->index, v->current_order.dest);
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1892
			}
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1893
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1894
			DEBUG(ms, 2, " force a slot clearing");
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1895
			ClearSlot(v);
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1896
		}
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1897
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1898
		StartRoadVehSound(v);
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8813
diff changeset
  1899
		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
  1900
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1901
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1902
	/* Check tile position conditions - i.e. stop position in depot,
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1903
	 * entry onto bridge or into tunnel */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1904
	r = VehicleEnterTile(v, v->tile, x, y);
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
  1905
	if (HasBit(r, VETS_CANNOT_ENTER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1906
		v->cur_speed = 0;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1907
		return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1908
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1909
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1910
	/* Move to next frame unless vehicle arrived at a stop position
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1911
	 * in a depot or entered a tunnel/bridge */
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
  1912
	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
  1913
7630
2cd754d7dfa4 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7592
diff changeset
  1914
	v->cur_image = v->GetImage(v->direction);
7054
edbb4d7765f2 (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 7049
diff changeset
  1915
	v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1916
	RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1917
	return true;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1918
}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1919
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1920
static void RoadVehController(Vehicle *v)
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1921
{
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1922
	/* decrease counters */
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1923
	v->tick_counter++;
7476
7f086e4b2a76 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 7449
diff changeset
  1924
	v->current_order_time++;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1925
	if (v->u.road.reverse_ctr != 0) v->u.road.reverse_ctr--;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1926
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1927
	/* handle crashed */
7972
dff055fff851 (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: 7965
diff changeset
  1928
	if (v->vehstatus & VS_CRASHED) {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1929
		RoadVehIsCrashed(v);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1930
		return;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1931
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1932
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1933
	RoadVehCheckTrainCrash(v);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1934
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1935
	/* road vehicle has broken down? */
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1936
	if (v->breakdown_ctr != 0) {
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1937
		if (v->breakdown_ctr <= 2) {
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1938
			HandleBrokenRoadVeh(v);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1939
			return;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1940
		}
10351
4f1563682453 (svn r12891) [0.6] -Fix (r12868): breakdowns were broken and would cause vehicles to go slower than they should have been moving.
rubidium
parents: 10330
diff changeset
  1941
		if (v->current_order.type != OT_LOADING) v->breakdown_ctr--;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1942
	}
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1943
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1944
	if (v->vehstatus & VS_STOPPED) return;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1945
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1946
	ProcessRoadVehOrder(v);
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1947
	v->HandleLoading();
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1948
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1949
	if (v->current_order.type == OT_LOADING) return;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1950
7986
881998b115c2 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7972
diff changeset
  1951
	if (v->IsInDepot() && RoadVehLeaveDepot(v, true)) return;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1952
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1953
	/* Check if vehicle needs to proceed, return if it doesn't */
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1954
	if (!RoadVehAccelerate(v)) return;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1955
7988
6075538f6111 (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: 7986
diff changeset
  1956
	for (Vehicle *prev = NULL; v != NULL; prev = v, v = v->Next()) {
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1957
		if (!IndividualRoadVehicleController(v, prev)) break;
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1958
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1959
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1960
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1961
static void AgeRoadVehCargo(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1962
{
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1963
	if (_age_cargo_skip_counter != 0) return;
7506
e52d89f5c7c1 (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: 7476
diff changeset
  1964
	v->cargo.AgeCargo();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1965
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1966
7631
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  1967
void RoadVehicle::Tick()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1968
{
7631
e810ef25497e (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7630
diff changeset
  1969
	AgeRoadVehCargo(this);
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  1970
9052
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  1971
	if (IsRoadVehFront(this)) {
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  1972
		if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  1973
		RoadVehController(this);
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  1974
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1975
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1976
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1977
static void CheckIfRoadVehNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1978
{
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
  1979
	/* If we already got a slot at a stop, use that FIRST, and go to a depot later */
7998
f1a308b61b8f (svn r11017) -Codechange: unify determining whether a vehicle needs/can be service a little more.
rubidium
parents: 7993
diff changeset
  1980
	if (v->u.road.slot != NULL || _patches.servint_roadveh == 0 || !VehicleNeedsService(v)) return;
7986
881998b115c2 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7972
diff changeset
  1981
	if (v->IsInDepot()) {
4529
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  1982
		VehicleServiceInDepot(v);
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  1983
		return;
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  1984
	}
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  1985
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
  1986
	/* XXX If we already have a depot order, WHY do we search over and over? */
7998
f1a308b61b8f (svn r11017) -Codechange: unify determining whether a vehicle needs/can be service a little more.
rubidium
parents: 7993
diff changeset
  1987
	const Depot *depot = FindClosestRoadDepot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1988
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1989
	if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) {
1520
c85dc79795e0 (svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents: 1467
diff changeset
  1990
		if (v->current_order.type == OT_GOTO_DEPOT) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1991
			v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1992
			v->current_order.flags = 0;
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8813
diff changeset
  1993
			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
  1994
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1995
		return;
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
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1998
	if (v->current_order.type == OT_GOTO_DEPOT &&
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
  1999
			v->current_order.flags & OFB_NON_STOP &&
8463
3920ac0ab803 (svn r11523) -Codechange: Move the CHANCE macros to core/random_func.cpp cause they depend on Random()
skidd13
parents: 8450
diff changeset
  2000
			!Chance16(1, 20)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2001
		return;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  2002
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2003
7109
7d410365d6fc (svn r9833) -Fix: also 'leave' the station when leaving for automatic servicing.
rubidium
parents: 7104
diff changeset
  2004
	if (v->current_order.type == OT_LOADING) v->LeaveStation();
7d410365d6fc (svn r9833) -Fix: also 'leave' the station when leaving for automatic servicing.
rubidium
parents: 7104
diff changeset
  2005
	ClearSlot(v);
7d410365d6fc (svn r9833) -Fix: also 'leave' the station when leaving for automatic servicing.
rubidium
parents: 7104
diff changeset
  2006
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2007
	v->current_order.type = OT_GOTO_DEPOT;
8798
eea3a465bd86 (svn r11866) -Codechange: swap OFB_* and OF_* so it conceptually in sync with other cases of *B_* vs *_* like VETSB_* and VETS_*.
rubidium
parents: 8766
diff changeset
  2008
	v->current_order.flags = OFB_NON_STOP;
4527
b18634a31a4a (svn r6353) -Codechange: Make DestinationID a typedef of uin16, which is as large as any type of destinataion (StationID, DepotID, WaypointID) it can hold
tron
parents: 4526
diff changeset
  2009
	v->current_order.dest = depot->index;
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  2010
	v->dest_tile = depot->xy;
8846
f3e877a92e2b (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8813
diff changeset
  2011
	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
  2012
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2013
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2014
void RoadVehicle::OnNewDay()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2015
{
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2016
	if (!IsRoadVehFront(this)) return;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  2017
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2018
	if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this);
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2019
	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
  2020
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2021
	AgeVehicle(this);
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2022
	CheckIfRoadVehNeedsService(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2023
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2024
	CheckOrders(this);
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 11
diff changeset
  2025
6889
f7f6d9cb07a0 (svn r9523) -Cleanup: doxygen changes. Time to take care of "R"
belugas
parents: 6683
diff changeset
  2026
	/* Current slot has expired */
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2027
	if (this->current_order.type == OT_GOTO_STATION && this->u.road.slot != NULL && this->u.road.slot_age-- == 0) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2028
		DEBUG(ms, 3, "Slot expired for vehicle %d (index %d) at stop 0x%X",
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2029
			this->unitnumber, this->index, this->u.road.slot->xy);
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2030
		ClearSlot(this);
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2031
	}
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2032
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2033
	/* update destination */
9052
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  2034
	if (!(this->vehstatus & VS_STOPPED) && this->current_order.type == OT_GOTO_STATION && this->u.road.slot == NULL && !(this->vehstatus & VS_CRASHED)) {
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2035
		Station *st = GetStation(this->current_order.dest);
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2036
		RoadStop *rs = st->GetPrimaryRoadStop(this);
7965
c1a170885a60 (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: 7946
diff changeset
  2037
		RoadStop *best = NULL;
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  2038
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2039
		if (rs != NULL) {
6161
754debc55207 (svn r8536) -Fix (FS#577): Road Vehicles now can obtain a slot even if the station is very spread out
celestar
parents: 6127
diff changeset
  2040
			/* We try to obtain a slot if:
754debc55207 (svn r8536) -Fix (FS#577): Road Vehicles now can obtain a slot even if the station is very spread out
celestar
parents: 6127
diff changeset
  2041
			 * 1) we're reasonably close to the primary road stop
754debc55207 (svn r8536) -Fix (FS#577): Road Vehicles now can obtain a slot even if the station is very spread out
celestar
parents: 6127
diff changeset
  2042
			 * or
754debc55207 (svn r8536) -Fix (FS#577): Road Vehicles now can obtain a slot even if the station is very spread out
celestar
parents: 6127
diff changeset
  2043
			 * 2) we're somewhere close to the station rectangle (to make sure we do assign
754debc55207 (svn r8536) -Fix (FS#577): Road Vehicles now can obtain a slot even if the station is very spread out
celestar
parents: 6127
diff changeset
  2044
			 *    slots even if the station and its road stops are incredibly spread out)
754debc55207 (svn r8536) -Fix (FS#577): Road Vehicles now can obtain a slot even if the station is very spread out
celestar
parents: 6127
diff changeset
  2045
			 */
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2046
			if (DistanceManhattan(this->tile, rs->xy) < 16 || st->rect.PtInExtendedRect(TileX(this->tile), TileY(this->tile), 2)) {
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2047
				uint dist, badness;
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2048
				uint minbadness = UINT_MAX;
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  2049
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2050
				DEBUG(ms, 2, "Attempting to obtain a slot for vehicle %d (index %d) at station %d (0x%X)",
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2051
					this->unitnumber, this->index, st->index, st->xy
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3990
diff changeset
  2052
				);
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2053
				/* Now we find the nearest road stop that has a free slot */
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2054
				for (; rs != NULL; rs = rs->GetNextRoadStop(this)) {
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2055
					dist = RoadFindPathToStop(this, rs->xy);
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2056
					if (dist == UINT_MAX) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2057
						DEBUG(ms, 4, " stop 0x%X is unreachable, not treating further", rs->xy);
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2058
						continue;
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2059
					}
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  2060
					badness = (rs->num_vehicles + 1) * (rs->num_vehicles + 1) + dist;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2061
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2062
					DEBUG(ms, 4, " stop 0x%X has %d vehicle%s waiting", rs->xy, rs->num_vehicles, rs->num_vehicles == 1 ? "":"s");
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2063
					DEBUG(ms, 4, " distance is %u", dist);
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2064
					DEBUG(ms, 4, " badness %u", badness);
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2065
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2066
					if (badness < minbadness) {
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2067
						best = rs;
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2068
						minbadness = badness;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2069
					}
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  2070
				}
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  2071
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2072
				if (best != NULL) {
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2073
					best->num_vehicles++;
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2074
					DEBUG(ms, 3, "Assigned to stop 0x%X", best->xy);
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  2075
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2076
					this->u.road.slot = best;
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2077
					this->dest_tile = best->xy;
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2078
					this->u.road.slot_age = 14;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2079
				} else {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2080
					DEBUG(ms, 3, "Could not find a suitable stop");
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2081
				}
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2082
			} else {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2083
				DEBUG(ms, 5, "Distance from station too far. Postponing slotting for vehicle %d (index %d) at station %d, (0x%X)",
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2084
						this->unitnumber, this->index, st->index, st->xy);
1295
5dc810674581 (svn r1799) -Codechange: [ Multistop ] Added debug class "ms" and more debugging
celestar
parents: 1282
diff changeset
  2085
			}
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2086
		} else {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2087
			DEBUG(ms, 4, "No road stop for vehicle %d (index %d) at station %d (0x%X)",
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2088
					this->unitnumber, this->index, st->index, st->xy);
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  2089
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2090
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2091
9052
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  2092
	if (this->running_ticks == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2093
9122
bc3651767850 (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: 9112
diff changeset
  2094
	const RoadVehicleInfo *rvi = RoadVehInfo(this->engine_type);
bc3651767850 (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: 9112
diff changeset
  2095
	CommandCost cost(EXPENSES_ROADVEH_RUN, rvi->running_cost * GetPriceByIndex(rvi->running_cost_class) * this->running_ticks / (364 * DAY_TICKS));
9052
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  2096
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  2097
	this->profit_this_year -= cost.GetCost();
392f5995d323 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 9050
diff changeset
  2098
	this->running_ticks = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2099
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2100
	SubtractMoneyFromPlayerFract(this->owner, cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2101
8963
4b41ed1df7e5 (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8920
diff changeset
  2102
	InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
1151
614cbcb5b9fe (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1128
diff changeset
  2103
	InvalidateWindowClasses(WC_ROADVEH_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2104
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2105
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2106
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6502
diff changeset
  2107
void RoadVehiclesYearlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2108
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2109
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2110
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2111
	FOR_ALL_VEHICLES(v) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  2112
		if (v->type == VEH_ROAD) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2113
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2114
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2115
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2116
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2117
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2118
}
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2119
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2120
/** Refit a road vehicle to the specified cargo type
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2121
 * @param tile unused
6979
d194d25020e7 (svn r9664) -Documentation: Doxygen corrections,errors and omissions
belugas
parents: 6949
diff changeset
  2122
 * @param flags operation to perform
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2123
 * @param p1 Vehicle ID of the vehicle to refit
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2124
 * @param p2 Bitstuffed elements
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2125
 * - p2 = (bit 0-7) - the new cargo type to refit to
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2126
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
7914
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2127
 * - p2 = (bit 16) - refit only this vehicle
7042
d841fb0d8e9a (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 7040
diff changeset
  2128
 * @return cost of refit or error
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2129
 */
7439
0c0e2945c890 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 7375
diff changeset
  2130
CommandCost CmdRefitRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2131
{
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2132
	Vehicle *v;
8726
5ae45b46506b (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8722
diff changeset
  2133
	CommandCost cost(EXPENSES_ROADVEH_RUN);
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2134
	CargoID new_cid = GB(p2, 0, 8);
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2135
	byte new_subtype = GB(p2, 8, 8);
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
  2136
	bool only_this = HasBit(p2, 16);
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2137
	uint16 capacity = CALLBACK_FAILED;
7914
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2138
	uint total_capacity = 0;
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2139
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  2140
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2141
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2142
	v = GetVehicle(p1);
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2143
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
  2144
	if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
7353
716c6dd9322a (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 7286
diff changeset
  2145
	if (!CheckRoadVehInDepotStopped(v)) return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);
8641
966d79f41865 (svn r11707) -Fix: do not allow refitting flooded (destroyed) vehicles
smatz
parents: 8640
diff changeset
  2146
	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE);
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2147
7914
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2148
	if (new_cid >= NUM_CARGO) return CMD_ERROR;
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2149
7988
6075538f6111 (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: 7986
diff changeset
  2150
	for (; v != NULL; v = v->Next()) {
7914
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2151
		/* XXX: We refit all the attached wagons en-masse if they can be
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2152
		 * refitted. This is how TTDPatch does it.  TODO: Have some nice
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2153
		 * [Refit] button near each wagon. */
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2154
		if (!CanRefitTo(v->engine_type, new_cid)) continue;
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2155
7914
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2156
		if (v->cargo_cap == 0) continue;
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2157
8424
4a488a90ccab (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 8420
diff changeset
  2158
		if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_REFIT_CAPACITY)) {
7914
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2159
			/* Back up the cargo type */
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2160
			CargoID temp_cid = v->cargo_type;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2161
			byte temp_subtype = v->cargo_subtype;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2162
			v->cargo_type = new_cid;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2163
			v->cargo_subtype = new_subtype;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2164
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2165
			/* Check the refit capacity callback */
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2166
			capacity = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2167
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2168
			/* Restore the original cargo type */
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2169
			v->cargo_type = temp_cid;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2170
			v->cargo_subtype = temp_subtype;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2171
		}
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2172
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2173
		if (capacity == CALLBACK_FAILED) {
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2174
			/* callback failed or not used, use default capacity */
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2175
			const RoadVehicleInfo *rvi = RoadVehInfo(v->engine_type);
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2176
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2177
			CargoID old_cid = rvi->cargo_type;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2178
			/* normally, the capacity depends on the cargo type, a vehicle can
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2179
			 * carry twice as much mail/goods as normal cargo, and four times as
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2180
			 * many passengers
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2181
			 */
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2182
			capacity = GetVehicleProperty(v, 0x0F, rvi->capacity);
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2183
			switch (old_cid) {
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2184
				case CT_PASSENGERS: break;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2185
				case CT_MAIL:
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2186
				case CT_GOODS: capacity *= 2; break;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2187
				default:       capacity *= 4; break;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2188
			}
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2189
			switch (new_cid) {
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2190
				case CT_PASSENGERS: break;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2191
				case CT_MAIL:
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2192
				case CT_GOODS: capacity /= 2; break;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2193
				default:       capacity /= 4; break;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2194
			}
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2195
		}
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2196
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2197
		if (capacity == 0) continue;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2198
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2199
		total_capacity += capacity;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2200
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2201
		if (IsHumanPlayer(v->owner) && new_cid != v->cargo_type) {
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2202
			cost.AddCost(GetRefitCost(v->engine_type));
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2203
		}
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2204
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2205
		if (flags & DC_EXEC) {
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2206
			v->cargo_cap = capacity;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2207
			v->cargo.Truncate((v->cargo_type == new_cid) ? capacity : 0);
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2208
			v->cargo_type = new_cid;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2209
			v->cargo_subtype = new_subtype;
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2210
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2211
			InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2212
			RebuildVehicleLists();
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2213
		}
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2214
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2215
		if (only_this) break;
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2216
	}
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2217
7914
946aa6131995 (svn r10811) -Fix (r10097): Refit all the parts of an articulated road vehicle, not just the first part.
maedhros
parents: 7912
diff changeset
  2218
	_returned_refit_capacity = total_capacity;
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2219
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2220
	return cost;
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2221
}