|
@@ -24,68 +24,68 @@
|
|
|
#include "train.h"
|
|
|
|
|
|
#include "table/strings.h"
|
|
|
|
|
|
/**
|
|
|
* Update the virtual coords needed to draw the waypoint sign.
|
|
|
*/
|
|
|
void Waypoint::UpdateVirtCoord()
|
|
|
{
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Set the default name for a waypoint
|
|
|
* @param wp Waypoint to work on
|
|
|
*/
|
|
|
static void MakeDefaultWaypointName(Waypoint *wp)
|
|
|
{
|
|
|
uint32 used = 0; // bitmap of used waypoint numbers, sliding window with 'next' as base
|
|
|
uint32 next = 0; // first waypoint number in the bitmap
|
|
|
WaypointID idx = 0; // index where we will stop
|
|
|
|
|
|
wp->town_index = ClosestTownFromTile(wp->xy, UINT_MAX)->index;
|
|
|
wp->town = ClosestTownFromTile(wp->xy, UINT_MAX);
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
WaypointID cid = 0; // current index, goes to Waypoint::GetPoolSize()-1, then wraps to 0
|
|
|
do {
|
|
|
Waypoint *lwp = Waypoint::Get(cid);
|
|
|
|
|
|
/* check only valid waypoints... */
|
|
|
if (lwp != NULL && wp != lwp) {
|
|
|
/* only waypoints with 'generic' name within the same city */
|
|
|
if (lwp->name == NULL && lwp->town_index == wp->town_index) {
|
|
|
if (lwp->name == NULL && lwp->town == wp->town) {
|
|
|
/* 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++;
|
|
|
if (cid == Waypoint::GetPoolSize()) cid = 0; // wrap to zero...
|
|
|
} while (cid != idx);
|
|
@@ -143,91 +143,91 @@ CommandCost CmdBuildTrainWaypoint(TileIn
|
|
|
)) {
|
|
|
return_cmd_error(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK);
|
|
|
}
|
|
|
|
|
|
Owner owner = GetTileOwner(tile);
|
|
|
if (!CheckOwnership(owner)) return CMD_ERROR;
|
|
|
if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
|
|
|
|
|
|
tileh = GetTileSlope(tile, NULL);
|
|
|
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);
|
|
|
|
|
|
/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
|
|
|
wp = FindDeletedWaypointCloseTo(tile);
|
|
|
if (wp == NULL && !Waypoint::CanAllocateItem()) return CMD_ERROR;
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
if (wp == NULL) {
|
|
|
wp = new Waypoint(tile);
|
|
|
|
|
|
wp->town_index = INVALID_TOWN;
|
|
|
wp->town = NULL;
|
|
|
wp->name = NULL;
|
|
|
wp->town_cn = 0;
|
|
|
} else {
|
|
|
/* Move existing (recently deleted) waypoint to the new location */
|
|
|
|
|
|
/* First we update the destination for all vehicles that
|
|
|
* have the old waypoint in their orders. */
|
|
|
Vehicle *v;
|
|
|
FOR_ALL_TRAINS(v) {
|
|
|
if (v->First() == v && v->current_order.IsType(OT_GOTO_WAYPOINT) &&
|
|
|
v->dest_tile == wp->xy) {
|
|
|
v->dest_tile = tile;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
wp->xy = tile;
|
|
|
InvalidateWindowData(WC_WAYPOINT_VIEW, wp->index);
|
|
|
}
|
|
|
wp->owner = owner;
|
|
|
|
|
|
bool reserved = HasBit(GetTrackReservation(tile), AxisToTrack(axis));
|
|
|
MakeRailWaypoint(tile, owner, axis, GetRailType(tile), wp->index);
|
|
|
SetDepotWaypointReservation(tile, reserved);
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
|
|
const StationSpec *statspec = GetCustomStationSpec(STAT_CLASS_WAYP, p1);
|
|
|
|
|
|
if (statspec != NULL) {
|
|
|
wp->spec.spec = statspec;
|
|
|
wp->spec.grfid = statspec->grffile->grfid;
|
|
|
wp->spec.localidx = statspec->localidx;
|
|
|
} else {
|
|
|
/* Specified custom graphics do not exist, so use default. */
|
|
|
wp->spec.spec = NULL;
|
|
|
wp->spec.grfid = 0;
|
|
|
wp->spec.localidx = 0;
|
|
|
}
|
|
|
|
|
|
wp->delete_ctr = 0;
|
|
|
wp->build_date = _date;
|
|
|
|
|
|
if (wp->town_index == INVALID_TOWN) MakeDefaultWaypointName(wp);
|
|
|
if (wp->town == NULL) MakeDefaultWaypointName(wp);
|
|
|
|
|
|
wp->UpdateVirtCoord();
|
|
|
YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
|
|
|
}
|
|
|
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price.build_train_depot);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* Remove a waypoint
|
|
|
* @param tile from which to remove waypoint
|
|
|
* @param flags type of operation
|
|
|
* @param justremove will indicate if it is removed from rail or if rails are removed too
|
|
|
* @return cost of operation or error
|
|
|
*/
|
|
|
CommandCost RemoveTrainWaypoint(TileIndex tile, DoCommandFlag flags, bool justremove)
|
|
|
{
|
|
|
Waypoint *wp;
|
|
|
|
|
|
/* Make sure it's a waypoint */
|
|
|
if (!IsRailWaypointTile(tile) ||
|
|
|
(!CheckTileOwnership(tile) && _current_company != OWNER_WATER) ||
|
|
|
!EnsureNoVehicleOnGround(tile)) {
|
|
|
return CMD_ERROR;
|