2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file ship_cmd.cpp Handling of ships. */
|
2007-04-04 01:35:16 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2006-06-05 11:28:00 +00:00
|
|
|
#include "ship.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2007-06-20 19:17:22 +00:00
|
|
|
#include "timetable.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
2009-12-01 23:56:04 +00:00
|
|
|
#include "pathfinder/npf/npf_func.h"
|
2008-04-17 19:10:30 +00:00
|
|
|
#include "depot_base.h"
|
2009-12-01 22:45:39 +00:00
|
|
|
#include "station_base.h"
|
2006-02-03 12:55:21 +00:00
|
|
|
#include "newgrf_engine.h"
|
2009-12-01 22:45:39 +00:00
|
|
|
#include "pathfinder/yapf/yapf.h"
|
2024-01-27 14:06:14 +00:00
|
|
|
#include "pathfinder/yapf/yapf_ship_regions.h"
|
2006-09-27 18:17:01 +00:00
|
|
|
#include "newgrf_sound.h"
|
2007-02-10 13:37:32 +00:00
|
|
|
#include "spritecache.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "window_func.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
2020-10-03 21:28:20 +00:00
|
|
|
#include "vehicle_gui.h"
|
2007-12-29 09:24:26 +00:00
|
|
|
#include "sound_func.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "ai/ai.hpp"
|
2017-04-23 09:19:32 +00:00
|
|
|
#include "game/game.hpp"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "engine_base.h"
|
|
|
|
#include "company_base.h"
|
2015-08-06 21:24:24 +00:00
|
|
|
#include "infrastructure_func.h"
|
2011-07-12 17:12:10 +00:00
|
|
|
#include "tunnelbridge_map.h"
|
2011-11-24 12:38:48 +00:00
|
|
|
#include "zoom_func.h"
|
2018-07-19 19:17:07 +00:00
|
|
|
#include "framerate_type.h"
|
2019-03-11 10:37:47 +00:00
|
|
|
#include "industry.h"
|
|
|
|
#include "industry_map.h"
|
2019-08-05 18:16:51 +00:00
|
|
|
#include "core/checksum_func.hpp"
|
2023-03-14 00:16:31 +00:00
|
|
|
#include "articulated_vehicles.h"
|
2024-01-27 14:06:14 +00:00
|
|
|
#include "core/ring_buffer.hpp"
|
|
|
|
#include "3rdparty/robin_hood/robin_hood.h"
|
2008-01-07 00:57:19 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2024-01-27 14:06:14 +00:00
|
|
|
/** Max distance in tiles (as the crow flies) to search for depots when user clicks "go to depot". */
|
|
|
|
constexpr int MAX_SHIP_DEPOT_SEARCH_DISTANCE = 80;
|
|
|
|
|
2019-03-27 18:12:04 +00:00
|
|
|
/** Directions to search towards given track bits and the ship's enter direction. */
|
|
|
|
const DiagDirection _ship_search_directions[6][4] = {
|
|
|
|
{ DIAGDIR_NE, INVALID_DIAGDIR, DIAGDIR_SW, INVALID_DIAGDIR },
|
|
|
|
{ INVALID_DIAGDIR, DIAGDIR_SE, INVALID_DIAGDIR, DIAGDIR_NW },
|
|
|
|
{ INVALID_DIAGDIR, DIAGDIR_NE, DIAGDIR_NW, INVALID_DIAGDIR },
|
|
|
|
{ DIAGDIR_SE, INVALID_DIAGDIR, INVALID_DIAGDIR, DIAGDIR_SW },
|
|
|
|
{ DIAGDIR_NW, DIAGDIR_SW, INVALID_DIAGDIR, INVALID_DIAGDIR },
|
|
|
|
{ INVALID_DIAGDIR, INVALID_DIAGDIR, DIAGDIR_SE, DIAGDIR_NE },
|
|
|
|
};
|
|
|
|
|
2011-07-12 17:12:10 +00:00
|
|
|
/**
|
|
|
|
* Determine the effective #WaterClass for a ship travelling on a tile.
|
|
|
|
* @param tile Tile of interest
|
|
|
|
* @return the waterclass to be used by the ship.
|
|
|
|
*/
|
2011-08-21 14:13:22 +00:00
|
|
|
WaterClass GetEffectiveWaterClass(TileIndex tile)
|
2011-07-12 17:12:10 +00:00
|
|
|
{
|
|
|
|
if (HasTileWaterClass(tile)) return GetWaterClass(tile);
|
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE)) {
|
2022-10-22 11:34:54 +00:00
|
|
|
dbg_assert_tile(GetTunnelBridgeTransportType(tile) == TRANSPORT_WATER, tile);
|
2011-07-12 17:12:10 +00:00
|
|
|
return WATER_CLASS_CANAL;
|
|
|
|
}
|
|
|
|
if (IsTileType(tile, MP_RAILWAY)) {
|
2022-10-22 11:34:54 +00:00
|
|
|
dbg_assert_tile(GetRailGroundType(tile) == RAIL_GROUND_WATER, tile);
|
2011-07-12 17:12:10 +00:00
|
|
|
return WATER_CLASS_SEA;
|
|
|
|
}
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
static const uint16_t _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-11-26 16:08:58 +00:00
|
|
|
template <>
|
2024-01-07 16:41:53 +00:00
|
|
|
bool IsValidImageIndex<VEH_SHIP>(uint8_t image_index)
|
2013-11-26 16:08:58 +00:00
|
|
|
{
|
|
|
|
return image_index < lengthof(_ship_sprites);
|
|
|
|
}
|
|
|
|
|
2008-02-20 17:49:50 +00:00
|
|
|
static inline TrackBits GetTileShipTrackStatus(TileIndex tile)
|
2005-06-24 12:38:35 +00:00
|
|
|
{
|
2023-03-04 01:50:18 +00:00
|
|
|
return TrackdirBitsToTrackBits(GetTileTrackdirBits(tile, TRANSPORT_WATER, 0));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 14:57:56 +00:00
|
|
|
static void GetShipIcon(EngineID engine, EngineImageType image_type, VehicleSpriteSeq *result)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-10-06 19:17:07 +00:00
|
|
|
const Engine *e = Engine::Get(engine);
|
2024-01-07 16:41:53 +00:00
|
|
|
uint8_t spritenum = e->u.ship.image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (is_custom_sprite(spritenum)) {
|
2016-10-16 14:57:56 +00:00
|
|
|
GetCustomVehicleIcon(engine, DIR_W, image_type, result);
|
|
|
|
if (result->IsValid()) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-10-06 19:17:07 +00:00
|
|
|
spritenum = e->original_image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2022-10-22 11:34:54 +00:00
|
|
|
dbg_assert(IsValidImageIndex<VEH_SHIP>(spritenum));
|
2016-10-16 14:57:56 +00:00
|
|
|
result->Set(DIR_W + _ship_sprites[spritenum]);
|
2008-04-21 20:50:58 +00:00
|
|
|
}
|
|
|
|
|
2011-11-01 16:51:47 +00:00
|
|
|
void DrawShipEngine(int left, int right, int preferred_x, int y, EngineID engine, PaletteID pal, EngineImageType image_type)
|
2008-04-21 20:50:58 +00:00
|
|
|
{
|
2016-10-16 14:57:56 +00:00
|
|
|
VehicleSpriteSeq seq;
|
|
|
|
GetShipIcon(engine, image_type, &seq);
|
|
|
|
|
2018-01-19 19:13:01 +00:00
|
|
|
Rect16 rect = seq.GetBounds();
|
2023-04-12 18:34:55 +00:00
|
|
|
preferred_x = SoftClamp(preferred_x,
|
2016-10-16 14:58:38 +00:00
|
|
|
left - UnScaleGUI(rect.left),
|
|
|
|
right - UnScaleGUI(rect.right));
|
|
|
|
|
|
|
|
seq.Draw(preferred_x, y, pal, pal == PALETTE_CRASH);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
2012-12-23 01:00:25 +00:00
|
|
|
* Get the size of the sprite of a ship sprite heading west (used for lists).
|
|
|
|
* @param engine The engine to get the sprite from.
|
|
|
|
* @param[out] width The width of the sprite.
|
|
|
|
* @param[out] height The height of the sprite.
|
|
|
|
* @param[out] xoffs Number of pixels to shift the sprite to the right.
|
|
|
|
* @param[out] yoffs Number of pixels to shift the sprite downwards.
|
|
|
|
* @param image_type Context the sprite is used in.
|
2007-02-10 13:37:32 +00:00
|
|
|
*/
|
2012-12-23 01:00:25 +00:00
|
|
|
void GetShipSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type)
|
2007-02-10 13:37:32 +00:00
|
|
|
{
|
2016-10-16 14:57:56 +00:00
|
|
|
VehicleSpriteSeq seq;
|
|
|
|
GetShipIcon(engine, image_type, &seq);
|
|
|
|
|
2022-12-03 23:40:22 +00:00
|
|
|
Rect rect = ConvertRect<Rect16, Rect>(seq.GetBounds());
|
2007-02-10 13:37:32 +00:00
|
|
|
|
2022-09-28 23:10:41 +00:00
|
|
|
width = UnScaleGUI(rect.Width());
|
|
|
|
height = UnScaleGUI(rect.Height());
|
2016-10-16 14:58:38 +00:00
|
|
|
xoffs = UnScaleGUI(rect.left);
|
|
|
|
yoffs = UnScaleGUI(rect.top);
|
2007-02-10 13:37:32 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 14:57:56 +00:00
|
|
|
void Ship::GetImage(Direction direction, EngineImageType image_type, VehicleSpriteSeq *result) const
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2024-01-07 16:41:53 +00:00
|
|
|
uint8_t spritenum = this->spritenum;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2018-05-20 10:03:14 +00:00
|
|
|
if (image_type == EIT_ON_MAP) direction = this->rotation;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (is_custom_sprite(spritenum)) {
|
2016-10-16 14:57:56 +00:00
|
|
|
GetCustomVehicleSprite(this, direction, image_type, result);
|
|
|
|
if (result->IsValid()) return;
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2011-11-01 00:21:08 +00:00
|
|
|
spritenum = this->GetEngine()->original_image_index;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-21 20:50:58 +00:00
|
|
|
|
2022-10-22 11:34:54 +00:00
|
|
|
dbg_assert(IsValidImageIndex<VEH_SHIP>(spritenum));
|
2016-10-16 14:57:56 +00:00
|
|
|
result->Set(_ship_sprites[spritenum] + direction);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-12-20 16:19:47 +00:00
|
|
|
static const Depot *FindClosestShipDepot(const Vehicle *v, uint max_distance)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2024-01-27 14:06:14 +00:00
|
|
|
const uint max_region_distance = (max_distance / WATER_REGION_EDGE_LENGTH) + 1;
|
|
|
|
|
|
|
|
static robin_hood::unordered_flat_set<uint32_t> visited_patch_hashes;
|
|
|
|
static ring_buffer<WaterRegionPatchDesc> patches_to_search;
|
|
|
|
visited_patch_hashes.clear();
|
|
|
|
patches_to_search.clear();
|
|
|
|
|
|
|
|
/* Step 1: find a set of reachable Water Region Patches using BFS. */
|
|
|
|
const WaterRegionPatchDesc start_patch = GetWaterRegionPatchInfo(v->tile);
|
|
|
|
patches_to_search.push_back(start_patch);
|
|
|
|
visited_patch_hashes.insert(CalculateWaterRegionPatchHash(start_patch));
|
|
|
|
|
|
|
|
while (!patches_to_search.empty()) {
|
|
|
|
/* Remove first patch from the queue and make it the current patch. */
|
|
|
|
const WaterRegionPatchDesc current_node = patches_to_search.front();
|
|
|
|
patches_to_search.pop_front();
|
|
|
|
|
|
|
|
/* Add neighbors of the current patch to the search queue. */
|
|
|
|
TVisitWaterRegionPatchCallBack visitFunc = [&](const WaterRegionPatchDesc &water_region_patch) {
|
|
|
|
/* Note that we check the max distance per axis, not the total distance. */
|
|
|
|
if (Delta(water_region_patch.x, start_patch.x) > max_region_distance ||
|
|
|
|
Delta(water_region_patch.y, start_patch.y) > max_region_distance) return;
|
|
|
|
|
|
|
|
const uint32_t hash = CalculateWaterRegionPatchHash(water_region_patch);
|
|
|
|
auto res = visited_patch_hashes.insert(hash);
|
|
|
|
if (res.second) {
|
|
|
|
patches_to_search.push_back(water_region_patch);
|
|
|
|
}
|
|
|
|
};
|
2008-02-13 17:54:11 +00:00
|
|
|
|
2024-01-27 14:06:14 +00:00
|
|
|
VisitWaterRegionPatchNeighbors(current_node, visitFunc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step 2: Find the closest depot within the reachable Water Region Patches. */
|
|
|
|
const uint max_distance_sq = max_distance * max_distance;
|
|
|
|
const Depot *best_depot = nullptr;
|
|
|
|
uint best_dist_sq = std::numeric_limits<uint>::max();
|
2019-12-15 16:54:05 +00:00
|
|
|
for (const Depot *depot : Depot::Iterate()) {
|
2024-01-27 14:06:14 +00:00
|
|
|
const TileIndex tile = depot->xy;
|
2015-08-06 21:24:24 +00:00
|
|
|
if (IsShipDepotTile(tile) && IsInfraTileUsageAllowed(VEH_SHIP, v->owner, tile)) {
|
2024-01-27 14:06:14 +00:00
|
|
|
const uint dist_sq = DistanceSquare(tile, v->tile);
|
|
|
|
if (dist_sq < best_dist_sq && dist_sq <= max_distance_sq &&
|
|
|
|
visited_patch_hashes.count(CalculateWaterRegionPatchHash(GetWaterRegionPatchInfo(tile))) > 0) {
|
|
|
|
best_dist_sq = dist_sq;
|
2008-02-13 17:54:11 +00:00
|
|
|
best_depot = depot;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-02-13 17:54:11 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return best_depot;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckIfShipNeedsService(Vehicle *v)
|
|
|
|
{
|
2009-05-26 21:59:49 +00:00
|
|
|
if (Company::Get(v->owner)->settings.vehicle.servint_ships == 0 || !v->NeedsAutomaticServicing()) return;
|
2012-07-07 15:39:46 +00:00
|
|
|
if (v->IsChainInDepot()) {
|
2006-09-03 11:49:38 +00:00
|
|
|
VehicleServiceInDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-13 10:48:44 +00:00
|
|
|
uint max_distance;
|
|
|
|
switch (_settings_game.pf.pathfinder_for_ships) {
|
|
|
|
case VPF_NPF: max_distance = _settings_game.pf.npf.maximum_go_to_depot_penalty / NPF_TILE_LENGTH; break;
|
|
|
|
case VPF_YAPF: max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; break;
|
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2009-12-20 16:19:47 +00:00
|
|
|
const Depot *depot = FindClosestShipDepot(v, max_distance);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (depot == nullptr) {
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT)) {
|
|
|
|
v->current_order.MakeDummy();
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-07 19:18:56 +00:00
|
|
|
v->current_order.MakeGoToDepot(depot->index, ODTFB_SERVICE);
|
2019-01-14 23:33:42 +00:00
|
|
|
v->SetDestTile(depot->xy);
|
2011-12-16 16:58:55 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2011-05-01 19:51:52 +00:00
|
|
|
/**
|
|
|
|
* Update the caches of this ship.
|
|
|
|
*/
|
2010-11-06 13:03:17 +00:00
|
|
|
void Ship::UpdateCache()
|
|
|
|
{
|
2011-07-07 14:16:22 +00:00
|
|
|
const ShipVehicleInfo *svi = ShipVehInfo(this->engine_type);
|
|
|
|
|
|
|
|
/* Get speed fraction for the current water type. Aqueducts are always canals. */
|
2011-08-27 10:33:45 +00:00
|
|
|
bool is_ocean = GetEffectiveWaterClass(this->tile) == WATER_CLASS_SEA;
|
|
|
|
uint raw_speed = GetVehicleProperty(this, PROP_SHIP_SPEED, svi->max_speed);
|
|
|
|
this->vcache.cached_max_speed = svi->ApplyWaterClassSpeedFrac(raw_speed, is_ocean);
|
2010-11-18 14:17:55 +00:00
|
|
|
|
2011-08-03 20:55:08 +00:00
|
|
|
/* Update cargo aging period. */
|
2023-03-14 00:16:31 +00:00
|
|
|
for (Ship *u = this; u != nullptr; u = u->Next()) {
|
|
|
|
u->vcache.cached_cargo_age_period = GetVehicleProperty(u, PROP_SHIP_CARGO_AGE_PERIOD, EngInfo(u->engine_type)->cargo_age_period);
|
|
|
|
}
|
2011-08-03 20:55:08 +00:00
|
|
|
|
2010-11-18 14:17:55 +00:00
|
|
|
this->UpdateVisualEffect();
|
2023-03-14 00:16:31 +00:00
|
|
|
|
|
|
|
SetBit(this->vcache.cached_veh_flags, VCF_LAST_VISUAL_EFFECT);
|
2010-11-06 13:03:17 +00:00
|
|
|
}
|
|
|
|
|
2009-01-22 21:33:08 +00:00
|
|
|
Money Ship::GetRunningCost() const
|
|
|
|
{
|
2011-11-01 00:21:08 +00:00
|
|
|
const Engine *e = this->GetEngine();
|
2009-11-24 13:12:34 +00:00
|
|
|
uint cost_factor = GetVehicleProperty(this, PROP_SHIP_RUNNING_COST_FACTOR, e->u.ship.running_cost);
|
2021-11-18 23:55:48 +00:00
|
|
|
Money cost = GetPrice(PR_RUNNING_SHIP, cost_factor, e->GetGRF());
|
2021-11-18 02:17:23 +00:00
|
|
|
|
2021-11-18 23:41:12 +00:00
|
|
|
if (this->cur_speed == 0) {
|
|
|
|
if (this->IsInDepot()) {
|
|
|
|
/* running costs if in depot */
|
2021-11-18 23:55:48 +00:00
|
|
|
cost = CeilDivT<Money>(cost, _settings_game.difficulty.vehicle_costs_in_depot);
|
2021-11-18 23:41:12 +00:00
|
|
|
} else {
|
|
|
|
/* running costs if stopped */
|
2021-11-18 23:55:48 +00:00
|
|
|
cost = CeilDivT<Money>(cost, _settings_game.difficulty.vehicle_costs_when_stopped);
|
2021-11-18 23:41:12 +00:00
|
|
|
}
|
2021-11-18 23:55:48 +00:00
|
|
|
}
|
|
|
|
return cost;
|
2009-01-22 21:33:08 +00:00
|
|
|
}
|
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
void Ship::OnNewDay()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2023-03-14 00:16:31 +00:00
|
|
|
if (!this->IsPrimaryVehicle()) return;
|
|
|
|
|
2010-07-24 10:14:39 +00:00
|
|
|
if ((++this->day_counter & 7) == 0) {
|
2008-02-01 22:02:14 +00:00
|
|
|
DecreaseVehicleValue(this);
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2024-02-16 20:23:23 +00:00
|
|
|
if (!EconTime::UsingWallclockUnits()) AgeVehicle(this);
|
2021-11-27 15:51:49 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2021-11-27 15:51:49 +00:00
|
|
|
void Ship::OnPeriodic()
|
|
|
|
{
|
2023-03-14 00:16:31 +00:00
|
|
|
if (!this->IsPrimaryVehicle()) return;
|
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
CheckVehicleBreakdown(this);
|
|
|
|
CheckIfShipNeedsService(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-01 22:02:14 +00:00
|
|
|
CheckOrders(this);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2008-02-13 19:24:40 +00:00
|
|
|
if (this->running_ticks == 0) return;
|
|
|
|
|
2009-01-22 21:33:08 +00:00
|
|
|
CommandCost cost(EXPENSES_SHIP_RUN, this->GetRunningCost() * this->running_ticks / (DAYS_IN_YEAR * DAY_TICKS));
|
2004-08-11 10:15:38 +00:00
|
|
|
|
2008-02-13 19:24:40 +00:00
|
|
|
this->profit_this_year -= cost.GetCost();
|
|
|
|
this->running_ticks = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
SubtractMoneyFromCompanyFract(this->owner, cost);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DETAILS, this->index);
|
2007-04-04 01:35:16 +00:00
|
|
|
/* we need this for the profit */
|
2020-10-03 21:28:20 +00:00
|
|
|
DirtyVehicleListWindowForVehicle(this);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
Trackdir Ship::GetVehicleTrackdir() const
|
|
|
|
{
|
|
|
|
if (this->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
|
|
|
|
|
|
|
|
if (this->IsInDepot()) {
|
|
|
|
/* We'll assume the ship is facing outwards */
|
|
|
|
return DiagDirToDiagTrackdir(GetShipDepotDirection(this->tile));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->state == TRACK_BIT_WORMHOLE) {
|
2021-05-09 17:02:17 +00:00
|
|
|
/* ship on aqueduct, so just use its direction and assume a diagonal track */
|
2009-05-22 18:17:20 +00:00
|
|
|
return DiagDirToDiagTrackdir(DirToDiagDir(this->direction));
|
|
|
|
}
|
|
|
|
|
|
|
|
return TrackDirectionToTrackdir(FindFirstTrack(this->state), this->direction);
|
|
|
|
}
|
|
|
|
|
2007-04-29 22:33:51 +00:00
|
|
|
void Ship::MarkDirty()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2013-08-04 14:02:27 +00:00
|
|
|
this->colourmap = PAL_NONE;
|
2020-08-25 00:26:44 +00:00
|
|
|
this->InvalidateImageCache();
|
2013-08-04 14:02:27 +00:00
|
|
|
this->UpdateViewport(true, false);
|
2010-11-11 22:19:27 +00:00
|
|
|
this->UpdateCache();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 20:36:18 +00:00
|
|
|
void Ship::PlayLeaveStationSound(bool force) const
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2022-11-10 20:36:18 +00:00
|
|
|
if (PlayVehicleSound(this, VSE_START, force)) return;
|
|
|
|
SndPlayVehicleFx(ShipVehInfo(this->engine_type)->sfx, this);
|
2007-05-07 15:58:05 +00:00
|
|
|
}
|
|
|
|
|
2008-04-05 10:55:50 +00:00
|
|
|
TileIndex Ship::GetOrderStationLocation(StationID station)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-05 12:01:34 +00:00
|
|
|
if (station == this->last_station_visited) this->last_station_visited = INVALID_STATION;
|
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
const Station *st = Station::Get(station);
|
2019-03-11 10:37:47 +00:00
|
|
|
if (CanVehicleUseStation(this, st)) {
|
2018-02-05 18:21:05 +00:00
|
|
|
return st->xy;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2011-01-31 20:44:15 +00:00
|
|
|
this->IncrementRealOrderIndex();
|
2008-04-05 10:55:50 +00:00
|
|
|
return 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-22 17:43:34 +00:00
|
|
|
void Ship::UpdateDeltaXY()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2024-01-07 16:41:53 +00:00
|
|
|
static const int8_t _delta_xy_table[8][4] = {
|
2012-06-06 14:03:22 +00:00
|
|
|
/* y_extent, x_extent, y_offs, x_offs */
|
|
|
|
{ 6, 6, -3, -3}, // N
|
|
|
|
{ 6, 32, -3, -16}, // NE
|
|
|
|
{ 6, 6, -3, -3}, // E
|
|
|
|
{32, 6, -16, -3}, // SE
|
|
|
|
{ 6, 6, -3, -3}, // S
|
|
|
|
{ 6, 32, -3, -16}, // SW
|
|
|
|
{ 6, 6, -3, -3}, // W
|
|
|
|
{32, 6, -16, -3}, // NW
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
2007-05-01 16:35:14 +00:00
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
const int8_t *bb = _delta_xy_table[this->rotation];
|
2012-06-06 14:03:22 +00:00
|
|
|
this->x_offs = bb[3];
|
|
|
|
this->y_offs = bb[2];
|
|
|
|
this->x_extent = bb[1];
|
|
|
|
this->y_extent = bb[0];
|
2008-04-01 14:03:20 +00:00
|
|
|
this->z_extent = 6;
|
2019-01-31 20:54:15 +00:00
|
|
|
|
|
|
|
if (this->direction != this->rotation) {
|
|
|
|
/* If we are rotating, then it is possible the ship was moved to its next position. In that
|
|
|
|
* case, because we are still showing the old direction, the ship will appear to glitch sideways
|
|
|
|
* slightly. We can work around this by applying an additional offset to make the ship appear
|
|
|
|
* where it was before it moved. */
|
|
|
|
this->x_offs -= this->x_pos - this->rotation_x_pos;
|
|
|
|
this->y_offs -= this->y_pos - this->rotation_y_pos;
|
|
|
|
}
|
2022-05-26 22:26:57 +00:00
|
|
|
}
|
2022-05-23 18:17:42 +00:00
|
|
|
|
2022-05-26 22:26:57 +00:00
|
|
|
bool RecentreShipSpriteBounds(Vehicle *v)
|
|
|
|
{
|
|
|
|
Ship *ship = Ship::From(v);
|
|
|
|
if (ship->rotation != ship->cur_image_valid_dir) {
|
|
|
|
ship->cur_image_valid_dir = INVALID_DIR;
|
|
|
|
Point offset = RemapCoords(ship->x_offs, ship->y_offs, 0);
|
|
|
|
ship->sprite_seq_bounds.left = -offset.x - 16;
|
|
|
|
ship->sprite_seq_bounds.right = ship->sprite_seq_bounds.left + 32;
|
|
|
|
ship->sprite_seq_bounds.top = -offset.y - 16;
|
|
|
|
ship->sprite_seq_bounds.bottom = ship->sprite_seq_bounds.top + 32;
|
|
|
|
return true;
|
2022-05-23 18:17:42 +00:00
|
|
|
}
|
2022-05-26 22:26:57 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 22:50:04 +00:00
|
|
|
int Ship::GetEffectiveMaxSpeed() const
|
|
|
|
{
|
|
|
|
int max_speed = this->vcache.cached_max_speed;
|
|
|
|
|
|
|
|
if (this->critical_breakdown_count == 0) return max_speed;
|
|
|
|
|
|
|
|
for (uint i = 0; i < this->critical_breakdown_count; i++) {
|
|
|
|
max_speed = std::min(max_speed - (max_speed / 3) + 1, max_speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clamp speed to be no less than lower of 5mph and 1/8 of base speed */
|
2024-01-07 16:41:53 +00:00
|
|
|
return std::max<uint16_t>(max_speed, std::min<uint16_t>(10, (this->vcache.cached_max_speed + 7) >> 3));
|
2021-09-11 22:50:04 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 17:02:53 +00:00
|
|
|
/**
|
2024-02-23 13:08:16 +00:00
|
|
|
* Test-procedure for HasVehicleOnPos to check for any ships which are moving.
|
2018-05-18 17:02:53 +00:00
|
|
|
*/
|
2023-09-16 20:20:53 +00:00
|
|
|
static Vehicle *EnsureNoMovingShipProc(Vehicle *v, void *)
|
2018-05-18 17:02:53 +00:00
|
|
|
{
|
2024-02-26 18:19:34 +00:00
|
|
|
return (v->cur_speed != 0) ? v : nullptr;
|
2018-05-18 17:02:53 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 15:58:19 +00:00
|
|
|
static bool CheckReverseShip(const Ship *v, Trackdir *trackdir = nullptr)
|
2021-10-05 15:58:19 +00:00
|
|
|
{
|
|
|
|
/* Ask pathfinder for best direction */
|
|
|
|
bool reverse = false;
|
|
|
|
switch (_settings_game.pf.pathfinder_for_ships) {
|
2021-10-05 15:58:19 +00:00
|
|
|
case VPF_NPF: reverse = NPFShipCheckReverse(v, trackdir); break;
|
|
|
|
case VPF_YAPF: reverse = YapfShipCheckReverse(v, trackdir); break;
|
2021-10-05 15:58:19 +00:00
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
|
|
|
return reverse;
|
|
|
|
}
|
|
|
|
|
2011-04-10 10:47:21 +00:00
|
|
|
static bool CheckShipLeaveDepot(Ship *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2012-07-07 15:39:46 +00:00
|
|
|
if (!v->IsChainInDepot()) return false;
|
2011-04-10 10:47:21 +00:00
|
|
|
|
2015-08-03 00:06:42 +00:00
|
|
|
if (v->current_order.IsWaitTimetabled()) {
|
2021-08-30 14:26:49 +00:00
|
|
|
v->HandleWaiting(false, true);
|
2015-08-03 00:06:42 +00:00
|
|
|
}
|
|
|
|
if (v->current_order.IsType(OT_WAITING)) {
|
2015-08-03 00:06:05 +00:00
|
|
|
return true;
|
2015-08-03 00:06:42 +00:00
|
|
|
}
|
2015-08-03 00:06:05 +00:00
|
|
|
|
2024-02-03 13:04:24 +00:00
|
|
|
/* Check if we should wait here for unbunching. */
|
|
|
|
if (v->IsWaitingForUnbunching()) return true;
|
|
|
|
|
2011-04-10 10:47:21 +00:00
|
|
|
/* We are leaving a depot, but have to go to the exact same one; re-enter */
|
|
|
|
if (v->current_order.IsType(OT_GOTO_DEPOT) &&
|
|
|
|
IsShipDepotTile(v->tile) && GetDepotIndex(v->tile) == v->current_order.GetDestination()) {
|
|
|
|
VehicleEnterDepot(v);
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2018-05-18 08:04:39 +00:00
|
|
|
/* Don't leave depot if no destination set */
|
|
|
|
if (v->dest_tile == 0) return true;
|
|
|
|
|
2018-05-18 17:02:53 +00:00
|
|
|
/* Don't leave depot if another vehicle is already entering/leaving */
|
|
|
|
/* This helps avoid CPU load if many ships are set to start at the same time */
|
2021-02-19 15:38:34 +00:00
|
|
|
if (HasVehicleOnPos(v->tile, VEH_SHIP, nullptr, &EnsureNoMovingShipProc)) return true;
|
2018-05-18 17:02:53 +00:00
|
|
|
|
2008-04-24 13:05:51 +00:00
|
|
|
TileIndex tile = v->tile;
|
|
|
|
Axis axis = GetShipDepotAxis(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2012-08-18 11:37:47 +00:00
|
|
|
DiagDirection north_dir = ReverseDiagDir(AxisToDiagDir(axis));
|
2017-03-19 22:02:20 +00:00
|
|
|
TileIndex north_neighbour = TILE_ADD(tile, TileOffsByDiagDir(north_dir));
|
2012-08-18 11:37:47 +00:00
|
|
|
DiagDirection south_dir = AxisToDiagDir(axis);
|
2017-03-19 22:02:20 +00:00
|
|
|
TileIndex south_neighbour = TILE_ADD(tile, 2 * TileOffsByDiagDir(south_dir));
|
2012-08-18 11:37:47 +00:00
|
|
|
|
|
|
|
TrackBits north_tracks = DiagdirReachesTracks(north_dir) & GetTileShipTrackStatus(north_neighbour);
|
|
|
|
TrackBits south_tracks = DiagdirReachesTracks(south_dir) & GetTileShipTrackStatus(south_neighbour);
|
|
|
|
if (north_tracks && south_tracks) {
|
2021-10-05 15:58:19 +00:00
|
|
|
if (CheckReverseShip(v)) north_tracks = TRACK_BIT_NONE;
|
2012-08-18 11:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (north_tracks) {
|
|
|
|
/* Leave towards north */
|
2018-05-20 10:03:14 +00:00
|
|
|
v->rotation = v->direction = DiagDirToDir(north_dir);
|
2012-08-18 11:37:47 +00:00
|
|
|
} else if (south_tracks) {
|
|
|
|
/* Leave towards south */
|
2018-05-20 10:03:14 +00:00
|
|
|
v->rotation = v->direction = DiagDirToDir(south_dir);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2012-08-18 11:37:47 +00:00
|
|
|
/* Both ways blocked */
|
2011-04-10 10:47:21 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-04-24 13:05:51 +00:00
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
v->state = AxisToTrackBits(axis);
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus &= ~VS_HIDDEN;
|
2020-02-02 22:59:18 +00:00
|
|
|
v->UpdateIsDrawn();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->cur_speed = 0;
|
2012-01-02 13:44:37 +00:00
|
|
|
v->UpdateViewport(true, true);
|
2010-07-14 12:27:51 +00:00
|
|
|
SetWindowDirty(WC_VEHICLE_DEPOT, v->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-09 21:46:56 +00:00
|
|
|
VehicleServiceInDepot(v);
|
2024-02-03 13:04:24 +00:00
|
|
|
v->LeaveUnbunchingDepot();
|
|
|
|
v->PlayLeaveStationSound();
|
2006-10-05 12:59:28 +00:00
|
|
|
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
|
2020-10-03 21:28:20 +00:00
|
|
|
DirtyVehicleListWindowForVehicle(v);
|
2011-04-10 10:47:21 +00:00
|
|
|
|
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2021-08-29 21:04:06 +00:00
|
|
|
static inline void UpdateShipSpeed(Vehicle *v, uint speed)
|
|
|
|
{
|
|
|
|
if (v->cur_speed == speed) return;
|
|
|
|
|
|
|
|
v->cur_speed = speed;
|
|
|
|
|
|
|
|
/* updates statusbar only if speed have changed to save CPU time */
|
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
|
|
|
|
|
|
|
if (HasBit(v->vcache.cached_veh_flags, VCF_REDRAW_ON_SPEED_CHANGE)) {
|
|
|
|
v->InvalidateImageCacheOfChain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
/**
|
|
|
|
* Accelerates the ship towards its target speed.
|
|
|
|
* @param v Ship to accelerate.
|
|
|
|
* @return Number of steps to move the ship.
|
|
|
|
*/
|
|
|
|
static uint ShipAccelerate(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2023-04-29 08:33:01 +00:00
|
|
|
uint speed;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2024-02-17 11:53:23 +00:00
|
|
|
speed = std::min<uint>(v->cur_speed + v->acceleration, Ship::From(v)->GetEffectiveMaxSpeed());
|
2023-04-29 08:33:01 +00:00
|
|
|
speed = std::min<uint>(speed, v->current_order.GetMaxSpeed() * 2);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2016-01-31 15:22:46 +00:00
|
|
|
if (v->breakdown_ctr == 1 && v->breakdown_type == BREAKDOWN_LOW_POWER && v->cur_speed > (v->breakdown_severity * ShipVehInfo(v->engine_type)->max_speed) >> 8) {
|
|
|
|
if ((v->tick_counter & 0x7) == 0 && v->cur_speed > 0) {
|
2023-05-29 23:49:14 +00:00
|
|
|
speed = v->cur_speed - 1;
|
2015-08-02 21:58:41 +00:00
|
|
|
} else {
|
2023-05-29 23:49:14 +00:00
|
|
|
speed = v->cur_speed;
|
2015-08-02 21:58:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-31 15:22:46 +00:00
|
|
|
if (v->breakdown_ctr == 1 && v->breakdown_type == BREAKDOWN_LOW_SPEED) {
|
2023-05-29 23:49:14 +00:00
|
|
|
speed = std::min<uint>(speed, v->breakdown_severity);
|
2015-08-02 21:58:41 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 23:49:14 +00:00
|
|
|
UpdateShipSpeed(v, speed);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
const uint advance_speed = v->GetAdvanceSpeed(speed);
|
|
|
|
const uint number_of_steps = (advance_speed + v->progress) / v->GetAdvanceDistance();
|
|
|
|
const uint remainder = (advance_speed + v->progress) % v->GetAdvanceDistance();
|
2023-05-29 23:49:14 +00:00
|
|
|
dbg_assert(remainder <= std::numeric_limits<byte>::max());
|
2023-04-29 08:33:01 +00:00
|
|
|
v->progress = static_cast<byte>(remainder);
|
|
|
|
return number_of_steps;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-05 16:34:22 +00:00
|
|
|
/**
|
|
|
|
* Ship arrives at a dock. If it is the first time, send out a news item.
|
|
|
|
* @param v Ship that arrived.
|
|
|
|
* @param st Station being visited.
|
|
|
|
*/
|
2009-01-10 00:31:47 +00:00
|
|
|
static void ShipArrivesAt(const Vehicle *v, Station *st)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
/* Check if station was ever visited before */
|
|
|
|
if (!(st->had_vehicle_of_type & HVOT_SHIP)) {
|
|
|
|
st->had_vehicle_of_type |= HVOT_SHIP;
|
2005-11-14 19:48:04 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, st->index);
|
2009-05-24 16:52:42 +00:00
|
|
|
AddVehicleNewsItem(
|
2009-04-21 23:40:56 +00:00
|
|
|
STR_NEWS_FIRST_SHIP_ARRIVAL,
|
2012-05-26 14:16:03 +00:00
|
|
|
(v->owner == _local_company) ? NT_ARRIVAL_COMPANY : NT_ARRIVAL_OTHER,
|
2004-08-09 17:04:08 +00:00
|
|
|
v->index,
|
2008-09-13 10:19:51 +00:00
|
|
|
st->index
|
|
|
|
);
|
2011-11-29 23:15:35 +00:00
|
|
|
AI::NewEvent(v->owner, new ScriptEventStationFirstVehicle(st->index, v->index));
|
2017-04-23 09:19:32 +00:00
|
|
|
Game::NewEvent(new ScriptEventStationFirstVehicle(st->index, v->index));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
2012-08-18 11:37:23 +00:00
|
|
|
* Runs the pathfinder to choose a track to continue along.
|
|
|
|
*
|
|
|
|
* @param v Ship to navigate
|
|
|
|
* @param tile Tile, the ship is about to enter
|
|
|
|
* @param enterdir Direction of entering
|
|
|
|
* @param tracks Available track choices on \a tile
|
|
|
|
* @return Track to choose, or INVALID_TRACK when to reverse.
|
2010-08-01 19:44:49 +00:00
|
|
|
*/
|
2010-12-13 21:56:40 +00:00
|
|
|
static Track ChooseShipTrack(Ship *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2022-10-22 11:34:54 +00:00
|
|
|
dbg_assert(IsValidDiagDirection(enterdir));
|
2005-01-31 11:23:10 +00:00
|
|
|
|
2010-12-13 21:56:40 +00:00
|
|
|
bool path_found = true;
|
|
|
|
Track track;
|
2018-05-18 08:05:24 +00:00
|
|
|
|
2019-02-25 13:07:22 +00:00
|
|
|
if (v->dest_tile == 0) {
|
|
|
|
/* No destination, don't invoke pathfinder. */
|
2018-05-23 16:34:39 +00:00
|
|
|
track = TrackBitsToTrack(v->state);
|
2019-01-15 21:37:32 +00:00
|
|
|
if (!IsDiagonalTrack(track)) track = TrackToOppositeTrack(track);
|
2018-05-18 21:16:15 +00:00
|
|
|
if (!HasBit(tracks, track)) track = FindFirstTrack(tracks);
|
2018-05-18 08:05:24 +00:00
|
|
|
path_found = false;
|
|
|
|
} else {
|
2019-01-14 23:33:42 +00:00
|
|
|
/* Attempt to follow cached path. */
|
2024-01-09 16:30:06 +00:00
|
|
|
if (!v->cached_path.empty()) {
|
|
|
|
track = TrackdirToTrack(v->cached_path.front());
|
2019-01-14 23:33:42 +00:00
|
|
|
|
|
|
|
if (HasBit(tracks, track)) {
|
2024-01-09 16:30:06 +00:00
|
|
|
v->cached_path.pop_front();
|
2019-01-14 23:33:42 +00:00
|
|
|
/* HandlePathfindResult() is not called here because this is not a new pathfinder result. */
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cached path is invalid so continue with pathfinder. */
|
2024-01-09 16:30:06 +00:00
|
|
|
v->cached_path.clear();
|
2019-01-14 23:33:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 08:05:24 +00:00
|
|
|
switch (_settings_game.pf.pathfinder_for_ships) {
|
2019-01-14 23:09:04 +00:00
|
|
|
case VPF_NPF: track = NPFShipChooseTrack(v, path_found); break;
|
2024-01-09 16:30:06 +00:00
|
|
|
case VPF_YAPF: track = YapfShipChooseTrack(v, tile, enterdir, tracks, path_found, v->cached_path); break;
|
2018-05-18 08:05:24 +00:00
|
|
|
default: NOT_REACHED();
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2021-01-18 00:58:43 +00:00
|
|
|
DEBUG_UPDATESTATECHECKSUM("ChooseShipTrack: v: %u, path_found: %d, track: %d", v->index, path_found, track);
|
2024-01-07 16:41:53 +00:00
|
|
|
UpdateStateChecksum((((uint64_t) v->index) << 32) | (path_found << 16) | track);
|
2010-12-13 21:56:40 +00:00
|
|
|
|
|
|
|
v->HandlePathfindingResult(path_found);
|
|
|
|
return track;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 18:07:06 +00:00
|
|
|
/**
|
|
|
|
* Get the available water tracks on a tile for a ship entering a tile.
|
|
|
|
* @param tile The tile about to enter.
|
|
|
|
* @param dir The entry direction.
|
|
|
|
* @return The available trackbits on the next tile.
|
|
|
|
*/
|
2020-01-06 16:16:10 +00:00
|
|
|
static inline TrackBits GetAvailShipTracks(TileIndex tile, DiagDirection dir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2019-02-14 18:07:06 +00:00
|
|
|
TrackBits tracks = GetTileShipTrackStatus(tile) & DiagdirReachesTracks(dir);
|
|
|
|
|
|
|
|
return tracks;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 10:50:30 +00:00
|
|
|
/** Structure for ship sub-coordinate data for moving into a new tile via a Diagdir onto a Track. */
|
|
|
|
struct ShipSubcoordData {
|
|
|
|
byte x_subcoord; ///< New X sub-coordinate on the new tile
|
|
|
|
byte y_subcoord; ///< New Y sub-coordinate on the new tile
|
|
|
|
Direction dir; ///< New Direction to move in on the new track
|
|
|
|
};
|
|
|
|
/** Ship sub-coordinate data for moving into a new tile via a Diagdir onto a Track.
|
|
|
|
* Array indexes are Diagdir, Track.
|
|
|
|
* There will always be three possible tracks going into an adjacent tile via a Diagdir,
|
|
|
|
* so each Diagdir sub-array will have three valid and three invalid structures per Track.
|
|
|
|
*/
|
|
|
|
static const ShipSubcoordData _ship_subcoord[DIAGDIR_END][TRACK_END] = {
|
|
|
|
// DIAGDIR_NE
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2022-05-06 10:50:30 +00:00
|
|
|
{15, 8, DIR_NE}, // TRACK_X
|
|
|
|
{ 0, 0, INVALID_DIR}, // TRACK_Y
|
|
|
|
{ 0, 0, INVALID_DIR}, // TRACK_UPPER
|
|
|
|
{15, 8, DIR_E}, // TRACK_LOWER
|
|
|
|
{15, 7, DIR_N}, // TRACK_LEFT
|
|
|
|
{ 0, 0, INVALID_DIR}, // TRACK_RIGHT
|
2004-08-09 17:04:08 +00:00
|
|
|
},
|
2022-05-06 10:50:30 +00:00
|
|
|
// DIAGDIR_SE
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2022-05-06 10:50:30 +00:00
|
|
|
{ 0, 0, INVALID_DIR}, // TRACK_X
|
|
|
|
{ 8, 0, DIR_SE}, // TRACK_Y
|
|
|
|
{ 7, 0, DIR_E}, // TRACK_UPPER
|
|
|
|
{ 0, 0, INVALID_DIR}, // TRACK_LOWER
|
|
|
|
{ 8, 0, DIR_S}, // TRACK_LEFT
|
|
|
|
{ 0, 0, INVALID_DIR}, // TRACK_RIGHT
|
2004-08-09 17:04:08 +00:00
|
|
|
},
|
2022-05-06 10:50:30 +00:00
|
|
|
// DIAGDIR_SW
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2022-05-06 10:50:30 +00:00
|
|
|
{ 0, 8, DIR_SW}, // TRACK_X
|
|
|
|
{ 0, 0, INVALID_DIR}, // TRACK_Y
|
|
|
|
{ 0, 7, DIR_W}, // TRACK_UPPER
|
|
|
|
{ 0, 0, INVALID_DIR}, // TRACK_LOWER
|
|
|
|
{ 0, 0, INVALID_DIR}, // TRACK_LEFT
|
|
|
|
{ 0, 8, DIR_S}, // TRACK_RIGHT
|
2004-08-09 17:04:08 +00:00
|
|
|
},
|
2022-05-06 10:50:30 +00:00
|
|
|
// DIAGDIR_NW
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2022-05-06 10:50:30 +00:00
|
|
|
{ 0, 0, INVALID_DIR}, // TRACK_X
|
|
|
|
{ 8, 15, DIR_NW}, // TRACK_Y
|
|
|
|
{ 0, 0, INVALID_DIR}, // TRACK_UPPER
|
|
|
|
{ 8, 15, DIR_W}, // TRACK_LOWER
|
|
|
|
{ 0, 0, INVALID_DIR}, // TRACK_LEFT
|
|
|
|
{ 7, 15, DIR_N}, // TRACK_RIGHT
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-25 18:27:07 +00:00
|
|
|
/** Temporary data storage for testing collisions. */
|
2018-01-22 23:49:23 +00:00
|
|
|
struct ShipCollideChecker {
|
|
|
|
|
2018-02-04 12:47:57 +00:00
|
|
|
TrackBits track_bits; ///< Pathfinder chosen track converted to trackbits, or is v->state of requesting ship. (one bit set)
|
|
|
|
TileIndex search_tile; ///< The tile that we really want to check.
|
|
|
|
Ship *v; ///< Ship we are testing for collision.
|
2016-01-19 22:01:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for collision avoidance. */
|
|
|
|
static Vehicle *FindShipOnTile(Vehicle *v, void *data)
|
|
|
|
{
|
2018-01-22 23:49:23 +00:00
|
|
|
ShipCollideChecker *scc = (ShipCollideChecker*)data;
|
|
|
|
|
|
|
|
/* Don't detect vehicles on different parallel tracks. */
|
|
|
|
TrackBits bits = scc->track_bits | Ship::From(v)->state;
|
2019-04-11 17:14:13 +00:00
|
|
|
if (bits == TRACK_BIT_HORZ || bits == TRACK_BIT_VERT) return nullptr;
|
2018-01-22 23:49:23 +00:00
|
|
|
|
2018-01-25 18:31:11 +00:00
|
|
|
/* Don't detect ships passing on aquaduct. */
|
2019-04-11 17:14:13 +00:00
|
|
|
if (abs(v->z_pos - scc->v->z_pos) >= 8) return nullptr;
|
2018-01-25 18:31:11 +00:00
|
|
|
|
2018-02-04 12:47:57 +00:00
|
|
|
/* Only requested tiles are checked. avoid desync. */
|
2019-04-11 17:14:13 +00:00
|
|
|
if (TileVirtXY(v->x_pos, v->y_pos) != scc->search_tile) return nullptr;
|
2018-01-25 18:27:07 +00:00
|
|
|
|
2016-01-19 22:01:12 +00:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2018-02-04 12:48:50 +00:00
|
|
|
/**
|
|
|
|
* Adjust speed while on aqueducts.
|
|
|
|
* @param search_tile Tile that the requesting ship will check, one will be added to look in front of the bow.
|
|
|
|
* @param ramp Ramp tile from aqueduct.
|
|
|
|
* @param v Ship that does the request.
|
|
|
|
* @return Allways false.
|
|
|
|
*/
|
|
|
|
static bool HandleSpeedOnAqueduct(Ship *v, TileIndex tile, TileIndex ramp)
|
|
|
|
{
|
2018-02-04 12:49:27 +00:00
|
|
|
TileIndexDiffC ti = TileIndexDiffCByDir(v->direction);
|
|
|
|
|
2018-02-04 12:48:50 +00:00
|
|
|
ShipCollideChecker scc;
|
|
|
|
scc.v = v;
|
|
|
|
scc.track_bits = TRACK_BIT_NONE;
|
2018-02-04 12:49:27 +00:00
|
|
|
scc.search_tile = TileAddWrap(tile, ti.x, ti.y);
|
|
|
|
|
|
|
|
if (scc.search_tile == INVALID_TILE) return false;
|
2018-02-04 12:48:50 +00:00
|
|
|
|
|
|
|
if (IsValidTile(scc.search_tile) &&
|
2020-02-28 22:21:10 +00:00
|
|
|
(HasVehicleOnPos(ramp, VEH_SHIP, &scc, FindShipOnTile) ||
|
|
|
|
HasVehicleOnPos(GetOtherTunnelBridgeEnd(ramp), VEH_SHIP, &scc, FindShipOnTile))) {
|
2021-08-29 21:04:06 +00:00
|
|
|
UpdateShipSpeed(v, v->cur_speed / 4);
|
2018-02-04 12:48:50 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-22 23:49:23 +00:00
|
|
|
/**
|
|
|
|
* If there is imminent collision or worse, direction and speed will be adjusted.
|
2018-01-25 18:27:07 +00:00
|
|
|
* @param tile Tile that the ship is about to enter.
|
|
|
|
* @param v Ship that does the request.
|
|
|
|
* @param tracks The available tracks that could be followed.
|
|
|
|
* @param track_old The track that the pathfinder assigned.
|
|
|
|
* @param diagdir The DiagDirection that tile will be entered.
|
2018-01-22 23:49:23 +00:00
|
|
|
* @return The new track if found.
|
|
|
|
*/
|
|
|
|
static void CheckDistanceBetweenShips(TileIndex tile, Ship *v, TrackBits tracks, Track *track_old, DiagDirection diagdir)
|
|
|
|
{
|
2019-02-22 22:08:30 +00:00
|
|
|
// No checking close to docks and depots.
|
|
|
|
if (v->current_order.IsType(OT_GOTO_STATION)) {
|
|
|
|
Station *st = Station::Get(v->current_order.GetDestination());
|
|
|
|
if (st->IsWithinRangeOfDockingTile(tile, 3)) return;
|
|
|
|
} else if (!v->current_order.IsType(OT_GOTO_WAYPOINT)) {
|
|
|
|
if (DistanceManhattan(v->dest_tile, tile) <= 3) return;
|
|
|
|
}
|
2018-01-22 23:49:23 +00:00
|
|
|
|
|
|
|
Track track = *track_old;
|
2018-01-25 18:27:07 +00:00
|
|
|
TrackBits track_bits = TrackToTrackBits(track);
|
|
|
|
|
|
|
|
/* Only check for collision when pathfinder did not change direction.
|
|
|
|
* This is done in order to keep ships moving towards the intended target. */
|
|
|
|
TrackBits combine = (v->state | track_bits);
|
2018-02-04 12:47:57 +00:00
|
|
|
if (combine != TRACK_BIT_HORZ && combine != TRACK_BIT_VERT && combine != track_bits) return;
|
2018-01-22 23:49:23 +00:00
|
|
|
|
2018-02-04 12:49:27 +00:00
|
|
|
TileIndexDiffC ti;
|
2018-01-22 23:49:23 +00:00
|
|
|
ShipCollideChecker scc;
|
2018-02-04 12:47:30 +00:00
|
|
|
scc.v = v;
|
2018-01-25 18:27:07 +00:00
|
|
|
scc.track_bits = track_bits;
|
2018-02-04 12:47:57 +00:00
|
|
|
scc.search_tile = tile;
|
|
|
|
|
2020-02-28 22:21:10 +00:00
|
|
|
bool found = HasVehicleOnPos(tile, VEH_SHIP, &scc, FindShipOnTile);
|
2018-01-22 23:49:23 +00:00
|
|
|
|
2018-02-04 12:47:57 +00:00
|
|
|
if (!found) {
|
2018-02-04 12:48:50 +00:00
|
|
|
/* Bridge entrance */
|
|
|
|
if (IsBridgeTile(tile) && HandleSpeedOnAqueduct(v, tile, tile)) return;
|
|
|
|
|
2021-02-27 01:50:57 +00:00
|
|
|
scc.track_bits = TrackToTrackBits(IsDiagonalTrack(track) ? track : TrackToOppositeTrack(track));
|
2018-02-04 12:49:27 +00:00
|
|
|
ti = TileIndexDiffCByDiagDir(_ship_search_directions[track][diagdir]);
|
|
|
|
scc.search_tile = TileAddWrap(tile, ti.x, ti.y);
|
|
|
|
if (scc.search_tile == INVALID_TILE) return;
|
2018-02-04 12:47:57 +00:00
|
|
|
|
2020-02-28 22:21:10 +00:00
|
|
|
found = HasVehicleOnPos(scc.search_tile, VEH_SHIP, &scc, FindShipOnTile);
|
2018-02-04 12:47:57 +00:00
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
scc.track_bits = track_bits;
|
2018-02-04 12:49:27 +00:00
|
|
|
ti = TileIndexDiffCByDiagDir(diagdir);
|
|
|
|
scc.search_tile = TileAddWrap(scc.search_tile, ti.x, ti.y);
|
|
|
|
if (scc.search_tile == INVALID_TILE) return;
|
2018-02-04 12:47:57 +00:00
|
|
|
|
2020-02-28 22:21:10 +00:00
|
|
|
found = HasVehicleOnPos(scc.search_tile, VEH_SHIP, &scc, FindShipOnTile);
|
2018-02-04 12:47:57 +00:00
|
|
|
}
|
2018-01-22 23:49:23 +00:00
|
|
|
if (found) {
|
|
|
|
|
|
|
|
/* Speed adjustment related to distance. */
|
2021-08-29 21:04:06 +00:00
|
|
|
UpdateShipSpeed(v, v->cur_speed / (scc.search_tile == tile ? 8 : 2));
|
2018-01-25 18:27:07 +00:00
|
|
|
|
2018-01-25 18:31:11 +00:00
|
|
|
/* Clean none wanted trackbits, including pathfinder track, TRACK_BIT_WORMHOLE and no 90 degree turns. */
|
2021-02-27 01:50:57 +00:00
|
|
|
if (IsDiagonalTrack(track)) {
|
|
|
|
ClrBit(tracks, track);
|
|
|
|
} else {
|
|
|
|
tracks &= TRACK_BIT_CROSS;
|
|
|
|
}
|
2018-01-25 18:27:07 +00:00
|
|
|
|
|
|
|
/* Just follow track 1 tile and see if there is a track to follow. (try not to bang in coast or ship) */
|
|
|
|
while (tracks != TRACK_BIT_NONE) {
|
|
|
|
track = RemoveFirstTrack(&tracks);
|
|
|
|
|
2018-02-04 12:49:27 +00:00
|
|
|
ti = TileIndexDiffCByDiagDir(_ship_search_directions[track][diagdir]);
|
|
|
|
TileIndex tile_check = TileAddWrap(tile, ti.x, ti.y);
|
|
|
|
if (tile_check == INVALID_TILE) continue;
|
2018-01-25 18:27:07 +00:00
|
|
|
|
2021-02-27 01:50:57 +00:00
|
|
|
scc.search_tile = tile_check;
|
|
|
|
scc.track_bits = TrackToTrackBits(IsDiagonalTrack(track) ? track : TrackToOppositeTrack(track));
|
|
|
|
if (HasVehicleOnPos(scc.search_tile, VEH_SHIP, &scc, FindShipOnTile)) continue;
|
2018-01-25 18:27:07 +00:00
|
|
|
|
2019-02-19 19:36:30 +00:00
|
|
|
TrackBits bits = GetTileShipTrackStatus(tile_check) & DiagdirReachesTracks(_ship_search_directions[track][diagdir]);
|
2018-01-25 18:27:07 +00:00
|
|
|
if (!IsDiagonalTrack(track)) bits &= TRACK_BIT_CROSS; // No 90 degree turns.
|
|
|
|
|
|
|
|
if (bits != INVALID_TRACK_BIT && bits != TRACK_BIT_NONE) {
|
|
|
|
*track_old = track;
|
2024-01-09 19:57:30 +00:00
|
|
|
v->cached_path.clear();
|
2018-01-25 18:27:07 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-01-22 23:49:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-19 20:50:03 +00:00
|
|
|
/**
|
|
|
|
* Test if a ship is in the centre of a lock and should move up or down.
|
|
|
|
* @param v Ship being tested.
|
|
|
|
* @return 0 if ship is not moving in lock, or -1 to move down, 1 to move up.
|
|
|
|
*/
|
|
|
|
static int ShipTestUpDownOnLock(const Ship *v)
|
|
|
|
{
|
|
|
|
/* Suitable tile? */
|
|
|
|
if (!IsTileType(v->tile, MP_WATER) || !IsLock(v->tile) || GetLockPart(v->tile) != LOCK_PART_MIDDLE) return 0;
|
|
|
|
|
|
|
|
/* Must be at the centre of the lock */
|
|
|
|
if ((v->x_pos & 0xF) != 8 || (v->y_pos & 0xF) != 8) return 0;
|
|
|
|
|
|
|
|
DiagDirection diagdir = GetInclinedSlopeDirection(GetTileSlope(v->tile));
|
2022-10-22 11:34:54 +00:00
|
|
|
dbg_assert(IsValidDiagDirection(diagdir));
|
2018-05-19 20:50:03 +00:00
|
|
|
|
|
|
|
if (DirToDiagDir(v->direction) == diagdir) {
|
|
|
|
/* Move up */
|
|
|
|
return (v->z_pos < GetTileMaxZ(v->tile) * (int)TILE_HEIGHT) ? 1 : 0;
|
|
|
|
} else {
|
|
|
|
/* Move down */
|
|
|
|
return (v->z_pos > GetTileZ(v->tile) * (int)TILE_HEIGHT) ? -1 : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test and move a ship up or down in a lock.
|
|
|
|
* @param v Ship to move.
|
|
|
|
* @return true iff ship is moving up or down in a lock.
|
|
|
|
*/
|
|
|
|
static bool ShipMoveUpDownOnLock(Ship *v)
|
|
|
|
{
|
|
|
|
/* Moving up/down through lock */
|
|
|
|
int dz = ShipTestUpDownOnLock(v);
|
|
|
|
if (dz == 0) return false;
|
|
|
|
|
2021-08-29 21:04:06 +00:00
|
|
|
UpdateShipSpeed(v, 0);
|
2018-05-19 20:50:03 +00:00
|
|
|
|
|
|
|
if ((v->tick_counter & 7) == 0) {
|
|
|
|
v->z_pos += dz;
|
|
|
|
v->UpdatePosition();
|
|
|
|
v->UpdateViewport(true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-11 10:37:47 +00:00
|
|
|
/**
|
|
|
|
* Test if a tile is a docking tile for the given station.
|
|
|
|
* @param tile Docking tile to test.
|
|
|
|
* @param station Destination station.
|
|
|
|
* @return true iff docking tile is next to station.
|
|
|
|
*/
|
|
|
|
bool IsShipDestinationTile(TileIndex tile, StationID station)
|
|
|
|
{
|
2022-10-22 11:34:54 +00:00
|
|
|
dbg_assert(IsDockingTile(tile));
|
2019-03-11 10:37:47 +00:00
|
|
|
/* Check each tile adjacent to docking tile. */
|
|
|
|
for (DiagDirection d = DIAGDIR_BEGIN; d != DIAGDIR_END; d++) {
|
|
|
|
TileIndex t = tile + TileOffsByDiagDir(d);
|
|
|
|
if (!IsValidTile(t)) continue;
|
2023-09-16 21:51:49 +00:00
|
|
|
if (IsDockTile(t) && GetStationIndex(t) == station && IsDockWaterPart(t)) return true;
|
2019-03-11 10:37:47 +00:00
|
|
|
if (IsTileType(t, MP_INDUSTRY)) {
|
|
|
|
const Industry *i = Industry::GetByTile(t);
|
|
|
|
if (i->neutral_station != nullptr && i->neutral_station->index == station) return true;
|
|
|
|
}
|
2020-02-24 22:49:11 +00:00
|
|
|
if (IsTileType(t, MP_STATION) && IsOilRig(t) && GetStationIndex(t) == station) return true;
|
2019-03-11 10:37:47 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
static void ReverseShipIntoTrackdir(Ship *v, Trackdir trackdir)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2023-04-29 08:33:01 +00:00
|
|
|
static constexpr Direction _trackdir_to_direction[] = {
|
|
|
|
DIR_NE, DIR_SE, DIR_E, DIR_E, DIR_S, DIR_S, INVALID_DIR, INVALID_DIR,
|
|
|
|
DIR_SW, DIR_NW, DIR_W, DIR_W, DIR_N, DIR_N, INVALID_DIR, INVALID_DIR,
|
|
|
|
};
|
|
|
|
|
|
|
|
v->direction = _trackdir_to_direction[trackdir];
|
2023-05-29 23:49:14 +00:00
|
|
|
dbg_assert(v->direction != INVALID_DIR);
|
2023-04-29 08:33:01 +00:00
|
|
|
v->state = TrackdirBitsToTrackBits(TrackdirToTrackdirBits(trackdir));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
/* Remember our current location to avoid movement glitch */
|
|
|
|
v->rotation_x_pos = v->x_pos;
|
|
|
|
v->rotation_y_pos = v->y_pos;
|
2023-05-29 23:49:14 +00:00
|
|
|
UpdateShipSpeed(v, 0);
|
2024-01-09 16:30:06 +00:00
|
|
|
v->cached_path.clear();
|
2023-04-29 08:33:01 +00:00
|
|
|
|
|
|
|
v->UpdatePosition();
|
|
|
|
v->UpdateViewport(true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ReverseShip(Ship *v)
|
|
|
|
{
|
|
|
|
v->direction = ReverseDir(v->direction);
|
|
|
|
|
|
|
|
/* Remember our current location to avoid movement glitch */
|
|
|
|
v->rotation_x_pos = v->x_pos;
|
|
|
|
v->rotation_y_pos = v->y_pos;
|
2023-05-29 23:49:14 +00:00
|
|
|
UpdateShipSpeed(v, 0);
|
2024-01-09 16:30:06 +00:00
|
|
|
v->cached_path.clear();
|
2023-04-29 08:33:01 +00:00
|
|
|
|
|
|
|
v->UpdatePosition();
|
|
|
|
v->UpdateViewport(true, true);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
static void ShipController(Ship *v)
|
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
v->tick_counter++;
|
2007-06-20 19:17:22 +00:00
|
|
|
v->current_order_time++;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-08-28 14:14:37 +00:00
|
|
|
if (v->HandleBreakdown()) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
if (ProcessOrders(v) && CheckReverseShip(v)) return ReverseShip(v);
|
2021-10-05 15:58:19 +00:00
|
|
|
|
2007-05-07 16:21:34 +00:00
|
|
|
v->HandleLoading();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-05 23:36:54 +00:00
|
|
|
if (v->current_order.IsType(OT_LOADING)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-04-10 10:47:21 +00:00
|
|
|
if (CheckShipLeaveDepot(v)) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2022-06-01 19:05:45 +00:00
|
|
|
v->ShowVisualEffect(UINT_MAX);
|
2010-11-18 14:17:55 +00:00
|
|
|
|
2018-05-20 10:03:14 +00:00
|
|
|
/* Rotating on spot */
|
|
|
|
if (v->direction != v->rotation) {
|
|
|
|
if ((v->tick_counter & 7) == 0) {
|
|
|
|
DirDiff diff = DirDifference(v->direction, v->rotation);
|
|
|
|
v->rotation = ChangeDir(v->rotation, diff > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
|
|
|
|
v->UpdateViewport(true, true);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-19 20:50:03 +00:00
|
|
|
if (ShipMoveUpDownOnLock(v)) return;
|
|
|
|
|
2023-05-29 23:49:14 +00:00
|
|
|
uint number_of_steps = ShipAccelerate(v);
|
|
|
|
if (number_of_steps == 0 && v->current_order.IsType(OT_LEAVESTATION)) number_of_steps = 1;
|
2023-04-29 08:33:01 +00:00
|
|
|
for (uint i = 0; i < number_of_steps; ++i) {
|
2023-04-28 19:23:59 +00:00
|
|
|
if (ShipMoveUpDownOnLock(v)) return;
|
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
GetNewVehiclePosResult gp = GetNewVehiclePos(v);
|
|
|
|
if (v->state != TRACK_BIT_WORMHOLE) {
|
|
|
|
/* Not on a bridge */
|
|
|
|
if (gp.old_tile == gp.new_tile) {
|
|
|
|
/* Staying in tile */
|
|
|
|
if (v->IsInDepot()) {
|
|
|
|
gp.x = v->x_pos;
|
|
|
|
gp.y = v->y_pos;
|
|
|
|
} else {
|
|
|
|
/* Not inside depot */
|
|
|
|
const VehicleEnterTileStatus r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
|
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) return ReverseShip(v);
|
|
|
|
|
|
|
|
/* A leave station order only needs one tick to get processed, so we can
|
|
|
|
* always skip ahead. */
|
|
|
|
if (v->current_order.IsType(OT_LEAVESTATION)) {
|
2023-05-29 23:49:14 +00:00
|
|
|
StationID station_id = v->current_order.GetDestination();
|
2023-04-29 08:33:01 +00:00
|
|
|
v->current_order.Free();
|
2023-05-29 23:49:14 +00:00
|
|
|
|
|
|
|
bool may_reverse = ProcessOrders(v);
|
|
|
|
|
|
|
|
if (v->current_order.IsType(OT_GOTO_STATION) && v->current_order.GetDestination() == station_id &&
|
|
|
|
IsDockingTile(gp.new_tile) && Company::Get(v->owner)->settings.remain_if_next_order_same_station) {
|
|
|
|
Station *st = Station::Get(station_id);
|
|
|
|
if (st->facilities & FACIL_DOCK && st->docking_station.Contains(gp.new_tile) && IsShipDestinationTile(gp.new_tile, station_id)) {
|
|
|
|
v->last_station_visited = station_id;
|
2018-02-05 18:21:05 +00:00
|
|
|
ShipArrivesAt(v, st);
|
|
|
|
v->BeginLoading();
|
2023-05-29 23:49:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v->PlayLeaveStationSound();
|
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
SetWindowWidgetDirty(WC_VEHICLE_VIEW, v->index, WID_VV_START_STOP);
|
2023-05-29 23:49:14 +00:00
|
|
|
if (may_reverse && CheckReverseShip(v)) return ReverseShip(v);
|
2023-04-29 08:33:01 +00:00
|
|
|
/* Test if continuing forward would lead to a dead-end, moving into the dock. */
|
|
|
|
const DiagDirection exitdir = VehicleExitDir(v->direction, v->state);
|
|
|
|
const TileIndex tile = TileAddByDiagDir(v->tile, exitdir);
|
2023-05-29 23:49:14 +00:00
|
|
|
if (TrackdirBitsToTrackBits(GetTileTrackdirBits(tile, TRANSPORT_WATER, 0, exitdir)) == TRACK_BIT_NONE) return ReverseShip(v);
|
2023-04-29 08:33:01 +00:00
|
|
|
} else if (v->dest_tile != 0) {
|
|
|
|
/* We have a target, let's see if we reached it... */
|
|
|
|
if (v->current_order.IsType(OT_GOTO_WAYPOINT) &&
|
|
|
|
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 */
|
|
|
|
UpdateVehicleTimetable(v, true);
|
|
|
|
v->IncrementRealOrderIndex();
|
|
|
|
v->current_order.MakeDummy();
|
|
|
|
} else if (v->current_order.IsType(OT_GOTO_DEPOT) &&
|
2023-05-29 23:49:14 +00:00
|
|
|
v->dest_tile == gp.new_tile) {
|
2023-04-29 08:33:01 +00:00
|
|
|
/* Depot orders really need to reach the tile */
|
|
|
|
if ((gp.x & 0xF) == 8 && (gp.y & 0xF) == 8) {
|
|
|
|
VehicleEnterDepot(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (v->current_order.IsType(OT_GOTO_STATION) && IsDockingTile(gp.new_tile)) {
|
|
|
|
/* Process station in the orderlist. */
|
|
|
|
Station *st = Station::Get(v->current_order.GetDestination());
|
|
|
|
if (st->docking_station.Contains(gp.new_tile) && IsShipDestinationTile(gp.new_tile, st->index)) {
|
|
|
|
v->last_station_visited = st->index;
|
|
|
|
if (st->facilities & FACIL_DOCK) { // ugly, ugly workaround for problem with ships able to drop off cargo at wrong stations
|
|
|
|
ShipArrivesAt(v, st);
|
|
|
|
v->BeginLoading();
|
|
|
|
} else { // leave stations without docks right away
|
|
|
|
v->current_order.MakeLeaveStation();
|
|
|
|
v->IncrementRealOrderIndex();
|
|
|
|
}
|
2005-05-02 22:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-29 08:33:01 +00:00
|
|
|
} else {
|
|
|
|
/* New tile */
|
|
|
|
if (!IsValidTile(gp.new_tile)) return ReverseShip(v);
|
|
|
|
|
|
|
|
const DiagDirection diagdir = DiagdirBetweenTiles(gp.old_tile, gp.new_tile);
|
2023-05-29 23:49:14 +00:00
|
|
|
dbg_assert(diagdir != INVALID_DIAGDIR);
|
2023-04-29 08:33:01 +00:00
|
|
|
const TrackBits tracks = GetAvailShipTracks(gp.new_tile, diagdir);
|
|
|
|
if (tracks == TRACK_BIT_NONE) {
|
|
|
|
Trackdir trackdir = INVALID_TRACKDIR;
|
|
|
|
CheckReverseShip(v, &trackdir);
|
|
|
|
if (trackdir == INVALID_TRACKDIR) return ReverseShip(v);
|
|
|
|
return ReverseShipIntoTrackdir(v, trackdir);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
/* Choose a direction, and continue if we find one */
|
2023-05-29 23:49:14 +00:00
|
|
|
Track track = ChooseShipTrack(v, gp.new_tile, diagdir, tracks);
|
2023-04-29 08:33:01 +00:00
|
|
|
if (track == INVALID_TRACK) return ReverseShip(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-05-29 23:49:14 +00:00
|
|
|
/* Try to avoid collision and keep distance between ships. */
|
|
|
|
if (_settings_game.vehicle.ship_collision_avoidance) CheckDistanceBetweenShips(gp.new_tile, v, tracks, &track, diagdir);
|
2016-01-19 22:01:12 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
const ShipSubcoordData &b = _ship_subcoord[diagdir][track];
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
gp.x = (gp.x & ~0xF) | b.x_subcoord;
|
|
|
|
gp.y = (gp.y & ~0xF) | b.y_subcoord;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
/* Call the landscape function and tell it that the vehicle entered the tile */
|
|
|
|
const VehicleEnterTileStatus r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
|
|
|
|
if (HasBit(r, VETS_CANNOT_ENTER)) return ReverseShip(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
if (!HasBit(r, VETS_ENTERED_WORMHOLE)) {
|
|
|
|
v->tile = gp.new_tile;
|
|
|
|
v->state = TrackToTrackBits(track);
|
2011-07-07 14:16:22 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
/* Update ship cache when the water class changes. Aqueducts are always canals. */
|
|
|
|
if (GetEffectiveWaterClass(gp.old_tile) != GetEffectiveWaterClass(gp.new_tile)) v->UpdateCache();
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
const Direction new_direction = b.dir;
|
|
|
|
const DirDiff diff = DirDifference(new_direction, v->direction);
|
|
|
|
switch (diff) {
|
|
|
|
case DIRDIFF_SAME:
|
|
|
|
case DIRDIFF_45RIGHT:
|
|
|
|
case DIRDIFF_45LEFT:
|
|
|
|
/* Continue at speed */
|
|
|
|
v->rotation = v->direction = new_direction;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Stop for rotation */
|
2023-05-29 23:49:14 +00:00
|
|
|
UpdateShipSpeed(v, 0);
|
2023-04-29 08:33:01 +00:00
|
|
|
v->direction = new_direction;
|
|
|
|
/* Remember our current location to avoid movement glitch */
|
|
|
|
v->rotation_x_pos = v->x_pos;
|
|
|
|
v->rotation_y_pos = v->y_pos;
|
|
|
|
break;
|
|
|
|
}
|
2018-05-20 10:03:14 +00:00
|
|
|
}
|
2023-04-29 08:33:01 +00:00
|
|
|
} else {
|
|
|
|
/* On a bridge */
|
|
|
|
if (!IsTileType(gp.new_tile, MP_TUNNELBRIDGE) || !HasBit(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
|
2023-05-29 23:49:14 +00:00
|
|
|
if (_settings_game.vehicle.ship_collision_avoidance && gp.new_tile != TileVirtXY(v->x_pos, v->y_pos)) HandleSpeedOnAqueduct(v, gp.new_tile, v->tile);
|
2023-04-29 08:33:01 +00:00
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
|
|
|
v->UpdatePosition();
|
2024-03-01 21:16:31 +00:00
|
|
|
if ((v->vehstatus & VS_HIDDEN) == 0) v->UpdateViewport(true, false);
|
2023-04-29 08:33:01 +00:00
|
|
|
return;
|
2018-05-20 10:03:14 +00:00
|
|
|
}
|
2023-05-29 23:49:14 +00:00
|
|
|
/* Bridge exit */
|
2018-02-04 12:48:50 +00:00
|
|
|
if (_settings_game.vehicle.ship_collision_avoidance && gp.new_tile != TileVirtXY(v->x_pos, v->y_pos)) HandleSpeedOnAqueduct(v, gp.new_tile, v->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
/* Ship is back on the bridge head, we need to consume its path
|
|
|
|
* cache entry here as we didn't have to choose a ship track. */
|
2024-01-09 16:30:06 +00:00
|
|
|
if (!v->cached_path.empty()) v->cached_path.pop_front();
|
2023-04-29 08:33:01 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
/* update image of ship, as well as delta XY */
|
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-04-29 08:33:01 +00:00
|
|
|
v->UpdatePosition();
|
|
|
|
v->UpdateViewport(true, true);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-22 13:53:14 +00:00
|
|
|
bool Ship::Tick()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2021-01-18 00:58:43 +00:00
|
|
|
DEBUG_UPDATESTATECHECKSUM("Ship::Tick: v: %u, x: %d, y: %d", this->index, this->x_pos, this->y_pos);
|
2024-01-07 16:41:53 +00:00
|
|
|
UpdateStateChecksum((((uint64_t) this->x_pos) << 32) | this->y_pos);
|
2020-11-05 22:58:16 +00:00
|
|
|
if (!((this->vehstatus & VS_STOPPED) || this->IsWaitingInDepot())) this->running_ticks++;
|
2008-02-13 19:24:40 +00:00
|
|
|
|
2007-07-01 19:24:54 +00:00
|
|
|
ShipController(this);
|
2009-05-22 13:53:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-14 23:33:42 +00:00
|
|
|
void Ship::SetDestTile(TileIndex tile)
|
|
|
|
{
|
|
|
|
if (tile == this->dest_tile) return;
|
2024-01-09 16:30:06 +00:00
|
|
|
this->cached_path.clear();
|
2019-01-14 23:33:42 +00:00
|
|
|
this->dest_tile = tile;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Build a ship.
|
2010-08-17 23:15:55 +00:00
|
|
|
* @param tile tile of the depot where ship is built.
|
|
|
|
* @param flags type of operation.
|
|
|
|
* @param e the engine to build.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param[out] ret the vehicle that has been built.
|
2010-08-17 23:15:55 +00:00
|
|
|
* @return the cost of this operation or an error.
|
2005-05-12 00:11:37 +00:00
|
|
|
*/
|
2023-06-04 14:57:36 +00:00
|
|
|
CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, const Engine *e, Vehicle **ret)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2010-08-19 13:44:41 +00:00
|
|
|
tile = GetShipDepotNorthTile(tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2006-04-10 07:15:58 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
2009-10-06 19:17:07 +00:00
|
|
|
const ShipVehicleInfo *svi = &e->u.ship;
|
2004-12-03 21:57:05 +00:00
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
Ship *v = new Ship();
|
2010-08-17 23:15:55 +00:00
|
|
|
*ret = v;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-09-30 20:39:50 +00:00
|
|
|
v->owner = _current_company;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->tile = tile;
|
2006-04-23 19:35:36 +00:00
|
|
|
x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
|
|
|
|
y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->x_pos = x;
|
|
|
|
v->y_pos = y;
|
2011-11-04 10:18:13 +00:00
|
|
|
v->z_pos = GetSlopePixelZ(x, y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2018-05-22 17:43:34 +00:00
|
|
|
v->UpdateDeltaXY();
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-03 21:57:05 +00:00
|
|
|
v->spritenum = svi->image_index;
|
2009-02-21 12:52:41 +00:00
|
|
|
v->cargo_type = e->GetDefaultCargoType();
|
2024-02-04 10:16:08 +00:00
|
|
|
assert(IsValidCargoID(v->cargo_type));
|
2004-12-03 21:57:05 +00:00
|
|
|
v->cargo_cap = svi->capacity;
|
2013-05-19 14:22:04 +00:00
|
|
|
v->refit_cap = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-02-02 16:16:43 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2013-05-19 14:22:04 +00:00
|
|
|
v->last_loading_station = INVALID_STATION;
|
2010-08-17 23:15:55 +00:00
|
|
|
v->engine_type = e->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->reliability = e->reliability;
|
|
|
|
v->reliability_spd_dec = e->reliability_spd_dec;
|
2016-01-31 22:55:25 +00:00
|
|
|
v->breakdown_chance_factor = 64; // ships have a 50% lower breakdown chance than normal
|
2009-06-16 13:52:18 +00:00
|
|
|
v->max_age = e->GetLifeLengthInDays();
|
2005-10-29 21:54:28 +00:00
|
|
|
_new_vehicle_id = v->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-05-22 18:17:20 +00:00
|
|
|
v->state = TRACK_BIT_DEPOT;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2013-02-14 17:04:01 +00:00
|
|
|
v->SetServiceInterval(Company::Get(_current_company)->settings.vehicle.servint_ships);
|
2024-02-13 21:34:09 +00:00
|
|
|
v->date_of_last_service = EconTime::CurDate();
|
|
|
|
v->date_of_last_service_newgrf = CalTime::CurDate();
|
|
|
|
v->build_year = CalTime::CurYear();
|
2016-10-16 14:57:56 +00:00
|
|
|
v->sprite_seq.Set(SPR_IMG_QUERY);
|
2023-04-23 20:22:17 +00:00
|
|
|
v->random_bits = Random();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2023-04-28 19:23:59 +00:00
|
|
|
v->acceleration = svi->acceleration;
|
2010-11-06 13:03:17 +00:00
|
|
|
v->UpdateCache();
|
|
|
|
|
2007-11-20 13:35:54 +00:00
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) SetBit(v->vehicle_flags, VF_BUILT_AS_PROTOTYPE);
|
2013-02-14 17:24:55 +00:00
|
|
|
v->SetServiceIntervalIsPercent(Company::Get(_current_company)->settings.vehicle.servint_ispercent);
|
2016-05-06 22:33:12 +00:00
|
|
|
SB(v->vehicle_flags, VF_AUTOMATE_TIMETABLE, 1, Company::Get(_current_company)->settings.vehicle.auto_timetable_by_default);
|
2016-05-07 00:15:46 +00:00
|
|
|
SB(v->vehicle_flags, VF_TIMETABLE_SEPARATION, 1, Company::Get(_current_company)->settings.vehicle.auto_separation_by_default);
|
2007-02-28 17:59:05 +00:00
|
|
|
|
2009-05-31 12:03:14 +00:00
|
|
|
v->InvalidateNewGRFCacheOfChain();
|
|
|
|
|
2011-11-09 16:38:50 +00:00
|
|
|
v->cargo_cap = e->DetermineCapacity(v);
|
2007-05-12 07:05:34 +00:00
|
|
|
|
2023-03-14 00:16:31 +00:00
|
|
|
AddArticulatedParts(v);
|
2009-05-31 12:03:14 +00:00
|
|
|
v->InvalidateNewGRFCacheOfChain();
|
|
|
|
|
2014-09-20 15:31:26 +00:00
|
|
|
v->UpdatePosition();
|
2019-01-25 20:17:15 +00:00
|
|
|
InvalidateVehicleTickCaches();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2010-08-17 23:15:55 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 21:33:09 +00:00
|
|
|
ClosestDepot Ship::FindClosestDepot()
|
2008-04-11 08:14:43 +00:00
|
|
|
{
|
2024-01-27 14:06:14 +00:00
|
|
|
const Depot *depot = FindClosestShipDepot(this, MAX_SHIP_DEPOT_SEARCH_DISTANCE);
|
2023-01-03 21:33:09 +00:00
|
|
|
if (depot == nullptr) return ClosestDepot();
|
2008-04-11 08:14:43 +00:00
|
|
|
|
2023-01-03 21:33:09 +00:00
|
|
|
return ClosestDepot(depot->xy, depot->index);
|
2008-04-11 08:14:43 +00:00
|
|
|
}
|