author | rubidium |
Mon, 16 Jul 2007 09:16:58 +0000 | |
changeset 7762 | 03721db0ac1c |
parent 7498 | ce6588257e8d |
child 7837 | 65d7362153a6 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
6443
b8f06d8eb7be
(svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents:
6182
diff
changeset
|
3 |
/** @file bridge_gui.cpp Graphical user interface for bridge construction */ |
2262
bd59b2d8d75f
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
4 |
|
0 | 5 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1781
diff
changeset
|
6 |
#include "openttd.h" |
507
8aa8100b0b22
(svn r815) Include strings.h only in the files which need it.
tron
parents:
337
diff
changeset
|
7 |
#include "table/strings.h" |
7762
03721db0ac1c
(svn r10587) -Codechange: move the string/dparam related stuff from variables.h to strings.h
rubidium
parents:
7498
diff
changeset
|
8 |
#include "strings.h" |
2163
637ec3c361f5
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
9 |
#include "functions.h" |
679
e959706a3e4d
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
543
diff
changeset
|
10 |
#include "map.h" |
0 | 11 |
#include "window.h" |
12 |
#include "gui.h" |
|
13 |
#include "viewport.h" |
|
14 |
#include "gfx.h" |
|
15 |
#include "command.h" |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
203
diff
changeset
|
16 |
#include "sound.h" |
2159
3b634157c3b2
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2133
diff
changeset
|
17 |
#include "variables.h" |
2262
bd59b2d8d75f
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
18 |
#include "bridge.h" |
0 | 19 |
|
20 |
static struct BridgeData { |
|
2133 | 21 |
uint count; |
0 | 22 |
TileIndex start_tile; |
23 |
TileIndex end_tile; |
|
24 |
byte type; |
|
25 |
byte indexes[MAX_BRIDGES]; |
|
7486
d130c10f4dab
(svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents:
7446
diff
changeset
|
26 |
Money costs[MAX_BRIDGES]; |
2262
bd59b2d8d75f
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
27 |
} _bridgedata; |
0 | 28 |
|
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1891
diff
changeset
|
29 |
void CcBuildBridge(bool success, TileIndex tile, uint32 p1, uint32 p2) |
0 | 30 |
{ |
541 | 31 |
if (success) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, tile); |
0 | 32 |
} |
33 |
||
34 |
static void BuildBridge(Window *w, int i) |
|
35 |
{ |
|
36 |
DeleteWindow(w); |
|
2639 | 37 |
DoCommandP(_bridgedata.end_tile, _bridgedata.start_tile, |
38 |
_bridgedata.indexes[i] | (_bridgedata.type << 8), CcBuildBridge, |
|
0 | 39 |
CMD_BUILD_BRIDGE | CMD_AUTO | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE)); |
40 |
} |
|
41 |
||
42 |
static void BuildBridgeWndProc(Window *w, WindowEvent *e) |
|
43 |
{ |
|
2952 | 44 |
switch (e->event) { |
6530
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
45 |
case WE_PAINT: |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
46 |
DrawWindowWidgets(w); |
0 | 47 |
|
6530
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
48 |
for (uint i = 0; i < 4 && i + w->vscroll.pos < _bridgedata.count; i++) { |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
49 |
const Bridge *b = &_bridge[_bridgedata.indexes[i + w->vscroll.pos]]; |
0 | 50 |
|
7498
ce6588257e8d
(svn r10258) -Codechange: as we are now using int64 all over the place, it's better to use int64 variables in the string generating too instead of packing them into two int32s.
rubidium
parents:
7487
diff
changeset
|
51 |
SetDParam(2, _bridgedata.costs[i + w->vscroll.pos]); |
6530
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
52 |
SetDParam(1, b->speed * 10 / 16); |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
53 |
SetDParam(0, b->material); |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
54 |
DrawSprite(b->sprite, b->pal, 3, 15 + i * 22); |
0 | 55 |
|
6530
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
56 |
DrawString(44, 15 + i * 22 , STR_500D, 0); |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
57 |
} |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
58 |
break; |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
59 |
|
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
60 |
case WE_KEYPRESS: { |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
61 |
uint i = e->we.keypress.keycode - '1'; |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
62 |
if (i < 9 && i < _bridgedata.count) { |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
63 |
e->we.keypress.cont = false; |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
64 |
BuildBridge(w, i); |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
65 |
} |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
66 |
|
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
67 |
break; |
0 | 68 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
176
diff
changeset
|
69 |
|
6530
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
70 |
case WE_CLICK: |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
71 |
if (e->we.click.widget == 2) { |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
72 |
uint ind = ((int)e->we.click.pt.y - 14) / 22; |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
73 |
if (ind < 4 && (ind += w->vscroll.pos) < _bridgedata.count) |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
74 |
BuildBridge(w, ind); |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
75 |
} |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
76 |
break; |
0 | 77 |
} |
78 |
} |
|
79 |
||
80 |
static const Widget _build_bridge_widgets[] = { |
|
4344
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
81 |
{ WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, |
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
82 |
{ WWT_CAPTION, RESIZE_NONE, 7, 11, 199, 0, 13, STR_100D_SELECT_RAIL_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS}, |
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
83 |
{ WWT_MATRIX, RESIZE_NONE, 7, 0, 187, 14, 101, 0x401, STR_101F_BRIDGE_SELECTION_CLICK}, |
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
84 |
{ WWT_SCROLLBAR, RESIZE_NONE, 7, 188, 199, 14, 101, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, |
176
84990c4b9212
(svn r177) -Fix: padded out Widget code to solve warnings on C99 compiler (Tron)
darkvater
parents:
0
diff
changeset
|
85 |
{ WIDGETS_END}, |
0 | 86 |
}; |
87 |
||
88 |
static const WindowDesc _build_bridge_desc = { |
|
5070
0bbf5264bfb7
(svn r7128) -Codechange: Replace magic numbers by magic enums (windowdesc positioning WDP_AUTO = -1)
Darkvater
parents:
4634
diff
changeset
|
89 |
WDP_AUTO, WDP_AUTO, 200, 102, |
0bbf5264bfb7
(svn r7128) -Codechange: Replace magic numbers by magic enums (windowdesc positioning WDP_AUTO = -1)
Darkvater
parents:
4634
diff
changeset
|
90 |
WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR, |
0 | 91 |
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET, |
92 |
_build_bridge_widgets, |
|
93 |
BuildBridgeWndProc |
|
94 |
}; |
|
95 |
||
96 |
||
97 |
static const Widget _build_road_bridge_widgets[] = { |
|
4344
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
98 |
{ WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, |
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
99 |
{ WWT_CAPTION, RESIZE_NONE, 7, 11, 199, 0, 13, STR_1803_SELECT_ROAD_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS}, |
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
100 |
{ WWT_MATRIX, RESIZE_NONE, 7, 0, 187, 14, 101, 0x401, STR_101F_BRIDGE_SELECTION_CLICK}, |
5d0e40cd67b9
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
101 |
{ WWT_SCROLLBAR, RESIZE_NONE, 7, 188, 199, 14, 101, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, |
176
84990c4b9212
(svn r177) -Fix: padded out Widget code to solve warnings on C99 compiler (Tron)
darkvater
parents:
0
diff
changeset
|
102 |
{ WIDGETS_END}, |
0 | 103 |
}; |
104 |
||
105 |
static const WindowDesc _build_road_bridge_desc = { |
|
5070
0bbf5264bfb7
(svn r7128) -Codechange: Replace magic numbers by magic enums (windowdesc positioning WDP_AUTO = -1)
Darkvater
parents:
4634
diff
changeset
|
106 |
WDP_AUTO, WDP_AUTO, 200, 102, |
0bbf5264bfb7
(svn r7128) -Codechange: Replace magic numbers by magic enums (windowdesc positioning WDP_AUTO = -1)
Darkvater
parents:
4634
diff
changeset
|
107 |
WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR, |
0 | 108 |
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET, |
109 |
_build_road_bridge_widgets, |
|
110 |
BuildBridgeWndProc |
|
111 |
}; |
|
112 |
||
113 |
||
1977
4392ae3d8e31
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1891
diff
changeset
|
114 |
void ShowBuildBridgeWindow(TileIndex start, TileIndex end, byte bridge_type) |
0 | 115 |
{ |
2133 | 116 |
uint j = 0; |
7439
0c0e2945c890
(svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents:
6530
diff
changeset
|
117 |
CommandCost ret; |
2470 | 118 |
StringID errmsg; |
0 | 119 |
|
120 |
DeleteWindowById(WC_BUILD_BRIDGE, 0); |
|
121 |
||
2262
bd59b2d8d75f
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
122 |
_bridgedata.type = bridge_type; |
bd59b2d8d75f
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
123 |
_bridgedata.start_tile = start; |
bd59b2d8d75f
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
124 |
_bridgedata.end_tile = end; |
0 | 125 |
|
2470 | 126 |
errmsg = INVALID_STRING_ID; |
0 | 127 |
|
128 |
// only query bridge building possibility once, result is the same for all bridges! |
|
2548
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2470
diff
changeset
|
129 |
// returns CMD_ERROR on failure, and price on success |
3491
4c8427796c64
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3477
diff
changeset
|
130 |
ret = DoCommand(end, start, (bridge_type << 8), DC_AUTO | DC_QUERY_COST, CMD_BUILD_BRIDGE); |
0 | 131 |
|
2737
f16e0a808897
(svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents:
2639
diff
changeset
|
132 |
if (CmdFailed(ret)) { |
0 | 133 |
errmsg = _error_message; |
2548
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2470
diff
changeset
|
134 |
} else { |
97ada3bd2702
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2470
diff
changeset
|
135 |
// check which bridges can be built |
0 | 136 |
// get absolute bridge length |
6530
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
137 |
// length of the middle parts of the bridge |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
138 |
int bridge_len = GetBridgeLength(start, end); |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
139 |
// total length of bridge |
c43fa965e8ae
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6443
diff
changeset
|
140 |
int tot_bridgedata_len = bridge_len + 2; |
0 | 141 |
|
2262
bd59b2d8d75f
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
142 |
tot_bridgedata_len = CalcBridgeLenCostFactor(tot_bridgedata_len); |
0 | 143 |
|
4434
4175805666a5
(svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents:
4344
diff
changeset
|
144 |
for (bridge_type = 0; bridge_type != MAX_BRIDGES; bridge_type++) { // loop for all bridgetypes |
0 | 145 |
if (CheckBridge_Stuff(bridge_type, bridge_len)) { |
2262
bd59b2d8d75f
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
146 |
const Bridge *b = &_bridge[bridge_type]; |
0 | 147 |
// bridge is accepted, add to list |
148 |
// add to terraforming & bulldozing costs the cost of the bridge itself (not computed with DC_QUERY_COST) |
|
7446
1c4d469f986e
(svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents:
7439
diff
changeset
|
149 |
_bridgedata.costs[j] = ret.GetCost() + (((int64)tot_bridgedata_len * _price.build_bridge * b->price) >> 8); |
2262
bd59b2d8d75f
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
150 |
_bridgedata.indexes[j] = bridge_type; |
0 | 151 |
j++; |
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
||
2262
bd59b2d8d75f
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
156 |
_bridgedata.count = j; |
0 | 157 |
|
158 |
if (j != 0) { |
|
2262
bd59b2d8d75f
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
159 |
Window *w = AllocateWindowDesc((_bridgedata.type & 0x80) ? &_build_road_bridge_desc : &_build_bridge_desc); |
0 | 160 |
w->vscroll.cap = 4; |
161 |
w->vscroll.count = (byte)j; |
|
162 |
} else { |
|
3422
12cdb13ddb56
(svn r4249) -Codechange: Replace more occurences of 16 by TILE_SIZE and of 8 by TILE_HEIGHT. Reverted one change from the previous commit because it was faulty
celestar
parents:
2952
diff
changeset
|
163 |
ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE); |
0 | 164 |
} |
165 |
} |