roadveh_cmd.c
author tron
Mon, 05 Jun 2006 10:23:18 +0000
changeset 3959 e2bbaa8d4978
parent 3958 d0762b6ae4e3
child 3977 edb5b94e2094
permissions -rw-r--r--
(svn r5118) Add IsRoadVehInDepot{Stopped,}()
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
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
     3
#include "stdafx.h"
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
     4
#include <limits.h>
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1793
diff changeset
     5
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1295
diff changeset
     6
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2150
diff changeset
     7
#include "functions.h"
3144
426b825578f9 (svn r3763) Adapt to the new 'map accessors go in foo_map.h'-scheme
tron
parents: 3140
diff changeset
     8
#include "road_map.h"
3959
e2bbaa8d4978 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3958
diff changeset
     9
#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
    10
#include "station_map.h"
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 493
diff changeset
    11
#include "table/strings.h"
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 593
diff changeset
    12
#include "map.h"
1209
a1ac96655b79 (svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents: 1206
diff changeset
    13
#include "tile.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include "engine.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include "command.h"
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"
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 241
diff changeset
    22
#include "sound.h"
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
    23
#include "depot.h"
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3915
diff changeset
    24
#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
    25
#include "tunnel_map.h"
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3915
diff changeset
    26
#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
    27
#include "vehicle_gui.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
    28
#include "newgrf_engine.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
    29
#include "yapf/yapf.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
static const uint16 _roadveh_images[63] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
	0xCD4, 0xCDC, 0xCE4, 0xCEC, 0xCF4, 0xCFC, 0xD0C, 0xD14,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
	0xD24, 0xD1C, 0xD2C, 0xD04, 0xD1C, 0xD24, 0xD6C, 0xD74,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
	0xD7C, 0xC14, 0xC1C, 0xC24, 0xC2C, 0xC34, 0xC3C, 0xC4C,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
	0xC54, 0xC64, 0xC5C, 0xC6C, 0xC44, 0xC5C, 0xC64, 0xCAC,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
	0xCB4, 0xCBC, 0xD94, 0xD9C, 0xDA4, 0xDAC, 0xDB4, 0xDBC,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
	0xDCC, 0xDD4, 0xDE4, 0xDDC, 0xDEC, 0xDC4, 0xDDC, 0xDE4,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
	0xE2C, 0xE34, 0xE3C, 0xC14, 0xC1C, 0xC2C, 0xC3C, 0xC4C,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
	0xC5C, 0xC64, 0xC6C, 0xC74, 0xC84, 0xC94, 0xCA4
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
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
    43
	 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
    44
	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
    45
	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
    46
	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
    47
	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
    48
	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
    49
	 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
    50
	 0,   0,   0,   8,   8,   8,   8
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
static const uint16 _road_veh_fp_ax_and[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
	0x1009, 0x16, 0x520, 0x2A00
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 byte _road_reverse_table[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
	6, 7, 14, 15
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
static const uint16 _road_pf_table_3[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
	0x910, 0x1600, 0x2005, 0x2A
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
    66
int GetRoadVehImage(const Vehicle* v, Direction direction)
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
	int img = v->spritenum;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
	int image;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
	if (is_custom_sprite(img)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
		image = GetCustomVehicleSprite(v, direction);
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
    73
		if (image != 0) return image;
2464
3dee59d241c0 (svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents: 2260
diff changeset
    74
		img = orig_road_vehicle_info[v->engine_type - ROAD_ENGINES_INDEX].image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
    76
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
	image = direction + _roadveh_images[img];
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
    78
	if (v->cargo_count >= v->cargo_cap / 2) image += _roadveh_full_adder[img];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
	return image;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2464
diff changeset
    82
void DrawRoadVehEngine(int x, int y, EngineID engine, uint32 image_ormod)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
{
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
    84
	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
    85
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
    86
	if (is_custom_sprite(spritenum)) {
3186
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3184
diff changeset
    87
		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
    88
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
    89
		if (sprite != 0) {
378
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
    90
			DrawSprite(sprite | image_ormod, x, y);
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
    91
			return;
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
    92
		}
2464
3dee59d241c0 (svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents: 2260
diff changeset
    93
		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
    94
	}
9f170b63d48e (svn r567) -newgrf: Support for road vehicles customization (seems to work at
celestar
parents: 374
diff changeset
    95
	DrawSprite((6 + _roadveh_images[spritenum]) | image_ormod, x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2810
diff changeset
    98
static int32 EstimateRoadVehCost(EngineID engine_type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
{
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   100
	return ((_price.roadveh_base >> 3) * RoadVehInfo(engine_type)->base_cost) >> 5;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   103
/** 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
   104
 * @param tile tile of depot where road vehicle is built
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   105
 * @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
   106
 * @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
   107
 */
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
   108
int32 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
   109
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
	int32 cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
	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
   112
	UnitID unit_num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
	Engine *e;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   114
2854
6b85a5f4b65f (svn r3402) -Fix: [building/cloning] added a string telling if build failed due to not being able to build a vehicle. Triggered when cloning a retired design
bjarni
parents: 2823
diff changeset
   115
	if (!IsEngineBuildable(p1, VEH_Road)) return_cmd_error(STR_ENGINE_NOT_BUILDABLE);
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
   116
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
	cost = EstimateRoadVehCost(p1);
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   120
	if (flags & DC_QUERY_COST) return cost;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
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
   122
	/* 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
   123
	 * 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
   124
	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
   125
	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
   126
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
	v = AllocateVehicle();
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   128
	if (v == NULL || IsOrderPoolFull())
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
		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
   130
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
	/* find the first free roadveh id */
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
   132
	unit_num = (HASBIT(p2, 0) == true) ? 0 : GetFreeUnitNumber(VEH_Road);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
	if (unit_num > _patches.max_roadveh)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
		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
   135
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
	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
   137
		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
   138
		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
   139
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   140
		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
   141
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
		v->unitnumber = unit_num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
		v->direction = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
		v->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   146
		v->tile = tile;
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3630
diff changeset
   147
		x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3630
diff changeset
   148
		y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
		v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   150
		v->y_pos = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
		v->z_pos = GetSlopeZ(x,y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
		v->z_height = 6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
		v->u.road.state = 254;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
		v->vehstatus = VS_HIDDEN|VS_STOPPED|VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   157
		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
   158
		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
   159
		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
   160
		v->cargo_cap = rvi->capacity;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
//		v->cargo_count = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
		v->value = cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
//		v->day_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
//		v->next_order_param = v->next_order = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
//		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
//		v->progress = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
//	v->u.road.unk2 = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
//	v->u.road.overtaking = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
1266
9dc5638fe8cc (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
   171
		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
   172
		v->max_speed = rvi->max_speed;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
		v->engine_type = (byte)p1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
1926
68d60188a22f (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1901
diff changeset
   175
		e = GetEngine(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
		v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
		v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
		v->max_age = e->lifelength * 366;
2564
371e0fa97e1e (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2552
diff changeset
   179
		_new_vehicle_id = v->index;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   180
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
		v->string_id = STR_SV_ROADVEH_NAME;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
		v->service_interval = _patches.servint_roadveh;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   184
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
		v->date_of_last_service = _date;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
		v->build_year = _cur_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   188
		v->type = VEH_Road;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
		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
   190
		v->random_bits = VehicleRandomBits();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   191
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   192
		VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   194
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   195
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
		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
   197
		if (IsLocalPlayer())
7546b838230b (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2574
diff changeset
   198
			InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Road); // updates the replace Road window
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
	return cost;
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
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   204
/** 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
   205
 * @param tile unused
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   206
 * @param p1 road vehicle ID to start/stop
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   207
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   208
 */
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
   209
int32 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
   210
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   212
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
   213
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   214
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 904
diff changeset
   215
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   216
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   217
	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
   218
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
	if (flags & DC_EXEC) {
3959
e2bbaa8d4978 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3958
diff changeset
   220
		if (IsRoadVehInDepotStopped(v)) {
3139
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3138
diff changeset
   221
			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
   222
		}
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3138
diff changeset
   223
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
		v->vehstatus ^= VS_STOPPED;
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
   225
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   228
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   229
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   230
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   232
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
   233
{
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   234
	RoadStop *rs = v->u.road.slot;
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   235
	if (v->u.road.slot == NULL) return;
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   236
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
   237
	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
   238
	v->u.road.slot_age = 0;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   239
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   240
	assert(rs->num_vehicles != 0);
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   241
	rs->num_vehicles--;
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   242
3630
0f7828d892ae (svn r4529) - Codechange: Use proper naming for hex numbers in debug prints eg. 0xF3A6. Use fixed lengths where applicable (newgrf). Unfortunately '%#X' is unusable since it gives 0XFF3 and '%#x' gives 0xff3 while we want 0xFF3 :P
Darkvater
parents: 3612
diff changeset
   243
	DEBUG(ms, 3) ("Multistop: 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
   244
}
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
   245
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   246
/** 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
   247
 * @param tile unused
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   248
 * @param p1 vehicle ID to be sold
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   249
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   250
 */
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
   251
int32 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
   252
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   254
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
   255
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   256
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 904
diff changeset
   257
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   259
	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
   260
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
   261
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   262
3959
e2bbaa8d4978 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3958
diff changeset
   263
	if (!IsRoadVehInDepotStopped(v)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
		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
   265
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   266
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   267
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   268
		// Invalidate depot
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   270
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
		InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   272
		DeleteWindowById(WC_VEHICLE_VIEW, v->index);
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   273
		ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
		DeleteVehicle(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
   275
		if (IsLocalPlayer()) InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Road);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   277
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   278
	return -(int32)v->value;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
typedef struct RoadFindDepotData {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
	uint best_length;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
   283
	TileIndex tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
	byte owner;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
} RoadFindDepotData;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
3186
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3184
diff changeset
   287
static const DiagDirection _road_pf_directions[] = {
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3184
diff changeset
   288
	DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_NE, DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_SE, 255, 255,
5798593c2d95 (svn r3832) Replace some magic numbers by (Diag)Direction enums
tron
parents: 3184
diff changeset
   289
	DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NW, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_NE, 255, 255
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   290
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   291
3137
1d321ced37f3 (svn r3752) FYOS avoid to cast function pointers
tron
parents: 3136
diff changeset
   292
static bool EnumRoadSignalFindDepot(TileIndex tile, void* data, int track, uint length, byte* state)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
{
3137
1d321ced37f3 (svn r3752) FYOS avoid to cast function pointers
tron
parents: 3136
diff changeset
   294
	RoadFindDepotData* rfdd = data;
1d321ced37f3 (svn r3752) FYOS avoid to cast function pointers
tron
parents: 3136
diff changeset
   295
904
bff808a1c7f0 (svn r1390) Use TileOffsByDir() instead of home brewed table
tron
parents: 900
diff changeset
   296
	tile += TileOffsByDir(_road_pf_directions[track]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   297
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
   298
	if (IsTileType(tile, MP_STREET) &&
3793
33cdb5bf7b21 (svn r4789) - Codechange: rename RoadType to RoadTileType and ROAD_{NORMAL,CROSSING,DEPOT} to ROAD_TILE_* for consistency
rubidium
parents: 3645
diff changeset
   299
			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
   300
			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
   301
			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
   302
		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
   303
		rfdd->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   308
static const Depot* FindClosestRoadDepot(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
{
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
   310
	TileIndex tile = v->tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   311
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   312
	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
   313
		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
   314
		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
   315
	} else if (_patches.new_pathfinding_all) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   316
		NPFFoundTargetData ftd;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   317
		/* See where we are now */
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   318
		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
   319
2006
324916f22a8a (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1998
diff changeset
   320
		ftd = NPFRouteToDepotBreadthFirst(v->tile, trackdir, TRANSPORT_ROAD, v->owner, INVALID_RAILTYPE);
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
   321
		if (ftd.best_bird_dist == 0) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   322
			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
   323
		} else {
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
   324
			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
   325
		}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   326
		/* 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
   327
	} else {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   328
		RoadFindDepotData rfdd;
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   329
		DiagDirection i;
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
   330
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   331
		rfdd.owner = v->owner;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   332
		rfdd.best_length = (uint)-1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   334
		/* search in all directions */
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   335
		for (i = 0; i != 4; i++) {
3137
1d321ced37f3 (svn r3752) FYOS avoid to cast function pointers
tron
parents: 3136
diff changeset
   336
			FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, i, EnumRoadSignalFindDepot, NULL, &rfdd);
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   337
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   338
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
   339
		if (rfdd.best_length == (uint)-1) return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   340
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   341
		return GetDepotByTile(rfdd.tile);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   342
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   343
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   345
/** 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
   346
 * @param tile unused
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   347
 * @param p1 vehicle ID to send to the depot
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   348
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   349
 */
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
   350
int32 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
   351
{
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
   352
	Vehicle *v;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   353
	const Depot *dep;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
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
   355
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   356
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   357
	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
   358
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   359
	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
   360
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   361
	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
   362
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   363
	/* If the current orders are already goto-depot */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   364
	if (v->current_order.type == OT_GOTO_DEPOT) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   365
		if (flags & DC_EXEC) {
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   366
			/* 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
   367
			 * then skip to the next order; effectively cancelling this forced service */
1530
caa16c506a22 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
   368
			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
   369
				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
   370
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   371
			v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   372
			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
   373
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   378
	dep = FindClosestRoadDepot(v);
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   379
	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
   380
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
	if (flags & DC_EXEC) {
3236
36e8eb3ae55a (svn r3909) [multistop]
celestar
parents: 3186
diff changeset
   382
		ClearSlot(v);
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   383
		v->current_order.type = OT_GOTO_DEPOT;
1530
caa16c506a22 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
   384
		v->current_order.flags = OF_NON_STOP | OF_HALT_IN_DEPOT;
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   385
		v->current_order.station = dep->index;
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   386
		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
   387
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   389
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   391
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   393
/** 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
   394
 * @param tile unused
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   395
 * @param p1 vehicle ID to turn
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   396
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   397
 */
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
   398
int32 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
   399
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
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
   402
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1232
diff changeset
   403
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 904
diff changeset
   404
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   405
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   406
	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
   407
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3915
diff changeset
   408
	if (v->vehstatus & VS_STOPPED ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
			v->u.road.crashed_ctr != 0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
			v->breakdown_ctr != 0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
			v->u.road.overtaking != 0 ||
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3915
diff changeset
   412
			v->u.road.state == 255 ||
3959
e2bbaa8d4978 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3958
diff changeset
   413
			IsRoadVehInDepot(v) ||
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
			v->cur_speed < 5) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   416
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   417
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3915
diff changeset
   418
	if (IsTunnelTile(v->tile) && DirToDiagDir(v->direction) == GetTunnelDirection(v->tile)) return CMD_ERROR;
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3915
diff changeset
   419
	if (IsBridgeTile(v->tile) && DirToDiagDir(v->direction) == GetBridgeRampDirection(v->tile)) return CMD_ERROR;
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3915
diff changeset
   420
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   421
	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
   422
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   423
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
static void MarkRoadVehDirty(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   429
	v->cur_image = GetRoadVehImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
	MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
static void UpdateRoadVehDeltaXY(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
#define MKIT(a,b,c,d) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
	static const uint32 _delta_xy_table[8] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   441
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   442
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   443
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
	};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
#undef MKIT
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
	uint32 x = _delta_xy_table[v->direction];
2150
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2107
diff changeset
   448
	v->x_offs        = GB(x,  0, 8);
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2107
diff changeset
   449
	v->y_offs        = GB(x,  8, 8);
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2107
diff changeset
   450
	v->sprite_width  = GB(x, 16, 8);
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2107
diff changeset
   451
	v->sprite_height = GB(x, 24, 8);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
static void ClearCrashedStation(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
{
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
   456
	RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
	// mark station as not busy
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
   459
	CLRBIT(rs->status, 7);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   460
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
	// free parking bay
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
   462
	SETBIT(rs->status, HASBIT(v->u.road.state, 1) ? 1 : 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   465
static void RoadVehDelete(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   466
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   467
	DeleteWindowById(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   468
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   469
588
1b60458bdc29 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   470
	RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   471
	InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   472
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
   473
	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
   474
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   475
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   476
	EndVehicleMove(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   477
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   478
	ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   479
	DeleteVehicle(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   481
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   482
static byte SetRoadVehPosition(Vehicle *v, int x, int y)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   483
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   484
	byte new_z, old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   486
	// 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
   487
	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
   488
	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
   489
	new_z = GetSlopeZ(x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   490
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   491
	old_z = v->z_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   492
	v->z_pos = new_z;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   493
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   494
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   495
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   496
	return old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   497
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   498
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   499
static void RoadVehSetRandomDirection(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   500
{
3160
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   501
	static const DirDiff delta[] = {
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   502
		DIRDIFF_45LEFT, DIRDIFF_SAME, DIRDIFF_SAME, DIRDIFF_45RIGHT
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   503
	};
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   504
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   505
	uint32 r = Random();
3160
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   506
9201d3f304e0 (svn r3786) More work for DirDiff
tron
parents: 3159
diff changeset
   507
	v->direction = ChangeDir(v->direction, delta[r & 3]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   509
	UpdateRoadVehDeltaXY(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   510
	v->cur_image = GetRoadVehImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
	SetRoadVehPosition(v, v->x_pos, v->y_pos);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   512
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
static void RoadVehIsCrashed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   515
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   516
	v->u.road.crashed_ctr++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   517
	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
   518
		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   519
	} 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
   520
		if ((v->tick_counter & 7) == 0) RoadVehSetRandomDirection(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
	} else if (v->u.road.crashed_ctr >= 2220) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
		RoadVehDelete(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   523
	}
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
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   526
static void* EnumCheckRoadVehCrashTrain(Vehicle* v, void* data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   527
{
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   528
	const Vehicle* u = data;
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   529
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   530
	return
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   531
		v->type == VEH_Train &&
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   532
		myabs(v->z_pos - u->z_pos) <= 6 &&
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   533
		myabs(v->x_pos - u->x_pos) <= 4 &&
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   534
		myabs(v->y_pos - u->y_pos) <= 4 ?
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   535
			v : NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   537
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
static void RoadVehCrash(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
	uint16 pass;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
	v->u.road.crashed_ctr++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
	v->vehstatus |= VS_CRASHED;
3236
36e8eb3ae55a (svn r3909) [multistop]
celestar
parents: 3186
diff changeset
   544
	ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
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
   546
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
	pass = 1;
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   549
	if (v->cargo_type == CT_PASSENGERS) pass += v->cargo_count;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
	v->cargo_count = 0;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   551
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   552
	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
   553
	AddNewsItem(
3e97367f7cbc (svn r2780) Remove some more unused strings and make the use of a few strings more explicit
tron
parents: 2214
diff changeset
   554
		(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
   555
			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
   556
		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
   557
		v->index,
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   558
		0
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   559
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
	ModifyStationRatingAround(v->tile, v->owner, -160, 22);
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 538
diff changeset
   562
	SndPlayVehicleFx(SND_12_EXPLOSION, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   564
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
static void RoadVehCheckTrainCrash(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
{
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
   567
	TileIndex tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   569
	if (v->u.road.state == 255) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
	tile = v->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
3560
4389ae5ad967 (svn r4435) - Fix: an assertion triggered when trying to remove a bridge with the remove-tool (r4348 surfaced this). In CmdRemoveRoad tiletype was not checked for ownership. Intorudce IsLevelCrossingTile() which checks if a tile is a crossing without knowing the type. Suggested by peter1138 and Tron.
Darkvater
parents: 3508
diff changeset
   573
	if (!IsLevelCrossingTile(tile)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   575
	if (VehicleFromPos(tile, v, EnumCheckRoadVehCrashTrain) != NULL)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
		RoadVehCrash(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
static void HandleBrokenRoadVeh(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   584
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
			v->breakdowns_since_last_service++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   587
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   590
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 538
diff changeset
   591
		SndPlayVehicleFx((_opt.landscape != LT_CANDY) ?
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 538
diff changeset
   592
			SND_0F_VEHICLE_BREAKDOWN : SND_35_COMEDY_BREAKDOWN, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   594
		if (!(v->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
			Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   596
			if (u != NULL) u->u.special.unk0 = v->breakdown_delay * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   598
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
2989
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   600
	if ((v->tick_counter & 1) == 0) {
99c95a3ebcaa (svn r3564) Several smaller changes:
tron
parents: 2962
diff changeset
   601
		if (--v->breakdown_delay == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
			v->breakdown_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   605
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   606
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   608
static void ProcessRoadVehOrder(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
{
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   610
	const Order *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   612
	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
   613
		case OT_GOTO_DEPOT:
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   614
			// Let a depot order in the orderlist interrupt.
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   615
			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
   616
			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
   617
					!VehicleNeedsService(v)) {
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   618
				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
   619
			}
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   620
			break;
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   621
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   622
		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
   623
		case OT_LEAVESTATION:
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   627
	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
   628
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   629
	order = GetVehicleOrder(v, v->cur_order_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   631
	if (order == NULL) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   632
		v->current_order.type = OT_NOTHING;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
   633
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
		v->dest_tile = 0;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
   635
		ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   639
	if (order->type    == v->current_order.type &&
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   640
			order->flags   == v->current_order.flags &&
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   641
			order->station == v->current_order.station) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
		return;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   643
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   645
	v->current_order = *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   646
	v->dest_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   647
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   648
	if (order->type == OT_GOTO_STATION) {
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   649
		const Station* st = GetStation(order->station);
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   650
		const RoadStop* rs;
3136
e2682061ef1d (svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
tron
parents: 3135
diff changeset
   651
		TileIndex dest;
e2682061ef1d (svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
tron
parents: 3135
diff changeset
   652
		uint mindist;
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
   653
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   654
		if (order->station == v->last_station_visited) {
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   655
			v->last_station_visited = INVALID_STATION;
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   656
		}
1231
64cc1f5c1c82 (svn r1735) -Fix: [ 1112285 ] Fixed an assertion error with stations where stops
celestar
parents: 1224
diff changeset
   657
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   658
		rs = GetPrimaryRoadStop(st, v->cargo_type == CT_PASSENGERS ? RS_BUS : RS_TRUCK);
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
   659
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   660
		if (rs == NULL) {
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   661
			// There is no stop left at the station, so don't even TRY to go there
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   662
			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
   663
			InvalidateVehicleOrder(v);
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   664
			return;
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   665
		}
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
   666
3136
e2682061ef1d (svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
tron
parents: 3135
diff changeset
   667
		dest = rs->xy;
e2682061ef1d (svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
tron
parents: 3135
diff changeset
   668
		mindist = DistanceManhattan(v->tile, rs->xy);
e2682061ef1d (svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
tron
parents: 3135
diff changeset
   669
		for (rs = rs->next; rs != NULL; rs = rs->next) {
e2682061ef1d (svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
tron
parents: 3135
diff changeset
   670
			uint dist = DistanceManhattan(v->tile, rs->xy);
e2682061ef1d (svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
tron
parents: 3135
diff changeset
   671
e2682061ef1d (svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
tron
parents: 3135
diff changeset
   672
			if (dist < mindist) {
e2682061ef1d (svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
tron
parents: 3135
diff changeset
   673
				mindist = dist;
e2682061ef1d (svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
tron
parents: 3135
diff changeset
   674
				dest = rs->xy;
e2682061ef1d (svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
tron
parents: 3135
diff changeset
   675
			}
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
   676
		}
3136
e2682061ef1d (svn r3751) -Fix: Correctly implement minimum search, so road vehicles head twoards the closest station, not the last one in the list
tron
parents: 3135
diff changeset
   677
		v->dest_tile = dest;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   678
	} else if (order->type == OT_GOTO_DEPOT) {
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
   679
		v->dest_tile = GetDepot(order->station)->xy;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   680
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   681
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   682
	InvalidateVehicleOrder(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   683
}
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
static void HandleRoadVehLoading(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   686
{
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   687
	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
   688
		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
   689
			Order b;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   690
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   691
			if (--v->load_unload_time_rem != 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   692
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   693
			if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) {
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   694
				SET_EXPENSES_TYPE(EXPENSES_ROADVEH_INC);
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   695
				if (LoadUnloadVehicle(v)) {
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   696
					InvalidateWindow(WC_ROADVEH_LIST, v->owner);
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   697
					MarkRoadVehDirty(v);
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   698
				}
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   699
				return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   700
			}
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   701
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   702
			b = v->current_order;
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   703
			v->current_order.type = OT_LEAVESTATION;
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   704
			v->current_order.flags = 0;
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   705
			if (!(b.flags & OF_NON_STOP)) return;
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   706
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   708
3004
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   709
		case OT_DUMMY: break;
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   710
32d777e2353c (svn r3584) Replace the rather obscure control flow for handling road vehicle orders by something remotly comprehensible
tron
parents: 2989
diff changeset
   711
		default: return;
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
	v->cur_order_index++;
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
   715
	InvalidateVehicleOrder(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   717
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
   718
static void StartRoadVehSound(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
{
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 538
diff changeset
   720
	SoundFx s = RoadVehInfo(v->engine_type)->sfx;
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 538
diff changeset
   721
	if (s == SND_19_BUS_START_PULL_AWAY && (v->tick_counter & 3) == 0)
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 538
diff changeset
   722
		s = SND_1A_BUS_START_PULL_AWAY_WITH_HORN;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   723
	SndPlayVehicleFx(s, v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   726
typedef 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
   727
	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
   728
	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
   729
	const Vehicle* veh;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   730
	Direction dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
} RoadVehFindData;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   732
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   733
static void* EnumCheckRoadVehClose(Vehicle *v, void* data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
{
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   735
	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
   736
	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
   737
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   738
	const RoadVehFindData* rvf = data;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   739
63
53cc59601b3c (svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents: 19
diff changeset
   740
	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
   741
	short y_diff = v->y_pos - rvf->y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   742
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   743
	return
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   744
		rvf->veh != v &&
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   745
		v->type == VEH_Road &&
3959
e2bbaa8d4978 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3958
diff changeset
   746
		!IsRoadVehInDepot(v) &&
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   747
		myabs(v->z_pos - rvf->veh->z_pos) < 6 &&
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   748
		v->direction == rvf->dir &&
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   749
		(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
   750
		(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
   751
		(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
   752
		(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
   753
			v : NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   754
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   755
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   756
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
   757
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   758
	RoadVehFindData rvf;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   759
	Vehicle *u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   760
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
   761
	if (v->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
   762
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   763
	rvf.x = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   764
	rvf.y = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   765
	rvf.dir = dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   766
	rvf.veh = v;
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   767
	u = VehicleFromPos(TileVirtXY(x, y), &rvf, EnumCheckRoadVehClose);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   768
63
53cc59601b3c (svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents: 19
diff changeset
   769
	// This code protects a roadvehicle from being blocked for ever
826
0e2b569b737b (svn r1297) Language fixes in the source.. (ln-)
miham
parents: 817
diff changeset
   770
	//  If more than 1480 / 74 days a road vehicle is blocked, it will
63
53cc59601b3c (svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents: 19
diff changeset
   771
	//  drive just through it. The ultimate backup-code of TTD.
53cc59601b3c (svn r64) Fix: Road vehicles don't get stuck any more (Truelight + Celestar
dominik
parents: 19
diff changeset
   772
	// It can be disabled.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   773
	if (u == NULL) {
3009
c477ab0f00b3 (svn r3589) - Rename u.road.unk2 to u.road.blocked_ctr
peter1138
parents: 3004
diff changeset
   774
		v->u.road.blocked_ctr = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   775
		return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   776
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   777
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   778
	if (++v->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
   779
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   780
	return u;
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
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   783
static void RoadVehArrivesAt(const Vehicle* v, Station* st)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   784
{
2550
5e405f5b2836 (svn r3079) Determine the type of the "first arrival" message for road vehicles using the cargo type and not a magic engine number
tron
parents: 2549
diff changeset
   785
	if (v->cargo_type == CT_PASSENGERS) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   786
		/* Check if station was ever visited before */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   787
		if (!(st->had_vehicle_of_type & HVOT_BUS)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   788
			uint32 flags;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   789
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   790
			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
   791
			SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
			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
   793
			AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   794
				STR_902F_CITIZENS_CELEBRATE_FIRST,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   795
				flags,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   796
				v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   797
				0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   798
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   799
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   800
		/* Check if station was ever visited before */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   801
		if (!(st->had_vehicle_of_type & HVOT_TRUCK)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   802
			uint32 flags;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   803
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   804
			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
   805
			SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   806
			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
   807
			AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   808
				STR_9030_CITIZENS_CELEBRATE_FIRST,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   809
				flags,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   810
				v->index,
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   811
				0
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   812
			);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   813
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   814
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   815
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   817
static bool RoadVehAccelerate(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   818
{
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   819
	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
   820
	byte t;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   821
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   822
	// Clamp
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   823
	spd = min(spd, v->max_speed);
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3915
diff changeset
   824
	if (v->u.road.state == 255) spd = min(spd, SetSpeedLimitOnBridge(v));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   825
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
	//updates statusbar only if speed have changed to save CPU time
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   827
	if (spd != v->cur_speed) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   828
		v->cur_speed = spd;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   829
		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
   830
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   831
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
	// Decrease somewhat when turning
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   835
	if (!(v->direction & 1)) spd = spd * 3 >> 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   836
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   837
	if (spd == 0) return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   838
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   839
	if ((byte)++spd == 0) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   841
	v->progress = (t = v->progress) - (byte)spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   842
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   843
	return (t < v->progress);
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
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   846
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
   847
{
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   848
	static const Direction _roadveh_new_dir[] = {
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   849
		DIR_N , DIR_NW, DIR_W , 0,
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   850
		DIR_NE, DIR_N , DIR_SW, 0,
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   851
		DIR_E , DIR_SE, DIR_S
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   852
	};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   853
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   854
	x = x - v->x_pos + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   855
	y = y - v->y_pos + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   856
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
	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
   858
	return _roadveh_new_dir[y * 4 + x];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   859
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   860
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   861
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
   862
{
3168
34cfe6f61a12 (svn r3796) When unmagicfying code, do it Right(tm) and also give the variables sensible names. I flipped a 'b' and a 'd' in r3785
tron
parents: 3167
diff changeset
   863
	Direction new = RoadVehGetNewDirection(v, x, y);
34cfe6f61a12 (svn r3796) When unmagicfying code, do it Right(tm) and also give the variables sensible names. I flipped a 'b' and a 'd' in r3785
tron
parents: 3167
diff changeset
   864
	Direction old = 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
   865
	DirDiff delta;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
   866
3168
34cfe6f61a12 (svn r3796) When unmagicfying code, do it Right(tm) and also give the variables sensible names. I flipped a 'b' and a 'd' in r3785
tron
parents: 3167
diff changeset
   867
	if (new == old) return old;
34cfe6f61a12 (svn r3796) When unmagicfying code, do it Right(tm) and also give the variables sensible names. I flipped a 'b' and a 'd' in r3785
tron
parents: 3167
diff changeset
   868
	delta = (DirDifference(new, old) > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
34cfe6f61a12 (svn r3796) When unmagicfying code, do it Right(tm) and also give the variables sensible names. I flipped a 'b' and a 'd' in r3785
tron
parents: 3167
diff changeset
   869
	return ChangeDir(old, delta);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   870
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   871
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   872
typedef struct OvertakeData {
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   873
	const Vehicle* u;
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   874
	const Vehicle* v;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
   875
	TileIndex tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   876
	byte tilebits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   877
} OvertakeData;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   878
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   879
static void* EnumFindVehToOvertake(Vehicle* v, void* data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   880
{
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   881
	const OvertakeData* od = data;
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   882
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
   883
	return
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
   884
		v->tile == od->tile && v->type == VEH_Road && v == od->u && v == od->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
   885
			v : NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
}
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
static bool FindRoadVehToOvertake(OvertakeData *od)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
	uint32 bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   891
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   892
	bits = GetTileTrackStatus(od->tile, TRANSPORT_ROAD) & 0x3F;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   894
	if (!(od->tilebits & bits) || (bits & 0x3C) || (bits & 0x3F3F0000))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   895
		return true;
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
   896
	return VehicleFromPos(od->tile, od, EnumFindVehToOvertake) != NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   897
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   898
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   899
static void RoadVehCheckOvertake(Vehicle *v, Vehicle *u)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   901
	OvertakeData od;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   902
	byte tt;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   904
	od.v = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   905
	od.u = u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   906
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   907
	if (u->max_speed >= v->max_speed &&
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   908
			!(u->vehstatus & VS_STOPPED) &&
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   909
			u->cur_speed != 0) {
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   910
		return;
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   911
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   912
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   913
	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
   914
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
   915
	if (v->u.road.state >= 32 || (v->u.road.state & 7) > 1) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   916
2150
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2107
diff changeset
   917
	tt = GetTileTrackStatus(v->tile, TRANSPORT_ROAD) & 0x3F;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   918
	if ((tt & 3) == 0) return;
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   919
	if ((tt & 0x3C) != 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   920
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   921
	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
   922
	od.tilebits = tt;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
	od.tile = v->tile;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   925
	if (FindRoadVehToOvertake(&od)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   926
3161
0274072d39b4 (svn r3787) Use DirToDiagDir() instead of >> 1
tron
parents: 3160
diff changeset
   927
	od.tile = v->tile + TileOffsByDir(DirToDiagDir(v->direction));
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   928
	if (FindRoadVehToOvertake(&od)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   929
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
   930
	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
   931
		v->u.road.overtaking_ctr = 0x11;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   932
		v->u.road.overtaking = 0x10;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
	} else {
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   934
//		if (FindRoadVehToOvertake(&od)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
		v->u.road.overtaking_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
		v->u.road.overtaking = 0x10;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
	}
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
static void RoadZPosAffectSpeed(Vehicle *v, byte old_z)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
{
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
   942
	if (old_z == v->z_pos) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   943
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
	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
   945
		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
   946
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
		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
   948
		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
   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 int PickRandomBit(uint bits)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   954
	uint num = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   955
	uint b = bits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
	uint i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   957
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
	do {
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
   959
		if (b & 1) num++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
	} while (b >>= 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
2637
8807e6a63f89 (svn r3179) - RandomRange() and RandomTile() instead of home brewed versions
tron
parents: 2630
diff changeset
   962
	num = RandomRange(num);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
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
   964
	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
   965
	return i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
typedef struct {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
	TileIndex dest;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   970
	uint maxtracklen;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
	uint mindist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
} FindRoadToChooseData;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
3137
1d321ced37f3 (svn r3752) FYOS avoid to cast function pointers
tron
parents: 3136
diff changeset
   974
static bool EnumRoadTrackFindDist(TileIndex tile, void* data, int track, uint length, byte* state)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   975
{
3137
1d321ced37f3 (svn r3752) FYOS avoid to cast function pointers
tron
parents: 3136
diff changeset
   976
	FindRoadToChooseData* frd = 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
   977
	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
   978
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
	if (dist <= frd->mindist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
		if (dist != frd->mindist || length < frd->maxtracklen) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
			frd->maxtracklen = length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
		frd->mindist = dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   986
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   988
static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailTypeMask railtypes)
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   989
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   990
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   991
	void* perf = NpfBeginInterval();
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   992
	NPFFoundTargetData ret = NPFRouteToStationOrTile(tile, trackdir, target, type, owner, railtypes);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   993
	int t = NpfEndInterval(perf);
3947
7ab994b0fe9c (svn r5093) -CodeChange: [YAPF] min. debug level changed from 1 to 3 and 4 for frequent debug messages (performance stats)
KUDr
parents: 3937
diff changeset
   994
	DEBUG(yapf, 4)("[YAPF][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
   995
	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
   996
}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   997
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
// Returns direction to choose
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
// or -1 if the direction is currently blocked
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1000
static int RoadFindPathToDest(Vehicle* v, TileIndex tile, DiagDirection enterdir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
#define return_track(x) {best_track = x; goto found_best_track; }
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1004
	uint16 signal;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1005
	uint bitmask;
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
  1006
	TileIndex desttile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1007
	FindRoadToChooseData frd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1008
	int best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
	uint best_dist, best_maxlen;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1010
	uint i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1011
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1012
	{
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
  1013
		uint32 r = GetTileTrackStatus(tile, TRANSPORT_ROAD);
2484
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2477
diff changeset
  1014
		signal  = GB(r, 16, 16);
8e0c88a833fb (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2477
diff changeset
  1015
		bitmask = GB(r,  0, 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1016
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1017
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
  1018
	if (IsTileType(tile, MP_STREET)) {
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
  1019
		if (GetRoadTileType(tile) == ROAD_TILE_DEPOT && !IsTileOwner(tile, v->owner)) {
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
  1020
			/* Road depot owned by another player */
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
  1021
			bitmask = 0;
2549
f1d3b383d557 (svn r3078) Some more stuff, which piled up:
tron
parents: 2493
diff changeset
  1022
		}
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
  1023
	} else if (IsTileType(tile, MP_STATION) && IsRoadStopTile(tile)) {
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
  1024
		if (!IsTileOwner(tile, v->owner)) {
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
  1025
			// different station owner
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
  1026
			bitmask = 0;
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
  1027
		} else {
241
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1028
			/* Our station */
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
  1029
			RoadStopType rstype = (v->cargo_type == CT_PASSENGERS) ? RS_BUS : RS_TRUCK;
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1030
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
  1031
			if (GetRoadStopType(tile) != rstype) {
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
  1032
				// wrong station type
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
  1033
				bitmask = 0;
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
  1034
			} else {
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
  1035
				// proper station type, check if there is free loading bay
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
  1036
				const RoadStop *rs = GetRoadStopByTile(tile, rstype);
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
  1037
				if (rs == NULL || (!_patches.roadveh_queue && GB(rs->status, 0, 2) == 0)) {
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
  1038
					// station is full and RV queuing is off
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
  1039
					bitmask = 0;
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
  1040
				}
241
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1041
			}
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1042
		}
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1043
	}
e6e62a5e7f52 (svn r242) -Fix: Pathfinding bug for road vehicles introduced in r160 fixed (blathijs)
darkvater
parents: 193
diff changeset
  1044
	/* 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
  1045
	 * 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
  1046
	 * 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
  1047
	 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1048
159
139cf78bfb28 (svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents: 156
diff changeset
  1049
	/* remove unreachable tracks */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1050
	bitmask &= _road_veh_fp_ax_and[enterdir];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1051
	if (bitmask == 0) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1052
		/* No reachable tracks, so we'll reverse */
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1053
		return_track(_road_reverse_table[enterdir]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1055
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1056
	if (v->u.road.reverse_ctr != 0) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1057
		/* What happens here?? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1058
		v->u.road.reverse_ctr = 0;
1986
5dd3db2b86d7 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
  1059
		if (v->tile != tile) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1060
			return_track(_road_reverse_table[enterdir]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1061
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1062
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1063
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1064
	desttile = v->dest_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1065
	if (desttile == 0) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1066
		// Pick a random track
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1067
		return_track(PickRandomBit(bitmask));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1068
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1069
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1070
	// Only one track to choose between?
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1071
	if (!(KillFirstBit2x64(bitmask))) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1072
		return_track(FindFirstBit2x64(bitmask));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1074
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1075
	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
  1076
		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
  1077
		if (trackdir != INVALID_TRACKDIR) return_track(trackdir);
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1078
		return_track(PickRandomBit(bitmask));
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1079
	} else if (_patches.new_pathfinding_all) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1080
		NPFFindStationOrTileData fstd;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1081
		NPFFoundTargetData ftd;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1082
		byte trackdir;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1083
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1084
		NPFFillWithOrderData(&fstd, v);
1942
634961366cdc (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1085
		trackdir = DiagdirToDiagTrackdir(enterdir);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1086
		//debug("Finding path. Enterdir: %d, Trackdir: %d", enterdir, trackdir);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1087
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
  1088
		ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDir(enterdir), trackdir, &fstd, TRANSPORT_ROAD, v->owner, INVALID_RAILTYPE);
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
  1089
		if (ftd.best_trackdir == 0xff) {
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
  1090
			/* We are already at our target. Just do something */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1091
			//TODO: maybe display error?
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1092
			//TODO: go straight ahead if possible?
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1093
			return_track(FindFirstBit2x64(bitmask));
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1094
		} 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
  1095
			/* 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
  1096
			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
  1097
			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
  1098
			to the tile closest to our target. */
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1099
			return_track(ftd.best_trackdir);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1100
		}
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1101
	} else {
3167
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1102
		DiagDirection dir;
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1103
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1104
		if (IsTileType(desttile, MP_STREET)) {
3793
33cdb5bf7b21 (svn r4789) - Codechange: rename RoadType to RoadTileType and ROAD_{NORMAL,CROSSING,DEPOT} to ROAD_TILE_* for consistency
rubidium
parents: 3645
diff changeset
  1105
			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
  1106
				dir = GetRoadDepotDirection(desttile);
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1107
				goto do_it;
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1108
			}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1109
		} else if (IsTileType(desttile, MP_STATION)) {
3338
2c60ed0dcce3 (svn r4120) Use the new station functions where appropriate
tron
parents: 3315
diff changeset
  1110
			if (IsRoadStop(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
  1111
				dir = GetRoadStopDir(desttile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1112
do_it:;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1113
				/* When we are heading for a depot or station, we just
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1114
				 * pretend we are heading for the tile in front, we'll
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1115
				 * see from there */
3167
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1116
				desttile += TileOffsByDir(dir);
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1117
				if (desttile == tile && bitmask & _road_pf_table_3[dir]) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1118
					/* If we are already in front of the
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1119
					 * station/depot and we can get in from here,
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1120
					 * we enter */
3167
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1121
					return_track(FindFirstBit2x64(bitmask & _road_pf_table_3[dir]));
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1122
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1123
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1124
		}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1125
		// do pathfind
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1126
		frd.dest = desttile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1127
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1128
		best_track = -1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1129
		best_dist = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1130
		best_maxlen = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1131
		i = 0;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1132
		do {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1133
			if (bitmask & 1) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1134
				if (best_track == -1) best_track = i; // in case we don't find the path, just pick a track
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1135
				frd.maxtracklen = (uint)-1;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1136
				frd.mindist = (uint)-1;
3611
5dd134f2cf25 (svn r4506) -Fix: (FS#95) Use the tile hash when pathfinding to a depot. Eats a tiny bit of performance, but the likelihood that the depot is found is greatly increased
celestar
parents: 3560
diff changeset
  1137
				FollowTrack(tile, 0x2000 | TRANSPORT_ROAD, _road_pf_directions[i], EnumRoadTrackFindDist, NULL, &frd);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1139
				if (frd.mindist < best_dist || (frd.mindist==best_dist && frd.maxtracklen < best_maxlen)) {
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1140
					best_dist = frd.mindist;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1141
					best_maxlen = frd.maxtracklen;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1142
					best_track = i;
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1143
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1144
			}
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1145
		} while (++i,(bitmask>>=1) != 0);
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1146
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1147
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1148
found_best_track:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1149
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
  1150
	if (HASBIT(signal, best_track)) return -1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1152
	return best_track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1153
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1154
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1155
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
  1156
{
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1157
	uint dist = UINT_MAX;
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1158
	if (_patches.yapf.road_use_yapf) {
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1159
		// use YAPF
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1160
		dist = YapfRoadVehDistanceToTile(v, tile);
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1161
	} else {
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1162
		// use NPF
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1163
		NPFFindStationOrTileData fstd;
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1164
		byte trackdir = GetVehicleTrackdir(v);
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1165
		uint dist = UINT_MAX;
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1166
		assert(trackdir != 0xFF);
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1167
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1168
		fstd.dest_coords = tile;
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1169
		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
  1170
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1171
		dist = NPFRouteToStationOrTile(v->tile, trackdir, &fstd, TRANSPORT_ROAD, v->owner, INVALID_RAILTYPE).best_path_dist;
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1172
		// change units from NPF_TILE_LENGTH to # of tiles
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1173
		if (dist != UINT_MAX)
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1174
			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
  1175
	}
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1176
	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
  1177
}
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1178
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1179
typedef struct RoadDriveEntry {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1180
	byte x,y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
} RoadDriveEntry;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1182
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1183
#include "table/roadveh.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1185
static const byte _road_veh_data_1[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1186
	20, 20, 16, 16, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1187
	19, 19, 15, 15, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1188
	16, 16, 12, 12, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1189
	15, 15, 11, 11
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1190
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1191
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1192
static const byte _roadveh_data_2[4] = { 0,1,8,9 };
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1193
1401
56d372a75b74 (svn r1905) - Fix: [ 1118810 ] openttd: ship_cmd.c:642 ... Assertion failed. Mapwrap fixed in ship_cmd.c (was implicitely ok before biggermaps).
Darkvater
parents: 1359
diff changeset
  1194
static void RoadVehController(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1195
{
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1196
	Direction new_dir;
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1197
	Direction old_dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1198
	RoadDriveEntry rd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1199
	int x,y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1200
	uint32 r;
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
	// decrease counters
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1203
	v->tick_counter++;
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
  1204
	if (v->u.road.reverse_ctr != 0) v->u.road.reverse_ctr--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1205
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1206
	// handle crashed
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1207
	if (v->u.road.crashed_ctr != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1208
		RoadVehIsCrashed(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1209
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1210
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1211
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1212
	RoadVehCheckTrainCrash(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1213
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1214
	// road vehicle has broken down?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1215
	if (v->breakdown_ctr != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1216
		if (v->breakdown_ctr <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1217
			HandleBrokenRoadVeh(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1218
			return;
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
		v->breakdown_ctr--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1221
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1222
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1223
	if (v->vehstatus & VS_STOPPED) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1224
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1225
	ProcessRoadVehOrder(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1226
	HandleRoadVehLoading(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1227
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1228
	if (v->current_order.type == OT_LOADING) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1229
3959
e2bbaa8d4978 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3958
diff changeset
  1230
	if (IsRoadVehInDepot(v)) {
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
  1231
		DiagDirection dir;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1232
		const RoadDriveEntry* rdp;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1233
		byte rd2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1234
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1235
		v->cur_speed = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1236
3167
197b5ee5a831 (svn r3795) Add a function to request the orientation of a depot
tron
parents: 3161
diff changeset
  1237
		dir = GetRoadDepotDirection(v->tile);
3153
301c1d71122b (svn r3776) Replace many ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3144
diff changeset
  1238
		v->direction = DiagDirToDir(dir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1239
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1240
		rd2 = _roadveh_data_2[dir];
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1241
		rdp = _road_drive_data[(_opt.road_side << 4) + rd2];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1242
3421
8ab76c47c72c (svn r4246) -Codechange. Replaced about 100 occurences of '16' by TILE_SIZE
celestar
parents: 3404
diff changeset
  1243
		x = TileX(v->tile) * TILE_SIZE + (rdp[6].x & 0xF);
8ab76c47c72c (svn r4246) -Codechange. Replaced about 100 occurences of '16' by TILE_SIZE
celestar
parents: 3404
diff changeset
  1244
		y = TileY(v->tile) * TILE_SIZE + (rdp[6].y & 0xF);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1245
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1246
		if (RoadVehFindCloseTo(v, x, y, v->direction) != NULL) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1247
578
86e352980acd (svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents: 555
diff changeset
  1248
		VehicleServiceInDepot(v);
86e352980acd (svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents: 555
diff changeset
  1249
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1250
		StartRoadVehSound(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1251
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1252
		BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1253
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1254
		v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1255
		v->u.road.state = rd2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1256
		v->u.road.frame = 6;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1257
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1258
		v->cur_image = GetRoadVehImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1259
		UpdateRoadVehDeltaXY(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1260
		SetRoadVehPosition(v,x,y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1261
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1262
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1263
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1264
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1265
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2933
diff changeset
  1266
	if (!RoadVehAccelerate(v)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1267
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1268
	if (v->u.road.overtaking != 0)  {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1269
		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
  1270
			/* 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
  1271
			 *  if the vehicle started a corner. To protect that, only allow an abort of
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
  1272
			 *  overtake if we are on straight road, which are the 8 states below */
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
  1273
			if (v->u.road.state == 0  || v->u.road.state == 1  ||
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
  1274
					v->u.road.state == 8  || v->u.road.state == 9  ||
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
  1275
					v->u.road.state == 16 || v->u.road.state == 17 ||
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1276
					v->u.road.state == 24 || v->u.road.state == 25) {
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
  1277
				v->u.road.overtaking = 0;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1278
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1279
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1280
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1281
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1282
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1283
	if (v->u.road.state == 255) {
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1284
		GetNewVehiclePosResult gp;
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1285
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1286
		GetNewVehiclePos(v, &gp);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1287
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1288
		if (RoadVehFindCloseTo(v, gp.x, gp.y, v->direction) != NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1289
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1290
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1291
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1292
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3915
diff changeset
  1293
		if ((IsTunnelTile(gp.new_tile) || IsBridgeTile(gp.new_tile)) && VehicleEnterTile(v, gp.new_tile, gp.x, gp.y) & 4) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1294
			//new_dir = RoadGetNewDirection(v, gp.x, gp.y)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1295
			v->cur_image = GetRoadVehImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1296
			UpdateRoadVehDeltaXY(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1297
			SetRoadVehPosition(v,gp.x,gp.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1298
			return;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1301
		v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1302
		v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1303
		VehiclePositionChanged(v);
3933
a5f08e17f4a0 (svn r5070) Merged the bridge branch
celestar
parents: 3915
diff changeset
  1304
		if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1305
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1306
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1307
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1308
	rd = _road_drive_data[(v->u.road.state + (_opt.road_side << 4)) ^ 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
  1309
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1310
// switch to another tile
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1311
	if (rd.x & 0x80) {
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
  1312
		TileIndex tile = v->tile + TileOffsByDir(rd.x & 3);
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1313
		int dir = RoadFindPathToDest(v, tile, rd.x & 3);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1314
		uint32 r;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1315
		Direction newdir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1316
		const RoadDriveEntry *rdp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1317
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1318
		if (dir == -1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1319
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1320
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1321
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1322
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1323
again:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1324
		if ((dir & 7) >= 6) {
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1325
			/* Turning around */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1326
			tile = v->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1327
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1328
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
  1329
		rdp = _road_drive_data[(dir + (_opt.road_side << 4)) ^ v->u.road.overtaking];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1330
3421
8ab76c47c72c (svn r4246) -Codechange. Replaced about 100 occurences of '16' by TILE_SIZE
celestar
parents: 3404
diff changeset
  1331
		x = TileX(tile) * TILE_SIZE + rdp[0].x;
8ab76c47c72c (svn r4246) -Codechange. Replaced about 100 occurences of '16' by TILE_SIZE
celestar
parents: 3404
diff changeset
  1332
		y = TileY(tile) * TILE_SIZE + rdp[0].y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1333
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
  1334
		newdir = RoadVehGetSlidingDirection(v, x, y);
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1335
		if (RoadVehFindCloseTo(v, x, y, newdir) != NULL) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1336
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1337
		r = VehicleEnterTile(v, tile, x, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1338
		if (r & 8) {
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
  1339
			if (!IsTileType(tile, MP_TUNNELBRIDGE)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1340
				v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1341
				return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1342
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1343
			dir = _road_reverse_table[rd.x&3];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1344
			goto again;
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
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
  1347
		if (IS_BYTE_INSIDE(v->u.road.state, 0x20, 0x30) && IsTileType(v->tile, MP_STATION)) {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1348
			if ((dir & 7) >= 6) {
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1349
				v->cur_speed = 0;
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1350
				return;
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1351
			}
3338
2c60ed0dcce3 (svn r4120) Use the new station functions where appropriate
tron
parents: 3315
diff changeset
  1352
			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
  1353
				RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1354
3054
299ca9034442 (svn r3643) Remove a local variable which is just a pointer to a road stop status field and had the sensible name 'b'; instead directly use the road stop status field
tron
parents: 3033
diff changeset
  1355
				// reached a loading bay, mark it as used and clear the usage bit
299ca9034442 (svn r3643) Remove a local variable which is just a pointer to a road stop status field and had the sensible name 'b'; instead directly use the road stop status field
tron
parents: 3033
diff changeset
  1356
				SETBIT(rs->status, v->u.road.state & 2 ? 1 : 0); // occupied bay
299ca9034442 (svn r3643) Remove a local variable which is just a pointer to a road stop status field and had the sensible name 'b'; instead directly use the road stop status field
tron
parents: 3033
diff changeset
  1357
				CLRBIT(rs->status, 7); // usage bit
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1358
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1359
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1360
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1361
		if (!(r & 4)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1362
			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
  1363
			v->u.road.state = (byte)dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1364
			v->u.road.frame = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1365
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1366
		if (newdir != v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1367
			v->direction = newdir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1368
			v->cur_speed -= v->cur_speed >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1369
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1370
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1371
		v->cur_image = GetRoadVehImage(v, newdir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1372
		UpdateRoadVehDeltaXY(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1373
		RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1374
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1375
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1376
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1377
	if (rd.x & 0x40) {
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1378
		int dir = RoadFindPathToDest(v, v->tile,	rd.x & 3);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1379
		uint32 r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1380
		int tmp;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3153
diff changeset
  1381
		Direction newdir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1382
		const RoadDriveEntry *rdp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1383
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1384
		if (dir == -1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1385
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1386
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1387
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1388
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1389
		tmp = (_opt.road_side << 4) + dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1390
		rdp = _road_drive_data[tmp];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1391
3421
8ab76c47c72c (svn r4246) -Codechange. Replaced about 100 occurences of '16' by TILE_SIZE
celestar
parents: 3404
diff changeset
  1392
		x = TileX(v->tile) * TILE_SIZE + rdp[1].x;
8ab76c47c72c (svn r4246) -Codechange. Replaced about 100 occurences of '16' by TILE_SIZE
celestar
parents: 3404
diff changeset
  1393
		y = TileY(v->tile) * TILE_SIZE + rdp[1].y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1394
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
  1395
		newdir = RoadVehGetSlidingDirection(v, x, y);
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1396
		if (RoadVehFindCloseTo(v, x, y, newdir) != NULL) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1397
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1398
		r = VehicleEnterTile(v, v->tile, x, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1399
		if (r & 8) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1400
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1401
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1402
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1403
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1404
		v->u.road.state = tmp & ~16;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1405
		v->u.road.frame = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1406
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1407
		if (newdir != v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1408
			v->direction = newdir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1409
			v->cur_speed -= v->cur_speed >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1410
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1411
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1412
		v->cur_image = GetRoadVehImage(v, newdir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1413
		UpdateRoadVehDeltaXY(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1414
		RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1415
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1416
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1417
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1418
	x = (v->x_pos & ~15) + (rd.x & 15);
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1419
	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
  1420
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1421
	new_dir = RoadVehGetSlidingDirection(v, x, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1422
3138
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1423
	if (!IS_BYTE_INSIDE(v->u.road.state, 0x20, 0x30)) {
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1424
		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
  1425
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1426
		if (u != NULL) {
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1427
			if (v->u.road.overtaking == 0) RoadVehCheckOvertake(v, u);
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1428
			return;
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1429
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1430
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1431
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1432
	old_dir = v->direction;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1433
	if (new_dir != old_dir) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1434
		v->direction = new_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1435
		v->cur_speed -= (v->cur_speed >> 2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1436
		if (old_dir != v->u.road.state) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1437
			v->cur_image = GetRoadVehImage(v, new_dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1438
			UpdateRoadVehDeltaXY(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1439
			SetRoadVehPosition(v, v->x_pos, v->y_pos);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1440
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1441
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1442
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1443
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1444
	if (v->u.road.state >= 0x20 &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1445
			_road_veh_data_1[v->u.road.state - 0x20 + (_opt.road_side<<4)] == v->u.road.frame) {
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1446
		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
  1447
		Station* st = GetStationByTile(v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1448
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1449
		if (v->current_order.type != OT_LEAVESTATION &&
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1450
				v->current_order.type != OT_GOTO_DEPOT) {
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1451
			Order old_order;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1452
3054
299ca9034442 (svn r3643) Remove a local variable which is just a pointer to a road stop status field and had the sensible name 'b'; instead directly use the road stop status field
tron
parents: 3033
diff changeset
  1453
			CLRBIT(rs->status, 7);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1454
3315
1f65f8260092 (svn r4079) Add GetSation{Index,ByTile}() to get the station index resp. the station from a tile
tron
parents: 3236
diff changeset
  1455
			v->last_station_visited = GetStationIndex(v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1456
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1457
			RoadVehArrivesAt(v, st);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1458
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1459
			old_order = v->current_order;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1460
			v->current_order.type = OT_LOADING;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1461
			v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1462
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1463
			if (old_order.type == OT_GOTO_STATION &&
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1464
					v->current_order.station == v->last_station_visited) {
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1465
				v->current_order.flags =
1935
f43f062c9498 (svn r2441) -Feature: You can now give transfer order to set up feeder systems
celestar
parents: 1927
diff changeset
  1466
					(old_order.flags & (OF_FULL_LOAD | OF_UNLOAD | OF_TRANSFER)) | OF_NON_STOP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1467
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1468
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1469
			SET_EXPENSES_TYPE(EXPENSES_ROADVEH_INC);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1470
			if (LoadUnloadVehicle(v)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1471
				InvalidateWindow(WC_ROADVEH_LIST, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1472
				MarkRoadVehDirty(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1473
			}
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
  1474
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1475
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1476
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1477
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1478
		if (v->current_order.type != OT_GOTO_DEPOT) {
3054
299ca9034442 (svn r3643) Remove a local variable which is just a pointer to a road stop status field and had the sensible name 'b'; instead directly use the road stop status field
tron
parents: 3033
diff changeset
  1479
			if (HASBIT(rs->status, 7)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1480
				v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1481
				return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1482
			}
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1483
			v->current_order.type = OT_NOTHING;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1484
			v->current_order.flags = 0;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1485
			ClearSlot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1486
		}
3054
299ca9034442 (svn r3643) Remove a local variable which is just a pointer to a road stop status field and had the sensible name 'b'; instead directly use the road stop status field
tron
parents: 3033
diff changeset
  1487
		SETBIT(rs->status, 7);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1488
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1489
		if (rs == v->u.road.slot) {
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1490
			//we have arrived at the correct station
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1491
			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
  1492
		} else if (v->u.road.slot != NULL) {
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1493
			//we have arrived at the wrong station
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1494
			//XXX The question is .. what to do? Actually we shouldn't be here
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1495
			//but I guess we need to clear the slot
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1496
			DEBUG(ms, 0) ("Multistop: 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
  1497
			if (v->tile != v->dest_tile) {
3630
0f7828d892ae (svn r4529) - Codechange: Use proper naming for hex numbers in debug prints eg. 0xF3A6. Use fixed lengths where applicable (newgrf). Unfortunately '%#X' is unusable since it gives 0XFF3 and '%#x' gives 0xff3 while we want 0xFF3 :P
Darkvater
parents: 3612
diff changeset
  1498
				DEBUG(ms, 0) ("Multistop: -- 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
  1499
			}
2bd76fda80c1 (svn r3754) Miscellaneous changes: indentation, reduce variable scope, constness, avoid some more function pointer casts
tron
parents: 3137
diff changeset
  1500
			if (v->dest_tile != v->u.road.slot->xy) {
3630
0f7828d892ae (svn r4529) - Codechange: Use proper naming for hex numbers in debug prints eg. 0xF3A6. Use fixed lengths where applicable (newgrf). Unfortunately '%#X' is unusable since it gives 0XFF3 and '%#x' gives 0xff3 while we want 0xFF3 :P
Darkvater
parents: 3612
diff changeset
  1501
				DEBUG(ms, 0) ("Multistop: -- 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
  1502
			}
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1503
			if (v->current_order.type != OT_GOTO_STATION) {
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1504
				DEBUG(ms, 0) ("Multistop: -- Current order type (%d) is not OT_GOTO_STATION.", v->current_order.type);
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1505
			} else {
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1506
				if (v->current_order.station != st->index)
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1507
					DEBUG(ms, 0) ("Multistop: -- Current station %d is not target station in current_order.station (%d).",
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1508
							st->index, v->current_order.station);
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1509
			}
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1510
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1511
			DEBUG(ms, 0) ("           -- Force a slot clearing.");
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1512
			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
  1513
		}
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1514
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1515
		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
  1516
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1517
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1518
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1519
	r = VehicleEnterTile(v, v->tile, x, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1520
	if (r & 8) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1521
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1522
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1523
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1524
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
  1525
	if ((r & 4) == 0) v->u.road.frame++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1526
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1527
	v->cur_image = GetRoadVehImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1528
	UpdateRoadVehDeltaXY(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1529
	RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1530
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1531
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1532
void RoadVehEnterDepot(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1533
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1534
	v->u.road.state = 254;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1535
	v->vehstatus |= VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1536
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1537
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1538
578
86e352980acd (svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents: 555
diff changeset
  1539
	VehicleServiceInDepot(v);
86e352980acd (svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents: 555
diff changeset
  1540
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 378
diff changeset
  1541
	TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1542
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1543
	if (v->current_order.type == OT_GOTO_DEPOT) {
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1544
		Order t;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1545
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1546
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1547
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1548
		t = v->current_order;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1549
		v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1550
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1551
1024
9b06b01490a4 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 926
diff changeset
  1552
		// Part of the orderlist?
1530
caa16c506a22 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
  1553
		if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1554
			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
  1555
		} else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1556
			v->vehstatus |= VS_STOPPED;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1557
			if (v->owner == _local_player) {
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  1558
				SetDParam(0, v->unitnumber);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1559
				AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1560
					STR_9016_ROAD_VEHICLE_IS_WAITING,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1561
					NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1562
					v->index,
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
  1563
					0
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
  1564
				);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1565
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1566
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1567
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1568
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1569
	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
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
  1570
	InvalidateWindowClasses(WC_ROADVEH_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1571
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1572
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1573
static void AgeRoadVehCargo(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1574
{
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
  1575
	if (_age_cargo_skip_counter != 0) return;
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
  1576
	if (v->cargo_days != 255) v->cargo_days++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1577
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1578
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1579
void RoadVeh_Tick(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1580
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1581
	AgeRoadVehCargo(v);
1401
56d372a75b74 (svn r1905) - Fix: [ 1118810 ] openttd: ship_cmd.c:642 ... Assertion failed. Mapwrap fixed in ship_cmd.c (was implicitely ok before biggermaps).
Darkvater
parents: 1359
diff changeset
  1582
	RoadVehController(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1583
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1584
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1585
static void CheckIfRoadVehNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1586
{
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1587
	const Depot* depot;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1588
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
  1589
	if (_patches.servint_roadveh == 0) return;
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
  1590
	if (!VehicleNeedsService(v)) return;
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1591
	if (v->vehstatus & VS_STOPPED) return;
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
  1592
	if (_patches.gotodepot && VehicleHasDepotOrders(v)) return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1593
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1594
	// Don't interfere with a depot visit scheduled by the user, or a
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1595
	// depot visit by the order list.
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1596
	if (v->current_order.type == OT_GOTO_DEPOT &&
1530
caa16c506a22 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
  1597
			(v->current_order.flags & (OF_HALT_IN_DEPOT | OF_PART_OF_ORDERS)) != 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1598
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1599
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
  1600
	// If we already got a slot at a stop, use that FIRST, and go to a depot later
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
  1601
	if (v->u.road.slot != NULL) return;
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1602
3612
f83a816c2099 (svn r4507) Just a comment
celestar
parents: 3611
diff changeset
  1603
	// XXX If we already have a depot order, WHY do we search over and over?
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1604
	depot = FindClosestRoadDepot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1605
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1606
	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
  1607
		if (v->current_order.type == OT_GOTO_DEPOT) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1608
			v->current_order.type = OT_DUMMY;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1609
			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
  1610
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1611
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1612
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1613
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1614
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1615
	if (v->current_order.type == OT_GOTO_DEPOT &&
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1616
			v->current_order.flags & OF_NON_STOP &&
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1617
			!CHANCE16(1, 20)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1618
		return;
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1619
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1620
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1621
	v->current_order.type = OT_GOTO_DEPOT;
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1622
	v->current_order.flags = OF_NON_STOP;
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1623
	v->current_order.station = depot->index;
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1299
diff changeset
  1624
	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
  1625
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1626
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1627
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1628
void OnNewDay_RoadVeh(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1629
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1630
	int32 cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1631
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1632
3017
915fae59d5e0 (svn r3597) Miscellaneous (I like that word) changes: Fix some indentation, add consts, reduce indentation level by short-circuit logic, convert if cascades to switch, whitespace, bracing, plus some minor stuff
tron
parents: 3009
diff changeset
  1633
	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
  1634
	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
  1635
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1636
	AgeVehicle(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1637
	CheckIfRoadVehNeedsService(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1638
3140
69cb681c6d86 (svn r3758) Remove the news validation callback. It is superseded by r3757.
tron
parents: 3139
diff changeset
  1639
	CheckOrders(v);
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 11
diff changeset
  1640
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1641
	//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
  1642
	if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot != NULL && v->u.road.slot_age-- == 0) {
3630
0f7828d892ae (svn r4529) - Codechange: Use proper naming for hex numbers in debug prints eg. 0xF3A6. Use fixed lengths where applicable (newgrf). Unfortunately '%#X' is unusable since it gives 0XFF3 and '%#x' gives 0xff3 while we want 0xFF3 :P
Darkvater
parents: 3612
diff changeset
  1643
		DEBUG(ms, 2) ("Multistop: Slot expired for vehicle %d (index %d) at stop 0x%X",
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1644
			v->unitnumber, v->index, v->u.road.slot->xy);
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1645
		ClearSlot(v);
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1646
	}
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1647
3236
36e8eb3ae55a (svn r3909) [multistop]
celestar
parents: 3186
diff changeset
  1648
	if (v->vehstatus & VS_STOPPED) return;
36e8eb3ae55a (svn r3909) [multistop]
celestar
parents: 3186
diff changeset
  1649
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1650
	/* update destination */
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1651
	if (v->current_order.type == OT_GOTO_STATION && v->u.road.slot == NULL && !(v->vehstatus & VS_CRASHED)) {
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1652
		RoadStop *rs;
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1653
		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
  1654
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 904
diff changeset
  1655
		st = GetStation(v->current_order.station);
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1656
		rs = GetPrimaryRoadStop(st, v->cargo_type == CT_PASSENGERS ? RS_BUS : RS_TRUCK);
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1657
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1658
		if (rs != NULL) {
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1659
			if (DistanceManhattan(v->tile, st->xy) < 16) {
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1660
				uint dist, badness;
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1661
				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
  1662
3630
0f7828d892ae (svn r4529) - Codechange: Use proper naming for hex numbers in debug prints eg. 0xF3A6. Use fixed lengths where applicable (newgrf). Unfortunately '%#X' is unusable since it gives 0XFF3 and '%#x' gives 0xff3 while we want 0xFF3 :P
Darkvater
parents: 3612
diff changeset
  1663
				DEBUG(ms, 2) ("Multistop: Attempting to obtain a slot for vehicle %d (index %d) at station %d (0x%X)", v->unitnumber,
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1664
						v->index, st->index, st->xy);
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1665
				/* Now we find the nearest road stop that has a free slot */
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1666
				for (; rs != NULL; rs = rs->next) {
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1667
					dist = RoadFindPathToStop(v, rs->xy);
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1668
					if (dist == UINT_MAX) {
3630
0f7828d892ae (svn r4529) - Codechange: Use proper naming for hex numbers in debug prints eg. 0xF3A6. Use fixed lengths where applicable (newgrf). Unfortunately '%#X' is unusable since it gives 0XFF3 and '%#x' gives 0xff3 while we want 0xFF3 :P
Darkvater
parents: 3612
diff changeset
  1669
						DEBUG(ms, 4) (" ---- stop 0x%X is not reachable, not treating further", rs->xy);
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1670
						continue;
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1671
					}
3915
281c7ebd27e0 (svn r5033) -CodeChange: [YAPF] RoadFindPathToStop() can now use YAPF for multistop handling.
KUDr
parents: 3900
diff changeset
  1672
					badness = (rs->num_vehicles + 1) * (rs->num_vehicles + 1) + dist;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1673
3630
0f7828d892ae (svn r4529) - Codechange: Use proper naming for hex numbers in debug prints eg. 0xF3A6. Use fixed lengths where applicable (newgrf). Unfortunately '%#X' is unusable since it gives 0XFF3 and '%#x' gives 0xff3 while we want 0xFF3 :P
Darkvater
parents: 3612
diff changeset
  1674
					DEBUG(ms, 4) (" ---- stop 0x%X has %d vehicle%s waiting", rs->xy, rs->num_vehicles, rs->num_vehicles == 1 ? "":"s");
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1675
					DEBUG(ms, 4) (" ---- Distance is %u", dist);
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1676
					DEBUG(ms, 4) (" -- Badness %u", badness);
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1677
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1678
					if (badness < minbadness) {
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1679
						best = rs;
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1680
						minbadness = badness;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1681
					}
3033
e4f7c60a1742 (svn r3613) Some more const, indentation, whitespace and similar stuff
tron
parents: 3032
diff changeset
  1682
				}
1217
ab9f02a224ab (svn r1721) -Feature: It is now possible to build multiple road stations (up to 8) on
celestar
parents: 1209
diff changeset
  1683
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1684
				if (best != NULL) {
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1685
					best->num_vehicles++;
3630
0f7828d892ae (svn r4529) - Codechange: Use proper naming for hex numbers in debug prints eg. 0xF3A6. Use fixed lengths where applicable (newgrf). Unfortunately '%#X' is unusable since it gives 0XFF3 and '%#x' gives 0xff3 while we want 0xFF3 :P
Darkvater
parents: 3612
diff changeset
  1686
					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
  1687
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1688
					v->u.road.slot = best;
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1689
					v->dest_tile = best->xy;
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1690
					v->u.road.slot_age = 14;
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1691
				} else {
3431
0d7fa19d0b4a (svn r4259) -[multistop] Fix/Feature/Codechange:
celestar
parents: 3421
diff changeset
  1692
					DEBUG(ms, 3) (" -- Could not find a suitable stop");
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1693
				}
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1694
			} else {
3630
0f7828d892ae (svn r4529) - Codechange: Use proper naming for hex numbers in debug prints eg. 0xF3A6. Use fixed lengths where applicable (newgrf). Unfortunately '%#X' is unusable since it gives 0XFF3 and '%#x' gives 0xff3 while we want 0xFF3 :P
Darkvater
parents: 3612
diff changeset
  1695
				DEBUG(ms, 5) ("Multistop: --- 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
  1696
						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
  1697
			}
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1698
		} else {
3630
0f7828d892ae (svn r4529) - Codechange: Use proper naming for hex numbers in debug prints eg. 0xF3A6. Use fixed lengths where applicable (newgrf). Unfortunately '%#X' is unusable since it gives 0XFF3 and '%#x' gives 0xff3 while we want 0xFF3 :P
Darkvater
parents: 3612
diff changeset
  1699
			DEBUG(ms, 4) ("Multistop: No road stop for vehicle %d (index %d) at station %d (0x%X)",
3123
a82dd124b3f2 (svn r3730) Multistop modifications:
celestar
parents: 3092
diff changeset
  1700
					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
  1701
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1702
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1703
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
  1704
	cost = 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
  1705
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1706
	v->profit_this_year -= cost >> 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1707
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1708
	SET_EXPENSES_TYPE(EXPENSES_ROADVEH_RUN);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1709
	SubtractMoneyFromPlayerFract(v->owner, cost);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1710
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1711
	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
  1712
	InvalidateWindowClasses(WC_ROADVEH_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1713
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1714
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1715
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1053
diff changeset
  1716
void RoadVehiclesYearlyLoop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1717
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1718
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1719
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1720
	FOR_ALL_VEHICLES(v) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1721
		if (v->type == VEH_Road) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1722
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1723
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1724
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1725
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1726
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1727
}