summaryrefslogtreecommitdiff
path: root/src/station_gui.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2009-07-31 16:28:28 +0000
committerrubidium <rubidium@openttd.org>2009-07-31 16:28:28 +0000
commita148a991bbc9325bebac7a3871f44f150a9b7b62 (patch)
treeefb7c0a1e22fe013521add2d9ba0852384c74f76 /src/station_gui.cpp
parent097e79a86de967c416d71d934a325d883d917fd5 (diff)
downloadopenttd-a148a991bbc9325bebac7a3871f44f150a9b7b62.tar.xz
(svn r17002) -Change: also support distant join for waypoints
Diffstat (limited to 'src/station_gui.cpp')
-rw-r--r--src/station_gui.cpp70
1 files changed, 52 insertions, 18 deletions
diff --git a/src/station_gui.cpp b/src/station_gui.cpp
index 326e50661..a1e3a6aa9 100644
--- a/src/station_gui.cpp
+++ b/src/station_gui.cpp
@@ -20,6 +20,7 @@
#include "widgets/dropdown_func.h"
#include "newgrf_cargo.h"
#include "station_base.h"
+#include "waypoint_base.h"
#include "tilehighlight_func.h"
#include "core/smallmap_type.hpp"
#include "company_base.h"
@@ -1081,7 +1082,9 @@ static SmallVector<StationID, 8> _stations_nearby_list;
* station spread.
* @param tile Tile just being checked
* @param user_data Pointer to TileArea context
+ * @tparam T the type of station to look for
*/
+template <class T>
static bool AddNearbyStation(TileIndex tile, void *user_data)
{
TileArea *ctx = (TileArea *)user_data;
@@ -1102,9 +1105,9 @@ static bool AddNearbyStation(TileIndex tile, void *user_data)
StationID sid = GetStationIndex(tile);
/* This station is (likely) a waypoint */
- if (!Station::IsValidID(sid)) return false;
+ if (!T::IsValidID(sid)) return false;
- Station *st = Station::Get(sid);
+ T *st = T::Get(sid);
if (st->owner != _local_company || _stations_nearby_list.Contains(sid)) return false;
if (st->rect.BeforeAddRect(ctx->tile, ctx->w, ctx->h, StationRect::ADD_TEST)) {
@@ -1123,8 +1126,10 @@ static bool AddNearbyStation(TileIndex tile, void *user_data)
* @param h Height of the to-be-built station
* @param distant_join Search for adjacent stations (false) or stations fully
* within station spread
+ * @tparam T the type of station to look for
**/
-static const Station *FindStationsNearby(TileArea ta, bool distant_join)
+template <class T>
+static const T *FindStationsNearby(TileArea ta, bool distant_join)
{
TileArea ctx = ta;
@@ -1133,13 +1138,13 @@ static const Station *FindStationsNearby(TileArea ta, bool distant_join)
/* Check the inside, to return, if we sit on another station */
TILE_LOOP(t, ta.w, ta.h, ta.tile) {
- if (t < MapSize() && IsTileType(t, MP_STATION) && Station::IsValidID(GetStationIndex(t))) return Station::GetByTile(t);
+ if (t < MapSize() && IsTileType(t, MP_STATION) && T::IsValidID(GetStationIndex(t))) return T::GetByTile(t);
}
/* Look for deleted stations */
const BaseStation *st;
FOR_ALL_BASE_STATIONS(st) {
- if (Station::IsExpected(st) && !st->IsInUse() && st->owner == _local_company) {
+ if (T::IsExpected(st) && !st->IsInUse() && st->owner == _local_company) {
/* Include only within station spread (yes, it is strictly less than) */
if (max(DistanceMax(ta.tile, st->xy), DistanceMax(TILE_ADDXY(ta.tile, ta.w - 1, ta.h - 1), st->xy)) < _settings_game.station.station_spread) {
TileAndStation *ts = _deleted_stations_nearby.Append();
@@ -1149,7 +1154,7 @@ static const Station *FindStationsNearby(TileArea ta, bool distant_join)
/* Add the station when it's within where we're going to build */
if (IsInsideBS(TileX(st->xy), TileX(ctx.tile), ctx.w) &&
IsInsideBS(TileY(st->xy), TileY(ctx.tile), ctx.h)) {
- AddNearbyStation(st->xy, &ctx);
+ AddNearbyStation<T>(st->xy, &ctx);
}
}
}
@@ -1162,7 +1167,7 @@ static const Station *FindStationsNearby(TileArea ta, bool distant_join)
uint max_dist = distant_join ? _settings_game.station.station_spread - min(ta.w, ta.h) : 1;
TileIndex tile = TILE_ADD(ctx.tile, TileOffsByDir(DIR_N));
- CircularTileSearch(&tile, max_dist, ta.w, ta.h, AddNearbyStation, &ctx);
+ CircularTileSearch(&tile, max_dist, ta.w, ta.h, AddNearbyStation<T>, &ctx);
return NULL;
}
@@ -1198,6 +1203,11 @@ static const NWidgetPart _nested_select_station_widgets[] = {
EndContainer(),
};
+/**
+ * Window for selecting stations/waypoints to (distant) join to.
+ * @tparam T The type of station to join with
+ */
+template <class T>
struct SelectStationWindow : Window {
CommandContainer select_station_cmd; ///< Command to build new station
TileArea area; ///< Location of new station
@@ -1210,8 +1220,9 @@ struct SelectStationWindow : Window {
this->vscroll.cap = 6;
this->resize.step_height = 10;
- FindStationsNearby(this->area, true);
+ FindStationsNearby<T>(this->area, true);
+ this->widget[JSW_WIDGET_CAPTION].data = T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_SELECT_WAYPOINT_TO_JOIN : STR_SELECT_STATION_TO_JOIN;
this->FindWindowPlacementAndResize(desc);
}
@@ -1223,7 +1234,7 @@ struct SelectStationWindow : Window {
uint y = 17;
if (this->vscroll.pos == 0) {
- DrawString(3, this->widget[JSW_PANEL].right - 2, y, STR_CREATE_SPLITTED_STATION);
+ DrawString(3, this->widget[JSW_PANEL].right - 2, y, T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_CREATE_SPLITTED_WAYPOINT : STR_CREATE_SPLITTED_STATION);
y += 10;
}
@@ -1231,10 +1242,10 @@ struct SelectStationWindow : Window {
/* Don't draw anything if it extends past the end of the window. */
if (i - this->vscroll.pos >= this->vscroll.cap) break;
- const Station *st = Station::Get(_stations_nearby_list[i - 1]);
+ const T *st = T::Get(_stations_nearby_list[i - 1]);
SetDParam(0, st->index);
SetDParam(1, st->facilities);
- DrawString(3, this->widget[JSW_PANEL].right - 2, y, STR_STATION_LIST_STATION);
+ DrawString(3, this->widget[JSW_PANEL].right - 2, y, T::EXPECTED_FACIL == FACIL_WAYPOINT ? STR_STATION_LIST_WAYPOINT : STR_STATION_LIST_STATION);
}
}
@@ -1274,7 +1285,7 @@ struct SelectStationWindow : Window {
virtual void OnInvalidateData(int data)
{
- FindStationsNearby(this->area, true);
+ FindStationsNearby<T>(this->area, true);
this->SetDirty();
}
};
@@ -1292,8 +1303,10 @@ static const WindowDesc _select_station_desc(
* @param cmd Command to build the station.
* @param w Width of the to-be-built station
* @param h Height of the to-be-built station
+ * @tparam T the type of station
* @return whether we need to show the station selection window.
*/
+template <class T>
static bool StationJoinerNeeded(CommandContainer cmd, TileArea ta)
{
/* Only show selection if distant join is enabled in the settings */
@@ -1319,23 +1332,44 @@ static bool StationJoinerNeeded(CommandContainer cmd, TileArea ta)
/* Test for adjacent station or station below selection.
* If adjacent-stations is disabled and we are building next to a station, do not show the selection window.
* but join the other station immediatelly. */
- const Station *st = FindStationsNearby(ta, false);
+ const T *st = FindStationsNearby<T>(ta, false);
return st == NULL && (_settings_game.station.adjacent_stations || _stations_nearby_list.Length() == 0);
}
/**
* Show the station selection window when needed. If not, build the station.
* @param cmd Command to build the station.
- * @param w Width of the to-be-built station
- * @param h Height of the to-be-built station
+ * @param ta Area to build the station in
+ * @tparam the class to find stations for
*/
-void ShowSelectStationIfNeeded(CommandContainer cmd, TileArea ta)
+template <class T>
+void ShowSelectBaseStationIfNeeded(CommandContainer cmd, TileArea ta)
{
- if (StationJoinerNeeded(cmd, ta)) {
+ if (StationJoinerNeeded<T>(cmd, ta)) {
if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
if (BringWindowToFrontById(WC_SELECT_STATION, 0)) return;
- new SelectStationWindow(&_select_station_desc, cmd, ta);
+ new SelectStationWindow<T>(&_select_station_desc, cmd, ta);
} else {
DoCommandP(&cmd);
}
}
+
+/**
+ * Show the station selection window when needed. If not, build the station.
+ * @param cmd Command to build the station.
+ * @param ta Area to build the station in
+ */
+void ShowSelectStationIfNeeded(CommandContainer cmd, TileArea ta)
+{
+ ShowSelectBaseStationIfNeeded<Station>(cmd, ta);
+}
+
+/**
+ * Show the waypoint selection window when needed. If not, build the waypoint.
+ * @param cmd Command to build the waypoint.
+ * @param ta Area to build the waypoint in
+ */
+void ShowSelectWaypointIfNeeded(CommandContainer cmd, TileArea ta)
+{
+ ShowSelectBaseStationIfNeeded<Waypoint>(cmd, ta);
+}