src/ship_cmd.cpp
author rubidium
Thu, 18 Dec 2008 12:23:08 +0000
changeset 10436 8d3a9fbe8f19
parent 10236 50afe9dd466e
permissions -rw-r--r--
(svn r14689) -Change: make configure die on commonly made user mistakes, like not having SDL development files or zlib headers installed; you can still compile a dedicated server or a binary without zlib, but you have to explicitly force it.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9070
diff changeset
     3
/** @file ship_cmd.cpp Handling of ships. */
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1802
diff changeset
     6
#include "openttd.h"
3961
9868b766fda7 (svn r5120) Add IsShipInDepot{Stopped,}() and remove some redundant checks
tron
parents: 3955
diff changeset
     7
#include "ship.h"
8119
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8116
diff changeset
     8
#include "tile_cmd.h"
6453
226bcddeba32 (svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents: 6420
diff changeset
     9
#include "landscape.h"
6980
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6953
diff changeset
    10
#include "timetable.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
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
41d828e2d5ab (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"
8785
871586967963 (svn r12489) -Codechange: split station.h into station_base.h and station_func.h.
rubidium
parents: 8784
diff changeset
    14
#include "station_base.h"
8763
81fadd257f67 (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 8616
diff changeset
    15
#include "news_func.h"
8786
1823ff88a054 (svn r12490) -Codechange: rename engine.h to engine_func.h and remove unneeded inclusions of engine.h and/or replace them with engine_type.h.
rubidium
parents: 8785
diff changeset
    16
#include "engine_func.h"
9070
dd0121143eba (svn r12924) -Feature: Introducing the so called 'engine pool' which primarily removes the fixed engine type limits and also happens to allow (with the patch option 'dynamic_engines') multiple NewGRF vehicle sets to coexist.
peter1138
parents: 9045
diff changeset
    17
#include "engine_base.h"
10208
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    18
#include "company_func.h"
72c00af5c95d (svn r14422) -Codechange: also reflect the changes of r14421 in the filenames.
rubidium
parents: 10207
diff changeset
    19
#include "company_base.h"
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    20
#include "npf.h"
8962
1b263c69799d (svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents: 8961
diff changeset
    21
#include "depot_base.h"
1b263c69799d (svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents: 8961
diff changeset
    22
#include "depot_func.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2150
diff changeset
    23
#include "vehicle_gui.h"
2962
f0a49b646c48 (svn r3524) - Split newgrf features from engine.[ch] into newgrf_engine.[ch], and add the new files to project files.
peter1138
parents: 2916
diff changeset
    24
#include "newgrf_engine.h"
3454
8dfe8de79c02 (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
2c84ed52709d (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"
2c84ed52709d (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
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
    28
#include "newgrf_callbacks.h"
4244
d40c73c55357 (svn r5822) - NewGRF: add support for callback 31, vehicle start/stop check. This allows a set to disallow a vehicle from being started, i.e. to not be able to leave the depot. This is almost a partner to callback 1D.
peter1138
parents: 4242
diff changeset
    29
#include "newgrf_text.h"
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
    30
#include "newgrf_sound.h"
5972
59953719a3ff (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: 5944
diff changeset
    31
#include "spritecache.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8108
diff changeset
    32
#include "strings_func.h"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8119
diff changeset
    33
#include "functions.h"
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8119
diff changeset
    34
#include "window_func.h"
8140
0d0d8c94f84b (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8139
diff changeset
    35
#include "date_func.h"
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
    36
#include "vehicle_func.h"
8157
1505def01945 (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium
parents: 8145
diff changeset
    37
#include "sound_func.h"
8211
29a8510dfd62 (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8157
diff changeset
    38
#include "variables.h"
8212
cf3fce5c7464 (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium
parents: 8211
diff changeset
    39
#include "autoreplace_gui.h"
8224
c5a64d87cc54 (svn r11787) -Codechange: more header rewrites. This time related to viewport.h.
rubidium
parents: 8212
diff changeset
    40
#include "gfx_func.h"
8270
e7c342f6b14c (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8264
diff changeset
    41
#include "settings_type.h"
8784
c2e9d649a9ce (svn r12488) -Codechange: split order.h into order_base.h and order_func.h.
rubidium
parents: 8763
diff changeset
    42
#include "order_func.h"
9009
6684576ef32b (svn r12804) -Codechange: move the effect vehicle handling out of vehicle.cpp
rubidium
parents: 9008
diff changeset
    43
#include "effectvehicle_func.h"
8211
29a8510dfd62 (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8157
diff changeset
    44
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
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
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    49
static const TrackBits _ship_sometracks[4] = {
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    50
	TRACK_BIT_X | TRACK_BIT_LOWER | TRACK_BIT_LEFT,  // 0x19, // DIAGDIR_NE
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    51
	TRACK_BIT_Y | TRACK_BIT_UPPER | TRACK_BIT_LEFT,  // 0x16, // DIAGDIR_SE
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    52
	TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_RIGHT, // 0x25, // DIAGDIR_SW
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    53
	TRACK_BIT_Y | TRACK_BIT_LOWER | TRACK_BIT_RIGHT, // 0x2A, // DIAGDIR_NW
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    54
};
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    55
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8611
diff changeset
    56
static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
    57
{
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8611
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
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
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
{
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
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)) {
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
    66
		SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
    67
		if (sprite != 0) return sprite;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
9070
dd0121143eba (svn r12924) -Feature: Introducing the so called 'engine pool' which primarily removes the fixed engine type limits and also happens to allow (with the patch option 'dynamic_engines') multiple NewGRF vehicle sets to coexist.
peter1138
parents: 9045
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
	}
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
    71
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
    72
	return 6 + _ship_sprites[spritenum];
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
    73
}
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
    74
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
    75
void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal)
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
    76
{
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
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
5972
59953719a3ff (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: 5944
diff changeset
    80
/** Get the size of the sprite of a ship sprite heading west (used for lists)
59953719a3ff (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: 5944
diff changeset
    81
 * @param engine The engine to get the sprite from
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
diff changeset
    82
 * @param width The width of the sprite
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
diff changeset
    83
 * @param height The height of the sprite
5972
59953719a3ff (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: 5944
diff changeset
    84
 */
59953719a3ff (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: 5944
diff changeset
    85
void GetShipSpriteSize(EngineID engine, uint &width, uint &height)
59953719a3ff (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: 5944
diff changeset
    86
{
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 9923
diff changeset
    87
	const Sprite *spr = GetSprite(GetShipIcon(engine), ST_NORMAL);
5972
59953719a3ff (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: 5944
diff changeset
    88
59953719a3ff (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: 5944
diff changeset
    89
	width  = spr->width;
59953719a3ff (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: 5944
diff changeset
    90
	height = spr->height;
59953719a3ff (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: 5944
diff changeset
    91
}
59953719a3ff (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: 5944
diff changeset
    92
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
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
{
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
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)) {
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
diff changeset
    98
		SpriteID sprite = GetCustomVehicleSprite(this, direction);
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
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
9070
dd0121143eba (svn r12924) -Feature: Introducing the so called 'engine pool' which primarily removes the fixed engine type limits and also happens to allow (with the patch option 'dynamic_engines') multiple NewGRF vehicle sets to coexist.
peter1138
parents: 9045
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
	}
9022
8fa9e902b06e (svn r12824) -Codechange: Standardise routines for drawing vehicle images, using correct types and less duplication.
peter1138
parents: 9019
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
7206058a7e82 (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
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   109
	if (_settings_game.pf.pathfinder_for_ships == VPF_NPF) { /* NPF is used */
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   110
		Trackdir trackdir = GetVehicleTrackdir(v);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   111
		NPFFoundTargetData ftd = NPFRouteToDepotTrialError(v->tile, trackdir, false, TRANSPORT_WATER, 0, v->owner, INVALID_RAILTYPES);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   112
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   113
		if (ftd.best_bird_dist == 0) return GetDepotByTile(ftd.node.tile); /* Found target */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   114
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   115
		return NULL; /* Did not find target */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   116
	}
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   117
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   118
	/* OPF or YAPF - find the closest depot */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   119
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   120
	const Depot* depot;
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   121
	const Depot* best_depot = NULL;
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
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
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   124
	FOR_ALL_DEPOTS(depot) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   125
		TileIndex tile = depot->xy;
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   126
		if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   127
			uint dist = DistanceManhattan(tile, v->tile);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   128
			if (dist < best_dist) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   129
				best_dist = dist;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
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
	}
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
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
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   140
	if (_settings_game.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
7490
bb27d92565d3 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7425
diff changeset
   141
	if (v->IsInDepot()) {
4529
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   142
		VehicleServiceInDepot(v);
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   143
		return;
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   144
	}
18bd5e7e35aa (svn r6356) -Fix: FS#263 planes come out of hangar and drive back into hangar
bjarni
parents: 4527
diff changeset
   145
7502
f821f134ec5a (svn r11017) -Codechange: unify determining whether a vehicle needs/can be service a little more.
rubidium
parents: 7490
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
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1282
diff changeset
   148
	if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) {
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8830
diff changeset
   149
		if (v->current_order.IsType(OT_GOTO_DEPOT)) {
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8830
diff changeset
   150
			v->current_order.MakeDummy();
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8317
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
8853
3266f0374302 (svn r12615) -Codechange: rename some enums related to depot orders to make it more clear that they are no loading/unloading flags. Also add more type strictness.
rubidium
parents: 8843
diff changeset
   156
	v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1282
diff changeset
   157
	v->dest_tile = depot->xy;
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8317
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
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8392
diff changeset
   161
void Ship::OnNewDay()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
{
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8392
diff changeset
   163
	if ((++this->day_counter & 7) == 0)
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8392
diff changeset
   164
		DecreaseVehicleValue(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8392
diff changeset
   166
	CheckVehicleBreakdown(this);
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8392
diff changeset
   167
	AgeVehicle(this);
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8392
diff changeset
   168
	CheckIfShipNeedsService(this);
19
6080d2b6a959 (svn r20) Feature: warning when a vehicle has invalid orders (celestar)
dominik
parents: 11
diff changeset
   169
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8392
diff changeset
   170
	CheckOrders(this);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
   172
	if (this->running_ticks == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
   174
	CommandCost cost(EXPENSES_SHIP_RUN, GetVehicleProperty(this, 0x0F, ShipVehInfo(this->engine_type)->running_cost) * _price.ship_running * this->running_ticks / (364 * DAY_TICKS));
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
   175
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
   176
	this->profit_this_year -= cost.GetCost();
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
   177
	this->running_ticks = 0;
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8392
diff changeset
   178
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10184
diff changeset
   179
	SubtractMoneyFromCompanyFract(this->owner, cost);
8467
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8392
diff changeset
   180
605661f3a91c (svn r12037) -Codechange: replace OnNewDay_(Aircraft|RoadVeh|Ship|Train) with an OnNewDay method in the Vehicle class
glx
parents: 8392
diff changeset
   181
	InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
diff changeset
   182
	/* we need this for the profit */
1055
cc4f60cc9102 (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
9c1d8c4d3e60 (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)) {
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   199
			SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ?
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4574
diff changeset
   200
				SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v);
9c1d8c4d3e60 (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);
9008
de19c73e405f (svn r12803) -Cleanup: rename SpecialVehicle to EffectVehicle to have a uniform naming of the thing instead of using both names for the same thing.
rubidium
parents: 9000
diff changeset
   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
06c115ce7b7a (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
6553
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
   217
void Ship::MarkDirty()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
{
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7096
diff changeset
   219
	this->cur_image = this->GetImage(this->direction);
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8302
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
6593
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
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
9c1d8c4d3e60 (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)) {
9c1d8c4d3e60 (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);
9c1d8c4d3e60 (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
6593
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
diff changeset
   230
void Ship::PlayLeaveStationSound() const
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
diff changeset
   231
{
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
diff changeset
   232
	PlayShipSound(this);
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
diff changeset
   233
}
102aa05c5ca4 (svn r9807) -Codechange: unify playing of sound when vehicle has been loaded and leaves the station.
rubidium
parents: 6592
diff changeset
   234
8827
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
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
{
8830
b288359ab100 (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 8827
diff changeset
   237
	if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
b288359ab100 (svn r12578) -Codechange: merge the aircrafts ProcessOrder too into the 'unified' ProcessOrder.
rubidium
parents: 8827
diff changeset
   238
8928
78d869931d0d (svn r12698) -Codechange: skip orders to stations that do not have a train part anymore, just like is done for road vehicles and ships.
rubidium
parents: 8891
diff changeset
   239
	const Station *st = GetStation(station);
8827
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   240
	if (st->dock_tile != 0) {
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   241
		return TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   242
	} else {
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   243
		this->cur_order_index++;
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
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
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
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
{
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
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] = {
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   252
		MKIT( 6,  6,  -3,  -3),
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   253
		MKIT( 6, 32,  -3, -16),
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   254
		MKIT( 6,  6,  -3,  -3),
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   255
		MKIT(32,  6, -16,  -3),
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   256
		MKIT( 6,  6,  -3,  -3),
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   257
		MKIT( 6, 32,  -3, -16),
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   258
		MKIT( 6,  6,  -3,  -3),
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
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
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   262
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   263
	uint32 x = _delta_xy_table[direction];
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   264
	this->x_offs        = GB(x,  0, 8);
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   265
	this->y_offs        = GB(x,  8, 8);
8793
5e35232593c3 (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 8786
diff changeset
   266
	this->x_extent      = GB(x, 16, 8);
5e35232593c3 (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 8786
diff changeset
   267
	this->y_extent      = GB(x, 24, 8);
5e35232593c3 (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 8786
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
40cccaaa042c (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
{
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   273
	v->UpdateDeltaXY(v->direction);
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7096
diff changeset
   274
	v->cur_image = v->GetImage(v->direction);
6553
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
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
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   279
static const TileIndexDiffC _ship_leave_depot_offs[] = {
65cdb609b7a6 (svn r1396) Introduce TileIndexDiffC - the compile time version of TileIndexDiff
tron
parents: 900
diff changeset
   280
	{-1,  0},
65cdb609b7a6 (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
{
7490
bb27d92565d3 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7425
diff changeset
   286
	if (!v->IsInDepot()) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   287
9045
2e2f3fc16905 (svn r12870) -Codechange: remove some magic numbers from u.ship.state handling
smatz
parents: 9036
diff changeset
   288
	TileIndex tile = v->tile;
2e2f3fc16905 (svn r12870) -Codechange: remove some magic numbers from u.ship.state handling
smatz
parents: 9036
diff changeset
   289
	Axis axis = GetShipDepotAxis(tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   290
9045
2e2f3fc16905 (svn r12870) -Codechange: remove some magic numbers from u.ship.state handling
smatz
parents: 9036
diff changeset
   291
	/* Check first (north) side */
3454
8dfe8de79c02 (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])))) {
9045
2e2f3fc16905 (svn r12870) -Codechange: remove some magic numbers from u.ship.state handling
smatz
parents: 9036
diff changeset
   293
		v->direction = ReverseDir(AxisToDirection(axis));
2e2f3fc16905 (svn r12870) -Codechange: remove some magic numbers from u.ship.state handling
smatz
parents: 9036
diff changeset
   294
	/* Check second (south) side */
3454
8dfe8de79c02 (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])))) {
9045
2e2f3fc16905 (svn r12870) -Codechange: remove some magic numbers from u.ship.state handling
smatz
parents: 9036
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
	}
9045
2e2f3fc16905 (svn r12870) -Codechange: remove some magic numbers from u.ship.state handling
smatz
parents: 9036
diff changeset
   300
2e2f3fc16905 (svn r12870) -Codechange: remove some magic numbers from u.ship.state handling
smatz
parents: 9036
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
1e66514eb621 (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
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4725
diff changeset
   309
	InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
1055
cc4f60cc9102 (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
6490
fe4443fba5e3 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6453
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
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
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;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   323
		if (_settings_client.gui.vehicle_speed)
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8317
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
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
diff changeset
   327
	/* Decrease somewhat when turning */
2639
eeaefdabfdfd (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
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   330
	if (spd == 0) return false;
eeaefdabfdfd (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
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6888
diff changeset
   338
static CommandCost EstimateShipCost(EngineID engine_type)
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   339
{
9923
3056bf623139 (svn r14078) -Codechange: Rename *VehicleInfo::base_cost to cost_factor, indicating what it actually is.
peter1138
parents: 9921
diff changeset
   340
	return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine_type, 0x0A, ShipVehInfo(engine_type)->cost_factor) * (_price.ship_base >> 3) >> 5);
1793
b9a37c98b468 (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
7206058a7e82 (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
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   348
534
306bc86eb23e (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,
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10184
diff changeset
   352
			(v->owner == _local_company) ? NS_ARRIVAL_COMPANY : NS_ARRIVAL_OTHER,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
			v->index,
10123
f73d96619303 (svn r14307) -Fix: when deleting a station, remove news items regarding it
smatz
parents: 10056
diff changeset
   354
			st->index
f73d96619303 (svn r14307) -Fix: when deleting a station, remove news items regarding it
smatz
parents: 10056
diff changeset
   355
		);
0
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   359
struct PathFindShip {
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
   360
	TileIndex skiptile;
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1942
diff changeset
   361
	TileIndex dest_coords;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   362
	uint best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   363
	uint best_length;
6248
e4a2ed7e5613 (svn r9051) -Codechange: typedef [enum|struct] Y {} X; -> [enum|struct] X {};
rubidium
parents: 6247
diff changeset
   364
};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   365
8611
9037a4227d67 (svn r12193) -Codechange: Rename a magic variable, give it a decent type, and remove a 'goto'.
frosch
parents: 8556
diff changeset
   366
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
   367
{
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
diff changeset
   368
	/* Found dest? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
	if (tile == pfs->dest_coords) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   370
		pfs->best_bird_dist = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   371
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
		pfs->best_length = minu(pfs->best_length, length);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
diff changeset
   376
	/* Skip this tile in the calculation */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
	if (tile != pfs->skiptile) {
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1237
diff changeset
   378
		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
   379
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   380
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
	return false;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
static const byte _ship_search_directions[6][4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   385
	{ 0, 9, 2, 9 },
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
	{ 9, 1, 9, 3 },
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
	{ 9, 0, 3, 9 },
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
	{ 1, 9, 9, 2 },
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
	{ 3, 2, 9, 9 },
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
	{ 9, 9, 1, 0 },
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   391
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
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
   394
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   395
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
   396
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   397
	PathFindShip pfs;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   398
	Track i, best_track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
	uint best_bird_dist = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
	uint best_length    = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
	uint r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   402
	byte ship_dir = v->direction & 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
	pfs.dest_coords = v->dest_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   405
	pfs.skiptile = skiptile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   407
	best_track = INVALID_TRACK;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
	do {
5598
2fadbd43709d (svn r8052) - Codechange: RemoveFirstTrack() and RemoveFirstTrackdir() now accept pointer to TrackBits/TrackdirBits instead of reference.
KUDr
parents: 5587
diff changeset
   410
		i = RemoveFirstTrack(&bits);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   411
10236
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10208
diff changeset
   412
		pfs.best_bird_dist = UINT_MAX;
50afe9dd466e (svn r14464) -Codechange: replace (uint)-1 with UINT_MAX (PhilSophus)
rubidium
parents: 10208
diff changeset
   413
		pfs.best_length = UINT_MAX;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
8800
c30102fee110 (svn r12540) -Codechange: Enumify some values in original pathfinder and remove an unused variable.
frosch
parents: 8793
diff changeset
   415
		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
   416
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   417
		if (best_track != INVALID_TRACK) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
			if (pfs.best_bird_dist != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
				/* 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
   420
				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
   421
				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
   422
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   423
				if (pfs.best_length > best_length) goto bad;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
				if (pfs.best_length < best_length) goto good;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   426
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   427
			/* 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
   428
			 * pick one randomly. */
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2049
diff changeset
   429
			r = GB(Random(), 0, 8);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
			if (_pick_shiptrack_table[i] == ship_dir) r += 80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
			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
   432
			if (r <= 127) goto bad;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
good:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
		best_track = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
		best_bird_dist = pfs.best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
		best_length = pfs.best_length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
bad:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
	} while (bits != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   441
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   442
	*track = best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   443
	return best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8467
diff changeset
   446
static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailTypes railtypes)
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   447
{
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   448
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   449
	void* perf = NpfBeginInterval();
8510
1c0456a2feaf (svn r12085) -Fix(r12058): Road vehicles could get stuck, when NPF told them to reverse on junction tiles. (spotted by SmatZ)
frosch
parents: 8467
diff changeset
   450
	NPFFoundTargetData ret = NPFRouteToStationOrTile(tile, trackdir, ignore_start_tile, target, type, 0, owner, railtypes);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   451
	int t = NpfEndInterval(perf);
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5259
diff changeset
   452
	DEBUG(yapf, 4, "[NPFW] %d us - %d rounds - %d open - %d closed -- ", t, 0, _aystar_stats_open_size, _aystar_stats_closed_size);
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   453
	return ret;
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   454
}
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 3870
diff changeset
   455
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
diff changeset
   456
/** returns the track to choose on the next tile, or -1 when it's better to
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   457
 * reverse. The tile given is the tile we are about to enter, enterdir is the
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   458
 * direction in which we are entering the tile */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   459
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
   460
{
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   461
	assert(IsValidDiagDirection(enterdir));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   463
	switch (_settings_game.pf.pathfinder_for_ships) {
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   464
		case VPF_YAPF: { /* YAPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   465
			Trackdir trackdir = YapfChooseShipTrack(v, tile, enterdir, tracks);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   466
			if (trackdir != INVALID_TRACKDIR) return TrackdirToTrack(trackdir);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   467
		} break;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   468
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   469
		case VPF_NPF: { /* NPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   470
			NPFFindStationOrTileData fstd;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   471
			Trackdir trackdir = GetVehicleTrackdir(v);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   472
			assert(trackdir != INVALID_TRACKDIR); // Check that we are not in a depot
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   473
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   474
			NPFFillWithOrderData(&fstd, v);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   475
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   476
			NPFFoundTargetData ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_WATER, v->owner, INVALID_RAILTYPES);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   477
1698
3f979f9ecf6c (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
   478
			/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   479
			 * the direction we need to take to get there, if ftd.best_bird_dist is not 0,
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   480
			 * we did not find our target, but ftd.best_trackdir contains the direction leading
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   481
			 * to the tile closest to our target. */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   482
			if (ftd.best_trackdir != 0xff) return TrackdirToTrack(ftd.best_trackdir); /* TODO: Wrapper function? */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   483
		} break;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   484
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   485
		default:
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   486
		case VPF_OPF: { /* OPF */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   487
			TileIndex tile2 = TILE_ADD(tile, -TileOffsByDiagDir(enterdir));
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   488
			Track track;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   489
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   490
			/* Let's find out how far it would be if we would reverse first */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   491
			TrackBits b = GetTileShipTrackStatus(tile2) & _ship_sometracks[ReverseDiagDir(enterdir)] & v->u.ship.state;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   492
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   493
			uint distr = UINT_MAX; // distance if we reversed
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   494
			if (b != 0) {
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   495
				distr = FindShipTrack(v, tile2, ReverseDiagDir(enterdir), b, tile, &track);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   496
				if (distr != UINT_MAX) distr++; // penalty for reversing
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   497
			}
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   498
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   499
			/* And if we would not reverse? */
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   500
			uint dist = FindShipTrack(v, tile, enterdir, tracks, 0, &track);
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   501
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   502
			if (dist <= distr) return track;
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   503
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   504
	}
8554
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   505
81520f852e48 (svn r12132) -Cleanup: convert pathfinder selection from if/else to switch/case at many places
smatz
parents: 8551
diff changeset
   506
	return INVALID_TRACK; /* We could better reverse */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3147
diff changeset
   509
static const Direction _new_vehicle_direction_table[] = {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   510
	DIR_N , DIR_NW, DIR_W , INVALID_DIR,
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   511
	DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3147
diff changeset
   512
	DIR_E , DIR_SE, DIR_S
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   515
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
   516
{
926
a6d140a6a4de (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
	uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
a6d140a6a4de (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
   518
							TileX(new_tile) - TileX(old_tile) + 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   519
	assert(offs < 11 && offs != 3 && offs != 7);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   520
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   523
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
   524
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   525
	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
   526
	assert(offs < 11 && offs != 3 && offs != 7);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   527
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   528
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   529
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8611
diff changeset
   530
static inline TrackBits GetAvailShipTracks(TileIndex tile, int dir)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   531
{
8616
fd862a55c47f (svn r12199) -Codechange: Remove magic around the results of GetTileTrackStatus().
frosch
parents: 8611
diff changeset
   532
	return GetTileShipTrackStatus(tile) & _ship_sometracks[dir];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   533
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   534
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
static const byte _ship_subcoord[4][6][3] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   536
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   537
		{15, 8, 1},
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
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
		{15, 8, 2},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
		{15, 7, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
		{ 0, 0, 0},
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
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
		{ 8, 0, 3},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
		{ 7, 0, 2},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   549
		{ 8, 0, 4},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
		{ 0, 0, 0},
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
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
		{ 0, 8, 5},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
		{ 0, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
		{ 0, 7, 6},
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, 0, 0},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   558
		{ 0, 8, 4},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   559
	},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
	{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
		{ 0, 0, 0},
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6490
diff changeset
   562
		{ 8, 15, 7},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
		{ 0, 0, 0},
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6490
diff changeset
   564
		{ 8, 15, 6},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
		{ 0, 0, 0},
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6490
diff changeset
   566
		{ 7, 15, 0},
0
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
static void ShipController(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
	uint32 r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   573
	const byte *b;
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3147
diff changeset
   574
	Direction dir;
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   575
	Track track;
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   576
	TrackBits tracks;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
	v->tick_counter++;
6980
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6953
diff changeset
   579
	v->current_order_time++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
	if (v->breakdown_ctr != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
		if (v->breakdown_ctr <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
			HandleBrokenShip(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   584
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
		}
9000
0eee16b3074d (svn r12795) -Fix [FS#1938]: vehicles could break down during loading and keep loading. The intention of the break down code is not to break down when having zero speed, therefor break downs now do not happen when loading.
rubidium
parents: 8962
diff changeset
   586
		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
   587
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   589
	if (v->vehstatus & VS_STOPPED) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   590
8827
730524764a69 (svn r12575) -Codechange: unduplicate Process*Orders for trains, ships and road vehicles.
rubidium
parents: 8800
diff changeset
   591
	ProcessOrders(v);
6594
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6593
diff changeset
   592
	v->HandleLoading();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8830
diff changeset
   594
	if (v->current_order.IsType(OT_LOADING)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
	CheckShipLeaveDepot(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   598
	if (!ShipAccelerate(v)) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
6153
eb35b73b300a (svn r8897) -Fix
tron
parents: 6152
diff changeset
   602
	GetNewVehiclePosResult gp = GetNewVehiclePos(v);
9490
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   603
	if (v->u.ship.state != TRACK_BIT_WORMHOLE) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   604
		/* Not on a bridge */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   605
		if (gp.old_tile == gp.new_tile) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   606
			/* Staying in tile */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   607
			if (v->IsInDepot()) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   608
				gp.x = v->x_pos;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   609
				gp.y = v->y_pos;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   610
			} else {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   611
				/* Not inside depot */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   612
				r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   613
				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
   614
9490
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   615
				/* A leave station order only needs one tick to get processed, so we can
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   616
				* always skip ahead. */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   617
				if (v->current_order.IsType(OT_LEAVESTATION)) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   618
					v->current_order.Free();
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   619
					InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   620
				} else if (v->dest_tile != 0) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   621
					/* We have a target, let's see if we reached it... */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   622
					if (v->current_order.IsType(OT_GOTO_STATION) &&
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   623
							IsBuoyTile(v->dest_tile) &&
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   624
							DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   625
						/* We got within 3 tiles of our target buoy, so let's skip to our
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   626
						* next order */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   627
						UpdateVehicleTimetable(v, true);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   628
						v->cur_order_index++;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   629
						v->current_order.MakeDummy();
10184
bf4e3ff4cf16 (svn r14395) -Fix [FS#2285]: crashes and GUI desyncs when order is deleted/modified while the timetable window is open
smatz
parents: 10123
diff changeset
   630
						InvalidateVehicleOrder(v, 0);
9490
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   631
					} else {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   632
						/* Non-buoy orders really need to reach the tile */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   633
						if (v->dest_tile == gp.new_tile) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   634
							if (v->current_order.IsType(OT_GOTO_DEPOT)) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   635
								if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   636
									VehicleEnterDepot(v);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   637
									return;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   638
								}
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   639
							} else if (v->current_order.IsType(OT_GOTO_STATION)) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   640
								v->last_station_visited = v->current_order.GetDestination();
1751
009a240d035a (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1698
diff changeset
   641
9490
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   642
								/* Process station in the orderlist. */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   643
								Station *st = GetStation(v->current_order.GetDestination());
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   644
								if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   645
									ShipArrivesAt(v, st);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   646
									v->BeginLoading();
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   647
								} else { // leave stations without docks right aways
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   648
									v->current_order.MakeLeaveStation();
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   649
									v->cur_order_index++;
10184
bf4e3ff4cf16 (svn r14395) -Fix [FS#2285]: crashes and GUI desyncs when order is deleted/modified while the timetable window is open
smatz
parents: 10123
diff changeset
   650
									InvalidateVehicleOrder(v, 0);
9490
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   651
								}
1751
009a240d035a (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1698
diff changeset
   652
							}
009a240d035a (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1698
diff changeset
   653
						}
009a240d035a (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1698
diff changeset
   654
					}
009a240d035a (svn r2255) - Fix: [ 9680363 ] [NPF] Broken buoy handling for ships
matthijs
parents: 1698
diff changeset
   655
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
			}
9490
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   657
		} else {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   658
			DiagDirection diagdir;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   659
			/* New tile */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   660
			if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   661
				goto reverse_direction;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   662
			}
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   663
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   664
			dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   665
			assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   666
			diagdir = DirToDiagDir(dir);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   667
			tracks = GetAvailShipTracks(gp.new_tile, diagdir);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   668
			if (tracks == TRACK_BIT_NONE) goto reverse_direction;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   669
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   670
			/* Choose a direction, and continue if we find one */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   671
			track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   672
			if (track == INVALID_TRACK) goto reverse_direction;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   673
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   674
			b = _ship_subcoord[diagdir][track];
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   675
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   676
			gp.x = (gp.x & ~0xF) | b[0];
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   677
			gp.y = (gp.y & ~0xF) | b[1];
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   678
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   679
			/* Call the landscape function and tell it that the vehicle entered the tile */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   680
			r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   681
			if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   682
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   683
			if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   684
				v->tile = gp.new_tile;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   685
				v->u.ship.state = TrackToTrackBits(track);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   686
			}
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   687
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   688
			v->direction = (Direction)b[2];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   689
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   690
	} else {
9490
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   691
		/* On a bridge */
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   692
		if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   693
			v->x_pos = gp.x;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   694
			v->y_pos = gp.y;
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   695
			VehiclePositionChanged(v);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   696
			if (!(v->vehstatus & VS_HIDDEN)) EndVehicleMove(v);
01c07bde5e84 (svn r13464) -Codechange: support NewGRF Action 0x05, type 12.
rubidium
parents: 9413
diff changeset
   697
			return;
5994
a067afdb59b1 (svn r8703) -Codechange/cleanup: some magic numbers -> enums and other small coding style changes to the ShipController and TrainController.
rubidium
parents: 5993
diff changeset
   698
		}
0
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   701
	/* update image of ship, as well as delta XY */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   702
	dir = ShipGetNewDirection(v, gp.x, gp.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   703
	v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   704
	v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   705
	v->z_pos = GetSlopeZ(gp.x, gp.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   706
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
getout:
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   708
	v->UpdateDeltaXY(dir);
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7096
diff changeset
   709
	v->cur_image = v->GetImage(dir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   710
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   711
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
	return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   713
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   714
reverse_direction:
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3147
diff changeset
   715
	dir = ReverseDir(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
	v->direction = dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   717
	goto getout;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   720
static void AgeShipCargo(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   721
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   722
	if (_age_cargo_skip_counter != 0) return;
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6980
diff changeset
   723
	v->cargo.AgeCargo();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
7135
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
   726
void Ship::Tick()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   727
{
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
   728
	if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
   729
7135
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
   730
	AgeShipCargo(this);
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
   731
	ShipController(this);
0
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6176
diff changeset
   735
void ShipsYearlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   737
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   738
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
	FOR_ALL_VEHICLES(v) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   740
		if (v->type == VEH_SHIP) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   741
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   742
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   743
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   747
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   748
/** Build a ship.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3476
diff changeset
   749
 * @param tile tile of depot where ship is built
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
diff changeset
   750
 * @param flags type of operation
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   751
 * @param p1 ship type being built (engine)
9628
981ba3800c35 (svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz
parents: 9490
diff changeset
   752
 * @param p2 unused
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   753
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6888
diff changeset
   754
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
   755
{
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6888
diff changeset
   756
	CommandCost value;
1282
ea2ae881814c (svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents: 1266
diff changeset
   757
	UnitID unit_num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   758
	Engine *e;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   759
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10184
diff changeset
   760
	if (!IsEngineBuildable(p1, VEH_SHIP, _current_company)) return_cmd_error(STR_SHIP_NOT_AVAILABLE);
1196
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1151
diff changeset
   761
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   762
	value = EstimateShipCost(p1);
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   763
	if (flags & DC_QUERY_COST) return value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   764
1443
2b8e1b123d91 (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
	/* The ai_new queries the vehicle cost before building the route,
2b8e1b123d91 (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
   766
	 * so we must check against cheaters no sooner than now. --pasky */
8961
fb0848956387 (svn r12753) -Codechange: do not use IsDepotTypeTile() where simpler function can be used
smatz
parents: 8954
diff changeset
   767
	if (!IsShipDepotTile(tile)) return CMD_ERROR;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10184
diff changeset
   768
	if (!IsTileOwner(tile, _current_company)) return CMD_ERROR;
1443
2b8e1b123d91 (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
   769
9628
981ba3800c35 (svn r13691) -Codechange: make it easier to determine whether a command is ran in the context of autoreplace or not
smatz
parents: 9490
diff changeset
   770
	unit_num = (flags & DC_AUTOREPLACE) ? 0 : GetFreeUnitNumber(VEH_SHIP);
3816
28ee8b8c2522 (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
   771
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   772
	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
   773
		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
   774
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   775
	if (flags & DC_EXEC) {
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3476
diff changeset
   776
		int x;
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3476
diff changeset
   777
		int y;
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3476
diff changeset
   778
538
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   779
		const ShipVehicleInfo *svi = ShipVehInfo(p1);
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   780
8045
4fd4eb3f265a (svn r11605) -Fix [FS#1482, FS#1523]: wrong count of Kirby trains when a ship was build.
rubidium
parents: 7932
diff changeset
   781
		Vehicle *v = new Ship();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   782
		v->unitnumber = unit_num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   783
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10184
diff changeset
   784
		v->owner = _current_company;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   785
		v->tile = tile;
3645
7f950533d510 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3610
diff changeset
   786
		x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
7f950533d510 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3610
diff changeset
   787
		y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   788
		v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   789
		v->y_pos = y;
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6490
diff changeset
   790
		v->z_pos = GetSlopeZ(x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   791
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
   792
		v->running_ticks = 0;
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8554
diff changeset
   793
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   794
		v->UpdateDeltaXY(v->direction);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   795
		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
   796
538
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   797
		v->spritenum = svi->image_index;
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   798
		v->cargo_type = svi->cargo_type;
3870
d9ebc4ea750a (svn r4910) - NewGRF: add and initialize cargo subtype for vehicle visual variations
peter1138
parents: 3816
diff changeset
   799
		v->cargo_subtype = 0;
538
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   800
		v->cargo_cap = svi->capacity;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   801
		v->value = value.GetCost();
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   802
1266
eccd576e322f (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
   803
		v->last_station_visited = INVALID_STATION;
538
24fdb517fbe5 (svn r920) Replace vehicle info macros with inline functions and add asserts to check limits
tron
parents: 534
diff changeset
   804
		v->max_speed = svi->max_speed;
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2464
diff changeset
   805
		v->engine_type = p1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   806
1926
530480d14685 (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1901
diff changeset
   807
		e = GetEngine(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   808
		v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   809
		v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   810
		v->max_age = e->lifelength * 366;
2564
24533c2db978 (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2557
diff changeset
   811
		_new_vehicle_id = v->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   812
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   813
		v->name = NULL;
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5991
diff changeset
   814
		v->u.ship.state = TRACK_BIT_DEPOT;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   815
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9358
diff changeset
   816
		v->service_interval = _settings_game.vehicle.servint_ships;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   817
		v->date_of_last_service = _date;
4329
0e6e689f66e7 (svn r6002) -Cleanup: remove the now redundant BASE_YEAR constant.
rubidium
parents: 4293
diff changeset
   818
		v->build_year = _cur_year;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
		v->cur_image = 0x0E5E;
2804
ea4080819175 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2791
diff changeset
   820
		v->random_bits = VehicleRandomBits();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
6176
153f0f24dc1b (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6175
diff changeset
   822
		v->vehicle_flags = 0;
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7929
diff changeset
   823
		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
6176
153f0f24dc1b (svn r8946) -Feature: [NewGRF] Add support for vehicle variables 0xFE and 0xFF bit 10,
maedhros
parents: 6175
diff changeset
   824
6608
dc0b009642a4 (svn r9828) -Codechange: [NewGRF] Add support for changing cargo capacity with callback 36. This is set on construction for ships and roadvehicles, and whenever carriages are attached for trains.
peter1138
parents: 6595
diff changeset
   825
		v->cargo_cap = GetVehicleProperty(v, 0x0D, svi->capacity);
dc0b009642a4 (svn r9828) -Codechange: [NewGRF] Add support for changing cargo capacity with callback 36. This is set on construction for ships and roadvehicles, and whenever carriages are attached for trains.
peter1138
parents: 6595
diff changeset
   826
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   827
		VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4725
diff changeset
   829
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9234
diff changeset
   830
		InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   831
		InvalidateWindow(WC_COMPANY, v->owner);
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10184
diff changeset
   832
		if (IsLocalCompany())
7425
d9bc116f2f54 (svn r10823) -Fix r8610 [FS#1097]: the autoreplace window vehicle count didn't always update correctly) (Matthias)
bjarni
parents: 7398
diff changeset
   833
			InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Ship window
5944
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5922
diff changeset
   834
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10184
diff changeset
   835
		GetCompany(_current_company)->num_engines[p1]++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   836
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   837
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   838
	return value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   839
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   841
/** Sell a ship.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3476
diff changeset
   842
 * @param tile unused
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
diff changeset
   843
 * @param flags type of operation
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   844
 * @param p1 vehicle ID to be sold
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   845
 * @param p2 unused
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   846
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6888
diff changeset
   847
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
   848
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   849
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   850
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   851
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   852
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   853
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   854
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   855
	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
   856
7695
4b27fd86b318 (svn r11229) -Fix [FS#1307]: one could sell vehicles that were crashed in a depot, which would still yield money.
rubidium
parents: 7642
diff changeset
   857
	if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
4b27fd86b318 (svn r11229) -Fix [FS#1307]: one could sell vehicles that were crashed in a depot, which would still yield money.
rubidium
parents: 7642
diff changeset
   858
7490
bb27d92565d3 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7425
diff changeset
   859
	if (!v->IsStoppedInDepot()) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   860
		return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN);
3961
9868b766fda7 (svn r5120) Add IsShipInDepot{Stopped,}() and remove some redundant checks
tron
parents: 3955
diff changeset
   861
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   862
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8226
diff changeset
   863
	CommandCost ret(EXPENSES_NEW_VEHICLES, -v->value);
7782
e2999b5fcfad (svn r11332) -Fix: vehicles getting a value of 0 on construction.
rubidium
parents: 7695
diff changeset
   864
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   865
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   866
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9234
diff changeset
   867
		InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   868
		InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   869
		DeleteWindowById(WC_VEHICLE_VIEW, v->index);
5256
9056fd4b30f4 (svn r7386) -Codechange r7385: moved deletion of the vehicle highlight from DeleteVehicle to the sell commands as they are not called as often
bjarni
parents: 5215
diff changeset
   870
		DeleteDepotHighlightOfVehicle(v);
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7334
diff changeset
   871
		delete v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   872
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   873
7782
e2999b5fcfad (svn r11332) -Fix: vehicles getting a value of 0 on construction.
rubidium
parents: 7695
diff changeset
   874
	return ret;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   876
8890
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   877
bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   878
{
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   879
	const Depot *depot = FindClosestShipDepot(this);
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   880
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   881
	if (depot == NULL) return false;
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   882
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   883
	if (location    != NULL) *location    = depot->xy;
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   884
	if (destination != NULL) *destination = depot->index;
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   885
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   886
	return true;
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   887
}
8a0fa7ff70a0 (svn r12657) -Codechange: add 'FindClosestDepot' to the vehicle class.
rubidium
parents: 8862
diff changeset
   888
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   889
/** Send a ship to the depot.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3476
diff changeset
   890
 * @param tile unused
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
diff changeset
   891
 * @param flags type of operation
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   892
 * @param p1 vehicle ID to send to the depot
4451
2e86da0081b5 (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4412
diff changeset
   893
 * @param p2 various bitmasked elements
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   894
 * - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   895
 * - p2 bit 8-10 - VLW flag (for mass goto depot)
1793
b9a37c98b468 (svn r2297) - CodeChange: server-check the next batch of commands.
Darkvater
parents: 1790
diff changeset
   896
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6888
diff changeset
   897
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
   898
{
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   899
	if (p2 & DEPOT_MASS_SEND) {
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   900
		/* Mass goto depot requested */
4546
507b7d9bd834 (svn r6378) -Codechange: Rename VLW_FLAGS to VLW_MASK as it is a mask
Darkvater
parents: 4544
diff changeset
   901
		if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10184
diff changeset
   902
		return SendAllVehiclesToDepot(VEH_SHIP, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
4463
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   903
	}
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
   904
4506
e6a56518135c (svn r6291) -Feature: Vehicle lists from the station window now also got the goto depot button
bjarni
parents: 4463
diff changeset
   905
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   906
8891
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8890
diff changeset
   907
	Vehicle *v = GetVehicle(p1);
4510
54d30a2372c3 (svn r6295) -Feature: using goto depot with a different control selection will now alter the service/stopping in depot flag instead of cancelling the goto depot order
bjarni
parents: 4506
diff changeset
   908
8891
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8890
diff changeset
   909
	if (v->type != VEH_SHIP) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   910
8891
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8890
diff changeset
   911
	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
   912
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   913
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   914
1802
da61740cc1e7 (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
   915
/** Refits a ship to the specified cargo type.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3476
diff changeset
   916
 * @param tile unused
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6407
diff changeset
   917
 * @param flags type of operation
1802
da61740cc1e7 (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
   918
 * @param p1 vehicle ID of the ship to refit
da61740cc1e7 (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
   919
 * @param p2 various bitstuffed elements
da61740cc1e7 (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
   920
 * - p2 = (bit 0-7) - the new cargo type to refit to (p2 & 0xFF)
3954
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   921
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
6546
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6502
diff changeset
   922
 * - p2 = (bit 16) - refit only this vehicle (ignored)
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6502
diff changeset
   923
 * @return cost of refit or error
1802
da61740cc1e7 (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
   924
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6888
diff changeset
   925
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
   926
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   927
	Vehicle *v;
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8226
diff changeset
   928
	CommandCost cost(EXPENSES_SHIP_RUN);
3954
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   929
	CargoID new_cid = GB(p2, 0, 8); //gets the cargo number
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   930
	byte new_subtype = GB(p2, 8, 8);
3989
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   931
	uint16 capacity = CALLBACK_FAILED;
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   932
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4351
diff changeset
   933
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   934
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   935
	v = GetVehicle(p1);
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1235
diff changeset
   936
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   937
	if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
8145
2116aa2c6263 (svn r11707) -Fix: do not allow refitting flooded (destroyed) vehicles
smatz
parents: 8144
diff changeset
   938
	if (!v->IsStoppedInDepot()) return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN);
2116aa2c6263 (svn r11707) -Fix: do not allow refitting flooded (destroyed) vehicles
smatz
parents: 8144
diff changeset
   939
	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
   940
1802
da61740cc1e7 (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
   941
	/* Check cargo */
da61740cc1e7 (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
   942
	if (!ShipVehInfo(v->engine_type)->refittable) return CMD_ERROR;
6316
d2b70e10f106 (svn r9259) -Codechange: NUM_CARGO isn't a valid cargo type...
peter1138
parents: 6263
diff changeset
   943
	if (new_cid >= NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 909
diff changeset
   944
3989
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   945
	/* Check the refit capacity callback */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7815
diff changeset
   946
	if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_REFIT_CAPACITY)) {
3989
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   947
		/* Back up the existing cargo type */
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   948
		CargoID temp_cid = v->cargo_type;
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   949
		byte temp_subtype = v->cargo_subtype;
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   950
		v->cargo_type = new_cid;
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   951
		v->cargo_subtype = new_subtype;
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   952
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   953
		capacity = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   954
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   955
		/* Restore the cargo type */
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   956
		v->cargo_type = temp_cid;
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   957
		v->cargo_subtype = temp_subtype;
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   958
	}
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   959
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   960
	if (capacity == CALLBACK_FAILED) {
6608
dc0b009642a4 (svn r9828) -Codechange: [NewGRF] Add support for changing cargo capacity with callback 36. This is set on construction for ships and roadvehicles, and whenever carriages are attached for trains.
peter1138
parents: 6595
diff changeset
   961
		capacity = GetVehicleProperty(v, 0x0D, ShipVehInfo(v->engine_type)->capacity);
3989
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   962
	}
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   963
	_returned_refit_capacity = capacity;
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   964
10207
c291a21b304e (svn r14421) -Codechange: rename all player variables/types to company *or* client so it is immediatelly clear which one you are working with.
rubidium
parents: 10184
diff changeset
   965
	if (IsHumanCompany(v->owner) && new_cid != v->cargo_type) {
4544
69f1248a2d97 (svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents: 4529
diff changeset
   966
		cost = GetRefitCost(v->engine_type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
	if (flags & DC_EXEC) {
3989
9b99f0898ca2 (svn r5188) - NewGRF: check refit capacity callback when refitting a ship
peter1138
parents: 3961
diff changeset
   970
		v->cargo_cap = capacity;
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6980
diff changeset
   971
		v->cargo.Truncate((v->cargo_type == new_cid) ? capacity : 0);
1802
da61740cc1e7 (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
   972
		v->cargo_type = new_cid;
3954
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   973
		v->cargo_subtype = new_subtype;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   974
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
3954
7b51d0046efb (svn r5103) - Add cargo subtype parameter to refit commands (mart3p)
peter1138
parents: 3948
diff changeset
   975
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9234
diff changeset
   976
		InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
}