src/roadveh_cmd.cpp
author rubidium
Wed, 09 Jan 2008 18:11:12 +0000
branchnoai
changeset 9723 eee46cb39750
parent 9722 ebf0ece7d8f6
child 9724 b39bc69bb2f2
permissions -rw-r--r--
(svn r11796) [NoAI] -Sync: with trunk r11502:11795.
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
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
     3
/** @file roadveh_cmd.cpp */
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
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"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
     8
#include "tile_cmd.h"
9599
949374e83b78 (svn r9632) [NoAI] -Sync with trunk r9574:9631.
rubidium
parents: 9574
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"
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 493
diff changeset
    13
#include "table/strings.h"
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
    14
#include "timetable.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include "engine.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    16
#include "command_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#include "station.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
#include "news.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
#include "pathfind.h"
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    20
#include "npf.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
#include "player.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"
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
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"
9476
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6589
diff changeset
    33
#include "cargotype.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    34
#include "strings_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    35
#include "tunnelbridge_map.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    36
#include "functions.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    37
#include "window_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    38
#include "date_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    39
#include "vehicle_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    40
#include "sound_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    41
#include "variables.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    42
#include "autoreplace_gui.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    43
#include "gfx_func.h"
9682
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9641
diff changeset
    44
#include "ai/ai.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
    46
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
static const uint16 _roadveh_images[63] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
	0xCD4, 0xCDC, 0xCE4, 0xCEC, 0xCF4, 0xCFC, 0xD0C, 0xD14,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
	0xD24, 0xD1C, 0xD2C, 0xD04, 0xD1C, 0xD24, 0xD6C, 0xD74,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
	0xD7C, 0xC14, 0xC1C, 0xC24, 0xC2C, 0xC34, 0xC3C, 0xC4C,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
	0xC54, 0xC64, 0xC5C, 0xC6C, 0xC44, 0xC5C, 0xC64, 0xCAC,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
	0xCB4, 0xCBC, 0xD94, 0xD9C, 0xDA4, 0xDAC, 0xDB4, 0xDBC,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
	0xDCC, 0xDD4, 0xDE4, 0xDDC, 0xDEC, 0xDC4, 0xDDC, 0xDE4,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
	0xE2C, 0xE34, 0xE3C, 0xC14, 0xC1C, 0xC2C, 0xC3C, 0xC4C,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
	0xC5C, 0xC64, 0xC6C, 0xC74, 0xC84, 0xC94, 0xCA4
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
};
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
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
    59
	 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
    60
	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
    61
	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
    62
	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
    63
	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
    64
	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
    65
	 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
    66
	 0,   0,   0,   8,   8,   8,   8
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
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
    69
/** '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
    70
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
    71
	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
    72
	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
    73
	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
    74
	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
    75
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
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
    77
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
    78
	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
    79
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
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
    81
/** '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
    82
 * 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
    83
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
    84
	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
    85
	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
    86
	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
    87
	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
    88
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
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
    90
/** 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
    91
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
    92
	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
    93
};
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
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
    95
int RoadVehicle::GetImage(Direction direction) const
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
{
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
    97
	int img = this->spritenum;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    98
	int image;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
	if (is_custom_sprite(img)) {
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   101
		image = GetCustomVehicleSprite(this, (Direction)(direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img)));
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   102
		if (image != 0) return image;
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
   103
		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
   104
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   105
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
	image = direction + _roadveh_images[img];
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   107
	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
   108
	return image;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5902
diff changeset
   111
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
   112
{
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   113
	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
   114
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
   115
	if (is_custom_sprite(spritenum)) {
3186
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3184
diff changeset
   116
		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
   117
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
   118
		if (sprite != 0) {
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5902
diff changeset
   119
			DrawSprite(sprite, pal, x, y);
378
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
   120
			return;
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
   121
		}
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
   122
		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
   123
	}
5919
2b58160d667d (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5902
diff changeset
   124
	DrawSprite(6 + _roadveh_images[spritenum], pal, x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   127
static CommandCost EstimateRoadVehCost(EngineID engine_type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
{
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
   129
	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
   130
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   132
byte GetRoadVehLength(const Vehicle *v)
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   133
{
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   134
	byte length = 8;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   135
9641
855e32c08c9b (svn r10533) [NoAI] -Sync with trunk r10460:r10532.
truelight
parents: 9631
diff changeset
   136
	uint16 veh_len = GetVehicleCallback(CBID_VEHICLE_LENGTH, 0, 0, v->engine_type, v);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   137
	if (veh_len != CALLBACK_FAILED) {
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   138
		length -= Clamp(veh_len, 0, 7);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   139
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   140
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   141
	return length;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   142
}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   143
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   144
void RoadVehUpdateCache(Vehicle *v)
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   145
{
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   146
	assert(v->type == VEH_ROAD);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   147
	assert(IsRoadVehFront(v));
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   148
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   149
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   150
		/* Check the v->first cache. */
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   151
		assert(u->First() == v);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   152
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   153
		/* Update the 'first engine' */
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   154
		u->u.road.first_engine = (v == u) ? INVALID_ENGINE : v->engine_type;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   155
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   156
		/* Update the length of the vehicle. */
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   157
		u->u.road.cached_veh_length = GetRoadVehLength(u);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   158
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   159
}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   160
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   161
/** 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
   162
 * @param tile tile of depot where road vehicle is built
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 9599
diff changeset
   163
 * @param flags operation to perform
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   164
 * @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
   165
 * @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
   166
 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   167
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
   168
{
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   169
	CommandCost cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
	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
   171
	UnitID unit_num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   172
	Engine *e;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   173
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
   174
	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
   175
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
	cost = EstimateRoadVehCost(p1);
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   177
	if (flags & DC_QUERY_COST) return cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
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
   179
	/* 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
   180
	 * 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
   181
	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
   182
	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
   183
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   184
	if (HasBit(GetRoadTypes(tile), ROADTYPE_TRAM) != HasBit(EngInfo(p1)->misc_flags, EF_ROAD_TRAM)) return_cmd_error(STR_DEPOT_WRONG_DEPOT_TYPE);
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   185
9703
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   186
	uint num_vehicles = 1 + CountArticulatedParts(p1, false);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   187
9703
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   188
	/* Allow for the front and the articulated parts, plus one to "terminate" the list. */
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   189
	Vehicle **vl = (Vehicle**)alloca(sizeof(*vl) * (num_vehicles + 1));
d2a6acdbd665 (svn r11146) [NoAI] -Sync: with trunk r11035:11045.
rubidium
parents: 9701
diff changeset
   190
	memset(vl, 0, sizeof(*vl) * (num_vehicles + 1));
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   191
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9686
diff changeset
   192
	if (!Vehicle::AllocateList(vl, num_vehicles)) {
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   193
		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   194
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   195
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   196
	v = vl[0];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
	/* find the first free roadveh id */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   199
	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
   200
	if (unit_num > _patches.max_roadveh)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
		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
   202
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
	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
   204
		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
   205
		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
   206
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   207
		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
   208
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   209
		v = new (v) RoadVehicle();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
		v->unitnumber = unit_num;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   211
		v->direction = DiagDirToDir(GetRoadDepotDirection(tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   212
		v->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
		v->tile = tile;
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3630
diff changeset
   215
		x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3630
diff changeset
   216
		y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   217
		v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
		v->y_pos = y;
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 9599
diff changeset
   219
		v->z_pos = GetSlopeZ(x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
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
   221
		v->u.road.state = RVSB_IN_DEPOT;
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 9599
diff changeset
   222
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   223
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   224
		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
   225
		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
   226
		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
   227
		v->cargo_cap = rvi->capacity;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   228
//		v->cargo_count = 0;
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   229
		v->value = cost.GetCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   230
//		v->day_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
//		v->next_order_param = v->next_order = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   232
//		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
//		v->progress = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
//	v->u.road.unk2 = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
//	v->u.road.overtaking = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
1266
9dc5638fe8cc (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
   238
		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
   239
		v->max_speed = rvi->max_speed;
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
   240
		v->engine_type = (EngineID)p1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
1926
68d60188a22f (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1901
diff changeset
   242
		e = GetEngine(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
		v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   244
		v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
		v->max_age = e->lifelength * 366;
2564
371e0fa97e1e (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2552
diff changeset
   246
		_new_vehicle_id = v->index;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   247
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
		v->string_id = STR_SV_ROADVEH_NAME;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
		v->service_interval = _patches.servint_roadveh;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   251
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
		v->date_of_last_service = _date;
4329
9759d5c52010 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   253
		v->build_year = _cur_year;
0
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->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
   256
		v->random_bits = VehicleRandomBits();
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   257
		SetRoadVehFront(v);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   258
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   259
		v->u.road.roadtype = HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   260
		v->u.road.compatible_roadtypes = RoadTypeToRoadTypes(v->u.road.roadtype);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   261
		v->u.road.cached_veh_length = GetRoadVehLength(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
6502
7ca154aca2b7 (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6501
diff changeset
   263
		v->vehicle_flags = 0;
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   264
		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
   265
9624
b71483f2330f (svn r9915) [NoAI] -Sync with trunk -r9815:9914
glx
parents: 9620
diff changeset
   266
		v->cargo_cap = GetVehicleProperty(v, 0x0F, rvi->capacity);
b71483f2330f (svn r9915) [NoAI] -Sync with trunk -r9815:9914
glx
parents: 9620
diff changeset
   267
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   268
		AddArticulatedParts(vl, VEH_ROAD);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   269
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
		VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4725
diff changeset
   272
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   273
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
		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
   275
		if (IsLocalPlayer())
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   276
			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
   277
b90cf92697b9 (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 6194
diff changeset
   278
		GetPlayer(_current_player)->num_engines[p1]++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   279
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   280
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
   281
	return cost;
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
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   284
/** 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
   285
 * @param tile unused
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 9599
diff changeset
   286
 * @param flags operation to perform
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   287
 * @param p1 road vehicle ID to start/stop
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   288
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   289
 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   290
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
   291
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   292
	Vehicle *v;
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
   293
	uint16 callback;
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
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 904
diff changeset
   297
	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. */
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
   303
	callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
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
   304
	if (callback != CALLBACK_FAILED && callback != 0xFF) {
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) {
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
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;
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   315
		v->cur_speed = 0;
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 679
diff changeset
   316
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
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
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
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
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   337
static bool CheckRoadVehInDepotStopped(const Vehicle *v)
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   338
{
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   339
	TileIndex tile = v->tile;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   340
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   341
	if (!IsTileDepotType(tile, TRANSPORT_ROAD)) return false;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   342
	if (IsRoadVehFront(v) && !(v->vehstatus & VS_STOPPED)) return false;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   343
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   344
	for (; v != NULL; v = v->Next()) {
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   345
		if (v->u.road.state != RVSB_IN_DEPOT || v->tile != tile) return false;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   346
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   347
	return true;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   348
}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
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
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 9599
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
 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
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
9718
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9704
diff changeset
   366
	if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
f82a4facea8b (svn r11309) [NoAI] -Sync: with trunk r11145:11308.
truelight
parents: 9704
diff changeset
   367
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
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
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
   372
	CommandCost ret(EXPENSES_NEW_VEHICLES, -v->value);
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
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);
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9686
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
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
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
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   398
static bool EnumRoadSignalFindDepot(TileIndex tile, void* data, Trackdir trackdir, uint length, byte* state)
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
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9686
diff changeset
   404
	if (IsTileType(tile, MP_ROAD) &&
3793
33cdb5bf7b21 (svn r4789) - Codechange: rename RoadType to RoadTileType and ROAD_{NORMAL,CROSSING,DEPOT} to ROAD_TILE_* for consistency
rubidium
parents: 3645
diff changeset
   405
			GetRoadTileType(tile) == ROAD_TILE_DEPOT &&
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   406
			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
   407
			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
   408
		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
   409
		rfdd->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   414
static const Depot* FindClosestRoadDepot(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
{
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
   416
	TileIndex tile = v->tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   417
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   418
	if (_patches.yapf.road_use_yapf) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   419
		Depot* ret = YapfFindNearestRoadDepot(v);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   420
		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
   421
	} else if (_patches.new_pathfinding_all) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   422
		NPFFoundTargetData ftd;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   423
		/* See where we are now */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   424
		Trackdir trackdir = GetVehicleTrackdir(v);
1745
2dcc0a66d91d (svn r2249) - Fix: [ 1187703 ] [NPF] Road vehicles cannot find a depot when in a roadstop
matthijs
parents: 1709
diff changeset
   425
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   426
		ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, v->tile, ReverseTrackdir(trackdir), TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPE, 0);
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
   427
		if (ftd.best_bird_dist == 0) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   428
			return GetDepotByTile(ftd.node.tile); /* Target found */
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
   429
		} else {
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
   430
			return NULL; /* Target not found */
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
   431
		}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   432
		/* We do not search in two directions here, why should we? We can't reverse right now can we? */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   433
	} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   434
		RoadFindDepotData rfdd;
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   435
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   436
		rfdd.owner = v->owner;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   437
		rfdd.best_length = (uint)-1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   439
		/* search in all directions */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   440
		for (DiagDirection i = DIAGDIR_BEGIN; i != DIAGDIR_END; i++) {
9624
b71483f2330f (svn r9915) [NoAI] -Sync with trunk -r9815:9914
glx
parents: 9620
diff changeset
   441
			FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, v->u.road.compatible_roadtypes, i, EnumRoadSignalFindDepot, NULL, &rfdd);
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   442
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   443
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
   444
		if (rfdd.best_length == (uint)-1) return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   446
		return GetDepotByTile(rfdd.tile);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   447
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   448
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   449
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   450
/** 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
   451
 * @param tile unused
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 9599
diff changeset
   452
 * @param flags operation to perform
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   453
 * @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
   454
 * @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
   455
 * - 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
   456
 * - 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
   457
 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   458
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
   459
{
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
   460
	Vehicle *v;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   461
	const Depot *dep;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   463
	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
   464
		/* 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
   465
		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
   466
		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
   467
	}
3a70624c40eb (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   468
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   469
	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
   470
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   471
	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
   472
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
   473
	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
   474
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   475
	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
   476
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   477
	if (v->IsInDepot()) return CMD_ERROR;
4526
77ef74d28e76 (svn r6352) -Fix: FS#322 Send to depot bug
bjarni
parents: 4519
diff changeset
   478
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   479
	/* If the current orders are already goto-depot */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   480
	if (v->current_order.type == OT_GOTO_DEPOT) {
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   481
		if (!!(p2 & DEPOT_SERVICE) == HasBit(v->current_order.flags, OFB_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
   482
			/* 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
   483
			 * 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
   484
			 * 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
   485
			if (flags & DC_EXEC) {
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   486
				ClrBit(v->current_order.flags, OFB_PART_OF_ORDERS);
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   487
				ToggleBit(v->current_order.flags, OFB_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
   488
				InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
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
			}
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   490
			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
   491
		}
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
   492
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   493
		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
   494
		if (flags & DC_EXEC) {
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   495
			/* 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
   496
			 * then skip to the next order; effectively cancelling this forced service */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   497
			if (HasBit(v->current_order.flags, OFB_PART_OF_ORDERS))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
				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
   499
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   500
			v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   501
			v->current_order.flags = 0;
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 679
diff changeset
   502
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
		}
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   504
		return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   505
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   507
	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
   508
	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
   509
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   510
	if (flags & DC_EXEC) {
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 9599
diff changeset
   511
		if (v->current_order.type == OT_LOADING) v->LeaveStation();
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 9599
diff changeset
   512
3236
36e8eb3ae55a (svn r3909) [multistop]
celestar
parents: 3186
diff changeset
   513
		ClearSlot(v);
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   514
		v->current_order.type = OT_GOTO_DEPOT;
4412
945e8a9bb89a (svn r6165) -Feature: control click Goto Depot will now make the vehicle service
bjarni
parents: 4389
diff changeset
   515
		v->current_order.flags = OF_NON_STOP;
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   516
		if (!(p2 & DEPOT_SERVICE)) SetBit(v->current_order.flags, OFB_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
   517
		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
   518
		v->current_order.dest = dep->index;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   519
		v->dest_tile = dep->xy;
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 679
diff changeset
   520
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   522
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   523
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   525
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   526
/** 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
   527
 * @param tile unused
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 9599
diff changeset
   528
 * @param flags operation to perform
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   529
 * @param p1 vehicle ID to turn
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   530
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   531
 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   532
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
   533
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   534
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   536
	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
   537
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 904
diff changeset
   538
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
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
   540
	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
   541
5573
afa6f92a71fd (svn r7573) -Merged the bridge branch. Allows to build bridges of arbitrary rail/road combinations (including signals)
celestar
parents: 5568
diff changeset
   542
	if (v->vehstatus & VS_STOPPED ||
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   543
			v->vehstatus & VS_CRASHED ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
			v->breakdown_ctr != 0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
			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
   546
			v->u.road.state == RVSB_WORMHOLE ||
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   547
			v->IsInDepot() ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
			v->cur_speed < 5) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   549
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   551
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9686
diff changeset
   552
	if (IsTileType(v->tile, MP_ROAD) && GetRoadTileType(v->tile) == ROAD_TILE_NORMAL && GetDisallowedRoadDirections(v->tile) != DRD_NONE) return CMD_ERROR;
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   553
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
   554
	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
   555
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   556
	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
   557
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   558
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   559
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
9620
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
   562
void RoadVehicle::MarkDirty()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
{
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   564
	for (Vehicle *v = this; v != NULL; v = v->Next()) {
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   565
		v->cur_image = v->GetImage(v->direction);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   566
		MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   567
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   569
9620
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
   570
void RoadVehicle::UpdateDeltaXY(Direction direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
{
9620
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
   572
#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
   573
	static const uint32 _delta_xy_table[8] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
	};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
#undef MKIT
9620
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
   584
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
   585
	uint32 x = _delta_xy_table[direction];
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
   586
	this->x_offs        = GB(x,  0, 8);
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
   587
	this->y_offs        = GB(x,  8, 8);
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
   588
	this->sprite_width  = GB(x, 16, 8);
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
   589
	this->sprite_height = GB(x, 24, 8);
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
   590
	this->z_height      = 6;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
static void ClearCrashedStation(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   594
{
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
   595
	RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
6316
6ba1efc2b7fc (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 6315
diff changeset
   597
	/* Mark the station entrance as not busy */
6ba1efc2b7fc (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 6315
diff changeset
   598
	rs->SetEntranceBusy(false);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   599
6316
6ba1efc2b7fc (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 6315
diff changeset
   600
	/* Free the parking bay */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   601
	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
   602
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   604
static void DeleteLastRoadVeh(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   605
{
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   606
	Vehicle *u = v;
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   607
	for (; v->Next() != NULL; v = v->Next()) u = v;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   608
	u->SetNext(NULL);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   609
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
	DeleteWindowById(WC_VEHICLE_VIEW, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   611
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   612
	RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
	InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
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
   615
	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
   616
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
	EndVehicleMove(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   619
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9686
diff changeset
   620
	delete v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
static byte SetRoadVehPosition(Vehicle *v, int x, int y)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
	byte new_z, old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
   627
	/* 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
   628
	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
   629
	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
   630
	new_z = GetSlopeZ(x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
	old_z = v->z_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
	v->z_pos = new_z;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   634
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
	return old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   640
static void RoadVehSetRandomDirection(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
{
3160
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   642
	static const DirDiff delta[] = {
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   643
		DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   644
	};
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   645
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   646
	do {
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   647
		uint32 r = Random();
3160
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   648
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   649
		v->direction = ChangeDir(v->direction, delta[r & 3]);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   650
		BeginVehicleMove(v);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   651
		v->UpdateDeltaXY(v->direction);
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
   652
		v->cur_image = v->GetImage(v->direction);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   653
		SetRoadVehPosition(v, v->x_pos, v->y_pos);
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   654
	} while ((v = v->Next()) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   657
static void RoadVehIsCrashed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   658
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
	v->u.road.crashed_ctr++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   660
	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
   661
		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   662
	} 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
   663
		if ((v->tick_counter & 7) == 0) RoadVehSetRandomDirection(v);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   664
	} else if (v->u.road.crashed_ctr >= 2220 && !(v->tick_counter & 0x1F)) {
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   665
		DeleteLastRoadVeh(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   666
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   667
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   668
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   669
static void* EnumCheckRoadVehCrashTrain(Vehicle* v, void* data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   670
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   671
	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
   672
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   673
	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
   674
		v->type == VEH_TRAIN &&
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   675
		abs(v->z_pos - u->z_pos) <= 6 &&
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   676
		abs(v->x_pos - u->x_pos) <= 4 &&
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   677
		abs(v->y_pos - u->y_pos) <= 4 ?
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   678
			v : NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   679
}
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
static void RoadVehCrash(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   682
{
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   683
	uint16 pass = 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   684
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   685
	v->u.road.crashed_ctr++;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   686
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   687
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   688
		if (IsCargoInClass(u->cargo_type, CC_PASSENGERS)) pass += u->cargo.Count();
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   689
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   690
		u->vehstatus |= VS_CRASHED;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   691
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   692
		MarkAllViewportsDirty(u->left_coord, u->top_coord, u->right_coord + 1, u->bottom_coord + 1);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   693
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   694
3236
36e8eb3ae55a (svn r3909) [multistop]
celestar
parents: 3186
diff changeset
   695
	ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   696
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 679
diff changeset
   697
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   698
9682
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9641
diff changeset
   699
	AI_Event(v->owner, new AIEventVehicleCrash(v->index, v->tile));
d031eb183733 (svn r10631) [NoAI] -Add: AIEvent, to take care of events; for now it only reports when vehicles are crashed
truelight
parents: 9641
diff changeset
   700
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   701
	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
   702
	AddNewsItem(
3e97367f7cbc (svn r2780) Remove some more unused strings and make the use of a few strings more explicit
tron
parents: 2214
diff changeset
   703
		(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
   704
			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
   705
		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
   706
		v->index,
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   707
		0
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   708
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   709
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   710
	ModifyStationRatingAround(v->tile, v->owner, -160, 22);
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 538
diff changeset
   711
	SndPlayVehicleFx(SND_12_EXPLOSION, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   713
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   714
static void RoadVehCheckTrainCrash(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
{
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   716
	for (Vehicle *u = v; u != NULL; u = u->Next()) {
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   717
		if (u->u.road.state == RVSB_WORMHOLE) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   719
		TileIndex tile = u->tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   720
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   721
		if (!IsLevelCrossingTile(tile)) continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   722
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   723
		if (VehicleFromPosXY(v->x_pos, v->y_pos, u, EnumCheckRoadVehCrashTrain) != NULL) {
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   724
			RoadVehCrash(v);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   725
			return;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   726
		}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
   727
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   728
}
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
static void HandleBrokenRoadVeh(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   732
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   737
			v->breakdowns_since_last_service++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   738
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   740
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   741
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   742
		if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
9517
be1775555bbd (svn r9431) [NoAI] -Sync with trunk r9394:9430
truelight
parents: 9476
diff changeset
   743
			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
   744
				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
   745
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   747
		if (!(v->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   748
			Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9686
diff changeset
   749
			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
   750
		}
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
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   753
	if ((v->tick_counter & 1) == 0) {
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   754
		if (--v->breakdown_delay == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   755
			v->breakdown_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   756
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   757
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   758
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   759
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   760
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   761
static void ProcessRoadVehOrder(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   762
{
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   763
	const Order *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   764
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   765
	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
   766
		case OT_GOTO_DEPOT:
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
   767
			/* Let a depot order in the orderlist interrupt. */
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   768
			if (!(v->current_order.flags & OF_PART_OF_ORDERS)) return;
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   769
			if (v->current_order.flags & OF_SERVICE_IF_NEEDED &&
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   770
					!VehicleNeedsService(v)) {
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
   771
				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
   772
				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
   773
			}
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   774
			break;
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   775
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   776
		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
   777
		case OT_LEAVESTATION:
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   778
			return;
4351
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4344
diff changeset
   779
63ae31104f07 (svn r6052) -Codechange: change OrderType (order->type) in a typedef
truelight
parents: 4344
diff changeset
   780
		default: break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   781
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   782
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   783
	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
   784
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   785
	order = GetVehicleOrder(v, v->cur_order_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   786
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   787
	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
   788
		v->current_order.Free();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   789
		v->dest_tile = 0;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   790
		ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   791
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   793
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
   794
	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
   795
			order->flags == v->current_order.flags &&
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
   796
			order->dest  == v->current_order.dest) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   797
		return;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   798
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   799
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   800
	v->current_order = *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   801
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   802
	switch (order->type) {
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   803
		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
   804
			if (order->dest == v->last_station_visited) {
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   805
				v->last_station_visited = INVALID_STATION;
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   806
			}
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   807
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   808
			const RoadStop *rs = GetStation(order->dest)->GetPrimaryRoadStop(v);
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   809
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   810
			TileIndex dest = INVALID_TILE;
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   811
			if (rs != NULL) {
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   812
				uint mindist = MAX_UVALUE(uint);
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   813
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   814
				for (; rs != NULL; rs = rs->GetNextRoadStop(v)) {
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   815
					uint dist = DistanceManhattan(v->tile, rs->xy);
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   816
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   817
					if (dist < mindist) {
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   818
						mindist = dist;
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   819
						dest = rs->xy;
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   820
					}
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   821
				}
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   822
			}
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   823
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   824
			if (dest != INVALID_TILE) {
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   825
					v->dest_tile = dest;
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   826
			} else {
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
   827
				/* 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
   828
				v->cur_order_index++;
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   829
				v->dest_tile = 0;
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   830
			}
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   831
			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
   832
		}
1231
64cc1f5c1c82 (svn r1735) -Fix: [ 1112285 ] Fixed an assertion error with stations where stops
celestar
parents: 1224
diff changeset
   833
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   834
		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
   835
			v->dest_tile = GetDepot(order->dest)->xy;
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   836
			break;
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
   837
4179
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   838
		default:
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   839
			v->dest_tile = 0;
9e09dfdc1316 (svn r5617) if cascade -> switch
tron
parents: 4170
diff changeset
   840
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   841
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   842
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   843
	InvalidateVehicleOrder(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   844
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   845
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
   846
static void StartRoadVehSound(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   847
{
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   848
	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
   849
		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
   850
		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
   851
			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
   852
		SndPlayVehicleFx(s, v);
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   853
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   854
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   855
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   856
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
   857
	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
   858
	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
   859
	const Vehicle* veh;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   860
	Direction dir;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   861
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   862
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   863
static void* EnumCheckRoadVehClose(Vehicle *v, void* data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   864
{
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   865
	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
   866
	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
   867
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   868
	const RoadVehFindData* rvf = (RoadVehFindData*)data;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   869
63
53cc59601b3c (svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents: 19
diff changeset
   870
	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
   871
	short y_diff = v->y_pos - rvf->y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   872
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   873
	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
   874
		v->type == VEH_ROAD &&
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   875
		!v->IsInDepot() &&
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   876
		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
   877
		v->direction == rvf->dir &&
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
   878
		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
   879
		(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
   880
		(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
   881
		(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
   882
		(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
   883
			v : NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   884
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   885
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   886
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
   887
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   888
	RoadVehFindData rvf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
	Vehicle *u;
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   890
	Vehicle *front = v->First();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   891
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   892
	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
   893
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   894
	rvf.x = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   895
	rvf.y = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   896
	rvf.dir = dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   897
	rvf.veh = v;
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   898
	if (front->u.road.state == RVSB_WORMHOLE) {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   899
		u = (Vehicle*)VehicleFromPos(v->tile, &rvf, EnumCheckRoadVehClose);
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   900
	} else {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   901
		u = (Vehicle*)VehicleFromPosXY(x, y, &rvf, EnumCheckRoadVehClose);
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   902
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   903
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
   904
	/* This code protects a roadvehicle from being blocked for ever
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
   905
	 * If more than 1480 / 74 days a road vehicle is blocked, it will
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
   906
	 * drive just through it. The ultimate backup-code of TTD.
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
   907
	 * It can be disabled. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
	if (u == NULL) {
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   909
		front->u.road.blocked_ctr = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   910
		return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   911
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   912
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
   913
	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
   914
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   915
	return u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   916
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   917
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   918
static void RoadVehArrivesAt(const Vehicle* v, Station* st)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   919
{
9476
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6589
diff changeset
   920
	if (IsCargoInClass(v->cargo_type, CC_PASSENGERS)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   921
		/* Check if station was ever visited before */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   922
		if (!(st->had_vehicle_of_type & HVOT_BUS)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
			uint32 flags;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   925
			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
   926
			SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   927
			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
   928
			AddNewsItem(
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   929
				v->u.road.roadtype == ROADTYPE_ROAD ? STR_902F_CITIZENS_CELEBRATE_FIRST : STR_902F_CITIZENS_CELEBRATE_FIRST_TRAM,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   930
				flags,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   931
				v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   932
				0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
		/* Check if station was ever visited before */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
		if (!(st->had_vehicle_of_type & HVOT_TRUCK)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
			uint32 flags;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   938
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
			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
   940
			SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
			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
   942
			AddNewsItem(
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
   943
				v->u.road.roadtype == ROADTYPE_ROAD ? STR_9030_CITIZENS_CELEBRATE_FIRST : STR_9030_CITIZENS_CELEBRATE_FIRST_TRAM,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
				flags,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
				v->index,
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   946
				0
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   947
			);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   952
static bool RoadVehAccelerate(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
{
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   954
	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
   955
	byte t;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   956
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
   957
	/* Clamp */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
	spd = min(spd, v->max_speed);
6467
6228b576422d (svn r8884) -Fix
tron
parents: 6457
diff changeset
   959
	if (v->u.road.state == RVSB_WORMHOLE && !(v->vehstatus & VS_HIDDEN)) {
6228b576422d (svn r8884) -Fix
tron
parents: 6457
diff changeset
   960
		spd = min(spd, GetBridge(GetBridgeType(v->tile))->speed * 2);
6228b576422d (svn r8884) -Fix
tron
parents: 6457
diff changeset
   961
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   962
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
   963
	/* 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
   964
	if (spd != v->cur_speed) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   965
		v->cur_speed = spd;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   966
		if (_patches.vehicle_speed) {
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 679
diff changeset
   967
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   968
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   970
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
   971
	/* Decrease somewhat when turning */
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   972
	if (!(v->direction & 1)) spd = spd * 3 >> 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   974
	if (spd == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   975
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   976
	if ((byte)++spd == 0) return true;
0
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
	v->progress = (t = v->progress) - (byte)spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
	return (t < v->progress);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   983
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
   984
{
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   985
	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
   986
		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
   987
		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
   988
		DIR_E , DIR_SE, DIR_S
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
	};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   990
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
	x = x - v->x_pos + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   992
	y = y - v->y_pos + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
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
   994
	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
   995
	return _roadveh_new_dir[y * 4 + x];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   996
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   997
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   998
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
   999
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1000
	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
  1001
	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
  1002
	DirDiff delta;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1003
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1004
	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
  1005
	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
  1006
	return ChangeDir(old_dir, delta);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1007
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1008
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1009
struct OvertakeData {
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1010
	const Vehicle* u;
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1011
	const Vehicle* v;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
  1012
	TileIndex tile;
9704
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1013
	uint16 tilebits;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1014
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1015
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1016
static void* EnumFindVehToOvertake(Vehicle* v, void* data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1017
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1018
	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
  1019
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
  1020
	return
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1021
		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
  1022
			v : NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1023
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1024
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1025
static bool FindRoadVehToOvertake(OvertakeData *od)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1026
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1027
	uint32 bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1028
9704
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1029
	bits = GetTileTrackStatus(od->tile, TRANSPORT_ROAD, od->v->u.road.compatible_roadtypes);
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1030
	bits |= bits >> 8;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1031
9704
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1032
	if (!(od->tilebits & bits) || (bits & 0x3C3C) || (bits & 0x3F3F0000))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1033
		return true;
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1034
	return VehicleFromPos(od->tile, od, EnumFindVehToOvertake) != NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1035
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1036
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1037
static void RoadVehCheckOvertake(Vehicle *v, Vehicle *u)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1038
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1039
	OvertakeData od;
9704
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1040
	uint16 tt;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1041
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1042
	od.v = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1043
	od.u = u;
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
	if (u->max_speed >= v->max_speed &&
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1046
			!(u->vehstatus & VS_STOPPED) &&
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1047
			u->cur_speed != 0) {
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1048
		return;
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1049
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1050
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1051
	/* Trams can't overtake other trams */
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1052
	if (v->u.road.roadtype == ROADTYPE_TRAM) return;
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1053
9704
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1054
	/* Don't overtake in stations */
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1055
	if (IsTileType(v->tile, MP_STATION)) return;
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1056
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1057
	/* For now, articulated road vehicles can't overtake anything. */
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1058
	if (RoadVehHasArticPart(v)) return;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1059
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1060
	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
  1061
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
  1062
	/* 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
  1063
	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
  1064
9704
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1065
	tt = GetTileTrackStatus(v->tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes);
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1066
	tt |= tt >> 8;
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1067
	tt &= 0x3F;
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1068
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1069
	if ((tt & 3) == 0) return;
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1070
	if ((tt & 0x3C) != 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1071
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1072
	if (tt == 3) tt = (v->direction & 2) ? 2 : 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
	od.tilebits = tt;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1074
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1075
	od.tile = v->tile;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1076
	if (FindRoadVehToOvertake(&od)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1077
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4549
diff changeset
  1078
	od.tile = v->tile + TileOffsByDiagDir(DirToDiagDir(v->direction));
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1079
	if (FindRoadVehToOvertake(&od)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1080
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1081
	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
  1082
		v->u.road.overtaking_ctr = 0x11;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1083
		v->u.road.overtaking = 0x10;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1084
	} else {
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1085
//		if (FindRoadVehToOvertake(&od)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1086
		v->u.road.overtaking_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1087
		v->u.road.overtaking = 0x10;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1088
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1089
}
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
static void RoadZPosAffectSpeed(Vehicle *v, byte old_z)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1092
{
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
  1093
	if (old_z == v->z_pos) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1094
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1095
	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
  1096
		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
  1097
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1098
		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
  1099
		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
  1100
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1101
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1102
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1103
static int PickRandomBit(uint bits)
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
	uint i;
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1106
	uint num = RandomRange(CountBits(bits));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1107
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
  1108
	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
  1109
	return i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1110
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1111
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1112
struct FindRoadToChooseData {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1113
	TileIndex dest;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1114
	uint maxtracklen;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1115
	uint mindist;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1116
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1117
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1118
static bool EnumRoadTrackFindDist(TileIndex tile, void* data, Trackdir trackdir, uint length, byte* state)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
{
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1120
	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
  1121
	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
  1122
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
	if (dist <= frd->mindist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1124
		if (dist != frd->mindist || length < frd->maxtracklen) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1125
			frd->maxtracklen = length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1126
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1127
		frd->mindist = dist;
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
	return false;
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
9624
b71483f2330f (svn r9915) [NoAI] -Sync with trunk -r9815:9914
glx
parents: 9620
diff changeset
  1132
static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, uint sub_type, Owner owner, RailTypeMask 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
  1133
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1134
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1135
	void* perf = NpfBeginInterval();
9624
b71483f2330f (svn r9915) [NoAI] -Sync with trunk -r9815:9914
glx
parents: 9620
diff changeset
  1136
	NPFFoundTargetData ret = NPFRouteToStationOrTile(tile, trackdir, 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
  1137
	int t = NpfEndInterval(perf);
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1138
	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
  1139
	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
  1140
}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1141
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
  1142
/**
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
  1143
 * 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
  1144
 * INVALID_TRACKDIR if the direction is currently blocked
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 9599
diff changeset
  1145
 * @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
  1146
 * @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
  1147
 * @param enterdir the direction the vehicle enters the tile from
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 9599
diff changeset
  1148
 * @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
  1149
 */
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
  1150
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
  1151
{
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
  1152
#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
  1153
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
  1154
	TileIndex desttile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1155
	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
  1156
	Trackdir best_track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1157
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1158
	uint32 r = GetTileTrackStatus(tile, TRANSPORT_ROAD, v->u.road.compatible_roadtypes);
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
  1159
	TrackdirBits signal    = (TrackdirBits)GB(r, 16, 16);
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
  1160
	TrackdirBits trackdirs = (TrackdirBits)GB(r,  0, 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1161
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9686
diff changeset
  1162
	if (IsTileType(tile, MP_ROAD)) {
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1163
		if (GetRoadTileType(tile) == ROAD_TILE_DEPOT && (!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
  1164
			/* 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
  1165
			trackdirs = TRACKDIR_BIT_NONE;
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2493
diff changeset
  1166
		}
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1167
	} 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
  1168
		/* Standard road stop (drive-through stops are treated as normal road) */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1169
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1170
		if (!IsTileOwner(tile, v->owner) || GetRoadStopDir(tile) == enterdir || RoadVehHasArticPart(v)) {
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1171
			/* 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
  1172
			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
  1173
		} else {
241
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1174
			/* Our station */
9476
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6589
diff changeset
  1175
			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
  1176
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
  1177
			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
  1178
				/* 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
  1179
				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
  1180
			} 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
  1181
				/* Proper station type, check if there is free loading bay */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1182
				if (!_patches.roadveh_queue && IsStandardRoadStopTile(tile) &&
6316
6ba1efc2b7fc (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 6315
diff changeset
  1183
						!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
  1184
					/* 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
  1185
					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
  1186
				}
241
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1187
			}
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1188
		}
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
	/* 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
  1191
	 * 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
  1192
	 * 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
  1193
	 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1194
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
  1195
	/* 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
  1196
	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
  1197
	if (trackdirs == TRACKDIR_BIT_NONE) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1198
		/* No reachable tracks, so we'll reverse */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1199
		return_track(_road_reverse_table[enterdir]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1200
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1201
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1202
	if (v->u.road.reverse_ctr != 0) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1203
		/* What happens here?? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1204
		v->u.road.reverse_ctr = 0;
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
  1205
		if (v->tile != tile) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1206
			return_track(_road_reverse_table[enterdir]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1207
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1208
	}
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
	desttile = v->dest_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1211
	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
  1212
		/* 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
  1213
		return_track(PickRandomBit(trackdirs));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1214
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1215
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
  1216
	/* Only one track to choose between? */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1217
	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
  1218
		return_track(FindFirstBit2x64(trackdirs));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1219
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1220
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1221
	if (_patches.yapf.road_use_yapf) {
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1222
		Trackdir trackdir = YapfChooseRoadTrack(v, tile, enterdir);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1223
		if (trackdir != INVALID_TRACKDIR) return_track(trackdir);
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
  1224
		return_track(PickRandomBit(trackdirs));
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1225
	} else if (_patches.new_pathfinding_all) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1226
		NPFFindStationOrTileData fstd;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1227
		NPFFoundTargetData ftd;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1228
		Trackdir trackdir;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1229
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1230
		NPFFillWithOrderData(&fstd, v);
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1231
		trackdir = DiagdirToDiagTrackdir(enterdir);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1232
		//debug("Finding path. Enterdir: %d, Trackdir: %d", enterdir, trackdir);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1233
9624
b71483f2330f (svn r9915) [NoAI] -Sync with trunk -r9815:9914
glx
parents: 9620
diff changeset
  1234
		ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPE);
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
  1235
		if (ftd.best_trackdir == INVALID_TRACKDIR) {
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
  1236
			/* We are already at our target. Just do something
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
  1237
			 * @todo: maybe display error?
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
  1238
			 * @todo: go straight ahead if possible? */
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
  1239
			return_track(FindFirstBit2x64(trackdirs));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1240
		} else {
1698
bf364c664a60 (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1680
diff changeset
  1241
			/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
bf364c664a60 (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1680
diff changeset
  1242
			the direction we need to take to get there, if ftd.best_bird_dist is not 0,
bf364c664a60 (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1680
diff changeset
  1243
			we did not find our target, but ftd.best_trackdir contains the direction leading
bf364c664a60 (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1680
diff changeset
  1244
			to the tile closest to our target. */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1245
			return_track(ftd.best_trackdir);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1246
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1247
	} else {
3167
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1248
		DiagDirection dir;
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1249
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9686
diff changeset
  1250
		if (IsTileType(desttile, MP_ROAD)) {
3793
33cdb5bf7b21 (svn r4789) - Codechange: rename RoadType to RoadTileType and ROAD_{NORMAL,CROSSING,DEPOT} to ROAD_TILE_* for consistency
rubidium
parents: 3645
diff changeset
  1251
			if (GetRoadTileType(desttile) == ROAD_TILE_DEPOT) {
3167
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1252
				dir = GetRoadDepotDirection(desttile);
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1253
				goto do_it;
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1254
			}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1255
		} else if (IsTileType(desttile, MP_STATION)) {
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1256
			/* For drive-through stops we can head for the actual station tile */
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1257
			if (IsStandardRoadStopTile(desttile)) {
3404
3ac4f7fedfb5 (svn r4215) -Codechange: Renamed *RoadStation* functions to *RoadStop* and moved them to station_map.h to keep consistency
celestar
parents: 3338
diff changeset
  1258
				dir = GetRoadStopDir(desttile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1259
do_it:;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1260
				/* When we are heading for a depot or station, we just
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1261
				 * pretend we are heading for the tile in front, we'll
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1262
				 * see from there */
4559
c853d2440065 (svn r6406) -Codechange: Rename TileOffsByDir to TileOffsByDiagDir because it accepts
Darkvater
parents: 4549
diff changeset
  1263
				desttile += TileOffsByDiagDir(dir);
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
  1264
				if (desttile == tile && trackdirs & _road_exit_dir_to_incoming_trackdirs[dir]) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1265
					/* If we are already in front of the
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1266
					 * station/depot and we can get in from here,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1267
					 * we enter */
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
  1268
					return_track(FindFirstBit2x64(trackdirs & _road_exit_dir_to_incoming_trackdirs[dir]));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1269
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1270
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1271
		}
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
  1272
		/* Do some pathfinding */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1273
		frd.dest = desttile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1274
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
  1275
		best_track = INVALID_TRACKDIR;
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
  1276
		uint best_dist = (uint)-1;
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
  1277
		uint best_maxlen = (uint)-1;
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
  1278
		uint bitmask = (uint)trackdirs;
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1279
		uint i;
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1280
		FOR_EACH_SET_BIT(i, bitmask) {
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1281
			if (best_track == INVALID_TRACKDIR) best_track = (Trackdir)i; // in case we don't find the path, just pick a track
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1282
			frd.maxtracklen = (uint)-1;
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1283
			frd.mindist = (uint)-1;
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1284
			FollowTrack(tile, 0x2000 | 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
  1285
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1286
			if (frd.mindist < best_dist || (frd.mindist == best_dist && frd.maxtracklen < best_maxlen)) {
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1287
				best_dist = frd.mindist;
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1288
				best_maxlen = frd.maxtracklen;
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1289
				best_track = (Trackdir)i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1290
			}
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
  1291
		}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1292
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1293
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1294
found_best_track:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1295
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1296
	if (HasBit(signal, best_track)) return INVALID_TRACKDIR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1297
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1298
	return best_track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1299
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1300
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1301
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
  1302
{
4271
1a86f0faa2cb (svn r5899) - Fix [FS#242]: Multistop not working when using NPF (r5033) (mart3p)
Darkvater
parents: 4261
diff changeset
  1303
	uint dist;
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1304
	if (_patches.yapf.road_use_yapf) {
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
  1305
		/* use YAPF */
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1306
		dist = YapfRoadVehDistanceToTile(v, tile);
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1307
	} else {
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
  1308
		/* use NPF */
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1309
		NPFFindStationOrTileData fstd;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
  1310
		Trackdir trackdir = GetVehicleTrackdir(v);
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
  1311
		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
  1312
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1313
		fstd.dest_coords = tile;
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1314
		fstd.station_index = INVALID_STATION; // indicates that the destination is a tile, not a station
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1315
9624
b71483f2330f (svn r9915) [NoAI] -Sync with trunk -r9815:9914
glx
parents: 9620
diff changeset
  1316
		dist = NPFRouteToStationOrTile(v->tile, trackdir, &fstd, TRANSPORT_ROAD, v->u.road.compatible_roadtypes, v->owner, INVALID_RAILTYPE).best_path_dist;
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
  1317
		/* change units from NPF_TILE_LENGTH to # of tiles */
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1318
		if (dist != UINT_MAX)
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1319
			dist = (dist + NPF_TILE_LENGTH - 1) / NPF_TILE_LENGTH;
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1320
	}
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1321
	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
  1322
}
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1323
6329
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1324
enum {
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1325
	RDE_NEXT_TILE = 0x80,
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1326
	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
  1327
0c3dc188f099 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6331
diff changeset
  1328
	/* 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
  1329
	 * The start frame is different for vehicles that turned around or
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1330
	 * are leaving the depot as the do not start at the edge of the tile.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1331
	 * For trams there are a few different start frames as there are two
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1332
	 * places where trams can turn. */
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1333
	RVC_DEFAULT_START_FRAME                =  0,
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1334
	RVC_TURN_AROUND_START_FRAME            =  1,
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1335
	RVC_DEPOT_START_FRAME                  =  6,
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1336
	RVC_START_FRAME_AFTER_LONG_TRAM        = 22,
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1337
	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
  1338
	/* Stop frame for a vehicle in a drive-through stop */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1339
	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
  1340
};
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1341
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1342
struct RoadDriveEntry {
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1343
	byte x, y;
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
  1344
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1345
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1346
#include "table/roadveh.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1347
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1348
static const byte _road_veh_data_1[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1349
	20, 20, 16, 16, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1350
	19, 19, 15, 15, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1351
	16, 16, 12, 12, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1352
	15, 15, 11, 11
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
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1355
static bool RoadVehLeaveDepot(Vehicle *v, bool first)
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1356
{
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1357
	/* Don't leave if not all the wagons are in the depot. */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1358
	for (const Vehicle *u = v; u != NULL; u = u->Next()) {
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1359
		if (u->u.road.state != RVSB_IN_DEPOT || u->tile != v->tile) return false;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1360
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1361
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1362
	DiagDirection dir = GetRoadDepotDirection(v->tile);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1363
	v->direction = DiagDirToDir(dir);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1364
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1365
	Trackdir tdir = _roadveh_depot_exit_trackdir[dir];
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1366
	const RoadDriveEntry *rdp = _road_drive_data[v->u.road.roadtype][(_opt.road_side << RVS_DRIVE_SIDE) + tdir];
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1367
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1368
	int x = TileX(v->tile) * TILE_SIZE + (rdp[RVC_DEPOT_START_FRAME].x & 0xF);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1369
	int y = TileY(v->tile) * TILE_SIZE + (rdp[RVC_DEPOT_START_FRAME].y & 0xF);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1370
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1371
	if (first) {
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1372
		if (RoadVehFindCloseTo(v, x, y, v->direction) != NULL) return true;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1373
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1374
		VehicleServiceInDepot(v);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1375
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1376
		StartRoadVehSound(v);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1377
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1378
		/* Vehicle is about to leave a depot */
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1379
		v->cur_speed = 0;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1380
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1381
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1382
	BeginVehicleMove(v);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1383
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1384
	v->vehstatus &= ~VS_HIDDEN;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1385
	v->u.road.state = tdir;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1386
	v->u.road.frame = RVC_DEPOT_START_FRAME;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1387
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
  1388
	v->cur_image = v->GetImage(v->direction);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1389
	v->UpdateDeltaXY(v->direction);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1390
	SetRoadVehPosition(v,x,y);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1391
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1392
	InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1393
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1394
	return true;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1395
}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1396
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1397
static Trackdir FollowPreviousRoadVehicle(const Vehicle *v, const Vehicle *prev, TileIndex tile, DiagDirection entry_dir, bool already_reversed)
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1398
{
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1399
	if (prev->tile == v->tile && !already_reversed) {
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1400
		/* If the previous vehicle is on the same tile as this vehicle is
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1401
		 * then it must have reversed. */
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1402
		return _road_reverse_table[entry_dir];
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1403
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1404
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1405
	byte prev_state = prev->u.road.state;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1406
	Trackdir dir;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1407
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1408
	if (prev_state == RVSB_WORMHOLE || prev_state == RVSB_IN_DEPOT) {
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1409
		DiagDirection diag_dir = INVALID_DIAGDIR;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1410
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1411
		if (IsTileType(tile, MP_TUNNELBRIDGE)) {
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1412
			diag_dir = GetTunnelBridgeDirection(tile);
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9686
diff changeset
  1413
		} else if (IsTileType(tile, MP_ROAD) && GetRoadTileType(tile) == ROAD_TILE_DEPOT) {
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1414
			diag_dir = ReverseDiagDir(GetRoadDepotDirection(tile));
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1415
		}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1416
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1417
		if (diag_dir == INVALID_DIAGDIR) return INVALID_TRACKDIR;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1418
		dir = DiagdirToDiagTrackdir(diag_dir);
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1419
	} else if (HasBit(prev_state, RVS_IN_DT_ROAD_STOP)) {
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1420
		dir = (Trackdir)(prev_state & RVSB_ROAD_STOP_TRACKDIR_MASK);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1421
	} else if (prev_state < TRACKDIR_END) {
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1422
		if (already_reversed && prev->tile != tile) {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1423
			/*
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1424
			 * The vehicle has reversed, but did not go straight back.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1425
			 * It immediatelly turn onto another tile. This means that
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1426
			 * the roadstate of the previous vehicle cannot be used
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1427
			 * as the direction we have to go with this vehicle.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1428
			 *
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1429
			 * Next table is build in the following way:
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1430
			 *  - first row for when the vehicle in front went to the northern or
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1431
			 *    western tile, second for southern and eastern.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1432
			 *  - columns represent the entry direction.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1433
			 *  - cell values are determined by the Trackdir one has to take from
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1434
			 *    the entry dir (column) to the tile in north or south by only
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1435
			 *    going over the trackdirs used for turning 90 degrees, i.e.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1436
			 *    TRACKDIR_{UPPER,RIGHT,LOWER,LEFT}_{N,E,S,W}.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1437
			 */
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1438
			Trackdir reversed_turn_lookup[2][DIAGDIR_END] = {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1439
				{ TRACKDIR_UPPER_W, TRACKDIR_RIGHT_N, TRACKDIR_LEFT_N,  TRACKDIR_UPPER_E },
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1440
				{ TRACKDIR_RIGHT_S, TRACKDIR_LOWER_W, TRACKDIR_LOWER_E, TRACKDIR_LEFT_S  }};
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1441
			dir = reversed_turn_lookup[prev->tile < tile ? 0 : 1][ReverseDiagDir(entry_dir)];
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1442
		} else {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1443
			dir = (Trackdir)prev_state;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1444
		}
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1445
	} else {
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1446
		return INVALID_TRACKDIR;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1447
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1448
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1449
	/* Do some sanity checking. */
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1450
	static const RoadBits required_roadbits[] = {
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1451
		ROAD_X,            ROAD_Y,            ROAD_NW | ROAD_NE, ROAD_SW | ROAD_SE,
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1452
		ROAD_NW | ROAD_SW, ROAD_NE | ROAD_SE, ROAD_X,            ROAD_Y
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1453
	};
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1454
	RoadBits required = required_roadbits[dir & 0x07];
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1455
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1456
	if ((required & GetAnyRoadBits(tile, v->u.road.roadtype)) == ROAD_NONE) {
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1457
		dir = INVALID_TRACKDIR;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1458
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1459
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1460
	return dir;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1461
}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1462
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1463
/**
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1464
 * Can a tram track build without destruction on the given tile?
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1465
 * @param p the player that would be building the tram tracks
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1466
 * @param t the tile to build on.
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1467
 * @param r the road bits needed.
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1468
 * @return true when a track track can be build on 't'
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1469
 */
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1470
static bool CanBuildTramTrackOnTile(PlayerID p, TileIndex t, RoadBits r)
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1471
{
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1472
	/* The 'current' player is not necessarily the owner of the vehicle. */
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1473
	PlayerID original_player = _current_player;
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1474
	_current_player = p;
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1475
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1476
	CommandCost ret = DoCommand(t, ROADTYPE_TRAM << 4 | r, 0, 0, CMD_BUILD_ROAD);
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1477
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1478
	_current_player = original_player;
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1479
	return CmdSucceeded(ret);
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1480
}
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1481
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1482
static bool IndividualRoadVehicleController(Vehicle *v, const Vehicle *prev)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1483
{
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1484
	Direction new_dir;
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1485
	Direction old_dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1486
	RoadDriveEntry rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1487
	int x,y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1488
	uint32 r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1489
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1490
	if (v->u.road.overtaking != 0)  {
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1491
		if (IsTileType(v->tile, MP_STATION)) {
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1492
			/* Force us to be not overtaking! */
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1493
			v->u.road.overtaking = 0;
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1494
		} 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
  1495
			/* 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
  1496
			 *  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
  1497
			 *  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
  1498
			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
  1499
				v->u.road.overtaking = 0;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1500
			}
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1501
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1502
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1503
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1504
	/* If this vehicle is in a depot and we've reached this point it must be
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1505
	 * one of the articulated parts. It will stay in the depot until activated
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1506
	 * by the previous vehicle in the chain when it gets to the right place. */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1507
	if (v->IsInDepot()) return true;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1508
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1509
	/* 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
  1510
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1511
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
  1512
	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
  1513
		/* Vehicle is entering a depot or is on a bridge or in a tunnel */
6479
b228a94a1832 (svn r8897) -Fix
tron
parents: 6467
diff changeset
  1514
		GetNewVehiclePosResult gp = GetNewVehiclePos(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1515
6194
3d84c2b85f3d (svn r8609) -Fix: a road vehicle that is very close after another (slower) road vehicle gets its speed reset to 0 when entering a tunnel, which causes a traffic jam outside of the tunnel.
rubidium
parents: 6173
diff changeset
  1516
		const Vehicle *u = RoadVehFindCloseTo(v, gp.x, gp.y, v->direction);
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1517
		if (u != NULL && u->First()->cur_speed < v->cur_speed) {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1518
			v->cur_speed = u->First()->cur_speed;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1519
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1520
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1521
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1522
		if ((IsTunnelTile(gp.new_tile) || IsBridgeTile(gp.new_tile)) && 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
  1523
			/* Vehicle has just entered a bridge or tunnel */
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
  1524
			v->cur_image = v->GetImage(v->direction);
9620
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
  1525
			v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1526
			SetRoadVehPosition(v,gp.x,gp.y);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1527
			return true;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1530
		v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1531
		v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1532
		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
  1533
		if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1534
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1535
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1536
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1537
	/* 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
  1538
	 * 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
  1539
	 * In this case v->u.road.state is masked to give the road stop entry direction. */
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1540
	rd = _road_drive_data[v->u.road.roadtype][(
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1541
		(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
  1542
		(_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
  1543
6329
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1544
	if (rd.x & RDE_NEXT_TILE) {
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9686
diff changeset
  1545
		TileIndex tile = v->tile + TileOffsByDiagDir((DiagDirection)(rd.x & 3));
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1546
		Trackdir dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1547
		uint32 r;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1548
		Direction newdir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1549
		const RoadDriveEntry *rdp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1550
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1551
		if (IsRoadVehFront(v)) {
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1552
			/* If this is the front engine, look for the right path. */
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1553
			dir = RoadFindPathToDest(v, tile, (DiagDirection)(rd.x & 3));
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1554
		} else {
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1555
			dir = FollowPreviousRoadVehicle(v, prev, tile, (DiagDirection)(rd.x & 3), false);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1556
		}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1557
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
  1558
		if (dir == INVALID_TRACKDIR) {
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1559
			if (!IsRoadVehFront(v)) error("!Disconnecting road vehicle.");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1560
			v->cur_speed = 0;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1561
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1562
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1563
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1564
again:
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1565
		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
  1566
		if (IsReversingRoadTrackdir(dir)) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1567
			/* Turning around */
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1568
			if (v->u.road.roadtype == ROADTYPE_TRAM) {
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1569
				/* Determine the road bits the tram needs to be able to turn around
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1570
				 * using the 'big' corner loop. */
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1571
				RoadBits needed;
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1572
				switch (dir) {
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1573
					default: NOT_REACHED();
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1574
					case TRACKDIR_RVREV_NE: needed = ROAD_SW; break;
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1575
					case TRACKDIR_RVREV_SE: needed = ROAD_NW; break;
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1576
					case TRACKDIR_RVREV_SW: needed = ROAD_NE; break;
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1577
					case TRACKDIR_RVREV_NW: needed = ROAD_SE; break;
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1578
				}
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1579
				if ((v->Previous() != NULL && v->Previous()->tile == tile) ||
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1580
						(IsRoadVehFront(v) && IsTileType(tile, MP_ROAD) &&
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1581
							GetRoadTileType(tile) == ROAD_TILE_NORMAL && !HasRoadWorks(tile) &&
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1582
							(needed & GetRoadBits(tile, ROADTYPE_TRAM)) != ROAD_NONE)) {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1583
					/*
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1584
					 * Taking the 'big' corner for trams only happens when:
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1585
					 * - The previous vehicle in this (articulated) tram chain is
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1586
					 *   already on the 'next' tile, we just follow them regardless of
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1587
					 *   anything. When it is NOT on the 'next' tile, the tram started
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1588
					 *   doing a reversing turn when the piece of tram track on the next
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1589
					 *   tile did not exist yet. Do not use the big tram loop as that is
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1590
					 *   going to cause the tram to split up.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1591
					 * - Or the front of the tram can drive over the next tile.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1592
					 */
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1593
				} else if (!IsRoadVehFront(v) || !CanBuildTramTrackOnTile(v->owner, tile, needed)) {
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1594
					/*
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1595
					 * Taking the 'small' corner for trams only happens when:
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1596
					 * - We are not the from vehicle of an articulated tram.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1597
					 * - Or when the player cannot build on the next tile.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1598
					 *
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1599
					 * The 'small' corner means that the vehicle is on the end of a
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1600
					 * tram track and needs to start turning there. To do this properly
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1601
					 * the tram needs to start at an offset in the tram turning 'code'
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1602
					 * for 'big' corners. It furthermore does not go to the next tile,
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1603
					 * so that needs to be fixed too.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1604
					 */
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1605
					tile = v->tile;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1606
					start_frame = RVC_TURN_AROUND_START_FRAME_SHORT_TRAM;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1607
				} else {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1608
					/* The player can build on the next tile, so wait till (s)he does. */
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1609
					v->cur_speed = 0;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1610
					return false;
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1611
				}
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9686
diff changeset
  1612
			} else if (IsTileType(v->tile, MP_ROAD) && GetRoadTileType(v->tile) == ROAD_TILE_NORMAL && GetDisallowedRoadDirections(v->tile) != DRD_NONE) {
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1613
				v->cur_speed = 0;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1614
				return false;
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1615
			} else {
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1616
				tile = v->tile;
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1617
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1618
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1619
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1620
		/* Get position data for first frame on the new tile */
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1621
		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
  1622
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1623
		x = TileX(tile) * TILE_SIZE + rdp[start_frame].x;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1624
		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
  1625
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
  1626
		newdir = RoadVehGetSlidingDirection(v, x, y);
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1627
		if (IsRoadVehFront(v)) {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1628
			Vehicle *u = RoadVehFindCloseTo(v, x, y, newdir);
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1629
			if (u != NULL) {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1630
				v->cur_speed = u->First()->cur_speed;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1631
				return false;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1632
			}
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1633
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1634
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1635
		r = VehicleEnterTile(v, tile, x, y);
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1636
		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
  1637
			if (!IsTileType(tile, MP_TUNNELBRIDGE)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1638
				v->cur_speed = 0;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1639
				return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1640
			}
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1641
			/* 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
  1642
			dir = _road_reverse_table[rd.x & 3];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1643
			goto again;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1644
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1645
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1646
		if (IsInsideMM(v->u.road.state, RVSB_IN_ROAD_STOP, RVSB_IN_DT_ROAD_STOP_END) && IsTileType(v->tile, MP_STATION)) {
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1647
			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
  1648
				/* New direction is trying to turn vehicle around.
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1649
				 * 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
  1650
				v->cur_speed = 0;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1651
				return false;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1652
			}
3338
2c60ed0dcce3 (svn r4120) Use the new station functions where appropriate
tron
parents: 3315
diff changeset
  1653
			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
  1654
				RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1655
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1656
				/* 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
  1657
				 * For drive-through stops, only do it if the vehicle stopped here */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1658
				if (IsStandardRoadStopTile(v->tile) || HasBit(v->u.road.state, RVS_IS_STOPPING)) {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1659
					rs->FreeBay(HasBit(v->u.road.state, RVS_USING_SECOND_BAY));
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1660
					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
  1661
				}
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1662
				if (IsStandardRoadStopTile(v->tile)) rs->SetEntranceBusy(false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1663
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1664
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1665
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1666
		if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1667
			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
  1668
			v->u.road.state = (byte)dir;
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1669
			v->u.road.frame = start_frame;
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
		if (newdir != v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1672
			v->direction = newdir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1673
			v->cur_speed -= v->cur_speed >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1674
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1675
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
  1676
		v->cur_image = v->GetImage(newdir);
9620
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
  1677
		v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1678
		RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1679
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1680
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1681
6329
13ccf5062387 (svn r8723) -Codechange: replace yet another couple of magic numbers with enums.
rubidium
parents: 6328
diff changeset
  1682
	if (rd.x & RDE_TURNED) {
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1683
		/* Vehicle has finished turning around, it will now head back onto the same tile */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1684
		Trackdir dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1685
		uint32 r;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1686
		Direction newdir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1687
		const RoadDriveEntry *rdp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1688
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1689
		uint turn_around_start_frame = RVC_TURN_AROUND_START_FRAME;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1690
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1691
		RoadBits tram;
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1692
		if (v->u.road.roadtype == ROADTYPE_TRAM && CountBits(tram = GetAnyRoadBits(v->tile, ROADTYPE_TRAM)) == 1) {
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1693
			/*
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1694
			 * The tram is turning around with one tram 'roadbit'. This means that
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1695
			 * it is using the 'big' corner 'drive data'. However, to support the
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1696
			 * trams to take a small corner, there is a 'turned' marker in the middle
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1697
			 * of the turning 'drive data'. When the tram took the long corner, we
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1698
			 * will still use the 'big' corner drive data, but we advance it one
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1699
			 * frame. We furthermore set the driving direction so the turning is
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1700
			 * going to be properly shown.
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1701
			 */
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1702
			turn_around_start_frame = RVC_START_FRAME_AFTER_LONG_TRAM;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1703
			switch (tram) {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1704
				default: NOT_REACHED();
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1705
				case ROAD_SW: dir = TRACKDIR_RVREV_NE; break;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1706
				case ROAD_NW: dir = TRACKDIR_RVREV_SE; break;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1707
				case ROAD_NE: dir = TRACKDIR_RVREV_SW; break;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1708
				case ROAD_SE: dir = TRACKDIR_RVREV_NW; break;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1709
			}
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1710
		} else {
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1711
			if (IsRoadVehFront(v)) {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1712
				/* If this is the front engine, look for the right path. */
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1713
				dir = RoadFindPathToDest(v, v->tile, (DiagDirection)(rd.x & 3));
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1714
			} else {
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1715
				dir = FollowPreviousRoadVehicle(v, prev, v->tile, (DiagDirection)(rd.x & 3), true);
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1716
			}
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1717
		}
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1718
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
  1719
		if (dir == INVALID_TRACKDIR) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1720
			v->cur_speed = 0;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1721
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1722
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1723
9625
3301b1b3889c (svn r10016) [NoAI] -Sync with trunk r9914:r10015.
rubidium
parents: 9624
diff changeset
  1724
		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
  1725
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1726
		x = TileX(v->tile) * TILE_SIZE + rdp[turn_around_start_frame].x;
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1727
		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
  1728
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
  1729
		newdir = RoadVehGetSlidingDirection(v, x, y);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1730
		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
  1731
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1732
		r = VehicleEnterTile(v, v->tile, x, y);
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1733
		if (HasBit(r, VETS_CANNOT_ENTER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1734
			v->cur_speed = 0;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1735
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1736
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1737
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
  1738
		v->u.road.state = dir;
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1739
		v->u.road.frame = turn_around_start_frame;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1740
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1741
		if (newdir != v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1742
			v->direction = newdir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1743
			v->cur_speed -= v->cur_speed >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1744
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1745
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
  1746
		v->cur_image = v->GetImage(newdir);
9620
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
  1747
		v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1748
		RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1749
		return true;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1750
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1751
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1752
	/* This vehicle is not in a wormhole and it hasn't entered a new tile. If
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1753
	 * it's on a depot tile, check if it's time to activate the next vehicle in
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1754
	 * the chain yet. */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1755
	if (v->Next() != NULL &&
9694
e72987579514 (svn r10775) [NoAI] -Sync: with trunk r10535:r10774.
rubidium
parents: 9686
diff changeset
  1756
			IsTileType(v->tile, MP_ROAD) && GetRoadTileType(v->tile) == ROAD_TILE_DEPOT) {
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1757
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1758
		if (v->u.road.frame == v->u.road.cached_veh_length + RVC_DEPOT_START_FRAME) {
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1759
			RoadVehLeaveDepot(v->Next(), false);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1760
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1761
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1762
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1763
	/* Calculate new position for the vehicle */
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1764
	x = (v->x_pos & ~15) + (rd.x & 15);
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1765
	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
  1766
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1767
	new_dir = RoadVehGetSlidingDirection(v, x, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1768
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1769
	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
  1770
		/* Vehicle is not in a road stop.
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1771
		 * 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
  1772
		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
  1773
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1774
		if (u != NULL) {
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1775
			u = u->First();
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1776
			/* 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
  1777
			if (v->u.road.overtaking == 0) RoadVehCheckOvertake(v, u);
9704
197cb8c6ae17 (svn r11221) [NoAI] -Sync: with trunk r11145:11220
glx
parents: 9703
diff changeset
  1778
			if (v->u.road.overtaking == 0) v->cur_speed = u->cur_speed;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1779
			return false;
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1780
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1781
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1782
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1783
	old_dir = v->direction;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1784
	if (new_dir != old_dir) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1785
		v->direction = new_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1786
		v->cur_speed -= (v->cur_speed >> 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1787
		if (old_dir != v->u.road.state) {
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1788
			/* The vehicle is in a road stop */
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
  1789
			v->cur_image = v->GetImage(new_dir);
9620
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
  1790
			v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1791
			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
  1792
			/* 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
  1793
			 * 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
  1794
			 * be repeated. (XXX) Is this intended? */
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1795
			return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1796
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1797
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1798
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1799
	/* 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
  1800
	 * 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
  1801
	 * 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
  1802
	 * (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
  1803
	 * a through route, do not stop) */
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1804
	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
  1805
			_road_veh_data_1[v->u.road.state - RVSB_IN_ROAD_STOP + (_opt.road_side << RVS_DRIVE_SIDE)] == v->u.road.frame) ||
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1806
			(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
  1807
			v->current_order.dest == GetStationIndex(v->tile) &&
9476
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6589
diff changeset
  1808
			GetRoadStopType(v->tile) == (IsCargoInClass(v->cargo_type, CC_PASSENGERS) ? RoadStop::BUS : RoadStop::TRUCK) &&
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1809
			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
  1810
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1811
		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
  1812
		Station* st = GetStationByTile(v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1813
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1814
		/* 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
  1815
		 * 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
  1816
		 * 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
  1817
		if (v->current_order.type != OT_LEAVESTATION &&
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1818
				v->current_order.type != OT_GOTO_DEPOT) {
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1819
			/* 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
  1820
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1821
			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
  1822
				TileIndex next_tile = TILE_ADD(v->tile, TileOffsByDir(v->direction));
9476
1d1ed96f32ad (svn r9323) [NoAI] -Sync with trunk r9206:9322
truelight
parents: 6589
diff changeset
  1823
				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
  1824
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1825
				/* 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
  1826
				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
  1827
					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
  1828
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1829
					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
  1830
						/* 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
  1831
						ClearSlot(v);
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1832
						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
  1833
						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
  1834
						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
  1835
						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
  1836
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1837
						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
  1838
						RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1839
						return true;
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1840
					}
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
			}
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1843
6316
6ba1efc2b7fc (svn r8694) -Codechange: make RoadStop's status accessible via accessor functions.
rubidium
parents: 6315
diff changeset
  1844
			rs->SetEntranceBusy(false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1845
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3236
diff changeset
  1846
			v->last_station_visited = GetStationIndex(v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1847
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1848
			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
  1849
			v->BeginLoading();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1850
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1851
			return false;
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
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1854
		/* 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
  1855
		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
  1856
			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
  1857
				/* 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
  1858
				v->cur_speed = 0;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1859
				return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1860
			}
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
  1861
			v->current_order.Free();
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1862
			ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1863
		}
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
  1864
6338
0fb4f452873c (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6335
diff changeset
  1865
		if (IsStandardRoadStopTile(v->tile)) rs->SetEntranceBusy(true);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1866
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1867
		if (rs == v->u.road.slot) {
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1868
			/* We are leaving the correct station */
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1869
			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
  1870
		} else if (v->u.road.slot != NULL) {
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
  1871
			/* We are leaving the wrong station
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
  1872
			 * XXX The question is .. what to do? Actually we shouldn't be here
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
  1873
			 * 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
  1874
			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
  1875
			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
  1876
				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
  1877
			}
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1878
			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
  1879
				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
  1880
			}
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1881
			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
  1882
				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
  1883
			} 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
  1884
				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
  1885
					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
  1886
							st->index, v->current_order.dest);
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1887
			}
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1888
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  1889
			DEBUG(ms, 2, " force a slot clearing");
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1890
			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
  1891
		}
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1892
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1893
		StartRoadVehSound(v);
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 679
diff changeset
  1894
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1895
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1896
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1897
	/* 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
  1898
	 * entry onto bridge or into tunnel */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1899
	r = VehicleEnterTile(v, v->tile, x, y);
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1900
	if (HasBit(r, VETS_CANNOT_ENTER)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1901
		v->cur_speed = 0;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1902
		return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1903
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1904
6315
0f38464910a1 (svn r8692) -Cleanup: add comments to RoadVehController. Patch by mart3p.
rubidium
parents: 6195
diff changeset
  1905
	/* 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
  1906
	 * in a depot or entered a tunnel/bridge */
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  1907
	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
  1908
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
  1909
	v->cur_image = v->GetImage(v->direction);
9620
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
  1910
	v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1911
	RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1912
	return true;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1913
}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1914
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1915
static void RoadVehController(Vehicle *v)
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1916
{
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1917
	/* decrease counters */
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1918
	v->tick_counter++;
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
  1919
	v->current_order_time++;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1920
	if (v->u.road.reverse_ctr != 0) v->u.road.reverse_ctr--;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1921
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1922
	/* handle crashed */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1923
	if (v->vehstatus & VS_CRASHED) {
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1924
		RoadVehIsCrashed(v);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1925
		return;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1926
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1927
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1928
	RoadVehCheckTrainCrash(v);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1929
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1930
	/* road vehicle has broken down? */
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1931
	if (v->breakdown_ctr != 0) {
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1932
		if (v->breakdown_ctr <= 2) {
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1933
			HandleBrokenRoadVeh(v);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1934
			return;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1935
		}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1936
		v->breakdown_ctr--;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1937
	}
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1938
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1939
	if (v->vehstatus & VS_STOPPED) return;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1940
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1941
	ProcessRoadVehOrder(v);
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1942
	v->HandleLoading();
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1943
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1944
	if (v->current_order.type == OT_LOADING) return;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1945
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1946
	if (v->IsInDepot() && RoadVehLeaveDepot(v, true)) return;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1947
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1948
	/* Check if vehicle needs to proceed, return if it doesn't */
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1949
	if (!RoadVehAccelerate(v)) return;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1950
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1951
	for (Vehicle *prev = NULL; v != NULL; prev = v, v = v->Next()) {
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1952
		if (!IndividualRoadVehicleController(v, prev)) break;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1953
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1954
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1955
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1956
static void AgeRoadVehCargo(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1957
{
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
  1958
	if (_age_cargo_skip_counter != 0) return;
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
  1959
	v->cargo.AgeCargo();
0
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
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
  1962
void RoadVehicle::Tick()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1963
{
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
  1964
	AgeRoadVehCargo(this);
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  1965
9631
8a2d1c2ceb88 (svn r10461) [NoAI] -Sync with trunk r10349:r10460.
rubidium
parents: 9629
diff changeset
  1966
	if (IsRoadVehFront(this)) RoadVehController(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1967
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1968
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1969
static void CheckIfRoadVehNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1970
{
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
  1971
	/* If we already got a slot at a stop, use that FIRST, and go to a depot later */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1972
	if (v->u.road.slot != NULL || _patches.servint_roadveh == 0 || !VehicleNeedsService(v)) return;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1973
	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
  1974
		VehicleServiceInDepot(v);
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  1975
		return;
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  1976
	}
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
  1977
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
  1978
	/* XXX If we already have a depot order, WHY do we search over and over? */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  1979
	const Depot *depot = FindClosestRoadDepot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1980
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1981
	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
  1982
		if (v->current_order.type == OT_GOTO_DEPOT) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1983
			v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1984
			v->current_order.flags = 0;
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 679
diff changeset
  1985
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1986
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1987
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1988
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1989
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1990
	if (v->current_order.type == OT_GOTO_DEPOT &&
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1991
			v->current_order.flags & OF_NON_STOP &&
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  1992
			!Chance16(1, 20)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1993
		return;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1994
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1995
9624
b71483f2330f (svn r9915) [NoAI] -Sync with trunk -r9815:9914
glx
parents: 9620
diff changeset
  1996
	if (v->current_order.type == OT_LOADING) v->LeaveStation();
b71483f2330f (svn r9915) [NoAI] -Sync with trunk -r9815:9914
glx
parents: 9620
diff changeset
  1997
	ClearSlot(v);
b71483f2330f (svn r9915) [NoAI] -Sync with trunk -r9815:9914
glx
parents: 9620
diff changeset
  1998
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1999
	v->current_order.type = OT_GOTO_DEPOT;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2000
	v->current_order.flags = OF_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
  2001
	v->current_order.dest = depot->index;
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  2002
	v->dest_tile = depot->xy;
755
823358e83abf (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 679
diff changeset
  2003
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2004
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2005
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2006
void OnNewDay_RoadVeh(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2007
{
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  2008
	CommandCost cost(EXPENSES_ROADVEH_RUN);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2009
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  2010
	if (!IsRoadVehFront(v)) return;
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  2011
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
  2012
	if ((++v->day_counter & 7) == 0) DecreaseVehicleValue(v);
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
  2013
	if (v->u.road.blocked_ctr == 0) CheckVehicleBreakdown(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2014
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2015
	AgeVehicle(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2016
	CheckIfRoadVehNeedsService(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2017
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  2018
	CheckOrders(v);
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 11
diff changeset
  2019
9574
698395509d12 (svn r9575) [NoAI] -Sync with trunk r9504:9574
glx
parents: 9517
diff changeset
  2020
	/* Current slot has expired */
3481
5425ba1d135a (svn r4326) Only reduce the slot age of a vehicle if it has a slot assigned
tron
parents: 3431
diff changeset
  2021
	if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot != NULL && v->u.road.slot_age-- == 0) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2022
		DEBUG(ms, 3, "Slot expired for vehicle %d (index %d) at stop 0x%X",
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2023
			v->unitnumber, v->index, v->u.road.slot->xy);
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2024
		ClearSlot(v);
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2025
	}
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2026
3236
36e8eb3ae55a (svn r3909) [multistop]
celestar
parents: 3186
diff changeset
  2027
	if (v->vehstatus & VS_STOPPED) return;
36e8eb3ae55a (svn r3909) [multistop]
celestar
parents: 3186
diff changeset
  2028
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2029
	/* update destination */
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2030
	if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot == NULL && !(v->vehstatus & VS_CRASHED)) {
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2031
		Station *st = GetStation(v->current_order.dest);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2032
		RoadStop *rs = st->GetPrimaryRoadStop(v);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2033
		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
  2034
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2035
		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
  2036
			/* 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
  2037
			 * 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
  2038
			 * 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
  2039
			 * 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
  2040
			 *    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
  2041
			 */
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
			if (DistanceManhattan(v->tile, rs->xy) < 16 || st->rect.PtInExtendedRect(TileX(v->tile), TileY(v->tile), 2)) {
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2043
				uint dist, badness;
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2044
				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
  2045
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2046
				DEBUG(ms, 2, "Attempting to obtain a slot for vehicle %d (index %d) at station %d (0x%X)",
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3990
diff changeset
  2047
					v->unitnumber, v->index, st->index, st->xy
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3990
diff changeset
  2048
				);
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2049
				/* Now we find the nearest road stop that has a free slot */
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2050
				for (; rs != NULL; rs = rs->GetNextRoadStop(v)) {
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2051
					dist = RoadFindPathToStop(v, rs->xy);
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2052
					if (dist == UINT_MAX) {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2053
						DEBUG(ms, 4, " stop 0x%X is unreachable, not treating further", rs->xy);
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2054
						continue;
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2055
					}
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  2056
					badness = (rs->num_vehicles + 1) * (rs->num_vehicles + 1) + dist;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2057
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2058
					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
  2059
					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
  2060
					DEBUG(ms, 4, " badness %u", badness);
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2061
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2062
					if (badness < minbadness) {
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2063
						best = rs;
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2064
						minbadness = badness;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2065
					}
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  2066
				}
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  2067
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2068
				if (best != NULL) {
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2069
					best->num_vehicles++;
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2070
					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
  2071
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2072
					v->u.road.slot = best;
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2073
					v->dest_tile = best->xy;
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  2074
					v->u.road.slot_age = 14;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2075
				} else {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2076
					DEBUG(ms, 3, "Could not find a suitable stop");
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2077
				}
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2078
			} else {
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
  2079
				DEBUG(ms, 5, "Distance from station too far. Postponing slotting for vehicle %d (index %d) at station %d, (0x%X)",
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2080
						v->unitnumber, v->index, st->index, st->xy);
1295
5dc810674581 (svn r1799) -Codechange: [ Multistop ] Added debug class "ms" and more debugging
celestar
parents: 1282
diff changeset
  2081
			}
3123
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, 4, "No road stop for vehicle %d (index %d) at station %d (0x%X)",
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  2084
					v->unitnumber, v->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
  2085
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2086
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2087
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  2088
	cost = CommandCost(EXPENSES_ROADVEH_RUN, RoadVehInfo(v->engine_type)->running_cost * _price.roadveh_running / 364);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2089
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
  2090
	v->profit_this_year -= cost.GetCost() >> 8;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2091
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  2092
	SubtractMoneyFromPlayerFract(v->owner, cost);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2093
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2094
	InvalidateWindow(WC_VEHICLE_DETAILS, v->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
  2095
	InvalidateWindowClasses(WC_ROADVEH_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2096
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2097
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2098
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6502
diff changeset
  2099
void RoadVehiclesYearlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2100
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2101
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2102
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2103
	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
  2104
		if (v->type == VEH_ROAD) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2105
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2106
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2107
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
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
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2110
}
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2111
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2112
/** 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
  2113
 * @param tile unused
9601
b499fdd106d5 (svn r9713) [NoAI] -Sync with trunk (r9631:9712).
rubidium
parents: 9599
diff changeset
  2114
 * @param flags operation to perform
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2115
 * @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
  2116
 * @param p2 Bitstuffed elements
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2117
 * - 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
  2118
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2119
 * - p2 = (bit 16) - refit only this vehicle
9620
31e38d28a0af (svn r9816) [NoAI] -Sync with trunk r9712:9815 except r9759 (MorphOS threading) because that needs special attention.
rubidium
parents: 9601
diff changeset
  2120
 * @return cost of refit or error
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2121
 */
9629
66dde6412125 (svn r10350) [NoAI] -Sync with trunk r10194:10349
glx
parents: 9628
diff changeset
  2122
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
  2123
{
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2124
	Vehicle *v;
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  2125
	CommandCost cost(EXPENSES_ROADVEH_RUN);
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2126
	CargoID new_cid = GB(p2, 0, 8);
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2127
	byte new_subtype = GB(p2, 8, 8);
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  2128
	bool only_this = HasBit(p2, 16);
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2129
	uint16 capacity = CALLBACK_FAILED;
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2130
	uint total_capacity = 0;
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2131
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
  2132
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2133
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2134
	v = GetVehicle(p1);
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2135
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
  2136
	if (v->type != VEH_ROAD || !CheckOwnership(v->owner)) return CMD_ERROR;
9627
6a7c8ead2328 (svn r10120) [NoAI] -Sync with trunk r10096:r10119
glx
parents: 9626
diff changeset
  2137
	if (!CheckRoadVehInDepotStopped(v)) return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9722
diff changeset
  2138
	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
  2139
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2140
	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
  2141
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2142
	for (; v != NULL; v = v->Next()) {
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2143
		/* XXX: We refit all the attached wagons en-masse if they can be
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2144
		 * refitted. This is how TTDPatch does it.  TODO: Have some nice
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2145
		 * [Refit] button near each wagon. */
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2146
		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
  2147
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2148
		if (v->cargo_cap == 0) continue;
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2149
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9718
diff changeset
  2150
		if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_REFIT_CAPACITY)) {
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2151
			/* Back up the cargo type */
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2152
			CargoID temp_cid = v->cargo_type;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2153
			byte temp_subtype = v->cargo_subtype;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2154
			v->cargo_type = new_cid;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2155
			v->cargo_subtype = new_subtype;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2156
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2157
			/* Check the refit capacity callback */
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2158
			capacity = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2159
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2160
			/* Restore the original cargo type */
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2161
			v->cargo_type = temp_cid;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2162
			v->cargo_subtype = temp_subtype;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2163
		}
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2164
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2165
		if (capacity == CALLBACK_FAILED) {
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2166
			/* callback failed or not used, use default capacity */
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2167
			const RoadVehicleInfo *rvi = RoadVehInfo(v->engine_type);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2168
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2169
			CargoID old_cid = rvi->cargo_type;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2170
			/* normally, the capacity depends on the cargo type, a vehicle can
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2171
			 * carry twice as much mail/goods as normal cargo, and four times as
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2172
			 * many passengers
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2173
			 */
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2174
			capacity = GetVehicleProperty(v, 0x0F, rvi->capacity);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2175
			switch (old_cid) {
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2176
				case CT_PASSENGERS: break;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2177
				case CT_MAIL:
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2178
				case CT_GOODS: capacity *= 2; break;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2179
				default:       capacity *= 4; break;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2180
			}
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2181
			switch (new_cid) {
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2182
				case CT_PASSENGERS: break;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2183
				case CT_MAIL:
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2184
				case CT_GOODS: capacity /= 2; break;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2185
				default:       capacity /= 4; break;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2186
			}
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2187
		}
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2188
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2189
		if (capacity == 0) continue;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2190
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2191
		total_capacity += capacity;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2192
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2193
		if (IsHumanPlayer(v->owner) && new_cid != v->cargo_type) {
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2194
			cost.AddCost(GetRefitCost(v->engine_type));
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2195
		}
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2196
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2197
		if (flags & DC_EXEC) {
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2198
			v->cargo_cap = capacity;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2199
			v->cargo.Truncate((v->cargo_type == new_cid) ? capacity : 0);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2200
			v->cargo_type = new_cid;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2201
			v->cargo_subtype = new_subtype;
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2202
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2203
			InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2204
			RebuildVehicleLists();
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2205
		}
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2206
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2207
		if (only_this) break;
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2208
	}
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2209
9701
d1ac22c62f64 (svn r11036) [NoAI] -Sync: with trunk r10774:11035.
rubidium
parents: 9694
diff changeset
  2210
	_returned_refit_capacity = total_capacity;
3990
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2211
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2212
	return cost;
d3d23f3bcd34 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3977
diff changeset
  2213
}