2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file waypoint.cpp Handling of waypoints. */
|
2007-04-06 04:10:19 +00:00
|
|
|
|
2005-03-24 17:03:37 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-03-24 17:03:37 +00:00
|
|
|
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2008-03-30 23:24:18 +00:00
|
|
|
#include "order_func.h"
|
2006-03-01 08:56:38 +00:00
|
|
|
#include "rail_map.h"
|
2007-12-25 23:42:52 +00:00
|
|
|
#include "rail.h"
|
2006-12-27 12:38:02 +00:00
|
|
|
#include "bridge_map.h"
|
2005-03-24 17:03:37 +00:00
|
|
|
#include "saveload.h"
|
2008-03-31 00:06:17 +00:00
|
|
|
#include "station_base.h"
|
2005-03-24 17:03:37 +00:00
|
|
|
#include "town.h"
|
|
|
|
#include "waypoint.h"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "variables.h"
|
2006-05-27 16:12:16 +00:00
|
|
|
#include "yapf/yapf.h"
|
2007-04-11 21:04:03 +00:00
|
|
|
#include "newgrf.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2008-01-09 09:57:48 +00:00
|
|
|
#include "gfx_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
|
|
|
#include "window_func.h"
|
2007-12-25 23:42:52 +00:00
|
|
|
#include "economy_func.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_func.h"
|
|
|
|
#include "vehicle_base.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2008-01-09 23:00:59 +00:00
|
|
|
#include "signal_func.h"
|
2008-01-12 14:10:35 +00:00
|
|
|
#include "player_func.h"
|
2008-01-13 14:37:30 +00:00
|
|
|
#include "settings_type.h"
|
2008-03-31 06:42:26 +00:00
|
|
|
#include "newgrf_station.h"
|
2008-04-06 23:49:45 +00:00
|
|
|
#include "oldpool_func.h"
|
2008-05-07 13:18:33 +00:00
|
|
|
#include "viewport_func.h"
|
2008-08-02 22:55:08 +00:00
|
|
|
#include "pbs.h"
|
|
|
|
#include "train.h"
|
2005-03-24 17:03:37 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
2008-01-09 23:00:59 +00:00
|
|
|
|
2007-08-02 12:22:40 +00:00
|
|
|
DEFINE_OLD_POOL_GENERIC(Waypoint, Waypoint)
|
2005-03-24 17:03:37 +00:00
|
|
|
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/**
|
|
|
|
* Update the sign for the waypoint
|
|
|
|
* @param wp Waypoint to update sign */
|
2006-01-05 12:40:50 +00:00
|
|
|
static void UpdateWaypointSign(Waypoint* wp)
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
2006-04-03 09:07:21 +00:00
|
|
|
Point pt = RemapCoords2(TileX(wp->xy) * TILE_SIZE, TileY(wp->xy) * TILE_SIZE);
|
2005-03-24 17:03:37 +00:00
|
|
|
SetDParam(0, wp->index);
|
|
|
|
UpdateViewportSignPos(&wp->sign, pt.x, pt.y - 0x20, STR_WAYPOINT_VIEWPORT);
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/**
|
|
|
|
* Redraw the sign of a waypoint
|
|
|
|
* @param wp Waypoint to redraw sign */
|
2005-12-14 06:20:23 +00:00
|
|
|
static void RedrawWaypointSign(const Waypoint* wp)
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
MarkAllViewportsDirty(
|
|
|
|
wp->sign.left - 6,
|
|
|
|
wp->sign.top,
|
|
|
|
wp->sign.left + (wp->sign.width_1 << 2) + 12,
|
|
|
|
wp->sign.top + 48);
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/**
|
|
|
|
* Update all signs
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void UpdateAllWaypointSigns()
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
Waypoint *wp;
|
|
|
|
|
|
|
|
FOR_ALL_WAYPOINTS(wp) {
|
2006-08-22 15:33:35 +00:00
|
|
|
UpdateWaypointSign(wp);
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/**
|
|
|
|
* Set the default name for a waypoint
|
|
|
|
* @param wp Waypoint to work on
|
|
|
|
*/
|
2005-12-14 06:20:23 +00:00
|
|
|
static void MakeDefaultWaypointName(Waypoint* wp)
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
2008-02-16 16:40:47 +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
|
|
|
|
WaypointID idx = 0; // index where we will stop
|
2005-03-24 17:03:37 +00:00
|
|
|
|
|
|
|
wp->town_index = ClosestTownFromTile(wp->xy, (uint)-1)->index;
|
|
|
|
|
2008-02-16 16:40:47 +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 */
|
|
|
|
|
|
|
|
WaypointID cid = 0; // current index, goes to GetWaypointPoolSize()-1, then wraps to 0
|
|
|
|
do {
|
|
|
|
Waypoint *lwp = GetWaypoint(cid);
|
|
|
|
|
|
|
|
/* check only valid waypoints... */
|
2008-04-18 04:54:09 +00:00
|
|
|
if (lwp->IsValid() && wp != lwp) {
|
2008-02-16 16:40:47 +00:00
|
|
|
/* only waypoints with 'generic' name within the same city */
|
|
|
|
if (lwp->name == NULL && lwp->town_index == wp->town_index) {
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-03-24 17:03:37 +00:00
|
|
|
|
2008-02-16 16:40:47 +00:00
|
|
|
cid++;
|
|
|
|
if (cid == GetWaypointPoolSize()) cid = 0; // wrap to zero...
|
|
|
|
} while (cid != idx);
|
2005-03-24 17:03:37 +00:00
|
|
|
|
2008-02-16 16:40:47 +00:00
|
|
|
wp->town_cn = (uint16)next; // set index...
|
|
|
|
wp->name = NULL; // ... and use generic name
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/**
|
|
|
|
* Find a deleted waypoint close to a tile.
|
|
|
|
* @param tile to search from
|
|
|
|
*/
|
2005-06-24 12:38:35 +00:00
|
|
|
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile)
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
Waypoint *wp, *best = NULL;
|
2006-06-27 21:25:53 +00:00
|
|
|
uint thres = 8;
|
2005-03-24 17:03:37 +00:00
|
|
|
|
|
|
|
FOR_ALL_WAYPOINTS(wp) {
|
2008-08-23 16:26:01 +00:00
|
|
|
if (wp->deleted && (wp->owner == OWNER_NONE || wp->owner == _current_player)) {
|
2006-06-27 21:25:53 +00:00
|
|
|
uint cur_dist = DistanceManhattan(tile, wp->xy);
|
|
|
|
|
2005-03-24 17:03:37 +00:00
|
|
|
if (cur_dist < thres) {
|
|
|
|
thres = cur_dist;
|
|
|
|
best = wp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2005-11-16 22:20:15 +00:00
|
|
|
/**
|
|
|
|
* Update waypoint graphics id against saved GRFID/localidx.
|
|
|
|
* This is to ensure the chosen graphics are correct if GRF files are changed.
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void AfterLoadWaypoints()
|
2005-11-16 22:20:15 +00:00
|
|
|
{
|
|
|
|
Waypoint *wp;
|
|
|
|
|
|
|
|
FOR_ALL_WAYPOINTS(wp) {
|
|
|
|
uint i;
|
|
|
|
|
|
|
|
if (wp->grfid == 0) continue;
|
|
|
|
|
|
|
|
for (i = 0; i < GetNumCustomStations(STAT_CLASS_WAYP); i++) {
|
2006-04-27 18:28:56 +00:00
|
|
|
const StationSpec *statspec = GetCustomStationSpec(STAT_CLASS_WAYP, i);
|
2007-04-11 21:04:03 +00:00
|
|
|
if (statspec != NULL && statspec->grffile->grfid == wp->grfid && statspec->localidx == wp->localidx) {
|
2005-11-16 22:20:15 +00:00
|
|
|
wp->stat_id = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-09 16:37:40 +00:00
|
|
|
/** Convert existing rail to waypoint. Eg build a waypoint station over
|
|
|
|
* piece of rail
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile tile where waypoint will be built
|
2007-04-06 04:10:19 +00:00
|
|
|
* @param flags type of operation
|
2005-11-12 00:19:34 +00:00
|
|
|
* @param p1 graphics for waypoint type, 0 indicates standard graphics
|
2005-05-09 16:37:40 +00:00
|
|
|
* @param p2 unused
|
2005-07-16 23:47:37 +00:00
|
|
|
*
|
|
|
|
* @todo When checking for the tile slope,
|
|
|
|
* distingush between "Flat land required" and "land sloped in wrong direction"
|
2005-05-09 16:37:40 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdBuildTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
Waypoint *wp;
|
2006-04-23 13:48:16 +00:00
|
|
|
Slope tileh;
|
2006-03-01 08:56:38 +00:00
|
|
|
Axis axis;
|
2005-03-24 17:03:37 +00:00
|
|
|
|
2005-05-09 16:37:40 +00:00
|
|
|
/* if custom gfx are used, make sure it is within bounds */
|
2005-11-12 00:19:34 +00:00
|
|
|
if (p1 >= GetNumCustomStations(STAT_CLASS_WAYP)) return CMD_ERROR;
|
2005-05-09 16:37:40 +00:00
|
|
|
|
2006-03-19 12:06:12 +00:00
|
|
|
if (!IsTileType(tile, MP_RAILWAY) ||
|
2006-05-09 08:17:33 +00:00
|
|
|
GetRailTileType(tile) != RAIL_TILE_NORMAL || (
|
2006-03-19 12:06:12 +00:00
|
|
|
(axis = AXIS_X, GetTrackBits(tile) != TRACK_BIT_X) &&
|
|
|
|
(axis = AXIS_Y, GetTrackBits(tile) != TRACK_BIT_Y)
|
2006-03-01 08:56:38 +00:00
|
|
|
)) {
|
2005-03-24 17:03:37 +00:00
|
|
|
return_cmd_error(STR_1005_NO_SUITABLE_RAILROAD_TRACK);
|
2006-03-01 08:56:38 +00:00
|
|
|
}
|
2005-03-24 17:03:37 +00:00
|
|
|
|
2008-08-23 02:15:46 +00:00
|
|
|
Owner owner = GetTileOwner(tile);
|
|
|
|
if (!CheckOwnership(owner)) return CMD_ERROR;
|
2007-10-19 22:46:55 +00:00
|
|
|
if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
|
2005-03-24 17:03:37 +00:00
|
|
|
|
|
|
|
tileh = GetTileSlope(tile, NULL);
|
2006-06-27 21:25:53 +00:00
|
|
|
if (tileh != SLOPE_FLAT &&
|
2008-05-29 15:13:28 +00:00
|
|
|
(!_settings_game.construction.build_on_slopes || IsSteepSlope(tileh) || !(tileh & (0x3 << axis)) || !(tileh & ~(0x3 << axis)))) {
|
2006-06-27 21:25:53 +00:00
|
|
|
return_cmd_error(STR_0007_FLAT_LAND_REQUIRED);
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
|
|
|
|
2006-12-27 12:38:02 +00:00
|
|
|
if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
|
|
|
|
|
2005-03-24 17:03:37 +00:00
|
|
|
/* Check if there is an already existing, deleted, waypoint close to us that we can reuse. */
|
|
|
|
wp = FindDeletedWaypointCloseTo(tile);
|
2008-04-23 20:56:08 +00:00
|
|
|
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->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_VEHICLES(v) {
|
|
|
|
if (v->type == VEH_TRAIN &&
|
|
|
|
v->First() == v &&
|
|
|
|
v->current_order.IsType(OT_GOTO_WAYPOINT) &&
|
|
|
|
v->dest_tile == wp->xy) {
|
|
|
|
v->dest_tile = tile;
|
|
|
|
}
|
2007-10-09 21:29:34 +00:00
|
|
|
}
|
|
|
|
|
2008-04-23 20:56:08 +00:00
|
|
|
RedrawWaypointSign(wp);
|
|
|
|
wp->xy = tile;
|
2008-08-23 16:34:05 +00:00
|
|
|
InvalidateWindowData(WC_WAYPOINT_VIEW, wp->index);
|
2008-04-23 20:56:08 +00:00
|
|
|
}
|
2008-08-23 19:14:27 +00:00
|
|
|
wp->owner = owner;
|
2005-03-24 17:03:37 +00:00
|
|
|
|
2006-06-21 11:13:02 +00:00
|
|
|
const StationSpec* statspec;
|
|
|
|
|
2008-08-02 22:48:27 +00:00
|
|
|
bool reserved = HasBit(GetTrackReservation(tile), AxisToTrack(axis));
|
2008-08-23 02:15:46 +00:00
|
|
|
MakeRailWaypoint(tile, owner, axis, GetRailType(tile), wp->index);
|
2008-08-02 22:48:27 +00:00
|
|
|
SetDepotWaypointReservation(tile, reserved);
|
2006-03-01 08:56:38 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
2005-11-16 22:20:15 +00:00
|
|
|
|
2006-06-21 11:13:02 +00:00
|
|
|
statspec = GetCustomStationSpec(STAT_CLASS_WAYP, p1);
|
2005-11-16 22:20:15 +00:00
|
|
|
|
2006-04-27 18:28:56 +00:00
|
|
|
if (statspec != NULL) {
|
2006-06-21 11:13:02 +00:00
|
|
|
wp->stat_id = p1;
|
2007-04-11 21:04:03 +00:00
|
|
|
wp->grfid = statspec->grffile->grfid;
|
2006-04-27 18:28:56 +00:00
|
|
|
wp->localidx = statspec->localidx;
|
2005-11-16 22:20:15 +00:00
|
|
|
} else {
|
2007-04-06 04:10:19 +00:00
|
|
|
/* Specified custom graphics do not exist, so use default. */
|
2005-11-16 22:20:15 +00:00
|
|
|
wp->stat_id = 0;
|
|
|
|
wp->grfid = 0;
|
|
|
|
wp->localidx = 0;
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
2005-11-16 22:20:15 +00:00
|
|
|
|
2005-03-24 17:03:37 +00:00
|
|
|
wp->deleted = 0;
|
|
|
|
wp->build_date = _date;
|
|
|
|
|
2008-02-07 01:56:39 +00:00
|
|
|
if (wp->town_index == INVALID_TOWN) MakeDefaultWaypointName(wp);
|
2005-03-24 17:03:37 +00:00
|
|
|
|
|
|
|
UpdateWaypointSign(wp);
|
|
|
|
RedrawWaypointSign(wp);
|
2006-12-20 23:41:24 +00:00
|
|
|
YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
|
|
|
|
2008-01-09 16:55:48 +00:00
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price.build_train_depot);
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/**
|
|
|
|
* Daily loop for waypoints
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void WaypointsDailyLoop()
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
Waypoint *wp;
|
|
|
|
|
|
|
|
/* Check if we need to delete a waypoint */
|
|
|
|
FOR_ALL_WAYPOINTS(wp) {
|
2008-09-07 18:21:57 +00:00
|
|
|
if (wp->deleted != 0 && --wp->deleted == 0) delete wp;
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost RemoveTrainWaypoint(TileIndex tile, uint32 flags, bool justremove)
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
Waypoint *wp;
|
|
|
|
|
|
|
|
/* Make sure it's a waypoint */
|
2008-09-09 12:26:25 +00:00
|
|
|
if (!IsRailWaypointTile(tile) ||
|
2006-06-27 21:25:53 +00:00
|
|
|
(!CheckTileOwnership(tile) && _current_player != OWNER_WATER) ||
|
2007-10-19 22:46:55 +00:00
|
|
|
!EnsureNoVehicleOnGround(tile)) {
|
2005-03-24 17:03:37 +00:00
|
|
|
return CMD_ERROR;
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-03-24 17:03:37 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2006-12-20 23:41:24 +00:00
|
|
|
Track track = GetRailWaypointTrack(tile);
|
2005-03-24 17:03:37 +00:00
|
|
|
wp = GetWaypointByTile(tile);
|
|
|
|
|
|
|
|
wp->deleted = 30; // let it live for this many days before we do the actual deletion.
|
|
|
|
RedrawWaypointSign(wp);
|
|
|
|
|
2008-08-02 22:55:08 +00:00
|
|
|
Vehicle *v = NULL;
|
2005-03-24 17:03:37 +00:00
|
|
|
if (justremove) {
|
2008-08-02 22:48:27 +00:00
|
|
|
TrackBits tracks = GetRailWaypointBits(tile);
|
|
|
|
bool reserved = GetDepotWaypointReservation(tile);
|
2008-08-23 02:15:46 +00:00
|
|
|
MakeRailNormal(tile, wp->owner, tracks, GetRailType(tile));
|
2008-08-02 22:48:27 +00:00
|
|
|
if (reserved) SetTrackReservation(tile, tracks);
|
2006-03-01 08:56:38 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
2005-03-24 17:03:37 +00:00
|
|
|
} else {
|
2008-08-02 22:55:08 +00:00
|
|
|
if (GetDepotWaypointReservation(tile)) {
|
|
|
|
v = GetTrainForReservation(tile, track);
|
|
|
|
if (v != NULL) FreeTrainTrackReservation(v);
|
|
|
|
}
|
2005-03-24 17:03:37 +00:00
|
|
|
DoClearSquare(tile);
|
2008-08-23 02:15:46 +00:00
|
|
|
AddTrackToSignalBuffer(tile, track, wp->owner);
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
2006-12-20 23:41:24 +00:00
|
|
|
YapfNotifyTrackLayoutChange(tile, track);
|
2008-08-02 22:55:08 +00:00
|
|
|
if (v != NULL) TryPathReserve(v, true);
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
|
|
|
|
2008-01-09 16:55:48 +00:00
|
|
|
return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_train_depot);
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/**
|
|
|
|
* Delete a waypoint
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile tile where waypoint is to be deleted
|
2007-04-06 04:10:19 +00:00
|
|
|
* @param flags type of operation
|
2005-05-09 16:37:40 +00:00
|
|
|
* @param p1 unused
|
|
|
|
* @param p2 unused
|
2007-04-06 04:10:19 +00:00
|
|
|
* @return cost of operation or error
|
2005-05-09 16:37:40 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdRemoveTrainWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
return RemoveTrainWaypoint(tile, flags, true);
|
|
|
|
}
|
|
|
|
|
2007-06-27 20:53:25 +00:00
|
|
|
static bool IsUniqueWaypointName(const char *name)
|
|
|
|
{
|
|
|
|
const Waypoint *wp;
|
|
|
|
char buf[512];
|
|
|
|
|
|
|
|
FOR_ALL_WAYPOINTS(wp) {
|
|
|
|
SetDParam(0, wp->index);
|
|
|
|
GetString(buf, STR_WAYPOINT_RAW, lastof(buf));
|
|
|
|
if (strcmp(buf, name) == 0) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/**
|
|
|
|
* Rename a waypoint.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-06 04:10:19 +00:00
|
|
|
* @param flags type of operation
|
2005-05-09 16:37:40 +00:00
|
|
|
* @param p1 id of waypoint
|
|
|
|
* @param p2 unused
|
2007-04-06 04:10:19 +00:00
|
|
|
* @return cost of operation or error
|
2005-05-09 16:37:40 +00:00
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdRenameWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
Waypoint *wp;
|
|
|
|
|
2006-08-22 18:15:17 +00:00
|
|
|
if (!IsValidWaypointID(p1)) return CMD_ERROR;
|
2005-05-09 16:37:40 +00:00
|
|
|
|
2007-06-27 23:40:21 +00:00
|
|
|
wp = GetWaypoint(p1);
|
|
|
|
if (!CheckTileOwnership(wp->xy)) return CMD_ERROR;
|
|
|
|
|
2007-06-27 20:53:25 +00:00
|
|
|
if (!StrEmpty(_cmd_text)) {
|
2008-08-13 06:22:04 +00:00
|
|
|
if (strlen(_cmd_text) >= MAX_LENGTH_WAYPOINT_NAME_BYTES) return CMD_ERROR;
|
2007-06-27 20:53:25 +00:00
|
|
|
if (!IsUniqueWaypointName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
|
|
|
|
|
2005-03-24 17:03:37 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-01-12 19:58:06 +00:00
|
|
|
free(wp->name);
|
|
|
|
wp->name = strdup(_cmd_text);
|
2005-03-24 17:03:37 +00:00
|
|
|
wp->town_cn = 0;
|
|
|
|
|
|
|
|
UpdateWaypointSign(wp);
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
2005-07-08 22:25:24 +00:00
|
|
|
} else {
|
2005-03-24 17:03:37 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-01-12 19:58:06 +00:00
|
|
|
free(wp->name);
|
2005-03-24 17:03:37 +00:00
|
|
|
|
|
|
|
MakeDefaultWaypointName(wp);
|
|
|
|
UpdateWaypointSign(wp);
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/**
|
|
|
|
* This hacks together some dummy one-shot Station structure for a waypoint.
|
|
|
|
* @param tile on which to work
|
|
|
|
* @return pointer to a Station
|
|
|
|
*/
|
2005-06-24 12:38:35 +00:00
|
|
|
Station *ComposeWaypointStation(TileIndex tile)
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
Waypoint *wp = GetWaypointByTile(tile);
|
2007-01-19 16:01:43 +00:00
|
|
|
|
|
|
|
/* instead of 'static Station stat' use byte array to avoid Station's destructor call upon exit. As
|
|
|
|
* a side effect, the station is not constructed now. */
|
2007-01-19 22:41:50 +00:00
|
|
|
static byte stat_raw[sizeof(Station)];
|
2007-01-19 16:01:43 +00:00
|
|
|
static Station &stat = *(Station*)stat_raw;
|
2005-03-24 17:03:37 +00:00
|
|
|
|
|
|
|
stat.train_tile = stat.xy = wp->xy;
|
|
|
|
stat.town = GetTown(wp->town_index);
|
|
|
|
stat.build_date = wp->build_date;
|
|
|
|
|
|
|
|
return &stat;
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/**
|
|
|
|
* Draw a waypoint
|
|
|
|
* @param x coordinate
|
|
|
|
* @param y coordinate
|
|
|
|
* @param stat_id station id
|
|
|
|
* @param railtype RailType to use for
|
|
|
|
*/
|
2005-10-16 09:13:04 +00:00
|
|
|
void DrawWaypointSprite(int x, int y, int stat_id, RailType railtype)
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
x += 33;
|
|
|
|
y += 17;
|
|
|
|
|
2006-05-06 20:48:40 +00:00
|
|
|
if (!DrawStationTile(x, y, railtype, AXIS_X, STAT_CLASS_WAYP, stat_id)) {
|
2005-08-01 16:31:19 +00:00
|
|
|
DrawDefaultWaypointSprite(x, y, railtype);
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-02 12:22:40 +00:00
|
|
|
Waypoint::Waypoint(TileIndex tile)
|
|
|
|
{
|
|
|
|
this->xy = tile;
|
|
|
|
}
|
|
|
|
|
|
|
|
Waypoint::~Waypoint()
|
|
|
|
{
|
2008-01-12 19:58:06 +00:00
|
|
|
free(this->name);
|
2007-08-05 21:20:55 +00:00
|
|
|
|
|
|
|
if (CleaningPool()) return;
|
2008-08-21 02:19:31 +00:00
|
|
|
DeleteWindowById(WC_WAYPOINT_VIEW, this->index);
|
2007-08-02 12:22:40 +00:00
|
|
|
RemoveOrderFromAllVehicles(OT_GOTO_WAYPOINT, this->index);
|
|
|
|
|
|
|
|
RedrawWaypointSign(this);
|
|
|
|
this->xy = 0;
|
|
|
|
}
|
|
|
|
|
2007-04-06 04:10:19 +00:00
|
|
|
/**
|
|
|
|
* Fix savegames which stored waypoints in their old format
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
void FixOldWaypoints()
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
Waypoint *wp;
|
|
|
|
|
|
|
|
/* Convert the old 'town_or_string', to 'string' / 'town' / 'town_cn' */
|
|
|
|
FOR_ALL_WAYPOINTS(wp) {
|
|
|
|
wp->town_index = ClosestTownFromTile(wp->xy, (uint)-1)->index;
|
|
|
|
wp->town_cn = 0;
|
|
|
|
if (wp->string & 0xC000) {
|
|
|
|
wp->town_cn = wp->string & 0x3F;
|
|
|
|
wp->string = STR_NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void InitializeWaypoints()
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
2007-08-02 12:22:40 +00:00
|
|
|
_Waypoint_pool.CleanPool();
|
|
|
|
_Waypoint_pool.AddBlockToPool();
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
|
|
|
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _waypoint_desc[] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_CONDVAR(Waypoint, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
|
|
|
|
SLE_CONDVAR(Waypoint, xy, SLE_UINT32, 6, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Waypoint, town_index, SLE_UINT16, 12, SL_MAX_VERSION),
|
2008-02-16 16:40:47 +00:00
|
|
|
SLE_CONDVAR(Waypoint, town_cn, SLE_FILE_U8 | SLE_VAR_U16, 12, 88),
|
|
|
|
SLE_CONDVAR(Waypoint, town_cn, SLE_UINT16, 89, SL_MAX_VERSION),
|
2008-01-12 19:58:06 +00:00
|
|
|
SLE_CONDVAR(Waypoint, string, SLE_STRINGID, 0, 83),
|
|
|
|
SLE_CONDSTR(Waypoint, name, SLE_STR, 0, 84, SL_MAX_VERSION),
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Waypoint, deleted, SLE_UINT8),
|
|
|
|
|
|
|
|
SLE_CONDVAR(Waypoint, build_date, SLE_FILE_U16 | SLE_VAR_I32, 3, 30),
|
|
|
|
SLE_CONDVAR(Waypoint, build_date, SLE_INT32, 31, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Waypoint, localidx, SLE_UINT8, 3, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Waypoint, grfid, SLE_UINT32, 17, SL_MAX_VERSION),
|
2008-08-23 02:15:46 +00:00
|
|
|
SLE_CONDVAR(Waypoint, owner, SLE_UINT8, 101, SL_MAX_VERSION),
|
2005-03-24 17:03:37 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void Save_WAYP()
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
Waypoint *wp;
|
|
|
|
|
|
|
|
FOR_ALL_WAYPOINTS(wp) {
|
2006-08-22 15:33:35 +00:00
|
|
|
SlSetArrayIndex(wp->index);
|
|
|
|
SlObject(wp, _waypoint_desc);
|
2005-03-24 17:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void Load_WAYP()
|
2005-03-24 17:03:37 +00:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
while ((index = SlIterateArray()) != -1) {
|
2007-08-02 12:22:40 +00:00
|
|
|
Waypoint *wp = new (index) Waypoint();
|
2005-03-24 17:03:37 +00:00
|
|
|
SlObject(wp, _waypoint_desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
extern const ChunkHandler _waypoint_chunk_handlers[] = {
|
2005-03-24 17:03:37 +00:00
|
|
|
{ 'CHKP', Save_WAYP, Load_WAYP, CH_ARRAY | CH_LAST},
|
|
|
|
};
|