2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/** @file ship_cmd.cpp */
|
|
|
|
|
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"
|
2004-11-25 10:47:30 +00:00
|
|
|
#include "table/strings.h"
|
2005-07-22 07:02:20 +00:00
|
|
|
#include "functions.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2004-12-15 22:18:54 +00:00
|
|
|
#include "map.h"
|
2005-01-29 12:19:05 +00:00
|
|
|
#include "tile.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "vehicle.h"
|
2007-06-20 19:17:22 +00:00
|
|
|
#include "timetable.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "command.h"
|
|
|
|
#include "pathfind.h"
|
2006-04-04 11:35:52 +00:00
|
|
|
#include "station_map.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "station.h"
|
|
|
|
#include "news.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include "player.h"
|
2004-11-05 23:12:33 +00:00
|
|
|
#include "sound.h"
|
2005-01-31 11:23:10 +00:00
|
|
|
#include "npf.h"
|
2005-02-06 10:18:47 +00:00
|
|
|
#include "depot.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"
|
2006-08-14 14:21:15 +00:00
|
|
|
#include "date.h"
|
2007-02-10 13:37:32 +00:00
|
|
|
#include "spritecache.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
|
|
|
|
};
|
|
|
|
|
|
|
|
static TrackBits GetTileShipTrackStatus(TileIndex tile)
|
2005-06-24 12:38:35 +00:00
|
|
|
{
|
2007-05-24 22:41:50 +00:00
|
|
|
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER, 0);
|
2007-01-10 18:56:51 +00:00
|
|
|
return TrackdirBitsToTrackBits((TrackdirBits)(TRACKDIR_BIT_MASK & (r | r >> 8)));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-01-14 19:57:49 +00:00
|
|
|
void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-12-03 21:57:05 +00:00
|
|
|
int spritenum = ShipVehInfo(engine)->image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
2006-03-12 17:42:04 +00:00
|
|
|
int sprite = GetCustomVehicleIcon(engine, DIR_W);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (sprite != 0) {
|
2007-01-14 19:57:49 +00:00
|
|
|
DrawSprite(sprite, pal, x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-09-26 19:01:49 +00:00
|
|
|
spritenum = orig_ship_vehicle_info[engine - SHIP_ENGINES_INDEX].image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-01-14 19:57:49 +00:00
|
|
|
DrawSprite(6 + _ship_sprites[spritenum], 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)
|
|
|
|
{
|
|
|
|
SpriteID spritenum = ShipVehInfo(engine)->image_index;
|
|
|
|
SpriteID custom_sprite = 0;
|
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
|
|
|
custom_sprite = GetCustomVehicleIcon(engine, DIR_W);
|
|
|
|
spritenum = orig_ship_vehicle_info[engine - SHIP_ENGINES_INDEX].image_index;
|
|
|
|
}
|
|
|
|
if (custom_sprite == 0) {
|
|
|
|
spritenum = 6 + _ship_sprites[spritenum];
|
|
|
|
} else {
|
|
|
|
spritenum = custom_sprite;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Sprite *spr = GetSprite(spritenum);
|
|
|
|
|
|
|
|
width = spr->width;
|
|
|
|
height = spr->height;
|
|
|
|
}
|
|
|
|
|
2006-03-08 06:55:33 +00:00
|
|
|
int GetShipImage(const Vehicle* v, Direction direction)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int spritenum = v->spritenum;
|
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
|
|
|
int sprite = GetCustomVehicleSprite(v, direction);
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (sprite != 0) return sprite;
|
2005-09-26 19:01:49 +00:00
|
|
|
spritenum = orig_ship_vehicle_info[v->engine_type - SHIP_ENGINES_INDEX].image_index;
|
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
|
|
|
{
|
2005-11-13 13:43:55 +00:00
|
|
|
const Depot* depot;
|
|
|
|
const Depot* best_depot = NULL;
|
2005-06-24 12:38:35 +00:00
|
|
|
uint dist;
|
|
|
|
uint best_dist = (uint)-1;
|
|
|
|
TileIndex tile;
|
|
|
|
TileIndex tile2 = v->tile;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
if (_patches.new_pathfinding_all) {
|
|
|
|
NPFFoundTargetData ftd;
|
2007-01-10 18:56:51 +00:00
|
|
|
Trackdir trackdir = GetVehicleTrackdir(v);
|
2007-05-24 22:41:50 +00:00
|
|
|
ftd = NPFRouteToDepotTrialError(v->tile, trackdir, TRANSPORT_WATER, 0, v->owner, INVALID_RAILTYPE);
|
2005-11-14 19:48:04 +00:00
|
|
|
if (ftd.best_bird_dist == 0) {
|
2005-02-06 10:18:47 +00:00
|
|
|
best_depot = GetDepotByTile(ftd.node.tile); /* Found target */
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2005-02-06 10:18:47 +00:00
|
|
|
best_depot = NULL; /* Did not find target */
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2005-01-31 11:23:10 +00:00
|
|
|
} else {
|
2005-02-06 10:18:47 +00:00
|
|
|
FOR_ALL_DEPOTS(depot) {
|
|
|
|
tile = depot->xy;
|
2006-08-22 15:33:35 +00:00
|
|
|
if (IsTileDepotType(tile, TRANSPORT_WATER) && IsTileOwner(tile, v->owner)) {
|
2005-01-31 11:23:10 +00:00
|
|
|
dist = DistanceManhattan(tile, tile2);
|
|
|
|
if (dist < best_dist) {
|
|
|
|
best_dist = dist;
|
2005-02-06 10:18:47 +00:00
|
|
|
best_depot = depot;
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return best_depot;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckIfShipNeedsService(Vehicle *v)
|
|
|
|
{
|
2005-11-13 13:43:55 +00:00
|
|
|
const Depot* depot;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_patches.servint_ships == 0) return;
|
|
|
|
if (!VehicleNeedsService(v)) return;
|
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT &&
|
2005-03-20 08:43:29 +00:00
|
|
|
v->current_order.flags & OF_HALT_IN_DEPOT)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (_patches.gotodepot && VehicleHasDepotOrders(v)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-09-03 11:49:38 +00:00
|
|
|
if (IsShipInDepot(v)) {
|
|
|
|
VehicleServiceInDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-02-06 10:18:47 +00:00
|
|
|
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) {
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-05-14 08:07:20 +00:00
|
|
|
if (v->current_order.type == OT_LOADING) v->LeaveStation();
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
|
|
|
v->current_order.flags = OF_NON_STOP;
|
2006-09-03 08:25:27 +00:00
|
|
|
v->current_order.dest = depot->index;
|
2005-02-06 10:18:47 +00:00
|
|
|
v->dest_tile = depot->xy;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnNewDay_Ship(Vehicle *v)
|
|
|
|
{
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost cost;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if ((++v->day_counter & 7) == 0)
|
|
|
|
DecreaseVehicleValue(v);
|
|
|
|
|
|
|
|
CheckVehicleBreakdown(v);
|
|
|
|
AgeVehicle(v);
|
|
|
|
CheckIfShipNeedsService(v);
|
|
|
|
|
2006-03-04 11:15:44 +00:00
|
|
|
CheckOrders(v);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-11 10:15:38 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
cost.AddCost(GetVehicleProperty(v, 0x0F, ShipVehInfo(v->engine_type)->running_cost) * _price.ship_running / 364);
|
|
|
|
v->profit_this_year -= cost.GetCost() >> 8;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_SHIP_RUN);
|
2007-06-18 19:53:50 +00:00
|
|
|
SubtractMoneyFromPlayerFract(v->owner, CommandCost(cost));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->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);
|
2005-11-14 19:48:04 +00:00
|
|
|
if (u != NULL) u->u.special.unk0 = 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-04-29 22:33:51 +00:00
|
|
|
this->cur_image = GetShipImage(this, this->direction);
|
|
|
|
MarkAllViewportsDirty(this->left_coord, this->top_coord, this->right_coord + 1, this->bottom_coord + 1);
|
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);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static void ProcessShipOrder(Vehicle *v)
|
|
|
|
{
|
2005-01-15 19:06:22 +00:00
|
|
|
const Order *order;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-10 06:33:56 +00:00
|
|
|
switch (v->current_order.type) {
|
|
|
|
case OT_GOTO_DEPOT:
|
|
|
|
if (!(v->current_order.flags & OF_PART_OF_ORDERS)) return;
|
|
|
|
if (v->current_order.flags & OF_SERVICE_IF_NEEDED &&
|
|
|
|
!VehicleNeedsService(v)) {
|
2007-06-20 19:17:22 +00:00
|
|
|
UpdateVehicleTimetable(v, true);
|
2007-06-25 23:14:13 +00:00
|
|
|
v->cur_order_index++;
|
2006-02-10 06:33:56 +00:00
|
|
|
}
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-02-10 06:33:56 +00:00
|
|
|
case OT_LOADING:
|
|
|
|
case OT_LEAVESTATION:
|
|
|
|
return;
|
2006-08-22 17:13:49 +00:00
|
|
|
|
|
|
|
default: break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2006-02-10 06:33:56 +00:00
|
|
|
if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
order = GetVehicleOrder(v, v->cur_order_index);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
if (order == NULL) {
|
2007-03-08 21:39:34 +00:00
|
|
|
v->current_order.Free();
|
2004-08-09 17:04:08 +00:00
|
|
|
v->dest_tile = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-09-03 08:25:27 +00:00
|
|
|
if (order->type == v->current_order.type &&
|
|
|
|
order->flags == v->current_order.flags &&
|
2007-06-13 09:12:10 +00:00
|
|
|
order->dest == v->current_order.dest &&
|
|
|
|
(order->type != OT_GOTO_STATION || GetStation(order->dest)->dock_tile != 0))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
v->current_order = *order;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
if (order->type == OT_GOTO_STATION) {
|
|
|
|
const Station *st;
|
|
|
|
|
2006-09-03 08:25:27 +00:00
|
|
|
if (order->dest == v->last_station_visited)
|
2005-02-02 16:16:43 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-09-03 08:25:27 +00:00
|
|
|
st = GetStation(order->dest);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (st->dock_tile != 0) {
|
2006-04-07 15:57:03 +00:00
|
|
|
v->dest_tile = TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
|
2007-06-13 09:12:10 +00:00
|
|
|
} else {
|
|
|
|
v->cur_order_index++;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2005-01-15 19:06:22 +00:00
|
|
|
} else if (order->type == OT_GOTO_DEPOT) {
|
2006-09-03 08:25:27 +00:00
|
|
|
v->dest_tile = GetDepot(order->dest)->xy;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
v->dest_tile = 0;
|
|
|
|
}
|
2005-01-15 19:06:22 +00:00
|
|
|
|
|
|
|
InvalidateVehicleOrder(v);
|
2005-01-15 09:28:08 +00:00
|
|
|
|
2005-01-18 09:35:31 +00:00
|
|
|
InvalidateWindowClasses(WC_SHIPS_LIST);
|
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);
|
|
|
|
this->sprite_width = GB(x, 16, 8);
|
|
|
|
this->sprite_height = GB(x, 24, 8);
|
|
|
|
this->z_height = 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);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_image = GetShipImage(v, 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)
|
|
|
|
{
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile;
|
2006-04-05 18:46:21 +00:00
|
|
|
Axis axis;
|
2004-08-09 17:04:08 +00:00
|
|
|
uint m;
|
|
|
|
|
2006-06-05 11:28:00 +00:00
|
|
|
if (!IsShipInDepot(v)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
tile = v->tile;
|
2006-04-05 18:46:21 +00:00
|
|
|
axis = GetShipDepotAxis(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Check first side */
|
2006-04-05 18:46:21 +00:00
|
|
|
if (_ship_sometracks[axis] & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
|
|
|
|
m = (axis == AXIS_X) ? 0x101 : 0x207;
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Check second 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])))) {
|
|
|
|
m = (axis == AXIS_X) ? 0x105 : 0x203;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2007-01-10 18:56:51 +00:00
|
|
|
v->direction = (Direction)GB(m, 0, 8);
|
|
|
|
v->u.ship.state = (TrackBits)GB(m, 8, 8);
|
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)
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
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
|
|
|
{
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost(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)) {
|
|
|
|
uint32 flags;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
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
|
|
|
flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0);
|
|
|
|
AddNewsItem(
|
|
|
|
STR_9833_CITIZENS_CELEBRATE_FIRST,
|
|
|
|
flags,
|
|
|
|
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
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
static bool ShipTrackFollower(TileIndex tile, PathFindShip *pfs, int track, uint length, byte *state)
|
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;
|
|
|
|
|
2007-05-24 22:41:50 +00:00
|
|
|
FollowTrack(tile, 0x3800 | 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;
|
|
|
|
}
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailTypeMask railtypes)
|
|
|
|
{
|
|
|
|
|
|
|
|
void* perf = NpfBeginInterval();
|
2007-05-24 22:41:50 +00:00
|
|
|
NPFFoundTargetData ret = NPFRouteToStationOrTile(tile, trackdir, 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
|
|
|
{
|
2007-04-18 22:10:36 +00:00
|
|
|
assert(enterdir >= 0 && enterdir <= 3);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
if (_patches.yapf.ship_use_yapf) {
|
|
|
|
Trackdir trackdir = YapfChooseShipTrack(v, tile, enterdir, tracks);
|
2007-01-10 18:56:51 +00:00
|
|
|
return (trackdir != INVALID_TRACKDIR) ? TrackdirToTrack(trackdir) : INVALID_TRACK;
|
2006-05-27 16:12:16 +00:00
|
|
|
} else if (_patches.new_pathfinding_all) {
|
2005-01-31 11:23:10 +00:00
|
|
|
NPFFindStationOrTileData fstd;
|
|
|
|
NPFFoundTargetData ftd;
|
2006-09-05 23:21:41 +00:00
|
|
|
TileIndex src_tile = TILE_ADD(tile, TileOffsByDiagDir(ReverseDiagDir(enterdir)));
|
2007-01-10 18:56:51 +00:00
|
|
|
Trackdir trackdir = GetVehicleTrackdir(v);
|
2007-04-04 01:35:16 +00:00
|
|
|
assert(trackdir != INVALID_TRACKDIR); // Check that we are not in a depot
|
2005-01-31 11:23:10 +00:00
|
|
|
|
|
|
|
NPFFillWithOrderData(&fstd, v);
|
|
|
|
|
2006-05-27 16:12:16 +00:00
|
|
|
ftd = PerfNPFRouteToStationOrTile(src_tile, trackdir, &fstd, TRANSPORT_WATER, v->owner, INVALID_RAILTYPE);
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (ftd.best_trackdir != 0xff) {
|
2005-04-15 13:48:08 +00:00
|
|
|
/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
|
|
|
|
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. */
|
2007-01-10 18:56:51 +00:00
|
|
|
return TrackdirToTrack(ftd.best_trackdir); /* TODO: Wrapper function? */
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2007-01-10 18:56:51 +00:00
|
|
|
return INVALID_TRACK; /* Already at target, reverse? */
|
2005-11-14 19:48:04 +00:00
|
|
|
}
|
2005-01-31 11:23:10 +00:00
|
|
|
} else {
|
|
|
|
uint tot_dist, dist;
|
2007-01-10 18:56:51 +00:00
|
|
|
Track track;
|
2005-06-24 12:38:35 +00:00
|
|
|
TileIndex tile2;
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2006-09-05 23:21:41 +00:00
|
|
|
tile2 = TILE_ADD(tile, -TileOffsByDiagDir(enterdir));
|
2005-01-31 11:23:10 +00:00
|
|
|
tot_dist = (uint)-1;
|
|
|
|
|
|
|
|
/* Let's find out how far it would be if we would reverse first */
|
2007-01-10 18:56:51 +00:00
|
|
|
TrackBits b = GetTileShipTrackStatus(tile2) & _ship_sometracks[ReverseDiagDir(enterdir)] & v->u.ship.state;
|
2005-01-31 11:23:10 +00:00
|
|
|
if (b != 0) {
|
2006-03-05 12:34:55 +00:00
|
|
|
dist = FindShipTrack(v, tile2, ReverseDiagDir(enterdir), b, tile, &track);
|
2005-01-31 11:23:10 +00:00
|
|
|
if (dist != (uint)-1)
|
|
|
|
tot_dist = dist + 1;
|
|
|
|
}
|
|
|
|
/* And if we would not reverse? */
|
|
|
|
dist = FindShipTrack(v, tile, enterdir, tracks, 0, &track);
|
|
|
|
if (dist > tot_dist)
|
|
|
|
/* We could better reverse */
|
2007-01-10 18:56:51 +00:00
|
|
|
return INVALID_TRACK;
|
2005-01-31 11:23:10 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
static TrackBits GetAvailShipTracks(TileIndex tile, int dir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-05-24 22:41:50 +00:00
|
|
|
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER, 0);
|
2007-01-10 18:56:51 +00:00
|
|
|
return (TrackBits)((r | r >> 8) & _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;
|
|
|
|
}
|
|
|
|
v->breakdown_ctr--;
|
|
|
|
}
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
ProcessShipOrder(v);
|
2007-05-07 16:21:34 +00:00
|
|
|
v->HandleLoading();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->current_order.type == 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 */
|
2006-06-05 11:28:00 +00:00
|
|
|
if (IsShipInDepot(v)) {
|
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-02-13 10:26:53 +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. */
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_LEAVESTATION) {
|
2007-03-08 21:39:34 +00:00
|
|
|
v->current_order.Free();
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
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... */
|
|
|
|
if (v->current_order.type == OT_GOTO_STATION &&
|
|
|
|
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++;
|
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
InvalidateVehicleOrder(v);
|
|
|
|
} else {
|
|
|
|
/* Non-buoy orders really need to reach the tile */
|
|
|
|
if (v->dest_tile == gp.new_tile) {
|
|
|
|
if (v->current_order.type == 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;
|
|
|
|
}
|
|
|
|
} else if (v->current_order.type == OT_GOTO_STATION) {
|
|
|
|
Station *st;
|
|
|
|
|
2006-09-03 08:25:27 +00:00
|
|
|
v->last_station_visited = v->current_order.dest;
|
2005-05-02 22:13:20 +00:00
|
|
|
|
|
|
|
/* Process station in the orderlist. */
|
2006-09-03 08:25:27 +00:00
|
|
|
st = GetStation(v->current_order.dest);
|
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
|
2005-05-02 22:13:20 +00:00
|
|
|
v->current_order.type = OT_LEAVESTATION;
|
|
|
|
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-02-13 10:26:53 +00:00
|
|
|
if (HASBIT(r, VETS_CANNOT_ENTER)) goto reverse_direction;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-13 10:26:53 +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);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_image = GetShipImage(v, dir);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void Ship_Tick(Vehicle *v)
|
|
|
|
{
|
|
|
|
AgeShipCargo(v);
|
|
|
|
ShipController(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2004-08-09 17:04:08 +00:00
|
|
|
Vehicle *v;
|
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
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
|
|
|
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 */
|
|
|
|
if (!IsTileDepotType(tile, TRANSPORT_WATER)) 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
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v = AllocateVehicle();
|
2007-03-08 16:27:54 +00:00
|
|
|
unit_num = HASBIT(p2, 0) ? 0 : GetFreeUnitNumber(VEH_SHIP);
|
2006-05-11 13:31:14 +00:00
|
|
|
|
2007-02-03 09:05:43 +00:00
|
|
|
if (v == NULL || 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);
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
v->string_id = STR_SV_SHIP_NAME;
|
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;
|
2007-04-29 21:24:08 +00:00
|
|
|
v = new (v) Ship();
|
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;
|
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SETBIT(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
|
|
|
|
|
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-03-08 16:27:54 +00:00
|
|
|
InvalidateAutoreplaceWindow(VEH_SHIP); // 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
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
2006-06-05 11:28:00 +00:00
|
|
|
if (!IsShipInDepotStopped(v)) {
|
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
|
|
|
|
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);
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteVehicle(v);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-06-18 21:44:47 +00:00
|
|
|
return CommandCost(-v->value);
|
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
|
|
|
{
|
|
|
|
Vehicle *v;
|
2006-08-09 21:11:45 +00:00
|
|
|
uint16 callback;
|
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
|
|
|
|
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
|
|
|
|
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. */
|
|
|
|
callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
|
|
|
|
if (callback != CALLBACK_FAILED && callback != 0xFF) {
|
|
|
|
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) {
|
2006-06-05 11:28:00 +00:00
|
|
|
if (IsShipInDepotStopped(v)) {
|
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;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
Vehicle *v;
|
2005-05-12 00:11:37 +00:00
|
|
|
const Depot *dep;
|
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
|
|
|
|
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;
|
2006-08-29 23:39:57 +00:00
|
|
|
|
2006-09-01 10:24:15 +00:00
|
|
|
if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
|
2005-05-03 19:31:33 +00:00
|
|
|
|
2006-09-02 22:47:45 +00:00
|
|
|
if (IsShipInDepot(v)) return CMD_ERROR;
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
/* If the current orders are already goto-depot */
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
2006-09-02 09:35:03 +00:00
|
|
|
if (!!(p2 & DEPOT_SERVICE) == HASBIT(v->current_order.flags, OFB_HALT_IN_DEPOT)) {
|
2006-09-01 12:37:03 +00:00
|
|
|
/* We called with a different DEPOT_SERVICE setting.
|
|
|
|
* Now we change the setting to apply the new one and let the vehicle head for the same depot.
|
|
|
|
* Note: the if is (true for requesting service == true for ordered to stop in depot) */
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
TOGGLEBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
|
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2006-09-01 12:37:03 +00:00
|
|
|
}
|
|
|
|
|
2006-09-01 10:24:15 +00:00
|
|
|
if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2005-05-12 00:11:37 +00:00
|
|
|
/* If the orders to 'goto depot' are in the orders list (forced servicing),
|
|
|
|
* then skip to the next order; effectively cancelling this forced service */
|
2005-03-20 08:43:29 +00:00
|
|
|
if (HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS))
|
|
|
|
v->cur_order_index++;
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2005-05-12 00:11:37 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
dep = FindClosestShipDepot(v);
|
2006-09-01 10:24:15 +00:00
|
|
|
if (dep == NULL) return_cmd_error(STR_981A_UNABLE_TO_FIND_LOCAL_DEPOT);
|
2005-05-12 00:11:37 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2007-04-20 11:39:01 +00:00
|
|
|
if (v->current_order.type == OT_LOADING) v->LeaveStation();
|
|
|
|
|
2005-05-12 00:11:37 +00:00
|
|
|
v->dest_tile = dep->xy;
|
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
2006-08-27 09:28:52 +00:00
|
|
|
v->current_order.flags = OF_NON_STOP;
|
2006-09-01 10:24:15 +00:00
|
|
|
if (!(p2 & DEPOT_SERVICE)) SETBIT(v->current_order.flags, OFB_HALT_IN_DEPOT);
|
2006-12-05 23:57:04 +00:00
|
|
|
v->current_order.refit_cargo = CT_INVALID;
|
2006-09-03 08:25:27 +00:00
|
|
|
v->current_order.dest = dep->index;
|
2005-05-12 00:11:37 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost cost;
|
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;
|
2005-10-30 11:00:54 +00:00
|
|
|
|
2006-06-05 11:28:00 +00:00
|
|
|
if (!IsShipInDepotStopped(v)) {
|
|
|
|
return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN);
|
|
|
|
}
|
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
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_SHIP_RUN);
|
|
|
|
|
2006-06-09 07:09:48 +00:00
|
|
|
/* Check the refit capacity callback */
|
|
|
|
if (HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_REFIT_CAPACITY)) {
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
}
|