changeset 12562:e6b8a343eb13 draft

(svn r17000) -Change: allow overbuilding/extending waypoints
author rubidium <rubidium@openttd.org>
date Thu, 30 Jul 2009 22:06:54 +0000
parents ea67e4c6c497
children bad474e5cd7e
files src/direction_type.h src/lang/english.txt src/rail_gui.cpp src/station_cmd.cpp src/waypoint_cmd.cpp src/waypoint_func.h
diffstat 6 files changed, 112 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/direction_type.h
+++ b/src/direction_type.h
@@ -118,9 +118,10 @@
  * align the north-east edge (and south-west) edge.
  */
 enum Axis {
-	AXIS_X = 0,     ///< The X axis
-	AXIS_Y = 1,     ///< The y axis
-	AXIS_END        ///< Used for iterations
+	AXIS_X = 0,          ///< The X axis
+	AXIS_Y = 1,          ///< The y axis
+	AXIS_END,            ///< Used for iterations
+	INVALID_AXIS = 0xFF, ///< Flag for an invalid Axis
 };
 
 #endif /* DIRECTION_TYPE_H */
--- a/src/lang/english.txt
+++ b/src/lang/english.txt
@@ -1584,6 +1584,7 @@
 STR_RAILROAD_TRACK_WITH_COMBO_NOENTRYSIGNALS                    :Railway track with combo- and one-way path signals
 STR_RAILROAD_TRACK_WITH_PBS_NOENTRYSIGNALS                      :Railway track with path and one-way path signals
 STR_MUST_REMOVE_RAILWAY_STATION_FIRST                           :{WHITE}Must remove railway station first
+STR_MUST_REMOVE_RAILWAYPOINT_FIRST                              :{WHITE}Must remove rail waypoint first
 STR_CREATE_SPLITTED_STATION                                     :{YELLOW}Build a separate station
 STR_SELECT_STATION_TO_JOIN                                      :{BLACK}Join station
 
@@ -1768,11 +1769,13 @@
 STR_STATION_BUILD_PLATFORM_LENGTH                               :{BLACK}Platform length
 STR_ERROR_TOO_CLOSE_TO_ANOTHER_RAILROAD                         :{WHITE}Too close to another railway station
 STR_ERROR_ADJOINS_MORE_THAN_ONE_EXISTING                        :{WHITE}Adjoins more than one existing station/loading area
+STR_ERROR_WAYPOINT_ADJOINS_MORE_THAN_ONE_EXISTING               :{WHITE}Adjoins more than one existing waypoint
 STR_ERROR_TOO_MANY_STATIONS_LOADING                             :{WHITE}Too many stations/loading areas
 STR_ERROR_TOO_MANY_STATION_SPECS                                :{WHITE}Too many railway station parts
 STR_ERROR_TOO_MANY_BUS_STOPS                                    :{WHITE}Too many bus stops
 STR_ERROR_TOO_MANY_TRUCK_STOPS                                  :{WHITE}Too many lorry stations
 STR_ERROR_TOO_CLOSE_TO_ANOTHER_STATION                          :{WHITE}Too close to another station/loading area
+STR_ERROR_TOO_CLOSE_TO_ANOTHER_WAYPOINT                         :{WHITE}Too close to another waypoint
 STR_STATION_VIEW_CAPTION                                        :{WHITE}{STATION} {STATIONFEATURES}
 STR_ERROR_MUST_DEMOLISH_RAILROAD                                :{WHITE}Must demolish railway station first
 STR_ERROR_TOO_CLOSE_TO_ANOTHER_AIRPORT                          :{WHITE}Too close to another airport
--- a/src/rail_gui.cpp
+++ b/src/rail_gui.cpp
@@ -153,15 +153,14 @@
 		return;
 	}
 
-	TrackBits bits = IsTileType(tile, MP_RAILWAY) && GetRailTileType(tile) == RAIL_TILE_NORMAL ? GetTrackBits(tile) : TRACK_BIT_NONE;
-	Track track = RemoveFirstTrack(&bits);
-	if (bits == TRACK_BIT_NONE && IsDiagonalTrack(track)) {
+	Axis axis = GetAxisForNewWaypoint(tile);
+	if (IsValidAxis(axis)) {
 		/* Valid tile for waypoints */
-		VpStartPlaceSizing(tile, track == TRACK_X ? VPM_FIX_X : VPM_FIX_Y, DDSP_BUILD_STATION);
+		VpStartPlaceSizing(tile, axis == AXIS_X ? VPM_FIX_X : VPM_FIX_Y, DDSP_BUILD_STATION);
 	} else {
 		/* Tile where we can't build rail waypoints. This is always going to fail,
 		 * but provides the user with a proper error message. */
-		DoCommandP(tile, 0, 0, CMD_BUILD_RAIL_WAYPOINT | CMD_MSG(STR_CANT_BUILD_TRAIN_WAYPOINT));
+		DoCommandP(tile, 1 << 8 | 1 << 16, STAT_CLASS_WAYP | INVALID_STATION << 16, CMD_BUILD_RAIL_WAYPOINT | CMD_MSG(STR_CANT_BUILD_TRAIN_WAYPOINT));
 	}
 }
 
@@ -177,7 +176,8 @@
 static void PlaceRail_Station(TileIndex tile)
 {
 	if (_remove_button_clicked) {
-		VpStartPlaceSizing(tile, VPM_X_AND_Y, DDSP_REMOVE_STATION);
+		VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_REMOVE_STATION);
+		VpSetPlaceSizingLimit(-1);
 	} else if (_settings_client.gui.station_dragdrop) {
 		VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED, DDSP_BUILD_STATION);
 		VpSetPlaceSizingLimit(_settings_game.station.station_spread);
@@ -776,7 +776,7 @@
 							uint32 p2 = STAT_CLASS_WAYP | _cur_waypoint_type << 8 | INVALID_STATION << 16;
 
 							CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAIL_WAYPOINT | CMD_MSG(STR_CANT_BUILD_TRAIN_WAYPOINT), CcPlaySound1E, "" };
-							ShowSelectStationIfNeeded(cmdcont, ta);
+							DoCommandP(&cmdcont);
 						}
 					}
 					break;
@@ -806,7 +806,7 @@
 	virtual EventState OnCTRLStateChange()
 	{
 		/* do not toggle Remove button by Ctrl when placing station */
-		if (!this->IsWidgetLowered(RTW_BUILD_STATION) && RailToolbar_CtrlChanged(this)) return ES_HANDLED;
+		if (!this->IsWidgetLowered(RTW_BUILD_STATION) && !this->IsWidgetLowered(RTW_BUILD_WAYPOINT) && RailToolbar_CtrlChanged(this)) return ES_HANDLED;
 		return ES_NOT_HANDLED;
 	}
 };
--- a/src/station_cmd.cpp
+++ b/src/station_cmd.cpp
@@ -638,7 +638,7 @@
 	UpdateStationSignCoord(st);
 }
 
-static CommandCost ClearTile_Station(TileIndex tile, DoCommandFlag flags);
+CommandCost ClearTile_Station(TileIndex tile, DoCommandFlag flags);
 
 /** Tries to clear the given area.
  * @param tile TileIndex to start check
@@ -727,7 +727,7 @@
  * @param axis the axis of the newly build rail
  * @return true if we are allowed to extend
  */
-static bool CanExpandRailStation(const Station *st, TileArea &new_ta, Axis axis)
+bool CanExpandRailStation(const BaseStation *st, TileArea &new_ta, Axis axis)
 {
 	TileArea cur_ta = st->train_station;
 
@@ -890,6 +890,20 @@
 }
 
 /**
+ * Find a nearby waypoint that joins this waypoint.
+ * @param existing_waypoint an existing waypoint we build over
+ * @param waypoint_to_join the waypoint to join to
+ * @param adjacent whether adjacent waypoints are allowed
+ * @param ta the area of the newly build waypoint
+ * @param st 'return' pointer for the found waypoint
+ * @return command cost with the error or 'okay'
+ */
+CommandCost FindJoiningWaypoint(StationID existing_waypoint, StationID waypoint_to_join, bool adjacent, TileArea ta, Waypoint **wp)
+{
+	return FindJoiningBaseStation<Waypoint, STR_MUST_REMOVE_RAILWAYPOINT_FIRST>(existing_waypoint, waypoint_to_join, adjacent, ta, wp);
+}
+
+/**
  * Build rail station
  * @param tile_org northern most position of station dragging/placement
  * @param flags operation to perform
@@ -3099,7 +3113,7 @@
 	return road_owner != OWNER_TOWN || CheckAllowRemoveRoad(tile, GetAnyRoadBits(tile, ROADTYPE_ROAD), OWNER_TOWN, ROADTYPE_ROAD, flags);
 }
 
-static CommandCost ClearTile_Station(TileIndex tile, DoCommandFlag flags)
+CommandCost ClearTile_Station(TileIndex tile, DoCommandFlag flags)
 {
 	if (flags & DC_AUTO) {
 		switch (GetStationType(tile)) {
--- a/src/waypoint_cmd.cpp
+++ b/src/waypoint_cmd.cpp
@@ -121,18 +121,54 @@
 }
 
 /**
+ * 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;
+	}
+}
+
+CommandCost ClearTile_Station(TileIndex tile, DoCommandFlag flags);
+
+/**
  * 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
  */
-static CommandCost IsValidTileForWaypoint(TileIndex tile, Axis axis)
+static CommandCost IsValidTileForWaypoint(TileIndex tile, Axis axis, StationID *waypoint)
 {
-	if (!IsTileType(tile, MP_RAILWAY) ||
-			GetRailTileType(tile) != RAIL_TILE_NORMAL ||
-			GetTrackBits(tile) != AxisToTrackBits(axis)) {
-		return_cmd_error(STR_ERROR_NO_SUITABLE_RAILROAD_TRACK);
+	/* 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. */
+	if (waypoint != NULL && IsTileType(tile, MP_STATION)) {
+		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);
+			}
+		}
 	}
 
+	if (GetAxisForNewWaypoint(tile) != axis) 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;
@@ -149,6 +185,8 @@
 }
 
 extern void GetStationLayout(byte *layout, int numtracks, int plat_len, const StationSpec *statspec);
+extern CommandCost FindJoiningWaypoint(StationID existing_station, StationID station_to_join, bool adjacent, TileArea ta, Waypoint **wp);
+extern bool CanExpandRailStation(const BaseStation *st, TileArea &new_ta, Axis axis);
 
 /** Convert existing rail to waypoint. Eg build a waypoint station over
  * piece of rail
@@ -158,6 +196,7 @@
  * - p1 = (bit  4)    - orientation (Axis)
  * - p1 = (bit  8-15) - width of waypoint
  * - p1 = (bit 16-23) - height of waypoint
+ * - p1 = (bit 24)    - allow waypoints directly adjacent to other waypoints.
  * @param p2 various bitstuffed elements
  * - p2 = (bit  0- 7) - custom station class
  * - p2 = (bit  8-15) - custom station id
@@ -170,9 +209,11 @@
 	Axis axis      = (Axis)GB(p1,  4, 1);
 	byte width     = GB(p1,  8, 8);
 	byte height    = GB(p1, 16, 8);
+	bool adjacent  = HasBit(p1, 24);
 
 	StationClassID spec_class = (StationClassID)GB(p2, 0, 8);
 	byte spec_index           = GB(p2, 8, 8);
+	StationID station_to_join = GB(p2, 16, 16);
 
 	/* Check if the given station class is valid */
 	if (spec_class != STAT_CLASS_WAYP) return CMD_ERROR;
@@ -184,18 +225,41 @@
 	if ((axis == AXIS_X ? width : height) != 1) return CMD_ERROR;
 	if (count == 0 || count > _settings_game.station.station_spread) return CMD_ERROR;
 
+	/* Temporary */
+	if (station_to_join != INVALID_STATION) return CMD_ERROR;
+
+	/* Make sure the area below consists of clear tiles. (OR tiles belonging to a certain rail station) */
+	StationID est = INVALID_STATION;
+
 	/* 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;
-		CommandCost ret = IsValidTileForWaypoint(tile, axis);
+		CommandCost ret = IsValidTileForWaypoint(tile, axis, _settings_game.station.nonuniform_stations ? &est : NULL);
 		if (ret.Failed()) return ret;
 	}
 
+	Waypoint *wp = NULL;
+	TileArea new_location(TileArea(start_tile, width, height));
+	CommandCost ret = FindJoiningWaypoint(est, station_to_join, adjacent, new_location, &wp);
+	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) * offset;
-	Waypoint *wp = FindDeletedWaypointCloseTo(center_tile, STR_SV_STNAME_WAYPOINT);
-	if (wp == NULL && !Waypoint::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
+	if (wp == NULL) wp = FindDeletedWaypointCloseTo(center_tile, STR_SV_STNAME_WAYPOINT);
+
+	if (wp != NULL) {
+		/* Reuse an existing station. */
+		if (wp->owner != _current_company) return_cmd_error(STR_ERROR_TOO_CLOSE_TO_ANOTHER_WAYPOINT);
+
+		/* check if we want to expanding an already existing station? */
+		if (wp->train_station.tile != INVALID_TILE && !CanExpandRailStation(wp, new_location, axis)) return CMD_ERROR;
+
+		if (!wp->rect.BeforeAddRect(start_tile, width, height, StationRect::ADD_TEST)) return CMD_ERROR;
+	} else {
+		/* allocate and initialize new station */
+		if (!Waypoint::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_STATIONS_LOADING);
+	}
 
 	if (flags & DC_EXEC) {
 		if (wp == NULL) {
@@ -213,9 +277,7 @@
 		wp->facilities |= FACIL_TRAIN;
 		wp->build_date = _date;
 		wp->string_id = STR_SV_STNAME_WAYPOINT;
-		wp->train_station.tile = start_tile;
-		wp->train_station.w = width;
-		wp->train_station.h = height;
+		wp->train_station = new_location;
 
 		if (wp->town == NULL) MakeDefaultWaypointName(wp);
 
@@ -234,12 +296,16 @@
 
 		for (int i = 0; i < count; i++) {
 			TileIndex tile = start_tile + i * offset;
-			bool reserved = HasBit(GetRailReservationTrackBits(tile), AxisToTrack(axis));
+			byte old_specindex = IsTileType(tile, MP_STATION) ? GetCustomStationSpecIndex(tile) : 0;
+			bool reserved = IsTileType(tile, MP_RAILWAY) ?
+					HasBit(GetRailReservationTrackBits(tile), AxisToTrack(axis)) :
+					HasStationReservation(tile);
 			MakeRailWaypoint(tile, wp->owner, wp->index, axis, layout_ptr[i], GetRailType(tile));
 			SetCustomStationSpecIndex(tile, map_spec_index);
 			SetRailStationReservation(tile, reserved);
 			MarkTileDirtyByTile(tile);
 
+			DeallocateSpecFromStation(wp, old_specindex);
 			YapfNotifyTrackLayoutChange(tile, AxisToTrack(axis));
 		}
 	}
--- a/src/waypoint_func.h
+++ b/src/waypoint_func.h
@@ -5,12 +5,14 @@
 #ifndef WAYPOINT_FUNC_H
 #define WAYPOINT_FUNC_H
 
+#include "direction_type.h"
 #include "rail_type.h"
 #include "command_type.h"
 #include "station_type.h"
 
 CommandCost RemoveBuoy(TileIndex tile, DoCommandFlag flags);
 
+Axis GetAxisForNewWaypoint(TileIndex tile);
 void ShowWaypointWindow(const Waypoint *wp);
 void DrawWaypointSprite(int x, int y, int stat_id, RailType railtype);
 void MakeDefaultWaypointName(Waypoint *wp);