src/ship_cmd.cpp
author richk
Tue, 17 Jun 2008 13:22:13 +0000
branchNewGRF_ports
changeset 10994 cd9968b6f96b
parent 10991 d8811e327d12
permissions -rw-r--r--
(svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
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
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
     3
/** @file ship_cmd.cpp Handling of ships. */
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1802
diff changeset
     6
#include "openttd.h"
3961
e2783f244ac6 (svn r5120) Add IsShipInDepot{Stopped,}() and remove some redundant checks
tron
parents: 3955
diff changeset
     7
#include "ship.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
     8
#include "tile_cmd.h"
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
     9
#include "landscape.h"
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
    10
#include "timetable.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    11
#include "command_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "pathfind.h"
3442
c494e7246d7b (svn r4272) -Codechange: Moved the map-accessing stuff from station.h into station_map.h
celestar
parents: 3421
diff changeset
    13
#include "station_map.h"
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    14
#include "station_base.h"
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    15
#include "news_func.h"
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    16
#include "engine_func.h"
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    17
#include "engine_base.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    18
#include "player_func.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    19
#include "player_base.h"
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    20
#include "npf.h"
10242
52b4a9006029 (svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents: 10210
diff changeset
    21
#include "depot_base.h"
52b4a9006029 (svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents: 10210
diff changeset
    22
#include "depot_func.h"
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    23
#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: 2916
diff changeset
    24
#include "newgrf_engine.h"
3454
c2447e8c1e23 (svn r4289) -Codechange: Make use of water_map accessors for ships. Make a mental note to clean ship_cmd
celestar
parents: 3442
diff changeset
    25
#include "water_map.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
    26
#include "yapf/yapf.h"
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
    27
#include "debug.h"
3989
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
    28
#include "newgrf_callbacks.h"
4244
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
    29
#include "newgrf_text.h"
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
    30
#include "newgrf_sound.h"
6223
92d2073c3d7e (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 6195
diff changeset
    31
#include "spritecache.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    32
#include "strings_func.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    33
#include "functions.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    34
#include "window_func.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    35
#include "date_func.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    36
#include "vehicle_func.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    37
#include "sound_func.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    38
#include "variables.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    39
#include "autoreplace_gui.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    40
#include "gfx_func.h"
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    41
#include "settings_type.h"
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
    42
#include "order_func.h"
10274
b3c58f3df92b (svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents: 10242
diff changeset
    43
#include "effectvehicle_func.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    44
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
    45
#include "table/strings.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    49
static const TrackBits _ship_sometracks[4] = {
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    50
	TRACK_BIT_X | TRACK_BIT_LOWER | TRACK_BIT_LEFT,  // 0x19, // DIAGDIR_NE
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    51
	TRACK_BIT_Y | TRACK_BIT_UPPER | TRACK_BIT_LEFT,  // 0x16, // DIAGDIR_SE
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    52
	TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_RIGHT, // 0x25, // DIAGDIR_SW
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    53
	TRACK_BIT_Y | TRACK_BIT_LOWER | TRACK_BIT_RIGHT, // 0x2A, // DIAGDIR_NW
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    54
};
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
    55
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
    56
static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
    57
{
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
    58
	return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    61
static SpriteID GetShipIcon(EngineID engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
{
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    63
	uint8 spritenum = ShipVehInfo(engine)->image_index;
0
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
	if (is_custom_sprite(spritenum)) {
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    66
		SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    67
		if (sprite != 0) return sprite;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    69
		spritenum = GetEngine(engine)->image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
	}
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    71
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    72
	return 6 + _ship_sprites[spritenum];
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    73
}
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    74
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    75
void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal)
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    76
{
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    77
	DrawSprite(GetShipIcon(engine), pal, x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
6223
92d2073c3d7e (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 6195
diff changeset
    80
/** Get the size of the sprite of a ship sprite heading west (used for lists)
92d2073c3d7e (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 6195
diff changeset
    81
 * @param engine The engine to get the sprite from
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
    82
 * @param width The width of the sprite
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
    83
 * @param height The height of the sprite
6223
92d2073c3d7e (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 6195
diff changeset
    84
 */
92d2073c3d7e (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 6195
diff changeset
    85
void GetShipSpriteSize(EngineID engine, uint &width, uint &height)
92d2073c3d7e (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 6195
diff changeset
    86
{
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    87
	const Sprite *spr = GetSprite(GetShipIcon(engine));
6223
92d2073c3d7e (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 6195
diff changeset
    88
92d2073c3d7e (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 6195
diff changeset
    89
	width  = spr->width;
92d2073c3d7e (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 6195
diff changeset
    90
	height = spr->height;
92d2073c3d7e (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 6195
diff changeset
    91
}
92d2073c3d7e (svn r8661) -Fix: [depot windows] Enlarge the blocks in the depot window if a sprite is too big to fit (ships and aircraft only)
bjarni
parents: 6195
diff changeset
    92
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    93
SpriteID Ship::GetImage(Direction direction) const
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
{
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    95
	uint8 spritenum = this->spritenum;
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
	if (is_custom_sprite(spritenum)) {
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    98
		SpriteID sprite = GetCustomVehicleSprite(this, direction);
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
    99
		if (sprite != 0) return sprite;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   101
		spritenum = GetEngine(this->engine_type)->image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
	}
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   103
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
	return _ship_sprites[spritenum] + direction;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   107
static const Depot* FindClosestShipDepot(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
{
10991
d8811e327d12 (svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents: 10731
diff changeset
   109
	if (_settings_game.pf.pathfinder_for_ships == VPF_NPF) { /* NPF is used */
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   110
		Trackdir trackdir = GetVehicleTrackdir(v);
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   111
		NPFFoundTargetData ftd = NPFRouteToDepotTrialError(v->tile, trackdir, false, TRANSPORT_WATER, 0, v->owner, INVALID_RAILTYPES);
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   112
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   113
		if (ftd.best_bird_dist == 0) return GetDepotByTile(ftd.node.tile); /* Found target */
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   114
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   115
		return NULL; /* Did not find target */
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   116
	}
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   117
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   118
	/* OPF or YAPF - find the closest depot */
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   119
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   120
	const Depot* depot;
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   121
	const Depot* best_depot = NULL;
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   122
	uint best_dist = UINT_MAX;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   124
	FOR_ALL_DEPOTS(depot) {
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   125
		TileIndex tile = depot->xy;
10242
52b4a9006029 (svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents: 10210
diff changeset
   126
		if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   127
			uint dist = DistanceManhattan(tile, v->tile);
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   128
			if (dist < best_dist) {
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   129
				best_dist = dist;
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   130
				best_depot = depot;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
	}
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   134
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
	return best_depot;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
static void CheckIfShipNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
{
10991
d8811e327d12 (svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents: 10731
diff changeset
   140
	if (_settings_game.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
6868
7eb395287b3d (svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents: 6800
diff changeset
   141
	if (v->IsInDepot()) {
4529
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   142
		VehicleServiceInDepot(v);
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   143
		return;
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   144
	}
2bfac24af02a (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   145
6868
7eb395287b3d (svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents: 6800
diff changeset
   146
	const Depot *depot = FindClosestShipDepot(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1282
diff changeset
   148
	if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) {
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   149
		if (v->current_order.IsType(OT_GOTO_DEPOT)) {
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   150
			v->current_order.MakeDummy();
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   151
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   156
	v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
1313
bba6afb8a995 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1282
diff changeset
   157
	v->dest_tile = depot->xy;
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   158
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   161
void Ship::OnNewDay()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
{
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   163
	if ((++this->day_counter & 7) == 0)
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   164
		DecreaseVehicleValue(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   166
	CheckVehicleBreakdown(this);
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   167
	AgeVehicle(this);
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   168
	CheckIfShipNeedsService(this);
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 11
diff changeset
   169
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   170
	CheckOrders(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   172
	if (this->running_ticks == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   174
	CommandCost cost(EXPENSES_SHIP_RUN, GetVehicleProperty(this, 0x0F, ShipVehInfo(this->engine_type)->running_cost) * _price.ship_running * this->running_ticks / (364 * DAY_TICKS));
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   175
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   176
	this->profit_this_year -= cost.GetCost();
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   177
	this->running_ticks = 0;
6877
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   178
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   179
	SubtractMoneyFromPlayerFract(this->owner, cost);
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   180
889301acc299 (svn r12053) [NewGRF_ports] -Sync: with trunk r11949:12050.
rubidium
parents: 6872
diff changeset
   181
	InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   182
	/* we need this for the profit */
1055
3aae1d6f12aa (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
   183
	InvalidateWindowClasses(WC_SHIPS_LIST);
0
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
static void HandleBrokenShip(Vehicle *v)
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
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   190
		v->cur_speed = 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
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
			v->breakdowns_since_last_service++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   194
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   195
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   197
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   198
		if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
10991
d8811e327d12 (svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents: 10731
diff changeset
   199
			SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ?
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   200
				SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v);
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   201
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
		if (!(v->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
			Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
10274
b3c58f3df92b (svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents: 10242
diff changeset
   205
			if (u != NULL) u->u.effect.animation_state = v->breakdown_delay * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   209
	if (!(v->tick_counter & 1)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
		if (!--v->breakdown_delay) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
			v->breakdown_ctr = 0;
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
   212
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   216
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   217
void Ship::MarkDirty()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
{
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   219
	this->cur_image = this->GetImage(this->direction);
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   220
	MarkSingleVehicleDirty(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   222
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   223
static void PlayShipSound(const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
{
4656
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   225
	if (!PlayVehicleSound(v, VSE_START)) {
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   226
		SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v);
acffecd6f484 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   227
	}
0
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
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   230
void Ship::PlayLeaveStationSound() const
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   231
{
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   232
	PlayShipSound(this);
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   233
}
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   234
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   235
TileIndex Ship::GetOrderStationLocation(StationID station)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
{
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   237
	if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
3005
db4007baa0ab (svn r3585) Similar changes to the order handlers of the other vehicle types like r3584
tron
parents: 2962
diff changeset
   238
10210
a2131f7a315d (svn r12742) [NewGRF_ports] -Sync: with trunk r12673:12705.
richk
parents: 10200
diff changeset
   239
	const Station *st = GetStation(station);
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   240
	if (st->dock_tile != 0) {
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   241
		return TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   242
	} else {
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   243
		this->cur_order_index++;
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   244
		return 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   248
void Ship::UpdateDeltaXY(Direction direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
{
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   250
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   251
	static const uint32 _delta_xy_table[8] = {
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   252
		MKIT( 6,  6,  -3,  -3),
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   253
		MKIT( 6, 32,  -3, -16),
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   254
		MKIT( 6,  6,  -3,  -3),
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   255
		MKIT(32,  6, -16,  -3),
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   256
		MKIT( 6,  6,  -3,  -3),
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   257
		MKIT( 6, 32,  -3, -16),
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   258
		MKIT( 6,  6,  -3,  -3),
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   259
		MKIT(32,  6, -16,  -3),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
	};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
#undef MKIT
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   262
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   263
	uint32 x = _delta_xy_table[direction];
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   264
	this->x_offs        = GB(x,  0, 8);
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   265
	this->y_offs        = GB(x,  8, 8);
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   266
	this->x_extent      = GB(x, 16, 8);
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   267
	this->y_extent      = GB(x, 24, 8);
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   268
	this->z_extent      = 6;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
4725
f7284b86833f (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
   271
void RecalcShipStuff(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   272
{
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   273
	v->UpdateDeltaXY(v->direction);
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   274
	v->cur_image = v->GetImage(v->direction);
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   275
	v->MarkDirty();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   277
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   278
909
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   279
static const TileIndexDiffC _ship_leave_depot_offs[] = {
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   280
	{-1,  0},
81bc9ef93f50 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   281
	{ 0, -1}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
static void CheckShipLeaveDepot(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
{
6868
7eb395287b3d (svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents: 6800
diff changeset
   286
	if (!v->IsInDepot()) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   287
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   288
	TileIndex tile = v->tile;
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   289
	Axis axis = GetShipDepotAxis(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   290
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   291
	/* Check first (north) side */
3454
c2447e8c1e23 (svn r4289) -Codechange: Make use of water_map accessors for ships. Make a mental note to clean ship_cmd
celestar
parents: 3442
diff changeset
   292
	if (_ship_sometracks[axis] & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   293
		v->direction = ReverseDir(AxisToDirection(axis));
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   294
	/* Check second (south) side */
3454
c2447e8c1e23 (svn r4289) -Codechange: Make use of water_map accessors for ships. Make a mental note to clean ship_cmd
celestar
parents: 3442
diff changeset
   295
	} else if (_ship_sometracks[axis + 2] & GetTileShipTrackStatus(TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   296
		v->direction = AxisToDirection(axis);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   297
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
	}
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   300
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   301
	v->u.ship.state = AxisToTrackBits(axis);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
	v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   303
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
	v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
	RecalcShipStuff(v);
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
	PlayShipSound(v);
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
   308
	VehicleServiceInDepot(v);
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4725
diff changeset
   309
	InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
1055
3aae1d6f12aa (svn r1556) -Fix: Vehicle list updates should now really work
Celestar
parents: 1053
diff changeset
   310
	InvalidateWindowClasses(WC_SHIPS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   311
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   313
static bool ShipAccelerate(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
	uint spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
	byte t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   318
	spd = min(v->cur_speed + 1, GetVehicleProperty(v, 0x0B, v->max_speed));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   320
	/*updates statusbar only if speed have changed to save CPU time */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   321
	if (spd != v->cur_speed) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   322
		v->cur_speed = spd;
10991
d8811e327d12 (svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents: 10731
diff changeset
   323
		if (_settings_client.gui.vehicle_speed)
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   324
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   325
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   326
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   327
	/* Decrease somewhat when turning */
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   328
	if (!(v->direction & 1)) spd = spd * 3 / 4;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   330
	if (spd == 0) return false;
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   331
	if ((byte)++spd == 0) return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
	v->progress = (t = v->progress) - (byte)spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   334
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   335
	return (t < v->progress);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   336
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   337
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   338
static CommandCost EstimateShipCost(EngineID engine_type)
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   339
{
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   340
	return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine_type, 0x0A, ShipVehInfo(engine_type)->base_cost) * (_price.ship_base >> 3) >> 5);
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   341
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   342
2630
35249d2ded3e (svn r3172) static, const
tron
parents: 2618
diff changeset
   343
static void ShipArrivesAt(const Vehicle* v, Station* st)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
	/* Check if station was ever visited before */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
	if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   347
		st->had_vehicle_of_type |= HVOT_SHIP;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   348
534
17ab2f22ff74 (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   349
		SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
		AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   351
			STR_9833_CITIZENS_CELEBRATE_FIRST,
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   352
			(v->owner == _local_player) ? NS_ARRIVAL_PLAYER : NS_ARRIVAL_OTHER,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
			v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
			0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   358
struct PathFindShip {
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
   359
	TileIndex skiptile;
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
   360
	TileIndex dest_coords;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   361
	uint best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   362
	uint best_length;
6574
e1d1a12faaf7 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6573
diff changeset
   363
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   364
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   365
static bool ShipTrackFollower(TileIndex tile, PathFindShip *pfs, int track, uint length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
{
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   367
	/* Found dest? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
	if (tile == pfs->dest_coords) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
		pfs->best_bird_dist = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   370
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   371
		pfs->best_length = minu(pfs->best_length, length);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   375
	/* Skip this tile in the calculation */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
	if (tile != pfs->skiptile) {
1245
768d9bc95aaa (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1237
diff changeset
   377
		pfs->best_bird_dist = minu(pfs->best_bird_dist, DistanceMaxPlusManhattan(pfs->dest_coords, tile));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   378
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   379
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   383
static const byte _ship_search_directions[6][4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
	{ 0, 9, 2, 9 },
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
	{ 9, 1, 9, 3 },
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
	{ 9, 0, 3, 9 },
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
	{ 1, 9, 9, 2 },
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
	{ 3, 2, 9, 9 },
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
	{ 9, 9, 1, 0 },
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
};
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
static const byte _pick_shiptrack_table[6] = {1, 3, 2, 2, 0, 0};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   394
static uint FindShipTrack(Vehicle *v, TileIndex tile, DiagDirection dir, TrackBits bits, TileIndex skiptile, Track *track)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   396
	PathFindShip pfs;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   397
	Track i, best_track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
	uint best_bird_dist = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
	uint best_length    = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
	uint r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
	byte ship_dir = v->direction & 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
	pfs.dest_coords = v->dest_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
	pfs.skiptile = skiptile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   405
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   406
	best_track = INVALID_TRACK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
	do {
5849
58039c9dc565 (svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents: 5838
diff changeset
   409
		i = RemoveFirstTrack(&bits);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   410
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
		pfs.best_bird_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
		pfs.best_length = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   414
		FollowTrack(tile, PATHFIND_FLAGS_SHIP_MODE | PATHFIND_FLAGS_DISABLE_TILE_HASH, TRANSPORT_WATER, 0, (DiagDirection)_ship_search_directions[i][dir], (TPFEnumProc*)ShipTrackFollower, NULL, &pfs);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   415
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   416
		if (best_track != INVALID_TRACK) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
			if (pfs.best_bird_dist != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
				/* neither reached the destination, pick the one with the smallest bird dist */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
				if (pfs.best_bird_dist > best_bird_dist) goto bad;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   420
				if (pfs.best_bird_dist < best_bird_dist) goto good;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
				if (pfs.best_length > best_length) goto bad;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   423
				if (pfs.best_length < best_length) goto good;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   425
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   426
			/* if we reach this position, there's two paths of equal value so far.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
			 * pick one randomly. */
2150
010d923a81a9 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2049
diff changeset
   428
			r = GB(Random(), 0, 8);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   429
			if (_pick_shiptrack_table[i] == ship_dir) r += 80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
			if (_pick_shiptrack_table[best_track] == ship_dir) r -= 80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
			if (r <= 127) goto bad;
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
good:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
		best_track = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
		best_bird_dist = pfs.best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
		best_length = pfs.best_length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
bad:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
	} while (bits != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   441
	*track = best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   442
	return best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   443
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   445
static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailTypes railtypes)
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   446
{
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   447
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   448
	void* perf = NpfBeginInterval();
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   449
	NPFFoundTargetData ret = NPFRouteToStationOrTile(tile, trackdir, ignore_start_tile, target, type, 0, owner, railtypes);
3900
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   450
	int t = NpfEndInterval(perf);
5568
75f13d7bfaed (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
   451
	DEBUG(yapf, 4, "[NPFW] %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
   452
	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
   453
}
4984308f9125 (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   454
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   455
/** returns the track to choose on the next tile, or -1 when it's better to
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   456
 * reverse. The tile given is the tile we are about to enter, enterdir is the
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   457
 * direction in which we are entering the tile */
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   458
static Track ChooseShipTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
{
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   460
	assert(IsValidDiagDirection(enterdir));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
10991
d8811e327d12 (svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents: 10731
diff changeset
   462
	switch (_settings_game.pf.pathfinder_for_ships) {
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   463
		case VPF_YAPF: { /* YAPF */
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   464
			Trackdir trackdir = YapfChooseShipTrack(v, tile, enterdir, tracks);
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   465
			if (trackdir != INVALID_TRACKDIR) return TrackdirToTrack(trackdir);
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   466
		} break;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   467
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   468
		case VPF_NPF: { /* NPF */
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   469
			NPFFindStationOrTileData fstd;
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   470
			Trackdir trackdir = GetVehicleTrackdir(v);
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   471
			assert(trackdir != INVALID_TRACKDIR); // Check that we are not in a depot
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   472
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   473
			NPFFillWithOrderData(&fstd, v);
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   474
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   475
			NPFFoundTargetData ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_WATER, v->owner, INVALID_RAILTYPES);
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   476
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: 1530
diff changeset
   477
			/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   478
			 * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   479
			 * we did not find our target, but ftd.best_trackdir contains the direction leading
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   480
			 * to the tile closest to our target. */
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   481
			if (ftd.best_trackdir != 0xff) return TrackdirToTrack(ftd.best_trackdir); /* TODO: Wrapper function? */
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   482
		} break;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   483
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   484
		default:
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   485
		case VPF_OPF: { /* OPF */
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   486
			TileIndex tile2 = TILE_ADD(tile, -TileOffsByDiagDir(enterdir));
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   487
			Track track;
1247
01711347f9ac (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   488
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   489
			/* Let's find out how far it would be if we would reverse first */
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   490
			TrackBits b = GetTileShipTrackStatus(tile2) & _ship_sometracks[ReverseDiagDir(enterdir)] & v->u.ship.state;
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   491
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   492
			uint distr = UINT_MAX; // distance if we reversed
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   493
			if (b != 0) {
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   494
				distr = FindShipTrack(v, tile2, ReverseDiagDir(enterdir), b, tile, &track);
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   495
				if (distr != UINT_MAX) distr++; // penalty for reversing
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   496
			}
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   497
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   498
			/* And if we would not reverse? */
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   499
			uint dist = FindShipTrack(v, tile, enterdir, tracks, 0, &track);
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   500
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   501
			if (dist <= distr) return track;
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   502
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
	}
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   504
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   505
	return INVALID_TRACK; /* We could better reverse */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3147
diff changeset
   508
static const Direction _new_vehicle_direction_table[] = {
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   509
	DIR_N , DIR_NW, DIR_W , INVALID_DIR,
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   510
	DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3147
diff changeset
   511
	DIR_E , DIR_SE, DIR_S
0
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
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   514
static Direction ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   515
{
926
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
   516
	uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
   517
							TileX(new_tile) - TileX(old_tile) + 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   518
	assert(offs < 11 && offs != 3 && offs != 7);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   519
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   520
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   522
static Direction ShipGetNewDirection(Vehicle *v, int x, int y)
0
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
	uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   525
	assert(offs < 11 && offs != 3 && offs != 7);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   527
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   528
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   529
static inline TrackBits GetAvailShipTracks(TileIndex tile, int dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   530
{
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   531
	return GetTileShipTrackStatus(tile) & _ship_sometracks[dir];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   532
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   533
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   534
static const byte _ship_subcoord[4][6][3] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
		{15, 8, 1},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   537
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
		{15, 8, 2},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
		{15, 7, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
	},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
		{ 8, 0, 3},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
		{ 7, 0, 2},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
		{ 8, 0, 4},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   549
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
	},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
		{ 0, 8, 5},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
		{ 0, 7, 6},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   556
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
		{ 0, 8, 4},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   558
	},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   559
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
		{ 0, 0, 0},
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   561
		{ 8, 15, 7},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   562
		{ 0, 0, 0},
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   563
		{ 8, 15, 6},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   564
		{ 0, 0, 0},
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   565
		{ 7, 15, 0},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   569
static void ShipController(Vehicle *v)
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
	uint32 r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
	const byte *b;
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3147
diff changeset
   573
	Direction dir;
5838
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   574
	Track track;
9c3129cb019b (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5835
diff changeset
   575
	TrackBits tracks;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
	v->tick_counter++;
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   578
	v->current_order_time++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
	if (v->breakdown_ctr != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
		if (v->breakdown_ctr <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
			HandleBrokenShip(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   584
		}
10274
b3c58f3df92b (svn r12806) [NewGRF_ports] -Sync: with trunk r12773:12805.
richk
parents: 10242
diff changeset
   585
		if (!v->current_order.IsType(OT_LOADING)) v->breakdown_ctr--;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   588
	if (v->vehstatus & VS_STOPPED) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   590
	ProcessOrders(v);
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   591
	v->HandleLoading();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
10184
fcf5fb2548eb (svn r12715) [NewGRF_ports] -Sync: with trunk r12351:12644.
richk
parents: 6878
diff changeset
   593
	if (v->current_order.IsType(OT_LOADING)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   594
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
	CheckShipLeaveDepot(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   597
	if (!ShipAccelerate(v)) return;
0
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
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
6479
b228a94a1832 (svn r8897) -Fix
tron
parents: 6478
diff changeset
   601
	GetNewVehiclePosResult gp = GetNewVehiclePos(v);
10994
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   602
	if (v->u.ship.state != TRACK_BIT_WORMHOLE) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   603
		/* Not on a bridge */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   604
		if (gp.old_tile == gp.new_tile) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   605
			/* Staying in tile */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   606
			if (v->IsInDepot()) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   607
				gp.x = v->x_pos;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   608
				gp.y = v->y_pos;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   609
			} else {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   610
				/* Not inside depot */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   611
				r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   612
				if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
10994
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   614
				/* A leave station order only needs one tick to get processed, so we can
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   615
				* always skip ahead. */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   616
				if (v->current_order.IsType(OT_LEAVESTATION)) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   617
					v->current_order.Free();
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   618
					InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   619
				} else if (v->dest_tile != 0) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   620
					/* We have a target, let's see if we reached it... */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   621
					if (v->current_order.IsType(OT_GOTO_STATION) &&
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   622
							IsBuoyTile(v->dest_tile) &&
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   623
							DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   624
						/* We got within 3 tiles of our target buoy, so let's skip to our
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   625
						* next order */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   626
						UpdateVehicleTimetable(v, true);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   627
						v->cur_order_index++;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   628
						v->current_order.MakeDummy();
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   629
						InvalidateVehicleOrder(v);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   630
					} else {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   631
						/* Non-buoy orders really need to reach the tile */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   632
						if (v->dest_tile == gp.new_tile) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   633
							if (v->current_order.IsType(OT_GOTO_DEPOT)) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   634
								if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   635
									VehicleEnterDepot(v);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   636
									return;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   637
								}
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   638
							} else if (v->current_order.IsType(OT_GOTO_STATION)) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   639
								v->last_station_visited = v->current_order.GetDestination();
1751
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1698
diff changeset
   640
10994
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   641
								/* Process station in the orderlist. */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   642
								Station *st = GetStation(v->current_order.GetDestination());
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   643
								if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   644
									ShipArrivesAt(v, st);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   645
									v->BeginLoading();
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   646
								} else { // leave stations without docks right aways
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   647
									v->current_order.MakeLeaveStation();
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   648
									v->cur_order_index++;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   649
									InvalidateVehicleOrder(v);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   650
								}
1751
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1698
diff changeset
   651
							}
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1698
diff changeset
   652
						}
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1698
diff changeset
   653
					}
954dd2900ac9 (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1698
diff changeset
   654
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
			}
10994
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   656
		} else {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   657
			DiagDirection diagdir;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   658
			/* New tile */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   659
			if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   660
				goto reverse_direction;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   661
			}
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   662
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   663
			dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   664
			assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   665
			diagdir = DirToDiagDir(dir);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   666
			tracks = GetAvailShipTracks(gp.new_tile, diagdir);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   667
			if (tracks == TRACK_BIT_NONE) goto reverse_direction;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   668
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   669
			/* Choose a direction, and continue if we find one */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   670
			track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   671
			if (track == INVALID_TRACK) goto reverse_direction;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   672
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   673
			b = _ship_subcoord[diagdir][track];
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   674
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   675
			gp.x = (gp.x & ~0xF) | b[0];
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   676
			gp.y = (gp.y & ~0xF) | b[1];
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   677
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   678
			/* Call the landscape function and tell it that the vehicle entered the tile */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   679
			r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   680
			if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   681
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   682
			if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   683
				v->tile = gp.new_tile;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   684
				v->u.ship.state = TrackToTrackBits(track);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   685
			}
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   686
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   687
			v->direction = (Direction)b[2];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   688
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   689
	} else {
10994
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   690
		/* On a bridge */
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   691
		if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   692
			v->x_pos = gp.x;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   693
			v->y_pos = gp.y;
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   694
			VehiclePositionChanged(v);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   695
			if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v);
cd9968b6f96b (svn r13548) [NewGRF_ports] -Sync: with trunk r13412:13544.
richk
parents: 10991
diff changeset
   696
			return;
6320
5fc5a089b147 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 6319
diff changeset
   697
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   698
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   699
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   700
	/* update image of ship, as well as delta XY */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   701
	dir = ShipGetNewDirection(v, gp.x, gp.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   702
	v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   703
	v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   704
	v->z_pos = GetSlopeZ(gp.x, gp.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   705
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   706
getout:
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   707
	v->UpdateDeltaXY(dir);
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   708
	v->cur_image = v->GetImage(dir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   709
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   710
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   711
	return;
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
reverse_direction:
3157
40de8616c04c (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3147
diff changeset
   714
	dir = ReverseDir(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
	v->direction = dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
	goto getout;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   717
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
static void AgeShipCargo(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   720
{
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   721
	if (_age_cargo_skip_counter != 0) return;
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   722
	v->cargo.AgeCargo();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   723
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   725
void Ship::Tick()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   726
{
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   727
	if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   728
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   729
	AgeShipCargo(this);
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   730
	ShipController(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   732
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6502
diff changeset
   734
void ShipsYearlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   737
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   738
	FOR_ALL_VEHICLES(v) {
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   739
		if (v->type == VEH_SHIP) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   740
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   741
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   742
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   743
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   744
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   745
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   747
/** Build a ship.
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: 3476
diff changeset
   748
 * @param tile tile of depot where ship is built
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   749
 * @param flags type of operation
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   750
 * @param p1 ship 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: 3645
diff changeset
   751
 * @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
   752
 */
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   753
CommandCost CmdBuildShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   754
{
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   755
	CommandCost value;
1282
e7a73ee62d2f (svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents: 1266
diff changeset
   756
	UnitID unit_num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   757
	Engine *e;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   758
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   759
	if (!IsEngineBuildable(p1, VEH_SHIP, _current_player)) return_cmd_error(STR_SHIP_NOT_AVAILABLE);
1196
115f46e3807d (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1151
diff changeset
   760
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   761
	value = EstimateShipCost(p1);
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   762
	if (flags & DC_QUERY_COST) return value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   763
1443
d9c101f7634b (svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents: 1401
diff changeset
   764
	/* The ai_new queries the vehicle cost before building the route,
d9c101f7634b (svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents: 1401
diff changeset
   765
	 * so we must check against cheaters no sooner than now. --pasky */
10242
52b4a9006029 (svn r12774) [NewGRF_ports] -Sync: with trunk r12742:12772.
richk
parents: 10210
diff changeset
   766
	if (!IsShipDepotTile(tile)) 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
   767
	if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
1443
d9c101f7634b (svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents: 1401
diff changeset
   768
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   769
	unit_num = HasBit(p2, 0) ? 0 : GetFreeUnitNumber(VEH_SHIP);
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: 3645
diff changeset
   770
10991
d8811e327d12 (svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents: 10731
diff changeset
   771
	if (!Vehicle::AllocateList(NULL, 1) || unit_num > _settings_game.vehicle.max_ships)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   772
		return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   773
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   774
	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: 3476
diff changeset
   775
		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: 3476
diff changeset
   776
		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: 3476
diff changeset
   777
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   778
		const ShipVehicleInfo *svi = ShipVehInfo(p1);
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   779
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   780
		Vehicle *v = new Ship();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   781
		v->unitnumber = unit_num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   782
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   783
		v->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   784
		v->tile = tile;
3645
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3610
diff changeset
   785
		x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
86af43f87885 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3610
diff changeset
   786
		y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   787
		v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   788
		v->y_pos = y;
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   789
		v->z_pos = GetSlopeZ(x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   790
6878
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   791
		v->running_ticks = 0;
7d1ff2f621c7 (svn r12351) [NewGRF_ports] -Sync: with trunk r12051:12350.
richk
parents: 6877
diff changeset
   792
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   793
		v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   794
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   795
538
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   796
		v->spritenum = svi->image_index;
5dd99f3b7eca (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   797
		v->cargo_type = svi->cargo_type;
3870
766d7bd6c299 (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3816
diff changeset
   798
		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
   799
		v->cargo_cap = svi->capacity;
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   800
		v->value = value.GetCost();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   801
1266
9dc5638fe8cc (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
   802
		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
   803
		v->max_speed = svi->max_speed;
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
   804
		v->engine_type = p1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   805
1926
68d60188a22f (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1901
diff changeset
   806
		e = GetEngine(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   807
		v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   808
		v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   809
		v->max_age = e->lifelength * 366;
2564
371e0fa97e1e (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2557
diff changeset
   810
		_new_vehicle_id = v->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   811
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   812
		v->name = NULL;
6319
27e68b914d3d (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 6317
diff changeset
   813
		v->u.ship.state = TRACK_BIT_DEPOT;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   814
10991
d8811e327d12 (svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents: 10731
diff changeset
   815
		v->service_interval = _settings_game.vehicle.servint_ships;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
		v->date_of_last_service = _date;
4329
9759d5c52010 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   817
		v->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   818
		v->cur_image = 0x0E5E;
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: 2791
diff changeset
   819
		v->random_bits = VehicleRandomBits();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
6502
7ca154aca2b7 (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6501
diff changeset
   821
		v->vehicle_flags = 0;
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   822
		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
6502
7ca154aca2b7 (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6501
diff changeset
   823
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   824
		v->cargo_cap = GetVehicleProperty(v, 0x0D, svi->capacity);
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   825
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
		VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   827
4739
bd535b408617 (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4725
diff changeset
   828
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
10731
67db0d431d5e (svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents: 10724
diff changeset
   829
		InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   830
		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
   831
		if (IsLocalPlayer())
6800
6c09e1e86fcb (svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents: 6743
diff changeset
   832
			InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Ship window
6195
b90cf92697b9 (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 6173
diff changeset
   833
b90cf92697b9 (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 6173
diff changeset
   834
		GetPlayer(_current_player)->num_engines[p1]++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   835
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   836
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   837
	return value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   838
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   839
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   840
/** Sell a ship.
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: 3476
diff changeset
   841
 * @param tile unused
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   842
 * @param flags type of operation
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   843
 * @param p1 vehicle ID to be sold
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   844
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   845
 */
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   846
CommandCost CmdSellShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   847
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   848
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   849
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   850
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   851
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   852
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   853
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   854
	if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   855
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   856
	if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   857
6868
7eb395287b3d (svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents: 6800
diff changeset
   858
	if (!v->IsStoppedInDepot()) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   859
		return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN);
3961
e2783f244ac6 (svn r5120) Add IsShipInDepot{Stopped,}() and remove some redundant checks
tron
parents: 3955
diff changeset
   860
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   861
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   862
	CommandCost ret(EXPENSES_NEW_VEHICLES, -v->value);
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   863
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   864
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   865
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
10731
67db0d431d5e (svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents: 10724
diff changeset
   866
		InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   867
		InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   868
		DeleteWindowById(WC_VEHICLE_VIEW, v->index);
5256
90226d8da8fb (svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents: 5215
diff changeset
   869
		DeleteDepotHighlightOfVehicle(v);
6800
6c09e1e86fcb (svn r10872) [NewGRF_ports] -Sync: with trunk r10765:10871.
rubidium
parents: 6743
diff changeset
   870
		delete v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   871
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   872
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   873
	return ret;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   874
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   876
/** Start/Stop a ship.
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: 3476
diff changeset
   877
 * @param tile unused
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   878
 * @param flags type of operation
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   879
 * @param p1 ship ID to start/stop
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   880
 * @param p2 unused
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   881
 */
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   882
CommandCost CmdStartStopShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   883
{
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   884
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   885
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   886
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   887
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   888
	if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
4244
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
   890
	/* Check if this ship can be started/stopped. The callback will fail or
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
   891
	 * return 0xFF if it can. */
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   892
	uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10274
diff changeset
   893
	if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF) {
4244
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
   894
		StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
   895
		return_cmd_error(error);
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
   896
	}
44dcec623504 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
   897
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   898
	if (flags & DC_EXEC) {
6868
7eb395287b3d (svn r11037) [NewGRF_ports] -Sync: with trunk r10844:11035.
rubidium
parents: 6800
diff changeset
   899
		if (v->IsStoppedInDepot()) {
3139
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3005
diff changeset
   900
			DeleteVehicleNews(p1, STR_981C_SHIP_IS_WAITING_IN_DEPOT);
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3005
diff changeset
   901
		}
4c950c7ec5c9 (svn r3757) -Feature: Delete news items about vehicles, when they get stale
tron
parents: 3005
diff changeset
   902
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
		v->vehstatus ^= VS_STOPPED;
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   904
		v->cur_speed = 0;
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   905
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   906
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   907
		InvalidateWindowClasses(WC_SHIPS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   909
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   910
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   911
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   912
10200
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   913
bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   914
{
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   915
	const Depot *depot = FindClosestShipDepot(this);
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   916
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   917
	if (depot == NULL) return false;
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   918
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   919
	if (location    != NULL) *location    = depot->xy;
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   920
	if (destination != NULL) *destination = depot->index;
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   921
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   922
	return true;
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   923
}
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   924
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   925
/** Send a ship 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: 3476
diff changeset
   926
 * @param tile unused
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   927
 * @param flags type of operation
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   928
 * @param p1 vehicle ID to send to the depot
4451
66603f0f732e (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4412
diff changeset
   929
 * @param p2 various bitmasked elements
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   930
 * - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   931
 * - p2 bit 8-10 - VLW flag (for mass goto depot)
1793
8ac8a8c9ec0f (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   932
 */
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   933
CommandCost CmdSendShipToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
{
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   935
	if (p2 & DEPOT_MASS_SEND) {
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   936
		/* Mass goto depot requested */
4546
9345e0569655 (svn r6378) -Codechange: Rename VLW_FLAGS to VLW_MASK as it is a mask
Darkvater
parents: 4544
diff changeset
   937
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   938
		return SendAllVehiclesToDepot(VEH_SHIP, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_MASK), p1);
4463
3a70624c40eb (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   939
	}
3a70624c40eb (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   940
4506
0d8fcc0a4e49 (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   941
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   942
10200
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   943
	Vehicle *v = GetVehicle(p1);
4510
071bca98c577 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   944
10200
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   945
	if (v->type != VEH_SHIP) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   946
10200
aba3af04cdbd (svn r12732) [NewGRF_ports] -Sync: with trunk r12657:12672.
richk
parents: 10184
diff changeset
   947
	return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   951
/** Refits a ship to the specified cargo type.
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: 3476
diff changeset
   952
 * @param tile unused
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   953
 * @param flags type of operation
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   954
 * @param p1 vehicle ID of the ship to refit
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   955
 * @param p2 various bitstuffed elements
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   956
 * - p2 = (bit 0-7) - the new cargo type to refit to (p2 & 0xFF)
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   957
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   958
 * - p2 = (bit 16) - refit only this vehicle (ignored)
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   959
 * @return cost of refit or error
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   960
 */
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
   961
CommandCost CmdRefitShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   962
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
	Vehicle *v;
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   964
	CommandCost cost(EXPENSES_SHIP_RUN);
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   965
	CargoID new_cid = GB(p2, 0, 8); //gets the cargo number
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   966
	byte new_subtype = GB(p2, 8, 8);
3989
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   967
	uint16 capacity = CALLBACK_FAILED;
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   968
4352
8ddb01bc6075 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   969
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   970
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   971
	v = GetVehicle(p1);
1237
0a1ce05c3d45 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   972
6585
7da94b26498a (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6574
diff changeset
   973
	if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   974
	if (!v->IsStoppedInDepot()) return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN);
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6871
diff changeset
   975
	if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   976
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   977
	/* Check cargo */
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
   978
	if (!ShipVehInfo(v->engine_type)->refittable) return CMD_ERROR;
6642
2e690e6fdc03 (svn r9259) -Codechange: NUM_CARGO isn't a valid cargo type...
peter1138
parents: 6589
diff changeset
   979
	if (new_cid >= NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
915
013cb2d74800 (svn r1402) Trim trailing whitespace
tron
parents: 909
diff changeset
   980
3989
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   981
	/* Check the refit capacity callback */
6871
5a9dc001e1ad (svn r11566) [NewGRF_ports] -Sync: with trunk r11218:r11565.
rubidium
parents: 6870
diff changeset
   982
	if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_REFIT_CAPACITY)) {
3989
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   983
		/* Back up the existing cargo type */
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   984
		CargoID temp_cid = v->cargo_type;
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   985
		byte temp_subtype = v->cargo_subtype;
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   986
		v->cargo_type = new_cid;
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   987
		v->cargo_subtype = new_subtype;
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   988
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   989
		capacity = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   990
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   991
		/* Restore the cargo type */
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   992
		v->cargo_type = temp_cid;
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   993
		v->cargo_subtype = temp_subtype;
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   994
	}
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   995
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   996
	if (capacity == CALLBACK_FAILED) {
6719
4cc327ad39d5 (svn r10027) [NewGRF_ports] -Sync: with trunk r9506-10026
richk
parents: 6683
diff changeset
   997
		capacity = GetVehicleProperty(v, 0x0D, ShipVehInfo(v->engine_type)->capacity);
3989
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   998
	}
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   999
	_returned_refit_capacity = capacity;
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
  1000
4845
49105d3b5a9a (svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents: 4739
diff changeset
  1001
	if (IsHumanPlayer(v->owner) && new_cid != v->cargo_type) {
4544
b2d5ad597e4d (svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents: 4529
diff changeset
  1002
		cost = GetRefitCost(v->engine_type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1004
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1005
	if (flags & DC_EXEC) {
3989
a5d50d90fd21 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
  1006
		v->cargo_cap = capacity;
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6719
diff changeset
  1007
		v->cargo.Truncate((v->cargo_type == new_cid) ? capacity : 0);
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1793
diff changeset
  1008
		v->cargo_type = new_cid;
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1009
		v->cargo_subtype = new_subtype;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1010
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
3954
634cd5e35803 (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
  1011
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
10731
67db0d431d5e (svn r13281) [NewGRF_ports] -Sync: with trunk r13145:13280. Includes support for Noise Level on Prop 24 in .grf files.
richk
parents: 10724
diff changeset
  1012
		InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1014
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1015
	return cost;
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
}