2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file ship_cmd.cpp Handling of ships. */
|
2007-04-04 01:35:16 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2006-06-05 11:28:00 +00:00
|
|
|
#include "ship.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2007-06-20 19:17:22 +00:00
|
|
|
#include "timetable.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
2009-12-01 23:56:04 +00:00
|
|
|
#include "pathfinder/npf/npf_func.h"
|
2008-04-17 19:10:30 +00:00
|
|
|
#include "depot_base.h"
|
2009-12-01 22:45:39 +00:00
|
|
|
#include "station_base.h"
|
2006-02-03 12:55:21 +00:00
|
|
|
#include "newgrf_engine.h"
|
2009-12-01 22:45:39 +00:00
|
|
|
#include "pathfinder/yapf/yapf.h"
|
2006-09-27 18:17:01 +00:00
|
|
|
#include "newgrf_sound.h"
|
2007-02-10 13:37:32 +00:00
|
|
|
#include "spritecache.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "window_func.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
2007-12-29 09:24:26 +00:00
|
|
|
#include "sound_func.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "ai/ai.hpp"
|
2009-12-01 22:45:39 +00:00
|
|
|
#include "pathfinder/opf/opf_ship.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "engine_base.h"
|
|
|
|
#include "company_base.h"
|
2011-07-12 17:12:10 +00:00
|
|
|
#include "tunnelbridge_map.h"
|
2011-11-24 12:38:48 +00:00
|
|
|
#include "zoom_func.h"
|
2008-01-07 00:57:19 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-07-12 17:12:10 +00:00
|
|
|
/**
|
|
|
|
* Determine the effective #WaterClass for a ship travelling on a tile.
|
|
|
|
* @param tile Tile of interest
|
|
|
|
* @return the waterclass to be used by the ship.
|
|
|
|
*/
|
2011-08-21 14:13:22 +00:00
|
|
|
WaterClass GetEffectiveWaterClass(TileIndex tile)
|
2011-07-12 17:12:10 +00:00
|
|
|
{
|
|
|
|
if (HasTileWaterClass(tile)) return GetWaterClass(tile);
|
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
|
|
|
assert(GetTunnelBridgeTransportType(tile) == TRANSPORT_WATER);
|
|
|
|
return WATER_CLASS_CANAL;
|
|
|
|
}
|
|
|
|
if (IsTileType(tile, MP_RAILWAY)) {
|
|
|
|
assert(GetRailGroundType(tile) == RAIL_GROUND_WATER);
|
|
|
|
return WATER_CLASS_SEA;
|
|
|
|
}
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
|
|
|
|
|
2008-02-20 17:49:50 +00:00
|
|
|
static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
|
2005-06-24 12:38:35 +00:00
|
|
|
{
|
2008-02-20 17:49:50 +00:00
|
|
|
return TrackStatusToTrackBits(GetTileTrackStatus(tile, TRANSPORT_WATER, 0));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-11-01 16:51:47 +00:00
|
|
|
static SpriteID GetShipIcon(EngineID engine, EngineImageType image_type)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-10-06 19:17:07 +00:00
|
|
|
const Engine *e = Engine::Get(engine);
|
|
|
|
uint8 spritenum = e->u.ship.image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
2011-11-01 16:51:47 +00:00
|
|
|
SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W, image_type);
|
2008-04-21 20:50:58 +00:00
|
|
|
if (sprite != 0) return sprite;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-10-06 19:17:07 +00:00
|
|
|
spritenum = e->original_image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2009-05-26 08:53:16 +00:00
|
|
|
return DIR_W + _ship_sprites[spritenum];
|
2008-04-21 20:50:58 +00:00
|
|
|
}
|
|
|
|
|
2011-11-01 16:51:47 +00:00
|
|
|
void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
|
2008-04-21 20:50:58 +00:00
|
|
|
{
|
2011-11-01 16:51:47 +00:00
|
|
|
SpriteID sprite = GetShipIcon(engine, image_type);
|
2009-11-16 17:58:46 +00:00
|
|
|
const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
|
2011-11-24 12:38:48 +00:00
|
|
|
preferred_x = Clamp(preferred_x, left - UnScaleByZoom(real_sprite->x_offs, ZOOM_LVL_GUI), right - UnScaleByZoom(real_sprite->width, ZOOM_LVL_GUI) - UnScaleByZoom(real_sprite->x_offs, ZOOM_LVL_GUI));
|
2009-11-16 17:58:46 +00:00
|
|
|
DrawSprite(sprite, pal, preferred_x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
2012-12-23 01:00:25 +00:00
|
|
|
* Get the size of the sprite of a ship sprite heading west (used for lists).
|
|
|
|
* @param engine The engine to get the sprite from.
|
|
|
|
* @param[out] width The width of the sprite.
|
|
|
|
* @param[out] height The height of the sprite.
|
|
|
|
* @param[out] xoffs Number of pixels to shift the sprite to the right.
|
|
|
|
* @param[out] yoffs Number of pixels to shift the sprite downwards.
|
|
|
|
* @param image_type Context the sprite is used in.
|
2007-02-10 13:37:32 +00:00
|
|
|
*/
|
2012-12-23 01:00:25 +00:00
|
|
|
void GetShipSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
|
2007-02-10 13:37:32 +00:00
|
|
|
{
|
2011-11-01 16:51:47 +00:00
|
|
|
const Sprite *spr = GetSprite(GetShipIcon(engine, image_type), ST_NORMAL);
|
2007-02-10 13:37:32 +00:00
|
|
|
|
2011-11-24 12:38:48 +00:00
|
|
|
width = UnScaleByZoom(spr->width, ZOOM_LVL_GUI);
|
|
|
|
height = UnScaleByZoom(spr->height, ZOOM_LVL_GUI);
|
2012-12-23 01:00:25 +00:00
|
|
|
xoffs = UnScaleByZoom(spr->x_offs, ZOOM_LVL_GUI);
|
|
|
|
yoffs = UnScaleByZoom(spr->y_offs, ZOOM_LVL_GUI);
|
2007-02-10 13:37:32 +00:00
|
|
|
}
|
|
|
|
|
2011-11-01 16:51:47 +00:00
|
|
|
SpriteID Ship::GetImage(Direction direction, EngineImageType image_type) const
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-21 20:50:58 +00:00
|
|
|
uint8 spritenum = this->spritenum;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
2011-11-01 16:51:47 +00:00
|
|
|
SpriteID sprite = GetCustomVehicleSprite(this, direction, image_type);
|
2005-11-14 19:48:04 +00:00
|
|
|
if (sprite != 0) return sprite;
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2011-11-01 00:21:08 +00:00
|
|
|
spritenum = this->GetEngine()->original_image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return _ship_sprites[spritenum] + direction;
|
|
|
|
}
|
|
|
|
|
2009-12-20 16:19:47 +00:00
|
|
|
static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-12-01 23:56:04 +00:00
|
|
|
/* Find the closest depot */
|
2009-01-10 00:31:47 +00:00
|
|
|
const Depot *depot;
|
|
|
|
const Depot *best_depot = NULL;
|
2009-12-20 16:19:47 +00:00
|
|
|
/* If we don't have a maximum distance, i.e. distance = 0,
|
|
|
|
* we want to find any depot so the best distance of no
|
|
|
|
* depot must be more than any correct distance. On the
|
|
|
|
* other hand if we have set a maximum distance, any depot
|
|
|
|
* further away than max_distance can safely be ignored. */
|
|
|
|
uint best_dist = max_distance == 0 ? UINT_MAX : max_distance + 1;
|
2008-02-13 17:54:11 +00:00
|
|
|
|
|
|
|
FOR_ALL_DEPOTS(depot) {
|
|
|
|
TileIndex tile = depot->xy;
|
2008-04-17 18:24:45 +00:00
|
|
|
if (IsShipDepotTile(tile) && IsTileOwner(tile, v->owner)) {
|
2008-02-13 17:54:11 +00:00
|
|
|
uint dist = DistanceManhattan(tile, v->tile);
|
|
|
|
if (dist < best_dist) {
|
|
|
|
best_dist = dist;
|
|
|
|
best_depot = depot;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-13 17:54:11 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return best_depot;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckIfShipNeedsService(Vehicle *v)
|
|
|
|
{
|
2009-05-26 21:59:49 +00:00
|
|
|
if (Company::Get(v->owner)->settings.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
|
2012-07-07 15:39:46 +00:00
|
|
|
if (v->IsChainInDepot()) {
|
2006-09-03 11:49:38 +00:00
|
|
|
VehicleServiceInDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-13 10:48:44 +00:00
|
|
|
uint max_distance;
|
|
|
|
switch (_settings_game.pf.pathfinder_for_ships) {
|
|
|
|
case VPF_OPF: max_distance = 12; break;
|
|
|
|
case VPF_NPF: max_distance = _settings_game.pf.npf.maximum_go_to_depot_penalty / NPF_TILE_LENGTH; break;
|
|
|
|
case VPF_YAPF: max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2009-12-20 16:19:47 +00:00
|
|
|
const Depot *depot = FindClosestShipDepot(v, max_distance);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-12-20 16:19:47 +00:00
|
|
|
if (depot == NULL) {
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT)) {
|
|
|
|
v->current_order.MakeDummy();
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-07 19:18:56 +00:00
|
|
|
v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
|
2005-02-06 10:18:47 +00:00
|
|
|
v->dest_tile = depot->xy;
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-05-01 19:51:52 +00:00
|
|
|
/**
|
|
|
|
* Update the caches of this ship.
|
|
|
|
*/
|
2010-11-06 13:03:17 +00:00
|
|
|
void Ship::UpdateCache()
|
|
|
|
{
|
2011-07-07 14:16:22 +00:00
|
|
|
const ShipVehicleInfo *svi = ShipVehInfo(this->engine_type);
|
|
|
|
|
|
|
|
/* Get speed fraction for the current water type. Aqueducts are always canals. */
|
2011-08-27 10:33:45 +00:00
|
|
|
bool is_ocean = GetEffectiveWaterClass(this->tile) == WATER_CLASS_SEA;
|
|
|
|
uint raw_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed);
|
|
|
|
this->vcache.cached_max_speed = svi->ApplyWaterClassSpeedFrac(raw_speed, is_ocean);
|
2010-11-18 14:17:55 +00:00
|
|
|
|
2011-08-03 20:55:08 +00:00
|
|
|
/* Update cargo aging period. */
|
|
|
|
this->vcache.cached_cargo_age_period = GetVehicleProperty(this, PROP_SHIP_CARGO_AGE_PERIOD, EngInfo(this->engine_type)->cargo_age_period);
|
|
|
|
|
2010-11-18 14:17:55 +00:00
|
|
|
this->UpdateVisualEffect();
|
2010-11-06 13:03:17 +00:00
|
|
|
}
|
|
|
|
|
2009-01-22 21:33:08 +00:00
|
|
|
Money Ship::GetRunningCost() const
|
|
|
|
{
|
2011-11-01 00:21:08 +00:00
|
|
|
const Engine *e = this->GetEngine();
|
2009-11-24 13:12:34 +00:00
|
|
|
uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
|
2011-11-01 00:23:41 +00:00
|
|
|
return GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
|
2009-01-22 21:33:08 +00:00
|
|
|
}
|
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
void Ship::OnNewDay()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-07-24 10:14:39 +00:00
|
|
|
if ((++this->day_counter & 7) == 0) {
|
2008-02-01 22:02:14 +00:00
|
|
|
DecreaseVehicleValue(this);
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
CheckVehicleBreakdown(this);
|
|
|
|
AgeVehicle(this);
|
|
|
|
CheckIfShipNeedsService(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
CheckOrders(this);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-02-13 19:24:40 +00:00
|
|
|
if (this->running_ticks == 0) return;
|
|
|
|
|
2009-01-22 21:33:08 +00:00
|
|
|
CommandCost cost(EXPENSES_SHIP_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
|
2004-08-11 10:15:38 +00:00
|
|
|
|
2008-02-13 19:24:40 +00:00
|
|
|
this->profit_this_year -= cost.GetCost();
|
|
|
|
this->running_ticks = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
SubtractMoneyFromCompanyFract(this->owner, cost);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
|
2007-04-04 01:35:16 +00:00
|
|
|
/* we need this for the profit */
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowClassesDirty(WC_SHIPS_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
Trackdir Ship::GetVehicleTrackdir() const
|
|
|
|
{
|
|
|
|
if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
|
|
|
|
|
|
|
|
if (this->IsInDepot()) {
|
|
|
|
/* We'll assume the ship is facing outwards */
|
|
|
|
return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->state == TRACK_BIT_WORMHOLE) {
|
|
|
|
/* ship on aqueduct, so just use his direction and assume a diagonal track */
|
|
|
|
return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
|
|
|
|
}
|
|
|
|
|
|
|
|
return TrackDirectionToTrackdir(FindFirstTrack(this->state), this->direction);
|
|
|
|
}
|
|
|
|
|
2007-04-29 22:33:51 +00:00
|
|
|
void Ship::MarkDirty()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-08-08 18:45:12 +00:00
|
|
|
this->UpdateViewport(false, false);
|
2010-11-11 22:19:27 +00:00
|
|
|
this->UpdateCache();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-05-07 15:58:05 +00:00
|
|
|
static void PlayShipSound(const Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-09-27 18:17:01 +00:00
|
|
|
if (!PlayVehicleSound(v, VSE_START)) {
|
|
|
|
SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-05-07 15:58:05 +00:00
|
|
|
void Ship::PlayLeaveStationSound() const
|
|
|
|
{
|
|
|
|
PlayShipSound(this);
|
|
|
|
}
|
|
|
|
|
2008-04-05 10:55:50 +00:00
|
|
|
TileIndex Ship::GetOrderStationLocation(StationID station)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-05 12:01:34 +00:00
|
|
|
if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
|
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
const Station *st = Station::Get(station);
|
2008-12-26 18:01:15 +00:00
|
|
|
if (st->dock_tile != INVALID_TILE) {
|
2008-04-05 10:55:50 +00:00
|
|
|
return TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2011-01-31 20:44:15 +00:00
|
|
|
this->IncrementRealOrderIndex();
|
2008-04-05 10:55:50 +00:00
|
|
|
return 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-01 16:35:14 +00:00
|
|
|
void Ship::UpdateDeltaXY(Direction direction)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2012-06-06 14:03:22 +00:00
|
|
|
static const int8 _delta_xy_table[8][4] = {
|
|
|
|
/* y_extent, x_extent, y_offs, x_offs */
|
|
|
|
{ 6, 6, -3, -3}, // N
|
|
|
|
{ 6, 32, -3, -16}, // NE
|
|
|
|
{ 6, 6, -3, -3}, // E
|
|
|
|
{32, 6, -16, -3}, // SE
|
|
|
|
{ 6, 6, -3, -3}, // S
|
|
|
|
{ 6, 32, -3, -16}, // SW
|
|
|
|
{ 6, 6, -3, -3}, // W
|
|
|
|
{32, 6, -16, -3}, // NW
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
2007-05-01 16:35:14 +00:00
|
|
|
|
2012-06-06 14:03:22 +00:00
|
|
|
const int8 *bb = _delta_xy_table[direction];
|
|
|
|
this->x_offs = bb[3];
|
|
|
|
this->y_offs = bb[2];
|
|
|
|
this->x_extent = bb[1];
|
|
|
|
this->y_extent = bb[0];
|
2008-04-01 14:03:20 +00:00
|
|
|
this->z_extent = 6;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-06 11:39:00 +00:00
|
|
|
static const TileIndexDiffC _ship_leave_depot_offs[] = {
|
|
|
|
{-1, 0},
|
|
|
|
{ 0, -1}
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2011-04-10 10:47:21 +00:00
|
|
|
static bool CheckShipLeaveDepot(Ship *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2012-07-07 15:39:46 +00:00
|
|
|
if (!v->IsChainInDepot()) return false;
|
2011-04-10 10:47:21 +00:00
|
|
|
|
|
|
|
/* We are leaving a depot, but have to go to the exact same one; re-enter */
|
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT) &&
|
|
|
|
IsShipDepotTile(v->tile) && GetDepotIndex(v->tile) == v->current_order.GetDestination()) {
|
|
|
|
VehicleEnterDepot(v);
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-24 13:05:51 +00:00
|
|
|
TileIndex tile = v->tile;
|
|
|
|
Axis axis = GetShipDepotAxis(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2012-08-18 11:37:47 +00:00
|
|
|
DiagDirection north_dir = ReverseDiagDir(AxisToDiagDir(axis));
|
|
|
|
TileIndex north_neighbour = TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[axis]));
|
|
|
|
DiagDirection south_dir = AxisToDiagDir(axis);
|
|
|
|
TileIndex south_neighbour = TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[axis]));
|
|
|
|
|
|
|
|
TrackBits north_tracks = DiagdirReachesTracks(north_dir) & GetTileShipTrackStatus(north_neighbour);
|
|
|
|
TrackBits south_tracks = DiagdirReachesTracks(south_dir) & GetTileShipTrackStatus(south_neighbour);
|
|
|
|
if (north_tracks && south_tracks) {
|
|
|
|
/* Ask pathfinder for best direction */
|
|
|
|
bool reverse = false;
|
|
|
|
bool path_found;
|
|
|
|
switch (_settings_game.pf.pathfinder_for_ships) {
|
|
|
|
case VPF_OPF: reverse = OPFShipChooseTrack(v, north_neighbour, north_dir, north_tracks, path_found) == INVALID_TRACK; break; // OPF always allows reversing
|
|
|
|
case VPF_NPF: reverse = NPFShipCheckReverse(v); break;
|
|
|
|
case VPF_YAPF: reverse = YapfShipCheckReverse(v); break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
if (reverse) north_tracks = TRACK_BIT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (north_tracks) {
|
|
|
|
/* Leave towards north */
|
|
|
|
v->direction = DiagDirToDir(north_dir);
|
|
|
|
} else if (south_tracks) {
|
|
|
|
/* Leave towards south */
|
|
|
|
v->direction = DiagDirToDir(south_dir);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2012-08-18 11:37:47 +00:00
|
|
|
/* Both ways blocked */
|
2011-04-10 10:47:21 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-24 13:05:51 +00:00
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
v->state = AxisToTrackBits(axis);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus &= ~VS_HIDDEN;
|
|
|
|
|
|
|
|
v->cur_speed = 0;
|
2012-01-02 13:44:37 +00:00
|
|
|
v->UpdateViewport(true, true);
|
2010-07-14 12:27:51 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
PlayShipSound(v);
|
2004-12-09 21:46:56 +00:00
|
|
|
VehicleServiceInDepot(v);
|
2006-10-05 12:59:28 +00:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowClassesDirty(WC_SHIPS_LIST);
|
2011-04-10 10:47:21 +00:00
|
|
|
|
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool ShipAccelerate(Vehicle *v)
|
|
|
|
{
|
|
|
|
uint spd;
|
|
|
|
byte t;
|
|
|
|
|
2010-11-06 13:03:17 +00:00
|
|
|
spd = min(v->cur_speed + 1, v->vcache.cached_max_speed);
|
2012-02-14 17:04:06 +00:00
|
|
|
spd = min(spd, v->current_order.max_speed * 2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* updates statusbar only if speed have changed to save CPU time */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (spd != v->cur_speed) {
|
2004-09-10 19:02:27 +00:00
|
|
|
v->cur_speed = spd;
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2013-01-08 22:46:42 +00:00
|
|
|
/* Convert direction-independent speed into direction-dependent speed. (old movement method) */
|
2010-07-04 13:07:47 +00:00
|
|
|
spd = v->GetOldAdvanceSpeed(spd);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (spd == 0) return false;
|
|
|
|
if ((byte)++spd == 0) return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->progress = (t = v->progress) - (byte)spd;
|
|
|
|
|
|
|
|
return (t < v->progress);
|
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Ship arrives at a dock. If it is the first time, send out a news item.
|
|
|
|
* @param v Ship that arrived.
|
|
|
|
* @param st Station being visited.
|
|
|
|
*/
|
2009-01-10 00:31:47 +00:00
|
|
|
static void ShipArrivesAt(const Vehicle *v, Station *st)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
/* Check if station was ever visited before */
|
|
|
|
if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
|
|
|
|
st->had_vehicle_of_type |= HVOT_SHIP;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, st->index);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddVehicleNewsItem(
|
2009-04-21 23:40:56 +00:00
|
|
|
STR_NEWS_FIRST_SHIP_ARRIVAL,
|
2012-05-26 14:16:03 +00:00
|
|
|
(v->owner == _local_company) ? NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
|
2004-08-09 17:04:08 +00:00
|
|
|
v->index,
|
2008-09-13 10:19:51 +00:00
|
|
|
st->index
|
|
|
|
);
|
2011-11-29 23:15:35 +00:00
|
|
|
AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
2012-08-18 11:37:23 +00:00
|
|
|
* Runs the pathfinder to choose a track to continue along.
|
|
|
|
*
|
|
|
|
* @param v Ship to navigate
|
|
|
|
* @param tile Tile, the ship is about to enter
|
|
|
|
* @param enterdir Direction of entering
|
|
|
|
* @param tracks Available track choices on \a tile
|
|
|
|
* @return Track to choose, or INVALID_TRACK when to reverse.
|
2010-08-01 19:44:49 +00:00
|
|
|
*/
|
2010-12-13 21:56:40 +00:00
|
|
|
static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-02-13 17:54:11 +00:00
|
|
|
assert(IsValidDiagDirection(enterdir));
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2010-12-13 21:56:40 +00:00
|
|
|
bool path_found = true;
|
|
|
|
Track track;
|
2008-05-29 15:13:28 +00:00
|
|
|
switch (_settings_game.pf.pathfinder_for_ships) {
|
2010-12-13 21:56:40 +00:00
|
|
|
case VPF_OPF: track = OPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
|
|
|
|
case VPF_NPF: track = NPFShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
|
|
|
|
case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found); break;
|
2009-12-01 23:56:04 +00:00
|
|
|
default: NOT_REACHED();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2010-12-13 21:56:40 +00:00
|
|
|
|
|
|
|
v->HandlePathfindingResult(path_found);
|
|
|
|
return track;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-03-08 06:55:33 +00:00
|
|
|
static const Direction _new_vehicle_direction_table[] = {
|
2007-01-10 18:56:51 +00:00
|
|
|
DIR_N , DIR_NW, DIR_W , INVALID_DIR,
|
|
|
|
DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
|
2006-03-08 06:55:33 +00:00
|
|
|
DIR_E , DIR_SE, DIR_S
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
static Direction ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-07 17:02:43 +00:00
|
|
|
uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
|
|
|
|
TileX(new_tile) - TileX(old_tile) + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(offs < 11 && offs != 3 && offs != 7);
|
|
|
|
return _new_vehicle_direction_table[offs];
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
static Direction ShipGetNewDirection(Vehicle *v, int x, int y)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
|
|
|
|
assert(offs < 11 && offs != 3 && offs != 7);
|
|
|
|
return _new_vehicle_direction_table[offs];
|
|
|
|
}
|
|
|
|
|
2009-12-01 23:19:46 +00:00
|
|
|
static inline TrackBits GetAvailShipTracks(TileIndex tile, DiagDirection dir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-12-01 23:19:46 +00:00
|
|
|
return GetTileShipTrackStatus(tile) & DiagdirReachesTracks(dir);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const byte _ship_subcoord[4][6][3] = {
|
|
|
|
{
|
|
|
|
{15, 8, 1},
|
|
|
|
{ 0, 0, 0},
|
|
|
|
{ 0, 0, 0},
|
|
|
|
{15, 8, 2},
|
|
|
|
{15, 7, 0},
|
|
|
|
{ 0, 0, 0},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{ 0, 0, 0},
|
|
|
|
{ 8, 0, 3},
|
|
|
|
{ 7, 0, 2},
|
|
|
|
{ 0, 0, 0},
|
|
|
|
{ 8, 0, 4},
|
|
|
|
{ 0, 0, 0},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{ 0, 8, 5},
|
|
|
|
{ 0, 0, 0},
|
|
|
|
{ 0, 7, 6},
|
|
|
|
{ 0, 0, 0},
|
|
|
|
{ 0, 0, 0},
|
|
|
|
{ 0, 8, 4},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{ 0, 0, 0},
|
2007-04-18 22:10:36 +00:00
|
|
|
{ 8, 15, 7},
|
2004-08-09 17:04:08 +00:00
|
|
|
{ 0, 0, 0},
|
2007-04-18 22:10:36 +00:00
|
|
|
{ 8, 15, 6},
|
2004-08-09 17:04:08 +00:00
|
|
|
{ 0, 0, 0},
|
2007-04-18 22:10:36 +00:00
|
|
|
{ 7, 15, 0},
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
static void ShipController(Ship *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint32 r;
|
|
|
|
const byte *b;
|
2006-03-08 06:55:33 +00:00
|
|
|
Direction dir;
|
2007-01-10 18:56:51 +00:00
|
|
|
Track track;
|
|
|
|
TrackBits tracks;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->tick_counter++;
|
2007-06-20 19:17:22 +00:00
|
|
|
v->current_order_time++;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-28 14:14:37 +00:00
|
|
|
if (v->HandleBreakdown()) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-05 10:55:50 +00:00
|
|
|
ProcessOrders(v);
|
2007-05-07 16:21:34 +00:00
|
|
|
v->HandleLoading();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_LOADING)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-04-10 10:47:21 +00:00
|
|
|
if (CheckShipLeaveDepot(v)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-11-18 14:17:55 +00:00
|
|
|
v->ShowVisualEffect();
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!ShipAccelerate(v)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-25 10:49:13 +00:00
|
|
|
GetNewVehiclePosResult gp = GetNewVehiclePos(v);
|
2009-05-22 18:17:20 +00:00
|
|
|
if (v->state != TRACK_BIT_WORMHOLE) {
|
2008-06-11 13:54:01 +00:00
|
|
|
/* Not on a bridge */
|
|
|
|
if (gp.old_tile == gp.new_tile) {
|
|
|
|
/* Staying in tile */
|
|
|
|
if (v->IsInDepot()) {
|
|
|
|
gp.x = v->x_pos;
|
|
|
|
gp.y = v->y_pos;
|
|
|
|
} else {
|
|
|
|
/* Not inside depot */
|
|
|
|
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
|
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
|
|
|
|
|
|
|
|
/* A leave station order only needs one tick to get processed, so we can
|
2009-03-14 18:16:29 +00:00
|
|
|
* always skip ahead. */
|
2008-06-11 13:54:01 +00:00
|
|
|
if (v->current_order.IsType(OT_LEAVESTATION)) {
|
|
|
|
v->current_order.Free();
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2008-06-11 13:54:01 +00:00
|
|
|
} else if (v->dest_tile != 0) {
|
|
|
|
/* We have a target, let's see if we reached it... */
|
2009-07-10 18:30:02 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
|
2008-06-11 13:54:01 +00:00
|
|
|
DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
|
|
|
|
/* We got within 3 tiles of our target buoy, so let's skip to our
|
2009-03-14 18:16:29 +00:00
|
|
|
* next order */
|
2008-06-11 13:54:01 +00:00
|
|
|
UpdateVehicleTimetable(v, true);
|
2011-01-31 20:44:15 +00:00
|
|
|
v->IncrementRealOrderIndex();
|
2008-06-11 13:54:01 +00:00
|
|
|
v->current_order.MakeDummy();
|
|
|
|
} else {
|
|
|
|
/* Non-buoy orders really need to reach the tile */
|
|
|
|
if (v->dest_tile == gp.new_tile) {
|
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT)) {
|
|
|
|
if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
|
|
|
|
VehicleEnterDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (v->current_order.IsType(OT_GOTO_STATION)) {
|
|
|
|
v->last_station_visited = v->current_order.GetDestination();
|
|
|
|
|
|
|
|
/* Process station in the orderlist. */
|
2009-05-16 23:34:14 +00:00
|
|
|
Station *st = Station::Get(v->current_order.GetDestination());
|
2008-06-11 13:54:01 +00:00
|
|
|
if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
|
|
|
|
ShipArrivesAt(v, st);
|
|
|
|
v->BeginLoading();
|
|
|
|
} else { // leave stations without docks right aways
|
|
|
|
v->current_order.MakeLeaveStation();
|
2011-01-31 20:44:15 +00:00
|
|
|
v->IncrementRealOrderIndex();
|
2008-06-11 13:54:01 +00:00
|
|
|
}
|
2005-05-02 22:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-06-11 13:54:01 +00:00
|
|
|
} else {
|
|
|
|
/* New tile */
|
2012-09-21 20:28:23 +00:00
|
|
|
if (!IsValidTile(gp.new_tile)) goto reverse_direction;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-11 13:54:01 +00:00
|
|
|
dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
|
|
|
|
assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW);
|
2012-09-21 20:28:23 +00:00
|
|
|
DiagDirection diagdir = DirToDiagDir(dir);
|
2008-06-11 13:54:01 +00:00
|
|
|
tracks = GetAvailShipTracks(gp.new_tile, diagdir);
|
|
|
|
if (tracks == TRACK_BIT_NONE) goto reverse_direction;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-11 13:54:01 +00:00
|
|
|
/* Choose a direction, and continue if we find one */
|
|
|
|
track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
|
|
|
|
if (track == INVALID_TRACK) goto reverse_direction;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-11 13:54:01 +00:00
|
|
|
b = _ship_subcoord[diagdir][track];
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-06-11 13:54:01 +00:00
|
|
|
gp.x = (gp.x & ~0xF) | b[0];
|
|
|
|
gp.y = (gp.y & ~0xF) | b[1];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-11 13:54:01 +00:00
|
|
|
/* Call the landscape function and tell it that the vehicle entered the tile */
|
|
|
|
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
|
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-11 13:54:01 +00:00
|
|
|
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
|
|
|
|
v->tile = gp.new_tile;
|
2009-05-22 18:17:20 +00:00
|
|
|
v->state = TrackToTrackBits(track);
|
2011-07-07 14:16:22 +00:00
|
|
|
|
|
|
|
/* Update ship cache when the water class changes. Aqueducts are always canals. */
|
2011-07-12 17:12:10 +00:00
|
|
|
WaterClass old_wc = GetEffectiveWaterClass(gp.old_tile);
|
|
|
|
WaterClass new_wc = GetEffectiveWaterClass(gp.new_tile);
|
2011-07-07 14:16:22 +00:00
|
|
|
if (old_wc != new_wc) v->UpdateCache();
|
2008-06-11 13:54:01 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-06-11 13:54:01 +00:00
|
|
|
v->direction = (Direction)b[2];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* On a bridge */
|
|
|
|
if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
|
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
2012-01-02 13:44:30 +00:00
|
|
|
VehicleUpdatePosition(v);
|
|
|
|
if ((v->vehstatus & VS_HIDDEN) == 0) VehicleUpdateViewport(v, true);
|
2008-06-11 13:54:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* update image of ship, as well as delta XY */
|
|
|
|
dir = ShipGetNewDirection(v, gp.x, gp.y);
|
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
2011-11-04 10:18:13 +00:00
|
|
|
v->z_pos = GetSlopePixelZ(gp.x, gp.y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
getout:
|
2012-01-02 13:44:37 +00:00
|
|
|
VehicleUpdatePosition(v);
|
2009-08-08 18:45:12 +00:00
|
|
|
v->UpdateViewport(true, true);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
reverse_direction:
|
2006-03-08 06:55:33 +00:00
|
|
|
dir = ReverseDir(v->direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->direction = dir;
|
|
|
|
goto getout;
|
|
|
|
}
|
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
bool Ship::Tick()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-02-13 19:24:40 +00:00
|
|
|
if (!(this->vehstatus & VS_STOPPED)) this->running_ticks++;
|
|
|
|
|
2007-07-01 19:24:54 +00:00
|
|
|
ShipController(this);
|
2009-05-22 13:53:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Build a ship.
|
2010-08-17 23:15:55 +00:00
|
|
|
* @param tile tile of the depot where ship is built.
|
|
|
|
* @param flags type of operation.
|
|
|
|
* @param e the engine to build.
|
|
|
|
* @param data unused.
|
|
|
|
* @param ret[out] the vehicle that has been built.
|
|
|
|
* @return the cost of this operation or an error.
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2010-08-17 23:15:55 +00:00
|
|
|
CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, const Engine *e, uint16 data, Vehicle **ret)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-08-19 13:44:41 +00:00
|
|
|
tile = GetShipDepotNorthTile(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2006-04-10 07:15:58 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
2009-10-06 19:17:07 +00:00
|
|
|
const ShipVehicleInfo *svi = &e->u.ship;
|
2004-12-03 21:57:05 +00:00
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
Ship *v = new Ship();
|
2010-08-17 23:15:55 +00:00
|
|
|
*ret = v;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
v->owner = _current_company;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->tile = tile;
|
2006-04-23 19:35:36 +00:00
|
|
|
x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
|
|
|
|
y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
2011-11-04 10:18:13 +00:00
|
|
|
v->z_pos = GetSlopePixelZ(x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
v->spritenum = svi->image_index;
|
2009-02-21 12:52:41 +00:00
|
|
|
v->cargo_type = e->GetDefaultCargoType();
|
2004-12-03 21:57:05 +00:00
|
|
|
v->cargo_cap = svi->capacity;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-02-02 16:16:43 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2010-08-17 23:15:55 +00:00
|
|
|
v->engine_type = e->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->reliability = e->reliability;
|
|
|
|
v->reliability_spd_dec = e->reliability_spd_dec;
|
2009-06-16 13:52:18 +00:00
|
|
|
v->max_age = e->GetLifeLengthInDays();
|
2005-10-29 21:54:28 +00:00
|
|
|
_new_vehicle_id = v->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
v->state = TRACK_BIT_DEPOT;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-02-14 17:04:01 +00:00
|
|
|
v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_ships);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->date_of_last_service = _date;
|
2006-08-20 19:05:28 +00:00
|
|
|
v->build_year = _cur_year;
|
2009-05-13 21:45:56 +00:00
|
|
|
v->cur_image = SPR_IMG_QUERY;
|
2005-12-28 22:29:59 +00:00
|
|
|
v->random_bits = VehicleRandomBits();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-11-06 13:03:17 +00:00
|
|
|
v->UpdateCache();
|
|
|
|
|
2007-11-20 13:35:54 +00:00
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
|
2013-02-14 17:24:55 +00:00
|
|
|
v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
|
2007-02-28 17:59:05 +00:00
|
|
|
|
2009-05-31 12:03:14 +00:00
|
|
|
v->InvalidateNewGRFCacheOfChain();
|
|
|
|
|
2011-11-09 16:38:50 +00:00
|
|
|
v->cargo_cap = e->DetermineCapacity(v);
|
2007-05-12 07:05:34 +00:00
|
|
|
|
2009-05-31 12:03:14 +00:00
|
|
|
v->InvalidateNewGRFCacheOfChain();
|
|
|
|
|
2012-01-02 13:44:30 +00:00
|
|
|
VehicleUpdatePosition(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2010-08-17 23:15:55 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-04-11 08:14:43 +00:00
|
|
|
bool Ship::FindClosestDepot(TileIndex *location, DestinationID *destination, bool *reverse)
|
|
|
|
{
|
2009-12-20 16:19:47 +00:00
|
|
|
const Depot *depot = FindClosestShipDepot(this, 0);
|
2008-04-11 08:14:43 +00:00
|
|
|
|
|
|
|
if (depot == NULL) return false;
|
|
|
|
|
|
|
|
if (location != NULL) *location = depot->xy;
|
|
|
|
if (destination != NULL) *destination = depot->index;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|