/** * Checks whether the 'next' tile is still part of the road same drive through * stop 'rs' in the same direction for the same vehicle. * @param rs the road stop tile to check against * @param next the 'next' tile to check * @return true if the 'next' tile is part of the road stop at 'next'. */ /* static */ bool RoadStop::IsDriveThroughRoadStopContinuation(TileIndex rs, TileIndex next) { return IsTileType(next, MP_STATION) && GetStationIndex(next) == GetStationIndex(rs) && GetStationType(next) == GetStationType(rs) && GetRoadStopDir(next) == GetRoadStopDir(rs) && IsDriveThroughStopTile(next); }
/** * Check whether the given tile is suitable for a waypoint. * @param tile the tile to check for suitability * @param axis the axis of the waypoint * @param waypoint Waypoint the waypoint to check for is already joined to. If we find another waypoint it can join to it will throw an error. */ static CommandCost IsValidTileForWaypoint(TileIndex tile, Axis axis, StationID *waypoint) { /* if waypoint is set, then we have special handling to allow building on top of already existing waypoints. * so waypoint points to INVALID_STATION if we can build on any waypoint. * Or it points to a waypoint if we're only allowed to build on exactly that waypoint. */ if (waypoint != NULL && IsTileType(tile, MP_STATION)) { if (!IsRailWaypoint(tile)) { return ClearTile_Station(tile, DC_AUTO); // get error message } else { StationID wp = GetStationIndex(tile); if (*waypoint == INVALID_STATION) { *waypoint = wp; } else if (*waypoint != wp) { return_cmd_error(STR_ERROR_WAYPOINT_ADJOINS_MORE_THAN_ONE_EXISTING); } } } if (GetAxisForNewWaypoint(tile) != axis) return_cmd_error(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK); Owner owner = GetTileOwner(tile); CommandCost ret = CheckOwnership(owner); if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile); if (ret.Failed()) return ret; Slope tileh = GetTileSlope(tile); if (tileh != SLOPE_FLAT && (!_settings_game.construction.build_on_slopes || IsSteepSlope(tileh) || !(tileh & (0x3 << axis)) || !(tileh & ~(0x3 << axis)))) { return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED); } if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST); return CommandCost(); }
/** * Check whether station tiles of the given station id exist in the given rectangle * @param st_id Station ID to look for in the rectangle * @param left_a Minimal tile X edge of the rectangle * @param top_a Minimal tile Y edge of the rectangle * @param right_a Maximal tile X edge of the rectangle (inclusive) * @param bottom_a Maximal tile Y edge of the rectangle (inclusive) * @return \c true if a station tile with the given \a st_id exists in the rectangle, \c false otherwise */ /* static */ bool StationRect::ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a) { TileArea ta(TileXY(left_a, top_a), TileXY(right_a, bottom_a)); TILE_AREA_LOOP(tile, ta) { if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st_id) return true; } return false; }
inline bool PfDetectDestinationTile(TileIndex tile, Trackdir trackdir) { if (m_dest_station != INVALID_STATION) { return IsTileType(tile, MP_STATION) && GetStationIndex(tile) == m_dest_station && (m_bus ? IsBusStop(tile) : IsTruckStop(tile)) && (m_non_artic || IsDriveThroughStopTile(tile)); } return tile == m_destTile && ((m_destTrackdirs & TrackdirToTrackdirBits(trackdir)) != TRACKDIR_BIT_NONE); }
/** * Check whether station tiles of the given station id exist in the given rectangle * @param st_id Station ID to look for in the rectangle * @param left_a Minimal tile X edge of the rectangle * @param top_a Minimal tile Y edge of the rectangle * @param right_a Maximal tile X edge of the rectangle (inclusive) * @param bottom_a Maximal tile Y edge of the rectangle (inclusive) * @return \c true if a station tile with the given \a st_id exists in the rectangle, \c false otherwise */ /* static */ bool StationRect::ScanForStationTiles(StationID st_id, int left_a, int top_a, int right_a, int bottom_a) { TileIndex top_left = TileXY(left_a, top_a); int width = right_a - left_a + 1; int height = bottom_a - top_a + 1; TILE_LOOP(tile, width, height, top_left) { if (IsTileType(tile, MP_STATION) && GetStationIndex(tile) == st_id) return true; } return false; }
/** * Find the end of a railway station, from the \a tile, in the direction of \a delta. * @param tile Start tile. * @param delta Movement direction. * @param check_type Stop when the custom station type changes. * @param check_axis Stop when the station direction changes. * @return Found end of the railway station. */ static TileIndex FindRailStationEnd(TileIndex tile, TileIndexDiff delta, bool check_type, bool check_axis) { byte orig_type = 0; Axis orig_axis = AXIS_X; StationID sid = GetStationIndex(tile); if (check_type) orig_type = GetCustomStationSpecIndex(tile); if (check_axis) orig_axis = GetRailStationAxis(tile); for (;;) { TileIndex new_tile = TILE_ADD(tile, delta); if (!IsTileType(new_tile, MP_STATION) || GetStationIndex(new_tile) != sid) break; if (!HasStationRail(new_tile)) break; if (check_type && GetCustomStationSpecIndex(new_tile) != orig_type) break; if (check_axis && GetRailStationAxis(new_tile) != orig_axis) break; tile = new_tile; } return tile; }
/** Called by YAPF to detect if node ends in the desired destination */ FORCEINLINE bool PfDetectDestination(TileIndex tile, Trackdir td) { bool bDest; if (m_dest_station_id != INVALID_STATION) { bDest = HasStationTileRail(tile) && (GetStationIndex(tile) == m_dest_station_id) && (GetRailStationTrack(tile) == TrackdirToTrack(td)); } else { bDest = (tile == m_destTile) && ((m_destTrackdirs & TrackdirToTrackdirBits(td)) != TRACKDIR_BIT_NONE); } return bDest; }
ScriptVehicleList_Depot::ScriptVehicleList_Depot(TileIndex tile) { if (!ScriptMap::IsValidTile(tile)) return; DestinationID dest; VehicleType type; switch (GetTileType(tile)) { case MP_STATION: // Aircraft if (!IsAirport(tile)) return; type = VEH_AIRCRAFT; dest = GetStationIndex(tile); break; case MP_RAILWAY: if (!IsRailDepot(tile)) return; type = VEH_TRAIN; dest = GetDepotIndex(tile); break; case MP_ROAD: if (!IsRoadDepot(tile)) return; type = VEH_ROAD; dest = GetDepotIndex(tile); break; case MP_WATER: if (!IsShipDepot(tile)) return; type = VEH_SHIP; dest = GetDepotIndex(tile); break; default: // No depot return; } const Vehicle *v; FOR_ALL_VEHICLES(v) { if ((v->owner == ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY) && v->IsPrimaryVehicle() && v->type == type) { const Order *order; FOR_VEHICLE_ORDERS(v, order) { if (order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == dest) { this->AddItem(v->index); break; } } } } }
/** * Perform all steps to upgrade from the old station buoys to the new version * that uses waypoints. This includes some old saveload mechanics. */ void MoveBuoysToWaypoints() { /* Buoy orders become waypoint orders */ OrderList *ol; FOR_ALL_ORDER_LISTS(ol) { if (ol->GetFirstSharedVehicle()->type != VEH_SHIP) continue; for (Order *o = ol->GetFirstOrder(); o != NULL; o = o->next) UpdateWaypointOrder(o); } Vehicle *v; FOR_ALL_VEHICLES(v) { if (v->type != VEH_SHIP) continue; UpdateWaypointOrder(&v->current_order); } /* Now make the stations waypoints */ Station *st; FOR_ALL_STATIONS(st) { if ((st->had_vehicle_of_type & HVOT_WAYPOINT) == 0) continue; StationID index = st->index; TileIndex xy = st->xy; Town *town = st->town; StringID string_id = st->string_id; char *name = st->name; Date build_date = st->build_date; /* Delete the station, so we can make it a real waypoint. */ delete st; Waypoint *wp = new (index) Waypoint(xy); wp->town = town; wp->string_id = STR_SV_STNAME_BUOY; wp->name = name; wp->delete_ctr = 0; // Just reset delete counter for once. wp->build_date = build_date; wp->owner = OWNER_NONE; if (IsInsideBS(string_id, STR_SV_STNAME_BUOY, 9)) wp->town_cn = string_id - STR_SV_STNAME_BUOY; if (IsBuoyTile(xy) && GetStationIndex(xy) == index) { wp->facilities |= FACIL_DOCK; } wp->rect.BeforeAddTile(xy, StationRect::ADD_FORCE); } }
bool NOAAStore::GetTAF(const char *code, TAF &taf) { #ifndef NDEBUG assert(strlen(code) == 4); for (unsigned i = 0; i < 4; i++) assert(code[i] >= 'A' && code[i] <= 'Z'); #endif unsigned index = GetStationIndex(code); if (index == (unsigned)-1) return false; return GetTAF(index, taf); }
void NOAAStore::RemoveStation(const char *code) { #ifndef NDEBUG assert(strlen(code) == 4); for (unsigned i = 0; i < 4; i++) assert(code[i] >= 'A' && code[i] <= 'Z'); #endif unsigned index = GetStationIndex(code); if (index == (unsigned)-1) return; RemoveStation(index); }
bool NOAAStore::UpdateStation(const char *code, JobRunner &runner) { #ifndef NDEBUG assert(strlen(code) == 4); for (unsigned i = 0; i < 4; i++) assert(code[i] >= 'A' && code[i] <= 'Z'); #endif unsigned index = GetStationIndex(code); if (index == (unsigned)-1) return false; return UpdateStation(index, runner); }
bool NOAAStore::GetParsedMETAR(const char *code, ParsedMETAR &metar) { #ifndef NDEBUG assert(strlen(code) == 4); for (unsigned i = 0; i < 4; i++) assert(code[i] >= 'A' && code[i] <= 'Z'); #endif unsigned index = GetStationIndex(code); if (index == (unsigned)-1) return false; return GetParsedMETAR(index, metar); }
/* Will find a station identified using the NPFFindStationOrTileData */ static int32 NPFFindStationOrTile(AyStar *as, OpenListNode *current) { NPFFindStationOrTileData *fstd = (NPFFindStationOrTileData*)as->user_target; 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 (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; }
/** * Check whether the given tile is suitable for a waypoint. * @param tile the tile to check for suitability * @param axis the axis of the waypoint * @param waypoint Waypoint the waypoint to check for is already joined to. If we find another waypoint it can join to it will throw an error. * @param flags flags for the command */ static CommandCost IsValidTileForWaypoint(TileIndex tile, Axis axis, StationID *waypoint, DoCommandFlag flags) { /* if waypoint is set, then we have special handling to allow building on top of already existing waypoints. * so waypoint points to INVALID_STATION if we can build on any waypoint. * Or it points to a waypoint if we're only allowed to build on exactly that waypoint. */ if (waypoint != NULL && IsTileType(tile, MP_STATION)) { if (!IsRailWaypoint(tile)) { return ClearTile_Station(tile, DC_AUTO); // get error message } else { StationID wp = GetStationIndex(tile); if (*waypoint == INVALID_STATION) { *waypoint = wp; } else if (*waypoint != wp) { return_cmd_error(STR_ERROR_WAYPOINT_ADJOINS_MORE_THAN_ONE_EXISTING); } } } /* When pasting a waypoint, there may be no track yet (it will be placed when DC_EXEC'ing). * Ignore that so we can calculate the cost more precisely. */ bool ignore_lack_of_tracks = (flags & DC_PASTE) && !(flags & DC_EXEC); if (!ignore_lack_of_tracks || IsTileType(tile, MP_RAILWAY)) { if (GetAxisForNewWaypoint(tile) != axis) return_cmd_error(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK); } Owner owner = GetTileOwner(tile); if (!ignore_lack_of_tracks || owner != OWNER_NONE) { CommandCost ret = CheckOwnership(owner); if (ret.Failed()) return ret; } CommandCost ret = EnsureNoVehicleOnGround(tile); if (ret.Failed()) return ret; Slope tileh = GetTileSlope(tile); if (tileh != SLOPE_FLAT && (!_settings_game.construction.build_on_slopes || IsSteepSlope(tileh) || !(tileh & (0x3 << axis)) || !(tileh & ~(0x3 << axis)))) { return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED); } if (IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST); return CommandCost(); }
/* Determine the cost of this node, for railway tracks */ static int32 NPFRailPathCost(AyStar *as, AyStarNode *current, OpenListNode *parent) { TileIndex tile = current->tile; Trackdir trackdir = current->direction; int32 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 */ NPFSetFlag(current, NPF_FLAG_LAST_SIGNAL_RED, true); } 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((Trackdir)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 occurence 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: (%d, %d). Result: %d", TileX(current->tile), TileY(current->tile), cost); return cost; }
/** * Called by YAPF to calculate the cost from the origin to the given node. * Calculates only the cost of given node, adds it to the parent node cost * and stores the result into Node::m_cost member */ inline bool PfCalcCost(Node &n, const TrackFollower *tf) { assert(!n.flags_u.flags_s.m_targed_seen); assert(tf->m_new_tile == n.m_key.m_tile); assert((TrackdirToTrackdirBits(n.m_key.m_td) & tf->m_new_td_bits) != TRACKDIR_BIT_NONE); CPerfStart perf_cost(Yapf().m_perf_cost); /* Does the node have some parent node? */ bool has_parent = (n.m_parent != NULL); /* Do we already have a cached segment? */ CachedData &segment = *n.m_segment; bool is_cached_segment = (segment.m_cost >= 0); int parent_cost = has_parent ? n.m_parent->m_cost : 0; /* Each node cost contains 2 or 3 main components: * 1. Transition cost - cost of the move from previous node (tile): * - curve cost (or zero for straight move) * 2. Tile cost: * - base tile cost * - YAPF_TILE_LENGTH for diagonal tiles * - YAPF_TILE_CORNER_LENGTH for non-diagonal tiles * - tile penalties * - tile slope penalty (upward slopes) * - red signal penalty * - level crossing penalty * - speed-limit penalty (bridges) * - station platform penalty * - penalty for reversing in the depot * - etc. * 3. Extra cost (applies to the last node only) * - last red signal penalty * - penalty for too long or too short platform on the destination station */ int transition_cost = 0; int extra_cost = 0; /* Segment: one or more tiles connected by contiguous tracks of the same type. * Each segment cost includes 'Tile cost' for all its tiles (including the first * and last), and the 'Transition cost' between its tiles. The first transition * cost of segment entry (move from the 'parent' node) is not included! */ int segment_entry_cost = 0; int segment_cost = 0; const Train *v = Yapf().GetVehicle(); /* start at n.m_key.m_tile / n.m_key.m_td and walk to the end of segment */ TILE cur(n.m_key.m_tile, n.m_key.m_td); /* the previous tile will be needed for transition cost calculations */ TILE prev = !has_parent ? TILE() : TILE(n.m_parent->GetLastTile(), n.m_parent->GetLastTrackdir()); EndSegmentReasonBits end_segment_reason = ESRB_NONE; TrackFollower tf_local(v, Yapf().GetCompatibleRailTypes(), &Yapf().m_perf_ts_cost); if (!has_parent) { /* We will jump to the middle of the cost calculator assuming that segment cache is not used. */ assert(!is_cached_segment); /* Skip the first transition cost calculation. */ goto no_entry_cost; } for (;;) { /* Transition cost (cost of the move from previous tile) */ transition_cost = Yapf().CurveCost(prev.td, cur.td); transition_cost += Yapf().SwitchCost(prev.tile, cur.tile, TrackdirToExitdir(prev.td)); /* First transition cost counts against segment entry cost, other transitions * inside segment will come to segment cost (and will be cached) */ if (segment_cost == 0) { /* We just entered the loop. First transition cost goes to segment entry cost)*/ segment_entry_cost = transition_cost; transition_cost = 0; /* It is the right time now to look if we can reuse the cached segment cost. */ if (is_cached_segment) { /* Yes, we already know the segment cost. */ segment_cost = segment.m_cost; /* We know also the reason why the segment ends. */ end_segment_reason = segment.m_end_segment_reason; /* We will need also some information about the last signal (if it was red). */ if (segment.m_last_signal_tile != INVALID_TILE) { assert(HasSignalOnTrackdir(segment.m_last_signal_tile, segment.m_last_signal_td)); SignalState sig_state = GetSignalStateByTrackdir(segment.m_last_signal_tile, segment.m_last_signal_td); bool is_red = (sig_state == SIGNAL_STATE_RED); n.flags_u.flags_s.m_last_signal_was_red = is_red; if (is_red) { n.m_last_red_signal_type = GetSignalType(segment.m_last_signal_tile, TrackdirToTrack(segment.m_last_signal_td)); } } /* No further calculation needed. */ cur = TILE(n.GetLastTile(), n.GetLastTrackdir()); break; } } else { /* Other than first transition cost count as the regular segment cost. */ segment_cost += transition_cost; } no_entry_cost: // jump here at the beginning if the node has no parent (it is the first node) /* All other tile costs will be calculated here. */ segment_cost += Yapf().OneTileCost(cur.tile, cur.td); /* If we skipped some tunnel/bridge/station tiles, add their base cost */ segment_cost += YAPF_TILE_LENGTH * tf->m_tiles_skipped; /* Slope cost. */ segment_cost += Yapf().SlopeCost(cur.tile, cur.td); /* Signal cost (routine can modify segment data). */ segment_cost += Yapf().SignalCost(n, cur.tile, cur.td); /* Reserved tiles. */ segment_cost += Yapf().ReservationCost(n, cur.tile, cur.td, tf->m_tiles_skipped); end_segment_reason = segment.m_end_segment_reason; /* Tests for 'potential target' reasons to close the segment. */ if (cur.tile == prev.tile) { /* Penalty for reversing in a depot. */ assert(IsRailDepot(cur.tile)); segment_cost += Yapf().PfGetSettings().rail_depot_reverse_penalty; /* We will end in this pass (depot is possible target) */ end_segment_reason |= ESRB_DEPOT; } else if (cur.tile_type == MP_STATION && IsRailWaypoint(cur.tile)) { if (v->current_order.IsType(OT_GOTO_WAYPOINT) && GetStationIndex(cur.tile) == v->current_order.GetDestination() && !Waypoint::Get(v->current_order.GetDestination())->IsSingleTile()) { /* 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. */ CFollowTrackRail ft(v); TileIndex t = cur.tile; Trackdir td = cur.td; 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(v, t, td, true, _settings_game.pf.forbid_90_deg)) break; } /* In the case this platform is (possibly) occupied we add penalty so the * other platforms of this waypoint are evaluated as well, i.e. we assume * that there is a red signal in the waypoint when it's occupied. */ if (td == INVALID_TRACKDIR || !IsSafeWaitingPosition(v, t, td, true, _settings_game.pf.forbid_90_deg) || !IsWaitingPositionFree(v, t, td, _settings_game.pf.forbid_90_deg)) { extra_cost += Yapf().PfGetSettings().rail_lastred_penalty; } } /* Waypoint is also a good reason to finish. */ end_segment_reason |= ESRB_WAYPOINT; } else if (tf->m_is_station) { /* Station penalties. */ uint platform_length = tf->m_tiles_skipped + 1; /* We don't know yet if the station is our target or not. Act like * if it is pass-through station (not our destination). */ segment_cost += Yapf().PfGetSettings().rail_station_penalty * platform_length; /* We will end in this pass (station is possible target) */ end_segment_reason |= ESRB_STATION; } else if (TrackFollower::DoTrackMasking() && cur.tile_type == MP_RAILWAY) { /* Searching for a safe tile? */ if (HasSignalOnTrackdir(cur.tile, cur.td) && !IsPbsSignal(GetSignalType(cur.tile, TrackdirToTrack(cur.td)))) { end_segment_reason |= ESRB_SAFE_TILE; } } /* Apply min/max speed penalties only when inside the look-ahead radius. Otherwise * it would cause desync in MP. */ if (n.m_num_signals_passed < m_sig_look_ahead_costs.Size()) { int min_speed = 0; int max_speed = tf->GetSpeedLimit(&min_speed); int max_veh_speed = v->GetDisplayMaxSpeed(); if (max_speed < max_veh_speed) { extra_cost += YAPF_TILE_LENGTH * (max_veh_speed - max_speed) * (4 + tf->m_tiles_skipped) / max_veh_speed; } if (min_speed > max_veh_speed) { extra_cost += YAPF_TILE_LENGTH * (min_speed - max_veh_speed); } } /* Finish if we already exceeded the maximum path cost (i.e. when * searching for the nearest depot). */ if (m_max_cost > 0 && (parent_cost + segment_entry_cost + segment_cost) > m_max_cost) { end_segment_reason |= ESRB_PATH_TOO_LONG; } /* Move to the next tile/trackdir. */ tf = &tf_local; tf_local.Init(v, Yapf().GetCompatibleRailTypes(), &Yapf().m_perf_ts_cost); if (!tf_local.Follow(cur.tile, cur.td)) { assert(tf_local.m_err != TrackFollower::EC_NONE); /* Can't move to the next tile (EOL?). */ if (tf_local.m_err == TrackFollower::EC_RAIL_TYPE) { end_segment_reason |= ESRB_RAIL_TYPE; } else { end_segment_reason |= ESRB_DEAD_END; } if (TrackFollower::DoTrackMasking() && !HasOnewaySignalBlockingTrackdir(cur.tile, cur.td)) { end_segment_reason |= ESRB_SAFE_TILE; } break; } /* Check if the next tile is not a choice. */ if (KillFirstBit(tf_local.m_new_td_bits) != TRACKDIR_BIT_NONE) { /* More than one segment will follow. Close this one. */ end_segment_reason |= ESRB_CHOICE_FOLLOWS; break; } /* Gather the next tile/trackdir/tile_type/rail_type. */ TILE next(tf_local.m_new_tile, (Trackdir)FindFirstBit2x64(tf_local.m_new_td_bits)); if (TrackFollower::DoTrackMasking() && IsTileType(next.tile, MP_RAILWAY)) { if (HasSignalOnTrackdir(next.tile, next.td) && IsPbsSignal(GetSignalType(next.tile, TrackdirToTrack(next.td)))) { /* Possible safe tile. */ end_segment_reason |= ESRB_SAFE_TILE; } else if (HasSignalOnTrackdir(next.tile, ReverseTrackdir(next.td)) && GetSignalType(next.tile, TrackdirToTrack(next.td)) == SIGTYPE_PBS_ONEWAY) { /* Possible safe tile, but not so good as it's the back of a signal... */ end_segment_reason |= ESRB_SAFE_TILE | ESRB_DEAD_END; extra_cost += Yapf().PfGetSettings().rail_lastred_exit_penalty; } } /* Check the next tile for the rail type. */ if (next.rail_type != cur.rail_type) { /* Segment must consist from the same rail_type tiles. */ end_segment_reason |= ESRB_RAIL_TYPE; break; } /* Avoid infinite looping. */ if (next.tile == n.m_key.m_tile && next.td == n.m_key.m_td) { end_segment_reason |= ESRB_INFINITE_LOOP; break; } if (segment_cost > s_max_segment_cost) { /* Potentially in the infinite loop (or only very long segment?). We should * not force it to finish prematurely unless we are on a regular tile. */ if (IsTileType(tf->m_new_tile, MP_RAILWAY)) { end_segment_reason |= ESRB_SEGMENT_TOO_LONG; break; } } /* Any other reason bit set? */ if (end_segment_reason != ESRB_NONE) { break; } /* For the next loop set new prev and cur tile info. */ prev = cur; cur = next; } // for (;;) bool target_seen = false; if ((end_segment_reason & ESRB_POSSIBLE_TARGET) != ESRB_NONE) { /* Depot, station or waypoint. */ if (Yapf().PfDetectDestination(cur.tile, cur.td)) { /* Destination found. */ target_seen = true; } } /* Update the segment if needed. */ if (!is_cached_segment) { /* Write back the segment information so it can be reused the next time. */ segment.m_cost = segment_cost; segment.m_end_segment_reason = end_segment_reason & ESRB_CACHED_MASK; /* Save end of segment back to the node. */ n.SetLastTileTrackdir(cur.tile, cur.td); } /* Do we have an excuse why not to continue pathfinding in this direction? */ if (!target_seen && (end_segment_reason & ESRB_ABORT_PF_MASK) != ESRB_NONE) { /* Reason to not continue. Stop this PF branch. */ return false; } /* Special costs for the case we have reached our target. */ if (target_seen) { n.flags_u.flags_s.m_targed_seen = true; /* Last-red and last-red-exit penalties. */ if (n.flags_u.flags_s.m_last_signal_was_red) { if (n.m_last_red_signal_type == SIGTYPE_EXIT) { /* last signal was red pre-signal-exit */ extra_cost += Yapf().PfGetSettings().rail_lastred_exit_penalty; } else if (!IsPbsSignal(n.m_last_red_signal_type)) { /* Last signal was red, but not exit or path signal. */ extra_cost += Yapf().PfGetSettings().rail_lastred_penalty; } } /* Station platform-length penalty. */ if ((end_segment_reason & ESRB_STATION) != ESRB_NONE) { const BaseStation *st = BaseStation::GetByTile(n.GetLastTile()); assert(st != NULL); uint platform_length = st->GetPlatformLength(n.GetLastTile(), ReverseDiagDir(TrackdirToExitdir(n.GetLastTrackdir()))); /* Reduce the extra cost caused by passing-station penalty (each station receives it in the segment cost). */ extra_cost -= Yapf().PfGetSettings().rail_station_penalty * platform_length; /* Add penalty for the inappropriate platform length. */ extra_cost += PlatformLengthPenalty(platform_length); } } /* total node cost */ n.m_cost = parent_cost + segment_entry_cost + segment_cost + extra_cost; return true; }
/** * Perform all steps to upgrade from the old station buoys to the new version * that uses waypoints. This includes some old saveload mechanics. */ void MoveBuoysToWaypoints() { /* Buoy orders become waypoint orders */ OrderList *ol; FOR_ALL_ORDER_LISTS(ol) { VehicleType vt = ol->GetFirstSharedVehicle()->type; if (vt != VEH_SHIP && vt != VEH_TRAIN) continue; for (Order *o = ol->GetFirstOrder(); o != NULL; o = o->next) UpdateWaypointOrder(o); } Vehicle *v; FOR_ALL_VEHICLES(v) { VehicleType vt = v->type; if (vt != VEH_SHIP && vt != VEH_TRAIN) continue; UpdateWaypointOrder(&v->current_order); } /* Now make the stations waypoints */ Station *st; FOR_ALL_STATIONS(st) { if ((st->had_vehicle_of_type & HVOT_WAYPOINT) == 0) continue; StationID index = st->index; TileIndex xy = st->xy; Town *town = st->town; StringID string_id = st->string_id; char *name = st->name; st->name = NULL; Date build_date = st->build_date; /* TTDPatch could use "buoys with rail station" for rail waypoints */ bool train = st->train_station.tile != INVALID_TILE; TileArea train_st = st->train_station; /* Delete the station, so we can make it a real waypoint. */ delete st; /* Stations and waypoints are in the same pool, so if a station * is deleted there must be place for a Waypoint. */ assert(Waypoint::CanAllocateItem()); Waypoint *wp = new (index) Waypoint(xy); wp->town = town; wp->string_id = train ? STR_SV_STNAME_WAYPOINT : STR_SV_STNAME_BUOY; wp->name = name; wp->delete_ctr = 0; // Just reset delete counter for once. wp->build_date = build_date; wp->owner = train ? GetTileOwner(xy) : OWNER_NONE; if (IsInsideBS(string_id, STR_SV_STNAME_BUOY, 9)) wp->town_cn = string_id - STR_SV_STNAME_BUOY; if (train) { /* When we make a rail waypoint of the station, convert the map as well. */ TILE_AREA_LOOP(t, train_st) { if (!IsTileType(t, MP_STATION) || GetStationIndex(t) != index) continue; SB(_me[t].m6, 3, 3, STATION_WAYPOINT); wp->rect.BeforeAddTile(t, StationRect::ADD_FORCE); } wp->train_station = train_st; wp->facilities |= FACIL_TRAIN; } else if (IsBuoyTile(xy) && GetStationIndex(xy) == index) { wp->rect.BeforeAddTile(xy, StationRect::ADD_FORCE); wp->facilities |= FACIL_DOCK; } }