2005-07-24 16:12:37 +02:00
|
|
|
/* $Id$ */
|
|
|
|
|
2007-04-04 03:35:16 +02:00
|
|
|
/** @file ship_cmd.cpp */
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "stdafx.h"
|
2005-06-02 21:30:21 +02:00
|
|
|
#include "openttd.h"
|
2006-06-05 13:28:00 +02:00
|
|
|
#include "ship.h"
|
2007-12-21 23:50:51 +01:00
|
|
|
#include "tile_cmd.h"
|
2007-04-12 15:07:15 +02:00
|
|
|
#include "landscape.h"
|
2007-06-20 21:17:22 +02:00
|
|
|
#include "timetable.h"
|
2007-12-21 22:50:46 +01:00
|
|
|
#include "command_func.h"
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "pathfind.h"
|
2006-04-04 13:35:52 +02:00
|
|
|
#include "station_map.h"
|
2004-08-09 19:04:08 +02:00
|
|
|
#include "station.h"
|
|
|
|
#include "news.h"
|
|
|
|
#include "engine.h"
|
2008-01-12 15:10:35 +01:00
|
|
|
#include "player_func.h"
|
|
|
|
#include "player_base.h"
|
2005-01-31 12:23:10 +01:00
|
|
|
#include "npf.h"
|
2005-02-06 11:18:47 +01:00
|
|
|
#include "depot.h"
|
2005-07-22 00:15:02 +02:00
|
|
|
#include "vehicle_gui.h"
|
2006-02-03 13:55:21 +01:00
|
|
|
#include "newgrf_engine.h"
|
2006-04-05 20:46:21 +02:00
|
|
|
#include "water_map.h"
|
2006-05-27 18:12:16 +02:00
|
|
|
#include "yapf/yapf.h"
|
|
|
|
#include "debug.h"
|
2006-06-09 09:09:48 +02:00
|
|
|
#include "newgrf_callbacks.h"
|
2006-08-09 23:11:45 +02:00
|
|
|
#include "newgrf_text.h"
|
2006-09-27 20:17:01 +02:00
|
|
|
#include "newgrf_sound.h"
|
2007-02-10 14:37:32 +01:00
|
|
|
#include "spritecache.h"
|
2007-08-03 21:36:00 +02:00
|
|
|
#include "misc/autoptr.hpp"
|
2007-12-21 20:49:27 +01:00
|
|
|
#include "strings_func.h"
|
2007-12-25 12:26:07 +01:00
|
|
|
#include "functions.h"
|
|
|
|
#include "window_func.h"
|
2007-12-26 14:50:40 +01:00
|
|
|
#include "date_func.h"
|
2007-12-27 14:35:39 +01:00
|
|
|
#include "vehicle_func.h"
|
2007-12-29 10:24:26 +01:00
|
|
|
#include "sound_func.h"
|
2008-01-07 01:57:19 +01:00
|
|
|
#include "variables.h"
|
2008-01-07 10:19:53 +01:00
|
|
|
#include "autoreplace_gui.h"
|
2008-01-09 10:45:45 +01:00
|
|
|
#include "gfx_func.h"
|
2008-01-13 15:37:30 +01:00
|
|
|
#include "settings_type.h"
|
2008-01-07 01:57:19 +01:00
|
|
|
|
2008-01-13 02:21:35 +01:00
|
|
|
#include "table/strings.h"
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
|
|
|
|
|
2007-01-10 19:56:51 +01: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 14:38:35 +02:00
|
|
|
{
|
2007-05-25 00:41:50 +02:00
|
|
|
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER, 0);
|
2007-01-10 19:56:51 +01:00
|
|
|
return TrackdirBitsToTrackBits((TrackdirBits)(TRACKDIR_BIT_MASK & (r | r >> 8)));
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-01-14 20:57:49 +01:00
|
|
|
void DrawShipEngine(int x, int y, EngineID engine, SpriteID pal)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2004-12-03 22:57:05 +01:00
|
|
|
int spritenum = ShipVehInfo(engine)->image_index;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
2006-03-12 18:42:04 +01:00
|
|
|
int sprite = GetCustomVehicleIcon(engine, DIR_W);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-11-14 20:48:04 +01:00
|
|
|
if (sprite != 0) {
|
2007-01-14 20:57:49 +01:00
|
|
|
DrawSprite(sprite, pal, x, y);
|
2004-08-09 19:04:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2008-01-09 11:15:20 +01:00
|
|
|
spritenum = _orig_ship_vehicle_info[engine - SHIP_ENGINES_INDEX].image_index;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2007-01-14 20:57:49 +01:00
|
|
|
DrawSprite(6 + _ship_sprites[spritenum], pal, x, y);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-02-10 14:37:32 +01: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 03:35:16 +02:00
|
|
|
* @param width The width of the sprite
|
|
|
|
* @param height The height of the sprite
|
2007-02-10 14:37:32 +01: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);
|
2008-01-09 11:15:20 +01:00
|
|
|
spritenum = _orig_ship_vehicle_info[engine - SHIP_ENGINES_INDEX].image_index;
|
2007-02-10 14:37:32 +01:00
|
|
|
}
|
|
|
|
if (custom_sprite == 0) {
|
|
|
|
spritenum = 6 + _ship_sprites[spritenum];
|
|
|
|
} else {
|
|
|
|
spritenum = custom_sprite;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Sprite *spr = GetSprite(spritenum);
|
|
|
|
|
|
|
|
width = spr->width;
|
|
|
|
height = spr->height;
|
|
|
|
}
|
|
|
|
|
2007-07-01 21:11:47 +02:00
|
|
|
int Ship::GetImage(Direction direction) const
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-07-01 21:11:47 +02:00
|
|
|
int spritenum = this->spritenum;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
2007-07-01 21:11:47 +02:00
|
|
|
int sprite = GetCustomVehicleSprite(this, direction);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-11-14 20:48:04 +01:00
|
|
|
if (sprite != 0) return sprite;
|
2008-01-09 11:15:20 +01:00
|
|
|
spritenum = _orig_ship_vehicle_info[this->engine_type - SHIP_ENGINES_INDEX].image_index;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
return _ship_sprites[spritenum] + direction;
|
|
|
|
}
|
|
|
|
|
2005-11-13 14:43:55 +01:00
|
|
|
static const Depot* FindClosestShipDepot(const Vehicle* v)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2005-11-13 14:43:55 +01:00
|
|
|
const Depot* depot;
|
|
|
|
const Depot* best_depot = NULL;
|
2005-06-24 14:38:35 +02:00
|
|
|
uint dist;
|
|
|
|
uint best_dist = (uint)-1;
|
|
|
|
TileIndex tile;
|
|
|
|
TileIndex tile2 = v->tile;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-31 12:23:10 +01:00
|
|
|
if (_patches.new_pathfinding_all) {
|
|
|
|
NPFFoundTargetData ftd;
|
2007-01-10 19:56:51 +01:00
|
|
|
Trackdir trackdir = GetVehicleTrackdir(v);
|
2008-01-09 22:05:03 +01:00
|
|
|
ftd = NPFRouteToDepotTrialError(v->tile, trackdir, TRANSPORT_WATER, 0, v->owner, INVALID_RAILTYPES);
|
2005-11-14 20:48:04 +01:00
|
|
|
if (ftd.best_bird_dist == 0) {
|
2005-02-06 11:18:47 +01:00
|
|
|
best_depot = GetDepotByTile(ftd.node.tile); /* Found target */
|
2005-11-14 20:48:04 +01:00
|
|
|
} else {
|
2005-02-06 11:18:47 +01:00
|
|
|
best_depot = NULL; /* Did not find target */
|
2005-11-14 20:48:04 +01:00
|
|
|
}
|
2005-01-31 12:23:10 +01:00
|
|
|
} else {
|
2005-02-06 11:18:47 +01:00
|
|
|
FOR_ALL_DEPOTS(depot) {
|
|
|
|
tile = depot->xy;
|
2006-08-22 17:33:35 +02:00
|
|
|
if (IsTileDepotType(tile, TRANSPORT_WATER) && IsTileOwner(tile, v->owner)) {
|
2005-01-31 12:23:10 +01:00
|
|
|
dist = DistanceManhattan(tile, tile2);
|
|
|
|
if (dist < best_dist) {
|
|
|
|
best_dist = dist;
|
2005-02-06 11:18:47 +01:00
|
|
|
best_depot = depot;
|
2005-01-31 12:23:10 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return best_depot;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckIfShipNeedsService(Vehicle *v)
|
|
|
|
{
|
2007-08-31 19:13:39 +02:00
|
|
|
if (_patches.servint_ships == 0 || !VehicleNeedsService(v)) return;
|
2007-08-29 23:49:08 +02:00
|
|
|
if (v->IsInDepot()) {
|
2006-09-03 13:49:38 +02:00
|
|
|
VehicleServiceInDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-31 19:13:39 +02:00
|
|
|
const Depot *depot = FindClosestShipDepot(v);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-02-06 11:18:47 +01:00
|
|
|
if (depot == NULL || DistanceManhattan(v->tile, depot->xy) > 12) {
|
2004-12-05 13:43:04 +01:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-12-05 13:43:04 +01:00
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
2008-01-15 19:44:22 +01:00
|
|
|
v->current_order.flags = OFB_NON_STOP;
|
2006-09-03 10:25:27 +02:00
|
|
|
v->current_order.dest = depot->index;
|
2005-02-06 11:18:47 +01:00
|
|
|
v->dest_tile = depot->xy;
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnNewDay_Ship(Vehicle *v)
|
|
|
|
{
|
2008-01-09 17:55:48 +01:00
|
|
|
CommandCost cost(EXPENSES_SHIP_RUN);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
if ((++v->day_counter & 7) == 0)
|
|
|
|
DecreaseVehicleValue(v);
|
|
|
|
|
|
|
|
CheckVehicleBreakdown(v);
|
|
|
|
AgeVehicle(v);
|
|
|
|
CheckIfShipNeedsService(v);
|
|
|
|
|
2006-03-04 12:15:44 +01:00
|
|
|
CheckOrders(v);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2005-11-14 20:48:04 +01:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-11 12:15:38 +02:00
|
|
|
|
2007-06-18 21:53:50 +02: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 19:04:08 +02:00
|
|
|
|
2008-01-09 17:55:48 +01:00
|
|
|
SubtractMoneyFromPlayerFract(v->owner, cost);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2007-04-04 03:35:16 +02:00
|
|
|
/* we need this for the profit */
|
2005-01-18 10:35:31 +01:00
|
|
|
InvalidateWindowClasses(WC_SHIPS_LIST);
|
2004-08-09 19:04:08 +02: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 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2006-09-27 20:17:01 +02:00
|
|
|
if (!PlayVehicleSound(v, VSE_BREAKDOWN)) {
|
2007-03-22 04:42:43 +01:00
|
|
|
SndPlayVehicleFx((_opt.landscape != LT_TOYLAND) ?
|
2006-09-27 20:17:01 +02:00
|
|
|
SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v);
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
if (!(v->vehstatus & VS_HIDDEN)) {
|
|
|
|
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
|
2007-07-26 17:37:19 +02:00
|
|
|
if (u != NULL) u->u.special.animation_state = v->breakdown_delay * 2;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(v->tick_counter & 1)) {
|
|
|
|
if (!--v->breakdown_delay) {
|
|
|
|
v->breakdown_ctr = 0;
|
2005-01-24 23:24:47 +01:00
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-30 00:33:51 +02:00
|
|
|
void Ship::MarkDirty()
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-07-01 21:11:47 +02:00
|
|
|
this->cur_image = this->GetImage(this->direction);
|
2008-01-16 22:17:31 +01:00
|
|
|
MarkSingleVehicleDirty(this);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-05-07 17:58:05 +02:00
|
|
|
static void PlayShipSound(const Vehicle *v)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2006-09-27 20:17:01 +02:00
|
|
|
if (!PlayVehicleSound(v, VSE_START)) {
|
|
|
|
SndPlayVehicleFx(ShipVehInfo(v->engine_type)->sfx, v);
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-05-07 17:58:05 +02:00
|
|
|
void Ship::PlayLeaveStationSound() const
|
|
|
|
{
|
|
|
|
PlayShipSound(this);
|
|
|
|
}
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
static void ProcessShipOrder(Vehicle *v)
|
|
|
|
{
|
2005-01-15 20:06:22 +01:00
|
|
|
const Order *order;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-10 07:33:56 +01:00
|
|
|
switch (v->current_order.type) {
|
|
|
|
case OT_GOTO_DEPOT:
|
2008-01-15 19:44:22 +01:00
|
|
|
if (!(v->current_order.flags & OFB_PART_OF_ORDERS)) return;
|
|
|
|
if (v->current_order.flags & OFB_SERVICE_IF_NEEDED &&
|
2006-02-10 07:33:56 +01:00
|
|
|
!VehicleNeedsService(v)) {
|
2007-06-20 21:17:22 +02:00
|
|
|
UpdateVehicleTimetable(v, true);
|
2007-06-26 01:14:13 +02:00
|
|
|
v->cur_order_index++;
|
2006-02-10 07:33:56 +01:00
|
|
|
}
|
|
|
|
break;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-02-10 07:33:56 +01:00
|
|
|
case OT_LOADING:
|
|
|
|
case OT_LEAVESTATION:
|
|
|
|
return;
|
2006-08-22 19:13:49 +02:00
|
|
|
|
|
|
|
default: break;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2006-02-10 07:33:56 +01:00
|
|
|
if (v->cur_order_index >= v->num_orders) v->cur_order_index = 0;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-15 20:06:22 +01:00
|
|
|
order = GetVehicleOrder(v, v->cur_order_index);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-15 20:06:22 +01:00
|
|
|
if (order == NULL) {
|
2007-03-08 22:39:34 +01:00
|
|
|
v->current_order.Free();
|
2004-08-09 19:04:08 +02:00
|
|
|
v->dest_tile = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-09-03 10:25:27 +02:00
|
|
|
if (order->type == v->current_order.type &&
|
|
|
|
order->flags == v->current_order.flags &&
|
2007-06-13 11:12:10 +02:00
|
|
|
order->dest == v->current_order.dest &&
|
|
|
|
(order->type != OT_GOTO_STATION || GetStation(order->dest)->dock_tile != 0))
|
2004-08-09 19:04:08 +02:00
|
|
|
return;
|
|
|
|
|
2005-01-15 20:06:22 +01:00
|
|
|
v->current_order = *order;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-01-15 20:06:22 +01:00
|
|
|
if (order->type == OT_GOTO_STATION) {
|
|
|
|
const Station *st;
|
|
|
|
|
2006-09-03 10:25:27 +02:00
|
|
|
if (order->dest == v->last_station_visited)
|
2005-02-02 17:16:43 +01:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2006-09-03 10:25:27 +02:00
|
|
|
st = GetStation(order->dest);
|
2004-08-09 19:04:08 +02:00
|
|
|
if (st->dock_tile != 0) {
|
2006-04-07 17:57:03 +02:00
|
|
|
v->dest_tile = TILE_ADD(st->dock_tile, ToTileIndexDiff(GetDockOffset(st->dock_tile)));
|
2007-06-13 11:12:10 +02:00
|
|
|
} else {
|
|
|
|
v->cur_order_index++;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2005-01-15 20:06:22 +01:00
|
|
|
} else if (order->type == OT_GOTO_DEPOT) {
|
2006-09-03 10:25:27 +02:00
|
|
|
v->dest_tile = GetDepot(order->dest)->xy;
|
2004-08-09 19:04:08 +02:00
|
|
|
} else {
|
|
|
|
v->dest_tile = 0;
|
|
|
|
}
|
2005-01-15 20:06:22 +01:00
|
|
|
|
|
|
|
InvalidateVehicleOrder(v);
|
2005-01-15 10:28:08 +01:00
|
|
|
|
2005-01-18 10:35:31 +01:00
|
|
|
InvalidateWindowClasses(WC_SHIPS_LIST);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-05-01 18:35:14 +02:00
|
|
|
void Ship::UpdateDeltaXY(Direction direction)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-05-01 18:35:14 +02:00
|
|
|
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
|
2004-08-09 19:04:08 +02:00
|
|
|
static const uint32 _delta_xy_table[8] = {
|
2007-05-01 18:35:14 +02: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 19:04:08 +02:00
|
|
|
};
|
|
|
|
#undef MKIT
|
2007-05-01 18:35:14 +02: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 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2006-10-04 14:01:59 +02:00
|
|
|
void RecalcShipStuff(Vehicle *v)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-05-01 18:35:14 +02:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2007-07-01 21:11:47 +02:00
|
|
|
v->cur_image = v->GetImage(v->direction);
|
2007-04-30 00:33:51 +02:00
|
|
|
v->MarkDirty();
|
2004-08-09 19:04:08 +02:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
}
|
|
|
|
|
2005-01-06 12:39:00 +01:00
|
|
|
static const TileIndexDiffC _ship_leave_depot_offs[] = {
|
|
|
|
{-1, 0},
|
|
|
|
{ 0, -1}
|
2004-08-09 19:04:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static void CheckShipLeaveDepot(Vehicle *v)
|
|
|
|
{
|
2005-06-24 14:38:35 +02:00
|
|
|
TileIndex tile;
|
2006-04-05 20:46:21 +02:00
|
|
|
Axis axis;
|
2004-08-09 19:04:08 +02:00
|
|
|
uint m;
|
|
|
|
|
2007-08-29 23:49:08 +02:00
|
|
|
if (!v->IsInDepot()) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
tile = v->tile;
|
2006-04-05 20:46:21 +02:00
|
|
|
axis = GetShipDepotAxis(tile);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-04-04 03:35:16 +02:00
|
|
|
/* Check first side */
|
2006-04-05 20:46:21 +02:00
|
|
|
if (_ship_sometracks[axis] & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[axis])))) {
|
|
|
|
m = (axis == AXIS_X) ? 0x101 : 0x207;
|
2007-04-04 03:35:16 +02:00
|
|
|
/* Check second side */
|
2006-04-05 20:46:21 +02: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 19:04:08 +02:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2007-01-10 19:56:51 +01:00
|
|
|
v->direction = (Direction)GB(m, 0, 8);
|
|
|
|
v->u.ship.state = (TrackBits)GB(m, 8, 8);
|
2004-08-09 19:04:08 +02:00
|
|
|
v->vehstatus &= ~VS_HIDDEN;
|
|
|
|
|
|
|
|
v->cur_speed = 0;
|
|
|
|
RecalcShipStuff(v);
|
|
|
|
|
|
|
|
PlayShipSound(v);
|
2004-12-09 22:46:56 +01:00
|
|
|
VehicleServiceInDepot(v);
|
2006-10-05 14:59:28 +02:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2005-01-18 10:35:31 +01:00
|
|
|
InvalidateWindowClasses(WC_SHIPS_LIST);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool ShipAccelerate(Vehicle *v)
|
|
|
|
{
|
|
|
|
uint spd;
|
|
|
|
byte t;
|
|
|
|
|
2007-04-18 20:37:40 +02:00
|
|
|
spd = min(v->cur_speed + 1, GetVehicleProperty(v, 0x0B, v->max_speed));
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-04-04 03:35:16 +02:00
|
|
|
/*updates statusbar only if speed have changed to save CPU time */
|
2004-08-09 19:04:08 +02:00
|
|
|
if (spd != v->cur_speed) {
|
2004-09-10 21:02:27 +02:00
|
|
|
v->cur_speed = spd;
|
2004-08-09 19:04:08 +02:00
|
|
|
if (_patches.vehicle_speed)
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-04-04 03:35:16 +02:00
|
|
|
/* Decrease somewhat when turning */
|
2005-11-14 20:48:04 +01:00
|
|
|
if (!(v->direction & 1)) spd = spd * 3 / 4;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-11-14 20:48:04 +01:00
|
|
|
if (spd == 0) return false;
|
|
|
|
if ((byte)++spd == 0) return true;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
v->progress = (t = v->progress) - (byte)spd;
|
|
|
|
|
|
|
|
return (t < v->progress);
|
|
|
|
}
|
|
|
|
|
2007-06-18 12:48:15 +02:00
|
|
|
static CommandCost EstimateShipCost(EngineID engine_type)
|
2005-05-12 02:11:37 +02:00
|
|
|
{
|
2008-01-09 17:55:48 +01:00
|
|
|
return CommandCost(EXPENSES_NEW_VEHICLES, GetEngineProperty(engine_type, 0x0A, ShipVehInfo(engine_type)->base_cost) * (_price.ship_base >> 3) >> 5);
|
2005-05-12 02:11:37 +02:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-11-13 14:43:55 +01:00
|
|
|
static void ShipArrivesAt(const Vehicle* v, Station* st)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
/* Check if station was ever visited before */
|
|
|
|
if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
|
|
|
|
uint32 flags;
|
2005-11-14 20:48:04 +01:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
st->had_vehicle_of_type |= HVOT_SHIP;
|
2005-11-14 20:48:04 +01:00
|
|
|
|
2004-12-02 23:53:07 +01:00
|
|
|
SetDParam(0, st->index);
|
2004-08-09 19:04:08 +02: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 13:11:48 +01:00
|
|
|
struct PathFindShip {
|
2005-06-24 14:38:35 +02:00
|
|
|
TileIndex skiptile;
|
|
|
|
TileIndex dest_coords;
|
2004-08-09 19:04:08 +02:00
|
|
|
uint best_bird_dist;
|
|
|
|
uint best_length;
|
2007-03-07 13:11:48 +01:00
|
|
|
};
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-06-24 14:38:35 +02:00
|
|
|
static bool ShipTrackFollower(TileIndex tile, PathFindShip *pfs, int track, uint length, byte *state)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-04-04 03:35:16 +02:00
|
|
|
/* Found dest? */
|
2004-08-09 19:04:08 +02:00
|
|
|
if (tile == pfs->dest_coords) {
|
|
|
|
pfs->best_bird_dist = 0;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
pfs->best_length = minu(pfs->best_length, length);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-04-04 03:35:16 +02:00
|
|
|
/* Skip this tile in the calculation */
|
2004-08-09 19:04:08 +02:00
|
|
|
if (tile != pfs->skiptile) {
|
2005-01-31 08:23:15 +01:00
|
|
|
pfs->best_bird_dist = minu(pfs->best_bird_dist, DistanceMaxPlusManhattan(pfs->dest_coords, tile));
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02: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 19:56:51 +01:00
|
|
|
static uint FindShipTrack(Vehicle *v, TileIndex tile, DiagDirection dir, TrackBits bits, TileIndex skiptile, Track *track)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
PathFindShip pfs;
|
2007-01-10 19:56:51 +01:00
|
|
|
Track i, best_track;
|
2004-08-09 19:04:08 +02: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 19:56:51 +01:00
|
|
|
best_track = INVALID_TRACK;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
do {
|
2007-01-11 11:34:42 +01:00
|
|
|
i = RemoveFirstTrack(&bits);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
pfs.best_bird_dist = (uint)-1;
|
|
|
|
pfs.best_length = (uint)-1;
|
|
|
|
|
2007-05-25 00:41:50 +02:00
|
|
|
FollowTrack(tile, 0x3800 | TRANSPORT_WATER, 0, (DiagDirection)_ship_search_directions[i][dir], (TPFEnumProc*)ShipTrackFollower, NULL, &pfs);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2007-01-10 19:56:51 +01:00
|
|
|
if (best_track != INVALID_TRACK) {
|
2004-08-09 19:04:08 +02: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 21:02:27 +02:00
|
|
|
|
|
|
|
/* if we reach this position, there's two paths of equal value so far.
|
2004-08-09 19:04:08 +02:00
|
|
|
* pick one randomly. */
|
2005-07-21 08:31:02 +02:00
|
|
|
r = GB(Random(), 0, 8);
|
2004-08-09 19:04:08 +02: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-01-09 22:05:03 +01:00
|
|
|
static inline NPFFoundTargetData PerfNPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, NPFFindStationOrTileData* target, TransportType type, Owner owner, RailTypes railtypes)
|
2006-05-27 18:12:16 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
void* perf = NpfBeginInterval();
|
2007-05-25 00:41:50 +02:00
|
|
|
NPFFoundTargetData ret = NPFRouteToStationOrTile(tile, trackdir, target, type, 0, owner, railtypes);
|
2006-05-27 18:12:16 +02:00
|
|
|
int t = NpfEndInterval(perf);
|
2006-12-26 18:36:18 +01: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 18:12:16 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-04-04 03:35:16 +02:00
|
|
|
/** returns the track to choose on the next tile, or -1 when it's better to
|
2005-01-31 12:23:10 +01: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 19:56:51 +01:00
|
|
|
static Track ChooseShipTrack(Vehicle *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-04-19 00:10:36 +02:00
|
|
|
assert(enterdir >= 0 && enterdir <= 3);
|
2005-01-31 12:23:10 +01:00
|
|
|
|
2006-05-27 18:12:16 +02:00
|
|
|
if (_patches.yapf.ship_use_yapf) {
|
|
|
|
Trackdir trackdir = YapfChooseShipTrack(v, tile, enterdir, tracks);
|
2007-01-10 19:56:51 +01:00
|
|
|
return (trackdir != INVALID_TRACKDIR) ? TrackdirToTrack(trackdir) : INVALID_TRACK;
|
2006-05-27 18:12:16 +02:00
|
|
|
} else if (_patches.new_pathfinding_all) {
|
2005-01-31 12:23:10 +01:00
|
|
|
NPFFindStationOrTileData fstd;
|
|
|
|
NPFFoundTargetData ftd;
|
2006-09-06 01:21:41 +02:00
|
|
|
TileIndex src_tile = TILE_ADD(tile, TileOffsByDiagDir(ReverseDiagDir(enterdir)));
|
2007-01-10 19:56:51 +01:00
|
|
|
Trackdir trackdir = GetVehicleTrackdir(v);
|
2007-04-04 03:35:16 +02:00
|
|
|
assert(trackdir != INVALID_TRACKDIR); // Check that we are not in a depot
|
2005-01-31 12:23:10 +01:00
|
|
|
|
|
|
|
NPFFillWithOrderData(&fstd, v);
|
|
|
|
|
2008-01-09 22:05:03 +01:00
|
|
|
ftd = PerfNPFRouteToStationOrTile(src_tile, trackdir, &fstd, TRANSPORT_WATER, v->owner, INVALID_RAILTYPES);
|
2005-01-31 12:23:10 +01:00
|
|
|
|
2005-11-14 20:48:04 +01:00
|
|
|
if (ftd.best_trackdir != 0xff) {
|
2005-04-15 15:48:08 +02: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 19:56:51 +01:00
|
|
|
return TrackdirToTrack(ftd.best_trackdir); /* TODO: Wrapper function? */
|
2005-11-14 20:48:04 +01:00
|
|
|
} else {
|
2007-01-10 19:56:51 +01:00
|
|
|
return INVALID_TRACK; /* Already at target, reverse? */
|
2005-11-14 20:48:04 +01:00
|
|
|
}
|
2005-01-31 12:23:10 +01:00
|
|
|
} else {
|
|
|
|
uint tot_dist, dist;
|
2007-01-10 19:56:51 +01:00
|
|
|
Track track;
|
2005-06-24 14:38:35 +02:00
|
|
|
TileIndex tile2;
|
2005-01-31 12:23:10 +01:00
|
|
|
|
2006-09-06 01:21:41 +02:00
|
|
|
tile2 = TILE_ADD(tile, -TileOffsByDiagDir(enterdir));
|
2005-01-31 12:23:10 +01:00
|
|
|
tot_dist = (uint)-1;
|
|
|
|
|
|
|
|
/* Let's find out how far it would be if we would reverse first */
|
2007-01-10 19:56:51 +01:00
|
|
|
TrackBits b = GetTileShipTrackStatus(tile2) & _ship_sometracks[ReverseDiagDir(enterdir)] & v->u.ship.state;
|
2005-01-31 12:23:10 +01:00
|
|
|
if (b != 0) {
|
2006-03-05 13:34:55 +01:00
|
|
|
dist = FindShipTrack(v, tile2, ReverseDiagDir(enterdir), b, tile, &track);
|
2005-01-31 12:23:10 +01: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 19:56:51 +01:00
|
|
|
return INVALID_TRACK;
|
2005-01-31 12:23:10 +01:00
|
|
|
return track;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-08 07:55:33 +01:00
|
|
|
static const Direction _new_vehicle_direction_table[] = {
|
2007-01-10 19:56:51 +01:00
|
|
|
DIR_N , DIR_NW, DIR_W , INVALID_DIR,
|
|
|
|
DIR_NE, DIR_N , DIR_SW, INVALID_DIR,
|
2006-03-08 07:55:33 +01:00
|
|
|
DIR_E , DIR_SE, DIR_S
|
2004-08-09 19:04:08 +02:00
|
|
|
};
|
|
|
|
|
2007-01-10 19:56:51 +01:00
|
|
|
static Direction ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2005-01-07 18:02:43 +01:00
|
|
|
uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
|
|
|
|
TileX(new_tile) - TileX(old_tile) + 1;
|
2004-08-09 19:04:08 +02:00
|
|
|
assert(offs < 11 && offs != 3 && offs != 7);
|
|
|
|
return _new_vehicle_direction_table[offs];
|
|
|
|
}
|
|
|
|
|
2007-01-10 19:56:51 +01:00
|
|
|
static Direction ShipGetNewDirection(Vehicle *v, int x, int y)
|
2004-08-09 19:04:08 +02: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 19:56:51 +01:00
|
|
|
static TrackBits GetAvailShipTracks(TileIndex tile, int dir)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-05-25 00:41:50 +02:00
|
|
|
uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER, 0);
|
2007-01-10 19:56:51 +01:00
|
|
|
return (TrackBits)((r | r >> 8) & _ship_sometracks[dir]);
|
2004-08-09 19:04:08 +02: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-19 00:10:36 +02:00
|
|
|
{ 8, 15, 7},
|
2004-08-09 19:04:08 +02:00
|
|
|
{ 0, 0, 0},
|
2007-04-19 00:10:36 +02:00
|
|
|
{ 8, 15, 6},
|
2004-08-09 19:04:08 +02:00
|
|
|
{ 0, 0, 0},
|
2007-04-19 00:10:36 +02:00
|
|
|
{ 7, 15, 0},
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void ShipController(Vehicle *v)
|
|
|
|
{
|
|
|
|
uint32 r;
|
|
|
|
const byte *b;
|
2006-03-08 07:55:33 +01:00
|
|
|
Direction dir;
|
2007-01-10 19:56:51 +01:00
|
|
|
Track track;
|
|
|
|
TrackBits tracks;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
v->tick_counter++;
|
2007-06-20 21:17:22 +02:00
|
|
|
v->current_order_time++;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
if (v->breakdown_ctr != 0) {
|
|
|
|
if (v->breakdown_ctr <= 2) {
|
|
|
|
HandleBrokenShip(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
v->breakdown_ctr--;
|
|
|
|
}
|
|
|
|
|
2005-11-14 20:48:04 +01:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
ProcessShipOrder(v);
|
2007-05-07 18:21:34 +02:00
|
|
|
v->HandleLoading();
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-11-14 20:48:04 +01:00
|
|
|
if (v->current_order.type == OT_LOADING) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
CheckShipLeaveDepot(v);
|
|
|
|
|
2005-11-14 20:48:04 +01:00
|
|
|
if (!ShipAccelerate(v)) return;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
BeginVehicleMove(v);
|
|
|
|
|
2007-02-25 11:49:13 +01:00
|
|
|
GetNewVehiclePosResult gp = GetNewVehiclePos(v);
|
2007-02-25 11:25:25 +01:00
|
|
|
if (gp.old_tile == gp.new_tile) {
|
2007-02-13 12:29:20 +01:00
|
|
|
/* Staying in tile */
|
2007-08-29 23:49:08 +02:00
|
|
|
if (v->IsInDepot()) {
|
2004-08-09 19:04:08 +02:00
|
|
|
gp.x = v->x_pos;
|
|
|
|
gp.y = v->y_pos;
|
|
|
|
} else {
|
2007-02-13 12:29:20 +01:00
|
|
|
/* Not inside depot */
|
2004-08-09 19:04:08 +02:00
|
|
|
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
|
2007-11-19 22:02:30 +01:00
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-05-03 00:13:20 +02:00
|
|
|
/* A leave station order only needs one tick to get processed, so we can
|
|
|
|
* always skip ahead. */
|
2004-12-05 13:43:04 +01:00
|
|
|
if (v->current_order.type == OT_LEAVESTATION) {
|
2007-03-08 22:39:34 +01:00
|
|
|
v->current_order.Free();
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2005-05-03 00:13:20 +02: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-26 01:14:13 +02:00
|
|
|
UpdateVehicleTimetable(v, true);
|
2005-05-03 00:13:20 +02: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 12:29:20 +01:00
|
|
|
if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
|
2006-10-04 14:01:59 +02:00
|
|
|
VehicleEnterDepot(v);
|
2005-05-03 00:13:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (v->current_order.type == OT_GOTO_STATION) {
|
|
|
|
Station *st;
|
|
|
|
|
2006-09-03 10:25:27 +02:00
|
|
|
v->last_station_visited = v->current_order.dest;
|
2005-05-03 00:13:20 +02:00
|
|
|
|
|
|
|
/* Process station in the orderlist. */
|
2006-09-03 10:25:27 +02:00
|
|
|
st = GetStation(v->current_order.dest);
|
2007-04-04 03:35:16 +02:00
|
|
|
if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
|
2005-05-03 00:13:20 +02:00
|
|
|
ShipArrivesAt(v, st);
|
2007-04-30 00:33:51 +02:00
|
|
|
v->BeginLoading();
|
2007-04-04 03:35:16 +02:00
|
|
|
} else { // leave stations without docks right aways
|
2005-05-03 00:13:20 +02:00
|
|
|
v->current_order.type = OT_LEAVESTATION;
|
|
|
|
v->cur_order_index++;
|
|
|
|
InvalidateVehicleOrder(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2006-03-08 07:55:33 +01:00
|
|
|
DiagDirection diagdir;
|
2007-02-13 12:29:20 +01:00
|
|
|
/* New tile */
|
|
|
|
if (TileX(gp.new_tile) >= MapMaxX() || TileY(gp.new_tile) >= MapMaxY()) {
|
2005-01-07 18:02:43 +01:00
|
|
|
goto reverse_direction;
|
2007-02-13 12:29:20 +01:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
dir = ShipGetNewDirectionFromTiles(gp.new_tile, gp.old_tile);
|
2006-03-08 07:55:33 +01: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 12:29:20 +01:00
|
|
|
if (tracks == TRACK_BIT_NONE) goto reverse_direction;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-02-13 12:29:20 +01:00
|
|
|
/* Choose a direction, and continue if we find one */
|
2006-03-08 07:55:33 +01:00
|
|
|
track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
|
2007-02-13 12:29:20 +01:00
|
|
|
if (track == INVALID_TRACK) goto reverse_direction;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-03-08 07:55:33 +01:00
|
|
|
b = _ship_subcoord[diagdir][track];
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2007-02-13 12:29:20 +01:00
|
|
|
gp.x = (gp.x & ~0xF) | b[0];
|
|
|
|
gp.y = (gp.y & ~0xF) | b[1];
|
2004-08-09 19:04:08 +02: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 22:02:30 +01:00
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) goto reverse_direction;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-11-19 22:02:30 +01:00
|
|
|
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
|
2004-08-09 19:04:08 +02:00
|
|
|
v->tile = gp.new_tile;
|
2007-01-10 19:56:51 +01:00
|
|
|
v->u.ship.state = TrackToTrackBits(track);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-01-10 19:56:51 +01:00
|
|
|
v->direction = (Direction)b[2];
|
2004-08-09 19:04:08 +02: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 18:35:14 +02:00
|
|
|
v->UpdateDeltaXY(dir);
|
2007-07-01 21:11:47 +02:00
|
|
|
v->cur_image = v->GetImage(dir);
|
2004-08-09 19:04:08 +02:00
|
|
|
VehiclePositionChanged(v);
|
|
|
|
EndVehicleMove(v);
|
|
|
|
return;
|
|
|
|
|
|
|
|
reverse_direction:
|
2006-03-08 07:55:33 +01:00
|
|
|
dir = ReverseDir(v->direction);
|
2004-08-09 19:04:08 +02:00
|
|
|
v->direction = dir;
|
|
|
|
goto getout;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AgeShipCargo(Vehicle *v)
|
|
|
|
{
|
2005-11-14 20:48:04 +01:00
|
|
|
if (_age_cargo_skip_counter != 0) return;
|
2007-06-22 13:58:59 +02:00
|
|
|
v->cargo.AgeCargo();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-07-01 21:24:54 +02:00
|
|
|
void Ship::Tick()
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-07-01 21:24:54 +02:00
|
|
|
AgeShipCargo(this);
|
|
|
|
ShipController(this);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-07 12:47:46 +01:00
|
|
|
void ShipsYearlyLoop()
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2007-03-08 17:27:54 +01:00
|
|
|
if (v->type == VEH_SHIP) {
|
2004-08-09 19:04:08 +02:00
|
|
|
v->profit_last_year = v->profit_this_year;
|
|
|
|
v->profit_this_year = 0;
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
/** Build a ship.
|
2006-04-10 09:15:58 +02:00
|
|
|
* @param tile tile of depot where ship is built
|
2007-04-04 03:35:16 +02:00
|
|
|
* @param flags type of operation
|
2005-05-12 02:11:37 +02:00
|
|
|
* @param p1 ship type being built (engine)
|
2006-05-11 15:31:14 +02:00
|
|
|
* @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
|
2005-05-12 02:11:37 +02:00
|
|
|
*/
|
2007-06-18 12:48:15 +02:00
|
|
|
CommandCost CmdBuildShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
2007-06-18 12:48:15 +02:00
|
|
|
CommandCost value;
|
2005-02-04 15:24:23 +01:00
|
|
|
UnitID unit_num;
|
2004-08-09 19:04:08 +02:00
|
|
|
Engine *e;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2007-04-02 13:35:08 +02:00
|
|
|
if (!IsEngineBuildable(p1, VEH_SHIP, _current_player)) return_cmd_error(STR_SHIP_NOT_AVAILABLE);
|
2005-01-27 22:00:05 +01:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
value = EstimateShipCost(p1);
|
2005-05-12 02:11:37 +02:00
|
|
|
if (flags & DC_QUERY_COST) return value;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-03-06 17:58:42 +01: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 13:56:32 +02:00
|
|
|
if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
|
2005-03-06 17:58:42 +01:00
|
|
|
|
2007-11-19 22:02:30 +01:00
|
|
|
unit_num = HasBit(p2, 0) ? 0 : GetFreeUnitNumber(VEH_SHIP);
|
2006-05-11 15:31:14 +02:00
|
|
|
|
2007-12-09 16:15:26 +01:00
|
|
|
if (!Vehicle::AllocateList(NULL, 1) || unit_num > _patches.max_ships)
|
2004-08-09 19:04:08 +02:00
|
|
|
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
if (flags & DC_EXEC) {
|
2006-04-10 09:15:58 +02:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
2004-12-03 22:57:05 +01:00
|
|
|
const ShipVehicleInfo *svi = ShipVehInfo(p1);
|
|
|
|
|
2007-12-09 16:15:26 +01:00
|
|
|
Vehicle *v = new Ship();
|
2004-08-09 19:04:08 +02:00
|
|
|
v->unitnumber = unit_num;
|
|
|
|
|
|
|
|
v->owner = _current_player;
|
|
|
|
v->tile = tile;
|
2006-04-23 21:35:36 +02:00
|
|
|
x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
|
|
|
|
y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
|
2004-08-09 19:04:08 +02:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
2007-04-19 00:10:36 +02:00
|
|
|
v->z_pos = GetSlopeZ(x, y);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-05-01 18:35:14 +02:00
|
|
|
v->UpdateDeltaXY(v->direction);
|
2004-08-09 19:04:08 +02:00
|
|
|
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-12-03 22:57:05 +01:00
|
|
|
v->spritenum = svi->image_index;
|
|
|
|
v->cargo_type = svi->cargo_type;
|
2006-05-19 12:04:03 +02:00
|
|
|
v->cargo_subtype = 0;
|
2004-12-03 22:57:05 +01:00
|
|
|
v->cargo_cap = svi->capacity;
|
2007-06-18 21:53:50 +02:00
|
|
|
v->value = value.GetCost();
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2005-02-02 17:16:43 +01:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2004-12-03 22:57:05 +01:00
|
|
|
v->max_speed = svi->max_speed;
|
2005-10-01 14:43:34 +02:00
|
|
|
v->engine_type = p1;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-06-07 20:13:49 +02:00
|
|
|
e = GetEngine(p1);
|
2004-08-09 19:04:08 +02:00
|
|
|
v->reliability = e->reliability;
|
|
|
|
v->reliability_spd_dec = e->reliability_spd_dec;
|
|
|
|
v->max_age = e->lifelength * 366;
|
2005-10-29 23:54:28 +02:00
|
|
|
_new_vehicle_id = v->index;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2008-01-12 20:58:06 +01:00
|
|
|
v->name = NULL;
|
2007-02-13 11:46:45 +01:00
|
|
|
v->u.ship.state = TRACK_BIT_DEPOT;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
|
|
|
v->service_interval = _patches.servint_ships;
|
|
|
|
v->date_of_last_service = _date;
|
2006-08-20 21:05:28 +02:00
|
|
|
v->build_year = _cur_year;
|
2004-08-09 19:04:08 +02:00
|
|
|
v->cur_image = 0x0E5E;
|
2005-12-28 23:29:59 +01:00
|
|
|
v->random_bits = VehicleRandomBits();
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-02-28 18:59:05 +01:00
|
|
|
v->vehicle_flags = 0;
|
2007-11-20 14:35:54 +01:00
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
|
2007-02-28 18:59:05 +01:00
|
|
|
|
2007-05-12 09:05:34 +02:00
|
|
|
v->cargo_cap = GetVehicleProperty(v, 0x0D, svi->capacity);
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
VehiclePositionChanged(v);
|
|
|
|
|
2006-10-05 14:59:28 +02:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2004-12-10 19:16:08 +01:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 19:04:08 +02:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
2005-11-09 00:18:09 +01:00
|
|
|
if (IsLocalPlayer())
|
2007-08-08 01:07:10 +02:00
|
|
|
InvalidateAutoreplaceWindow(v->engine_type, v->group_id); // updates the replace Ship window
|
2007-02-06 12:11:12 +01:00
|
|
|
|
|
|
|
GetPlayer(_current_player)->num_engines[p1]++;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
/** Sell a ship.
|
2006-04-10 09:15:58 +02:00
|
|
|
* @param tile unused
|
2007-04-04 03:35:16 +02:00
|
|
|
* @param flags type of operation
|
2005-05-12 02:11:37 +02:00
|
|
|
* @param p1 vehicle ID to be sold
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2007-06-18 12:48:15 +02:00
|
|
|
CommandCost CmdSellShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2006-08-22 20:15:17 +02:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2005-01-06 23:31:58 +01:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-03-08 17:27:54 +01:00
|
|
|
if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-10-08 22:06:37 +02:00
|
|
|
if (HASBITS(v->vehstatus, VS_CRASHED)) return_cmd_error(STR_CAN_T_SELL_DESTROYED_VEHICLE);
|
|
|
|
|
2007-08-29 23:49:08 +02:00
|
|
|
if (!v->IsStoppedInDepot()) {
|
2004-08-09 19:04:08 +02:00
|
|
|
return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN);
|
2006-06-05 13:28:00 +02:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2008-01-09 17:55:48 +01:00
|
|
|
CommandCost ret(EXPENSES_NEW_VEHICLES, -v->value);
|
2007-10-21 18:45:00 +02:00
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
2004-12-10 19:16:08 +01:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 19:04:08 +02:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
2006-12-05 23:59:42 +01:00
|
|
|
DeleteDepotHighlightOfVehicle(v);
|
2007-08-03 21:36:00 +02:00
|
|
|
delete v;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2007-10-21 18:45:00 +02:00
|
|
|
return ret;
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
/** Start/Stop a ship.
|
2006-04-10 09:15:58 +02:00
|
|
|
* @param tile unused
|
2007-04-04 03:35:16 +02:00
|
|
|
* @param flags type of operation
|
2005-05-12 02:11:37 +02:00
|
|
|
* @param p1 ship ID to start/stop
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2007-06-18 12:48:15 +02:00
|
|
|
CommandCost CmdStartStopShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
2006-08-09 23:11:45 +02:00
|
|
|
uint16 callback;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-08-22 20:15:17 +02:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2005-01-30 21:50:06 +01:00
|
|
|
|
2005-01-06 23:31:58 +01:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-03-08 17:27:54 +01:00
|
|
|
if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-08-09 23:11:45 +02: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 19:04:08 +02:00
|
|
|
if (flags & DC_EXEC) {
|
2007-08-29 23:49:08 +02:00
|
|
|
if (v->IsStoppedInDepot()) {
|
2006-03-04 12:01:35 +01:00
|
|
|
DeleteVehicleNews(p1, STR_981C_SHIP_IS_WAITING_IN_DEPOT);
|
|
|
|
}
|
|
|
|
|
2004-08-09 19:04:08 +02:00
|
|
|
v->vehstatus ^= VS_STOPPED;
|
2007-10-31 23:09:23 +01:00
|
|
|
v->cur_speed = 0;
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
2005-05-12 02:11:37 +02:00
|
|
|
InvalidateWindowClasses(WC_SHIPS_LIST);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-06-18 21:53:50 +02:00
|
|
|
return CommandCost();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
/** Send a ship to the depot.
|
2006-04-10 09:15:58 +02:00
|
|
|
* @param tile unused
|
2007-04-04 03:35:16 +02:00
|
|
|
* @param flags type of operation
|
2005-05-12 02:11:37 +02:00
|
|
|
* @param p1 vehicle ID to send to the depot
|
2006-08-30 01:39:57 +02:00
|
|
|
* @param p2 various bitmasked elements
|
2006-09-01 12:24:15 +02:00
|
|
|
* - p2 bit 0-3 - DEPOT_ flags (see vehicle.h)
|
|
|
|
* - p2 bit 8-10 - VLW flag (for mass goto depot)
|
2005-05-12 02:11:37 +02:00
|
|
|
*/
|
2007-06-18 12:48:15 +02:00
|
|
|
CommandCost CmdSendShipToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
2005-05-12 02:11:37 +02:00
|
|
|
const Depot *dep;
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2006-09-01 12:24:15 +02:00
|
|
|
if (p2 & DEPOT_MASS_SEND) {
|
|
|
|
/* Mass goto depot requested */
|
2006-09-04 17:16:58 +02:00
|
|
|
if (!ValidVLWFlags(p2 & VLW_MASK)) return CMD_ERROR;
|
2007-03-08 17:27:54 +01:00
|
|
|
return SendAllVehiclesToDepot(VEH_SHIP, flags, p2 & DEPOT_SERVICE, _current_player, (p2 & VLW_MASK), p1);
|
2006-08-30 23:39:01 +02:00
|
|
|
}
|
|
|
|
|
2006-09-01 12:24:15 +02:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2005-01-30 21:50:06 +01:00
|
|
|
|
2005-01-06 23:31:58 +01:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2007-03-08 17:27:54 +01:00
|
|
|
if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2006-08-30 01:39:57 +02:00
|
|
|
|
2006-09-01 12:24:15 +02:00
|
|
|
if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
|
2005-05-03 21:31:33 +02:00
|
|
|
|
2007-08-29 23:49:08 +02:00
|
|
|
if (v->IsInDepot()) return CMD_ERROR;
|
2006-09-03 00:47:45 +02:00
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
/* If the current orders are already goto-depot */
|
2004-12-05 13:43:04 +01:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
2008-01-15 19:44:22 +01:00
|
|
|
if (!!(p2 & DEPOT_SERVICE) == HasBit(v->current_order.flags, OF_HALT_IN_DEPOT)) {
|
2006-09-01 14:37:03 +02: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) {
|
2008-01-15 19:44:22 +01:00
|
|
|
ClrBit(v->current_order.flags, OF_PART_OF_ORDERS);
|
|
|
|
ToggleBit(v->current_order.flags, OF_HALT_IN_DEPOT);
|
2006-09-01 14:37:03 +02:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
|
|
|
}
|
2007-06-18 21:53:50 +02:00
|
|
|
return CommandCost();
|
2006-09-01 14:37:03 +02:00
|
|
|
}
|
|
|
|
|
2006-09-01 12:24:15 +02:00
|
|
|
if (p2 & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
|
2004-08-09 19:04:08 +02:00
|
|
|
if (flags & DC_EXEC) {
|
2005-05-12 02:11:37 +02: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 */
|
2008-01-15 19:44:22 +01:00
|
|
|
if (HasBit(v->current_order.flags, OF_PART_OF_ORDERS))
|
2005-03-20 09:43:29 +01:00
|
|
|
v->cur_order_index++;
|
|
|
|
|
2004-12-05 13:43:04 +01:00
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-22 00:27:58 +01:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
2007-06-18 21:53:50 +02:00
|
|
|
return CommandCost();
|
2005-05-12 02:11:37 +02:00
|
|
|
}
|
2004-08-09 19:04:08 +02:00
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
dep = FindClosestShipDepot(v);
|
2006-09-01 12:24:15 +02:00
|
|
|
if (dep == NULL) return_cmd_error(STR_981A_UNABLE_TO_FIND_LOCAL_DEPOT);
|
2005-05-12 02:11:37 +02:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2007-04-20 13:39:01 +02:00
|
|
|
if (v->current_order.type == OT_LOADING) v->LeaveStation();
|
|
|
|
|
2005-05-12 02:11:37 +02:00
|
|
|
v->dest_tile = dep->xy;
|
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
2008-01-15 19:44:22 +01:00
|
|
|
v->current_order.flags = OFB_NON_STOP;
|
|
|
|
if (!(p2 & DEPOT_SERVICE)) SetBit(v->current_order.flags, OF_HALT_IN_DEPOT);
|
2006-12-06 00:57:04 +01:00
|
|
|
v->current_order.refit_cargo = CT_INVALID;
|
2006-09-03 10:25:27 +02:00
|
|
|
v->current_order.dest = dep->index;
|
2005-05-12 02:11:37 +02:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
2007-06-18 21:53:50 +02:00
|
|
|
return CommandCost();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-05-14 14:36:16 +02:00
|
|
|
/** Refits a ship to the specified cargo type.
|
2006-04-10 09:15:58 +02:00
|
|
|
* @param tile unused
|
2007-04-04 03:35:16 +02:00
|
|
|
* @param flags type of operation
|
2005-05-14 14:36:16 +02: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 19:38:48 +02:00
|
|
|
* - p2 = (bit 8-15) - the new cargo subtype to refit to
|
2007-04-29 10:43:00 +02:00
|
|
|
* - p2 = (bit 16) - refit only this vehicle (ignored)
|
|
|
|
* @return cost of refit or error
|
2005-05-14 14:36:16 +02:00
|
|
|
*/
|
2007-06-18 12:48:15 +02:00
|
|
|
CommandCost CmdRefitShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 19:04:08 +02:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
2008-01-09 17:55:48 +01:00
|
|
|
CommandCost cost(EXPENSES_SHIP_RUN);
|
2006-06-04 19:38:48 +02:00
|
|
|
CargoID new_cid = GB(p2, 0, 8); //gets the cargo number
|
|
|
|
byte new_subtype = GB(p2, 8, 8);
|
2006-06-09 09:09:48 +02:00
|
|
|
uint16 capacity = CALLBACK_FAILED;
|
2005-01-30 21:50:06 +01:00
|
|
|
|
2006-08-22 20:15:17 +02:00
|
|
|
if (!IsValidVehicleID(p1)) return CMD_ERROR;
|
2004-09-10 21:02:27 +02:00
|
|
|
|
2005-01-06 23:31:58 +01:00
|
|
|
v = GetVehicle(p1);
|
2005-01-30 21:50:06 +01:00
|
|
|
|
2007-03-08 17:27:54 +01:00
|
|
|
if (v->type != VEH_SHIP || !CheckOwnership(v->owner)) return CMD_ERROR;
|
2007-12-27 15:10:47 +01: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 14:36:16 +02:00
|
|
|
|
|
|
|
/* Check cargo */
|
|
|
|
if (!ShipVehInfo(v->engine_type)->refittable) return CMD_ERROR;
|
2007-03-16 22:42:11 +01:00
|
|
|
if (new_cid >= NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
|
2005-01-06 19:45:28 +01:00
|
|
|
|
2006-06-09 09:09:48 +02:00
|
|
|
/* Check the refit capacity callback */
|
2007-11-19 22:02:30 +01:00
|
|
|
if (HasBit(EngInfo(v->engine_type)->callbackmask, CBM_VEHICLE_REFIT_CAPACITY)) {
|
2006-06-09 09:09:48 +02: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 09:05:34 +02:00
|
|
|
capacity = GetVehicleProperty(v, 0x0D, ShipVehInfo(v->engine_type)->capacity);
|
2006-06-09 09:09:48 +02:00
|
|
|
}
|
|
|
|
_returned_refit_capacity = capacity;
|
|
|
|
|
2006-10-14 17:15:56 +02:00
|
|
|
if (IsHumanPlayer(v->owner) && new_cid != v->cargo_type) {
|
2006-09-04 11:07:52 +02:00
|
|
|
cost = GetRefitCost(v->engine_type);
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2006-06-09 09:09:48 +02:00
|
|
|
v->cargo_cap = capacity;
|
2007-06-22 13:58:59 +02:00
|
|
|
v->cargo.Truncate((v->cargo_type == new_cid) ? capacity : 0);
|
2005-05-14 14:36:16 +02:00
|
|
|
v->cargo_type = new_cid;
|
2006-06-04 19:38:48 +02:00
|
|
|
v->cargo_subtype = new_subtype;
|
2004-08-09 19:04:08 +02:00
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2006-06-04 19:38:48 +02:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
RebuildVehicleLists();
|
2004-08-09 19:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
|
|
|
|
}
|