2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2007-02-23 01:48:53 +00:00
|
|
|
/** @file bridge_gui.cpp Graphical user interface for bridge construction */
|
2005-08-01 20:23:38 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2011-12-10 13:54:10 +00:00
|
|
|
#include "error.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2010-05-22 14:12:48 +00:00
|
|
|
#include "rail.h"
|
2019-04-06 06:46:15 +00:00
|
|
|
#include "road.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "window_func.h"
|
2007-12-29 09:24:26 +00:00
|
|
|
#include "sound_func.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "gfx_func.h"
|
2008-01-23 22:34:04 +00:00
|
|
|
#include "tunnelbridge.h"
|
2008-05-17 22:07:04 +00:00
|
|
|
#include "sortlist_type.h"
|
2008-05-18 12:25:46 +00:00
|
|
|
#include "widgets/dropdown_func.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "core/geometry_func.hpp"
|
2011-01-14 12:00:07 +00:00
|
|
|
#include "cmd_helper.h"
|
|
|
|
#include "tunnelbridge_map.h"
|
|
|
|
#include "road_gui.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-12-15 22:22:55 +00:00
|
|
|
#include "widgets/bridge_widget.h"
|
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2009-01-03 14:16:18 +00:00
|
|
|
/** The type of the last built rail bridge */
|
|
|
|
static BridgeType _last_railbridge_type = 0;
|
|
|
|
/** The type of the last built road bridge */
|
|
|
|
static BridgeType _last_roadbridge_type = 0;
|
|
|
|
|
2008-05-17 22:07:04 +00:00
|
|
|
/**
|
|
|
|
* Carriage for the data we need if we want to build a bridge
|
|
|
|
*/
|
|
|
|
struct BuildBridgeData {
|
|
|
|
BridgeType index;
|
|
|
|
const BridgeSpec *spec;
|
|
|
|
Money cost;
|
|
|
|
};
|
2007-12-06 18:29:31 +00:00
|
|
|
|
2010-11-20 14:15:02 +00:00
|
|
|
typedef GUIList<BuildBridgeData> GUIBridgeList; ///< List of bridges, used in #BuildBridgeWindow.
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-17 22:07:04 +00:00
|
|
|
/**
|
|
|
|
* Callback executed after a build Bridge CMD has been called
|
|
|
|
*
|
2010-11-20 13:33:24 +00:00
|
|
|
* @param result Whether the build succeeded
|
2011-01-14 12:00:07 +00:00
|
|
|
* @param end_tile End tile of the bridge.
|
|
|
|
* @param p1 packed start tile coords (~ dx)
|
|
|
|
* @param p2 various bitstuffed elements
|
|
|
|
* - p2 = (bit 0- 7) - bridge type (hi bh)
|
2018-07-22 23:05:23 +00:00
|
|
|
* - p2 = (bit 8-13) - rail type or road types.
|
2011-01-14 12:00:07 +00:00
|
|
|
* - p2 = (bit 15-16) - transport type.
|
2019-09-07 16:37:01 +00:00
|
|
|
* @param cmd unused
|
2008-05-17 22:07:04 +00:00
|
|
|
*/
|
2019-09-07 16:37:01 +00:00
|
|
|
void CcBuildBridge(const CommandCost &result, TileIndex end_tile, uint32 p1, uint32 p2, uint32 cmd)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2011-01-14 12:00:07 +00:00
|
|
|
if (result.Failed()) return;
|
2021-02-20 18:01:04 +00:00
|
|
|
if (_settings_client.sound.confirm) SndPlayTileFx(SND_27_CONSTRUCTION_BRIDGE, end_tile);
|
2011-01-14 12:00:07 +00:00
|
|
|
|
|
|
|
TransportType transport_type = Extract<TransportType, 15, 2>(p2);
|
|
|
|
|
|
|
|
if (transport_type == TRANSPORT_ROAD) {
|
|
|
|
DiagDirection end_direction = ReverseDiagDir(GetTunnelBridgeDirection(end_tile));
|
|
|
|
ConnectRoadToStructure(end_tile, end_direction);
|
|
|
|
|
|
|
|
DiagDirection start_direction = ReverseDiagDir(GetTunnelBridgeDirection(p1));
|
|
|
|
ConnectRoadToStructure(p1, start_direction);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2010-11-20 14:15:02 +00:00
|
|
|
/** Window class for handling the bridge-build GUI. */
|
2008-05-17 22:07:04 +00:00
|
|
|
class BuildBridgeWindow : public Window {
|
|
|
|
private:
|
2008-05-26 17:53:37 +00:00
|
|
|
/* Runtime saved values */
|
2010-11-20 14:15:02 +00:00
|
|
|
static Listing last_sorting; ///< Last setting of the sort.
|
2007-12-06 18:29:31 +00:00
|
|
|
|
2008-05-26 17:53:37 +00:00
|
|
|
/* Constants for sorting the bridges */
|
|
|
|
static const StringID sorter_names[];
|
2009-03-15 16:04:39 +00:00
|
|
|
static GUIBridgeList::SortFunction * const sorter_funcs[];
|
2008-05-26 17:53:37 +00:00
|
|
|
|
|
|
|
/* Internal variables */
|
2008-05-17 22:07:04 +00:00
|
|
|
TileIndex start_tile;
|
|
|
|
TileIndex end_tile;
|
|
|
|
uint32 type;
|
2008-05-18 12:25:46 +00:00
|
|
|
GUIBridgeList *bridges;
|
2011-12-16 16:27:45 +00:00
|
|
|
int bridgetext_offset; ///< Horizontal offset of the text describing the bridge properties in #WID_BBS_BRIDGE_LIST relative to the left edge.
|
2010-08-12 08:37:01 +00:00
|
|
|
Scrollbar *vscroll;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-26 17:53:37 +00:00
|
|
|
/** Sort the bridges by their index */
|
2019-04-11 19:26:02 +00:00
|
|
|
static bool BridgeIndexSorter(const BuildBridgeData &a, const BuildBridgeData &b)
|
2008-05-26 17:53:37 +00:00
|
|
|
{
|
2019-04-11 19:26:02 +00:00
|
|
|
return a.index < b.index;
|
2008-05-26 17:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sort the bridges by their price */
|
2019-04-11 19:26:02 +00:00
|
|
|
static bool BridgePriceSorter(const BuildBridgeData &a, const BuildBridgeData &b)
|
2008-05-26 17:53:37 +00:00
|
|
|
{
|
2019-04-11 19:26:02 +00:00
|
|
|
return a.cost < b.cost;
|
2008-05-26 17:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sort the bridges by their maximum speed */
|
2019-04-11 19:26:02 +00:00
|
|
|
static bool BridgeSpeedSorter(const BuildBridgeData &a, const BuildBridgeData &b)
|
2008-05-26 17:53:37 +00:00
|
|
|
{
|
2019-04-11 19:26:02 +00:00
|
|
|
return a.spec->speed < b.spec->speed;
|
2008-05-26 17:53:37 +00:00
|
|
|
}
|
|
|
|
|
2008-05-17 22:07:04 +00:00
|
|
|
void BuildBridge(uint8 i)
|
|
|
|
{
|
2009-01-03 14:16:18 +00:00
|
|
|
switch ((TransportType)(this->type >> 15)) {
|
2018-09-25 20:20:24 +00:00
|
|
|
case TRANSPORT_RAIL: _last_railbridge_type = this->bridges->at(i).index; break;
|
|
|
|
case TRANSPORT_ROAD: _last_roadbridge_type = this->bridges->at(i).index; break;
|
2009-01-03 14:16:18 +00:00
|
|
|
default: break;
|
|
|
|
}
|
2018-09-25 20:20:24 +00:00
|
|
|
DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->at(i).index,
|
2009-04-21 23:40:56 +00:00
|
|
|
CMD_BUILD_BRIDGE | CMD_MSG(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE), CcBuildBridge);
|
2008-05-17 22:07:04 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-18 12:25:46 +00:00
|
|
|
/** Sort the builable bridges */
|
|
|
|
void SortBridgeList()
|
|
|
|
{
|
2008-05-26 17:53:37 +00:00
|
|
|
this->bridges->Sort();
|
2008-05-18 12:25:46 +00:00
|
|
|
|
|
|
|
/* Display the current sort variant */
|
2011-12-16 16:27:45 +00:00
|
|
|
this->GetWidget<NWidgetCore>(WID_BBS_DROPDOWN_CRITERIA)->widget_data = this->sorter_names[this->bridges->SortType()];
|
2008-05-18 12:25:46 +00:00
|
|
|
|
|
|
|
/* Set the modified widgets dirty */
|
2011-12-16 16:27:45 +00:00
|
|
|
this->SetWidgetDirty(WID_BBS_DROPDOWN_CRITERIA);
|
|
|
|
this->SetWidgetDirty(WID_BBS_BRIDGE_LIST);
|
2008-05-18 12:25:46 +00:00
|
|
|
}
|
|
|
|
|
2008-05-17 22:07:04 +00:00
|
|
|
public:
|
2013-05-26 19:23:42 +00:00
|
|
|
BuildBridgeWindow(WindowDesc *desc, TileIndex start, TileIndex end, uint32 br_type, GUIBridgeList *bl) : Window(desc),
|
2008-05-17 22:07:04 +00:00
|
|
|
start_tile(start),
|
|
|
|
end_tile(end),
|
|
|
|
type(br_type),
|
|
|
|
bridges(bl)
|
|
|
|
{
|
2013-05-26 19:23:42 +00:00
|
|
|
this->CreateNestedTree();
|
2011-12-16 16:27:45 +00:00
|
|
|
this->vscroll = this->GetScrollbar(WID_BBS_SCROLLBAR);
|
2009-08-03 15:25:52 +00:00
|
|
|
/* Change the data, or the caption of the gui. Set it to road or rail, accordingly. */
|
2011-12-16 16:27:45 +00:00
|
|
|
this->GetWidget<NWidgetCore>(WID_BBS_CAPTION)->widget_data = (GB(this->type, 15, 2) == TRANSPORT_ROAD) ? STR_SELECT_ROAD_BRIDGE_CAPTION : STR_SELECT_RAIL_BRIDGE_CAPTION;
|
2013-05-26 19:23:42 +00:00
|
|
|
this->FinishInitNested(GB(br_type, 15, 2)); // Initializes 'this->bridgetext_offset'.
|
2009-08-03 15:25:52 +00:00
|
|
|
|
2009-02-08 15:45:34 +00:00
|
|
|
this->parent = FindWindowById(WC_BUILD_TOOLBAR, GB(this->type, 15, 2));
|
2008-05-26 17:53:37 +00:00
|
|
|
this->bridges->SetListing(this->last_sorting);
|
|
|
|
this->bridges->SetSortFuncs(this->sorter_funcs);
|
2008-06-08 19:18:01 +00:00
|
|
|
this->bridges->NeedResort();
|
2008-05-18 12:25:46 +00:00
|
|
|
this->SortBridgeList();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2019-03-27 23:09:33 +00:00
|
|
|
this->vscroll->SetCount((uint)bl->size());
|
2008-05-17 22:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~BuildBridgeWindow()
|
|
|
|
{
|
2008-05-26 17:53:37 +00:00
|
|
|
this->last_sorting = this->bridges->GetListing();
|
|
|
|
|
2008-05-17 22:07:04 +00:00
|
|
|
delete bridges;
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
|
2009-08-03 15:25:52 +00:00
|
|
|
{
|
|
|
|
switch (widget) {
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_BBS_DROPDOWN_ORDER: {
|
2009-09-19 11:31:12 +00:00
|
|
|
Dimension d = GetStringBoundingBox(this->GetWidget<NWidgetCore>(widget)->widget_data);
|
2014-10-05 11:20:02 +00:00
|
|
|
d.width += padding.width + Window::SortButtonWidth() * 2; // Doubled since the string is centred and it also looks better.
|
2009-08-03 15:25:52 +00:00
|
|
|
d.height += padding.height;
|
|
|
|
*size = maxdim(*size, d);
|
|
|
|
break;
|
|
|
|
}
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_BBS_DROPDOWN_CRITERIA: {
|
2009-08-03 15:25:52 +00:00
|
|
|
Dimension d = {0, 0};
|
|
|
|
for (const StringID *str = this->sorter_names; *str != INVALID_STRING_ID; str++) {
|
|
|
|
d = maxdim(d, GetStringBoundingBox(*str));
|
|
|
|
}
|
|
|
|
d.width += padding.width;
|
|
|
|
d.height += padding.height;
|
|
|
|
*size = maxdim(*size, d);
|
|
|
|
break;
|
|
|
|
}
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_BBS_BRIDGE_LIST: {
|
2009-08-03 15:25:52 +00:00
|
|
|
Dimension sprite_dim = {0, 0}; // Biggest bridge sprite dimension
|
|
|
|
Dimension text_dim = {0, 0}; // Biggest text dimension
|
2018-09-23 11:23:54 +00:00
|
|
|
for (int i = 0; i < (int)this->bridges->size(); i++) {
|
2018-09-25 20:20:24 +00:00
|
|
|
const BridgeSpec *b = this->bridges->at(i).spec;
|
2009-08-03 15:25:52 +00:00
|
|
|
sprite_dim = maxdim(sprite_dim, GetSpriteSize(b->sprite));
|
|
|
|
|
2018-09-25 20:20:24 +00:00
|
|
|
SetDParam(2, this->bridges->at(i).cost);
|
2009-08-03 15:25:52 +00:00
|
|
|
SetDParam(1, b->speed);
|
|
|
|
SetDParam(0, b->material);
|
2010-12-31 10:15:35 +00:00
|
|
|
text_dim = maxdim(text_dim, GetStringBoundingBox(_game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_SCENEDIT_INFO : STR_SELECT_BRIDGE_INFO));
|
2009-08-03 15:25:52 +00:00
|
|
|
}
|
2009-08-03 16:36:40 +00:00
|
|
|
sprite_dim.height++; // Sprite is rendered one pixel down in the matrix field.
|
|
|
|
text_dim.height++; // Allowing the bottom row pixels to be rendered on the edge of the matrix field.
|
2021-01-08 10:16:18 +00:00
|
|
|
resize->height = std::max(sprite_dim.height, text_dim.height) + 2; // Max of both sizes + account for matrix edges.
|
2008-05-18 12:25:46 +00:00
|
|
|
|
2009-08-03 15:25:52 +00:00
|
|
|
this->bridgetext_offset = WD_MATRIX_LEFT + sprite_dim.width + 1; // Left edge of text, 1 pixel distance from the sprite.
|
|
|
|
size->width = this->bridgetext_offset + text_dim.width + WD_MATRIX_RIGHT;
|
|
|
|
size->height = 4 * resize->height; // Smallest bridge gui is 4 entries high in the matrix.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-18 12:25:46 +00:00
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
|
2010-12-11 10:10:25 +00:00
|
|
|
{
|
|
|
|
/* Position the window so hopefully the first bridge from the list is under the mouse pointer. */
|
2011-12-16 16:27:45 +00:00
|
|
|
NWidgetBase *list = this->GetWidget<NWidgetBase>(WID_BBS_BRIDGE_LIST);
|
2010-12-11 10:10:25 +00:00
|
|
|
Point corner; // point of the top left corner of the window.
|
|
|
|
corner.y = Clamp(_cursor.pos.y - list->pos_y - 5, GetMainViewTop(), GetMainViewBottom() - sm_height);
|
|
|
|
corner.x = Clamp(_cursor.pos.x - list->pos_x - 5, 0, _screen.width - sm_width);
|
|
|
|
return corner;
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void DrawWidget(const Rect &r, int widget) const override
|
2009-08-03 15:25:52 +00:00
|
|
|
{
|
|
|
|
switch (widget) {
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_BBS_DROPDOWN_ORDER:
|
2009-08-03 15:25:52 +00:00
|
|
|
this->DrawSortButtonState(widget, this->bridges->IsDescSortOrder() ? SBS_DOWN : SBS_UP);
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_BBS_BRIDGE_LIST: {
|
2009-08-03 16:36:40 +00:00
|
|
|
uint y = r.top;
|
2018-09-23 11:23:54 +00:00
|
|
|
for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < (int)this->bridges->size(); i++) {
|
2018-09-25 20:20:24 +00:00
|
|
|
const BridgeSpec *b = this->bridges->at(i).spec;
|
2008-05-17 22:07:04 +00:00
|
|
|
|
2018-09-25 20:20:24 +00:00
|
|
|
SetDParam(2, this->bridges->at(i).cost);
|
2009-08-03 15:25:52 +00:00
|
|
|
SetDParam(1, b->speed);
|
|
|
|
SetDParam(0, b->material);
|
2008-05-18 12:25:46 +00:00
|
|
|
|
2009-08-03 16:36:40 +00:00
|
|
|
DrawSprite(b->sprite, b->pal, r.left + WD_MATRIX_LEFT, y + this->resize.step_height - 1 - GetSpriteSize(b->sprite).height);
|
2010-12-31 10:15:35 +00:00
|
|
|
DrawStringMultiLine(r.left + this->bridgetext_offset, r.right, y + 2, y + this->resize.step_height,
|
|
|
|
_game_mode == GM_EDITOR ? STR_SELECT_BRIDGE_SCENEDIT_INFO : STR_SELECT_BRIDGE_INFO);
|
2009-08-03 15:25:52 +00:00
|
|
|
y += this->resize.step_height;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-05-17 22:07:04 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
EventState OnKeyPress(WChar key, uint16 keycode) override
|
2008-05-17 22:07:04 +00:00
|
|
|
{
|
|
|
|
const uint8 i = keycode - '1';
|
2018-09-23 11:23:54 +00:00
|
|
|
if (i < 9 && i < this->bridges->size()) {
|
2008-05-17 22:07:04 +00:00
|
|
|
/* Build the requested bridge */
|
|
|
|
this->BuildBridge(i);
|
|
|
|
delete this;
|
2008-05-17 23:11:06 +00:00
|
|
|
return ES_HANDLED;
|
2008-05-17 22:07:04 +00:00
|
|
|
}
|
2008-05-17 23:11:06 +00:00
|
|
|
return ES_NOT_HANDLED;
|
2008-05-17 22:07:04 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnClick(Point pt, int widget, int click_count) override
|
2008-05-17 22:07:04 +00:00
|
|
|
{
|
2008-05-18 12:25:46 +00:00
|
|
|
switch (widget) {
|
|
|
|
default: break;
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_BBS_BRIDGE_LIST: {
|
|
|
|
uint i = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_BBS_BRIDGE_LIST);
|
2018-09-23 11:23:54 +00:00
|
|
|
if (i < this->bridges->size()) {
|
2010-07-26 13:08:48 +00:00
|
|
|
this->BuildBridge(i);
|
|
|
|
delete this;
|
2007-11-29 18:27:39 +00:00
|
|
|
}
|
2010-08-01 18:53:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-05-18 12:25:46 +00:00
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_BBS_DROPDOWN_ORDER:
|
2008-05-26 17:53:37 +00:00
|
|
|
this->bridges->ToggleSortOrder();
|
|
|
|
this->SetDirty();
|
2008-05-18 12:25:46 +00:00
|
|
|
break;
|
|
|
|
|
2011-12-16 16:27:45 +00:00
|
|
|
case WID_BBS_DROPDOWN_CRITERIA:
|
|
|
|
ShowDropDownMenu(this, this->sorter_names, this->bridges->SortType(), WID_BBS_DROPDOWN_CRITERIA, 0, 0);
|
2008-05-18 12:25:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnDropdownSelect(int widget, int index) override
|
2008-05-18 12:25:46 +00:00
|
|
|
{
|
2011-12-16 16:27:45 +00:00
|
|
|
if (widget == WID_BBS_DROPDOWN_CRITERIA && this->bridges->SortType() != index) {
|
2008-05-26 17:53:37 +00:00
|
|
|
this->bridges->SetSortType(index);
|
2008-05-18 12:25:46 +00:00
|
|
|
|
|
|
|
this->SortBridgeList();
|
2008-05-17 22:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-29 18:27:39 +00:00
|
|
|
|
2019-03-04 07:49:37 +00:00
|
|
|
void OnResize() override
|
2008-05-17 22:07:04 +00:00
|
|
|
{
|
2011-12-16 16:27:45 +00:00
|
|
|
this->vscroll->SetCapacityFromWidget(this, WID_BBS_BRIDGE_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2008-05-17 22:07:04 +00:00
|
|
|
};
|
|
|
|
|
2010-11-20 14:15:02 +00:00
|
|
|
/** Set the default sorting for the bridges */
|
2010-12-11 10:10:25 +00:00
|
|
|
Listing BuildBridgeWindow::last_sorting = {true, 2};
|
2008-05-26 17:53:37 +00:00
|
|
|
|
2010-11-20 14:15:02 +00:00
|
|
|
/** Available bridge sorting functions. */
|
2009-01-10 00:31:47 +00:00
|
|
|
GUIBridgeList::SortFunction * const BuildBridgeWindow::sorter_funcs[] = {
|
2008-05-26 17:53:37 +00:00
|
|
|
&BridgeIndexSorter,
|
|
|
|
&BridgePriceSorter,
|
|
|
|
&BridgeSpeedSorter
|
|
|
|
};
|
|
|
|
|
2010-11-20 14:15:02 +00:00
|
|
|
/** Names of the sorting functions. */
|
2008-05-26 17:53:37 +00:00
|
|
|
const StringID BuildBridgeWindow::sorter_names[] = {
|
|
|
|
STR_SORT_BY_NUMBER,
|
2009-07-22 22:44:56 +00:00
|
|
|
STR_SORT_BY_COST,
|
2008-05-26 17:53:37 +00:00
|
|
|
STR_SORT_BY_MAX_SPEED,
|
|
|
|
INVALID_STRING_ID
|
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2010-11-20 14:15:02 +00:00
|
|
|
/** Widgets of the bridge gui. */
|
2009-03-22 21:16:57 +00:00
|
|
|
static const NWidgetPart _nested_build_bridge_widgets[] = {
|
|
|
|
/* Header */
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2009-11-24 18:05:55 +00:00
|
|
|
NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
|
2011-12-16 16:27:45 +00:00
|
|
|
NWidget(WWT_CAPTION, COLOUR_DARK_GREEN, WID_BBS_CAPTION), SetDataTip(STR_SELECT_RAIL_BRIDGE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
|
2013-05-26 19:30:07 +00:00
|
|
|
NWidget(WWT_DEFSIZEBOX, COLOUR_DARK_GREEN),
|
2009-03-22 21:16:57 +00:00
|
|
|
EndContainer(),
|
2009-08-03 16:42:02 +00:00
|
|
|
|
2009-03-22 21:16:57 +00:00
|
|
|
NWidget(NWID_HORIZONTAL),
|
2009-08-03 16:42:02 +00:00
|
|
|
NWidget(NWID_VERTICAL),
|
|
|
|
/* Sort order + criteria buttons */
|
|
|
|
NWidget(NWID_HORIZONTAL),
|
2011-12-16 16:27:45 +00:00
|
|
|
NWidget(WWT_TEXTBTN, COLOUR_DARK_GREEN, WID_BBS_DROPDOWN_ORDER), SetFill(1, 0), SetDataTip(STR_BUTTON_SORT_BY, STR_TOOLTIP_SORT_ORDER),
|
|
|
|
NWidget(WWT_DROPDOWN, COLOUR_DARK_GREEN, WID_BBS_DROPDOWN_CRITERIA), SetFill(1, 0), SetDataTip(0x0, STR_TOOLTIP_SORT_CRITERIA),
|
2009-08-03 16:42:02 +00:00
|
|
|
EndContainer(),
|
|
|
|
/* Matrix. */
|
2013-06-30 14:36:31 +00:00
|
|
|
NWidget(WWT_MATRIX, COLOUR_DARK_GREEN, WID_BBS_BRIDGE_LIST), SetFill(1, 0), SetResize(0, 22), SetMatrixDataTip(1, 0, STR_SELECT_BRIDGE_SELECTION_TOOLTIP), SetScrollbar(WID_BBS_SCROLLBAR),
|
2009-08-03 16:42:02 +00:00
|
|
|
EndContainer(),
|
|
|
|
|
|
|
|
/* scrollbar + resize button */
|
2009-03-22 21:16:57 +00:00
|
|
|
NWidget(NWID_VERTICAL),
|
2011-12-16 16:27:45 +00:00
|
|
|
NWidget(NWID_VSCROLLBAR, COLOUR_DARK_GREEN, WID_BBS_SCROLLBAR),
|
2009-11-24 18:05:55 +00:00
|
|
|
NWidget(WWT_RESIZEBOX, COLOUR_DARK_GREEN),
|
2009-03-22 21:16:57 +00:00
|
|
|
EndContainer(),
|
|
|
|
EndContainer(),
|
|
|
|
};
|
|
|
|
|
2010-11-20 14:15:02 +00:00
|
|
|
/** Window definition for the rail bridge selection window. */
|
2013-05-26 19:23:42 +00:00
|
|
|
static WindowDesc _build_bridge_desc(
|
2013-05-26 19:25:01 +00:00
|
|
|
WDP_AUTO, "build_bridge", 200, 114,
|
2006-11-10 19:24:14 +00:00
|
|
|
WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
|
2009-11-24 17:28:29 +00:00
|
|
|
WDF_CONSTRUCTION,
|
2009-11-15 10:26:01 +00:00
|
|
|
_nested_build_bridge_widgets, lengthof(_nested_build_bridge_widgets)
|
2009-03-15 15:12:06 +00:00
|
|
|
);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-17 22:07:04 +00:00
|
|
|
/**
|
|
|
|
* Prepare the data for the build a bridge window.
|
|
|
|
* If we can't build a bridge under the given conditions
|
|
|
|
* show an error message.
|
|
|
|
*
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param start The start tile of the bridge
|
2008-05-17 22:07:04 +00:00
|
|
|
* @param end The end tile of the bridge
|
|
|
|
* @param transport_type The transport type
|
2008-09-14 19:36:31 +00:00
|
|
|
* @param road_rail_type The road/rail type
|
2008-05-17 22:07:04 +00:00
|
|
|
*/
|
2008-09-14 19:36:31 +00:00
|
|
|
void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, byte road_rail_type)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-07-21 22:25:21 +00:00
|
|
|
DeleteWindowByClass(WC_BUILD_BRIDGE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-05-17 22:07:04 +00:00
|
|
|
/* Data type for the bridge.
|
|
|
|
* Bit 16,15 = transport type,
|
2008-09-14 19:36:31 +00:00
|
|
|
* 14..8 = road/rail types,
|
2008-05-17 22:07:04 +00:00
|
|
|
* 7..0 = type of bridge */
|
2008-09-14 19:36:31 +00:00
|
|
|
uint32 type = (transport_type << 15) | (road_rail_type << 8);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-03 14:16:18 +00:00
|
|
|
/* The bridge length without ramps. */
|
|
|
|
const uint bridge_len = GetTunnelBridgeLength(start, end);
|
|
|
|
|
2013-01-08 22:46:42 +00:00
|
|
|
/* If Ctrl is being pressed, check whether the last bridge built is available
|
2009-01-03 14:16:18 +00:00
|
|
|
* If so, return this bridge type. Otherwise continue normally.
|
|
|
|
* We store bridge types for each transport type, so we have to check for
|
|
|
|
* the transport type beforehand.
|
|
|
|
*/
|
|
|
|
BridgeType last_bridge_type = 0;
|
|
|
|
switch (transport_type) {
|
|
|
|
case TRANSPORT_ROAD: last_bridge_type = _last_roadbridge_type; break;
|
|
|
|
case TRANSPORT_RAIL: last_bridge_type = _last_railbridge_type; break;
|
|
|
|
default: break; // water ways and air routes don't have bridge types
|
|
|
|
}
|
2010-04-04 14:22:55 +00:00
|
|
|
if (_ctrl_pressed && CheckBridgeAvailability(last_bridge_type, bridge_len).Succeeded()) {
|
2009-04-21 23:40:56 +00:00
|
|
|
DoCommandP(end, start, type | last_bridge_type, CMD_BUILD_BRIDGE | CMD_MSG(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE), CcBuildBridge);
|
2009-01-03 14:16:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-29 18:27:39 +00:00
|
|
|
/* only query bridge building possibility once, result is the same for all bridges!
|
|
|
|
* returns CMD_ERROR on failure, and price on success */
|
|
|
|
StringID errmsg = INVALID_STRING_ID;
|
2010-09-13 19:45:39 +00:00
|
|
|
CommandCost ret = DoCommand(end, start, type, CommandFlagsToDCFlags(GetCommandFlags(CMD_BUILD_BRIDGE)) | DC_QUERY_COST, CMD_BUILD_BRIDGE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
GUIBridgeList *bl = nullptr;
|
2010-01-18 22:57:21 +00:00
|
|
|
if (ret.Failed()) {
|
2010-03-14 14:36:37 +00:00
|
|
|
errmsg = ret.GetErrorMessage();
|
2005-10-22 06:39:32 +00:00
|
|
|
} else {
|
2009-01-03 14:16:18 +00:00
|
|
|
/* check which bridges can be built */
|
2007-11-29 18:27:39 +00:00
|
|
|
const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);
|
|
|
|
|
2008-05-26 16:23:23 +00:00
|
|
|
bl = new GUIBridgeList();
|
|
|
|
|
2010-05-22 14:12:48 +00:00
|
|
|
Money infra_cost = 0;
|
|
|
|
switch (transport_type) {
|
2019-04-06 06:46:15 +00:00
|
|
|
case TRANSPORT_ROAD: {
|
2011-07-30 14:10:12 +00:00
|
|
|
/* In case we add a new road type as well, we must be aware of those costs. */
|
2019-04-06 06:46:15 +00:00
|
|
|
RoadType road_rt = INVALID_ROADTYPE;
|
|
|
|
RoadType tram_rt = INVALID_ROADTYPE;
|
|
|
|
if (IsBridgeTile(start)) {
|
|
|
|
road_rt = GetRoadTypeRoad(start);
|
|
|
|
tram_rt = GetRoadTypeTram(start);
|
|
|
|
}
|
|
|
|
if (RoadTypeIsRoad((RoadType)road_rail_type)) {
|
|
|
|
road_rt = (RoadType)road_rail_type;
|
|
|
|
} else {
|
|
|
|
tram_rt = (RoadType)road_rail_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (road_rt != INVALID_ROADTYPE) infra_cost += (bridge_len + 2) * 2 * RoadBuildCost(road_rt);
|
|
|
|
if (tram_rt != INVALID_ROADTYPE) infra_cost += (bridge_len + 2) * 2 * RoadBuildCost(tram_rt);
|
|
|
|
|
2011-07-30 14:10:12 +00:00
|
|
|
break;
|
2019-04-06 06:46:15 +00:00
|
|
|
}
|
2010-05-22 14:12:48 +00:00
|
|
|
case TRANSPORT_RAIL: infra_cost = (bridge_len + 2) * RailBuildCost((RailType)road_rail_type); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2020-06-28 11:53:56 +00:00
|
|
|
bool any_available = false;
|
|
|
|
CommandCost type_check;
|
2007-11-29 18:27:39 +00:00
|
|
|
/* loop for all bridgetypes */
|
2008-02-11 04:12:30 +00:00
|
|
|
for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) {
|
2020-06-28 11:53:56 +00:00
|
|
|
type_check = CheckBridgeAvailability(brd_type, bridge_len);
|
|
|
|
if (type_check.Succeeded()) {
|
2007-11-29 18:27:39 +00:00
|
|
|
/* bridge is accepted, add to list */
|
2020-12-14 23:22:04 +00:00
|
|
|
BuildBridgeData &item = bl->emplace_back();
|
2019-02-18 22:39:06 +00:00
|
|
|
item.index = brd_type;
|
|
|
|
item.spec = GetBridgeSpec(brd_type);
|
2007-11-29 18:27:39 +00:00
|
|
|
/* Add to terraforming & bulldozing costs the cost of the
|
|
|
|
* bridge itself (not computed with DC_QUERY_COST) */
|
2019-02-18 22:39:06 +00:00
|
|
|
item.cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price[PR_BUILD_BRIDGE] * item.spec->price) >> 8) + infra_cost;
|
2020-06-28 11:53:56 +00:00
|
|
|
any_available = true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-28 11:53:56 +00:00
|
|
|
/* give error cause if no bridges available here*/
|
|
|
|
if (!any_available)
|
|
|
|
{
|
|
|
|
errmsg = type_check.GetErrorMessage();
|
|
|
|
}
|
2007-11-29 18:27:39 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (bl != nullptr && bl->size() != 0) {
|
2008-05-17 22:07:04 +00:00
|
|
|
new BuildBridgeWindow(&_build_bridge_desc, start, end, type, bl);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2009-05-22 13:13:55 +00:00
|
|
|
delete bl;
|
2010-02-24 14:46:15 +00:00
|
|
|
ShowErrorMessage(STR_ERROR_CAN_T_BUILD_BRIDGE_HERE, errmsg, WL_INFO, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|