2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-01-01 17:22:32 +00:00
|
|
|
/** @file waypoint_cmd.cpp %Command Handling for waypoints. */
|
2009-03-13 21:49:24 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
2010-04-17 13:31:41 +00:00
|
|
|
#include "cmd_helper.h"
|
2009-03-13 21:49:24 +00:00
|
|
|
#include "command_func.h"
|
|
|
|
#include "landscape.h"
|
|
|
|
#include "bridge_map.h"
|
|
|
|
#include "town.h"
|
2009-07-22 10:18:19 +00:00
|
|
|
#include "waypoint_base.h"
|
2009-12-02 16:48:55 +00:00
|
|
|
#include "pathfinder/yapf/yapf_cache.h"
|
2024-01-08 19:29:05 +00:00
|
|
|
#include "pathfinder/water_regions.h"
|
2009-03-13 21:49:24 +00:00
|
|
|
#include "strings_func.h"
|
2011-02-07 22:29:47 +00:00
|
|
|
#include "viewport_func.h"
|
2019-02-22 16:57:28 +00:00
|
|
|
#include "viewport_kdtree.h"
|
2009-03-13 21:49:24 +00:00
|
|
|
#include "window_func.h"
|
|
|
|
#include "date_func.h"
|
|
|
|
#include "vehicle_func.h"
|
|
|
|
#include "string_func.h"
|
|
|
|
#include "company_func.h"
|
|
|
|
#include "newgrf_station.h"
|
2022-02-09 02:11:55 +00:00
|
|
|
#include "newgrf_roadstop.h"
|
2009-10-04 17:16:41 +00:00
|
|
|
#include "company_base.h"
|
2009-07-23 00:14:05 +00:00
|
|
|
#include "water.h"
|
2011-12-03 23:40:30 +00:00
|
|
|
#include "company_gui.h"
|
2009-03-13 21:49:24 +00:00
|
|
|
|
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2009-03-13 21:49:24 +00:00
|
|
|
/**
|
2009-07-13 22:33:25 +00:00
|
|
|
* Update the virtual coords needed to draw the waypoint sign.
|
|
|
|
*/
|
|
|
|
void Waypoint::UpdateVirtCoord()
|
2009-03-13 21:49:24 +00:00
|
|
|
{
|
2024-01-22 23:22:09 +00:00
|
|
|
if (IsHeadless()) return;
|
2009-07-13 22:33:25 +00:00
|
|
|
Point pt = RemapCoords2(TileX(this->xy) * TILE_SIZE, TileY(this->xy) * TILE_SIZE);
|
2019-12-11 18:20:24 +00:00
|
|
|
if (_viewport_sign_kdtree_valid && this->sign.kdtree_valid) _viewport_sign_kdtree.Remove(ViewportSignKdtreeItem::MakeWaypoint(this->index));
|
2019-12-01 22:17:33 +00:00
|
|
|
|
2009-07-13 22:33:25 +00:00
|
|
|
SetDParam(0, this->index);
|
2022-09-21 17:07:02 +00:00
|
|
|
this->sign.UpdatePosition(ShouldShowBaseStationViewportLabel(this) ? ZOOM_LVL_DRAW_SPR : ZOOM_LVL_END, pt.x, pt.y - 32 * ZOOM_LVL_BASE, STR_VIEWPORT_WAYPOINT, STR_VIEWPORT_WAYPOINT_TINY);
|
2019-12-01 22:17:33 +00:00
|
|
|
|
2019-12-11 18:20:24 +00:00
|
|
|
if (_viewport_sign_kdtree_valid) _viewport_sign_kdtree.Insert(ViewportSignKdtreeItem::MakeWaypoint(this->index));
|
2019-12-01 22:17:33 +00:00
|
|
|
|
2009-08-01 11:55:49 +00:00
|
|
|
/* Recenter viewport */
|
|
|
|
InvalidateWindowData(WC_WAYPOINT_VIEW, this->index);
|
2009-03-13 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
2019-07-07 17:44:08 +00:00
|
|
|
/**
|
|
|
|
* Move the waypoint main coordinate somewhere else.
|
|
|
|
* @param new_xy new tile location of the sign
|
|
|
|
*/
|
|
|
|
void Waypoint::MoveSign(TileIndex new_xy)
|
|
|
|
{
|
|
|
|
if (this->xy == new_xy) return;
|
|
|
|
|
|
|
|
this->BaseStation::MoveSign(new_xy);
|
|
|
|
}
|
|
|
|
|
2009-03-13 21:49:24 +00:00
|
|
|
/**
|
|
|
|
* Find a deleted waypoint close to a tile.
|
|
|
|
* @param tile to search from
|
2009-07-23 00:14:05 +00:00
|
|
|
* @param str the string to get the 'type' of
|
2010-01-02 17:09:14 +00:00
|
|
|
* @param cid previous owner of the waypoint
|
2009-07-23 00:14:05 +00:00
|
|
|
* @return the deleted nearby waypoint
|
2009-03-13 21:49:24 +00:00
|
|
|
*/
|
2022-01-25 22:02:32 +00:00
|
|
|
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile, StringID str, CompanyID cid, bool is_road)
|
2009-03-13 21:49:24 +00:00
|
|
|
{
|
2019-12-15 04:55:59 +00:00
|
|
|
Waypoint *best = nullptr;
|
2009-03-13 21:49:24 +00:00
|
|
|
uint thres = 8;
|
|
|
|
|
2019-12-15 04:55:59 +00:00
|
|
|
for (Waypoint *wp : Waypoint::Iterate()) {
|
2022-01-25 22:02:32 +00:00
|
|
|
if (!wp->IsInUse() && wp->string_id == str && wp->owner == cid && HasBit(wp->waypoint_flags, WPF_ROAD) == is_road) {
|
2009-03-13 21:49:24 +00:00
|
|
|
uint cur_dist = DistanceManhattan(tile, wp->xy);
|
|
|
|
|
|
|
|
if (cur_dist < thres) {
|
|
|
|
thres = cur_dist;
|
|
|
|
best = wp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2009-07-30 22:06:54 +00:00
|
|
|
/**
|
|
|
|
* Get the axis for a new 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)
|
|
|
|
{
|
|
|
|
/* The axis for rail waypoints is easy. */
|
|
|
|
if (IsRailWaypointTile(tile)) return GetRailStationAxis(tile);
|
|
|
|
|
|
|
|
/* Non-plain rail type, no valid axis for waypoints. */
|
|
|
|
if (!IsTileType(tile, MP_RAILWAY) || GetRailTileType(tile) != RAIL_TILE_NORMAL) return INVALID_AXIS;
|
|
|
|
|
|
|
|
switch (GetTrackBits(tile)) {
|
|
|
|
case TRACK_BIT_X: return AXIS_X;
|
|
|
|
case TRACK_BIT_Y: return AXIS_Y;
|
|
|
|
default: return INVALID_AXIS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 22:02:32 +00:00
|
|
|
/**
|
|
|
|
* 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 rail 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;
|
|
|
|
}
|
|
|
|
|
2011-05-01 19:14:12 +00:00
|
|
|
extern CommandCost ClearTile_Station(TileIndex tile, DoCommandFlag flags);
|
2009-07-30 22:06:54 +00:00
|
|
|
|
2009-07-30 17:44:13 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2009-09-19 09:51:14 +00:00
|
|
|
* @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.
|
2009-03-13 21:49:24 +00:00
|
|
|
*/
|
2009-07-30 22:06:54 +00:00
|
|
|
static CommandCost IsValidTileForWaypoint(TileIndex tile, Axis axis, StationID *waypoint)
|
2009-03-13 21:49:24 +00:00
|
|
|
{
|
2009-07-30 22:06:54 +00:00
|
|
|
/* 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. */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (waypoint != nullptr && IsTileType(tile, MP_STATION)) {
|
2009-07-30 22:06:54 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2009-03-13 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 22:06:54 +00:00
|
|
|
if (GetAxisForNewWaypoint(tile) != axis) return_cmd_error(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK);
|
|
|
|
|
2009-03-13 21:49:24 +00:00
|
|
|
Owner owner = GetTileOwner(tile);
|
2010-03-13 17:11:28 +00:00
|
|
|
CommandCost ret = CheckOwnership(owner);
|
2010-03-20 17:58:24 +00:00
|
|
|
if (ret.Succeeded()) ret = EnsureNoVehicleOnGround(tile);
|
2010-03-05 21:20:22 +00:00
|
|
|
if (ret.Failed()) return ret;
|
2009-03-13 21:49:24 +00:00
|
|
|
|
2011-11-04 10:22:27 +00:00
|
|
|
Slope tileh = GetTileSlope(tile);
|
2009-03-13 21:49:24 +00:00
|
|
|
if (tileh != SLOPE_FLAT &&
|
|
|
|
(!_settings_game.construction.build_on_slopes || IsSteepSlope(tileh) || !(tileh & (0x3 << axis)) || !(tileh & ~(0x3 << axis)))) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_FLAT_LAND_REQUIRED);
|
2009-03-13 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
2009-07-30 17:44:13 +00:00
|
|
|
return CommandCost();
|
|
|
|
}
|
|
|
|
|
2021-05-01 17:07:47 +00:00
|
|
|
extern void GetStationLayout(byte *layout, uint numtracks, uint plat_len, const StationSpec *statspec);
|
2022-01-25 22:02:32 +00:00
|
|
|
extern CommandCost FindJoiningWaypoint(StationID existing_station, StationID station_to_join, bool adjacent, TileArea ta, Waypoint **wp, bool is_road);
|
2023-09-16 21:27:16 +00:00
|
|
|
extern CommandCost CanExpandRailStation(const BaseStation *st, TileArea &new_ta);
|
2018-11-17 19:49:05 +00:00
|
|
|
extern CommandCost IsRailStationBridgeAboveOk(TileIndex tile, const StationSpec *statspec, byte layout);
|
2009-07-30 17:44:13 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Convert existing rail to waypoint. Eg build a waypoint station over
|
2009-07-30 17:44:13 +00:00
|
|
|
* piece of rail
|
|
|
|
* @param start_tile northern most tile where waypoint will be built
|
|
|
|
* @param flags type of operation
|
|
|
|
* @param p1 various bitstuffed elements
|
2018-07-28 13:59:37 +00:00
|
|
|
* - p1 = (bit 0- 5) - railtype (not used)
|
|
|
|
* - p1 = (bit 6) - orientation (Axis)
|
2009-07-30 17:44:13 +00:00
|
|
|
* - p1 = (bit 8-15) - width of waypoint
|
|
|
|
* - p1 = (bit 16-23) - height of waypoint
|
2009-07-30 22:06:54 +00:00
|
|
|
* - p1 = (bit 24) - allow waypoints directly adjacent to other waypoints.
|
2009-07-30 17:44:13 +00:00
|
|
|
* @param p2 various bitstuffed elements
|
|
|
|
* - p2 = (bit 0- 7) - custom station class
|
2022-01-01 20:42:10 +00:00
|
|
|
* - p2 = (bit 31-16) - station ID to join
|
|
|
|
* @param p3 various bitstuffed elements
|
|
|
|
* - p3 = (bit 0-31) - custom station id
|
2009-07-30 17:44:13 +00:00
|
|
|
* @param text unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @return the cost of this operation or an error
|
2009-07-30 17:44:13 +00:00
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
CommandCost CmdBuildRailWaypoint(TileIndex start_tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, uint64_t p3, const char *text, const CommandAuxiliaryBase *aux_data)
|
2009-07-30 17:44:13 +00:00
|
|
|
{
|
|
|
|
/* Unpack parameters */
|
2018-07-28 13:59:37 +00:00
|
|
|
Axis axis = Extract<Axis, 6, 1>(p1);
|
2009-07-30 17:44:13 +00:00
|
|
|
byte width = GB(p1, 8, 8);
|
|
|
|
byte height = GB(p1, 16, 8);
|
2009-07-30 22:06:54 +00:00
|
|
|
bool adjacent = HasBit(p1, 24);
|
2009-07-30 17:44:13 +00:00
|
|
|
|
2010-04-17 13:31:41 +00:00
|
|
|
StationClassID spec_class = Extract<StationClassID, 0, 8>(p2);
|
2009-07-30 22:06:54 +00:00
|
|
|
StationID station_to_join = GB(p2, 16, 16);
|
2009-07-30 17:44:13 +00:00
|
|
|
|
2022-01-01 20:42:10 +00:00
|
|
|
uint spec_index = GB(p3, 0, 32);
|
|
|
|
|
2009-07-30 17:44:13 +00:00
|
|
|
/* Check if the given station class is valid */
|
|
|
|
if (spec_class != STAT_CLASS_WAYP) return CMD_ERROR;
|
2012-04-22 16:28:14 +00:00
|
|
|
if (spec_index >= StationClass::Get(spec_class)->GetSpecCount()) return CMD_ERROR;
|
2009-07-30 17:44:13 +00:00
|
|
|
|
|
|
|
/* The number of parts to build */
|
|
|
|
byte 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;
|
|
|
|
|
2009-07-31 16:28:28 +00:00
|
|
|
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;
|
2009-07-30 22:06:54 +00:00
|
|
|
|
2018-08-02 07:27:20 +00:00
|
|
|
const StationSpec *spec = StationClass::Get(spec_class)->GetSpec(spec_index);
|
|
|
|
byte *layout_ptr = AllocaM(byte, count);
|
2019-04-11 17:14:13 +00:00
|
|
|
if (spec == nullptr) {
|
2018-08-02 07:27:20 +00:00
|
|
|
/* The layout must be 0 for the 'normal' waypoints by design. */
|
|
|
|
memset(layout_ptr, 0, count);
|
|
|
|
} else {
|
|
|
|
/* But for NewGRF waypoints we like to have their style. */
|
|
|
|
GetStationLayout(layout_ptr, count, 1, spec);
|
|
|
|
}
|
|
|
|
|
2009-07-30 22:06:54 +00:00
|
|
|
/* Make sure the area below consists of clear tiles. (OR tiles belonging to a certain rail station) */
|
|
|
|
StationID est = INVALID_STATION;
|
|
|
|
|
2009-07-30 17:44:13 +00:00
|
|
|
/* Check whether the tiles we're building on are valid rail or not. */
|
|
|
|
TileIndexDiff offset = TileOffsByDiagDir(AxisToDiagDir(OtherAxis(axis)));
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
TileIndex tile = start_tile + i * offset;
|
2011-02-04 14:11:14 +00:00
|
|
|
CommandCost ret = IsValidTileForWaypoint(tile, axis, &est);
|
2009-07-30 17:44:13 +00:00
|
|
|
if (ret.Failed()) return ret;
|
2018-11-17 19:49:05 +00:00
|
|
|
ret = IsRailStationBridgeAboveOk(tile, spec, layout_ptr[i]);
|
|
|
|
if (ret.Failed()) {
|
|
|
|
return CommandCost::DualErrorMessage(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST, ret.GetErrorMessage());
|
|
|
|
}
|
2009-07-30 17:44:13 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
Waypoint *wp = nullptr;
|
2019-04-13 13:12:34 +00:00
|
|
|
TileArea new_location(start_tile, width, height);
|
2022-01-25 22:02:32 +00:00
|
|
|
CommandCost ret = FindJoiningWaypoint(est, station_to_join, adjacent, new_location, &wp, false);
|
2009-07-30 22:06:54 +00:00
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
|
2009-03-13 21:49:24 +00:00
|
|
|
/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
|
2009-07-30 17:44:13 +00:00
|
|
|
TileIndex center_tile = start_tile + (count / 2) * offset;
|
2022-01-25 22:02:32 +00:00
|
|
|
if (wp == nullptr && reuse) wp = FindDeletedWaypointCloseTo(center_tile, STR_SV_STNAME_WAYPOINT, _current_company, false);
|
2009-07-30 22:06:54 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (wp != nullptr) {
|
2010-01-02 17:09:14 +00:00
|
|
|
/* Reuse an existing waypoint. */
|
2022-01-25 22:02:32 +00:00
|
|
|
if (HasBit(wp->waypoint_flags, WPF_ROAD)) return CMD_ERROR;
|
2009-07-30 22:06:54 +00:00
|
|
|
if (wp->owner != _current_company) return_cmd_error(STR_ERROR_TOO_CLOSE_TO_ANOTHER_WAYPOINT);
|
|
|
|
|
2010-01-02 17:09:14 +00:00
|
|
|
/* check if we want to expand an already existing waypoint? */
|
2010-02-27 14:17:33 +00:00
|
|
|
if (wp->train_station.tile != INVALID_TILE) {
|
2023-11-20 22:26:57 +00:00
|
|
|
CommandCost ret = CanExpandRailStation(wp, new_location);
|
2010-02-27 14:17:33 +00:00
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
}
|
2009-07-30 22:06:54 +00:00
|
|
|
|
2010-03-06 13:23:33 +00:00
|
|
|
CommandCost ret = wp->rect.BeforeAddRect(start_tile, width, height, StationRect::ADD_TEST);
|
|
|
|
if (ret.Failed()) return ret;
|
2009-07-30 22:06:54 +00:00
|
|
|
} else {
|
2010-01-02 17:09:14 +00:00
|
|
|
/* allocate and initialize new waypoint */
|
2009-07-30 22:06:54 +00:00
|
|
|
if (!Waypoint::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
|
|
|
|
}
|
2009-03-13 21:49:24 +00:00
|
|
|
|
2022-02-09 02:13:46 +00:00
|
|
|
/* Check if we can allocate a custom stationspec to this station */
|
2022-02-13 16:28:01 +00:00
|
|
|
if (AllocateSpecToStation(spec, wp, false) == -1) return_cmd_error(STR_ERROR_TOO_MANY_STATION_SPECS);
|
2022-02-09 02:13:46 +00:00
|
|
|
|
2009-03-13 21:49:24 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2019-04-10 21:07:06 +00:00
|
|
|
if (wp == nullptr) {
|
2009-07-30 17:44:13 +00:00
|
|
|
wp = new Waypoint(start_tile);
|
|
|
|
} else if (!wp->IsInUse()) {
|
2009-03-13 21:49:24 +00:00
|
|
|
/* Move existing (recently deleted) waypoint to the new location */
|
2009-07-30 17:44:13 +00:00
|
|
|
wp->xy = start_tile;
|
2009-03-13 21:49:24 +00:00
|
|
|
}
|
2009-07-30 17:44:13 +00:00
|
|
|
wp->owner = GetTileOwner(start_tile);
|
2009-03-13 21:49:24 +00:00
|
|
|
|
2009-07-30 17:44:13 +00:00
|
|
|
wp->rect.BeforeAddRect(start_tile, width, height, StationRect::ADD_TRY);
|
2009-03-13 21:49:24 +00:00
|
|
|
|
2009-07-16 20:15:28 +00:00
|
|
|
wp->delete_ctr = 0;
|
2009-07-21 16:30:01 +00:00
|
|
|
wp->facilities |= FACIL_TRAIN;
|
2024-02-13 21:34:09 +00:00
|
|
|
wp->build_date = CalTime::CurDate();
|
2009-07-22 08:59:57 +00:00
|
|
|
wp->string_id = STR_SV_STNAME_WAYPOINT;
|
2009-07-30 22:06:54 +00:00
|
|
|
wp->train_station = new_location;
|
2009-03-13 21:49:24 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (wp->town == nullptr) MakeDefaultName(wp);
|
2009-03-13 21:49:24 +00:00
|
|
|
|
2009-07-13 22:33:25 +00:00
|
|
|
wp->UpdateVirtCoord();
|
2009-07-30 17:44:13 +00:00
|
|
|
|
2022-02-13 16:28:01 +00:00
|
|
|
byte map_spec_index = AllocateSpecToStation(spec, wp, true);
|
|
|
|
|
2011-12-03 23:40:30 +00:00
|
|
|
Company *c = Company::Get(wp->owner);
|
2009-07-30 17:44:13 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
TileIndex tile = start_tile + i * offset;
|
2010-01-26 23:03:47 +00:00
|
|
|
byte old_specindex = HasStationTileRail(tile) ? GetCustomStationSpecIndex(tile) : 0;
|
2011-12-03 23:40:30 +00:00
|
|
|
if (!HasStationTileRail(tile)) c->infrastructure.station++;
|
2009-07-30 22:06:54 +00:00
|
|
|
bool reserved = IsTileType(tile, MP_RAILWAY) ?
|
|
|
|
HasBit(GetRailReservationTrackBits(tile), AxisToTrack(axis)) :
|
|
|
|
HasStationReservation(tile);
|
2009-07-30 17:44:13 +00:00
|
|
|
MakeRailWaypoint(tile, wp->owner, wp->index, axis, layout_ptr[i], GetRailType(tile));
|
2022-02-24 18:19:00 +00:00
|
|
|
if (old_specindex != map_spec_index) DeallocateSpecFromStation(wp, old_specindex);
|
2009-07-30 17:44:13 +00:00
|
|
|
SetCustomStationSpecIndex(tile, map_spec_index);
|
2023-09-25 19:22:36 +00:00
|
|
|
|
|
|
|
/* Should be the same as layout but axis component could be wrong... */
|
|
|
|
StationGfx gfx = GetStationGfx(tile);
|
|
|
|
bool blocked = spec != nullptr && HasBit(spec->blocked, gfx);
|
|
|
|
/* Default stations do not draw pylons under roofs (gfx >= 4) */
|
|
|
|
bool pylons = spec != nullptr ? HasBit(spec->pylons, gfx) : gfx < 4;
|
|
|
|
bool wires = spec == nullptr || !HasBit(spec->wires, gfx);
|
|
|
|
|
|
|
|
SetStationTileBlocked(tile, blocked);
|
|
|
|
SetStationTileHavePylons(tile, pylons);
|
|
|
|
SetStationTileHaveWires(tile, wires);
|
|
|
|
|
2009-07-30 17:44:13 +00:00
|
|
|
SetRailStationReservation(tile, reserved);
|
2020-10-01 17:50:31 +00:00
|
|
|
MarkTileDirtyByTile(tile, VMDF_NOT_MAP_MODE);
|
2009-07-30 17:44:13 +00:00
|
|
|
|
|
|
|
YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
|
|
|
|
}
|
2011-12-03 23:40:30 +00:00
|
|
|
DirtyCompanyInfrastructureWindows(wp->owner);
|
2009-03-13 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
2009-11-24 22:15:42 +00:00
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, count * _price[PR_BUILD_WAYPOINT_RAIL]);
|
2009-03-13 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
2022-01-25 22:02:32 +00:00
|
|
|
/**
|
|
|
|
* Convert existing road to waypoint. Eg build a waypoint station over
|
|
|
|
* piece of road
|
|
|
|
* @param start_tile northern most tile where waypoint will be built
|
|
|
|
* @param flags Operation to perform.
|
|
|
|
* @param p1 bit 0..7: Width of the road stop.
|
|
|
|
* bit 8..15: Length of the road stop.
|
|
|
|
* bit 16: Allow stations directly adjacent to other stations.
|
|
|
|
* bit 17: #Axis of the road.
|
2022-02-09 02:11:55 +00:00
|
|
|
* @param p2 bit 0..7: Custom road stop class
|
|
|
|
* bit 16..31: Station ID to join (NEW_STATION if build new one).
|
|
|
|
* @param p3 various bitstuffed elements
|
|
|
|
* - p3 = (bit 0-31) - custom road stop id
|
2022-01-25 22:02:32 +00:00
|
|
|
* @param text Unused.
|
|
|
|
* @return The cost of this operation or an error.
|
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
CommandCost CmdBuildRoadWaypoint(TileIndex start_tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, uint64_t p3, const char *text, const CommandAuxiliaryBase *aux_data)
|
2022-01-25 22:02:32 +00:00
|
|
|
{
|
|
|
|
StationID station_to_join = GB(p2, 16, 16);
|
|
|
|
byte width = GB(p1, 0, 8);
|
|
|
|
byte height = GB(p1, 8, 8);
|
|
|
|
bool adjacent = HasBit(p1, 16);
|
|
|
|
Axis axis = Extract<Axis, 17, 1>(p1);
|
|
|
|
|
2022-02-09 02:11:55 +00:00
|
|
|
RoadStopClassID spec_class = Extract<RoadStopClassID, 0, 8>(p2);
|
|
|
|
uint spec_index = GB(p3, 0, 32);
|
|
|
|
|
|
|
|
/* Check if the given road stop class is valid */
|
|
|
|
if (spec_class != ROADSTOP_CLASS_WAYP) return CMD_ERROR;
|
|
|
|
if (spec_index >= RoadStopClass::Get(spec_class)->GetSpecCount()) return CMD_ERROR;
|
|
|
|
|
|
|
|
const RoadStopSpec *spec = RoadStopClass::Get(spec_class)->GetSpec(spec_index);
|
|
|
|
|
2022-01-25 22:02:32 +00:00
|
|
|
/* The number of parts to build */
|
|
|
|
byte 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;
|
|
|
|
|
|
|
|
/* Check if the first tile and the last tile are valid */
|
|
|
|
if (!IsValidTile(start_tile) || TileAddWrap(start_tile, width - 1, height - 1) == INVALID_TILE) return CMD_ERROR;
|
|
|
|
|
|
|
|
TileArea roadstop_area(start_tile, width, height);
|
|
|
|
/* Total road stop cost. */
|
2022-02-24 21:13:46 +00:00
|
|
|
Money unit_cost;
|
|
|
|
if (spec != nullptr) {
|
|
|
|
unit_cost = spec->GetBuildCost(PR_BUILD_STATION_TRUCK);
|
|
|
|
} else {
|
|
|
|
unit_cost = _price[PR_BUILD_STATION_TRUCK];
|
|
|
|
}
|
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION, roadstop_area.w * roadstop_area.h * unit_cost);
|
2022-01-25 22:02:32 +00:00
|
|
|
StationID est = INVALID_STATION;
|
2022-02-15 18:21:32 +00:00
|
|
|
extern CommandCost CheckFlatLandRoadStop(TileArea tile_area, const RoadStopSpec *spec, DoCommandFlag flags, uint invalid_dirs, bool is_drive_through, StationType station_type, Axis axis, StationID *station, RoadType rt, bool require_road);
|
|
|
|
CommandCost ret = CheckFlatLandRoadStop(roadstop_area, spec, flags, 5 << axis, true, STATION_ROADWAYPOINT, axis, &est, INVALID_ROADTYPE, true);
|
2022-01-25 22:02:32 +00:00
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
cost.AddCost(ret);
|
|
|
|
|
|
|
|
Waypoint *wp = nullptr;
|
|
|
|
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);
|
|
|
|
|
|
|
|
CommandCost ret = wp->rect.BeforeAddRect(start_tile, width, height, StationRect::ADD_TEST);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
} else {
|
|
|
|
/* allocate and initialize new waypoint */
|
|
|
|
if (!Waypoint::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
|
|
|
|
}
|
|
|
|
|
2022-02-09 02:11:55 +00:00
|
|
|
/* Check if we can allocate a custom stationspec to this station */
|
|
|
|
if (AllocateRoadStopSpecToStation(spec, wp, false) == -1) return_cmd_error(STR_ERROR_TOO_MANY_STATION_SPECS);
|
|
|
|
|
2022-01-25 22:02:32 +00:00
|
|
|
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);
|
|
|
|
|
2022-02-15 00:22:18 +00:00
|
|
|
if (spec != nullptr) {
|
|
|
|
/* Include this road stop spec's animation trigger bitmask
|
|
|
|
* in the station's cached copy. */
|
|
|
|
wp->cached_roadstop_anim_triggers |= spec->animation.triggers;
|
|
|
|
}
|
|
|
|
|
2022-01-25 22:02:32 +00:00
|
|
|
wp->delete_ctr = 0;
|
|
|
|
wp->facilities |= FACIL_BUS_STOP | FACIL_TRUCK_STOP;
|
2024-02-13 21:34:09 +00:00
|
|
|
wp->build_date = CalTime::CurDate();
|
2022-01-25 22:02:32 +00:00
|
|
|
wp->string_id = STR_SV_STNAME_WAYPOINT;
|
|
|
|
|
|
|
|
if (wp->town == nullptr) MakeDefaultName(wp);
|
|
|
|
|
|
|
|
wp->UpdateVirtCoord();
|
|
|
|
|
2022-02-09 02:11:55 +00:00
|
|
|
byte map_spec_index = AllocateRoadStopSpecToStation(spec, wp, true);
|
|
|
|
|
2022-01-25 22:02:32 +00:00
|
|
|
/* 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;
|
2023-03-15 18:42:36 +00:00
|
|
|
|
|
|
|
DisallowedRoadDirections drd = DRD_NONE;
|
2024-01-09 08:48:26 +00:00
|
|
|
if (road_rt != INVALID_ROADTYPE) {
|
|
|
|
if (IsNormalRoadTile(cur_tile)){
|
|
|
|
drd = GetDisallowedRoadDirections(cur_tile);
|
|
|
|
} else if (IsDriveThroughStopTile(cur_tile)) {
|
|
|
|
drd = GetDriveThroughStopDisallowedRoadDirections(cur_tile);
|
|
|
|
}
|
2023-03-15 18:42:36 +00:00
|
|
|
}
|
2022-01-25 22:02:32 +00:00
|
|
|
|
2022-02-24 18:19:00 +00:00
|
|
|
extern CommandCost RemoveRoadStop(TileIndex tile, DoCommandFlag flags, int replacement_spec_index);
|
2022-01-25 22:02:32 +00:00
|
|
|
if (IsTileType(cur_tile, MP_STATION) && IsAnyRoadStop(cur_tile)) {
|
2022-02-24 18:19:00 +00:00
|
|
|
RemoveRoadStop(cur_tile, flags, map_spec_index);
|
2022-01-25 22:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
SetDriveThroughStopDisallowedRoadDirections(cur_tile, drd);
|
2022-02-09 02:11:55 +00:00
|
|
|
SetCustomRoadStopSpecIndex(cur_tile, map_spec_index);
|
|
|
|
if (spec != nullptr) wp->SetRoadStopRandomBits(cur_tile, 0);
|
2022-01-25 22:02:32 +00:00
|
|
|
|
|
|
|
Company::Get(wp->owner)->infrastructure.station++;
|
|
|
|
|
|
|
|
MarkTileDirtyByTile(cur_tile);
|
|
|
|
UpdateRoadCachedOneWayStatesAroundTile(cur_tile);
|
|
|
|
}
|
|
|
|
NotifyRoadLayoutChanged(true);
|
|
|
|
DirtyCompanyInfrastructureWindows(wp->owner);
|
|
|
|
}
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Build a buoy.
|
2011-01-15 16:27:04 +00:00
|
|
|
* @param tile tile where to place the buoy
|
2009-07-23 00:14:05 +00:00
|
|
|
* @param flags operation to perform
|
|
|
|
* @param p1 unused
|
|
|
|
* @param p2 unused
|
|
|
|
* @param text unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @return the cost of this operation or an error
|
2009-07-23 00:14:05 +00:00
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
CommandCost CmdBuildBuoy(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
|
2009-07-23 00:14:05 +00:00
|
|
|
{
|
2010-11-21 18:38:45 +00:00
|
|
|
if (tile == 0 || !HasTileWaterGround(tile)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
|
2009-07-23 00:14:05 +00:00
|
|
|
|
2013-10-12 22:07:58 +00:00
|
|
|
if (!IsTileFlat(tile)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
|
2009-07-23 00:14:05 +00:00
|
|
|
|
|
|
|
/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
|
2022-01-25 22:02:32 +00:00
|
|
|
Waypoint *wp = FindDeletedWaypointCloseTo(tile, STR_SV_STNAME_BUOY, OWNER_NONE, false);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (wp == nullptr && !Waypoint::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
|
2009-07-23 00:14:05 +00:00
|
|
|
|
2010-09-05 16:33:32 +00:00
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_WAYPOINT_BUOY]);
|
|
|
|
if (!IsWaterTile(tile)) {
|
|
|
|
CommandCost ret = DoCommand(tile, 0, 0, flags | DC_AUTO, CMD_LANDSCAPE_CLEAR);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
cost.AddCost(ret);
|
|
|
|
}
|
|
|
|
|
2009-07-23 00:14:05 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2019-04-10 21:07:06 +00:00
|
|
|
if (wp == nullptr) {
|
2009-07-23 00:14:05 +00:00
|
|
|
wp = new Waypoint(tile);
|
|
|
|
} else {
|
|
|
|
/* Move existing (recently deleted) buoy to the new location */
|
|
|
|
wp->xy = tile;
|
|
|
|
InvalidateWindowData(WC_WAYPOINT_VIEW, wp->index);
|
|
|
|
}
|
2009-07-26 16:17:49 +00:00
|
|
|
wp->rect.BeforeAddTile(tile, StationRect::ADD_TRY);
|
2009-07-23 00:14:05 +00:00
|
|
|
|
|
|
|
wp->string_id = STR_SV_STNAME_BUOY;
|
|
|
|
|
|
|
|
wp->facilities |= FACIL_DOCK;
|
|
|
|
wp->owner = OWNER_NONE;
|
|
|
|
|
2024-02-13 21:34:09 +00:00
|
|
|
wp->build_date = CalTime::CurDate();
|
2009-07-23 00:14:05 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (wp->town == nullptr) MakeDefaultName(wp);
|
2009-07-23 00:14:05 +00:00
|
|
|
|
|
|
|
MakeBuoy(tile, wp->index, GetWaterClass(tile));
|
2024-01-08 19:29:05 +00:00
|
|
|
InvalidateWaterRegion(tile);
|
2019-03-11 10:37:47 +00:00
|
|
|
CheckForDockingTile(tile);
|
2017-03-12 15:32:40 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
2021-04-12 23:00:11 +00:00
|
|
|
ClearNeighbourNonFloodingStates(tile);
|
2009-07-23 00:14:05 +00:00
|
|
|
|
|
|
|
wp->UpdateVirtCoord();
|
|
|
|
InvalidateWindowData(WC_WAYPOINT_VIEW, wp->index);
|
|
|
|
}
|
|
|
|
|
2010-09-05 16:33:32 +00:00
|
|
|
return cost;
|
2009-07-23 00:14:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a buoy
|
|
|
|
* @param tile TileIndex been queried
|
|
|
|
* @param flags operation to perform
|
|
|
|
* @pre IsBuoyTile(tile)
|
|
|
|
* @return cost or failure of operation
|
|
|
|
*/
|
|
|
|
CommandCost RemoveBuoy(TileIndex tile, DoCommandFlag flags)
|
|
|
|
{
|
|
|
|
/* XXX: strange stuff, allow clearing as invalid company when clearing landscape */
|
|
|
|
if (!Company::IsValidID(_current_company) && !(flags & DC_BANKRUPT)) return_cmd_error(INVALID_STRING_ID);
|
|
|
|
|
|
|
|
Waypoint *wp = Waypoint::GetByTile(tile);
|
|
|
|
|
2010-08-20 12:50:59 +00:00
|
|
|
if (HasStationInUse(wp->index, false, _current_company)) return_cmd_error(STR_ERROR_BUOY_IS_IN_USE);
|
2009-07-23 00:14:05 +00:00
|
|
|
/* remove the buoy if there is a ship on tile when company goes bankrupt... */
|
2010-03-05 21:20:22 +00:00
|
|
|
if (!(flags & DC_BANKRUPT)) {
|
|
|
|
CommandCost ret = EnsureNoVehicleOnGround(tile);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
}
|
2009-07-23 00:14:05 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
wp->facilities &= ~FACIL_DOCK;
|
|
|
|
|
|
|
|
InvalidateWindowData(WC_WAYPOINT_VIEW, wp->index);
|
|
|
|
|
|
|
|
/* We have to set the water tile's state to the same state as before the
|
|
|
|
* buoy was placed. Otherwise one could plant a buoy on a canal edge,
|
|
|
|
* remove it and flood the land (if the canal edge is at level 0) */
|
|
|
|
MakeWaterKeepingClass(tile, GetTileOwner(tile));
|
|
|
|
|
2009-07-26 16:17:49 +00:00
|
|
|
wp->rect.AfterRemoveTile(wp, tile);
|
|
|
|
|
2009-07-23 00:14:05 +00:00
|
|
|
wp->UpdateVirtCoord();
|
|
|
|
wp->delete_ctr = 0;
|
|
|
|
}
|
|
|
|
|
2009-11-24 22:15:42 +00:00
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_CLEAR_WAYPOINT_BUOY]);
|
2009-07-23 00:14:05 +00:00
|
|
|
}
|
|
|
|
|
2011-05-01 19:14:12 +00:00
|
|
|
/**
|
|
|
|
* Check whether the name is unique amongst the waypoints.
|
|
|
|
* @param name The name to check.
|
|
|
|
* @return True iff the name is unique.
|
|
|
|
*/
|
2009-03-13 21:49:24 +00:00
|
|
|
static bool IsUniqueWaypointName(const char *name)
|
|
|
|
{
|
2019-12-15 04:55:59 +00:00
|
|
|
for (const Waypoint *wp : Waypoint::Iterate()) {
|
2020-05-17 21:31:59 +00:00
|
|
|
if (!wp->name.empty() && wp->name == name) return false;
|
2009-03-13 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename a waypoint.
|
|
|
|
* @param tile unused
|
|
|
|
* @param flags type of operation
|
|
|
|
* @param p1 id of waypoint
|
|
|
|
* @param p2 unused
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text the new name or an empty string when resetting to the default
|
|
|
|
* @return the cost of this operation or an error
|
2009-03-13 21:49:24 +00:00
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
CommandCost CmdRenameWaypoint(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
|
2009-03-13 21:49:24 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Waypoint *wp = Waypoint::GetIfValid(p1);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (wp == nullptr) return CMD_ERROR;
|
2010-03-13 17:11:28 +00:00
|
|
|
|
|
|
|
if (wp->owner != OWNER_NONE) {
|
|
|
|
CommandCost ret = CheckOwnership(wp->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
}
|
2009-03-13 21:49:24 +00:00
|
|
|
|
|
|
|
bool reset = StrEmpty(text);
|
|
|
|
|
|
|
|
if (!reset) {
|
2010-12-05 22:25:36 +00:00
|
|
|
if (Utf8StringLength(text) >= MAX_LENGTH_STATION_NAME_CHARS) return CMD_ERROR;
|
2009-08-05 17:59:21 +00:00
|
|
|
if (!IsUniqueWaypointName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
|
2009-03-13 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2020-05-17 21:31:59 +00:00
|
|
|
if (reset) {
|
|
|
|
wp->name.clear();
|
|
|
|
} else {
|
|
|
|
wp->name = text;
|
|
|
|
}
|
2009-03-13 21:49:24 +00:00
|
|
|
|
2009-07-13 22:33:25 +00:00
|
|
|
wp->UpdateVirtCoord();
|
2009-03-13 21:49:24 +00:00
|
|
|
}
|
|
|
|
return CommandCost();
|
|
|
|
}
|
2022-01-21 23:37:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set whether waypoint label is hidden
|
|
|
|
* @param tile unused
|
|
|
|
* @param flags type of operation
|
|
|
|
* @param p1 id of waypoint
|
|
|
|
* @param p2 hidden state
|
|
|
|
* @param text the new name or an empty string when resetting to the default
|
|
|
|
* @return the cost of this operation or an error
|
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
CommandCost CmdSetWaypointLabelHidden(TileIndex tile, DoCommandFlag flags, uint32_t p1, uint32_t p2, const char *text)
|
2022-01-21 23:37:53 +00:00
|
|
|
{
|
|
|
|
Waypoint *wp = Waypoint::GetIfValid(p1);
|
|
|
|
if (wp == nullptr) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (wp->owner != OWNER_NONE) {
|
|
|
|
CommandCost ret = CheckOwnership(wp->owner);
|
|
|
|
if (ret.Failed()) return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
SB(wp->waypoint_flags, WPF_HIDE_LABEL, 1, p2 != 0 ? 1 : 0);
|
|
|
|
|
|
|
|
if (HasBit(_display_opt, DO_SHOW_WAYPOINT_NAMES) &&
|
|
|
|
!(_local_company != wp->owner && wp->owner != OWNER_NONE && !HasBit(_display_opt, DO_SHOW_COMPETITOR_SIGNS))) {
|
|
|
|
wp->sign.MarkDirty(ZOOM_LVL_DRAW_SPR);
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidateWindowData(WC_WAYPOINT_VIEW, wp->index);
|
|
|
|
}
|
|
|
|
return CommandCost();
|
|
|
|
}
|