mirror of
https://github.com/OpenTTD/OpenTTD
synced 2026-01-17 17:32:45 +01:00
Add: Road waypoint functionality
This commit is contained in:
committed by
rubidium42
parent
c6387c7784
commit
9c84e5df3f
@@ -25,6 +25,7 @@
|
||||
#include "string_func.h"
|
||||
#include "company_func.h"
|
||||
#include "newgrf_station.h"
|
||||
#include "newgrf_roadstop.h"
|
||||
#include "company_base.h"
|
||||
#include "water.h"
|
||||
#include "company_gui.h"
|
||||
@@ -68,15 +69,16 @@ void Waypoint::MoveSign(TileIndex new_xy)
|
||||
* @param tile to search from
|
||||
* @param str the string to get the 'type' of
|
||||
* @param cid previous owner of the waypoint
|
||||
* @param is_road whether to find a road waypoint
|
||||
* @return the deleted nearby waypoint
|
||||
*/
|
||||
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile, StringID str, CompanyID cid)
|
||||
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile, StringID str, CompanyID cid, bool is_road)
|
||||
{
|
||||
Waypoint *best = nullptr;
|
||||
uint thres = 8;
|
||||
|
||||
for (Waypoint *wp : Waypoint::Iterate()) {
|
||||
if (!wp->IsInUse() && wp->string_id == str && wp->owner == cid) {
|
||||
if (!wp->IsInUse() && wp->string_id == str && wp->owner == cid && HasBit(wp->waypoint_flags, WPF_ROAD) == is_road) {
|
||||
uint cur_dist = DistanceManhattan(tile, wp->xy);
|
||||
|
||||
if (cur_dist < thres) {
|
||||
@@ -90,13 +92,13 @@ static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile, StringID str, Compan
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the axis for a new waypoint. This means that if it is a valid
|
||||
* Get the axis for a new rail waypoint. This means that if it is a valid
|
||||
* tile to build a waypoint on it returns a valid Axis, otherwise an
|
||||
* invalid one.
|
||||
* @param tile the tile to look at.
|
||||
* @return the axis for the to-be-build waypoint.
|
||||
*/
|
||||
Axis GetAxisForNewWaypoint(TileIndex tile)
|
||||
Axis GetAxisForNewRailWaypoint(TileIndex tile)
|
||||
{
|
||||
/* The axis for rail waypoints is easy. */
|
||||
if (IsRailWaypointTile(tile)) return GetRailStationAxis(tile);
|
||||
@@ -111,6 +113,29 @@ Axis GetAxisForNewWaypoint(TileIndex tile)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the axis for a new road waypoint. This means that if it is a valid
|
||||
* tile to build a waypoint on it returns a valid Axis, otherwise an
|
||||
* invalid one.
|
||||
* @param tile the tile to look at.
|
||||
* @return the axis for the to-be-build waypoint.
|
||||
*/
|
||||
Axis GetAxisForNewRoadWaypoint(TileIndex tile)
|
||||
{
|
||||
/* The axis for existing road waypoints is easy. */
|
||||
if (IsRoadWaypointTile(tile)) return DiagDirToAxis(GetRoadStopDir(tile));
|
||||
|
||||
/* Non-plain road type, no valid axis for waypoints. */
|
||||
if (!IsNormalRoadTile(tile)) return INVALID_AXIS;
|
||||
|
||||
RoadBits bits = GetAllRoadBits(tile);
|
||||
|
||||
if ((bits & ROAD_Y) == 0) return AXIS_X;
|
||||
if ((bits & ROAD_X) == 0) return AXIS_Y;
|
||||
|
||||
return INVALID_AXIS;
|
||||
}
|
||||
|
||||
extern CommandCost ClearTile_Station(TileIndex tile, DoCommandFlag flags);
|
||||
|
||||
/**
|
||||
@@ -137,7 +162,7 @@ static CommandCost IsValidTileForWaypoint(TileIndex tile, Axis axis, StationID *
|
||||
}
|
||||
}
|
||||
|
||||
if (GetAxisForNewWaypoint(tile) != axis) return_cmd_error(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK);
|
||||
if (GetAxisForNewRailWaypoint(tile) != axis) return_cmd_error(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK);
|
||||
|
||||
Owner owner = GetTileOwner(tile);
|
||||
CommandCost ret = CheckOwnership(owner);
|
||||
@@ -156,8 +181,10 @@ static CommandCost IsValidTileForWaypoint(TileIndex tile, Axis axis, StationID *
|
||||
}
|
||||
|
||||
extern void GetStationLayout(uint8_t *layout, uint numtracks, uint plat_len, const StationSpec *statspec);
|
||||
extern CommandCost FindJoiningWaypoint(StationID existing_station, StationID station_to_join, bool adjacent, TileArea ta, Waypoint **wp);
|
||||
extern CommandCost FindJoiningWaypoint(StationID existing_station, StationID station_to_join, bool adjacent, TileArea ta, Waypoint **wp, bool is_road);
|
||||
extern CommandCost CanExpandRailStation(const BaseStation *st, TileArea &new_ta);
|
||||
extern CommandCost CalculateRoadStopCost(TileArea tile_area, DoCommandFlag flags, bool is_drive_through, StationType station_type, Axis axis, DiagDirection ddir, StationID *est, RoadType rt, Money unit_cost);
|
||||
extern CommandCost RemoveRoadWaypointStop(TileIndex tile, DoCommandFlag flags, int replacement_spec_index);
|
||||
|
||||
/**
|
||||
* Convert existing rail to waypoint. Eg build a waypoint station over
|
||||
@@ -214,12 +241,12 @@ CommandCost CmdBuildRailWaypoint(DoCommandFlag flags, TileIndex start_tile, Axis
|
||||
}
|
||||
|
||||
Waypoint *wp = nullptr;
|
||||
CommandCost ret = FindJoiningWaypoint(est, station_to_join, adjacent, new_location, &wp);
|
||||
CommandCost ret = FindJoiningWaypoint(est, station_to_join, adjacent, new_location, &wp, false);
|
||||
if (ret.Failed()) return ret;
|
||||
|
||||
/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
|
||||
TileIndex center_tile = start_tile + (count / 2) * offset;
|
||||
if (wp == nullptr && reuse) wp = FindDeletedWaypointCloseTo(center_tile, STR_SV_STNAME_WAYPOINT, _current_company);
|
||||
if (wp == nullptr && reuse) wp = FindDeletedWaypointCloseTo(center_tile, STR_SV_STNAME_WAYPOINT, _current_company, false);
|
||||
|
||||
if (wp != nullptr) {
|
||||
/* Reuse an existing waypoint. */
|
||||
@@ -292,6 +319,146 @@ CommandCost CmdBuildRailWaypoint(DoCommandFlag flags, TileIndex start_tile, Axis
|
||||
return cost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a road waypoint on an existing road.
|
||||
* @param flags type of operation.
|
||||
* @param start_tile northern most tile where waypoint will be built.
|
||||
* @param axis orientation (Axis).
|
||||
* @param width width of waypoint.
|
||||
* @param height height of waypoint.
|
||||
* @param spec_class custom road stop class.
|
||||
* @param spec_index custom road stop id.
|
||||
* @param station_to_join station ID to join (NEW_STATION if build new one).
|
||||
* @param adjacent allow waypoints directly adjacent to other waypoints.
|
||||
* @return the cost of this operation or an error.
|
||||
*/
|
||||
CommandCost CmdBuildRoadWaypoint(DoCommandFlag flags, TileIndex start_tile, Axis axis, uint8_t width, uint8_t height, RoadStopClassID spec_class, uint16_t spec_index, StationID station_to_join, bool adjacent)
|
||||
{
|
||||
if (!IsValidAxis(axis)) return CMD_ERROR;
|
||||
/* Check if the given station class is valid */
|
||||
if (static_cast<uint>(spec_class) >= RoadStopClass::GetClassCount()) return CMD_ERROR;
|
||||
const RoadStopClass *cls = RoadStopClass::Get(spec_class);
|
||||
if (!IsWaypointClass(*cls)) return CMD_ERROR;
|
||||
if (spec_index >= cls->GetSpecCount()) return CMD_ERROR;
|
||||
|
||||
const RoadStopSpec *roadstopspec = RoadStopClass::Get(spec_class)->GetSpec(spec_index);
|
||||
|
||||
/* The number of parts to build */
|
||||
uint8_t count = axis == AXIS_X ? height : width;
|
||||
|
||||
if ((axis == AXIS_X ? width : height) != 1) return CMD_ERROR;
|
||||
if (count == 0 || count > _settings_game.station.station_spread) return CMD_ERROR;
|
||||
|
||||
bool reuse = (station_to_join != NEW_STATION);
|
||||
if (!reuse) station_to_join = INVALID_STATION;
|
||||
bool distant_join = (station_to_join != INVALID_STATION);
|
||||
|
||||
if (distant_join && (!_settings_game.station.distant_join_stations || !Waypoint::IsValidID(station_to_join))) return CMD_ERROR;
|
||||
|
||||
TileArea roadstop_area(start_tile, width, height);
|
||||
|
||||
/* Total road stop cost. */
|
||||
Money unit_cost;
|
||||
if (roadstopspec != nullptr) {
|
||||
unit_cost = roadstopspec->GetBuildCost(PR_BUILD_STATION_TRUCK);
|
||||
} else {
|
||||
unit_cost = _price[PR_BUILD_STATION_TRUCK];
|
||||
}
|
||||
StationID est = INVALID_STATION;
|
||||
CommandCost cost = CalculateRoadStopCost(roadstop_area, flags, true, STATION_ROADWAYPOINT, axis, AxisToDiagDir(axis), &est, INVALID_ROADTYPE, unit_cost);
|
||||
if (cost.Failed()) return cost;
|
||||
|
||||
Waypoint *wp = nullptr;
|
||||
CommandCost ret = FindJoiningWaypoint(est, station_to_join, adjacent, roadstop_area, &wp, true);
|
||||
if (ret.Failed()) return ret;
|
||||
|
||||
/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
|
||||
TileIndex center_tile = start_tile + (count / 2) * TileOffsByDiagDir(AxisToDiagDir(OtherAxis(axis)));;
|
||||
if (wp == nullptr && reuse) wp = FindDeletedWaypointCloseTo(center_tile, STR_SV_STNAME_WAYPOINT, _current_company, true);
|
||||
|
||||
if (wp != nullptr) {
|
||||
/* Reuse an existing waypoint. */
|
||||
if (!HasBit(wp->waypoint_flags, WPF_ROAD)) return CMD_ERROR;
|
||||
if (wp->owner != _current_company) return_cmd_error(STR_ERROR_TOO_CLOSE_TO_ANOTHER_WAYPOINT);
|
||||
|
||||
ret = wp->rect.BeforeAddRect(start_tile, width, height, StationRect::ADD_TEST);
|
||||
if (ret.Failed()) return ret;
|
||||
} else {
|
||||
/* Check if we can create a new waypoint. */
|
||||
if (!Waypoint::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
|
||||
}
|
||||
|
||||
/* Check if we can allocate a custom stationspec to this station */
|
||||
if (AllocateSpecToRoadStop(roadstopspec, wp, false) == -1) return_cmd_error(STR_ERROR_TOO_MANY_STATION_SPECS);
|
||||
|
||||
if (flags & DC_EXEC) {
|
||||
if (wp == nullptr) {
|
||||
wp = new Waypoint(start_tile);
|
||||
SetBit(wp->waypoint_flags, WPF_ROAD);
|
||||
} else if (!wp->IsInUse()) {
|
||||
/* Move existing (recently deleted) waypoint to the new location */
|
||||
wp->xy = start_tile;
|
||||
}
|
||||
wp->owner = _current_company;
|
||||
|
||||
wp->rect.BeforeAddRect(start_tile, width, height, StationRect::ADD_TRY);
|
||||
|
||||
if (roadstopspec != nullptr) {
|
||||
/* Include this road stop spec's animation trigger bitmask
|
||||
* in the station's cached copy. */
|
||||
wp->cached_roadstop_anim_triggers |= roadstopspec->animation.triggers;
|
||||
}
|
||||
|
||||
wp->delete_ctr = 0;
|
||||
wp->facilities |= FACIL_BUS_STOP | FACIL_TRUCK_STOP;
|
||||
wp->build_date = TimerGameCalendar::date;
|
||||
wp->string_id = STR_SV_STNAME_WAYPOINT;
|
||||
|
||||
if (wp->town == nullptr) MakeDefaultName(wp);
|
||||
|
||||
wp->UpdateVirtCoord();
|
||||
|
||||
uint8_t map_spec_index = AllocateSpecToRoadStop(roadstopspec, wp, true);
|
||||
|
||||
/* Check every tile in the area. */
|
||||
for (TileIndex cur_tile : roadstop_area) {
|
||||
/* Get existing road types and owners before any tile clearing */
|
||||
RoadType road_rt = MayHaveRoad(cur_tile) ? GetRoadType(cur_tile, RTT_ROAD) : INVALID_ROADTYPE;
|
||||
RoadType tram_rt = MayHaveRoad(cur_tile) ? GetRoadType(cur_tile, RTT_TRAM) : INVALID_ROADTYPE;
|
||||
Owner road_owner = road_rt != INVALID_ROADTYPE ? GetRoadOwner(cur_tile, RTT_ROAD) : _current_company;
|
||||
Owner tram_owner = tram_rt != INVALID_ROADTYPE ? GetRoadOwner(cur_tile, RTT_TRAM) : _current_company;
|
||||
|
||||
if (IsRoadWaypointTile(cur_tile)) {
|
||||
RemoveRoadWaypointStop(cur_tile, flags, map_spec_index);
|
||||
}
|
||||
|
||||
wp->road_waypoint_area.Add(cur_tile);
|
||||
|
||||
wp->rect.BeforeAddTile(cur_tile, StationRect::ADD_TRY);
|
||||
|
||||
/* Update company infrastructure counts. If the current tile is a normal road tile, remove the old
|
||||
* bits first. */
|
||||
if (IsNormalRoadTile(cur_tile)) {
|
||||
UpdateCompanyRoadInfrastructure(road_rt, road_owner, -(int)CountBits(GetRoadBits(cur_tile, RTT_ROAD)));
|
||||
UpdateCompanyRoadInfrastructure(tram_rt, tram_owner, -(int)CountBits(GetRoadBits(cur_tile, RTT_TRAM)));
|
||||
}
|
||||
|
||||
UpdateCompanyRoadInfrastructure(road_rt, road_owner, ROAD_STOP_TRACKBIT_FACTOR);
|
||||
UpdateCompanyRoadInfrastructure(tram_rt, tram_owner, ROAD_STOP_TRACKBIT_FACTOR);
|
||||
|
||||
MakeDriveThroughRoadStop(cur_tile, wp->owner, road_owner, tram_owner, wp->index, STATION_ROADWAYPOINT, road_rt, tram_rt, axis);
|
||||
SetCustomRoadStopSpecIndex(cur_tile, map_spec_index);
|
||||
if (roadstopspec != nullptr) wp->SetRoadStopRandomBits(cur_tile, 0);
|
||||
|
||||
Company::Get(wp->owner)->infrastructure.station++;
|
||||
|
||||
MarkTileDirtyByTile(cur_tile);
|
||||
}
|
||||
DirtyCompanyInfrastructureWindows(wp->owner);
|
||||
}
|
||||
return cost;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a buoy.
|
||||
* @param flags operation to perform
|
||||
@@ -306,7 +473,7 @@ CommandCost CmdBuildBuoy(DoCommandFlag flags, TileIndex tile)
|
||||
if (!IsTileFlat(tile)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
|
||||
|
||||
/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
|
||||
Waypoint *wp = FindDeletedWaypointCloseTo(tile, STR_SV_STNAME_BUOY, OWNER_NONE);
|
||||
Waypoint *wp = FindDeletedWaypointCloseTo(tile, STR_SV_STNAME_BUOY, OWNER_NONE, false);
|
||||
if (wp == nullptr && !Waypoint::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
|
||||
|
||||
CommandCost cost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_WAYPOINT_BUOY]);
|
||||
|
||||
Reference in New Issue
Block a user