2009-03-13 21:49:24 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-06-01 12:56:18 +00:00
|
|
|
/** @file waypoint_cmd.cpp Command Handling for waypoints. */
|
2009-03-13 21:49:24 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
|
|
|
#include "command_func.h"
|
|
|
|
#include "landscape.h"
|
|
|
|
#include "economy_func.h"
|
|
|
|
#include "bridge_map.h"
|
|
|
|
#include "town.h"
|
2009-07-22 10:18:19 +00:00
|
|
|
#include "waypoint_base.h"
|
2009-03-13 21:49:24 +00:00
|
|
|
#include "yapf/yapf.h"
|
|
|
|
#include "strings_func.h"
|
|
|
|
#include "gfx_func.h"
|
|
|
|
#include "functions.h"
|
|
|
|
#include "window_func.h"
|
|
|
|
#include "date_func.h"
|
|
|
|
#include "vehicle_func.h"
|
|
|
|
#include "string_func.h"
|
|
|
|
#include "company_func.h"
|
|
|
|
#include "newgrf_station.h"
|
|
|
|
#include "viewport_func.h"
|
|
|
|
#include "train.h"
|
2009-07-23 00:14:05 +00:00
|
|
|
#include "water.h"
|
2009-03-13 21:49:24 +00:00
|
|
|
|
|
|
|
#include "table/strings.h"
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
2009-07-13 22:33:25 +00:00
|
|
|
Point pt = RemapCoords2(TileX(this->xy) * TILE_SIZE, TileY(this->xy) * TILE_SIZE);
|
|
|
|
SetDParam(0, this->index);
|
|
|
|
this->sign.UpdatePosition(pt.x, pt.y - 0x20, STR_WAYPOINT_VIEWPORT);
|
2009-03-13 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the default name for a waypoint
|
|
|
|
* @param wp Waypoint to work on
|
|
|
|
*/
|
2009-07-22 08:59:57 +00:00
|
|
|
void MakeDefaultWaypointName(Waypoint *wp)
|
2009-03-13 21:49:24 +00:00
|
|
|
{
|
|
|
|
uint32 used = 0; // bitmap of used waypoint numbers, sliding window with 'next' as base
|
|
|
|
uint32 next = 0; // first waypoint number in the bitmap
|
2009-07-28 21:06:38 +00:00
|
|
|
StationID idx = 0; // index where we will stop
|
2009-03-13 21:49:24 +00:00
|
|
|
|
2009-07-16 23:02:39 +00:00
|
|
|
wp->town = ClosestTownFromTile(wp->xy, UINT_MAX);
|
2009-03-13 21:49:24 +00:00
|
|
|
|
|
|
|
/* Find first unused waypoint number belonging to this town. This can never fail,
|
|
|
|
* as long as there can be at most 65535 waypoints in total.
|
|
|
|
*
|
|
|
|
* This does 'n * m' search, but with 32bit 'used' bitmap, it needs at most 'n * (1 + ceil(m / 32))'
|
|
|
|
* steps (n - number of waypoints in pool, m - number of waypoints near this town).
|
|
|
|
* Usually, it needs only 'n' steps.
|
|
|
|
*
|
|
|
|
* If it wasn't using 'used' and 'idx', it would just search for increasing 'next',
|
|
|
|
* but this way it is faster */
|
|
|
|
|
2009-07-28 21:06:38 +00:00
|
|
|
StationID cid = 0; // current index, goes to Waypoint::GetPoolSize()-1, then wraps to 0
|
2009-03-13 21:49:24 +00:00
|
|
|
do {
|
2009-07-23 12:02:09 +00:00
|
|
|
Waypoint *lwp = Waypoint::GetIfValid(cid);
|
2009-03-13 21:49:24 +00:00
|
|
|
|
|
|
|
/* check only valid waypoints... */
|
2009-05-22 15:13:50 +00:00
|
|
|
if (lwp != NULL && wp != lwp) {
|
2009-03-13 21:49:24 +00:00
|
|
|
/* only waypoints with 'generic' name within the same city */
|
2009-07-22 08:59:57 +00:00
|
|
|
if (lwp->name == NULL && lwp->town == wp->town && lwp->string_id == wp->string_id) {
|
2009-03-13 21:49:24 +00:00
|
|
|
/* if lwp->town_cn < next, uint will overflow to '+inf' */
|
|
|
|
uint i = (uint)lwp->town_cn - next;
|
|
|
|
|
|
|
|
if (i < 32) {
|
|
|
|
SetBit(used, i); // update bitmap
|
|
|
|
if (i == 0) {
|
|
|
|
/* shift bitmap while the lowest bit is '1';
|
|
|
|
* increase the base of the bitmap too */
|
|
|
|
do {
|
|
|
|
used >>= 1;
|
|
|
|
next++;
|
|
|
|
} while (HasBit(used, 0));
|
|
|
|
/* when we are at 'idx' again at end of the loop and
|
|
|
|
* 'next' hasn't changed, then no waypoint had town_cn == next,
|
|
|
|
* so we can safely use it */
|
|
|
|
idx = cid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cid++;
|
2009-05-16 23:44:36 +00:00
|
|
|
if (cid == Waypoint::GetPoolSize()) cid = 0; // wrap to zero...
|
2009-03-13 21:49:24 +00:00
|
|
|
} while (cid != idx);
|
|
|
|
|
|
|
|
wp->town_cn = (uint16)next; // set index...
|
|
|
|
wp->name = NULL; // ... and use generic name
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @return the deleted nearby waypoint
|
2009-03-13 21:49:24 +00:00
|
|
|
*/
|
2009-07-23 00:14:05 +00:00
|
|
|
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile, StringID str)
|
2009-03-13 21:49:24 +00:00
|
|
|
{
|
|
|
|
Waypoint *wp, *best = NULL;
|
|
|
|
uint thres = 8;
|
|
|
|
|
|
|
|
FOR_ALL_WAYPOINTS(wp) {
|
2009-07-24 07:38:10 +00:00
|
|
|
if (!wp->IsInUse() && wp->string_id == str && (wp->owner == _current_company || wp->owner == OWNER_NONE)) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Convert existing rail to waypoint. Eg build a waypoint station over
|
|
|
|
* piece of rail
|
|
|
|
* @param tile tile where waypoint will be built
|
|
|
|
* @param flags type of operation
|
|
|
|
* @param p1 graphics for waypoint type, 0 indicates standard graphics
|
|
|
|
* @param p2 unused
|
2009-07-23 00:14:05 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return cost of operation or error
|
2009-03-13 21:49:24 +00:00
|
|
|
*
|
|
|
|
* @todo When checking for the tile slope,
|
|
|
|
* distingush between "Flat land required" and "land sloped in wrong direction"
|
|
|
|
*/
|
2009-07-29 22:32:20 +00:00
|
|
|
CommandCost CmdBuildRailWaypoint(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2009-03-13 21:49:24 +00:00
|
|
|
{
|
|
|
|
Axis axis;
|
|
|
|
|
|
|
|
/* if custom gfx are used, make sure it is within bounds */
|
|
|
|
if (p1 >= GetNumCustomStations(STAT_CLASS_WAYP)) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (!IsTileType(tile, MP_RAILWAY) ||
|
|
|
|
GetRailTileType(tile) != RAIL_TILE_NORMAL || (
|
|
|
|
(axis = AXIS_X, GetTrackBits(tile) != TRACK_BIT_X) &&
|
|
|
|
(axis = AXIS_Y, GetTrackBits(tile) != TRACK_BIT_Y)
|
|
|
|
)) {
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK);
|
2009-03-13 21:49:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Owner owner = GetTileOwner(tile);
|
|
|
|
if (!CheckOwnership(owner)) return CMD_ERROR;
|
|
|
|
if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
|
|
|
|
|
2009-07-23 00:14:05 +00:00
|
|
|
Slope tileh = GetTileSlope(tile, NULL);
|
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-04-21 23:40:56 +00:00
|
|
|
if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
|
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-23 00:14:05 +00:00
|
|
|
Waypoint *wp = FindDeletedWaypointCloseTo(tile, STR_SV_STNAME_WAYPOINT);
|
|
|
|
if (wp == NULL && !Waypoint::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
|
2009-03-13 21:49:24 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
if (wp == NULL) {
|
|
|
|
wp = new Waypoint(tile);
|
|
|
|
} else {
|
|
|
|
/* Move existing (recently deleted) waypoint to the new location */
|
|
|
|
|
|
|
|
/* First we update the destination for all vehicles that
|
2009-03-14 18:16:29 +00:00
|
|
|
* have the old waypoint in their orders. */
|
2009-03-13 21:49:24 +00:00
|
|
|
Vehicle *v;
|
2009-05-26 22:45:48 +00:00
|
|
|
FOR_ALL_TRAINS(v) {
|
|
|
|
if (v->First() == v && v->current_order.IsType(OT_GOTO_WAYPOINT) &&
|
2009-03-13 21:49:24 +00:00
|
|
|
v->dest_tile == wp->xy) {
|
|
|
|
v->dest_tile = tile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wp->xy = tile;
|
|
|
|
InvalidateWindowData(WC_WAYPOINT_VIEW, wp->index);
|
|
|
|
}
|
|
|
|
wp->owner = owner;
|
|
|
|
|
2009-07-26 16:17:49 +00:00
|
|
|
wp->rect.BeforeAddTile(tile, StationRect::ADD_TRY);
|
|
|
|
|
2009-07-21 22:37:10 +00:00
|
|
|
bool reserved = HasBit(GetRailReservationTrackBits(tile), AxisToTrack(axis));
|
2009-07-22 08:59:57 +00:00
|
|
|
MakeRailWaypoint(tile, owner, wp->index, axis, 0, GetRailType(tile));
|
2009-07-24 11:47:12 +00:00
|
|
|
SetRailStationReservation(tile, reserved);
|
2009-03-13 21:49:24 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
|
2009-07-22 08:59:57 +00:00
|
|
|
SetCustomStationSpecIndex(tile, AllocateSpecToStation(GetCustomStationSpec(STAT_CLASS_WAYP, p1), wp, true));
|
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;
|
2009-03-13 21:49:24 +00:00
|
|
|
wp->build_date = _date;
|
2009-07-22 08:59:57 +00:00
|
|
|
wp->string_id = STR_SV_STNAME_WAYPOINT;
|
2009-07-29 22:09:05 +00:00
|
|
|
wp->train_station.tile = tile;
|
|
|
|
wp->train_station.w = 1;
|
|
|
|
wp->train_station.h = 1;
|
2009-03-13 21:49:24 +00:00
|
|
|
|
2009-07-16 23:02:39 +00:00
|
|
|
if (wp->town == NULL) MakeDefaultWaypointName(wp);
|
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
|
|
|
YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price.build_train_depot);
|
|
|
|
}
|
|
|
|
|
2009-07-23 00:14:05 +00:00
|
|
|
/** Build a buoy.
|
|
|
|
* @param tile tile where to place the bouy
|
|
|
|
* @param flags operation to perform
|
|
|
|
* @param p1 unused
|
|
|
|
* @param p2 unused
|
|
|
|
* @param text unused
|
|
|
|
* @return cost of operation or error
|
|
|
|
*/
|
|
|
|
CommandCost CmdBuildBuoy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
|
|
|
if (!IsWaterTile(tile) || tile == 0) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
|
|
|
|
if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
|
|
|
|
|
|
|
|
if (GetTileSlope(tile, NULL) != SLOPE_FLAT) 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);
|
|
|
|
if (wp == NULL && !Waypoint::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
if (wp == NULL) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
wp->build_date = _date;
|
|
|
|
|
|
|
|
if (wp->town == NULL) MakeDefaultWaypointName(wp);
|
|
|
|
|
|
|
|
MakeBuoy(tile, wp->index, GetWaterClass(tile));
|
|
|
|
|
|
|
|
wp->UpdateVirtCoord();
|
|
|
|
InvalidateWindowData(WC_WAYPOINT_VIEW, wp->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price.build_dock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
if (HasStationInUse(wp->index, INVALID_COMPANY)) return_cmd_error(STR_BUOY_IS_IN_USE);
|
|
|
|
/* remove the buoy if there is a ship on tile when company goes bankrupt... */
|
|
|
|
if (!(flags & DC_BANKRUPT) && !EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
|
|
|
|
|
|
|
|
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));
|
|
|
|
MarkTileDirtyByTile(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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_truck_station);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-13 21:49:24 +00:00
|
|
|
static bool IsUniqueWaypointName(const char *name)
|
|
|
|
{
|
|
|
|
const Waypoint *wp;
|
|
|
|
|
|
|
|
FOR_ALL_WAYPOINTS(wp) {
|
|
|
|
if (wp->name != NULL && strcmp(wp->name, name) == 0) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rename a waypoint.
|
|
|
|
* @param tile unused
|
|
|
|
* @param flags type of operation
|
|
|
|
* @param p1 id of waypoint
|
|
|
|
* @param p2 unused
|
2009-07-23 00:14:05 +00:00
|
|
|
* @param text the new name of the waypoint or an empty string when resetting to the default
|
2009-03-13 21:49:24 +00:00
|
|
|
* @return cost of operation or error
|
|
|
|
*/
|
|
|
|
CommandCost CmdRenameWaypoint(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
Waypoint *wp = Waypoint::GetIfValid(p1);
|
2009-07-22 08:59:57 +00:00
|
|
|
if (wp == NULL || !(CheckOwnership(wp->owner) || wp->owner == OWNER_NONE)) return CMD_ERROR;
|
2009-03-13 21:49:24 +00:00
|
|
|
|
|
|
|
bool reset = StrEmpty(text);
|
|
|
|
|
|
|
|
if (!reset) {
|
2009-07-28 21:06:38 +00:00
|
|
|
if (strlen(text) >= MAX_LENGTH_STATION_NAME_BYTES) return CMD_ERROR;
|
2009-03-13 21:49:24 +00:00
|
|
|
if (!IsUniqueWaypointName(text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
free(wp->name);
|
|
|
|
|
|
|
|
if (reset) {
|
|
|
|
MakeDefaultWaypointName(wp); // sets wp->name = NULL
|
|
|
|
} else {
|
|
|
|
wp->name = strdup(text);
|
|
|
|
}
|
|
|
|
|
2009-07-13 22:33:25 +00:00
|
|
|
wp->UpdateVirtCoord();
|
2009-03-13 21:49:24 +00:00
|
|
|
}
|
|
|
|
return CommandCost();
|
|
|
|
}
|