Changeset - r12476:b14b8e966cd2
[Not reviewed]
master
0 3 0
rubidium - 15 years ago 2009-07-23 00:14:05
rubidium@openttd.org
(svn r16923) -Codechange: move Cmd*Buoy* to waypoint_cmd.cpp and make them behave more like waypoints, e.g. reuse station signs of recently deleted buoys
3 files changed with 100 insertions and 83 deletions:
0 comments (0 inline, 0 general)
src/station_cmd.cpp
Show inline comments
 
@@ -1940,50 +1940,12 @@ static CommandCost RemoveAirport(TileInd
 
		DeleteStationIfEmpty(st);
 
	}
 

	
 
	return cost;
 
}
 

	
 
/** Build a buoy.
 
 * @param tile tile where to place the bouy
 
 * @param flags operation to perform
 
 * @param p1 unused
 
 * @param p2 unused
 
 */
 
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);
 

	
 
	/* allocate and initialize new station */
 
	if (!Waypoint::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
 

	
 
	if (flags & DC_EXEC) {
 
		Waypoint *st = new Waypoint(tile);
 

	
 
		st->string_id = STR_SV_STNAME_BUOY;
 

	
 
		st->facilities |= FACIL_DOCK;
 
		st->owner = OWNER_NONE;
 

	
 
		st->build_date = _date;
 

	
 
		if (st->town == NULL) MakeDefaultWaypointName(st);
 

	
 
		MakeBuoy(tile, st->index, GetWaterClass(tile));
 

	
 
		st->UpdateVirtCoord();
 
		InvalidateWindowData(WC_STATION_LIST, st->owner, 0);
 
		InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_SHIPS);
 
	}
 

	
 
	return CommandCost(EXPENSES_CONSTRUCTION, _price.build_dock);
 
}
 

	
 
/**
 
 * Tests whether the company's vehicles have this station in orders
 
 * When company == INVALID_COMPANY, then check all vehicles
 
 * @param station station ID
 
 * @param company company ID, INVALID_COMPANY to disable the check
 
 */
 
@@ -2000,47 +1962,12 @@ bool HasStationInUse(StationID station, 
 
			}
 
		}
 
	}
 
	return false;
 
}
 

	
 
/**
 
 * Remove a buoy
 
 * @param tile TileIndex been queried
 
 * @param flags operation to perform
 
 * @return cost or failure of operation
 
 */
 
static 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 *st = Waypoint::GetByTile(tile);
 

	
 
	if (HasStationInUse(st->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) {
 
		st->facilities &= ~FACIL_DOCK;
 

	
 
		InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_SHIPS);
 

	
 
		/* 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);
 

	
 
		st->UpdateVirtCoord();
 
		st->delete_ctr = 0;
 
	}
 

	
 
	return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_truck_station);
 
}
 

	
 
static const TileIndexDiffC _dock_tileoffs_chkaround[] = {
 
	{-1,  0},
 
	{ 0,  0},
 
	{ 0,  0},
 
	{ 0, -1}
 
};
src/waypoint_cmd.cpp
Show inline comments
 
@@ -19,12 +19,13 @@
 
#include "vehicle_func.h"
 
#include "string_func.h"
 
#include "company_func.h"
 
#include "newgrf_station.h"
 
#include "viewport_func.h"
 
#include "train.h"
 
#include "water.h"
 

	
 
#include "table/strings.h"
 

	
 
/**
 
 * Update the virtual coords needed to draw the waypoint sign.
 
 */
 
@@ -94,20 +95,22 @@ void MakeDefaultWaypointName(Waypoint *w
 
	wp->name = NULL; // ... and use generic name
 
}
 

	
 
/**
 
 * Find a deleted waypoint close to a tile.
 
 * @param tile to search from
 
 * @param str  the string to get the 'type' of
 
 * @return the deleted nearby waypoint
 
 */
 
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile)
 
static Waypoint *FindDeletedWaypointCloseTo(TileIndex tile, StringID str)
 
{
 
	Waypoint *wp, *best = NULL;
 
	uint thres = 8;
 

	
 
	FOR_ALL_WAYPOINTS(wp) {
 
		if ((wp->facilities & ~FACIL_WAYPOINT) == 0 && wp->owner == _current_company) {
 
		if ((wp->facilities & ~FACIL_WAYPOINT) == 0 && wp->string_id == str && (wp->owner == _current_company || wp->owner == OWNER_NONE)) {
 
			uint cur_dist = DistanceManhattan(tile, wp->xy);
 

	
 
			if (cur_dist < thres) {
 
				thres = cur_dist;
 
				best = wp;
 
			}
 
@@ -120,20 +123,20 @@ static Waypoint *FindDeletedWaypointClos
 
/** 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
 
 * @param text unused
 
 * @return cost of operation or error
 
 *
 
 * @todo When checking for the tile slope,
 
 * distingush between "Flat land required" and "land sloped in wrong direction"
 
 */
 
CommandCost CmdBuildTrainWaypoint(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Waypoint *wp;
 
	Slope tileh;
 
	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) ||
 
@@ -145,23 +148,23 @@ CommandCost CmdBuildTrainWaypoint(TileIn
 
	}
 

	
 
	Owner owner = GetTileOwner(tile);
 
	if (!CheckOwnership(owner)) return CMD_ERROR;
 
	if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
 

	
 
	tileh = GetTileSlope(tile, NULL);
 
	Slope 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;
 
	Waypoint *wp = FindDeletedWaypointCloseTo(tile, STR_SV_STNAME_WAYPOINT);
 
	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) waypoint to the new location */
 
@@ -204,28 +207,27 @@ CommandCost CmdBuildTrainWaypoint(TileIn
 

	
 
/**
 
 * 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
 
 * @pre IsRailWaypointTile(tile)
 
 * @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;
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		Track track = GetRailStationTrack(tile);
 
		wp = Waypoint::GetByTile(tile);
 
		Waypoint *wp = Waypoint::GetByTile(tile);
 

	
 
		wp->sign.MarkDirty();
 
		wp->facilities &= ~FACIL_TRAIN;
 

	
 
		Train *v = NULL;
 
		uint specindex = GetCustomStationSpecIndex(tile);
 
@@ -255,19 +257,104 @@ CommandCost RemoveTrainWaypoint(TileInde
 
/**
 
 * Delete a waypoint
 
 * @param tile tile where waypoint is to be deleted
 
 * @param flags type of operation
 
 * @param p1 unused
 
 * @param p2 unused
 
 * @param text unused
 
 * @return cost of operation or error
 
 */
 
CommandCost CmdRemoveTrainWaypoint(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	return RemoveTrainWaypoint(tile, flags, true);
 
}
 

	
 

	
 
/** 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);
 
		}
 

	
 
		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);
 

	
 
		wp->UpdateVirtCoord();
 
		wp->delete_ctr = 0;
 
	}
 

	
 
	return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_truck_station);
 
}
 

	
 

	
 
static bool IsUniqueWaypointName(const char *name)
 
{
 
	const Waypoint *wp;
 

	
 
	FOR_ALL_WAYPOINTS(wp) {
 
		if (wp->name != NULL && strcmp(wp->name, name) == 0) return false;
 
@@ -279,12 +366,13 @@ static bool IsUniqueWaypointName(const c
 
/**
 
 * Rename a waypoint.
 
 * @param tile unused
 
 * @param flags type of operation
 
 * @param p1 id of waypoint
 
 * @param p2 unused
 
 * @param text the new name of the waypoint or an empty string when resetting to the default
 
 * @return cost of operation or error
 
 */
 
CommandCost CmdRenameWaypoint(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Waypoint *wp = Waypoint::GetIfValid(p1);
 
	if (wp == NULL || !(CheckOwnership(wp->owner) || wp->owner == OWNER_NONE)) return CMD_ERROR;
src/waypoint_func.h
Show inline comments
 
@@ -7,11 +7,13 @@
 

	
 
#include "rail_type.h"
 
#include "command_type.h"
 
#include "waypoint_type.h"
 

	
 
CommandCost RemoveTrainWaypoint(TileIndex tile, DoCommandFlag flags, bool justremove);
 
CommandCost RemoveBuoy(TileIndex tile, DoCommandFlag flags);
 

	
 
void ShowWaypointWindow(const Waypoint *wp);
 
void DrawWaypointSprite(int x, int y, int stat_id, RailType railtype);
 
void MakeDefaultWaypointName(Waypoint *wp);
 

	
 
#endif /* WAYPOINT_FUNC_H */
0 comments (0 inline, 0 general)