2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2006-06-05 11:28:00 +00:00
|
|
|
#include "ship.h"
|
2007-12-21 22:50:51 +00:00
|
|
|
#include "tile_cmd.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"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "pathfind.h"
|
2006-04-04 11:35:52 +00:00
|
|
|
#include "station_map.h"
|
2008-03-31 00:06:17 +00:00
|
|
|
#include "station_base.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2008-03-31 00:17:39 +00:00
|
|
|
#include "engine_func.h"
|
2008-04-29 21:31:29 +00:00
|
|
|
#include "engine_base.h"
|
2008-01-12 14:10:35 +00:00
|
|
|
#include "player_func.h"
|
|
|
|
#include "player_base.h"
|
2005-01-31 11:23:10 +00:00
|
|
|
#include "npf.h"
|
2008-04-17 19:10:30 +00:00
|
|
|
#include "depot_base.h"
|
|
|
|
#include "depot_func.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"
|
2006-04-05 18:46:21 +00:00
|
|
|
#include "water_map.h"
|
2006-05-27 16:12:16 +00:00
|
|
|
#include "yapf/yapf.h"
|
|
|
|
#include "debug.h"
|
2006-06-09 07:09:48 +00:00
|
|
|
#include "newgrf_callbacks.h"
|
2006-08-09 21:11:45 +00:00
|
|
|
#include "newgrf_text.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 00:57:19 +00:00
|
|
|
#include "variables.h"
|
2008-01-07 09:19:53 +00:00
|
|
|
#include "autoreplace_gui.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "gfx_func.h"
|
2008-01-13 14:37:30 +00:00
|
|
|
#include "settings_type.h"
|
2008-03-30 23:24:18 +00:00
|
|
|
#include "order_func.h"
|
2008-04-20 11:12:07 +00:00
|
|
|
#include "effectvehicle_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
|
|
|
|
|
|
|
static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
static const TrackBits _ship_sometracks[4] = {
|
|
|
|
TRACK_BIT_X | TRACK_BIT_LOWER | TRACK_BIT_LEFT, // 0x19, // DIAGDIR_NE
|
|
|
|
TRACK_BIT_Y | TRACK_BIT_UPPER | TRACK_BIT_LEFT, // 0x16, // DIAGDIR_SE
|
|
|
|
TRACK_BIT_X | TRACK_BIT_UPPER | TRACK_BIT_RIGHT, // 0x25, // DIAGDIR_SW
|
|
|
|
TRACK_BIT_Y | TRACK_BIT_LOWER | TRACK_BIT_RIGHT, // 0x2A, // DIAGDIR_NW
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
2008-04-21 20:50:58 +00:00
|
|
|
uint8 spritenum = ShipVehInfo(engine)->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
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
spritenum = GetEngine(engine)->image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-21 20:50:58 +00:00
|
|
|
|
|
|
|
return 6 + _ship_sprites[spritenum];
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal)
|
|
|
|
{
|
|
|
|
DrawSprite(GetShipIcon(engine), pal, 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-04-21 20:50:58 +00:00
|
|
|
const Sprite *spr = GetSprite(GetShipIcon(engine));
|
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
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
spritenum = GetEngine(this->engine_type)->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;
|
|
|
|
}
|
|
|
|
|
2005-11-13 13:43:55 +00:00
|
|
|
static const Depot* FindClosestShipDepot(const Vehicle* v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-02-13 14:21:36 +00:00
|
|
|
if (_patches.pathfinder_for_ships == VPF_NPF) { /* NPF is used */
|
2007-01-10 18:56:51 +00:00
|
|
|
Trackdir trackdir = GetVehicleTrackdir(v);
|
2008-02-13 17:54:11 +00:00
|
|
|
NPFFoundTargetData ftd = NPFRouteToDepotTrialError(v->tile, trackdir, false, TRANSPORT_WATER, 0, v->owner, INVALID_RAILTYPES);
|
|
|
|
|
|
|
|
if (ftd.best_bird_dist == 0) return GetDepotByTile(ftd.node.tile); /* Found target */
|
|
|
|
|
|
|
|
return NULL; /* Did not find target */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OPF or YAPF - find the closest depot */
|
|
|
|
|
|
|
|
const Depot* depot;
|
|
|
|
const Depot* best_depot = NULL;
|
|
|
|
uint best_dist = UINT_MAX;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2008-04-08 15:48:32 +00:00
|
|
|
if (_patches.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;
|
|
|
|
}
|
|
|
|
|
2007-08-31 17:13:39 +00:00
|
|
|
const Depot *depot = FindClosestShipDepot(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-02-06 10:18:47 +00:00
|
|
|
if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) {
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT)) {
|
|
|
|
v->current_order.MakeDummy();
|
2008-01-18 13:02:47 +00:00
|
|
|
InvalidateWindowWidget(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;
|
2008-01-18 13:02:47 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2004-08-09 17:04: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;
|
|
|
|
|
|
|
|
CommandCost cost(EXPENSES_SHIP_RUN, GetVehicleProperty(this, 0x0F, ShipVehInfo(this->engine_type)->running_cost) * _price.ship_running * this->running_ticks / (364 * 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-02-01 22:02:14 +00:00
|
|
|
SubtractMoneyFromPlayerFract(this->owner, cost);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
|
2007-04-04 01:35:16 +00:00
|
|
|
/* we need this for the profit */
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_SHIPS_LIST);
|
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
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
InvalidateWindow(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)) {
|
2007-03-22 03:42:43 +00:00
|
|
|
SndPlayVehicleFx((_opt.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)) {
|
|
|
|
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
|
2008-04-20 10:13:54 +00:00
|
|
|
if (u != NULL) u->u.effect.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;
|
2005-01-24 22:24:47 +00:00
|
|
|
InvalidateWindow(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
|
|
|
{
|
2007-07-01 19:11:47 +00:00
|
|
|
this->cur_image = this->GetImage(this->direction);
|
2008-01-16 21:17:31 +00:00
|
|
|
MarkSingleVehicleDirty(this);
|
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;
|
|
|
|
|
2008-04-14 07:08:43 +00:00
|
|
|
const Station *st = GetStation(station);
|
2008-04-05 10:55:50 +00:00
|
|
|
if (st->dock_tile != 0) {
|
|
|
|
return TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2008-04-05 10:55:50 +00:00
|
|
|
this->cur_order_index++;
|
|
|
|
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
|
|
|
{
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2007-07-01 19:11:47 +00:00
|
|
|
v->cur_image = v->GetImage(v->direction);
|
2007-04-29 22:33:51 +00:00
|
|
|
v->MarkDirty();
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
static void CheckShipLeaveDepot(Vehicle *v)
|
|
|
|
{
|
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 */
|
2006-04-05 18:46:21 +00:00
|
|
|
if (_ship_sometracks[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 */
|
2006-04-05 18:46:21 +00:00
|
|
|
} else if (_ship_sometracks[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
|
|
|
|
|
|
|
v->u.ship.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);
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_SHIPS_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool ShipAccelerate(Vehicle *v)
|
|
|
|
{
|
|
|
|
uint spd;
|
|
|
|
byte t;
|
|
|
|
|
2007-04-18 18:37:40 +00:00
|
|
|
spd = min(v->cur_speed + 1, GetVehicleProperty(v, 0x0B, v->max_speed));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 01:35:16 +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;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_patches.vehicle_speed)
|
2008-01-18 13:02:47 +00:00
|
|
|
InvalidateWindowWidget(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);
|
|
|
|
}
|
|
|
|
|
2007-06-18 10:48:15 +00:00
|
|
|
static CommandCost EstimateShipCost(EngineID engine_type)
|
2005-05-12 00:11:37 +00:00
|
|
|
{
|
2008-01-09 16:55:48 +00:00
|
|
|
return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine_type, 0x0A, ShipVehInfo(engine_type)->base_cost) * (_price.ship_base >> 3) >> 5);
|
2005-05-12 00:11:37 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-13 13:43:55 +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);
|
2004-08-09 17:04:08 +00:00
|
|
|
AddNewsItem(
|
|
|
|
STR_9833_CITIZENS_CELEBRATE_FIRST,
|
2008-03-28 08:53:36 +00:00
|
|
|
NM_THIN, NF_VIEWPORT | NF_VEHICLE, (v->owner == _local_player) ? NT_ARRIVAL_PLAYER : NT_ARRIVAL_OTHER, DNC_NONE,
|
2004-08-09 17:04:08 +00:00
|
|
|
v->index,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct PathFindShip {
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex skiptile;
|
|
|
|
TileIndex dest_coords;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint best_bird_dist;
|
|
|
|
uint best_length;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-20 15:13:42 +00:00
|
|
|
static bool ShipTrackFollower(TileIndex tile, PathFindShip *pfs, int track, uint length)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Found dest? */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (tile == pfs->dest_coords) {
|
|
|
|
pfs->best_bird_dist = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
pfs->best_length = minu(pfs->best_length, length);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Skip this tile in the calculation */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (tile != pfs->skiptile) {
|
2005-01-31 07:23:15 +00:00
|
|
|
pfs->best_bird_dist = minu(pfs->best_bird_dist, DistanceMaxPlusManhattan(pfs->dest_coords, tile));
|
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 false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const byte _ship_search_directions[6][4] = {
|
|
|
|
{ 0, 9, 2, 9 },
|
|
|
|
{ 9, 1, 9, 3 },
|
|
|
|
{ 9, 0, 3, 9 },
|
|
|
|
{ 1, 9, 9, 2 },
|
|
|
|
{ 3, 2, 9, 9 },
|
|
|
|
{ 9, 9, 1, 0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static const byte _pick_shiptrack_table[6] = {1, 3, 2, 2, 0, 0};
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
static uint FindShipTrack(Vehicle *v, TileIndex tile, DiagDirection dir, TrackBits bits, TileIndex skiptile, Track *track)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
PathFindShip pfs;
|
2007-01-10 18:56:51 +00:00
|
|
|
Track i, best_track;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint best_bird_dist = 0;
|
|
|
|
uint best_length = 0;
|
|
|
|
uint r;
|
|
|
|
byte ship_dir = v->direction & 3;
|
|
|
|
|
|
|
|
pfs.dest_coords = v->dest_tile;
|
|
|
|
pfs.skiptile = skiptile;
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
best_track = INVALID_TRACK;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
do {
|
2007-01-11 10:34:42 +00:00
|
|
|
i = RemoveFirstTrack(&bits);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
pfs.best_bird_dist = (uint)-1;
|
|
|
|
pfs.best_length = (uint)-1;
|
|
|
|
|
2008-04-02 13:57:25 +00:00
|
|
|
FollowTrack(tile, PATHFIND_FLAGS_SHIP_MODE | PATHFIND_FLAGS_DISABLE_TILE_HASH, TRANSPORT_WATER, 0, (DiagDirection)_ship_search_directions[i][dir], (TPFEnumProc*)ShipTrackFollower, NULL, &pfs);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
if (best_track != INVALID_TRACK) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (pfs.best_bird_dist != 0) {
|
|
|
|
/* neither reached the destination, pick the one with the smallest bird dist */
|
|
|
|
if (pfs.best_bird_dist > best_bird_dist) goto bad;
|
|
|
|
if (pfs.best_bird_dist < best_bird_dist) goto good;
|
|
|
|
} else {
|
|
|
|
if (pfs.best_length > best_length) goto bad;
|
|
|
|
if (pfs.best_length < best_length) goto good;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
|
|
|
/* if we reach this position, there's two paths of equal value so far.
|
2004-08-09 17:04:08 +00:00
|
|
|
* pick one randomly. */
|
2005-07-21 06:31:02 +00:00
|
|
|
r = GB(Random(), 0, 8);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (_pick_shiptrack_table[i] == ship_dir) r += 80;
|
|
|
|
if (_pick_shiptrack_table[best_track] == ship_dir) r -= 80;
|
|
|
|
if (r <= 127) goto bad;
|
|
|
|
}
|
|
|
|
good:;
|
|
|
|
best_track = i;
|
|
|
|
best_bird_dist = pfs.best_bird_dist;
|
|
|
|
best_length = pfs.best_length;
|
|
|
|
bad:;
|
|
|
|
|
|
|
|
} while (bits != 0);
|
|
|
|
|
|
|
|
*track = best_track;
|
|
|
|
return best_bird_dist;
|
|
|
|
}
|
|
|
|
|
2008-02-08 16:25:55 +00:00
|
|
|
static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailTypes railtypes)
|
2006-05-27 16:12:16 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
void* perf = NpfBeginInterval();
|
2008-02-08 16:25:55 +00:00
|
|
|
NPFFoundTargetData ret = NPFRouteToStationOrTile(tile, trackdir, ignore_start_tile, target, type, 0, owner, railtypes);
|
2006-05-27 16:12:16 +00:00
|
|
|
int t = NpfEndInterval(perf);
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(yapf, 4, "[NPFW] %d us - %d rounds - %d open - %d closed -- ", t, 0, _aystar_stats_open_size, _aystar_stats_closed_size);
|
2006-05-27 16:12:16 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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 */
|
2007-01-10 18:56:51 +00:00
|
|
|
static Track ChooseShipTrack(Vehicle *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-02-13 17:54:11 +00:00
|
|
|
switch (_patches.pathfinder_for_ships) {
|
|
|
|
case VPF_YAPF: { /* YAPF */
|
|
|
|
Trackdir trackdir = YapfChooseShipTrack(v, tile, enterdir, tracks);
|
|
|
|
if (trackdir != INVALID_TRACKDIR) return TrackdirToTrack(trackdir);
|
|
|
|
} break;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2008-02-13 17:54:11 +00:00
|
|
|
case VPF_NPF: { /* NPF */
|
|
|
|
NPFFindStationOrTileData fstd;
|
|
|
|
Trackdir trackdir = GetVehicleTrackdir(v);
|
|
|
|
assert(trackdir != INVALID_TRACKDIR); // Check that we are not in a depot
|
|
|
|
|
|
|
|
NPFFillWithOrderData(&fstd, v);
|
|
|
|
|
|
|
|
NPFFoundTargetData ftd = PerfNPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, TRANSPORT_WATER, v->owner, INVALID_RAILTYPES);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2005-04-15 13:48:08 +00:00
|
|
|
/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
|
2008-02-13 17:54:11 +00:00
|
|
|
* the direction we need to take to get there, if ftd.best_bird_dist is not 0,
|
|
|
|
* we did not find our target, but ftd.best_trackdir contains the direction leading
|
|
|
|
* to the tile closest to our target. */
|
|
|
|
if (ftd.best_trackdir != 0xff) return TrackdirToTrack(ftd.best_trackdir); /* TODO: Wrapper function? */
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
case VPF_OPF: { /* OPF */
|
|
|
|
TileIndex tile2 = TILE_ADD(tile, -TileOffsByDiagDir(enterdir));
|
|
|
|
Track track;
|
|
|
|
|
|
|
|
/* Let's find out how far it would be if we would reverse first */
|
|
|
|
TrackBits b = GetTileShipTrackStatus(tile2) & _ship_sometracks[ReverseDiagDir(enterdir)] & v->u.ship.state;
|
|
|
|
|
|
|
|
uint distr = UINT_MAX; // distance if we reversed
|
|
|
|
if (b != 0) {
|
|
|
|
distr = FindShipTrack(v, tile2, ReverseDiagDir(enterdir), b, tile, &track);
|
|
|
|
if (distr != UINT_MAX) distr++; // penalty for reversing
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And if we would not reverse? */
|
|
|
|
uint dist = FindShipTrack(v, tile, enterdir, tracks, 0, &track);
|
|
|
|
|
|
|
|
if (dist <= distr) return track;
|
|
|
|
} break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-02-13 17:54:11 +00:00
|
|
|
|
|
|
|
return INVALID_TRACK; /* We could better reverse */
|
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];
|
|
|
|
}
|
|
|
|
|
2008-02-20 17:49:50 +00:00
|
|
|
static inline TrackBits GetAvailShipTracks(TileIndex tile, int dir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-02-20 17:49:50 +00:00
|
|
|
return GetTileShipTrackStatus(tile) & _ship_sometracks[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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void ShipController(Vehicle *v)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
BeginVehicleMove(v);
|
|
|
|
|
2007-02-25 10:49:13 +00:00
|
|
|
GetNewVehiclePosResult gp = GetNewVehiclePos(v);
|
2007-02-25 10:25:25 +00:00
|
|
|
if (gp.old_tile == gp.new_tile) {
|
2007-02-13 11:29:20 +00:00
|
|
|
/* Staying in tile */
|
2007-08-29 21:49:08 +00:00
|
|
|
if (v->IsInDepot()) {
|
2004-08-09 17:04:08 +00:00
|
|
|
gp.x = v->x_pos;
|
|
|
|
gp.y = v->y_pos;
|
|
|
|
} else {
|
2007-02-13 11:29:20 +00:00
|
|
|
/* Not inside depot */
|
2004-08-09 17:04:08 +00:00
|
|
|
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-02 22:13:20 +00:00
|
|
|
/* A leave station order only needs one tick to get processed, so we can
|
|
|
|
* always skip ahead. */
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_LEAVESTATION)) {
|
2007-03-08 21:39:34 +00:00
|
|
|
v->current_order.Free();
|
2008-01-18 13:02:47 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2005-05-02 22:13:20 +00:00
|
|
|
} else if (v->dest_tile != 0) {
|
|
|
|
/* We have a target, let's see if we reached it... */
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_STATION) &&
|
2005-05-02 22:13:20 +00:00
|
|
|
IsBuoyTile(v->dest_tile) &&
|
|
|
|
DistanceManhattan(v->dest_tile, gp.new_tile) <= 3) {
|
|
|
|
/* We got within 3 tiles of our target buoy, so let's skip to our
|
|
|
|
* next order */
|
2007-06-25 23:14:13 +00:00
|
|
|
UpdateVehicleTimetable(v, true);
|
2005-05-02 22:13:20 +00:00
|
|
|
v->cur_order_index++;
|
2008-04-05 23:36:54 +00:00
|
|
|
v->current_order.MakeDummy();
|
2005-05-02 22:13:20 +00:00
|
|
|
InvalidateVehicleOrder(v);
|
|
|
|
} else {
|
|
|
|
/* Non-buoy orders really need to reach the tile */
|
|
|
|
if (v->dest_tile == gp.new_tile) {
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT)) {
|
2007-02-13 11:29:20 +00:00
|
|
|
if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
|
2006-10-04 12:01:59 +00:00
|
|
|
VehicleEnterDepot(v);
|
2005-05-02 22:13:20 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-04-05 23:36:54 +00:00
|
|
|
} else if (v->current_order.IsType(OT_GOTO_STATION)) {
|
2008-04-06 07:48:51 +00:00
|
|
|
v->last_station_visited = v->current_order.GetDestination();
|
2005-05-02 22:13:20 +00:00
|
|
|
|
|
|
|
/* Process station in the orderlist. */
|
2008-04-06 07:48:51 +00:00
|
|
|
Station *st = GetStation(v->current_order.GetDestination());
|
2007-04-04 01:35:16 +00:00
|
|
|
if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
|
2005-05-02 22:13:20 +00:00
|
|
|
ShipArrivesAt(v, st);
|
2007-04-29 22:33:51 +00:00
|
|
|
v->BeginLoading();
|
2007-04-04 01:35:16 +00:00
|
|
|
} else { // leave stations without docks right aways
|
2008-04-05 23:36:54 +00:00
|
|
|
v->current_order.MakeLeaveStation();
|
2005-05-02 22:13:20 +00:00
|
|
|
v->cur_order_index++;
|
|
|
|
InvalidateVehicleOrder(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2006-03-08 06:55:33 +00:00
|
|
|
DiagDirection diagdir;
|
2007-02-13 11:29:20 +00:00
|
|
|
/* New tile */
|
|
|
|
if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) {
|
2005-01-07 17:02:43 +00:00
|
|
|
goto reverse_direction;
|
2007-02-13 11:29:20 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
|
2006-03-08 06:55:33 +00:00
|
|
|
assert(dir == DIR_NE || dir == DIR_SE || dir == DIR_SW || dir == DIR_NW);
|
|
|
|
diagdir = DirToDiagDir(dir);
|
|
|
|
tracks = GetAvailShipTracks(gp.new_tile, diagdir);
|
2007-02-13 11:29:20 +00:00
|
|
|
if (tracks == TRACK_BIT_NONE) goto reverse_direction;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-13 11:29:20 +00:00
|
|
|
/* Choose a direction, and continue if we find one */
|
2006-03-08 06:55:33 +00:00
|
|
|
track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
|
2007-02-13 11:29:20 +00:00
|
|
|
if (track == INVALID_TRACK) goto reverse_direction;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-03-08 06:55:33 +00:00
|
|
|
b = _ship_subcoord[diagdir][track];
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-13 11:29:20 +00:00
|
|
|
gp.x = (gp.x & ~0xF) | b[0];
|
|
|
|
gp.y = (gp.y & ~0xF) | b[1];
|
2004-08-09 17:04:08 +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);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->tile = gp.new_tile;
|
2007-01-10 18:56:51 +00:00
|
|
|
v->u.ship.state = TrackToTrackBits(track);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
v->direction = (Direction)b[2];
|
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:
|
2007-05-01 16:35:14 +00:00
|
|
|
v->UpdateDeltaXY(dir);
|
2007-07-01 19:11:47 +00:00
|
|
|
v->cur_image = v->GetImage(dir);
|
2004-08-09 17:04:08 +00:00
|
|
|
VehiclePositionChanged(v);
|
|
|
|
EndVehicleMove(v);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AgeShipCargo(Vehicle *v)
|
|
|
|
{
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_age_cargo_skip_counter != 0) return;
|
2007-06-22 11:58:59 +00:00
|
|
|
v->cargo.AgeCargo();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-07-01 19:24:54 +00:00
|
|
|
void 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
|
|
|
AgeShipCargo(this);
|
|
|
|
ShipController(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void ShipsYearlyLoop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type == VEH_SHIP) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->profit_last_year = v->profit_this_year;
|
|
|
|
v->profit_this_year = 0;
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2006-05-11 13:31:14 +00:00
|
|
|
* @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdBuildShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost value;
|
2005-02-04 14:24:23 +00:00
|
|
|
UnitID unit_num;
|
2004-08-09 17:04:08 +00:00
|
|
|
Engine *e;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-02 11:35:08 +00:00
|
|
|
if (!IsEngineBuildable(p1, VEH_SHIP, _current_player)) return_cmd_error(STR_SHIP_NOT_AVAILABLE);
|
2005-01-27 21:00:05 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
value = EstimateShipCost(p1);
|
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;
|
2005-06-04 11:56:32 +00:00
|
|
|
if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
|
2005-03-06 16:58:42 +00:00
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
unit_num = HasBit(p2, 0) ? 0 : GetFreeUnitNumber(VEH_SHIP);
|
2006-05-11 13:31:14 +00:00
|
|
|
|
2007-12-09 15:15:26 +00:00
|
|
|
if (!Vehicle::AllocateList(NULL, 1) || unit_num > _patches.max_ships)
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_00E1_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;
|
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
const ShipVehicleInfo *svi = ShipVehInfo(p1);
|
|
|
|
|
2007-12-09 15:15:26 +00:00
|
|
|
Vehicle *v = new Ship();
|
2004-08-09 17:04:08 +00:00
|
|
|
v->unitnumber = unit_num;
|
|
|
|
|
|
|
|
v->owner = _current_player;
|
|
|
|
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
|
|
|
|
2008-02-13 19:24:40 +00:00
|
|
|
v->running_ticks = 0;
|
|
|
|
|
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;
|
|
|
|
v->cargo_type = svi->cargo_type;
|
2006-05-19 10:04:03 +00:00
|
|
|
v->cargo_subtype = 0;
|
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
|
|
|
|
2005-06-07 18:13:49 +00:00
|
|
|
e = GetEngine(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->reliability = e->reliability;
|
|
|
|
v->reliability_spd_dec = e->reliability_spd_dec;
|
|
|
|
v->max_age = e->lifelength * 366;
|
2005-10-29 21:54:28 +00:00
|
|
|
_new_vehicle_id = v->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-12 19:58:06 +00:00
|
|
|
v->name = NULL;
|
2007-02-13 10:46:45 +00:00
|
|
|
v->u.ship.state = TRACK_BIT_DEPOT;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->service_interval = _patches.servint_ships;
|
|
|
|
v->date_of_last_service = _date;
|
2006-08-20 19:05:28 +00:00
|
|
|
v->build_year = _cur_year;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_image = 0x0E5E;
|
2005-12-28 22:29:59 +00:00
|
|
|
v->random_bits = VehicleRandomBits();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-28 17:59:05 +00:00
|
|
|
v->vehicle_flags = 0;
|
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
|
|
|
|
2007-05-12 07:05:34 +00:00
|
|
|
v->cargo_cap = GetVehicleProperty(v, 0x0D, svi->capacity);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
VehiclePositionChanged(v);
|
|
|
|
|
2006-10-05 12:59:28 +00:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2004-12-10 18:16:08 +00:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
2005-11-08 23:18:09 +00:00
|
|
|
if (IsLocalPlayer())
|
2007-08-07 23:07:10 +00:00
|
|
|
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Ship window
|
2007-02-06 11:11:12 +00:00
|
|
|
|
|
|
|
GetPlayer(_current_player)->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
|
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdSellShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-10-08 20:06:37 +00:00
|
|
|
if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
|
|
|
|
|
2007-08-29 21:49:08 +00:00
|
|
|
if (!v->IsStoppedInDepot()) {
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN);
|
2006-06-05 11:28:00 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-01-09 16:55:48 +00:00
|
|
|
CommandCost ret(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) {
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
2004-12-10 18:16:08 +00:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
2006-12-05 22:59:42 +00:00
|
|
|
DeleteDepotHighlightOfVehicle(v);
|
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
|
|
|
}
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/** Start/Stop 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 ship ID to start/stop
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdStartStopShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2008-04-21 13:45:03 +00:00
|
|
|
Vehicle *v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-08-09 21:11:45 +00:00
|
|
|
/* Check if this ship can be started/stopped. The callback will fail or
|
|
|
|
* return 0xFF if it can. */
|
2008-04-21 13:45:03 +00:00
|
|
|
uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
|
|
|
|
if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF) {
|
2006-08-09 21:11:45 +00:00
|
|
|
StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
|
|
|
|
return_cmd_error(error);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2007-08-29 21:49:08 +00:00
|
|
|
if (v->IsStoppedInDepot()) {
|
2006-03-04 11:01:35 +00:00
|
|
|
DeleteVehicleNews(p1, STR_981C_SHIP_IS_WAITING_IN_DEPOT);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus ^= VS_STOPPED;
|
2007-10-31 22:09:23 +00:00
|
|
|
v->cur_speed = 0;
|
2008-01-18 13:02:47 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
2005-05-12 00:11:37 +00:00
|
|
|
InvalidateWindowClasses(WC_SHIPS_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +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)
|
|
|
|
{
|
|
|
|
const Depot *depot = FindClosestShipDepot(this);
|
|
|
|
|
|
|
|
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)
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdSendShipToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
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;
|
2007-03-08 16:27:54 +00:00
|
|
|
return SendAllVehiclesToDepot(VEH_SHIP, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_MASK), p1);
|
2006-08-30 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 10:24:15 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2008-04-11 08:40:10 +00:00
|
|
|
Vehicle *v = GetVehicle(p1);
|
2005-05-03 19:31:33 +00:00
|
|
|
|
2008-04-11 08:40:10 +00:00
|
|
|
if (v->type != VEH_SHIP) 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)
|
|
|
|
* @return cost of refit or error
|
2005-05-14 12:36:16 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdRefitShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
2008-01-09 16:55:48 +00:00
|
|
|
CommandCost cost(EXPENSES_SHIP_RUN);
|
2006-06-04 17:38:48 +00:00
|
|
|
CargoID new_cid = GB(p2, 0, 8); //gets the cargo number
|
|
|
|
byte new_subtype = GB(p2, 8, 8);
|
2006-06-09 07:09:48 +00:00
|
|
|
uint16 capacity = CALLBACK_FAILED;
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2007-12-27 14:10:47 +00:00
|
|
|
if (!v->IsStoppedInDepot()) return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN);
|
|
|
|
if (v->vehstatus & VS_CRASHED) return_cmd_error(STR_CAN_T_REFIT_DESTROYED_VEHICLE);
|
2005-05-14 12:36:16 +00:00
|
|
|
|
|
|
|
/* Check cargo */
|
|
|
|
if (!ShipVehInfo(v->engine_type)->refittable) return CMD_ERROR;
|
2007-03-16 21:42:11 +00:00
|
|
|
if (new_cid >= NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2006-06-09 07:09:48 +00:00
|
|
|
/* Check the refit capacity callback */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_REFIT_CAPACITY)) {
|
2006-06-09 07:09:48 +00:00
|
|
|
/* Back up the existing cargo type */
|
|
|
|
CargoID temp_cid = v->cargo_type;
|
|
|
|
byte temp_subtype = v->cargo_subtype;
|
|
|
|
v->cargo_type = new_cid;
|
|
|
|
v->cargo_subtype = new_subtype;
|
|
|
|
|
|
|
|
capacity = GetVehicleCallback(CBID_VEHICLE_REFIT_CAPACITY, 0, 0, v->engine_type, v);
|
|
|
|
|
|
|
|
/* Restore the cargo type */
|
|
|
|
v->cargo_type = temp_cid;
|
|
|
|
v->cargo_subtype = temp_subtype;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (capacity == CALLBACK_FAILED) {
|
2007-05-12 07:05:34 +00:00
|
|
|
capacity = GetVehicleProperty(v, 0x0D, ShipVehInfo(v->engine_type)->capacity);
|
2006-06-09 07:09:48 +00:00
|
|
|
}
|
|
|
|
_returned_refit_capacity = capacity;
|
|
|
|
|
2006-10-14 15:15:56 +00:00
|
|
|
if (IsHumanPlayer(v->owner) && new_cid != v->cargo_type) {
|
2006-09-04 09:07:52 +00:00
|
|
|
cost = GetRefitCost(v->engine_type);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2006-06-09 07:09:48 +00:00
|
|
|
v->cargo_cap = capacity;
|
2007-06-22 11:58:59 +00:00
|
|
|
v->cargo.Truncate((v->cargo_type == new_cid) ? capacity : 0);
|
2005-05-14 12:36:16 +00:00
|
|
|
v->cargo_type = new_cid;
|
2006-06-04 17:38:48 +00:00
|
|
|
v->cargo_subtype = new_subtype;
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2006-06-04 17:38:48 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
|
|
|
|
}
|