diff options
author | rubidium <rubidium@openttd.org> | 2009-07-26 21:07:03 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2009-07-26 21:07:03 +0000 |
commit | 47a37b6093c3bb93dba81e4d3440c4098699a849 (patch) | |
tree | 322f91b3bf8e463dde3d472a4b390600a84ca350 /src | |
parent | 4aa2785757b132a4faee3a5fa821f2795be94f76 (diff) | |
download | openttd-47a37b6093c3bb93dba81e4d3440c4098699a849.tar.xz |
(svn r16965) -Codechange: use tile area instead of sets of variables for the station joiner code.
Diffstat (limited to 'src')
-rw-r--r-- | src/airport_gui.cpp | 2 | ||||
-rw-r--r-- | src/base_station_base.h | 12 | ||||
-rw-r--r-- | src/dock_gui.cpp | 2 | ||||
-rw-r--r-- | src/rail_gui.cpp | 25 | ||||
-rw-r--r-- | src/road_gui.cpp | 2 | ||||
-rw-r--r-- | src/station.cpp | 15 | ||||
-rw-r--r-- | src/station_gui.cpp | 45 | ||||
-rw-r--r-- | src/station_gui.h | 3 | ||||
-rw-r--r-- | src/station_type.h | 26 |
9 files changed, 73 insertions, 59 deletions
diff --git a/src/airport_gui.cpp b/src/airport_gui.cpp index 18162a785..08235d722 100644 --- a/src/airport_gui.cpp +++ b/src/airport_gui.cpp @@ -40,7 +40,7 @@ static void PlaceAirport(TileIndex tile) 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, "" }; - 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. */ diff --git a/src/base_station_base.h b/src/base_station_base.h index 5f99adab8..a3b28eb08 100644 --- a/src/base_station_base.h +++ b/src/base_station_base.h @@ -23,18 +23,6 @@ struct StationSpecList { 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 */ struct StationRect : public Rect { diff --git a/src/dock_gui.cpp b/src/dock_gui.cpp index f7aed41ed..f2d357872 100644 --- a/src/dock_gui.cpp +++ b/src/dock_gui.cpp @@ -50,7 +50,7 @@ static void PlaceDocks_Dock(TileIndex tile) /* 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, "" }; - 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) diff --git a/src/rail_gui.cpp b/src/rail_gui.cpp index fb0c96b66..fd7ae8881 100644 --- a/src/rail_gui.cpp +++ b/src/rail_gui.cpp @@ -181,7 +181,7 @@ static void PlaceRail_Station(TileIndex tile) 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, "" }; - 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) { - uint sx = TileX(start); - uint sy = TileY(start); - uint ex = TileX(end); - 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; + TileArea ta(start, end); + uint numtracks = ta.w; + uint platlength = ta.h; + 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; - 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, "" }; - ShowSelectStationIfNeeded(cmdcont, w, h); + CommandContainer cmdcont = { ta.tile, p1, p2, CMD_BUILD_RAILROAD_STATION | CMD_MSG(STR_ERROR_CAN_T_BUILD_RAILROAD_STATION), CcStation, "" }; + ShowSelectStationIfNeeded(cmdcont, ta); } /** Enum referring to the widgets of the rail stations window */ diff --git a/src/road_gui.cpp b/src/road_gui.cpp index 5ce82268c..1afa42bd6 100644 --- a/src/road_gui.cpp +++ b/src/road_gui.cpp @@ -212,7 +212,7 @@ static void PlaceRoadStop(TileIndex tile, uint32 p2, uint32 cmd) p1 -= DIAGDIR_END; // Adjust picker result to actual direction } CommandContainer cmdcont = { tile, p1, p2, cmd, CcRoadDepot, "" }; - ShowSelectStationIfNeeded(cmdcont, 1, 1); + ShowSelectStationIfNeeded(cmdcont, TileArea(tile, 1, 1)); } static void PlaceRoad_BusStation(TileIndex tile) diff --git a/src/station.cpp b/src/station.cpp index f4138af04..84c7efb60 100644 --- a/src/station.cpp +++ b/src/station.cpp @@ -491,6 +491,21 @@ StationRect& StationRect::operator = (Rect src) 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() { diff --git a/src/station_gui.cpp b/src/station_gui.cpp index 39a4c95c5..3e171b687 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -1124,27 +1124,24 @@ static bool AddNearbyStation(TileIndex tile, void *user_data) * @param distant_join Search for adjacent stations (false) or stations fully * 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; - ctx.tile = tile; - ctx.w = w; - ctx.h = h; + TileArea ctx = ta; _stations_nearby_list.Clear(); _deleted_stations_nearby.Clear(); /* 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); - END_TILE_LOOP(t, w, h, tile) + END_TILE_LOOP(t, ta.w, ta.h, ta.tile) /* Look for deleted stations */ const BaseStation *st; FOR_ALL_BASE_STATIONS(st) { if (Station::IsExpected(st) && !st->IsInUse() && st->owner == _local_company) { /* 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(); ts->tile = st->xy; 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. * The complete check needs to be done in the callback as we don't know the * extent of the found station, yet. */ - if (distant_join && min(w, h) >= _settings_game.station.station_spread) return NULL; - uint max_dist = distant_join ? _settings_game.station.station_spread - min(w, h) : 1; + 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(ta.w, ta.h) : 1; - tile = TILE_ADD(ctx.tile, TileOffsByDir(DIR_N)); - CircularTileSearch(&tile, max_dist, w, h, AddNearbyStation, &ctx); + TileIndex tile = TILE_ADD(ctx.tile, TileOffsByDir(DIR_N)); + CircularTileSearch(&tile, max_dist, ta.w, ta.h, AddNearbyStation, &ctx); return NULL; } @@ -1203,21 +1200,17 @@ static const NWidgetPart _nested_select_station_widgets[] = { struct SelectStationWindow : Window { CommandContainer select_station_cmd; ///< Command to build new station - TileIndex tile; ///< Base tile of new station - int size_x; ///< Size in x direction of new station - int size_y; ///< Size in y direction of new station + TileArea area; ///< Location of new station - SelectStationWindow(const WindowDesc *desc, CommandContainer cmd, int w, int h) : + SelectStationWindow(const WindowDesc *desc, CommandContainer cmd, TileArea ta) : Window(desc, 0), select_station_cmd(cmd), - tile(cmd.tile), - size_x(w), - size_y(h) + area(ta) { this->vscroll.cap = 6; this->resize.step_height = 10; - FindStationsNearby(this->tile, this->size_x, this->size_y, true); + FindStationsNearby(this->area, true); this->FindWindowPlacementAndResize(desc); } @@ -1281,7 +1274,7 @@ struct SelectStationWindow : Window { virtual void OnInvalidateData(int data) { - FindStationsNearby(this->tile, this->size_x, this->size_y, true); + FindStationsNearby(this->area, true); this->SetDirty(); } }; @@ -1301,7 +1294,7 @@ static const WindowDesc _select_station_desc( * @param h Height of the to-be-built station * @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 */ 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. * 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(cmd.tile, w, h, false); + const Station *st = FindStationsNearby(ta, false); 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 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 (BringWindowToFrontById(WC_SELECT_STATION, 0)) return; - new SelectStationWindow(&_select_station_desc, cmd, w, h); + new SelectStationWindow(&_select_station_desc, cmd, ta); } else { DoCommandP(&cmd); } diff --git a/src/station_gui.h b/src/station_gui.h index fce0283c3..5ce0aaa60 100644 --- a/src/station_gui.h +++ b/src/station_gui.h @@ -6,6 +6,7 @@ #define STATION_GUI_H #include "command_type.h" +#include "station_type.h" /** Enum for StationView, referring to _station_view_widgets and _station_view_expanded_widgets */ enum StationViewWidgets { @@ -36,6 +37,6 @@ enum StationCoverageType { int DrawStationCoverageAreaText(int left, int right, int top, StationCoverageType sct, int rad, bool supplies); void CheckRedrawStationCoverage(const Window *w); -void ShowSelectStationIfNeeded(CommandContainer cmd, int w, int h); +void ShowSelectStationIfNeeded(CommandContainer cmd, TileArea ta); #endif /* STATION_GUI_H */ diff --git a/src/station_type.h b/src/station_type.h index f6803421f..4af075f20 100644 --- a/src/station_type.h +++ b/src/station_type.h @@ -6,6 +6,7 @@ #define STATION_TYPE_H #include "core/enum_type.hpp" +#include "tile_type.h" typedef uint16 StationID; typedef uint16 RoadStopID; @@ -81,4 +82,29 @@ enum { 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 */ |