From 18d054150a26f49f2ffe7b8cdee6ba24bfc74a8a Mon Sep 17 00:00:00 2001 From: merni-ns Date: Sat, 2 Mar 2024 18:40:00 +0530 Subject: [PATCH] Remove: NPF and pathfinder change settings --- src/debug.cpp | 2 - src/debug.h | 1 - src/lang/english.txt | 9 - src/misc.cpp | 3 - src/pathfinder/npf/CMakeLists.txt | 2 - src/pathfinder/npf/aystar.cpp | 2 +- src/pathfinder/npf/npf.cpp | 1338 ------------------- src/pathfinder/npf/npf_func.h | 92 -- src/pathfinder/pathfinder_type.h | 12 - src/roadveh_cmd.cpp | 21 +- src/saveload/afterload.cpp | 19 - src/saveload/compat/settings_sl_compat.h | 34 +- src/settings_gui.cpp | 3 - src/settings_type.h | 37 - src/ship_cmd.cpp | 22 +- src/table/settings/pathfinding_settings.ini | 220 --- src/train_cmd.cpp | 37 +- src/vehicle_type.h | 2 +- 18 files changed, 22 insertions(+), 1834 deletions(-) delete mode 100644 src/pathfinder/npf/npf.cpp delete mode 100644 src/pathfinder/npf/npf_func.h diff --git a/src/debug.cpp b/src/debug.cpp index d12265a099..8ba96ef942 100644 --- a/src/debug.cpp +++ b/src/debug.cpp @@ -42,7 +42,6 @@ int _debug_misc_level; int _debug_net_level; int _debug_sprite_level; int _debug_oldloader_level; -int _debug_npf_level; int _debug_yapf_level; int _debug_fontcache_level; int _debug_script_level; @@ -68,7 +67,6 @@ static const DebugLevel _debug_levels[] = { DEBUG_LEVEL(net), DEBUG_LEVEL(sprite), DEBUG_LEVEL(oldloader), - DEBUG_LEVEL(npf), DEBUG_LEVEL(yapf), DEBUG_LEVEL(fontcache), DEBUG_LEVEL(script), diff --git a/src/debug.h b/src/debug.h index de91c21e39..c5afb9180a 100644 --- a/src/debug.h +++ b/src/debug.h @@ -44,7 +44,6 @@ extern int _debug_misc_level; extern int _debug_net_level; extern int _debug_sprite_level; extern int _debug_oldloader_level; -extern int _debug_npf_level; extern int _debug_yapf_level; extern int _debug_fontcache_level; extern int _debug_script_level; diff --git a/src/lang/english.txt b/src/lang/english.txt index 1530dd1e20..f43f46ee20 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -2132,17 +2132,8 @@ STR_CONFIG_SETTING_AI :Competitors STR_CONFIG_SETTING_AI_NPC :Computer players STR_CONFIG_SETTING_NETWORK :Network -STR_CONFIG_SETTING_PATHFINDER_FOR_TRAINS :Pathfinder for trains: {STRING2} -STR_CONFIG_SETTING_PATHFINDER_FOR_TRAINS_HELPTEXT :Path finder to use for trains -STR_CONFIG_SETTING_PATHFINDER_FOR_ROAD_VEHICLES :Pathfinder for road vehicles: {STRING2} -STR_CONFIG_SETTING_PATHFINDER_FOR_ROAD_VEHICLES_HELPTEXT :Path finder to use for road vehicles -STR_CONFIG_SETTING_PATHFINDER_FOR_SHIPS :Pathfinder for ships: {STRING2} -STR_CONFIG_SETTING_PATHFINDER_FOR_SHIPS_HELPTEXT :Path finder to use for ships STR_CONFIG_SETTING_REVERSE_AT_SIGNALS :Automatic reversing at signals: {STRING2} STR_CONFIG_SETTING_REVERSE_AT_SIGNALS_HELPTEXT :Allow trains to reverse on a signal, if they waited there a long time -###length 2 -STR_CONFIG_SETTING_PATHFINDER_NPF :NPF -STR_CONFIG_SETTING_PATHFINDER_YAPF :YAPF {BLUE}(Recommended) STR_CONFIG_SETTING_QUERY_CAPTION :{WHITE}Change setting value diff --git a/src/misc.cpp b/src/misc.cpp index 9137c075b6..72701ff23c 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -54,7 +54,6 @@ void InitializeObjects(); void InitializeTrees(); void InitializeCompanies(); void InitializeCheats(); -void InitializeNPF(); void InitializeOldNames(); /** @@ -149,8 +148,6 @@ void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settin InitializeIndustries(); InitializeObjects(); - InitializeNPF(); - InitializeCompanies(); AI::Initialize(); Game::Initialize(); diff --git a/src/pathfinder/npf/CMakeLists.txt b/src/pathfinder/npf/CMakeLists.txt index e3ace57e52..8a79949dce 100644 --- a/src/pathfinder/npf/CMakeLists.txt +++ b/src/pathfinder/npf/CMakeLists.txt @@ -1,8 +1,6 @@ add_files( aystar.cpp aystar.h - npf.cpp - npf_func.h queue.cpp queue.h ) diff --git a/src/pathfinder/npf/aystar.cpp b/src/pathfinder/npf/aystar.cpp index b52b762d84..31cdd13de1 100644 --- a/src/pathfinder/npf/aystar.cpp +++ b/src/pathfinder/npf/aystar.cpp @@ -168,7 +168,7 @@ int AyStar::Loop() if (current == nullptr) return AYSTAR_EMPTY_OPENLIST; /* Check for end node and if found, return that code */ - if (this->EndNodeCheck(this, current) == AYSTAR_FOUND_END_NODE && !CheckIgnoreFirstTile(¤t->path)) { + if (this->EndNodeCheck(this, current) == AYSTAR_FOUND_END_NODE && (¤t->path)->parent != nullptr) { if (this->FoundEndNode != nullptr) { this->FoundEndNode(this, current); } diff --git a/src/pathfinder/npf/npf.cpp b/src/pathfinder/npf/npf.cpp deleted file mode 100644 index 5ac43cd8c9..0000000000 --- a/src/pathfinder/npf/npf.cpp +++ /dev/null @@ -1,1338 +0,0 @@ -/* - * 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 . - */ - -/** @file npf.cpp Implementation of the NPF pathfinder. */ - -#include "../../stdafx.h" -#include "../../debug.h" -#include "../../network/network.h" -#include "../../viewport_func.h" -#include "../../ship.h" -#include "../../roadstop_base.h" -#include "../../vehicle_func.h" -#include "../pathfinder_func.h" -#include "../pathfinder_type.h" -#include "../follow_track.hpp" -#include "aystar.h" - -#include "../../safeguards.h" - -static const uint NPF_HASH_BITS = 12; ///< The size of the hash used in pathfinding. Just changing this value should be sufficient to change the hash size. Should be an even value. -/* Do no change below values */ -static const uint NPF_HASH_SIZE = 1 << NPF_HASH_BITS; -static const uint NPF_HASH_HALFBITS = NPF_HASH_BITS / 2; -static const uint NPF_HASH_HALFMASK = (1 << NPF_HASH_HALFBITS) - 1; - -/** Meant to be stored in AyStar.targetdata */ -struct NPFFindStationOrTileData { - TileIndex dest_coords; ///< An indication of where the station is, for heuristic purposes, or the target tile - StationID station_index; ///< station index we're heading for, or INVALID_STATION when we're heading for a tile - bool reserve_path; ///< Indicates whether the found path should be reserved - StationType station_type; ///< The type of station we're heading for - bool not_articulated; ///< The (road) vehicle is not articulated - const Vehicle *v; ///< The vehicle we are pathfinding for -}; - -/** Indices into AyStar.userdata[] */ -struct AyStarUserData { - Owner owner; - TransportType type; - RailTypes railtypes; - RoadTypes roadtypes; - uint subtype; -}; - -/** Indices into AyStarNode.userdata[] */ -enum AyStarNodeUserDataType { - NPF_TRACKDIR_CHOICE = 0, ///< The trackdir chosen to get here - NPF_NODE_FLAGS, -}; - -/** Flags for AyStarNode.userdata[NPF_NODE_FLAGS]. Use NPFSetFlag() and NPFGetFlag() to use them. */ -enum NPFNodeFlag { - NPF_FLAG_SEEN_SIGNAL, ///< Used to mark that a signal was seen on the way, for rail only - NPF_FLAG_2ND_SIGNAL, ///< Used to mark that two signals were seen, rail only - NPF_FLAG_3RD_SIGNAL, ///< Used to mark that three signals were seen, rail only - NPF_FLAG_REVERSE, ///< Used to mark that this node was reached from the second start node, if applicable - NPF_FLAG_LAST_SIGNAL_RED, ///< Used to mark that the last signal on this path was red - NPF_FLAG_LAST_SIGNAL_BLOCK, ///< Used to mark that the last signal on this path was a block signal - NPF_FLAG_IGNORE_START_TILE, ///< Used to mark that the start tile is invalid, and searching should start from the second tile on - NPF_FLAG_TARGET_RESERVED, ///< Used to mark that the possible reservation target is already reserved - NPF_FLAG_IGNORE_RESERVED, ///< Used to mark that reserved tiles should be considered impassable -}; - -/** Meant to be stored in AyStar.userpath */ -struct NPFFoundTargetData { - uint best_bird_dist; ///< The best heuristic found. Is 0 if the target was found - uint best_path_dist; ///< The shortest path. Is UINT_MAX if no path is found - Trackdir best_trackdir; ///< The trackdir that leads to the shortest path/closest birds dist - AyStarNode node; ///< The node within the target the search led us to - bool res_okay; ///< True if a path reservation could be made -}; - -static AyStar _npf_aystar; - -/* The cost of each trackdir. A diagonal piece is the full NPF_TILE_LENGTH, - * the shorter piece is sqrt(2)/2*NPF_TILE_LENGTH =~ 0.7071 - */ -#define NPF_STRAIGHT_LENGTH (uint)(NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH) -static const uint _trackdir_length[TRACKDIR_END] = { - NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, - 0, 0, - NPF_TILE_LENGTH, NPF_TILE_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH, NPF_STRAIGHT_LENGTH -}; - -/** - * Returns the current value of the given flag on the given AyStarNode. - */ -static inline bool NPFGetFlag(const AyStarNode *node, NPFNodeFlag flag) -{ - return HasBit(node->user_data[NPF_NODE_FLAGS], flag); -} - -/** - * Sets the given flag on the given AyStarNode to the given value. - */ -static inline void NPFSetFlag(AyStarNode *node, NPFNodeFlag flag, bool value) -{ - SB(node->user_data[NPF_NODE_FLAGS], flag, 1, value); -} - -bool CheckIgnoreFirstTile(const PathNode *node) -{ - return (node->parent == nullptr && HasBit(node->node.user_data[NPF_NODE_FLAGS], NPF_FLAG_IGNORE_START_TILE)); -} - -/** - * Calculates the minimum distance travelled to get from t0 to t1 when only - * using tracks (ie, only making 45 degree turns). Returns the distance in the - * NPF scale, ie the number of full tiles multiplied by NPF_TILE_LENGTH to - * prevent rounding. - */ -static uint NPFDistanceTrack(TileIndex t0, TileIndex t1) -{ - const uint dx = Delta(TileX(t0), TileX(t1)); - const uint dy = Delta(TileY(t0), TileY(t1)); - - const uint straightTracks = 2 * std::min(dx, dy); // The number of straight (not full length) tracks - /* OPTIMISATION: - * Original: diagTracks = max(dx, dy) - min(dx,dy); - * Proof: - * (dx+dy) - straightTracks == (min + max) - straightTracks = min + max - 2 * min = max - min */ - const uint diagTracks = dx + dy - straightTracks; // The number of diagonal (full tile length) tracks. - - /* Don't factor out NPF_TILE_LENGTH below, this will round values and lose - * precision */ - return diagTracks * NPF_TILE_LENGTH + straightTracks * NPF_TILE_LENGTH * STRAIGHT_TRACK_LENGTH; -} - -/** - * Calculates a hash value for use in the NPF. - * @param tile The TileIndex of the tile to hash - * @param dir The Trackdir of the track on the tile. - * - * @todo Think of a better hash. - */ -static uint NPFHash(TileIndex tile, Trackdir dir) -{ - /* TODO: think of a better hash? */ - uint part1 = TileX(tile) & NPF_HASH_HALFMASK; - uint part2 = TileY(tile) & NPF_HASH_HALFMASK; - - assert(IsValidTrackdir(dir)); - assert(IsValidTile(tile)); - return ((part1 << NPF_HASH_HALFBITS | part2) + (NPF_HASH_SIZE * dir / TRACKDIR_END)) % NPF_HASH_SIZE; -} - -static int32_t NPFCalcZero(AyStar *, AyStarNode *, OpenListNode *) -{ - return 0; -} - -/* Calculates the heuristic to the target station or tile. For train stations, it - * takes into account the direction of approach. - */ -static int32_t NPFCalcStationOrTileHeuristic(AyStar *as, AyStarNode *current, OpenListNode *) -{ - NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target; - NPFFoundTargetData *ftd = (NPFFoundTargetData*)as->user_path; - TileIndex from = current->tile; - TileIndex to = fstd->dest_coords; - uint dist; - AyStarUserData *user = (AyStarUserData *)as->user_data; - - /* aim for the closest station tile */ - if (fstd->station_index != INVALID_STATION) { - to = CalcClosestStationTile(fstd->station_index, from, fstd->station_type); - } - - if (user->type == TRANSPORT_ROAD) { - /* Since roads only have diagonal pieces, we use manhattan distance here */ - dist = DistanceManhattan(from, to) * NPF_TILE_LENGTH; - } else { - /* Ships and trains can also go diagonal, so the minimum distance is shorter */ - dist = NPFDistanceTrack(from, to); - } - - Debug(npf, 4, "Calculating H for: ({}, {}). Result: {}", TileX(current->tile), TileY(current->tile), dist); - - if (dist < ftd->best_bird_dist) { - ftd->best_bird_dist = dist; - ftd->best_trackdir = (Trackdir)current->user_data[NPF_TRACKDIR_CHOICE]; - } - return dist; -} - - -/* Fills AyStarNode.user_data[NPF_TRACKDIRCHOICE] with the chosen direction to - * get here, either getting it from the current choice or from the parent's - * choice */ -static void NPFFillTrackdirChoice(AyStarNode *current, OpenListNode *parent) -{ - if (parent->path.parent == nullptr) { - Trackdir trackdir = current->direction; - /* This is a first order decision, so we'd better save the - * direction we chose */ - current->user_data[NPF_TRACKDIR_CHOICE] = trackdir; - Debug(npf, 6, "Saving trackdir: 0x{:X}", trackdir); - } else { - /* We've already made the decision, so just save our parent's decision */ - current->user_data[NPF_TRACKDIR_CHOICE] = parent->path.node.user_data[NPF_TRACKDIR_CHOICE]; - } -} - -/* Will return the cost of the tunnel. If it is an entry, it will return the - * cost of that tile. If the tile is an exit, it will return the tunnel length - * including the exit tile. Requires that this is a Tunnel tile */ -static uint NPFTunnelCost(AyStarNode *current) -{ - DiagDirection exitdir = TrackdirToExitdir(current->direction); - TileIndex tile = current->tile; - if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) { - /* We just popped out if this tunnel, since were - * facing the tunnel exit */ - return NPF_TILE_LENGTH * (GetTunnelBridgeLength(current->tile, GetOtherTunnelEnd(current->tile)) + 1); - /* @todo: Penalty for tunnels? */ - } else { - /* We are entering the tunnel, the enter tile is just a - * straight track */ - return NPF_TILE_LENGTH; - } -} - -static inline uint NPFBridgeCost(AyStarNode *current) -{ - return NPF_TILE_LENGTH * GetTunnelBridgeLength(current->tile, GetOtherBridgeEnd(current->tile)); -} - -static uint NPFSlopeCost(AyStarNode *current) -{ - TileIndex next = current->tile + TileOffsByDiagDir(TrackdirToExitdir(current->direction)); - - /* Get center of tiles */ - int x1 = TileX(current->tile) * TILE_SIZE + TILE_SIZE / 2; - int y1 = TileY(current->tile) * TILE_SIZE + TILE_SIZE / 2; - int x2 = TileX(next) * TILE_SIZE + TILE_SIZE / 2; - int y2 = TileY(next) * TILE_SIZE + TILE_SIZE / 2; - - int dx4 = (x2 - x1) / 4; - int dy4 = (y2 - y1) / 4; - - /* Get the height on both sides of the tile edge. - * Avoid testing the height on the tile-center. This will fail for halftile-foundations. - */ - int z1 = GetSlopePixelZ(x1 + dx4, y1 + dy4, true); - int z2 = GetSlopePixelZ(x2 - dx4, y2 - dy4, true); - - if (z2 - z1 > 1) { - /* Slope up */ - return _settings_game.pf.npf.npf_rail_slope_penalty; - } - return 0; - /* Should we give a bonus for slope down? Probably not, we - * could just subtract that bonus from the penalty, because - * there is only one level of steepness... */ -} - -static uint NPFReservedTrackCost(AyStarNode *current) -{ - TileIndex tile = current->tile; - TrackBits track = TrackToTrackBits(TrackdirToTrack(current->direction)); - TrackBits res = GetReservedTrackbits(tile); - - if (NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL) || NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_BLOCK) || ((res & track) == TRACK_BIT_NONE && !TracksOverlap(res | track))) return 0; - - if (IsTileType(tile, MP_TUNNELBRIDGE)) { - DiagDirection exitdir = TrackdirToExitdir(current->direction); - if (GetTunnelBridgeDirection(tile) == ReverseDiagDir(exitdir)) { - return _settings_game.pf.npf.npf_rail_pbs_cross_penalty * (GetTunnelBridgeLength(tile, GetOtherTunnelBridgeEnd(tile)) + 1); - } - } - return _settings_game.pf.npf.npf_rail_pbs_cross_penalty; -} - -/** - * Mark tiles by mowing the grass when npf debug level >= 1. - * Will not work for multiplayer games, since it can (will) cause desyncs. - */ -static void NPFMarkTile(TileIndex tile) -{ - if (_debug_npf_level < 1 || _networking) return; - switch (GetTileType(tile)) { - case MP_RAILWAY: - /* DEBUG: mark visited tiles by mowing the grass under them ;-) */ - if (!IsRailDepot(tile)) { - SetRailGroundType(tile, RAIL_GROUND_BARREN); - MarkTileDirtyByTile(tile); - } - break; - - case MP_ROAD: - if (!IsRoadDepot(tile)) { - SetRoadside(tile, ROADSIDE_BARREN); - MarkTileDirtyByTile(tile); - } - break; - - default: - break; - } -} - -static Vehicle *CountShipProc(Vehicle *v, void *data) -{ - uint *count = (uint *)data; - /* Ignore other vehicles (aircraft) and ships inside depot. */ - if (v->type == VEH_SHIP && (v->vehstatus & VS_HIDDEN) == 0) (*count)++; - - return nullptr; -} - -static int32_t NPFWaterPathCost(AyStar *, AyStarNode *current, OpenListNode *parent) -{ - int32_t cost = 0; - Trackdir trackdir = current->direction; - - cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks - - if (IsBuoyTile(current->tile) && IsDiagonalTrackdir(trackdir)) { - cost += _settings_game.pf.npf.npf_buoy_penalty; // A small penalty for going over buoys - } - - if (current->direction != NextTrackdir(parent->path.node.direction)) { - cost += _settings_game.pf.npf.npf_water_curve_penalty; - } - - if (IsDockingTile(current->tile)) { - /* Check docking tile for occupancy */ - uint count = 0; - HasVehicleOnPos(current->tile, &count, &CountShipProc); - cost += count * 3 * _trackdir_length[trackdir]; - } - - /* @todo More penalties? */ - - return cost; -} - -/* Determine the cost of this node, for road tracks */ -static int32_t NPFRoadPathCost(AyStar *, AyStarNode *current, OpenListNode *) -{ - TileIndex tile = current->tile; - int32_t cost = 0; - - /* Determine base length */ - switch (GetTileType(tile)) { - case MP_TUNNELBRIDGE: - cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current); - break; - - case MP_ROAD: - cost = NPF_TILE_LENGTH; - /* Increase the cost for level crossings */ - if (IsLevelCrossing(tile)) cost += _settings_game.pf.npf.npf_crossing_penalty; - break; - - case MP_STATION: { - cost = NPF_TILE_LENGTH; - const RoadStop *rs = RoadStop::GetByTile(tile, GetRoadStopType(tile)); - if (IsDriveThroughStopTile(tile)) { - /* Increase the cost for drive-through road stops */ - cost += _settings_game.pf.npf.npf_road_drive_through_penalty; - DiagDirection dir = TrackdirToExitdir(current->direction); - if (!RoadStop::IsDriveThroughRoadStopContinuation(tile, tile - TileOffsByDiagDir(dir))) { - /* When we're the first road stop in a 'queue' of them we increase - * cost based on the fill percentage of the whole queue. */ - const RoadStop::Entry *entry = rs->GetEntry(dir); - cost += entry->GetOccupied() * _settings_game.pf.npf.npf_road_dt_occupied_penalty / entry->GetLength(); - } - } else { - /* Increase cost for filled road stops */ - cost += _settings_game.pf.npf.npf_road_bay_occupied_penalty * (!rs->IsFreeBay(0) + !rs->IsFreeBay(1)) / 2; - } - break; - } - - default: - break; - } - - /* Determine extra costs */ - - /* Check for slope */ - cost += NPFSlopeCost(current); - - /* Check for turns. Road vehicles only really drive diagonal, turns are - * represented by non-diagonal tracks */ - if (!IsDiagonalTrackdir(current->direction)) { - cost += _settings_game.pf.npf.npf_road_curve_penalty; - } - - NPFMarkTile(tile); - Debug(npf, 4, "Calculating G for: ({}, {}). Result: {}", TileX(current->tile), TileY(current->tile), cost); - return cost; -} - - -/* Determine the cost of this node, for railway tracks */ -static int32_t NPFRailPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent) -{ - TileIndex tile = current->tile; - Trackdir trackdir = current->direction; - int32_t cost = 0; - /* HACK: We create a OpenListNode manually, so we can call EndNodeCheck */ - OpenListNode new_node; - - /* Determine base length */ - switch (GetTileType(tile)) { - case MP_TUNNELBRIDGE: - cost = IsTunnel(tile) ? NPFTunnelCost(current) : NPFBridgeCost(current); - break; - - case MP_RAILWAY: - cost = _trackdir_length[trackdir]; // Should be different for diagonal tracks - break; - - case MP_ROAD: // Railway crossing - cost = NPF_TILE_LENGTH; - break; - - case MP_STATION: - /* We give a station tile a penalty. Logically we would only want to give - * station tiles that are not our destination this penalty. This would - * discourage trains to drive through busy stations. But, we can just - * give any station tile a penalty, because every possible route will get - * this penalty exactly once, on its end tile (if it's a station) and it - * will therefore not make a difference. */ - cost = NPF_TILE_LENGTH + _settings_game.pf.npf.npf_rail_station_penalty; - - if (IsRailWaypoint(tile)) { - NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target; - if (fstd->v->current_order.IsType(OT_GOTO_WAYPOINT) && GetStationIndex(tile) == fstd->v->current_order.GetDestination()) { - /* This waypoint is our destination; maybe this isn't an unreserved - * one, so check that and if so see that as the last signal being - * red. This way waypoints near stations should work better. */ - const Train *train = Train::From(fstd->v); - CFollowTrackRail ft(train); - TileIndex t = tile; - Trackdir td = trackdir; - while (ft.Follow(t, td)) { - assert(t != ft.m_new_tile); - t = ft.m_new_tile; - if (KillFirstBit(ft.m_new_td_bits) != TRACKDIR_BIT_NONE) { - /* We encountered a junction; it's going to be too complex to - * handle this perfectly, so just bail out. There is no simple - * free path, so try the other possibilities. */ - td = INVALID_TRACKDIR; - break; - } - td = RemoveFirstTrackdir(&ft.m_new_td_bits); - /* If this is a safe waiting position we're done searching for it */ - if (IsSafeWaitingPosition(train, t, td, true, _settings_game.pf.forbid_90_deg)) break; - } - if (td == INVALID_TRACKDIR || - !IsSafeWaitingPosition(train, t, td, true, _settings_game.pf.forbid_90_deg) || - !IsWaitingPositionFree(train, t, td, _settings_game.pf.forbid_90_deg)) { - cost += _settings_game.pf.npf.npf_rail_lastred_penalty; - } - } - } - break; - - default: - break; - } - - /* Determine extra costs */ - - /* Check for signals */ - if (IsTileType(tile, MP_RAILWAY)) { - if (HasSignalOnTrackdir(tile, trackdir)) { - SignalType sigtype = GetSignalType(tile, TrackdirToTrack(trackdir)); - /* Ordinary track with signals */ - if (GetSignalStateByTrackdir(tile, trackdir) == SIGNAL_STATE_RED) { - /* Signal facing us is red */ - if (!NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) { - /* Penalize the first signal we - * encounter, if it is red */ - - /* Is this a presignal exit or combo? */ - if (!IsPbsSignal(sigtype)) { - if (sigtype == SIGTYPE_EXIT || sigtype == SIGTYPE_COMBO) { - /* Penalise exit and combo signals differently (heavier) */ - cost += _settings_game.pf.npf.npf_rail_firstred_exit_penalty; - } else { - cost += _settings_game.pf.npf.npf_rail_firstred_penalty; - } - } - } - /* Record the state of this signal. Path signals are assumed to - * be green as the signal state of them has no meaning for this. */ - NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, !IsPbsSignal(sigtype)); - } else { - /* Record the state of this signal */ - NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, false); - } - if (NPFGetFlag(current, NPF_FLAG_SEEN_SIGNAL)) { - if (NPFGetFlag(current, NPF_FLAG_2ND_SIGNAL)) { - NPFSetFlag(current, NPF_FLAG_3RD_SIGNAL, true); - } else { - NPFSetFlag(current, NPF_FLAG_2ND_SIGNAL, true); - } - } else { - NPFSetFlag(current, NPF_FLAG_SEEN_SIGNAL, true); - } - NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_BLOCK, !IsPbsSignal(sigtype)); - } - - if (HasPbsSignalOnTrackdir(tile, ReverseTrackdir(trackdir)) && !NPFGetFlag(current, NPF_FLAG_3RD_SIGNAL)) { - cost += _settings_game.pf.npf.npf_rail_pbs_signal_back_penalty; - } - } - - /* Penalise the tile if it is a target tile and the last signal was - * red */ - /* HACK: We create a new_node here so we can call EndNodeCheck. Ugly as hell - * of course... */ - new_node.path.node = *current; - if (as->EndNodeCheck(as, &new_node) == AYSTAR_FOUND_END_NODE && NPFGetFlag(current, NPF_FLAG_LAST_SIGNAL_RED)) { - cost += _settings_game.pf.npf.npf_rail_lastred_penalty; - } - - /* Check for slope */ - cost += NPFSlopeCost(current); - - /* Check for turns */ - if (current->direction != NextTrackdir(parent->path.node.direction)) { - cost += _settings_game.pf.npf.npf_rail_curve_penalty; - } - /* TODO, with realistic acceleration, also the amount of straight track between - * curves should be taken into account, as this affects the speed limit. */ - - /* Check for reverse in depot */ - if (IsRailDepotTile(tile) && as->EndNodeCheck(as, &new_node) != AYSTAR_FOUND_END_NODE) { - /* Penalise any depot tile that is not the last tile in the path. This - * _should_ penalise every occurrence of reversing in a depot (and only - * that) */ - cost += _settings_game.pf.npf.npf_rail_depot_reverse_penalty; - } - - /* Check for occupied track */ - cost += NPFReservedTrackCost(current); - - NPFMarkTile(tile); - Debug(npf, 4, "Calculating G for: ({}, {}). Result: {}", TileX(current->tile), TileY(current->tile), cost); - return cost; -} - -/* Will find any depot */ -static int32_t NPFFindDepot(const AyStar *as, const OpenListNode *current) -{ - AyStarUserData *user = (AyStarUserData *)as->user_data; - /* It's not worth caching the result with NPF_FLAG_IS_TARGET here as below, - * since checking the cache not that much faster than the actual check */ - return IsDepotTypeTile(current->path.node.tile, user->type) ? - AYSTAR_FOUND_END_NODE : AYSTAR_DONE; -} - -/** Find any safe and free tile. */ -static int32_t NPFFindSafeTile(const AyStar *as, const OpenListNode *current) -{ - const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v); - - return (IsSafeWaitingPosition(v, current->path.node.tile, current->path.node.direction, true, _settings_game.pf.forbid_90_deg) && - IsWaitingPositionFree(v, current->path.node.tile, current->path.node.direction, _settings_game.pf.forbid_90_deg)) ? - AYSTAR_FOUND_END_NODE : AYSTAR_DONE; -} - -/* Will find a station identified using the NPFFindStationOrTileData */ -static int32_t NPFFindStationOrTile(const AyStar *as, const OpenListNode *current) -{ - NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target; - const AyStarNode *node = ¤t->path.node; - TileIndex tile = node->tile; - - if (fstd->station_index == INVALID_STATION && tile == fstd->dest_coords) return AYSTAR_FOUND_END_NODE; - - if (fstd->v->type == VEH_SHIP) { - /* Ships do not actually reach the destination station, so we check for a docking tile instead. */ - if (IsDockingTile(tile) && IsShipDestinationTile(tile, fstd->station_index)) return AYSTAR_FOUND_END_NODE; - return AYSTAR_DONE; - } - - if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == fstd->station_index) { - if (fstd->v->type == VEH_TRAIN) return AYSTAR_FOUND_END_NODE; - - assert(fstd->v->type == VEH_ROAD); - /* Only if it is a valid station *and* we can stop there */ - if (GetStationType(tile) == fstd->station_type && (fstd->not_articulated || IsDriveThroughStopTile(tile))) return AYSTAR_FOUND_END_NODE; - } - return AYSTAR_DONE; -} - -/** - * Find the node containing the first signal on the path. - * - * If the first signal is on the very first two tiles of the path, - * the second signal is returned. If no suitable signal is present, the - * last node of the path is returned. - */ -static const PathNode *FindSafePosition(PathNode *path, const Train *v) -{ - /* If there is no signal, reserve the whole path. */ - PathNode *sig = path; - - for (; path->parent != nullptr; path = path->parent) { - if (IsSafeWaitingPosition(v, path->node.tile, path->node.direction, true, _settings_game.pf.forbid_90_deg)) { - sig = path; - } - } - - return sig; -} - -/** - * Lift the reservation of the tiles from @p start till @p end, excluding @p end itself. - */ -static void ClearPathReservation(const PathNode *start, const PathNode *end) -{ - bool first_run = true; - for (; start != end; start = start->parent) { - if (IsRailStationTile(start->node.tile) && first_run) { - SetRailStationPlatformReservation(start->node.tile, TrackdirToExitdir(start->node.direction), false); - } else { - UnreserveRailTrack(start->node.tile, TrackdirToTrack(start->node.direction)); - } - first_run = false; - } -} - -/** - * To be called when @p current contains the (shortest route to) the target node. - * Will fill the contents of the NPFFoundTargetData using - * AyStarNode[NPF_TRACKDIR_CHOICE]. If requested, path reservation - * is done here. - */ -static void NPFSaveTargetData(AyStar *as, OpenListNode *current) -{ - AyStarUserData *user = (AyStarUserData *)as->user_data; - NPFFoundTargetData *ftd = (NPFFoundTargetData*)as->user_path; - ftd->best_trackdir = (Trackdir)current->path.node.user_data[NPF_TRACKDIR_CHOICE]; - ftd->best_path_dist = current->g; - ftd->best_bird_dist = 0; - ftd->node = current->path.node; - ftd->res_okay = false; - - if (as->user_target != nullptr && ((NPFFindStationOrTileData*)as->user_target)->reserve_path && user->type == TRANSPORT_RAIL) { - /* Path reservation is requested. */ - const Train *v = Train::From(((NPFFindStationOrTileData *)as->user_target)->v); - - const PathNode *target = FindSafePosition(¤t->path, v); - ftd->node = target->node; - - /* If the target is a station skip to platform end. */ - if (IsRailStationTile(target->node.tile)) { - DiagDirection dir = TrackdirToExitdir(target->node.direction); - uint len = Station::GetByTile(target->node.tile)->GetPlatformLength(target->node.tile, dir); - TileIndex end_tile = TileAdd(target->node.tile, (len - 1) * TileOffsByDiagDir(dir)); - - /* Update only end tile, trackdir of a station stays the same. */ - ftd->node.tile = end_tile; - if (!IsWaitingPositionFree(v, end_tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return; - SetRailStationPlatformReservation(target->node.tile, dir, true); - SetRailStationReservation(target->node.tile, false); - } else { - if (!IsWaitingPositionFree(v, target->node.tile, target->node.direction, _settings_game.pf.forbid_90_deg)) return; - } - - for (const PathNode *cur = target; cur->parent != nullptr; cur = cur->parent) { - if (!TryReserveRailTrack(cur->node.tile, TrackdirToTrack(cur->node.direction))) { - /* Reservation failed, undo. */ - ClearPathReservation(target, cur); - return; - } - } - - ftd->res_okay = true; - } -} - -/** - * Finds out if a given company's vehicles are allowed to enter a given tile. - * @param owner The owner of the vehicle. - * @param tile The tile that is about to be entered. - * @param enterdir The direction in which the vehicle wants to enter the tile. - * @return true if the vehicle can enter the tile. - * @todo This function should be used in other places than just NPF, - * maybe moved to another file too. - */ -static bool CanEnterTileOwnerCheck(Owner owner, TileIndex tile, DiagDirection enterdir) -{ - if (IsTileType(tile, MP_RAILWAY) || // Rail tile (also rail depot) - HasStationTileRail(tile) || // Rail station tile/waypoint - IsRoadDepotTile(tile) || // Road depot tile - IsBayRoadStopTile(tile)) { // Road station tile (but not drive-through stops) - return IsTileOwner(tile, owner); // You need to own these tiles entirely to use them - } - - switch (GetTileType(tile)) { - case MP_ROAD: - /* rail-road crossing : are we looking at the railway part? */ - if (IsLevelCrossing(tile) && - DiagDirToAxis(enterdir) != GetCrossingRoadAxis(tile)) { - return IsTileOwner(tile, owner); // Railway needs owner check, while the street is public - } - break; - - case MP_TUNNELBRIDGE: - if (GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL) { - return IsTileOwner(tile, owner); - } - break; - - default: - break; - } - - return true; // no need to check -} - - -/** - * Returns the direction the exit of the depot on the given tile is facing. - */ -static DiagDirection GetDepotDirection(TileIndex tile, TransportType type) -{ - assert(IsDepotTypeTile(tile, type)); - - switch (type) { - case TRANSPORT_RAIL: return GetRailDepotDirection(tile); - case TRANSPORT_ROAD: return GetRoadDepotDirection(tile); - case TRANSPORT_WATER: return GetShipDepotDirection(tile); - default: return INVALID_DIAGDIR; // Not reached - } -} - -/** Tests if a tile is a road tile with a single tramtrack (tram can reverse) */ -static DiagDirection GetSingleTramBit(TileIndex tile) -{ - if (IsNormalRoadTile(tile)) { - RoadBits rb = GetRoadBits(tile, RTT_TRAM); - switch (rb) { - case ROAD_NW: return DIAGDIR_NW; - case ROAD_SW: return DIAGDIR_SW; - case ROAD_SE: return DIAGDIR_SE; - case ROAD_NE: return DIAGDIR_NE; - default: break; - } - } - return INVALID_DIAGDIR; -} - -/** - * Tests if a tile can be entered or left only from one side. - * - * Depots, non-drive-through roadstops, and tiles with single trambits are tested. - * - * @param tile The tile of interest. - * @param type The transporttype of the vehicle. - * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. - * @return The single entry/exit-direction of the tile, or INVALID_DIAGDIR if there are more or less directions - */ -static DiagDirection GetTileSingleEntry(TileIndex tile, TransportType type, uint subtype) -{ - if (type != TRANSPORT_WATER && IsDepotTypeTile(tile, type)) return GetDepotDirection(tile, type); - - if (type == TRANSPORT_ROAD) { - if (IsBayRoadStopTile(tile)) return GetRoadStopDir(tile); - if ((RoadTramType)subtype == RTT_TRAM) return GetSingleTramBit(tile); - } - - return INVALID_DIAGDIR; -} - -/** - * Tests if a vehicle must reverse on a tile. - * - * @param tile The tile of interest. - * @param dir The direction in which the vehicle drives on a tile. - * @param type The transporttype of the vehicle. - * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. - * @return true iff the vehicle must reverse on the tile. - */ -static inline bool ForceReverse(TileIndex tile, DiagDirection dir, TransportType type, uint subtype) -{ - DiagDirection single_entry = GetTileSingleEntry(tile, type, subtype); - return single_entry != INVALID_DIAGDIR && single_entry != dir; -} - -/** - * Tests if a vehicle can enter a tile. - * - * @param tile The tile of interest. - * @param dir The direction in which the vehicle drives onto a tile. - * @param user Vehicle information. - * @return true iff the vehicle can enter the tile. - */ -static bool CanEnterTile(TileIndex tile, DiagDirection dir, AyStarUserData *user) -{ - /* Check tunnel entries and bridge ramps */ - if (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(tile) != dir) return false; - - /* Test ownership */ - if (!CanEnterTileOwnerCheck(user->owner, tile, dir)) return false; - - /* check correct rail type (mono, maglev, etc) */ - switch (user->type) { - case TRANSPORT_RAIL: { - RailType rail_type = GetTileRailType(tile); - if (!HasBit(user->railtypes, rail_type)) return false; - break; - } - - case TRANSPORT_ROAD: { - RoadType road_type = GetRoadType(tile, (RoadTramType)user->subtype); - if (!HasBit(user->roadtypes, road_type)) return false; - break; - } - - default: break; - } - - /* Depots, standard roadstops and single tram bits can only be entered from one direction */ - DiagDirection single_entry = GetTileSingleEntry(tile, user->type, user->subtype); - return single_entry == INVALID_DIAGDIR || single_entry == ReverseDiagDir(dir); -} - -/** - * Returns the driveable Trackdirs on a tile. - * - * One-way-roads are taken into account. Signals are not tested. - * - * @param dst_tile The tile of interest. - * @param src_tile The originating tile. - * @param src_trackdir The direction the vehicle is currently moving. - * @param type The transporttype of the vehicle. - * @param subtype For TRANSPORT_ROAD the compatible RoadTypes of the vehicle. - * @return The Trackdirs the vehicle can continue moving on. - */ -static TrackdirBits GetDriveableTrackdirBits(TileIndex dst_tile, TileIndex src_tile, Trackdir src_trackdir, TransportType type, uint subtype) -{ - TrackdirBits trackdirbits = TrackStatusToTrackdirBits(GetTileTrackStatus(dst_tile, type, subtype)); - - if (trackdirbits == TRACKDIR_BIT_NONE && type == TRANSPORT_ROAD && (RoadTramType)subtype == RTT_TRAM) { - /* GetTileTrackStatus() returns 0 for single tram bits. - * As we cannot change it there (easily) without breaking something, change it here */ - switch (GetSingleTramBit(dst_tile)) { - case DIAGDIR_NE: - case DIAGDIR_SW: - trackdirbits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW; - break; - - case DIAGDIR_NW: - case DIAGDIR_SE: - trackdirbits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE; - break; - - default: break; - } - } - - Debug(npf, 4, "Next node: ({}, {}) [{}], possible trackdirs: 0x{:X}", TileX(dst_tile), TileY(dst_tile), dst_tile, trackdirbits); - - /* Select only trackdirs we can reach from our current trackdir */ - trackdirbits &= TrackdirReachesTrackdirs(src_trackdir); - - /* Filter out trackdirs that would make 90 deg turns for trains */ - if (type == TRANSPORT_RAIL && Rail90DegTurnDisallowed(GetTileRailType(src_tile), GetTileRailType(dst_tile))) { - trackdirbits &= ~TrackdirCrossesTrackdirs(src_trackdir); - } - - Debug(npf, 6, "After filtering: ({}, {}), possible trackdirs: 0x{:X}", TileX(dst_tile), TileY(dst_tile), trackdirbits); - - return trackdirbits; -} - - -/* Will just follow the results of GetTileTrackStatus concerning where we can - * go and where not. Uses AyStar.user_data[NPF_TYPE] as the transport type and - * an argument to GetTileTrackStatus. Will skip tunnels, meaning that the - * entry and exit are neighbours. Will fill - * AyStarNode.user_data[NPF_TRACKDIR_CHOICE] with an appropriate value, and - * copy AyStarNode.user_data[NPF_NODE_FLAGS] from the parent */ -static void NPFFollowTrack(AyStar *aystar, OpenListNode *current) -{ - AyStarUserData *user = (AyStarUserData *)aystar->user_data; - /* We leave src_tile on track src_trackdir in direction src_exitdir */ - Trackdir src_trackdir = current->path.node.direction; - TileIndex src_tile = current->path.node.tile; - DiagDirection src_exitdir = TrackdirToExitdir(src_trackdir); - - /* Information about the vehicle: TransportType (road/rail/water) and SubType (compatible rail/road types) */ - TransportType type = user->type; - uint subtype = user->subtype; - - /* Initialize to 0, so we can jump out (return) somewhere an have no neighbours */ - aystar->num_neighbours = 0; - Debug(npf, 4, "Expanding: ({}, {}, {}) [{}]", TileX(src_tile), TileY(src_tile), src_trackdir, src_tile); - - /* We want to determine the tile we arrive, and which choices we have there */ - TileIndex dst_tile; - TrackdirBits trackdirbits; - - /* Find dest tile */ - /* Is src_tile valid, and can be used? - * When choosing track on a junction src_tile is the tile neighboured to the junction wrt. exitdir. - * But we must not check the validity of this move, as src_tile is totally unrelated to the move, if a roadvehicle reversed on a junction. */ - if (CheckIgnoreFirstTile(¤t->path)) { - /* Do not perform any checks that involve src_tile */ - dst_tile = src_tile + TileOffsByDiagDir(src_exitdir); - trackdirbits = GetDriveableTrackdirBits(dst_tile, src_tile, src_trackdir, type, subtype); - } else if (IsTileType(src_tile, MP_TUNNELBRIDGE) && GetTunnelBridgeDirection(src_tile) == src_exitdir) { - /* We drive through the wormhole and arrive on the other side */ - dst_tile = GetOtherTunnelBridgeEnd(src_tile); - trackdirbits = TrackdirToTrackdirBits(src_trackdir); - } else if (ForceReverse(src_tile, src_exitdir, type, subtype)) { - /* We can only reverse on this tile */ - dst_tile = src_tile; - src_trackdir = ReverseTrackdir(src_trackdir); - trackdirbits = TrackdirToTrackdirBits(src_trackdir); - } else { - /* We leave src_tile in src_exitdir and reach dst_tile */ - dst_tile = AddTileIndexDiffCWrap(src_tile, TileIndexDiffCByDiagDir(src_exitdir)); - - if (dst_tile != INVALID_TILE && IsNormalRoadTile(dst_tile) && !CanEnterTile(dst_tile, src_exitdir, user)) dst_tile = INVALID_TILE; - - if (dst_tile == INVALID_TILE) { - /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */ - if (type != TRANSPORT_ROAD || (RoadTramType)subtype == RTT_TRAM) return; - - dst_tile = src_tile; - src_trackdir = ReverseTrackdir(src_trackdir); - } - - trackdirbits = GetDriveableTrackdirBits(dst_tile, src_tile, src_trackdir, type, subtype); - - if (trackdirbits == TRACKDIR_BIT_NONE) { - /* We cannot enter the next tile. Road vehicles can reverse, others reach dead end */ - if (type != TRANSPORT_ROAD || (RoadTramType)subtype == RTT_TRAM) return; - - dst_tile = src_tile; - src_trackdir = ReverseTrackdir(src_trackdir); - - trackdirbits = GetDriveableTrackdirBits(dst_tile, src_tile, src_trackdir, type, subtype); - } - } - - if (NPFGetFlag(¤t->path.node, NPF_FLAG_IGNORE_RESERVED)) { - /* Mask out any reserved tracks. */ - TrackBits reserved = GetReservedTrackbits(dst_tile); - trackdirbits &= ~TrackBitsToTrackdirBits(reserved); - - for (Track t : SetTrackBitIterator(TrackdirBitsToTrackBits(trackdirbits))) { - if (TracksOverlap(reserved | TrackToTrackBits(t))) trackdirbits &= ~TrackToTrackdirBits(t); - } - } - - /* Enumerate possible track */ - uint i = 0; - while (trackdirbits != TRACKDIR_BIT_NONE) { - Trackdir dst_trackdir = RemoveFirstTrackdir(&trackdirbits); - Debug(npf, 5, "Expanded into trackdir: {}, remaining trackdirs: 0x{:X}", dst_trackdir, trackdirbits); - - /* Tile with signals? */ - if (IsTileType(dst_tile, MP_RAILWAY) && GetRailTileType(dst_tile) == RAIL_TILE_SIGNALS) { - if (HasSignalOnTrackdir(dst_tile, ReverseTrackdir(dst_trackdir)) && !HasSignalOnTrackdir(dst_tile, dst_trackdir) && IsOnewaySignal(dst_tile, TrackdirToTrack(dst_trackdir))) { - /* If there's a one-way signal not pointing towards us, stop going in this direction. */ - break; - } - } - { - /* We've found ourselves a neighbour :-) */ - AyStarNode *neighbour = &aystar->neighbours[i]; - neighbour->tile = dst_tile; - neighbour->direction = dst_trackdir; - /* Save user data */ - neighbour->user_data[NPF_NODE_FLAGS] = current->path.node.user_data[NPF_NODE_FLAGS]; - NPFFillTrackdirChoice(neighbour, current); - } - i++; - } - aystar->num_neighbours = i; -} - -/* - * Plan a route to the specified target (which is checked by target_proc), - * from start1 and if not nullptr, from start2 as well. The type of transport we - * are checking is in type. reverse_penalty is applied to all routes that - * originate from the second start node. - * When we are looking for one specific target (optionally multiple tiles), we - * should use a good heuristic to perform aystar search. When we search for - * multiple targets that are spread around, we should perform a breadth first - * search by specifying CalcZero as our heuristic. - */ -static NPFFoundTargetData NPFRouteInternal(AyStarNode *start1, bool ignore_start_tile1, AyStarNode *start2, bool ignore_start_tile2, NPFFindStationOrTileData *target, AyStar_EndNodeCheck target_proc, AyStar_CalculateH heuristic_proc, AyStarUserData *user, uint reverse_penalty, bool ignore_reserved = false, int max_penalty = 0) -{ - /* Initialize procs */ - _npf_aystar.max_path_cost = max_penalty; - _npf_aystar.CalculateH = heuristic_proc; - _npf_aystar.EndNodeCheck = target_proc; - _npf_aystar.FoundEndNode = NPFSaveTargetData; - _npf_aystar.GetNeighbours = NPFFollowTrack; - switch (user->type) { - default: NOT_REACHED(); - case TRANSPORT_RAIL: _npf_aystar.CalculateG = NPFRailPathCost; break; - case TRANSPORT_ROAD: _npf_aystar.CalculateG = NPFRoadPathCost; break; - case TRANSPORT_WATER: _npf_aystar.CalculateG = NPFWaterPathCost; break; - } - - /* Initialize Start Node(s) */ - start1->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; - start1->user_data[NPF_NODE_FLAGS] = 0; - NPFSetFlag(start1, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile1); - NPFSetFlag(start1, NPF_FLAG_IGNORE_RESERVED, ignore_reserved); - _npf_aystar.AddStartNode(start1, 0); - if (start2 != nullptr) { - start2->user_data[NPF_TRACKDIR_CHOICE] = INVALID_TRACKDIR; - start2->user_data[NPF_NODE_FLAGS] = 0; - NPFSetFlag(start2, NPF_FLAG_IGNORE_START_TILE, ignore_start_tile2); - NPFSetFlag(start2, NPF_FLAG_REVERSE, true); - NPFSetFlag(start2, NPF_FLAG_IGNORE_RESERVED, ignore_reserved); - _npf_aystar.AddStartNode(start2, reverse_penalty); - } - - /* Initialize result */ - NPFFoundTargetData result; - result.best_bird_dist = UINT_MAX; - result.best_path_dist = UINT_MAX; - result.best_trackdir = INVALID_TRACKDIR; - result.node.tile = INVALID_TILE; - result.res_okay = false; - _npf_aystar.user_path = &result; - - /* Initialize target */ - _npf_aystar.user_target = target; - - /* Initialize user_data */ - _npf_aystar.user_data = user; - - /* We will limit the number of nodes for now, until we have a better - * solution to really fix performance */ - _npf_aystar.max_search_nodes = _settings_game.pf.npf.npf_max_search_nodes; - - /* GO! */ - [[maybe_unused]] int r = _npf_aystar.Main(); - assert(r != AYSTAR_STILL_BUSY); - - if (result.best_bird_dist != 0) { - if (target != nullptr) { - Debug(npf, 1, "Could not find route to tile 0x{:X} from 0x{:X}.", target->dest_coords, start1->tile); - } else { - /* Assumption: target == nullptr, so we are looking for a depot */ - Debug(npf, 1, "Could not find route to a depot from tile 0x{:X}.", start1->tile); - } - - } - return result; -} - -/* Will search as below, but with two start nodes, the second being the - * reverse. Look at the NPF_FLAG_REVERSE flag in the result node to see which - * direction was taken (NPFGetFlag(result.node, NPF_FLAG_REVERSE)) */ -static NPFFoundTargetData NPFRouteToStationOrTileTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData *target, AyStarUserData *user) -{ - AyStarNode start1; - AyStarNode start2; - - start1.tile = tile1; - start2.tile = tile2; - start1.direction = trackdir1; - start2.direction = trackdir2; - - return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : nullptr), ignore_start_tile2, target, NPFFindStationOrTile, NPFCalcStationOrTileHeuristic, user, 0); -} - -/* Will search from the given tile and direction, for a route to the given - * station for the given transport type. See the declaration of - * NPFFoundTargetData above for the meaning of the result. */ -static NPFFoundTargetData NPFRouteToStationOrTile(TileIndex tile, Trackdir trackdir, bool ignore_start_tile, NPFFindStationOrTileData *target, AyStarUserData *user) -{ - return NPFRouteToStationOrTileTwoWay(tile, trackdir, ignore_start_tile, INVALID_TILE, INVALID_TRACKDIR, false, target, user); -} - -/* Search using breadth first. Good for little track choice and inaccurate - * heuristic, such as railway/road with two start nodes, the second being the reverse. Call - * NPFGetFlag(result.node, NPF_FLAG_REVERSE) to see from which node the path - * originated. All paths from the second node will have the given - * reverse_penalty applied (NPF_TILE_LENGTH is the equivalent of one full - * tile). - */ -static NPFFoundTargetData NPFRouteToDepotBreadthFirstTwoWay(TileIndex tile1, Trackdir trackdir1, bool ignore_start_tile1, TileIndex tile2, Trackdir trackdir2, bool ignore_start_tile2, NPFFindStationOrTileData *target, AyStarUserData *user, uint reverse_penalty, int max_penalty) -{ - AyStarNode start1; - AyStarNode start2; - - start1.tile = tile1; - start2.tile = tile2; - start1.direction = trackdir1; - start2.direction = trackdir2; - - /* perform a breadth first search. Target is nullptr, - * since we are just looking for any depot...*/ - return NPFRouteInternal(&start1, ignore_start_tile1, (IsValidTile(tile2) ? &start2 : nullptr), ignore_start_tile2, target, NPFFindDepot, NPFCalcZero, user, reverse_penalty, false, max_penalty); -} - -void InitializeNPF() -{ - static bool first_init = true; - if (first_init) { - first_init = false; - _npf_aystar.Init(NPFHash, NPF_HASH_SIZE); - } else { - _npf_aystar.Clear(); - } - _npf_aystar.loops_per_tick = 0; - _npf_aystar.max_path_cost = 0; -} - -static void NPFFillWithOrderData(NPFFindStationOrTileData *fstd, const Vehicle *v, bool reserve_path = false) -{ - /* Ships don't really reach their stations, but the tile in front. So don't - * save the station id for ships. For roadvehs we don't store it either, - * because multistop depends on vehicles actually reaching the exact - * dest_tile, not just any stop of that station. - * So only for train orders to stations we fill fstd->station_index, for all - * others only dest_coords */ - if (v->current_order.IsType(OT_GOTO_STATION) || v->current_order.IsType(OT_GOTO_WAYPOINT)) { - fstd->station_index = v->current_order.GetDestination(); - if (v->type == VEH_TRAIN) { - fstd->station_type = v->current_order.IsType(OT_GOTO_STATION) ? STATION_RAIL : STATION_WAYPOINT; - } else if (v->type == VEH_ROAD) { - fstd->station_type = RoadVehicle::From(v)->IsBus() ? STATION_BUS : STATION_TRUCK; - } else if (v->type == VEH_SHIP) { - fstd->station_type = v->current_order.IsType(OT_GOTO_STATION) ? STATION_DOCK : STATION_BUOY; - } - - fstd->not_articulated = v->type == VEH_ROAD && !RoadVehicle::From(v)->HasArticulatedPart(); - /* Let's take the closest tile of the station as our target for vehicles */ - fstd->dest_coords = CalcClosestStationTile(fstd->station_index, v->tile, fstd->station_type); - } else { - fstd->dest_coords = v->dest_tile; - fstd->station_index = INVALID_STATION; - } - fstd->reserve_path = reserve_path; - fstd->v = v; -} - -/*** Road vehicles ***/ - -FindDepotData NPFRoadVehicleFindNearestDepot(const RoadVehicle *v, int max_penalty) -{ - Trackdir trackdir = v->GetVehicleTrackdir(); - - AyStarUserData user = { v->owner, TRANSPORT_ROAD, RAILTYPES_NONE, v->compatible_roadtypes, GetRoadTramType(v->roadtype) }; - NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, INVALID_TILE, INVALID_TRACKDIR, false, nullptr, &user, 0, max_penalty); - - if (ftd.best_bird_dist != 0) return FindDepotData(); - - /* Found target */ - /* Our caller expects a number of tiles, so we just approximate that - * number by this. It might not be completely what we want, but it will - * work for now :-) We can possibly change this when the old pathfinder - * is removed. */ - return FindDepotData(ftd.node.tile, ftd.best_path_dist); -} - -Trackdir NPFRoadVehicleChooseTrack(const RoadVehicle *v, TileIndex tile, DiagDirection enterdir, bool &path_found) -{ - NPFFindStationOrTileData fstd; - - NPFFillWithOrderData(&fstd, v); - Trackdir trackdir = DiagDirToDiagTrackdir(enterdir); - - AyStarUserData user = { v->owner, TRANSPORT_ROAD, RAILTYPES_NONE, v->compatible_roadtypes, GetRoadTramType(v->roadtype) }; - NPFFoundTargetData ftd = NPFRouteToStationOrTile(tile - TileOffsByDiagDir(enterdir), trackdir, true, &fstd, &user); - - assert(ftd.best_trackdir != INVALID_TRACKDIR); - - /* 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. */ - path_found = (ftd.best_bird_dist == 0); - return ftd.best_trackdir; -} - -/*** Ships ***/ - -Track NPFShipChooseTrack(const Ship *v, bool &path_found) -{ - NPFFindStationOrTileData fstd; - Trackdir trackdir = v->GetVehicleTrackdir(); - assert(trackdir != INVALID_TRACKDIR); // Check that we are not in a depot - - NPFFillWithOrderData(&fstd, v); - - AyStarUserData user = { v->owner, TRANSPORT_WATER, RAILTYPES_NONE, ROADTYPES_NONE, 0 }; - NPFFoundTargetData ftd = NPFRouteToStationOrTile(v->tile, trackdir, true, &fstd, &user); - - assert(ftd.best_trackdir != INVALID_TRACKDIR); - - /* 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. */ - path_found = (ftd.best_bird_dist == 0); - return TrackdirToTrack(ftd.best_trackdir); -} - -bool NPFShipCheckReverse(const Ship *v, Trackdir *best_td) -{ - NPFFindStationOrTileData fstd; - NPFFoundTargetData ftd; - - NPFFillWithOrderData(&fstd, v); - - Trackdir trackdir = v->GetVehicleTrackdir(); - Trackdir trackdir_rev = ReverseTrackdir(trackdir); - assert(trackdir != INVALID_TRACKDIR); - assert(trackdir_rev != INVALID_TRACKDIR); - - AyStarUserData user = { v->owner, TRANSPORT_WATER, RAILTYPES_NONE, ROADTYPES_NONE, 0 }; - if (best_td != nullptr) { - DiagDirection entry = ReverseDiagDir(VehicleExitDir(v->direction, v->state)); - TrackdirBits rtds = DiagdirReachesTrackdirs(entry) & TrackStatusToTrackdirBits(GetTileTrackStatus(v->tile, TRANSPORT_WATER, 0, entry)); - Trackdir best = (Trackdir)FindFirstBit(rtds); - rtds = KillFirstBit(rtds); - if (rtds == TRACKDIR_BIT_NONE) return false; /* At most one choice. */ - for (; rtds != TRACKDIR_BIT_NONE; rtds = KillFirstBit(rtds)) { - Trackdir td = (Trackdir)FindFirstBit(rtds); - ftd = NPFRouteToStationOrTileTwoWay(v->tile, best, false, v->tile, td, false, &fstd, &user); - if (ftd.best_bird_dist == 0 && NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)) best = td; - } - if (ftd.best_bird_dist == 0) { - *best_td = best; - return true; - } - } else { - ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, false, v->tile, trackdir_rev, false, &fstd, &user); - } - /* If we didn't find anything, just keep on going straight ahead, otherwise take the reverse flag */ - return ftd.best_bird_dist == 0 && NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE); -} - -/*** Trains ***/ - -FindDepotData NPFTrainFindNearestDepot(const Train *v, int max_penalty) -{ - const Train *last = v->Last(); - Trackdir trackdir = v->GetVehicleTrackdir(); - Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir()); - NPFFindStationOrTileData fstd; - fstd.v = v; - fstd.reserve_path = false; - - assert(trackdir != INVALID_TRACKDIR); - AyStarUserData user = { v->owner, TRANSPORT_RAIL, v->compatible_railtypes, ROADTYPES_NONE, 0 }; - NPFFoundTargetData ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, &fstd, &user, NPF_INFINITE_PENALTY, max_penalty); - if (ftd.best_bird_dist != 0) return FindDepotData(); - - /* Found target */ - /* Our caller expects a number of tiles, so we just approximate that - * number by this. It might not be completely what we want, but it will - * work for now :-) We can possibly change this when the old pathfinder - * is removed. */ - return FindDepotData(ftd.node.tile, ftd.best_path_dist, NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE)); -} - -bool NPFTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir trackdir, bool override_railtype) -{ - assert(v->type == VEH_TRAIN); - - NPFFindStationOrTileData fstd; - fstd.v = v; - fstd.reserve_path = true; - - AyStarNode start1; - start1.tile = tile; - start1.direction = trackdir; - - RailTypes railtypes = v->compatible_railtypes; - if (override_railtype) railtypes |= GetRailTypeInfo(v->railtype)->compatible_railtypes; - - /* perform a breadth first search. Target is nullptr, - * since we are just looking for any safe tile...*/ - AyStarUserData user = { v->owner, TRANSPORT_RAIL, railtypes, ROADTYPES_NONE, 0 }; - return NPFRouteInternal(&start1, true, nullptr, false, &fstd, NPFFindSafeTile, NPFCalcZero, &user, 0, true).res_okay; -} - -bool NPFTrainCheckReverse(const Train *v) -{ - NPFFindStationOrTileData fstd; - NPFFoundTargetData ftd; - const Train *last = v->Last(); - - NPFFillWithOrderData(&fstd, v); - - Trackdir trackdir = v->GetVehicleTrackdir(); - Trackdir trackdir_rev = ReverseTrackdir(last->GetVehicleTrackdir()); - assert(trackdir != INVALID_TRACKDIR); - assert(trackdir_rev != INVALID_TRACKDIR); - - AyStarUserData user = { v->owner, TRANSPORT_RAIL, v->compatible_railtypes, ROADTYPES_NONE, 0 }; - ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, false, last->tile, trackdir_rev, false, &fstd, &user); - /* If we didn't find anything, just keep on going straight ahead, otherwise take the reverse flag */ - return ftd.best_bird_dist == 0 && NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE); -} - -Track NPFTrainChooseTrack(const Train *v, bool &path_found, bool reserve_track, struct PBSTileInfo *target) -{ - NPFFindStationOrTileData fstd; - NPFFillWithOrderData(&fstd, v, reserve_track); - - PBSTileInfo origin = FollowTrainReservation(v); - assert(IsValidTrackdir(origin.trackdir)); - - AyStarUserData user = { v->owner, TRANSPORT_RAIL, v->compatible_railtypes, ROADTYPES_NONE, 0 }; - NPFFoundTargetData ftd = NPFRouteToStationOrTile(origin.tile, origin.trackdir, true, &fstd, &user); - - if (target != nullptr) { - target->tile = ftd.node.tile; - target->trackdir = ftd.node.direction; - target->okay = ftd.res_okay; - } - - assert(ftd.best_trackdir != INVALID_TRACKDIR); - - /* 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. */ - path_found = (ftd.best_bird_dist == 0); - /* Discard enterdir information, making it a normal track */ - return TrackdirToTrack(ftd.best_trackdir); -} diff --git a/src/pathfinder/npf/npf_func.h b/src/pathfinder/npf/npf_func.h deleted file mode 100644 index 9183f76ed5..0000000000 --- a/src/pathfinder/npf/npf_func.h +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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 . - */ - -/** @file npf_func.h Functions to access the new pathfinder. */ - -#ifndef NPF_FUNC_H -#define NPF_FUNC_H - -#include "../../track_type.h" -#include "../../direction_type.h" -#include "../../vehicle_type.h" -#include "../pathfinder_type.h" - -/** - * Used when user sends road vehicle to the nearest depot or if road vehicle needs servicing using NPF. - * @param v vehicle that needs to go to some depot - * @param max_penalty max distance (in pathfinder penalty) from the current vehicle position - * (used also as optimization - the pathfinder can stop path finding if max_penalty - * was reached and no depot was seen) - * @return the data about the depot - */ -FindDepotData NPFRoadVehicleFindNearestDepot(const RoadVehicle *v, int max_penalty); - -/** - * Finds the best path for given road vehicle using NPF. - * @param v the RV that needs to find a path - * @param tile the tile to find the path from (should be next tile the RV is about to enter) - * @param enterdir diagonal direction which the RV will enter this new tile from - * @param path_found [out] Whether a path has been found (true) or has been guessed (false) - * @return the best trackdir for next turn or INVALID_TRACKDIR if the path could not be found - */ -Trackdir NPFRoadVehicleChooseTrack(const RoadVehicle *v, TileIndex tile, DiagDirection enterdir, bool &path_found); - -/** - * Finds the best path for given ship using NPF. - * @param v the ship that needs to find a path - * @param path_found [out] Whether a path has been found (true) or has been guessed (false) - * @return the best trackdir for next turn or INVALID_TRACK if the path could not be found - */ -Track NPFShipChooseTrack(const Ship *v, bool &path_found); - -/** - * Returns true if it is better to reverse the ship before leaving depot using NPF. - * @param v the ship leaving the depot - * @param trackdir [out] the best of all possible reversed trackdirs - * @return true if reversing is better - */ -bool NPFShipCheckReverse(const Ship *v, Trackdir *trackdir); - -/** - * Used when user sends train to the nearest depot or if train needs servicing using NPF - * @param v train that needs to go to some depot - * @param max_penalty max max_penalty (in pathfinder penalty) from the current train position - * (used also as optimization - the pathfinder can stop path finding if max_penalty - * was reached and no depot was seen) - * @return the data about the depot - */ -FindDepotData NPFTrainFindNearestDepot(const Train *v, int max_penalty); - -/** - * Try to extend the reserved path of a train to the nearest safe tile using NPF. - * - * @param v The train that needs to find a safe tile. - * @param tile Last tile of the current reserved path. - * @param td Last trackdir of the current reserved path. - * @param override_railtype Should all physically compatible railtypes be searched, even if the vehicle can't run on them on its own? - * @return True if the path could be extended to a safe tile. - */ -bool NPFTrainFindNearestSafeTile(const Train *v, TileIndex tile, Trackdir td, bool override_railtype); - -/** - * Returns true if it is better to reverse the train before leaving station using NPF. - * @param v the train leaving the station - * @return true if reversing is better - */ -bool NPFTrainCheckReverse(const Train *v); - -/** - * Finds the best path for given train using NPF. - * @param v the train that needs to find a path - * @param path_found [out] Whether a path has been found (true) or has been guessed (false) - * @param reserve_track indicates whether YAPF should try to reserve the found path - * @param target [out] the target tile of the reservation, free is set to true if path was reserved - * @return the best track for next turn - */ -Track NPFTrainChooseTrack(const Train *v, bool &path_found, bool reserve_track, struct PBSTileInfo *target); - -#endif /* NPF_FUNC_H */ diff --git a/src/pathfinder/pathfinder_type.h b/src/pathfinder/pathfinder_type.h index a7e6228a93..7f3a3f474b 100644 --- a/src/pathfinder/pathfinder_type.h +++ b/src/pathfinder/pathfinder_type.h @@ -13,18 +13,6 @@ #include "../tile_type.h" #include "npf/aystar.h" -/** Length (penalty) of one tile with NPF */ -static const int NPF_TILE_LENGTH = 100; - -/** - * This penalty is the equivalent of "infinite", which means that paths that - * get this penalty will be chosen, but only if there is no other route - * without it. Be careful with not applying this penalty too often, or the - * total path cost might overflow. - */ -static const int NPF_INFINITE_PENALTY = 1000 * NPF_TILE_LENGTH; - - /** Length (penalty) of one tile with YAPF */ static const int YAPF_TILE_LENGTH = 100; diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 734059d580..a638f379fe 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -12,7 +12,6 @@ #include "command_func.h" #include "error_func.h" #include "news_func.h" -#include "pathfinder/npf/npf_func.h" #include "station_base.h" #include "company_func.h" #include "articulated_vehicles.h" @@ -342,12 +341,7 @@ static FindDepotData FindClosestRoadDepot(const RoadVehicle *v, int max_distance { if (IsRoadDepotTile(v->tile)) return FindDepotData(v->tile, 0); - switch (_settings_game.pf.pathfinder_for_roadvehs) { - case VPF_NPF: return NPFRoadVehicleFindNearestDepot(v, max_distance); - case VPF_YAPF: return YapfRoadVehicleFindNearestDepot(v, max_distance); - - default: NOT_REACHED(); - } + return YapfRoadVehicleFindNearestDepot(v, max_distance); } ClosestDepot RoadVehicle::FindClosestDepot() @@ -989,12 +983,8 @@ static Trackdir RoadFindPathToDest(RoadVehicle *v, TileIndex tile, DiagDirection } } - switch (_settings_game.pf.pathfinder_for_roadvehs) { - case VPF_NPF: best_track = NPFRoadVehicleChooseTrack(v, tile, enterdir, path_found); break; - case VPF_YAPF: best_track = YapfRoadVehicleChooseTrack(v, tile, enterdir, trackdirs, path_found, v->path); break; + best_track = YapfRoadVehicleChooseTrack(v, tile, enterdir, trackdirs, path_found, v->path); - default: NOT_REACHED(); - } v->HandlePathfindingResult(path_found); found_best_track:; @@ -1683,12 +1673,7 @@ static void CheckIfRoadVehNeedsService(RoadVehicle *v) return; } - uint max_penalty; - switch (_settings_game.pf.pathfinder_for_roadvehs) { - case VPF_NPF: max_penalty = _settings_game.pf.npf.maximum_go_to_depot_penalty; break; - case VPF_YAPF: max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty; break; - default: NOT_REACHED(); - } + uint max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty; FindDepotData rfdd = FindClosestRoadDepot(v, max_penalty); /* Only go to the depot if it is not too far out of our way. */ diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 43f9c79253..e3748e9b64 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -1889,25 +1889,6 @@ bool AfterLoadGame() if (!Company::IsValidID(GetTileOwner(t))) FixOwnerOfRailTrack(t); } } - - /* Convert old PF settings to new */ - if (_settings_game.pf.yapf.rail_use_yapf || IsSavegameVersionBefore(SLV_28)) { - _settings_game.pf.pathfinder_for_trains = VPF_YAPF; - } else { - _settings_game.pf.pathfinder_for_trains = VPF_NPF; - } - - if (_settings_game.pf.yapf.road_use_yapf || IsSavegameVersionBefore(SLV_28)) { - _settings_game.pf.pathfinder_for_roadvehs = VPF_YAPF; - } else { - _settings_game.pf.pathfinder_for_roadvehs = VPF_NPF; - } - - if (_settings_game.pf.yapf.ship_use_yapf) { - _settings_game.pf.pathfinder_for_ships = VPF_YAPF; - } else { - _settings_game.pf.pathfinder_for_ships = VPF_NPF; - } } if (IsSavegameVersionBefore(SLV_88)) { diff --git a/src/saveload/compat/settings_sl_compat.h b/src/saveload/compat/settings_sl_compat.h index 84a0464c71..37005b98e2 100644 --- a/src/saveload/compat/settings_sl_compat.h +++ b/src/saveload/compat/settings_sl_compat.h @@ -95,13 +95,9 @@ const SaveLoadCompat _settings_sl_compat[] = { SLC_VAR("vehicle.smoke_amount"), SLC_NULL(1, SL_MIN_VERSION, SLV_159), SLC_VAR("pf.roadveh_queue"), - SLC_VAR("pf.new_pathfinding_all"), - SLC_VAR("pf.yapf.ship_use_yapf"), - SLC_VAR("pf.yapf.road_use_yapf"), - SLC_VAR("pf.yapf.rail_use_yapf"), - SLC_VAR("pf.pathfinder_for_trains"), - SLC_VAR("pf.pathfinder_for_roadvehs"), - SLC_VAR("pf.pathfinder_for_ships"), + SLC_NULL(1, SL_MIN_VERSION, SLV_87), + SLC_NULL(3, SLV_28, SLV_87), + SLC_NULL(3, SLV_87, SLV_TABLE_CHUNKS), SLC_VAR("vehicle.never_expire_vehicles"), SLC_VAR("vehicle.max_trains"), SLC_VAR("vehicle.max_roadveh"), @@ -183,24 +179,12 @@ const SaveLoadCompat _settings_sl_compat[] = { SLC_VAR("pf.reserve_paths"), SLC_VAR("pf.path_backoff_interval"), SLC_NULL(3, SL_MIN_VERSION, SLV_REMOVE_OPF), - SLC_VAR("pf.npf.npf_max_search_nodes"), - SLC_VAR("pf.npf.npf_rail_firstred_penalty"), - SLC_VAR("pf.npf.npf_rail_firstred_exit_penalty"), - SLC_VAR("pf.npf.npf_rail_lastred_penalty"), - SLC_VAR("pf.npf.npf_rail_station_penalty"), - SLC_VAR("pf.npf.npf_rail_slope_penalty"), - SLC_VAR("pf.npf.npf_rail_curve_penalty"), - SLC_VAR("pf.npf.npf_rail_depot_reverse_penalty"), - SLC_VAR("pf.npf.npf_rail_pbs_cross_penalty"), - SLC_VAR("pf.npf.npf_rail_pbs_signal_back_penalty"), - SLC_VAR("pf.npf.npf_buoy_penalty"), - SLC_VAR("pf.npf.npf_water_curve_penalty"), - SLC_VAR("pf.npf.npf_road_curve_penalty"), - SLC_VAR("pf.npf.npf_crossing_penalty"), - SLC_VAR("pf.npf.npf_road_drive_through_penalty"), - SLC_VAR("pf.npf.npf_road_dt_occupied_penalty"), - SLC_VAR("pf.npf.npf_road_bay_occupied_penalty"), - SLC_VAR("pf.npf.maximum_go_to_depot_penalty"), + SLC_NULL(32, SL_MIN_VERSION, SLV_TABLE_CHUNKS), + SLC_NULL(8, SLV_100, SLV_TABLE_CHUNKS), + SLC_NULL(16, SL_MIN_VERSION, SLV_TABLE_CHUNKS), + SLC_NULL(4, SLV_47, SLV_TABLE_CHUNKS), + SLC_NULL(8, SLV_130, SLV_TABLE_CHUNKS), + SLC_NULL(4, SLV_131, SLV_TABLE_CHUNKS), SLC_VAR("pf.yapf.disable_node_optimization"), SLC_VAR("pf.yapf.max_search_nodes"), SLC_VAR("pf.yapf.rail_firstred_twoway_eol"), diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index 793b938e06..0d2df3fcba 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -2118,12 +2118,9 @@ static SettingsContainer &GetSettingsTree() SettingsPage *routing = vehicles->Add(new SettingsPage(STR_CONFIG_SETTING_VEHICLES_ROUTING)); { routing->Add(new SettingEntry("vehicle.road_side")); - routing->Add(new SettingEntry("pf.pathfinder_for_trains")); routing->Add(new SettingEntry("difficulty.line_reverse_mode")); routing->Add(new SettingEntry("pf.reverse_at_signals")); routing->Add(new SettingEntry("pf.forbid_90_deg")); - routing->Add(new SettingEntry("pf.pathfinder_for_roadvehs")); - routing->Add(new SettingEntry("pf.pathfinder_for_ships")); } SettingsPage *orders = vehicles->Add(new SettingsPage(STR_CONFIG_SETTING_VEHICLES_ORDERS)); diff --git a/src/settings_type.h b/src/settings_type.h index 017a74664e..438e1f996d 100644 --- a/src/settings_type.h +++ b/src/settings_type.h @@ -412,42 +412,11 @@ struct ScriptSettings { uint32_t script_max_memory_megabytes; ///< limit on memory a single script instance may have allocated }; -/** Settings related to the new pathfinder. */ -struct NPFSettings { - /** - * The maximum amount of search nodes a single NPF run should take. This - * limit should make sure performance stays at acceptable levels at the cost - * of not being perfect anymore. - */ - uint32_t npf_max_search_nodes; - uint32_t maximum_go_to_depot_penalty; ///< What is the maximum penalty that may be endured for going to a depot - - uint32_t npf_rail_firstred_penalty; ///< the penalty for when the first signal is red (and it is not an exit or combo signal) - uint32_t npf_rail_firstred_exit_penalty; ///< the penalty for when the first signal is red (and it is an exit or combo signal) - uint32_t npf_rail_lastred_penalty; ///< the penalty for when the last signal is red - uint32_t npf_rail_station_penalty; ///< the penalty for station tiles - uint32_t npf_rail_slope_penalty; ///< the penalty for sloping upwards - uint32_t npf_rail_curve_penalty; ///< the penalty for curves - uint32_t npf_rail_depot_reverse_penalty; ///< the penalty for reversing in depots - uint32_t npf_rail_pbs_cross_penalty; ///< the penalty for crossing a reserved rail track - uint32_t npf_rail_pbs_signal_back_penalty; ///< the penalty for passing a pbs signal from the backside - uint32_t npf_buoy_penalty; ///< the penalty for going over (through) a buoy - uint32_t npf_water_curve_penalty; ///< the penalty for curves - uint32_t npf_road_curve_penalty; ///< the penalty for curves - uint32_t npf_crossing_penalty; ///< the penalty for level crossings - uint32_t npf_road_drive_through_penalty; ///< the penalty for going through a drive-through road stop - uint32_t npf_road_dt_occupied_penalty; ///< the penalty multiplied by the fill percentage of a drive-through road stop - uint32_t npf_road_bay_occupied_penalty; ///< the penalty multiplied by the fill percentage of a road bay -}; - /** Settings related to the yet another pathfinder. */ struct YAPFSettings { bool disable_node_optimization; ///< whether to use exit-dir instead of trackdir in node key uint32_t max_search_nodes; ///< stop path-finding when this number of nodes visited uint32_t maximum_go_to_depot_penalty; ///< What is the maximum penalty that may be endured for going to a depot - bool ship_use_yapf; ///< use YAPF for ships - bool road_use_yapf; ///< use YAPF for road - bool rail_use_yapf; ///< use YAPF for rail uint32_t road_slope_penalty; ///< penalty for up-hill slope uint32_t road_curve_penalty; ///< penalty for curves uint32_t road_crossing_penalty; ///< penalty for level crossing @@ -484,11 +453,6 @@ struct YAPFSettings { /** Settings related to all pathfinders. */ struct PathfinderSettings { - uint8_t pathfinder_for_trains; ///< the pathfinder to use for trains - uint8_t pathfinder_for_roadvehs; ///< the pathfinder to use for roadvehicles - uint8_t pathfinder_for_ships; ///< the pathfinder to use for ships - bool new_pathfinding_all; ///< use the newest pathfinding algorithm for all - bool roadveh_queue; ///< buggy road vehicle queueing bool forbid_90_deg; ///< forbid trains to make 90 deg turns @@ -500,7 +464,6 @@ struct PathfinderSettings { uint8_t wait_for_pbs_path; ///< how long to wait for a path reservation. uint8_t path_backoff_interval; ///< ticks between checks for a free path. - NPFSettings npf; ///< pathfinder settings for the new pathfinder YAPFSettings yapf; ///< pathfinder settings for the yet another pathfinder }; diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index d674d4d919..dcd2f6825f 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -13,7 +13,6 @@ #include "timetable.h" #include "news_func.h" #include "company_func.h" -#include "pathfinder/npf/npf_func.h" #include "depot_base.h" #include "station_base.h" #include "newgrf_engine.h" @@ -210,12 +209,7 @@ static void CheckIfShipNeedsService(Vehicle *v) return; } - 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(); - } + uint max_distance = _settings_game.pf.yapf.maximum_go_to_depot_penalty / YAPF_TILE_LENGTH; const Depot *depot = FindClosestShipDepot(v, max_distance); @@ -376,13 +370,7 @@ static Vehicle *EnsureNoMovingShipProc(Vehicle *v, void *) static bool CheckReverseShip(const Ship *v, Trackdir *trackdir = nullptr) { /* Ask pathfinder for best direction */ - bool reverse = false; - switch (_settings_game.pf.pathfinder_for_ships) { - case VPF_NPF: reverse = NPFShipCheckReverse(v, trackdir); break; - case VPF_YAPF: reverse = YapfShipCheckReverse(v, trackdir); break; - default: NOT_REACHED(); - } - return reverse; + return YapfShipCheckReverse(v, trackdir); } static bool CheckShipLeaveDepot(Ship *v) @@ -530,11 +518,7 @@ static Track ChooseShipTrack(Ship *v, TileIndex tile, TrackBits tracks) v->path.clear(); } - switch (_settings_game.pf.pathfinder_for_ships) { - case VPF_NPF: track = NPFShipChooseTrack(v, path_found); break; - case VPF_YAPF: track = YapfShipChooseTrack(v, tile, path_found, v->path); break; - default: NOT_REACHED(); - } + track = YapfShipChooseTrack(v, tile, path_found, v->path); } v->HandlePathfindingResult(path_found); diff --git a/src/table/settings/pathfinding_settings.ini b/src/table/settings/pathfinding_settings.ini index 105fdc263f..ac83c763c9 100644 --- a/src/table/settings/pathfinding_settings.ini +++ b/src/table/settings/pathfinding_settings.ini @@ -51,76 +51,6 @@ var = pf.roadveh_queue def = true cat = SC_EXPERT -[SDT_BOOL] -var = pf.new_pathfinding_all -to = SLV_87 -def = false -cat = SC_EXPERT - -[SDT_BOOL] -var = pf.yapf.ship_use_yapf -from = SLV_28 -to = SLV_87 -def = false -cat = SC_EXPERT - -[SDT_BOOL] -var = pf.yapf.road_use_yapf -from = SLV_28 -to = SLV_87 -def = true -cat = SC_EXPERT - -[SDT_BOOL] -var = pf.yapf.rail_use_yapf -from = SLV_28 -to = SLV_87 -def = true -cat = SC_EXPERT - -[SDT_VAR] -var = pf.pathfinder_for_trains -type = SLE_UINT8 -from = SLV_87 -flags = SF_GUI_DROPDOWN -def = 2 -min = 1 -max = 2 -interval = 1 -str = STR_CONFIG_SETTING_PATHFINDER_FOR_TRAINS -strhelp = STR_CONFIG_SETTING_PATHFINDER_FOR_TRAINS_HELPTEXT -strval = STR_CONFIG_SETTING_PATHFINDER_NPF -cat = SC_EXPERT - -[SDT_VAR] -var = pf.pathfinder_for_roadvehs -type = SLE_UINT8 -from = SLV_87 -flags = SF_GUI_DROPDOWN -def = 2 -min = 1 -max = 2 -interval = 1 -str = STR_CONFIG_SETTING_PATHFINDER_FOR_ROAD_VEHICLES -strhelp = STR_CONFIG_SETTING_PATHFINDER_FOR_ROAD_VEHICLES_HELPTEXT -strval = STR_CONFIG_SETTING_PATHFINDER_NPF -cat = SC_EXPERT - -[SDT_VAR] -var = pf.pathfinder_for_ships -type = SLE_UINT8 -from = SLV_87 -flags = SF_GUI_DROPDOWN -def = 2 -min = 1 -max = 2 -interval = 1 -str = STR_CONFIG_SETTING_PATHFINDER_FOR_SHIPS -strhelp = STR_CONFIG_SETTING_PATHFINDER_FOR_SHIPS_HELPTEXT -strval = STR_CONFIG_SETTING_PATHFINDER_NPF -post_cb = InvalidateShipPathCache -cat = SC_EXPERT - [SDT_BOOL] var = pf.reverse_at_signals from = SLV_159 @@ -168,156 +98,6 @@ min = 1 max = 255 cat = SC_EXPERT -[SDT_VAR] -var = pf.npf.npf_max_search_nodes -type = SLE_UINT -def = AYSTAR_DEF_MAX_SEARCH_NODES -min = 500 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_rail_firstred_penalty -type = SLE_UINT -def = 10 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_rail_firstred_exit_penalty -type = SLE_UINT -def = 100 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_rail_lastred_penalty -type = SLE_UINT -def = 10 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_rail_station_penalty -type = SLE_UINT -def = 1 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_rail_slope_penalty -type = SLE_UINT -def = 1 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_rail_curve_penalty -type = SLE_UINT -def = 1 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_rail_depot_reverse_penalty -type = SLE_UINT -def = 50 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_rail_pbs_cross_penalty -type = SLE_UINT -from = SLV_100 -def = 3 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_rail_pbs_signal_back_penalty -type = SLE_UINT -from = SLV_100 -def = 15 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_buoy_penalty -type = SLE_UINT -def = 2 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_water_curve_penalty -type = SLE_UINT -def = 1 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_road_curve_penalty -type = SLE_UINT -def = 1 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_crossing_penalty -type = SLE_UINT -def = 3 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_road_drive_through_penalty -type = SLE_UINT -from = SLV_47 -def = 8 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_road_dt_occupied_penalty -type = SLE_UINT -from = SLV_130 -def = 8 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.npf_road_bay_occupied_penalty -type = SLE_UINT -from = SLV_130 -def = 15 * NPF_TILE_LENGTH -min = 0 -max = 100000 -cat = SC_EXPERT - -[SDT_VAR] -var = pf.npf.maximum_go_to_depot_penalty -type = SLE_UINT -from = SLV_131 -def = 20 * NPF_TILE_LENGTH -min = 0 -max = 1000000 -cat = SC_EXPERT - [SDT_BOOL] var = pf.yapf.disable_node_optimization from = SLV_28 diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 51353c5281..9664204f14 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -12,7 +12,6 @@ #include "articulated_vehicles.h" #include "command_func.h" #include "error_func.h" -#include "pathfinder/npf/npf_func.h" #include "pathfinder/yapf/yapf.hpp" #include "news_func.h" #include "company_func.h" @@ -2171,12 +2170,7 @@ static FindDepotData FindClosestTrainDepot(Train *v, int max_distance) PBSTileInfo origin = FollowTrainReservation(v); if (IsRailDepotTile(origin.tile)) return FindDepotData(origin.tile, 0); - switch (_settings_game.pf.pathfinder_for_trains) { - case VPF_NPF: return NPFTrainFindNearestDepot(v, max_distance); - case VPF_YAPF: return YapfTrainFindNearestDepot(v, max_distance); - - default: NOT_REACHED(); - } + return YapfTrainFindNearestDepot(v, max_distance); } ClosestDepot Train::FindClosestDepot() @@ -2473,13 +2467,7 @@ static const uint8_t _initial_tile_subcoord[6][4][3] = { static Track DoTrainPathfind(const Train *v, TileIndex tile, DiagDirection enterdir, TrackBits tracks, bool &path_found, bool do_track_reservation, PBSTileInfo *dest, TileIndex *final_dest) { if (final_dest != nullptr) *final_dest = INVALID_TILE; - - switch (_settings_game.pf.pathfinder_for_trains) { - case VPF_NPF: return NPFTrainChooseTrack(v, path_found, do_track_reservation, dest); - case VPF_YAPF: return YapfTrainChooseTrack(v, tile, enterdir, tracks, path_found, do_track_reservation, dest, final_dest); - - default: NOT_REACHED(); - } + return YapfTrainChooseTrack(v, tile, enterdir, tracks, path_found, do_track_reservation, dest, final_dest); } /** @@ -2581,12 +2569,7 @@ static PBSTileInfo ExtendTrainReservation(const Train *v, TrackBits *new_tracks, */ static bool TryReserveSafeTrack(const Train *v, TileIndex tile, Trackdir td, bool override_railtype) { - switch (_settings_game.pf.pathfinder_for_trains) { - case VPF_NPF: return NPFTrainFindNearestSafeTile(v, tile, td, override_railtype); - case VPF_YAPF: return YapfTrainFindNearestSafeTile(v, tile, td, override_railtype); - - default: NOT_REACHED(); - } + return YapfTrainFindNearestSafeTile(v, tile, td, override_railtype); } /** This class will save the current order of a vehicle and restore it on destruction. */ @@ -2934,12 +2917,7 @@ static bool CheckReverseTrain(const Train *v) assert(v->track != TRACK_BIT_NONE); - switch (_settings_game.pf.pathfinder_for_trains) { - case VPF_NPF: return NPFTrainCheckReverse(v); - case VPF_YAPF: return YapfTrainCheckReverse(v); - - default: NOT_REACHED(); - } + return YapfTrainCheckReverse(v); } /** @@ -4142,12 +4120,7 @@ static void CheckIfTrainNeedsService(Train *v) return; } - uint max_penalty; - switch (_settings_game.pf.pathfinder_for_trains) { - case VPF_NPF: max_penalty = _settings_game.pf.npf.maximum_go_to_depot_penalty; break; - case VPF_YAPF: max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty; break; - default: NOT_REACHED(); - } + uint max_penalty = _settings_game.pf.yapf.maximum_go_to_depot_penalty; FindDepotData tfdd = FindClosestTrainDepot(v, max_penalty); /* Only go to the depot if it is not too far out of our way. */ diff --git a/src/vehicle_type.h b/src/vehicle_type.h index 89003224e4..71c83d2a38 100644 --- a/src/vehicle_type.h +++ b/src/vehicle_type.h @@ -56,7 +56,7 @@ static const VehicleID INVALID_VEHICLE = 0xFFFFF; ///< Constant representing a n /** Pathfinding option states */ enum VehiclePathFinders { // Original PathFinder (OPF) used to be 0 - VPF_NPF = 1, ///< New PathFinder + // New PathFinder (NPF) used to be 1 VPF_YAPF = 2, ///< Yet Another PathFinder };