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"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.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"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "vehicle_gui.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 "functions.h"
|
|
|
|
#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"
|
2008-01-07 09:19:53 +00:00
|
|
|
#include "autoreplace_gui.h"
|
2008-04-20 11:12:07 +00:00
|
|
|
#include "effectvehicle_func.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "effectvehicle_base.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"
|
2009-06-24 21:33:11 +00:00
|
|
|
#include "landscape_type.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "engine_base.h"
|
|
|
|
#include "engine_func.h"
|
|
|
|
#include "company_base.h"
|
2008-01-07 00:57:19 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
2009-01-27 19:44:36 +00:00
|
|
|
#include "table/sprites.h"
|
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
|
|
|
}
|
|
|
|
|
2008-04-21 20:50:58 +00:00
|
|
|
static SpriteID GetShipIcon(EngineID engine)
|
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)) {
|
2008-04-21 20:50:58 +00:00
|
|
|
SpriteID sprite = GetCustomVehicleIcon(engine, DIR_W);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-01-21 01:38:13 +00:00
|
|
|
void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal)
|
2008-04-21 20:50:58 +00:00
|
|
|
{
|
2009-11-16 17:58:46 +00:00
|
|
|
SpriteID sprite = GetShipIcon(engine);
|
|
|
|
const Sprite *real_sprite = GetSprite(sprite, ST_NORMAL);
|
|
|
|
preferred_x = Clamp(preferred_x, left - real_sprite->x_offs, right - real_sprite->width - real_sprite->x_offs);
|
|
|
|
DrawSprite(sprite, pal, preferred_x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-10 13:37:32 +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
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param width The width of the sprite
|
|
|
|
* @param height The height of the sprite
|
2007-02-10 13:37:32 +00:00
|
|
|
*/
|
|
|
|
void GetShipSpriteSize(EngineID engine, uint &width, uint &height)
|
|
|
|
{
|
2008-09-02 15:20:38 +00:00
|
|
|
const Sprite *spr = GetSprite(GetShipIcon(engine), ST_NORMAL);
|
2007-02-10 13:37:32 +00:00
|
|
|
|
|
|
|
width = spr->width;
|
|
|
|
height = spr->height;
|
|
|
|
}
|
|
|
|
|
2008-04-21 20:50:58 +00:00
|
|
|
SpriteID Ship::GetImage(Direction direction) 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)) {
|
2008-04-21 20:50:58 +00:00
|
|
|
SpriteID sprite = GetCustomVehicleSprite(this, direction);
|
2005-11-14 19:48:04 +00:00
|
|
|
if (sprite != 0) return sprite;
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2009-06-16 13:31:06 +00:00
|
|
|
spritenum = Engine::Get(this->engine_type)->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;
|
2007-08-29 21:49:08 +00:00
|
|
|
if (v->IsInDepot()) {
|
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();
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
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;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-01-22 21:33:08 +00:00
|
|
|
Money Ship::GetRunningCost() const
|
|
|
|
{
|
2009-11-24 13:12:34 +00:00
|
|
|
const Engine *e = Engine::Get(this->engine_type);
|
|
|
|
uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
|
2009-11-24 13:15:58 +00:00
|
|
|
return GetPrice(PR_RUNNING_SHIP, cost_factor, e->grffile);
|
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
|
|
|
{
|
2008-02-01 22:02:14 +00:00
|
|
|
if ((++this->day_counter & 7) == 0)
|
|
|
|
DecreaseVehicleValue(this);
|
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);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static void HandleBrokenShip(Vehicle *v)
|
|
|
|
{
|
|
|
|
if (v->breakdown_ctr != 1) {
|
|
|
|
v->breakdown_ctr = 1;
|
|
|
|
v->cur_speed = 0;
|
|
|
|
|
|
|
|
if (v->breakdowns_since_last_service != 255)
|
|
|
|
v->breakdowns_since_last_service++;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-09-20 19:36:27 +00:00
|
|
|
v->MarkDirty();
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_VIEW, v->index);
|
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-09-27 18:17:01 +00:00
|
|
|
if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
|
2008-05-29 15:13:28 +00:00
|
|
|
SndPlayVehicleFx((_settings_game.game_creation.landscape != LT_TOYLAND) ?
|
2006-09-27 18:17:01 +00:00
|
|
|
SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (!(v->vehstatus & VS_HIDDEN)) {
|
2009-05-22 18:56:25 +00:00
|
|
|
EffectVehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
|
|
|
|
if (u != NULL) u->animation_state = v->breakdown_delay * 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(v->tick_counter & 1)) {
|
|
|
|
if (!--v->breakdown_delay) {
|
|
|
|
v->breakdown_ctr = 0;
|
2009-09-20 19:36:27 +00:00
|
|
|
v->MarkDirty();
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_VIEW, v->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
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 {
|
2009-05-09 13:37:18 +00:00
|
|
|
this->IncrementOrderIndex();
|
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
|
|
|
{
|
2007-05-01 16:35:14 +00:00
|
|
|
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
|
2004-08-09 17:04:08 +00:00
|
|
|
static const uint32 _delta_xy_table[8] = {
|
2007-05-01 16:35:14 +00:00
|
|
|
MKIT( 6, 6, -3, -3),
|
|
|
|
MKIT( 6, 32, -3, -16),
|
|
|
|
MKIT( 6, 6, -3, -3),
|
|
|
|
MKIT(32, 6, -16, -3),
|
|
|
|
MKIT( 6, 6, -3, -3),
|
|
|
|
MKIT( 6, 32, -3, -16),
|
|
|
|
MKIT( 6, 6, -3, -3),
|
|
|
|
MKIT(32, 6, -16, -3),
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
#undef MKIT
|
2007-05-01 16:35:14 +00:00
|
|
|
|
|
|
|
uint32 x = _delta_xy_table[direction];
|
|
|
|
this->x_offs = GB(x, 0, 8);
|
|
|
|
this->y_offs = GB(x, 8, 8);
|
2008-04-01 14:03:20 +00:00
|
|
|
this->x_extent = GB(x, 16, 8);
|
|
|
|
this->y_extent = GB(x, 24, 8);
|
|
|
|
this->z_extent = 6;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-10-04 12:01:59 +00:00
|
|
|
void RecalcShipStuff(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-08-08 18:45:12 +00:00
|
|
|
v->UpdateViewport(false, true);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
|
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
|
|
|
};
|
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
static void CheckShipLeaveDepot(Ship *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-08-29 21:49:08 +00:00
|
|
|
if (!v->IsInDepot()) return;
|
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
|
|
|
|
2008-04-24 13:05:51 +00:00
|
|
|
/* Check first (north) side */
|
2009-12-01 23:19:46 +00:00
|
|
|
if (DiagdirReachesTracks((DiagDirection)axis) & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
|
2008-04-24 13:05:51 +00:00
|
|
|
v->direction = ReverseDir(AxisToDirection(axis));
|
|
|
|
/* Check second (south) side */
|
2009-12-01 23:19:46 +00:00
|
|
|
} else if (DiagdirReachesTracks((DiagDirection)(axis + 2)) & GetTileShipTrackStatus(TILE_ADD(tile, -2 * ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
|
2008-04-24 13:05:51 +00:00
|
|
|
v->direction = AxisToDirection(axis);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
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;
|
|
|
|
RecalcShipStuff(v);
|
|
|
|
|
|
|
|
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);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool ShipAccelerate(Vehicle *v)
|
|
|
|
{
|
|
|
|
uint spd;
|
|
|
|
byte t;
|
|
|
|
|
2009-09-22 19:28:57 +00:00
|
|
|
spd = min(v->cur_speed + 1, GetVehicleProperty(v, PROP_SHIP_SPEED, v->max_speed));
|
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;
|
2008-05-29 15:13:28 +00:00
|
|
|
if (_settings_client.gui.vehicle_speed)
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Decrease somewhat when turning */
|
2005-11-14 19:48:04 +00:00
|
|
|
if (!(v->direction & 1)) spd = spd * 3 / 4;
|
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);
|
|
|
|
}
|
|
|
|
|
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,
|
2008-09-30 20:39:50 +00:00
|
|
|
(v->owner == _local_company) ? NS_ARRIVAL_COMPANY : NS_ARRIVAL_OTHER,
|
2004-08-09 17:04:08 +00:00
|
|
|
v->index,
|
2008-09-13 10:19:51 +00:00
|
|
|
st->index
|
|
|
|
);
|
2009-01-12 17:11:45 +00:00
|
|
|
AI::NewEvent(v->owner, new AIEventStationFirstVehicle(st->index, v->index));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/** returns the track to choose on the next tile, or -1 when it's better to
|
2005-01-31 11:23:10 +00:00
|
|
|
* reverse. The tile given is the tile we are about to enter, enterdir is the
|
|
|
|
* direction in which we are entering the tile */
|
2009-12-02 09:31:38 +00:00
|
|
|
static Track ChooseShipTrack(const 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
|
|
|
|
2008-05-29 15:13:28 +00:00
|
|
|
switch (_settings_game.pf.pathfinder_for_ships) {
|
2009-12-01 23:22:41 +00:00
|
|
|
case VPF_OPF: return OPFShipChooseTrack(v, tile, enterdir, tracks);
|
2009-12-01 23:56:04 +00:00
|
|
|
case VPF_NPF: return NPFShipChooseTrack(v, tile, enterdir, tracks);
|
2009-12-02 10:44:38 +00:00
|
|
|
case VPF_YAPF: return YapfShipChooseTrack(v, tile, enterdir, tracks);
|
2009-12-01 23:56:04 +00:00
|
|
|
default: NOT_REACHED();
|
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
|
|
|
|
|
|
|
if (v->breakdown_ctr != 0) {
|
|
|
|
if (v->breakdown_ctr <= 2) {
|
|
|
|
HandleBrokenShip(v);
|
|
|
|
return;
|
|
|
|
}
|
2008-04-19 21:23:42 +00:00
|
|
|
if (!v->current_order.IsType(OT_LOADING)) v->breakdown_ctr--;
|
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
|
|
|
|
|
|
|
CheckShipLeaveDepot(v);
|
|
|
|
|
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();
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
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);
|
2009-05-09 13:37:18 +00:00
|
|
|
v->IncrementOrderIndex();
|
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();
|
2009-05-09 13:37:18 +00:00
|
|
|
v->IncrementOrderIndex();
|
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 {
|
|
|
|
DiagDirection diagdir;
|
|
|
|
/* New tile */
|
|
|
|
if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) {
|
|
|
|
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);
|
|
|
|
diagdir = DirToDiagDir(dir);
|
|
|
|
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);
|
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;
|
2009-03-11 20:43:14 +00:00
|
|
|
VehicleMove(v, !(v->vehstatus & VS_HIDDEN));
|
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;
|
|
|
|
v->z_pos = GetSlopeZ(gp.x, gp.y);
|
|
|
|
|
|
|
|
getout:
|
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
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Build a ship.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile tile of depot where ship is built
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param flags type of operation
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 ship type being built (engine)
|
2008-07-09 20:55:47 +00:00
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-02-04 14:24:23 +00:00
|
|
|
UnitID unit_num;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-08-05 17:59:21 +00:00
|
|
|
if (!IsEngineBuildable(p1, VEH_SHIP, _current_company)) return_cmd_error(STR_ERROR_SHIP_NOT_AVAILABLE);
|
2005-01-27 21:00:05 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
const Engine *e = Engine::Get(p1);
|
2009-01-25 00:57:03 +00:00
|
|
|
CommandCost value(EXPENSES_NEW_VEHICLES, e->GetCost());
|
|
|
|
|
2009-02-21 12:52:41 +00:00
|
|
|
/* Engines without valid cargo should not be available */
|
|
|
|
if (e->GetDefaultCargoType() == CT_INVALID) return CMD_ERROR;
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
if (flags & DC_QUERY_COST) return value;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-06 16:58:42 +00:00
|
|
|
/* The ai_new queries the vehicle cost before building the route,
|
|
|
|
* so we must check against cheaters no sooner than now. --pasky */
|
2008-04-17 18:24:45 +00:00
|
|
|
if (!IsShipDepotTile(tile)) return CMD_ERROR;
|
2008-09-30 20:39:50 +00:00
|
|
|
if (!IsTileOwner(tile, _current_company)) return CMD_ERROR;
|
2005-03-06 16:58:42 +00:00
|
|
|
|
2008-07-09 20:55:47 +00:00
|
|
|
unit_num = (flags & DC_AUTOREPLACE) ? 0 : GetFreeUnitNumber(VEH_SHIP);
|
2006-05-11 13:31:14 +00:00
|
|
|
|
2009-01-09 14:59:02 +00:00
|
|
|
if (!Vehicle::CanAllocateItem() || unit_num > _settings_game.vehicle.max_ships)
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_MANY_VEHICLES_IN_GAME);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
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();
|
2004-08-09 17:04:08 +00:00
|
|
|
v->unitnumber = unit_num;
|
|
|
|
|
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;
|
2007-04-18 22:10:36 +00:00
|
|
|
v->z_pos = GetSlopeZ(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;
|
2007-06-18 19:53:50 +00:00
|
|
|
v->value = value.GetCost();
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-02-02 16:16:43 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2004-12-03 21:57:05 +00:00
|
|
|
v->max_speed = svi->max_speed;
|
2005-10-01 12:43:34 +00:00
|
|
|
v->engine_type = p1;
|
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
|
|
|
|
2009-05-26 21:59:49 +00:00
|
|
|
v->service_interval = 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
|
|
|
|
2007-11-20 13:35:54 +00:00
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
|
2007-02-28 17:59:05 +00:00
|
|
|
|
2009-05-31 12:03:14 +00:00
|
|
|
v->InvalidateNewGRFCacheOfChain();
|
|
|
|
|
2009-10-28 18:31:16 +00:00
|
|
|
v->cargo_cap = GetVehicleCapacity(v);
|
2007-05-12 07:05:34 +00:00
|
|
|
|
2009-05-31 12:03:14 +00:00
|
|
|
v->InvalidateNewGRFCacheOfChain();
|
|
|
|
|
2009-03-11 20:43:14 +00:00
|
|
|
VehicleMove(v, false);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-10-05 12:59:28 +00:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_COMPANY, v->owner);
|
2010-02-10 21:28:26 +00:00
|
|
|
if (IsLocalCompany()) {
|
2007-08-07 23:07:10 +00:00
|
|
|
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Ship window
|
2010-02-10 21:28:26 +00:00
|
|
|
}
|
2007-02-06 11:11:12 +00:00
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
Company::Get(_current_company)->num_engines[p1]++;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Sell a ship.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param flags type of operation
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 vehicle ID to be sold
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdSellShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-26 22:45:48 +00:00
|
|
|
Ship *v = Ship::GetIfValid(p1);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (v == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-08-05 17:59:21 +00:00
|
|
|
if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_SELL_DESTROYED_VEHICLE);
|
2007-10-08 20:06:37 +00:00
|
|
|
|
2007-08-29 21:49:08 +00:00
|
|
|
if (!v->IsStoppedInDepot()) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_SHIP_MUST_BE_STOPPED_IN_DEPOT);
|
2006-06-05 11:28:00 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2010-03-13 17:11:28 +00:00
|
|
|
ret = CommandCost(EXPENSES_NEW_VEHICLES, -v->value);
|
2007-10-21 16:45:00 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2007-08-03 19:36:00 +00:00
|
|
|
delete v;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-10-21 16:45:00 +00:00
|
|
|
return ret;
|
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;
|
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Send a ship to the depot.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param flags type of operation
|
2005-05-12 00:11:37 +00:00
|
|
|
* @param p1 vehicle ID to send to the depot
|
2006-08-29 23:39:57 +00:00
|
|
|
* @param p2 various bitmasked elements
|
2006-09-01 10:24:15 +00:00
|
|
|
* - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
|
|
|
|
* - p2 bit 8-10 - VLW flag (for mass goto depot)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdSendShipToDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-09-01 10:24:15 +00:00
|
|
|
if (p2 & DEPOT_MASS_SEND) {
|
|
|
|
/* Mass goto depot requested */
|
2006-09-04 15:16:58 +00:00
|
|
|
if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
|
2008-09-30 20:39:50 +00:00
|
|
|
return SendAllVehiclesToDepot(VEH_SHIP, flags, p2 & DEPOT_SERVICE, _current_company, (p2 & VLW_MASK), p1);
|
2006-08-30 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
2009-05-26 22:45:48 +00:00
|
|
|
Ship *v = Ship::GetIfValid(p1);
|
|
|
|
if (v == NULL) return CMD_ERROR;
|
2006-09-02 22:47:45 +00:00
|
|
|
|
2008-04-11 08:40:10 +00:00
|
|
|
return v->SendToDepot(flags, (DepotCommand)(p2 & DEPOT_COMMAND_MASK));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-14 12:36:16 +00:00
|
|
|
/** Refits a ship to the specified cargo type.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-04 01:35:16 +00:00
|
|
|
* @param flags type of operation
|
2005-05-14 12:36:16 +00:00
|
|
|
* @param p1 vehicle ID of the ship to refit
|
|
|
|
* @param p2 various bitstuffed elements
|
|
|
|
* - p2 = (bit 0-7) - the new cargo type to refit to (p2 & 0xFF)
|
2006-06-04 17:38:48 +00:00
|
|
|
* - p2 = (bit 8-15) - the new cargo subtype to refit to
|
2007-04-29 08:43:00 +00:00
|
|
|
* - p2 = (bit 16) - refit only this vehicle (ignored)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2005-05-14 12:36:16 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdRefitShip(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-03-15 00:32:18 +00:00
|
|
|
CargoID new_cid = GB(p2, 0, 8); // gets the cargo number
|
2006-06-04 17:38:48 +00:00
|
|
|
byte new_subtype = GB(p2, 8, 8);
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2009-05-26 22:45:48 +00:00
|
|
|
Ship *v = Ship::GetIfValid(p1);
|
2010-03-13 17:11:28 +00:00
|
|
|
if (v == NULL) return CMD_ERROR;
|
|
|
|
|
|
|
|
CommandCost ret = CheckOwnership(v->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2009-04-21 23:40:56 +00:00
|
|
|
if (!v->IsStoppedInDepot()) return_cmd_error(STR_ERROR_SHIP_MUST_BE_STOPPED_IN_DEPOT);
|
2009-08-05 17:59:21 +00:00
|
|
|
if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_ERROR_CAN_T_REFIT_DESTROYED_VEHICLE);
|
2005-05-14 12:36:16 +00:00
|
|
|
|
|
|
|
/* Check cargo */
|
2009-10-28 21:09:37 +00:00
|
|
|
if (new_cid >= NUM_CARGO) return CMD_ERROR;
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2009-10-28 21:09:37 +00:00
|
|
|
CommandCost cost = RefitVehicle(v, true, new_cid, new_subtype, flags);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2009-02-09 02:57:15 +00:00
|
|
|
v->colourmap = PAL_NONE; // invalidate vehicle colour map
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
|
2008-05-18 16:51:44 +00:00
|
|
|
InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-10-27 20:37:55 +00:00
|
|
|
v->InvalidateNewGRFCacheOfChain(); // always invalidate; querycost might have filled it
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return cost;
|
|
|
|
|
|
|
|
}
|