2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2004-11-25 10:47:30 +00:00
|
|
|
#include "table/strings.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "gui.h"
|
2007-12-19 20:45:46 +00:00
|
|
|
#include "window_gui.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
|
|
|
#include "economy_func.h"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "variables.h"
|
2005-08-01 20:23:38 +00:00
|
|
|
#include "bridge.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"
|
2007-12-26 11:45:43 +00:00
|
|
|
#include "map_func.h"
|
2008-01-09 09:45:45 +00:00
|
|
|
#include "viewport_func.h"
|
|
|
|
#include "gfx_func.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static struct BridgeData {
|
2007-12-06 18:29:31 +00:00
|
|
|
uint8 last_size;
|
2005-07-19 21:49:35 +00:00
|
|
|
uint count;
|
2004-08-09 17:04:08 +00:00
|
|
|
TileIndex start_tile;
|
|
|
|
TileIndex end_tile;
|
2007-11-29 18:27:39 +00:00
|
|
|
uint8 type;
|
|
|
|
uint8 indexes[MAX_BRIDGES];
|
2007-06-21 14:32:27 +00:00
|
|
|
Money costs[MAX_BRIDGES];
|
2007-12-06 18:29:31 +00:00
|
|
|
|
|
|
|
BridgeData()
|
|
|
|
: last_size(4)
|
|
|
|
, count(0)
|
|
|
|
{};
|
2005-08-01 20:23:38 +00:00
|
|
|
} _bridgedata;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
void CcBuildBridge(bool success, TileIndex tile, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-12-04 09:26:39 +00:00
|
|
|
if (success) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void BuildBridge(Window *w, int i)
|
|
|
|
{
|
|
|
|
DeleteWindow(w);
|
2005-11-14 19:48:04 +00:00
|
|
|
DoCommandP(_bridgedata.end_tile, _bridgedata.start_tile,
|
|
|
|
_bridgedata.indexes[i] | (_bridgedata.type << 8), CcBuildBridge,
|
2007-09-04 11:58:27 +00:00
|
|
|
CMD_BUILD_BRIDGE | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-12-06 18:29:31 +00:00
|
|
|
/* Names of the build bridge selection window */
|
|
|
|
enum BuildBridgeSelectionWidgets {
|
|
|
|
BBSW_CLOSEBOX = 0,
|
|
|
|
BBSW_CAPTION,
|
|
|
|
BBSW_BRIDGE_LIST,
|
|
|
|
BBSW_SCROLLBAR,
|
|
|
|
BBSW_RESIZEBOX
|
|
|
|
};
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static void BuildBridgeWndProc(Window *w, WindowEvent *e)
|
|
|
|
{
|
2006-02-01 07:36:15 +00:00
|
|
|
switch (e->event) {
|
2007-12-06 18:29:31 +00:00
|
|
|
case WE_CREATE:
|
|
|
|
w->resize.step_height = 22;
|
|
|
|
w->vscroll.count = _bridgedata.count;
|
|
|
|
|
|
|
|
if (_bridgedata.last_size <= 4) {
|
|
|
|
w->vscroll.cap = 4;
|
|
|
|
} else {
|
|
|
|
/* Resize the bridge selection window if we used a bigger one the last time */
|
|
|
|
w->vscroll.cap = (w->vscroll.count > _bridgedata.last_size) ? _bridgedata.last_size : w->vscroll.count;
|
|
|
|
ResizeWindow(w, 0, (w->vscroll.cap - 4) * w->resize.step_height);
|
|
|
|
w->widget[BBSW_BRIDGE_LIST].data = (w->vscroll.cap << 8) + 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2007-11-29 18:27:39 +00:00
|
|
|
case WE_PAINT: {
|
2007-03-03 10:48:25 +00:00
|
|
|
DrawWindowWidgets(w);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-29 18:27:39 +00:00
|
|
|
uint y = 15;
|
|
|
|
for (uint i = 0; (i < w->vscroll.cap) && ((i + w->vscroll.pos) < _bridgedata.count); i++) {
|
2007-03-03 10:48:25 +00:00
|
|
|
const Bridge *b = &_bridge[_bridgedata.indexes[i + w->vscroll.pos]];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-21 17:25:17 +00:00
|
|
|
SetDParam(2, _bridgedata.costs[i + w->vscroll.pos]);
|
2007-03-03 10:48:25 +00:00
|
|
|
SetDParam(1, b->speed * 10 / 16);
|
|
|
|
SetDParam(0, b->material);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-29 18:27:39 +00:00
|
|
|
DrawSprite(b->sprite, b->pal, 3, y);
|
|
|
|
DrawString(44, y, STR_500D, TC_FROMSTRING);
|
|
|
|
y += w->resize.step_height;
|
2007-03-03 10:48:25 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-11-29 18:27:39 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-03 10:48:25 +00:00
|
|
|
case WE_KEYPRESS: {
|
2007-11-29 18:27:39 +00:00
|
|
|
const uint8 i = e->we.keypress.keycode - '1';
|
2007-03-03 10:48:25 +00:00
|
|
|
if (i < 9 && i < _bridgedata.count) {
|
|
|
|
e->we.keypress.cont = false;
|
|
|
|
BuildBridge(w, i);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-03 10:48:25 +00:00
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-03-03 10:48:25 +00:00
|
|
|
case WE_CLICK:
|
2007-12-06 18:29:31 +00:00
|
|
|
if (e->we.click.widget == BBSW_BRIDGE_LIST) {
|
2007-11-29 18:27:39 +00:00
|
|
|
uint ind = ((int)e->we.click.pt.y - 14) / w->resize.step_height;
|
|
|
|
if (ind < w->vscroll.cap) {
|
|
|
|
ind += w->vscroll.pos;
|
|
|
|
if (ind < _bridgedata.count) {
|
|
|
|
BuildBridge(w, ind);
|
|
|
|
}
|
|
|
|
}
|
2007-03-03 10:48:25 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-11-29 18:27:39 +00:00
|
|
|
|
|
|
|
case WE_RESIZE:
|
|
|
|
w->vscroll.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
|
2007-12-06 18:29:31 +00:00
|
|
|
w->widget[BBSW_BRIDGE_LIST].data = (w->vscroll.cap << 8) + 1;
|
2007-11-29 18:27:39 +00:00
|
|
|
SetVScrollCount(w, _bridgedata.count);
|
2007-12-06 18:29:31 +00:00
|
|
|
|
|
|
|
_bridgedata.last_size = w->vscroll.cap;
|
2007-11-29 18:27:39 +00:00
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-06 18:29:31 +00:00
|
|
|
/* Widget definition for the rail bridge selection window */
|
2004-08-09 17:04:08 +00:00
|
|
|
static const Widget _build_bridge_widgets[] = {
|
2007-12-06 18:29:31 +00:00
|
|
|
{ WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, // BBSW_CLOSEBOX
|
|
|
|
{ WWT_CAPTION, RESIZE_NONE, 7, 11, 199, 0, 13, STR_100D_SELECT_RAIL_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS}, // BBSW_CAPTION
|
|
|
|
{ WWT_MATRIX, RESIZE_BOTTOM, 7, 0, 187, 14, 101, 0x401, STR_101F_BRIDGE_SELECTION_CLICK}, // BBSW_BRIDGE_LIST
|
|
|
|
{ WWT_SCROLLBAR, RESIZE_BOTTOM, 7, 188, 199, 14, 89, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, // BBSW_SCROLLBAR
|
|
|
|
{ WWT_RESIZEBOX, RESIZE_TB, 7, 188, 199, 90, 101, 0x0, STR_RESIZE_BUTTON}, // BBSW_RESIZEBOX
|
2004-09-07 21:48:09 +00:00
|
|
|
{ WIDGETS_END},
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2007-12-06 18:29:31 +00:00
|
|
|
/* Window definition for the rail bridge selection window */
|
2004-08-09 17:04:08 +00:00
|
|
|
static const WindowDesc _build_bridge_desc = {
|
2007-07-27 12:49:04 +00:00
|
|
|
WDP_AUTO, WDP_AUTO, 200, 102, 200, 102,
|
2006-11-10 19:24:14 +00:00
|
|
|
WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
|
2007-11-29 18:27:39 +00:00
|
|
|
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE,
|
2004-08-09 17:04:08 +00:00
|
|
|
_build_bridge_widgets,
|
|
|
|
BuildBridgeWndProc
|
|
|
|
};
|
|
|
|
|
2007-12-06 18:29:31 +00:00
|
|
|
/* Widget definition for the road bridge selection window */
|
2004-08-09 17:04:08 +00:00
|
|
|
static const Widget _build_road_bridge_widgets[] = {
|
2007-12-06 18:29:31 +00:00
|
|
|
{ WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, // BBSW_CLOSEBOX
|
|
|
|
{ WWT_CAPTION, RESIZE_NONE, 7, 11, 199, 0, 13, STR_1803_SELECT_ROAD_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS}, // BBSW_CAPTION
|
|
|
|
{ WWT_MATRIX, RESIZE_BOTTOM, 7, 0, 187, 14, 101, 0x401, STR_101F_BRIDGE_SELECTION_CLICK}, // BBSW_BRIDGE_LIST
|
|
|
|
{ WWT_SCROLLBAR, RESIZE_BOTTOM, 7, 188, 199, 14, 89, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, // BBSW_SCROLLBAR
|
|
|
|
{ WWT_RESIZEBOX, RESIZE_TB, 7, 188, 199, 90, 101, 0x0, STR_RESIZE_BUTTON}, // BBSW_RESIZEBOX
|
2004-09-07 21:48:09 +00:00
|
|
|
{ WIDGETS_END},
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2007-12-06 18:29:31 +00:00
|
|
|
/* Window definition for the road bridge selection window */
|
2004-08-09 17:04:08 +00:00
|
|
|
static const WindowDesc _build_road_bridge_desc = {
|
2007-07-27 12:49:04 +00:00
|
|
|
WDP_AUTO, WDP_AUTO, 200, 102, 200, 102,
|
2006-11-10 19:24:14 +00:00
|
|
|
WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
|
2007-11-29 18:27:39 +00:00
|
|
|
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE,
|
2004-08-09 17:04:08 +00:00
|
|
|
_build_road_bridge_widgets,
|
|
|
|
BuildBridgeWndProc
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2005-06-24 12:38:35 +00:00
|
|
|
void ShowBuildBridgeWindow(TileIndex start, TileIndex end, byte bridge_type)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
DeleteWindowById(WC_BUILD_BRIDGE, 0);
|
|
|
|
|
2005-08-01 20:23:38 +00:00
|
|
|
_bridgedata.type = bridge_type;
|
|
|
|
_bridgedata.start_tile = start;
|
|
|
|
_bridgedata.end_tile = end;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
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;
|
|
|
|
CommandCost ret = DoCommand(end, start, (bridge_type << 8), DC_AUTO | DC_QUERY_COST, CMD_BUILD_BRIDGE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-29 18:27:39 +00:00
|
|
|
uint8 j = 0;
|
2005-12-10 12:05:39 +00:00
|
|
|
if (CmdFailed(ret)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
errmsg = _error_message;
|
2005-10-22 06:39:32 +00:00
|
|
|
} else {
|
2007-11-29 18:27:39 +00:00
|
|
|
/* check which bridges can be built
|
|
|
|
* get absolute bridge length
|
|
|
|
* length of the middle parts of the bridge */
|
|
|
|
const uint bridge_len = GetBridgeLength(start, end);
|
|
|
|
/* total length of bridge */
|
|
|
|
const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);
|
|
|
|
|
|
|
|
/* loop for all bridgetypes */
|
|
|
|
for (bridge_type = 0; bridge_type != MAX_BRIDGES; bridge_type++) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (CheckBridge_Stuff(bridge_type, bridge_len)) {
|
2007-11-29 18:27:39 +00:00
|
|
|
/* bridge is accepted, add to list */
|
2005-08-01 20:23:38 +00:00
|
|
|
const Bridge *b = &_bridge[bridge_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) */
|
2007-06-18 19:53:50 +00:00
|
|
|
_bridgedata.costs[j] = ret.GetCost() + (((int64)tot_bridgedata_len * _price.build_bridge * b->price) >> 8);
|
2005-08-01 20:23:38 +00:00
|
|
|
_bridgedata.indexes[j] = bridge_type;
|
2004-08-09 17:04:08 +00:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-29 18:27:39 +00:00
|
|
|
_bridgedata.count = j;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (j != 0) {
|
2007-12-06 18:29:31 +00:00
|
|
|
AllocateWindowDesc((_bridgedata.type & 0x80) ? &_build_road_bridge_desc : &_build_bridge_desc);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2006-04-03 09:07:21 +00:00
|
|
|
ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|