mirror of https://github.com/OpenTTD/OpenTTD
(svn r16965) -Codechange: use tile area instead of sets of variables for the station joiner code.
parent
4aa2785757
commit
47a37b6093
|
@ -40,7 +40,7 @@ static void PlaceAirport(TileIndex tile)
|
||||||
SB(p2, 16, 16, INVALID_STATION); // no station to join
|
SB(p2, 16, 16, INVALID_STATION); // no station to join
|
||||||
|
|
||||||
CommandContainer cmdcont = { tile, _selected_airport_type, p2, CMD_BUILD_AIRPORT | CMD_MSG(STR_ERROR_CAN_T_BUILD_AIRPORT_HERE), CcBuildAirport, "" };
|
CommandContainer cmdcont = { tile, _selected_airport_type, p2, CMD_BUILD_AIRPORT | CMD_MSG(STR_ERROR_CAN_T_BUILD_AIRPORT_HERE), CcBuildAirport, "" };
|
||||||
ShowSelectStationIfNeeded(cmdcont, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE);
|
ShowSelectStationIfNeeded(cmdcont, TileArea(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE));
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Widget number of the airport build window. */
|
/** Widget number of the airport build window. */
|
||||||
|
|
|
@ -23,18 +23,6 @@ struct StationSpecList {
|
||||||
uint8 localidx; ///< Station ID within GRF of station
|
uint8 localidx; ///< Station ID within GRF of station
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Represents the covered area */
|
|
||||||
struct TileArea {
|
|
||||||
/** Just construct this tile area */
|
|
||||||
TileArea() {}
|
|
||||||
/** Construct this tile area with some set values */
|
|
||||||
TileArea(TileIndex tile, uint8 w, uint8 h) : tile(tile), w(w), h(h) {}
|
|
||||||
|
|
||||||
TileIndex tile; ///< The base tile of the area
|
|
||||||
uint8 w; ///< The width of the area
|
|
||||||
uint8 h; ///< The height of the area
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/** StationRect - used to track station spread out rectangle - cheaper than scanning whole map */
|
/** StationRect - used to track station spread out rectangle - cheaper than scanning whole map */
|
||||||
struct StationRect : public Rect {
|
struct StationRect : public Rect {
|
||||||
|
|
|
@ -50,7 +50,7 @@ static void PlaceDocks_Dock(TileIndex tile)
|
||||||
|
|
||||||
/* tile is always the land tile, so need to evaluate _thd.pos */
|
/* tile is always the land tile, so need to evaluate _thd.pos */
|
||||||
CommandContainer cmdcont = { tile, _ctrl_pressed, p2, CMD_BUILD_DOCK | CMD_MSG(STR_ERROR_CAN_T_BUILD_DOCK_HERE), CcBuildDocks, "" };
|
CommandContainer cmdcont = { tile, _ctrl_pressed, p2, CMD_BUILD_DOCK | CMD_MSG(STR_ERROR_CAN_T_BUILD_DOCK_HERE), CcBuildDocks, "" };
|
||||||
ShowSelectStationIfNeeded(cmdcont, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE);
|
ShowSelectStationIfNeeded(cmdcont, TileArea(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PlaceDocks_Depot(TileIndex tile)
|
static void PlaceDocks_Depot(TileIndex tile)
|
||||||
|
|
|
@ -181,7 +181,7 @@ static void PlaceRail_Station(TileIndex tile)
|
||||||
if (!_railstation.orientation) Swap(w, h);
|
if (!_railstation.orientation) Swap(w, h);
|
||||||
|
|
||||||
CommandContainer cmdcont = { tile, p1, p2, CMD_BUILD_RAILROAD_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
|
CommandContainer cmdcont = { tile, p1, p2, CMD_BUILD_RAILROAD_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
|
||||||
ShowSelectStationIfNeeded(cmdcont, w, h);
|
ShowSelectStationIfNeeded(cmdcont, TileArea(tile, w, h));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -873,26 +873,17 @@ void ShowBuildRailToolbar(RailType railtype, int button)
|
||||||
|
|
||||||
static void HandleStationPlacement(TileIndex start, TileIndex end)
|
static void HandleStationPlacement(TileIndex start, TileIndex end)
|
||||||
{
|
{
|
||||||
uint sx = TileX(start);
|
TileArea ta(start, end);
|
||||||
uint sy = TileY(start);
|
uint numtracks = ta.w;
|
||||||
uint ex = TileX(end);
|
uint platlength = ta.h;
|
||||||
uint ey = TileY(end);
|
|
||||||
uint w, h;
|
|
||||||
|
|
||||||
if (sx > ex) Swap(sx, ex);
|
|
||||||
if (sy > ey) Swap(sy, ey);
|
|
||||||
w = ex - sx + 1;
|
|
||||||
h = ey - sy + 1;
|
|
||||||
|
|
||||||
uint numtracks = w;
|
|
||||||
uint platlength = h;
|
|
||||||
if (_railstation.orientation == AXIS_X) Swap(numtracks, platlength);
|
if (_railstation.orientation == AXIS_X) Swap(numtracks, platlength);
|
||||||
|
|
||||||
uint32 p1 = _cur_railtype | _railstation.orientation << 4 | _ctrl_pressed << 24;
|
uint32 p1 = _cur_railtype | _railstation.orientation << 4 | numtracks << 8 | platlength << 16 | _ctrl_pressed << 24;
|
||||||
uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
|
uint32 p2 = _railstation.station_class | _railstation.station_type << 8 | INVALID_STATION << 16;
|
||||||
|
|
||||||
CommandContainer cmdcont = { TileXY(sx, sy), p1 | numtracks << 8 | platlength << 16, p2, CMD_BUILD_RAILROAD_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
|
CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAILROAD_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" };
|
||||||
ShowSelectStationIfNeeded(cmdcont, w, h);
|
ShowSelectStationIfNeeded(cmdcont, ta);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Enum referring to the widgets of the rail stations window */
|
/** Enum referring to the widgets of the rail stations window */
|
||||||
|
|
|
@ -212,7 +212,7 @@ static void PlaceRoadStop(TileIndex tile, uint32 p2, uint32 cmd)
|
||||||
p1 -= DIAGDIR_END; // Adjust picker result to actual direction
|
p1 -= DIAGDIR_END; // Adjust picker result to actual direction
|
||||||
}
|
}
|
||||||
CommandContainer cmdcont = { tile, p1, p2, cmd, CcRoadDepot, "" };
|
CommandContainer cmdcont = { tile, p1, p2, cmd, CcRoadDepot, "" };
|
||||||
ShowSelectStationIfNeeded(cmdcont, 1, 1);
|
ShowSelectStationIfNeeded(cmdcont, TileArea(tile, 1, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void PlaceRoad_BusStation(TileIndex tile)
|
static void PlaceRoad_BusStation(TileIndex tile)
|
||||||
|
|
|
@ -491,6 +491,21 @@ StationRect& StationRect::operator = (Rect src)
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TileArea::TileArea(TileIndex start, TileIndex end)
|
||||||
|
{
|
||||||
|
uint sx = TileX(start);
|
||||||
|
uint sy = TileY(start);
|
||||||
|
uint ex = TileX(end);
|
||||||
|
uint ey = TileY(end);
|
||||||
|
|
||||||
|
if (sx > ex) Swap(sx, ex);
|
||||||
|
if (sy > ey) Swap(sy, ey);
|
||||||
|
|
||||||
|
this->tile = TileXY(sx, sy);
|
||||||
|
this->w = ex - sx + 1;
|
||||||
|
this->h = ey - sy + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void InitializeStations()
|
void InitializeStations()
|
||||||
{
|
{
|
||||||
|
|
|
@ -1124,27 +1124,24 @@ static bool AddNearbyStation(TileIndex tile, void *user_data)
|
||||||
* @param distant_join Search for adjacent stations (false) or stations fully
|
* @param distant_join Search for adjacent stations (false) or stations fully
|
||||||
* within station spread
|
* within station spread
|
||||||
**/
|
**/
|
||||||
static const Station *FindStationsNearby(TileIndex tile, int w, int h, bool distant_join)
|
static const Station *FindStationsNearby(TileArea ta, bool distant_join)
|
||||||
{
|
{
|
||||||
TileArea ctx;
|
TileArea ctx = ta;
|
||||||
ctx.tile = tile;
|
|
||||||
ctx.w = w;
|
|
||||||
ctx.h = h;
|
|
||||||
|
|
||||||
_stations_nearby_list.Clear();
|
_stations_nearby_list.Clear();
|
||||||
_deleted_stations_nearby.Clear();
|
_deleted_stations_nearby.Clear();
|
||||||
|
|
||||||
/* Check the inside, to return, if we sit on another station */
|
/* Check the inside, to return, if we sit on another station */
|
||||||
BEGIN_TILE_LOOP(t, w, h, tile)
|
BEGIN_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) && Station::IsValidID(GetStationIndex(t))) return Station::GetByTile(t);
|
||||||
END_TILE_LOOP(t, w, h, tile)
|
END_TILE_LOOP(t, ta.w, ta.h, ta.tile)
|
||||||
|
|
||||||
/* Look for deleted stations */
|
/* Look for deleted stations */
|
||||||
const BaseStation *st;
|
const BaseStation *st;
|
||||||
FOR_ALL_BASE_STATIONS(st) {
|
FOR_ALL_BASE_STATIONS(st) {
|
||||||
if (Station::IsExpected(st) && !st->IsInUse() && st->owner == _local_company) {
|
if (Station::IsExpected(st) && !st->IsInUse() && st->owner == _local_company) {
|
||||||
/* Include only within station spread (yes, it is strictly less than) */
|
/* Include only within station spread (yes, it is strictly less than) */
|
||||||
if (max(DistanceMax(tile, st->xy), DistanceMax(TILE_ADDXY(tile, w - 1, h - 1), st->xy)) < _settings_game.station.station_spread) {
|
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();
|
TileAndStation *ts = _deleted_stations_nearby.Append();
|
||||||
ts->tile = st->xy;
|
ts->tile = st->xy;
|
||||||
ts->station = st->index;
|
ts->station = st->index;
|
||||||
|
@ -1161,11 +1158,11 @@ static const Station *FindStationsNearby(TileIndex tile, int w, int h, bool dist
|
||||||
/* Only search tiles where we have a chance to stay within the station spread.
|
/* Only search tiles where we have a chance to stay within the station spread.
|
||||||
* The complete check needs to be done in the callback as we don't know the
|
* The complete check needs to be done in the callback as we don't know the
|
||||||
* extent of the found station, yet. */
|
* extent of the found station, yet. */
|
||||||
if (distant_join && min(w, h) >= _settings_game.station.station_spread) return NULL;
|
if (distant_join && min(ta.w, ta.h) >= _settings_game.station.station_spread) return NULL;
|
||||||
uint max_dist = distant_join ? _settings_game.station.station_spread - min(w, h) : 1;
|
uint max_dist = distant_join ? _settings_game.station.station_spread - min(ta.w, ta.h) : 1;
|
||||||
|
|
||||||
tile = TILE_ADD(ctx.tile, TileOffsByDir(DIR_N));
|
TileIndex tile = TILE_ADD(ctx.tile, TileOffsByDir(DIR_N));
|
||||||
CircularTileSearch(&tile, max_dist, w, h, AddNearbyStation, &ctx);
|
CircularTileSearch(&tile, max_dist, ta.w, ta.h, AddNearbyStation, &ctx);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -1203,21 +1200,17 @@ static const NWidgetPart _nested_select_station_widgets[] = {
|
||||||
|
|
||||||
struct SelectStationWindow : Window {
|
struct SelectStationWindow : Window {
|
||||||
CommandContainer select_station_cmd; ///< Command to build new station
|
CommandContainer select_station_cmd; ///< Command to build new station
|
||||||
TileIndex tile; ///< Base tile of new station
|
TileArea area; ///< Location of new station
|
||||||
int size_x; ///< Size in x direction of new station
|
|
||||||
int size_y; ///< Size in y direction of new station
|
|
||||||
|
|
||||||
SelectStationWindow(const WindowDesc *desc, CommandContainer cmd, int w, int h) :
|
SelectStationWindow(const WindowDesc *desc, CommandContainer cmd, TileArea ta) :
|
||||||
Window(desc, 0),
|
Window(desc, 0),
|
||||||
select_station_cmd(cmd),
|
select_station_cmd(cmd),
|
||||||
tile(cmd.tile),
|
area(ta)
|
||||||
size_x(w),
|
|
||||||
size_y(h)
|
|
||||||
{
|
{
|
||||||
this->vscroll.cap = 6;
|
this->vscroll.cap = 6;
|
||||||
this->resize.step_height = 10;
|
this->resize.step_height = 10;
|
||||||
|
|
||||||
FindStationsNearby(this->tile, this->size_x, this->size_y, true);
|
FindStationsNearby(this->area, true);
|
||||||
|
|
||||||
this->FindWindowPlacementAndResize(desc);
|
this->FindWindowPlacementAndResize(desc);
|
||||||
}
|
}
|
||||||
|
@ -1281,7 +1274,7 @@ struct SelectStationWindow : Window {
|
||||||
|
|
||||||
virtual void OnInvalidateData(int data)
|
virtual void OnInvalidateData(int data)
|
||||||
{
|
{
|
||||||
FindStationsNearby(this->tile, this->size_x, this->size_y, true);
|
FindStationsNearby(this->area, true);
|
||||||
this->SetDirty();
|
this->SetDirty();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1301,7 +1294,7 @@ static const WindowDesc _select_station_desc(
|
||||||
* @param h Height of the to-be-built station
|
* @param h Height of the to-be-built station
|
||||||
* @return whether we need to show the station selection window.
|
* @return whether we need to show the station selection window.
|
||||||
*/
|
*/
|
||||||
static bool StationJoinerNeeded(CommandContainer cmd, int w, int h)
|
static bool StationJoinerNeeded(CommandContainer cmd, TileArea ta)
|
||||||
{
|
{
|
||||||
/* Only show selection if distant join is enabled in the settings */
|
/* Only show selection if distant join is enabled in the settings */
|
||||||
if (!_settings_game.station.distant_join_stations) return false;
|
if (!_settings_game.station.distant_join_stations) return false;
|
||||||
|
@ -1326,7 +1319,7 @@ static bool StationJoinerNeeded(CommandContainer cmd, int w, int h)
|
||||||
/* Test for adjacent station or station below selection.
|
/* 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.
|
* 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. */
|
* but join the other station immediatelly. */
|
||||||
const Station *st = FindStationsNearby(cmd.tile, w, h, false);
|
const Station *st = FindStationsNearby(ta, false);
|
||||||
return st == NULL && (_settings_game.station.adjacent_stations || _stations_nearby_list.Length() == 0);
|
return st == NULL && (_settings_game.station.adjacent_stations || _stations_nearby_list.Length() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1336,12 +1329,12 @@ static bool StationJoinerNeeded(CommandContainer cmd, int w, int h)
|
||||||
* @param w Width of the to-be-built station
|
* @param w Width of the to-be-built station
|
||||||
* @param h Height of the to-be-built station
|
* @param h Height of the to-be-built station
|
||||||
*/
|
*/
|
||||||
void ShowSelectStationIfNeeded(CommandContainer cmd, int w, int h)
|
void ShowSelectStationIfNeeded(CommandContainer cmd, TileArea ta)
|
||||||
{
|
{
|
||||||
if (StationJoinerNeeded(cmd, w, h)) {
|
if (StationJoinerNeeded(cmd, ta)) {
|
||||||
if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
|
if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
|
||||||
if (BringWindowToFrontById(WC_SELECT_STATION, 0)) return;
|
if (BringWindowToFrontById(WC_SELECT_STATION, 0)) return;
|
||||||
new SelectStationWindow(&_select_station_desc, cmd, w, h);
|
new SelectStationWindow(&_select_station_desc, cmd, ta);
|
||||||
} else {
|
} else {
|
||||||
DoCommandP(&cmd);
|
DoCommandP(&cmd);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#define STATION_GUI_H
|
#define STATION_GUI_H
|
||||||
|
|
||||||
#include "command_type.h"
|
#include "command_type.h"
|
||||||
|
#include "station_type.h"
|
||||||
|
|
||||||
/** Enum for StationView, referring to _station_view_widgets and _station_view_expanded_widgets */
|
/** Enum for StationView, referring to _station_view_widgets and _station_view_expanded_widgets */
|
||||||
enum StationViewWidgets {
|
enum StationViewWidgets {
|
||||||
|
@ -36,6 +37,6 @@ enum StationCoverageType {
|
||||||
int DrawStationCoverageAreaText(int left, int right, int top, StationCoverageType sct, int rad, bool supplies);
|
int DrawStationCoverageAreaText(int left, int right, int top, StationCoverageType sct, int rad, bool supplies);
|
||||||
void CheckRedrawStationCoverage(const Window *w);
|
void CheckRedrawStationCoverage(const Window *w);
|
||||||
|
|
||||||
void ShowSelectStationIfNeeded(CommandContainer cmd, int w, int h);
|
void ShowSelectStationIfNeeded(CommandContainer cmd, TileArea ta);
|
||||||
|
|
||||||
#endif /* STATION_GUI_H */
|
#endif /* STATION_GUI_H */
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#define STATION_TYPE_H
|
#define STATION_TYPE_H
|
||||||
|
|
||||||
#include "core/enum_type.hpp"
|
#include "core/enum_type.hpp"
|
||||||
|
#include "tile_type.h"
|
||||||
|
|
||||||
typedef uint16 StationID;
|
typedef uint16 StationID;
|
||||||
typedef uint16 RoadStopID;
|
typedef uint16 RoadStopID;
|
||||||
|
@ -81,4 +82,29 @@ enum {
|
||||||
MAX_LENGTH_STATION_NAME_PIXELS = 180, ///< The maximum length of a station name in pixels
|
MAX_LENGTH_STATION_NAME_PIXELS = 180, ///< The maximum length of a station name in pixels
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Represents the covered area of e.g. a rail station */
|
||||||
|
struct TileArea {
|
||||||
|
/** Just construct this tile area */
|
||||||
|
TileArea() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct this tile area with some set values
|
||||||
|
* @param tile the base tile
|
||||||
|
* @param w the width
|
||||||
|
* @param h the height
|
||||||
|
*/
|
||||||
|
TileArea(TileIndex tile, uint8 w, uint8 h) : tile(tile), w(w), h(h) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct this tile area based on two points.
|
||||||
|
* @param start the start of the area
|
||||||
|
* @param end the end of the area
|
||||||
|
*/
|
||||||
|
TileArea(TileIndex start, TileIndex end);
|
||||||
|
|
||||||
|
TileIndex tile; ///< The base tile of the area
|
||||||
|
uint8 w; ///< The width of the area
|
||||||
|
uint8 h; ///< The height of the area
|
||||||
|
};
|
||||||
|
|
||||||
#endif /* STATION_TYPE_H */
|
#endif /* STATION_TYPE_H */
|
||||||
|
|
Loading…
Reference in New Issue