Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / genworld_gui.cpp
blob5a7c7fcf049b360b478eafe585a95cfbb26e8b10
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file genworld_gui.cpp GUI to configure and show progress during map generation. */
12 #include "stdafx.h"
13 #include "heightmap.h"
14 #include "debug.h"
15 #include "genworld.h"
16 #include "network/network.h"
17 #include "strings_func.h"
18 #include "window_func.h"
19 #include "date_func.h"
20 #include "sound_func.h"
21 #include "fios.h"
22 #include "string_func.h"
23 #include "widgets/dropdown_type.h"
24 #include "widgets/dropdown_func.h"
25 #include "querystring_gui.h"
26 #include "town.h"
27 #include "core/geometry_func.hpp"
28 #include "core/random_func.hpp"
29 #include "saveload/saveload.h"
30 #include "progress.h"
31 #include "error.h"
33 #include "widgets/genworld_widget.h"
35 #include "safeguards.h"
38 extern void MakeNewgameSettingsLive();
40 /** Enum for the modes we can generate in. */
41 enum GenerateLandscapeWindowMode {
42 GLWM_GENERATE, ///< Generate new game.
43 GLWM_HEIGHTMAP, ///< Load from heightmap.
44 GLWM_SCENARIO, ///< Generate flat land.
47 /**
48 * Changes landscape type and sets genworld window dirty
49 * @param landscape new landscape type
51 void SetNewLandscapeType(byte landscape)
53 _settings_newgame.game_creation.landscape = landscape;
54 InvalidateWindowClassesData(WC_SELECT_GAME);
55 InvalidateWindowClassesData(WC_GENERATE_LANDSCAPE);
58 /** Widgets of GenerateLandscapeWindow when generating world */
59 static const NWidgetPart _nested_generate_landscape_widgets[] = {
60 NWidget(NWID_HORIZONTAL),
61 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
62 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MAPGEN_WORLD_GENERATION_CAPTION, STR_NULL),
63 EndContainer(),
64 NWidget(WWT_PANEL, COLOUR_BROWN),
65 NWidget(NWID_SPACER), SetMinimalSize(0, 10),
66 /* Landscape selection. */
67 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
68 NWidget(NWID_SPACER), SetFill(1, 0),
69 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TEMPERATE), SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
70 NWidget(NWID_SPACER), SetFill(1, 0),
71 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_ARCTIC), SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
72 NWidget(NWID_SPACER), SetFill(1, 0),
73 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TROPICAL), SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
74 NWidget(NWID_SPACER), SetFill(1, 0),
75 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TOYLAND), SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
76 NWidget(NWID_SPACER), SetFill(1, 0),
77 EndContainer(),
78 NWidget(NWID_SPACER), SetMinimalSize(0, 11),
79 NWidget(NWID_HORIZONTAL), SetPIP(10, 5, 10),
80 NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0),
81 /* Left column with labels. */
82 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
83 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAPSIZE, STR_MAPGEN_MAPSIZE_TOOLTIP), SetFill(1, 1),
84 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_LAND_GENERATOR, STR_NULL), SetFill(1, 1),
85 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_TOWNS, STR_NULL), SetFill(1, 1),
86 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_INDUSTRIES, STR_NULL), SetFill(1, 1),
87 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_TERRAIN_TYPE, STR_NULL), SetFill(1, 1),
88 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_VARIETY, STR_NULL), SetFill(1, 1),
89 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_QUANTITY_OF_SEA_LAKES, STR_NULL), SetFill(1, 1),
90 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_TREE_PLACER, STR_NULL), SetFill(1, 1),
91 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BORDER_TYPE, STR_NULL), SetFill(1, 1),
92 EndContainer(),
93 /* Widgets at the right of the labels. */
94 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
95 /* Mapsize X * Y. */
96 NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
97 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_X_PULLDOWN), SetDataTip(STR_JUST_INT, STR_MAPGEN_MAPSIZE_TOOLTIP), SetFill(1, 0),
98 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BY, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(1, 1),
99 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_Y_PULLDOWN), SetDataTip(STR_JUST_INT, STR_MAPGEN_MAPSIZE_TOOLTIP), SetFill(1, 0),
100 EndContainer(),
101 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_LANDSCAPE_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
102 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TOWN_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
103 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_INDUSTRY_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
104 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TERRAIN_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
105 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_VARIETY_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
106 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_WATER_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
107 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TREE_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
108 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_BORDERS_RANDOM), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
109 EndContainer(),
110 EndContainer(),
111 NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
112 NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0),
113 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
114 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAX_HEIGHTLEVEL, STR_NULL), SetFill(1, 1),
115 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SNOW_LINE_HEIGHT, STR_NULL), SetFill(1, 1),
116 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_TREE_LINE_HEIGHT, STR_NULL), SetFill(1, 1),
117 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DESERT_AMOUNT, STR_NULL), SetFill(1, 1),
118 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DATE, STR_NULL), SetFill(1, 1),
119 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SMOOTHNESS, STR_NULL), SetFill(1, 1),
120 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_QUANTITY_OF_RIVERS, STR_NULL), SetFill(1, 1),
121 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MIN_RIVER_LENGTH, STR_NULL), SetFill(1, 1),
122 EndContainer(),
123 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
124 /* Max. heightlevel. */
125 NWidget(NWID_HORIZONTAL),
126 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_MAX_HEIGHTLEVEL_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_MAX_HEIGHTLEVEL_DOWN), SetFill(0, 1),
127 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_MAX_HEIGHTLEVEL_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
128 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_MAX_HEIGHTLEVEL_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_MAX_HEIGHTLEVEL_UP), SetFill(0, 1),
129 EndContainer(),
130 /* Snow line. */
131 NWidget(NWID_HORIZONTAL),
132 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_LEVEL_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_SNOW_LINE_DOWN), SetFill(0, 1),
133 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_SNOW_LEVEL_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
134 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_LEVEL_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_SNOW_LINE_UP), SetFill(0, 1),
135 EndContainer(),
136 /* Tree line. */
137 NWidget(NWID_HORIZONTAL),
138 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_TREE_LEVEL_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_TREE_LINE_DOWN), SetFill(0, 1),
139 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_TREE_LEVEL_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
140 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_TREE_LEVEL_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_TREE_LINE_UP), SetFill(0, 1),
141 EndContainer(),
142 /* Desert line. */
143 NWidget(NWID_HORIZONTAL),
144 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_DESERT_AMOUNT_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_DESERT_AMOUNT_DOWN), SetFill(0, 1),
145 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_DESERT_AMOUNT_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
146 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_DESERT_AMOUNT_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_DESERT_AMOUNT_UP), SetFill(0, 1),
147 EndContainer(),
148 /* Starting date. */
149 NWidget(NWID_HORIZONTAL),
150 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_START_DATE_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_SCENEDIT_TOOLBAR_TOOLTIP_MOVE_THE_STARTING_DATE_BACKWARD), SetFill(0, 1),
151 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_GL_START_DATE_TEXT), SetDataTip(STR_BLACK_DATE_LONG, STR_NULL), SetFill(1, 0),
152 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_START_DATE_UP), SetDataTip(SPR_ARROW_UP, STR_SCENEDIT_TOOLBAR_TOOLTIP_MOVE_THE_STARTING_DATE_FORWARD), SetFill(0, 1),
153 EndContainer(),
154 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_SMOOTHNESS_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
155 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_RIVER_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
156 /* Minimum river length. */
157 NWidget(NWID_HORIZONTAL),
158 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_MIN_RIVER_LENGTH_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_MIN_RIVER_LENGTH_DOWN), SetFill(0, 1),
159 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_MIN_RIVER_LENGTH_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
160 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_MIN_RIVER_LENGTH_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_MIN_RIVER_LENGTH_UP), SetFill(0, 1),
161 EndContainer(),
162 EndContainer(),
163 EndContainer(),
164 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_GL_GENERATE_BUTTON), SetMinimalSize(84, 0), SetDataTip(STR_MAPGEN_GENERATE, STR_NULL), SetFill(1, 1),
165 EndContainer(),
166 EndContainer(),
167 NWidget(NWID_SPACER), SetMinimalSize(0, 4),
168 /* Map borders buttons for each edge. */
169 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
170 NWidget(NWID_HORIZONTAL), SetPIP(0, 0, 3),
171 NWidget(NWID_SPACER), SetFill(1, 1),
172 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NORTHWEST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
173 EndContainer(),
174 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_WATER_NW), SetDataTip(STR_JUST_STRING, STR_MAPGEN_NORTHWEST), SetFill(1, 1),
175 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_WATER_NE), SetDataTip(STR_JUST_STRING, STR_MAPGEN_NORTHEAST), SetFill(1, 1),
176 NWidget(NWID_HORIZONTAL), SetPIP(3, 0, 0),
177 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NORTHEAST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
178 NWidget(NWID_SPACER), SetFill(1, 1),
179 EndContainer(),
180 EndContainer(),
181 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
182 NWidget(NWID_HORIZONTAL), SetPIP(0, 0, 3),
183 NWidget(NWID_SPACER), SetFill(1, 1),
184 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SOUTHWEST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
185 EndContainer(),
186 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_WATER_SW), SetDataTip(STR_JUST_STRING, STR_MAPGEN_SOUTHWEST), SetFill(1, 1),
187 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_WATER_SE), SetDataTip(STR_JUST_STRING, STR_MAPGEN_SOUTHEAST), SetFill(1, 1),
188 NWidget(NWID_HORIZONTAL), SetPIP(3, 0, 0),
189 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SOUTHEAST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
190 NWidget(NWID_SPACER), SetFill(1, 1),
191 EndContainer(),
192 EndContainer(),
193 NWidget(NWID_SPACER), SetMinimalSize(0, 9), SetFill(1, 1),
194 EndContainer(),
197 /** Widgets of GenerateLandscapeWindow when loading heightmap */
198 static const NWidgetPart _nested_heightmap_load_widgets[] = {
199 /* Window header. */
200 NWidget(NWID_HORIZONTAL),
201 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
202 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MAPGEN_WORLD_GENERATION_CAPTION, STR_NULL),
203 EndContainer(),
204 NWidget(WWT_PANEL, COLOUR_BROWN),
205 NWidget(NWID_SPACER), SetMinimalSize(0, 10),
206 /* Landscape selection. */
207 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
208 NWidget(NWID_SPACER), SetFill(1, 0),
209 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TEMPERATE), SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
210 NWidget(NWID_SPACER), SetFill(1, 0),
211 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_ARCTIC), SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
212 NWidget(NWID_SPACER), SetFill(1, 0),
213 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TROPICAL), SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
214 NWidget(NWID_SPACER), SetFill(1, 0),
215 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TOYLAND), SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
216 NWidget(NWID_SPACER), SetFill(1, 0),
217 EndContainer(),
218 NWidget(NWID_SPACER), SetMinimalSize(0, 11), SetFill(0, 1),
219 NWidget(NWID_HORIZONTAL), SetPIP(10, 3, 10),
220 /* Labels at the left side. */
221 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
222 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_NAME, STR_NULL), SetFill(1, 1),
223 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAPSIZE, STR_NULL), SetFill(1, 1),
224 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_ROTATION, STR_NULL), SetFill(1, 1),
225 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_TOWNS, STR_NULL), SetFill(1, 1),
226 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_INDUSTRIES, STR_NULL), SetFill(1, 1),
227 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_QUANTITY_OF_RIVERS, STR_NULL), SetFill(1, 1),
228 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MIN_RIVER_LENGTH, STR_NULL), SetFill(1, 1),
229 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_TREE_PLACER, STR_NULL), SetFill(1, 1),
230 EndContainer(),
231 /* Widgets at the right of the labels. */
232 NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
233 NWidget(WWT_EMPTY, COLOUR_ORANGE, WID_GL_HEIGHTMAP_NAME_TEXT), SetFill(1, 0),
234 NWidget(NWID_HORIZONTAL), SetPIP(0, 5, 0),
235 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
236 /* Mapsize X * Y. */
237 NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
238 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_X_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetFill(1, 0),
239 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BY, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(1, 1),
240 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_Y_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetFill(1, 0),
241 EndContainer(),
242 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_HEIGHTMAP_ROTATION_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
243 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TOWN_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
244 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_INDUSTRY_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
245 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_RIVER_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
246 NWidget(NWID_HORIZONTAL),
247 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_MIN_RIVER_LENGTH_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_MIN_RIVER_LENGTH_DOWN), SetFill(0, 1),
248 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_MIN_RIVER_LENGTH_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
249 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_MIN_RIVER_LENGTH_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_MIN_RIVER_LENGTH_UP), SetFill(0, 1),
250 EndContainer(),
251 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TREE_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
252 EndContainer(),
253 NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
254 NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0),
255 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
256 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_SIZE_LABEL, STR_NULL), SetFill(1, 1),
257 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SNOW_LINE_HEIGHT, STR_NULL), SetFill(1, 1),
258 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_TREE_LINE_HEIGHT, STR_NULL), SetFill(1, 1),
259 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DESERT_AMOUNT, STR_NULL), SetFill(1, 1),
260 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAX_HEIGHTLEVEL, STR_NULL), SetFill(1, 1),
261 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DATE, STR_NULL), SetFill(1, 1),
262 EndContainer(),
263 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
264 NWidget(WWT_TEXT, COLOUR_ORANGE, WID_GL_HEIGHTMAP_SIZE_TEXT), SetDataTip(STR_MAPGEN_HEIGHTMAP_SIZE, STR_NULL), SetFill(1, 0),
265 NWidget(NWID_HORIZONTAL),
266 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_LEVEL_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_SNOW_LINE_DOWN), SetFill(0, 1),
267 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_SNOW_LEVEL_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
268 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_LEVEL_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_SNOW_LINE_UP), SetFill(0, 1),
269 EndContainer(),
270 NWidget(NWID_HORIZONTAL),
271 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_TREE_LEVEL_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_TREE_LINE_DOWN), SetFill(0, 1),
272 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_TREE_LEVEL_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
273 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_TREE_LEVEL_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_TREE_LINE_UP), SetFill(0, 1),
274 EndContainer(),
275 NWidget(NWID_HORIZONTAL),
276 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_DESERT_AMOUNT_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_DESERT_AMOUNT_DOWN), SetFill(0, 1),
277 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_DESERT_AMOUNT_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
278 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_DESERT_AMOUNT_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_DESERT_AMOUNT_UP), SetFill(0, 1),
279 EndContainer(),
280 NWidget(NWID_HORIZONTAL),
281 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_MAX_HEIGHTLEVEL_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_MAX_HEIGHTLEVEL_DOWN), SetFill(0, 1),
282 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_MAX_HEIGHTLEVEL_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
283 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_MAX_HEIGHTLEVEL_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_MAX_HEIGHTLEVEL_UP), SetFill(0, 1),
284 EndContainer(),
285 NWidget(NWID_HORIZONTAL),
286 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_START_DATE_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_SCENEDIT_TOOLBAR_TOOLTIP_MOVE_THE_STARTING_DATE_BACKWARD), SetFill(0, 1),
287 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_GL_START_DATE_TEXT), SetDataTip(STR_BLACK_DATE_LONG, STR_NULL), SetFill(1, 0),
288 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_START_DATE_UP), SetDataTip(SPR_ARROW_UP, STR_SCENEDIT_TOOLBAR_TOOLTIP_MOVE_THE_STARTING_DATE_FORWARD), SetFill(0, 1),
289 EndContainer(),
290 EndContainer(),
291 EndContainer(),
292 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_GL_GENERATE_BUTTON), SetMinimalSize(84, 0), SetDataTip(STR_MAPGEN_GENERATE, STR_NULL), SetFill(1, 1),
293 EndContainer(),
294 EndContainer(),
295 EndContainer(),
296 EndContainer(),
297 NWidget(NWID_SPACER), SetMinimalSize(0, 9), SetFill(1, 1),
298 EndContainer(),
301 static void StartGeneratingLandscape(GenerateLandscapeWindowMode mode)
303 DeleteAllNonVitalWindows();
304 ClearErrorMessages();
306 /* Copy all XXX_newgame to XXX when coming from outside the editor */
307 MakeNewgameSettingsLive();
308 ResetGRFConfig(true);
310 if (_settings_client.sound.confirm) SndPlayFx(SND_15_BEEP);
311 switch (mode) {
312 case GLWM_GENERATE: _switch_mode = (_game_mode == GM_EDITOR) ? SM_GENRANDLAND : SM_NEWGAME; break;
313 case GLWM_HEIGHTMAP: _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_HEIGHTMAP : SM_START_HEIGHTMAP; break;
314 case GLWM_SCENARIO: _switch_mode = SM_EDITOR; break;
315 default: NOT_REACHED();
319 static void LandscapeGenerationCallback(Window *w, bool confirmed)
321 if (confirmed) StartGeneratingLandscape((GenerateLandscapeWindowMode)w->window_number);
324 static DropDownList *BuildMapsizeDropDown()
326 DropDownList *list = new DropDownList();
328 for (uint i = MIN_MAP_SIZE_BITS; i <= MAX_MAP_SIZE_BITS; i++) {
329 DropDownListParamStringItem *item = new DropDownListParamStringItem(STR_JUST_INT, i, false);
330 item->SetParam(0, 1 << i);
331 *list->Append() = item;
334 return list;
337 static const StringID _elevations[] = {STR_TERRAIN_TYPE_VERY_FLAT, STR_TERRAIN_TYPE_FLAT, STR_TERRAIN_TYPE_HILLY, STR_TERRAIN_TYPE_MOUNTAINOUS, STR_TERRAIN_TYPE_ALPINIST, INVALID_STRING_ID};
338 static const StringID _sea_lakes[] = {STR_SEA_LEVEL_VERY_LOW, STR_SEA_LEVEL_LOW, STR_SEA_LEVEL_MEDIUM, STR_SEA_LEVEL_HIGH, STR_SEA_LEVEL_CUSTOM, INVALID_STRING_ID};
339 static const StringID _rivers[] = {STR_RIVERS_NONE, STR_RIVERS_FEW, STR_RIVERS_MODERATE, STR_RIVERS_LOT, INVALID_STRING_ID};
340 static const StringID _smoothness[] = {STR_CONFIG_SETTING_ROUGHNESS_OF_TERRAIN_VERY_SMOOTH, STR_CONFIG_SETTING_ROUGHNESS_OF_TERRAIN_SMOOTH, STR_CONFIG_SETTING_ROUGHNESS_OF_TERRAIN_ROUGH, STR_CONFIG_SETTING_ROUGHNESS_OF_TERRAIN_VERY_ROUGH, INVALID_STRING_ID};
341 static const StringID _tree_placer[] = {STR_CONFIG_SETTING_TREE_PLACER_NONE, STR_CONFIG_SETTING_TREE_PLACER_ORIGINAL, STR_CONFIG_SETTING_TREE_PLACER_IMPROVED, INVALID_STRING_ID};
342 static const StringID _rotation[] = {STR_CONFIG_SETTING_HEIGHTMAP_ROTATION_COUNTER_CLOCKWISE, STR_CONFIG_SETTING_HEIGHTMAP_ROTATION_CLOCKWISE, INVALID_STRING_ID};
343 static const StringID _landscape[] = {STR_CONFIG_SETTING_LAND_GENERATOR_ORIGINAL, STR_CONFIG_SETTING_LAND_GENERATOR_TERRA_GENESIS, INVALID_STRING_ID};
344 static const StringID _num_towns[] = {STR_NUM_VERY_LOW, STR_NUM_LOW, STR_NUM_NORMAL, STR_NUM_HIGH, STR_NUM_CUSTOM, INVALID_STRING_ID};
345 static const StringID _num_inds[] = {STR_FUNDING_ONLY, STR_MINIMAL, STR_NUM_VERY_LOW, STR_NUM_LOW, STR_NUM_NORMAL, STR_NUM_HIGH, INVALID_STRING_ID};
346 static const StringID _variety[] = {STR_VARIETY_NONE, STR_VARIETY_VERY_LOW, STR_VARIETY_LOW, STR_VARIETY_MEDIUM, STR_VARIETY_HIGH, STR_VARIETY_VERY_HIGH, INVALID_STRING_ID};
348 assert_compile(lengthof(_num_inds) == ID_END + 1);
350 struct GenerateLandscapeWindow : public Window {
351 uint widget_id;
352 uint x;
353 uint y;
354 char name[64];
355 GenerateLandscapeWindowMode mode;
357 GenerateLandscapeWindow(WindowDesc *desc, WindowNumber number = 0) : Window(desc)
359 this->InitNested(number);
361 this->LowerWidget(_settings_newgame.game_creation.landscape + WID_GL_TEMPERATE);
363 this->mode = (GenerateLandscapeWindowMode)this->window_number;
365 /* Disable town, industry and trees in SE */
366 this->SetWidgetDisabledState(WID_GL_TOWN_PULLDOWN, _game_mode == GM_EDITOR);
367 this->SetWidgetDisabledState(WID_GL_INDUSTRY_PULLDOWN, _game_mode == GM_EDITOR);
368 this->SetWidgetDisabledState(WID_GL_TREE_PULLDOWN, _game_mode == GM_EDITOR);
370 this->OnInvalidateData();
374 virtual void SetStringParameters(int widget) const
376 switch (widget) {
377 case WID_GL_START_DATE_TEXT: SetDParam(0, ConvertYMDToDate(_settings_newgame.game_creation.starting_year, 0, 1)); break;
378 case WID_GL_MAPSIZE_X_PULLDOWN: SetDParam(0, 1 << _settings_newgame.game_creation.map_x); break;
379 case WID_GL_MAPSIZE_Y_PULLDOWN: SetDParam(0, 1 << _settings_newgame.game_creation.map_y); break;
380 case WID_GL_MAX_HEIGHTLEVEL_TEXT: SetDParam(0, _settings_newgame.construction.max_heightlevel); break;
381 case WID_GL_SNOW_LEVEL_TEXT: SetDParam(0, _settings_newgame.game_creation.snow_line_height); break;
382 case WID_GL_MIN_RIVER_LENGTH_TEXT:SetDParam(0, _settings_newgame.game_creation.min_river_length); break;
383 case WID_GL_TREE_LEVEL_TEXT: SetDParam(0, _settings_newgame.game_creation.tree_line_height); break;
384 case WID_GL_DESERT_AMOUNT_TEXT: SetDParam(0, _settings_newgame.game_creation.desert_amount); break;
386 case WID_GL_TOWN_PULLDOWN:
387 if (_game_mode == GM_EDITOR) {
388 SetDParam(0, STR_CONFIG_SETTING_OFF);
389 } else if (_settings_newgame.difficulty.number_towns == CUSTOM_TOWN_NUMBER_DIFFICULTY) {
390 SetDParam(0, STR_NUM_CUSTOM_NUMBER);
391 SetDParam(1, _settings_newgame.game_creation.custom_town_number);
392 } else {
393 SetDParam(0, _num_towns[_settings_newgame.difficulty.number_towns]);
395 break;
397 case WID_GL_INDUSTRY_PULLDOWN: SetDParam(0, _game_mode == GM_EDITOR ? STR_CONFIG_SETTING_OFF : _num_inds[_settings_newgame.difficulty.industry_density]); break;
398 case WID_GL_LANDSCAPE_PULLDOWN: SetDParam(0, _landscape[_settings_newgame.game_creation.land_generator]); break;
399 case WID_GL_TREE_PULLDOWN: SetDParam(0, _tree_placer[_settings_newgame.game_creation.tree_placer]); break;
400 case WID_GL_TERRAIN_PULLDOWN: SetDParam(0, _elevations[_settings_newgame.difficulty.terrain_type]); break;
402 case WID_GL_WATER_PULLDOWN:
403 if (_settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
404 SetDParam(0, STR_SEA_LEVEL_CUSTOM_PERCENTAGE);
405 SetDParam(1, _settings_newgame.game_creation.custom_sea_level);
406 } else {
407 SetDParam(0, _sea_lakes[_settings_newgame.difficulty.quantity_sea_lakes]);
409 break;
411 case WID_GL_RIVER_PULLDOWN: SetDParam(0, _rivers[_settings_newgame.game_creation.amount_of_rivers]); break;
412 case WID_GL_SMOOTHNESS_PULLDOWN: SetDParam(0, _smoothness[_settings_newgame.game_creation.tgen_smoothness]); break;
413 case WID_GL_VARIETY_PULLDOWN: SetDParam(0, _variety[_settings_newgame.game_creation.variety]); break;
414 case WID_GL_BORDERS_RANDOM: SetDParam(0, (_settings_newgame.game_creation.water_borders == BORDERS_RANDOM) ? STR_MAPGEN_BORDER_RANDOMIZE : STR_MAPGEN_BORDER_MANUAL); break;
415 case WID_GL_WATER_NE: SetDParam(0, (_settings_newgame.game_creation.water_borders == BORDERS_RANDOM) ? STR_MAPGEN_BORDER_RANDOM : HasBit(_settings_newgame.game_creation.water_borders, BORDER_NE) ? STR_MAPGEN_BORDER_WATER : STR_MAPGEN_BORDER_FREEFORM); break;
416 case WID_GL_WATER_NW: SetDParam(0, (_settings_newgame.game_creation.water_borders == BORDERS_RANDOM) ? STR_MAPGEN_BORDER_RANDOM : HasBit(_settings_newgame.game_creation.water_borders, BORDER_NW) ? STR_MAPGEN_BORDER_WATER : STR_MAPGEN_BORDER_FREEFORM); break;
417 case WID_GL_WATER_SE: SetDParam(0, (_settings_newgame.game_creation.water_borders == BORDERS_RANDOM) ? STR_MAPGEN_BORDER_RANDOM : HasBit(_settings_newgame.game_creation.water_borders, BORDER_SE) ? STR_MAPGEN_BORDER_WATER : STR_MAPGEN_BORDER_FREEFORM); break;
418 case WID_GL_WATER_SW: SetDParam(0, (_settings_newgame.game_creation.water_borders == BORDERS_RANDOM) ? STR_MAPGEN_BORDER_RANDOM : HasBit(_settings_newgame.game_creation.water_borders, BORDER_SW) ? STR_MAPGEN_BORDER_WATER : STR_MAPGEN_BORDER_FREEFORM); break;
419 case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: SetDParam(0, _rotation[_settings_newgame.game_creation.heightmap_rotation]); break;
421 case WID_GL_HEIGHTMAP_SIZE_TEXT:
422 if (_settings_newgame.game_creation.heightmap_rotation == HM_CLOCKWISE) {
423 SetDParam(0, this->y);
424 SetDParam(1, this->x);
425 } else {
426 SetDParam(0, this->x);
427 SetDParam(1, this->y);
429 break;
434 * Some data on this window has become invalid.
435 * @param data Information about the changed data.
436 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
438 virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
440 if (!gui_scope) return;
441 /* Update the climate buttons */
442 this->SetWidgetLoweredState(WID_GL_TEMPERATE, _settings_newgame.game_creation.landscape == LT_TEMPERATE);
443 this->SetWidgetLoweredState(WID_GL_ARCTIC, _settings_newgame.game_creation.landscape == LT_ARCTIC);
444 this->SetWidgetLoweredState(WID_GL_TROPICAL, _settings_newgame.game_creation.landscape == LT_TROPIC);
445 this->SetWidgetLoweredState(WID_GL_TOYLAND, _settings_newgame.game_creation.landscape == LT_TOYLAND);
447 /* You can't select smoothness / non-water borders if not terragenesis */
448 if (mode == GLWM_GENERATE) {
449 this->SetWidgetDisabledState(WID_GL_SMOOTHNESS_PULLDOWN, _settings_newgame.game_creation.land_generator == LG_ORIGINAL);
450 this->SetWidgetDisabledState(WID_GL_VARIETY_PULLDOWN, _settings_newgame.game_creation.land_generator == LG_ORIGINAL);
451 this->SetWidgetDisabledState(WID_GL_BORDERS_RANDOM, _settings_newgame.game_creation.land_generator == LG_ORIGINAL || !_settings_newgame.construction.freeform_edges);
452 this->SetWidgetsDisabledState(_settings_newgame.game_creation.land_generator == LG_ORIGINAL || !_settings_newgame.construction.freeform_edges || _settings_newgame.game_creation.water_borders == BORDERS_RANDOM,
453 WID_GL_WATER_NW, WID_GL_WATER_NE, WID_GL_WATER_SE, WID_GL_WATER_SW, WIDGET_LIST_END);
455 this->SetWidgetLoweredState(WID_GL_BORDERS_RANDOM, _settings_newgame.game_creation.water_borders == BORDERS_RANDOM);
457 this->SetWidgetLoweredState(WID_GL_WATER_NW, HasBit(_settings_newgame.game_creation.water_borders, BORDER_NW));
458 this->SetWidgetLoweredState(WID_GL_WATER_NE, HasBit(_settings_newgame.game_creation.water_borders, BORDER_NE));
459 this->SetWidgetLoweredState(WID_GL_WATER_SE, HasBit(_settings_newgame.game_creation.water_borders, BORDER_SE));
460 this->SetWidgetLoweredState(WID_GL_WATER_SW, HasBit(_settings_newgame.game_creation.water_borders, BORDER_SW));
462 this->SetWidgetsDisabledState(_settings_newgame.game_creation.land_generator == LG_ORIGINAL && (_settings_newgame.game_creation.landscape == LT_ARCTIC || _settings_newgame.game_creation.landscape == LT_TROPIC),
463 WID_GL_TERRAIN_PULLDOWN, WID_GL_WATER_PULLDOWN, WIDGET_LIST_END);
466 /* Disable desertline if not tropic. */
467 this->SetWidgetDisabledState(WID_GL_DESERT_AMOUNT_TEXT, _settings_newgame.game_creation.landscape != LT_TROPIC);
469 /* Update availability of decreasing / increasing start date, snow level and desert level. */
470 this->SetWidgetDisabledState(WID_GL_MAX_HEIGHTLEVEL_DOWN, _settings_newgame.construction.max_heightlevel <= MIN_MAX_HEIGHTLEVEL);
471 this->SetWidgetDisabledState(WID_GL_MAX_HEIGHTLEVEL_UP, _settings_newgame.construction.max_heightlevel >= MAX_MAX_HEIGHTLEVEL);
472 this->SetWidgetDisabledState(WID_GL_SNOW_LEVEL_DOWN, _settings_newgame.game_creation.snow_line_height <= MIN_SNOWLINE_HEIGHT);
473 this->SetWidgetDisabledState(WID_GL_SNOW_LEVEL_UP, _settings_newgame.game_creation.snow_line_height >= MAX_SNOWLINE_HEIGHT);
474 this->SetWidgetDisabledState(WID_GL_MIN_RIVER_LENGTH_DOWN, _settings_newgame.game_creation.min_river_length <= MIN_MIN_RIVER_LENGTH);
475 this->SetWidgetDisabledState(WID_GL_MIN_RIVER_LENGTH_UP, _settings_newgame.game_creation.min_river_length >= MAX_MIN_RIVER_LENGTH);
476 this->SetWidgetDisabledState(WID_GL_TREE_LEVEL_DOWN, _settings_newgame.game_creation.tree_line_height <= MIN_TREELINE_HEIGHT);
477 this->SetWidgetDisabledState(WID_GL_TREE_LEVEL_UP, _settings_newgame.game_creation.tree_line_height >= MAX_TREELINE_HEIGHT);
478 this->SetWidgetDisabledState(WID_GL_START_DATE_DOWN, _settings_newgame.game_creation.starting_year <= MIN_YEAR);
479 this->SetWidgetDisabledState(WID_GL_START_DATE_UP, _settings_newgame.game_creation.starting_year >= MAX_YEAR);
480 this->SetWidgetDisabledState(WID_GL_DESERT_AMOUNT_DOWN, _settings_newgame.game_creation.desert_amount <= MIN_DESERT_AMOUNT || _settings_newgame.game_creation.landscape != LT_TROPIC);
481 this->SetWidgetDisabledState(WID_GL_DESERT_AMOUNT_UP, _settings_newgame.game_creation.desert_amount >= MAX_DESERT_AMOUNT || _settings_newgame.game_creation.landscape != LT_TROPIC);
483 /* Do not allow a custom sea level with the original land generator. */
484 if (_settings_newgame.game_creation.land_generator == LG_ORIGINAL &&
485 _settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
486 _settings_newgame.difficulty.quantity_sea_lakes = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE;
491 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
493 const StringID *strs = NULL;
494 switch (widget) {
495 case WID_GL_MAX_HEIGHTLEVEL_TEXT:
496 SetDParam(0, MAX_TILE_HEIGHT);
497 *size = GetStringBoundingBox(STR_JUST_INT);
498 break;
500 case WID_GL_START_DATE_TEXT:
501 SetDParam(0, ConvertYMDToDate(MAX_YEAR, 0, 1));
502 *size = maxdim(*size, GetStringBoundingBox(STR_BLACK_DATE_LONG));
503 break;
505 case WID_GL_MAPSIZE_X_PULLDOWN:
506 case WID_GL_MAPSIZE_Y_PULLDOWN:
507 SetDParamMaxValue(0, MAX_MAP_SIZE);
508 *size = maxdim(*size, GetStringBoundingBox(STR_JUST_INT));
509 break;
511 case WID_GL_SNOW_LEVEL_TEXT:
512 SetDParamMaxValue(0, MAX_TILE_HEIGHT);
513 *size = maxdim(*size, GetStringBoundingBox(STR_JUST_INT));
514 break;
516 case WID_GL_TREE_LEVEL_TEXT:
517 SetDParamMaxValue(0, MAX_TILE_HEIGHT);
518 *size = maxdim(*size, GetStringBoundingBox(STR_JUST_INT));
519 break;
521 case WID_GL_DESERT_AMOUNT_TEXT:
522 SetDParam(0, MAX_DESERT_AMOUNT);
523 *size = GetStringBoundingBox(STR_JUST_INT);
524 break;
526 case WID_GL_MIN_RIVER_LENGTH_TEXT:
527 SetDParam(0, MAX_MIN_RIVER_LENGTH);
528 *size = GetStringBoundingBox(STR_JUST_INT);
529 break;
531 case WID_GL_HEIGHTMAP_SIZE_TEXT:
532 SetDParam(0, this->x);
533 SetDParam(1, this->y);
534 *size = maxdim(*size, GetStringBoundingBox(STR_MAPGEN_HEIGHTMAP_SIZE));
535 break;
537 case WID_GL_TOWN_PULLDOWN:
538 strs = _num_towns;
539 SetDParamMaxValue(0, CUSTOM_TOWN_MAX_NUMBER);
540 *size = maxdim(*size, GetStringBoundingBox(STR_NUM_CUSTOM_NUMBER));
541 break;
543 case WID_GL_INDUSTRY_PULLDOWN: strs = _num_inds; break;
544 case WID_GL_LANDSCAPE_PULLDOWN: strs = _landscape; break;
545 case WID_GL_TREE_PULLDOWN: strs = _tree_placer; break;
546 case WID_GL_TERRAIN_PULLDOWN: strs = _elevations; break;
547 case WID_GL_WATER_PULLDOWN:
548 strs = _sea_lakes;
549 SetDParamMaxValue(0, CUSTOM_SEA_LEVEL_MAX_PERCENTAGE);
550 *size = maxdim(*size, GetStringBoundingBox(STR_SEA_LEVEL_CUSTOM_PERCENTAGE));
551 break;
553 case WID_GL_RIVER_PULLDOWN: strs = _rivers; break;
554 case WID_GL_SMOOTHNESS_PULLDOWN: strs = _smoothness; break;
555 case WID_GL_VARIETY_PULLDOWN: strs = _variety; break;
556 case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: strs = _rotation; break;
557 case WID_GL_BORDERS_RANDOM:
558 *size = maxdim(GetStringBoundingBox(STR_MAPGEN_BORDER_RANDOMIZE), GetStringBoundingBox(STR_MAPGEN_BORDER_MANUAL));
559 break;
561 case WID_GL_WATER_NE:
562 case WID_GL_WATER_NW:
563 case WID_GL_WATER_SE:
564 case WID_GL_WATER_SW:
565 *size = maxdim(GetStringBoundingBox(STR_MAPGEN_BORDER_RANDOM), maxdim(GetStringBoundingBox(STR_MAPGEN_BORDER_WATER), GetStringBoundingBox(STR_MAPGEN_BORDER_FREEFORM)));
566 break;
568 case WID_GL_HEIGHTMAP_NAME_TEXT:
569 size->width = 0;
570 break;
572 default:
573 return;
575 if (strs != NULL) {
576 while (*strs != INVALID_STRING_ID) {
577 *size = maxdim(*size, GetStringBoundingBox(*strs++));
580 size->width += padding.width;
581 size->height = max(size->height, (uint)(FONT_HEIGHT_NORMAL + WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM));
584 virtual void DrawWidget(const Rect &r, int widget) const
586 switch (widget) {
587 case WID_GL_HEIGHTMAP_NAME_TEXT: {
588 DrawString(r.left, r.right, r.top, this->name, TC_ORANGE);
589 break;
594 virtual void OnClick(Point pt, int widget, int click_count)
596 switch (widget) {
597 case WID_GL_TEMPERATE:
598 case WID_GL_ARCTIC:
599 case WID_GL_TROPICAL:
600 case WID_GL_TOYLAND:
601 SetNewLandscapeType(widget - WID_GL_TEMPERATE);
602 break;
604 case WID_GL_MAPSIZE_X_PULLDOWN: // Mapsize X
605 ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_x, WID_GL_MAPSIZE_X_PULLDOWN);
606 break;
608 case WID_GL_MAPSIZE_Y_PULLDOWN: // Mapsize Y
609 ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_y, WID_GL_MAPSIZE_Y_PULLDOWN);
610 break;
612 case WID_GL_TOWN_PULLDOWN: // Number of towns
613 ShowDropDownMenu(this, _num_towns, _settings_newgame.difficulty.number_towns, WID_GL_TOWN_PULLDOWN, 0, 0);
614 break;
616 case WID_GL_INDUSTRY_PULLDOWN: // Number of industries
617 ShowDropDownMenu(this, _num_inds, _settings_newgame.difficulty.industry_density, WID_GL_INDUSTRY_PULLDOWN, 0, 0);
618 break;
620 case WID_GL_GENERATE_BUTTON: { // Generate
621 /* Get rotated map size. */
622 uint map_x;
623 uint map_y;
624 if (_settings_newgame.game_creation.heightmap_rotation == HM_CLOCKWISE) {
625 map_x = this->y;
626 map_y = this->x;
627 } else {
628 map_x = this->x;
629 map_y = this->y;
631 if (mode == GLWM_HEIGHTMAP &&
632 (map_x * 2 < (1U << _settings_newgame.game_creation.map_x) ||
633 map_x / 2 > (1U << _settings_newgame.game_creation.map_x) ||
634 map_y * 2 < (1U << _settings_newgame.game_creation.map_y) ||
635 map_y / 2 > (1U << _settings_newgame.game_creation.map_y))) {
636 ShowQuery(
637 STR_WARNING_HEIGHTMAP_SCALE_CAPTION,
638 STR_WARNING_HEIGHTMAP_SCALE_MESSAGE,
639 this,
640 LandscapeGenerationCallback);
641 } else {
642 StartGeneratingLandscape(mode);
644 break;
647 case WID_GL_MAX_HEIGHTLEVEL_DOWN:
648 case WID_GL_MAX_HEIGHTLEVEL_UP: // Height level buttons
649 /* Don't allow too fast scrolling */
650 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
651 this->HandleButtonClick(widget);
653 _settings_newgame.construction.max_heightlevel = Clamp(_settings_newgame.construction.max_heightlevel + widget - WID_GL_MAX_HEIGHTLEVEL_TEXT, MIN_MAX_HEIGHTLEVEL, MAX_MAX_HEIGHTLEVEL);
654 this->InvalidateData();
656 _left_button_clicked = false;
657 break;
659 case WID_GL_MAX_HEIGHTLEVEL_TEXT: // Height level text
660 this->widget_id = WID_GL_MAX_HEIGHTLEVEL_TEXT;
661 SetDParam(0, _settings_newgame.construction.max_heightlevel);
662 ShowQueryString(STR_JUST_INT, STR_MAPGEN_MAX_HEIGHTLEVEL_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
663 break;
666 case WID_GL_START_DATE_DOWN:
667 case WID_GL_START_DATE_UP: // Year buttons
668 /* Don't allow too fast scrolling */
669 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
670 this->HandleButtonClick(widget);
672 _settings_newgame.game_creation.starting_year = Clamp(_settings_newgame.game_creation.starting_year + widget - WID_GL_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
673 this->InvalidateData();
675 _left_button_clicked = false;
676 break;
678 case WID_GL_START_DATE_TEXT: // Year text
679 this->widget_id = WID_GL_START_DATE_TEXT;
680 SetDParam(0, _settings_newgame.game_creation.starting_year);
681 ShowQueryString(STR_JUST_INT, STR_MAPGEN_START_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
682 break;
684 case WID_GL_SNOW_LEVEL_DOWN:
685 case WID_GL_SNOW_LEVEL_UP: // Snow line buttons
686 /* Don't allow too fast scrolling */
687 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
688 this->HandleButtonClick(widget);
690 _settings_newgame.game_creation.snow_line_height = Clamp(_settings_newgame.game_creation.snow_line_height + widget - WID_GL_SNOW_LEVEL_TEXT, MIN_SNOWLINE_HEIGHT, MAX_SNOWLINE_HEIGHT);
691 this->InvalidateData();
693 _left_button_clicked = false;
694 break;
696 case WID_GL_SNOW_LEVEL_TEXT: // Snow line text
697 this->widget_id = WID_GL_SNOW_LEVEL_TEXT;
698 SetDParam(0, _settings_newgame.game_creation.snow_line_height);
699 ShowQueryString(STR_JUST_INT, STR_MAPGEN_SNOW_LINE_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
700 break;
702 case WID_GL_TREE_LEVEL_DOWN:
703 case WID_GL_TREE_LEVEL_UP: // tree line buttons
704 /* Don't allow too fast scrolling */
705 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
706 this->HandleButtonClick(widget);
708 _settings_newgame.game_creation.tree_line_height = Clamp(_settings_newgame.game_creation.tree_line_height + widget - WID_GL_TREE_LEVEL_TEXT, MIN_TREELINE_HEIGHT, MAX_TREELINE_HEIGHT);
709 this->InvalidateData();
711 _left_button_clicked = false;
712 break;
714 case WID_GL_TREE_LEVEL_TEXT: // tree line text
715 this->widget_id = WID_GL_TREE_LEVEL_TEXT;
716 SetDParam(0, _settings_newgame.game_creation.tree_line_height);
717 ShowQueryString(STR_JUST_INT, STR_MAPGEN_TREE_LINE_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
718 break;
720 case WID_GL_DESERT_AMOUNT_DOWN:
721 case WID_GL_DESERT_AMOUNT_UP: // Desert amount buttons.
722 /* Don't allow too fast scrolling */
723 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
724 this->HandleButtonClick(widget);
726 _settings_newgame.game_creation.desert_amount = Clamp(_settings_newgame.game_creation.desert_amount + widget - WID_GL_DESERT_AMOUNT_TEXT, MIN_DESERT_AMOUNT, MAX_DESERT_AMOUNT);
727 this->InvalidateData();
729 _left_button_clicked = false;
730 break;
732 case WID_GL_DESERT_AMOUNT_TEXT: // Desert amount text.
733 this->widget_id = WID_GL_DESERT_AMOUNT_TEXT;
734 SetDParam(0, _settings_newgame.game_creation.desert_amount);
735 ShowQueryString(STR_JUST_INT, STR_MAPGEN_DESERT_AMOUNT_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
736 break;
738 case WID_GL_MIN_RIVER_LENGTH_DOWN:
739 case WID_GL_MIN_RIVER_LENGTH_UP: // Minimum river length buttons.
740 /* Don't allow too fast scrolling */
741 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
742 this->HandleButtonClick(widget);
744 _settings_newgame.game_creation.min_river_length = Clamp(_settings_newgame.game_creation.min_river_length + widget - WID_GL_MIN_RIVER_LENGTH_TEXT, MIN_MIN_RIVER_LENGTH, MAX_MIN_RIVER_LENGTH);
745 this->InvalidateData();
747 _left_button_clicked = false;
748 break;
750 case WID_GL_MIN_RIVER_LENGTH_TEXT: // Minimum river length text.
751 this->widget_id = WID_GL_MIN_RIVER_LENGTH_TEXT;
752 SetDParam(0, _settings_newgame.game_creation.min_river_length);
753 ShowQueryString(STR_JUST_INT, STR_MAPGEN_MIN_RIVER_LENGTH_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
754 break;
756 case WID_GL_TREE_PULLDOWN: // Tree placer
757 ShowDropDownMenu(this, _tree_placer, _settings_newgame.game_creation.tree_placer, WID_GL_TREE_PULLDOWN, 0, 0);
758 break;
760 case WID_GL_LANDSCAPE_PULLDOWN: // Landscape generator
761 ShowDropDownMenu(this, _landscape, _settings_newgame.game_creation.land_generator, WID_GL_LANDSCAPE_PULLDOWN, 0, 0);
762 break;
764 case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: // Heightmap rotation
765 ShowDropDownMenu(this, _rotation, _settings_newgame.game_creation.heightmap_rotation, WID_GL_HEIGHTMAP_ROTATION_PULLDOWN, 0, 0);
766 break;
768 case WID_GL_TERRAIN_PULLDOWN: // Terrain type
769 /* For the original map generation only the first four are valid. */
770 ShowDropDownMenu(this, _elevations, _settings_newgame.difficulty.terrain_type, WID_GL_TERRAIN_PULLDOWN, 0, _settings_newgame.game_creation.land_generator == LG_ORIGINAL ? ~0xF : 0);
771 break;
773 case WID_GL_WATER_PULLDOWN: { // Water quantity
774 uint32 hidden_mask = 0;
775 /* Disable custom water level when the original map generator is active. */
776 if (_settings_newgame.game_creation.land_generator == LG_ORIGINAL) {
777 SetBit(hidden_mask, CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY);
779 ShowDropDownMenu(this, _sea_lakes, _settings_newgame.difficulty.quantity_sea_lakes, WID_GL_WATER_PULLDOWN, 0, hidden_mask);
780 break;
783 case WID_GL_RIVER_PULLDOWN: // Amount of rivers
784 ShowDropDownMenu(this, _rivers, _settings_newgame.game_creation.amount_of_rivers, WID_GL_RIVER_PULLDOWN, 0, 0);
785 break;
787 case WID_GL_SMOOTHNESS_PULLDOWN: // Map smoothness
788 ShowDropDownMenu(this, _smoothness, _settings_newgame.game_creation.tgen_smoothness, WID_GL_SMOOTHNESS_PULLDOWN, 0, 0);
789 break;
791 case WID_GL_VARIETY_PULLDOWN: // Map variety
792 ShowDropDownMenu(this, _variety, _settings_newgame.game_creation.variety, WID_GL_VARIETY_PULLDOWN, 0, 0);
793 break;
795 /* Freetype map borders */
796 case WID_GL_WATER_NW:
797 _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_NW);
798 this->InvalidateData();
799 break;
801 case WID_GL_WATER_NE:
802 _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_NE);
803 this->InvalidateData();
804 break;
806 case WID_GL_WATER_SE:
807 _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_SE);
808 this->InvalidateData();
809 break;
811 case WID_GL_WATER_SW:
812 _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_SW);
813 this->InvalidateData();
814 break;
816 case WID_GL_BORDERS_RANDOM:
817 _settings_newgame.game_creation.water_borders = (_settings_newgame.game_creation.water_borders == BORDERS_RANDOM) ? 0 : BORDERS_RANDOM;
818 this->InvalidateData();
819 break;
823 virtual void OnTimeout()
825 static const int raise_widgets[] = { WID_GL_MAX_HEIGHTLEVEL_DOWN, WID_GL_MAX_HEIGHTLEVEL_UP, WID_GL_START_DATE_DOWN, WID_GL_START_DATE_UP, WID_GL_SNOW_LEVEL_UP, WID_GL_SNOW_LEVEL_DOWN, WID_GL_TREE_LEVEL_UP, WID_GL_TREE_LEVEL_DOWN, WID_GL_DESERT_AMOUNT_UP, WID_GL_DESERT_AMOUNT_DOWN, WID_GL_MIN_RIVER_LENGTH_DOWN, WID_GL_MIN_RIVER_LENGTH_UP, WIDGET_LIST_END};
827 for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
828 if (this->IsWidgetLowered(*widget)) {
829 this->RaiseWidget(*widget);
830 this->SetWidgetDirty(*widget);
835 virtual void OnDropdownSelect(int widget, int index)
837 switch (widget) {
838 case WID_GL_MAPSIZE_X_PULLDOWN: _settings_newgame.game_creation.map_x = index; break;
839 case WID_GL_MAPSIZE_Y_PULLDOWN: _settings_newgame.game_creation.map_y = index; break;
840 case WID_GL_TREE_PULLDOWN: _settings_newgame.game_creation.tree_placer = index; break;
841 case WID_GL_RIVER_PULLDOWN: _settings_newgame.game_creation.amount_of_rivers = index; break;
842 case WID_GL_SMOOTHNESS_PULLDOWN: _settings_newgame.game_creation.tgen_smoothness = index; break;
843 case WID_GL_VARIETY_PULLDOWN: _settings_newgame.game_creation.variety = index; break;
845 case WID_GL_LANDSCAPE_PULLDOWN: _settings_newgame.game_creation.land_generator = index;
846 /* If original landgenerator is selected and alpinist terrain_type was selected, revert to mountainous. */
847 if (_settings_newgame.game_creation.land_generator == LG_ORIGINAL) {
848 _settings_newgame.difficulty.terrain_type = Clamp(_settings_newgame.difficulty.terrain_type, 0, 3);
850 break;
852 case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: _settings_newgame.game_creation.heightmap_rotation = index; break;
854 case WID_GL_TOWN_PULLDOWN:
855 if ((uint)index == CUSTOM_TOWN_NUMBER_DIFFICULTY) {
856 this->widget_id = widget;
857 SetDParam(0, _settings_newgame.game_creation.custom_town_number);
858 ShowQueryString(STR_JUST_INT, STR_MAPGEN_NUMBER_OF_TOWNS, 5, this, CS_NUMERAL, QSF_NONE);
860 _settings_newgame.difficulty.number_towns = index;
861 break;
863 case WID_GL_INDUSTRY_PULLDOWN: _settings_newgame.difficulty.industry_density = index; break;
864 case WID_GL_TERRAIN_PULLDOWN: _settings_newgame.difficulty.terrain_type = index; break;
866 case WID_GL_WATER_PULLDOWN: {
867 if ((uint)index == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
868 this->widget_id = widget;
869 SetDParam(0, _settings_newgame.game_creation.custom_sea_level);
870 ShowQueryString(STR_JUST_INT, STR_MAPGEN_QUANTITY_OF_SEA_LAKES, 3, this, CS_NUMERAL, QSF_NONE);
872 _settings_newgame.difficulty.quantity_sea_lakes = index;
873 break;
876 this->InvalidateData();
879 virtual void OnQueryTextFinished(char *str)
881 /* Was 'cancel' pressed? */
882 if (str == NULL) return;
884 int32 value;
885 if (!StrEmpty(str)) {
886 value = atoi(str);
887 } else {
888 /* An empty string means revert to the default */
889 switch (this->widget_id) {
890 case WID_GL_MAX_HEIGHTLEVEL_TEXT: value = DEF_MAX_HEIGHTLEVEL; break;
891 case WID_GL_START_DATE_TEXT: value = DEF_START_YEAR; break;
892 case WID_GL_SNOW_LEVEL_TEXT: value = DEF_SNOWLINE_HEIGHT; break;
893 case WID_GL_TREE_LEVEL_TEXT: value = DEF_TREELINE_HEIGHT; break;
894 case WID_GL_MIN_RIVER_LENGTH_TEXT: value = DEF_MIN_RIVER_LENGTH; break;
895 case WID_GL_DESERT_AMOUNT_TEXT: value = DEF_DESERT_AMOUNT; break;
896 case WID_GL_TOWN_PULLDOWN: value = 1; break;
897 case WID_GL_WATER_PULLDOWN: value = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE; break;
898 default: NOT_REACHED();
902 switch (this->widget_id) {
903 case WID_GL_MAX_HEIGHTLEVEL_TEXT:
904 this->SetWidgetDirty(WID_GL_MAX_HEIGHTLEVEL_TEXT);
905 _settings_newgame.construction.max_heightlevel = Clamp(value, MIN_MAX_HEIGHTLEVEL, MAX_MAX_HEIGHTLEVEL);
906 break;
908 case WID_GL_START_DATE_TEXT:
909 this->SetWidgetDirty(WID_GL_START_DATE_TEXT);
910 _settings_newgame.game_creation.starting_year = Clamp(value, MIN_YEAR, MAX_YEAR);
911 break;
913 case WID_GL_SNOW_LEVEL_TEXT:
914 this->SetWidgetDirty(WID_GL_SNOW_LEVEL_TEXT);
915 _settings_newgame.game_creation.snow_line_height = Clamp(value, MIN_SNOWLINE_HEIGHT, MAX_SNOWLINE_HEIGHT);
916 break;
918 case WID_GL_MIN_RIVER_LENGTH_TEXT:
919 this->SetWidgetDirty(WID_GL_SNOW_LEVEL_TEXT);
920 _settings_newgame.game_creation.min_river_length = Clamp(value, MIN_MIN_RIVER_LENGTH, MAX_MIN_RIVER_LENGTH);
921 break;
923 case WID_GL_TREE_LEVEL_TEXT:
924 this->SetWidgetDirty(WID_GL_TREE_LEVEL_TEXT);
925 _settings_newgame.game_creation.tree_line_height = Clamp(value, MIN_TREELINE_HEIGHT, MAX_TREELINE_HEIGHT);
926 break;
928 case WID_GL_DESERT_AMOUNT_TEXT:
929 this->SetWidgetDirty(WID_GL_DESERT_AMOUNT_TEXT);
930 _settings_newgame.game_creation.desert_amount = Clamp(value, MIN_DESERT_AMOUNT, MAX_DESERT_AMOUNT);
931 break;
933 case WID_GL_TOWN_PULLDOWN:
934 _settings_newgame.game_creation.custom_town_number = Clamp(value, 1, CUSTOM_TOWN_MAX_NUMBER);
935 break;
937 case WID_GL_WATER_PULLDOWN:
938 _settings_newgame.game_creation.custom_sea_level = Clamp(value, CUSTOM_SEA_LEVEL_MIN_PERCENTAGE, CUSTOM_SEA_LEVEL_MAX_PERCENTAGE);
939 break;
942 this->InvalidateData();
946 static WindowDesc _generate_landscape_desc(
947 WDP_CENTER, NULL, 0, 0,
948 WC_GENERATE_LANDSCAPE, WC_NONE,
950 _nested_generate_landscape_widgets, lengthof(_nested_generate_landscape_widgets)
953 static WindowDesc _heightmap_load_desc(
954 WDP_CENTER, NULL, 0, 0,
955 WC_GENERATE_LANDSCAPE, WC_NONE,
957 _nested_heightmap_load_widgets, lengthof(_nested_heightmap_load_widgets)
960 static void _ShowGenerateLandscape(GenerateLandscapeWindowMode mode)
962 uint x = 0;
963 uint y = 0;
965 DeleteWindowByClass(WC_GENERATE_LANDSCAPE);
967 /* Generate a new seed when opening the window */
968 _settings_newgame.game_creation.generation_seed = InteractiveRandom();
970 if (mode == GLWM_HEIGHTMAP) {
971 /* If the function returns negative, it means there was a problem loading the heightmap */
972 if (!GetHeightmapDimensions(_file_to_saveload.detail_ftype, _file_to_saveload.name, &x, &y)) return;
975 WindowDesc *desc = (mode == GLWM_HEIGHTMAP) ? &_heightmap_load_desc : &_generate_landscape_desc;
976 GenerateLandscapeWindow *w = AllocateWindowDescFront<GenerateLandscapeWindow>(desc, mode, true);
978 if (mode == GLWM_HEIGHTMAP) {
979 w->x = x;
980 w->y = y;
981 strecpy(w->name, _file_to_saveload.title, lastof(w->name));
984 SetWindowDirty(WC_GENERATE_LANDSCAPE, mode);
987 /** Start with a normal game. */
988 void ShowGenerateLandscape()
990 _ShowGenerateLandscape(GLWM_GENERATE);
993 /** Start with loading a heightmap. */
994 void ShowHeightmapLoad()
996 _ShowGenerateLandscape(GLWM_HEIGHTMAP);
999 /** Start with a scenario editor. */
1000 void StartScenarioEditor()
1002 StartGeneratingLandscape(GLWM_SCENARIO);
1006 * Start a normal game without the GUI.
1007 * @param seed The seed of the new game.
1009 void StartNewGameWithoutGUI(uint seed)
1011 /* GenerateWorld takes care of the possible GENERATE_NEW_SEED value in 'seed' */
1012 _settings_newgame.game_creation.generation_seed = seed;
1014 StartGeneratingLandscape(GLWM_GENERATE);
1017 struct CreateScenarioWindow : public Window
1019 uint widget_id;
1021 CreateScenarioWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
1023 this->InitNested(window_number);
1024 this->LowerWidget(_settings_newgame.game_creation.landscape + WID_CS_TEMPERATE);
1027 virtual void SetStringParameters(int widget) const
1029 switch (widget) {
1030 case WID_CS_START_DATE_TEXT:
1031 SetDParam(0, ConvertYMDToDate(_settings_newgame.game_creation.starting_year, 0, 1));
1032 break;
1034 case WID_CS_MAPSIZE_X_PULLDOWN:
1035 SetDParam(0, 1 << _settings_newgame.game_creation.map_x);
1036 break;
1038 case WID_CS_MAPSIZE_Y_PULLDOWN:
1039 SetDParam(0, 1 << _settings_newgame.game_creation.map_y);
1040 break;
1042 case WID_CS_FLAT_LAND_HEIGHT_TEXT:
1043 SetDParam(0, _settings_newgame.game_creation.se_flat_world_height);
1044 break;
1048 virtual void OnPaint()
1050 this->SetWidgetDisabledState(WID_CS_START_DATE_DOWN, _settings_newgame.game_creation.starting_year <= MIN_YEAR);
1051 this->SetWidgetDisabledState(WID_CS_START_DATE_UP, _settings_newgame.game_creation.starting_year >= MAX_YEAR);
1052 this->SetWidgetDisabledState(WID_CS_FLAT_LAND_HEIGHT_DOWN, _settings_newgame.game_creation.se_flat_world_height <= 0);
1053 this->SetWidgetDisabledState(WID_CS_FLAT_LAND_HEIGHT_UP, _settings_newgame.game_creation.se_flat_world_height >= MAX_TILE_HEIGHT);
1055 this->SetWidgetLoweredState(WID_CS_TEMPERATE, _settings_newgame.game_creation.landscape == LT_TEMPERATE);
1056 this->SetWidgetLoweredState(WID_CS_ARCTIC, _settings_newgame.game_creation.landscape == LT_ARCTIC);
1057 this->SetWidgetLoweredState(WID_CS_TROPICAL, _settings_newgame.game_creation.landscape == LT_TROPIC);
1058 this->SetWidgetLoweredState(WID_CS_TOYLAND, _settings_newgame.game_creation.landscape == LT_TOYLAND);
1060 this->DrawWidgets();
1063 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1065 StringID str = STR_JUST_INT;
1066 switch (widget) {
1067 case WID_CS_START_DATE_TEXT:
1068 SetDParam(0, ConvertYMDToDate(MAX_YEAR, 0, 1));
1069 str = STR_BLACK_DATE_LONG;
1070 break;
1072 case WID_CS_MAPSIZE_X_PULLDOWN:
1073 case WID_CS_MAPSIZE_Y_PULLDOWN:
1074 SetDParamMaxValue(0, MAX_MAP_SIZE);
1075 break;
1077 case WID_CS_FLAT_LAND_HEIGHT_TEXT:
1078 SetDParamMaxValue(0, MAX_TILE_HEIGHT);
1079 break;
1081 default:
1082 return;
1084 *size = maxdim(*size, GetStringBoundingBox(str));
1085 size->width += padding.width;
1086 size->height += padding.height;
1089 virtual void OnClick(Point pt, int widget, int click_count)
1091 switch (widget) {
1092 case WID_CS_TEMPERATE:
1093 case WID_CS_ARCTIC:
1094 case WID_CS_TROPICAL:
1095 case WID_CS_TOYLAND:
1096 this->RaiseWidget(_settings_newgame.game_creation.landscape + WID_CS_TEMPERATE);
1097 SetNewLandscapeType(widget - WID_CS_TEMPERATE);
1098 break;
1100 case WID_CS_MAPSIZE_X_PULLDOWN: // Mapsize X
1101 ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_x, WID_CS_MAPSIZE_X_PULLDOWN);
1102 break;
1104 case WID_CS_MAPSIZE_Y_PULLDOWN: // Mapsize Y
1105 ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_y, WID_CS_MAPSIZE_Y_PULLDOWN);
1106 break;
1108 case WID_CS_EMPTY_WORLD: // Empty world / flat world
1109 StartGeneratingLandscape(GLWM_SCENARIO);
1110 break;
1112 case WID_CS_RANDOM_WORLD: // Generate
1113 ShowGenerateLandscape();
1114 break;
1116 case WID_CS_START_DATE_DOWN:
1117 case WID_CS_START_DATE_UP: // Year buttons
1118 /* Don't allow too fast scrolling */
1119 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
1120 this->HandleButtonClick(widget);
1121 this->SetDirty();
1123 _settings_newgame.game_creation.starting_year = Clamp(_settings_newgame.game_creation.starting_year + widget - WID_CS_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
1125 _left_button_clicked = false;
1126 break;
1128 case WID_CS_START_DATE_TEXT: // Year text
1129 this->widget_id = WID_CS_START_DATE_TEXT;
1130 SetDParam(0, _settings_newgame.game_creation.starting_year);
1131 ShowQueryString(STR_JUST_INT, STR_MAPGEN_START_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_NONE);
1132 break;
1134 case WID_CS_FLAT_LAND_HEIGHT_DOWN:
1135 case WID_CS_FLAT_LAND_HEIGHT_UP: // Height level buttons
1136 /* Don't allow too fast scrolling */
1137 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
1138 this->HandleButtonClick(widget);
1139 this->SetDirty();
1141 _settings_newgame.game_creation.se_flat_world_height = Clamp(_settings_newgame.game_creation.se_flat_world_height + widget - WID_CS_FLAT_LAND_HEIGHT_TEXT, 0, _settings_game.construction.max_heightlevel);
1143 _left_button_clicked = false;
1144 break;
1146 case WID_CS_FLAT_LAND_HEIGHT_TEXT: // Height level text
1147 this->widget_id = WID_CS_FLAT_LAND_HEIGHT_TEXT;
1148 SetDParam(0, _settings_newgame.game_creation.se_flat_world_height);
1149 ShowQueryString(STR_JUST_INT, STR_SE_MAPGEN_FLAT_WORLD_HEIGHT_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_NONE);
1150 break;
1154 virtual void OnTimeout()
1156 static const int raise_widgets[] = {WID_CS_START_DATE_DOWN, WID_CS_START_DATE_UP, WID_CS_FLAT_LAND_HEIGHT_DOWN, WID_CS_FLAT_LAND_HEIGHT_UP, WIDGET_LIST_END};
1157 for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
1158 if (this->IsWidgetLowered(*widget)) {
1159 this->RaiseWidget(*widget);
1160 this->SetWidgetDirty(*widget);
1165 virtual void OnDropdownSelect(int widget, int index)
1167 switch (widget) {
1168 case WID_CS_MAPSIZE_X_PULLDOWN: _settings_newgame.game_creation.map_x = index; break;
1169 case WID_CS_MAPSIZE_Y_PULLDOWN: _settings_newgame.game_creation.map_y = index; break;
1171 this->SetDirty();
1174 virtual void OnQueryTextFinished(char *str)
1176 if (!StrEmpty(str)) {
1177 int32 value = atoi(str);
1179 switch (this->widget_id) {
1180 case WID_CS_START_DATE_TEXT:
1181 this->SetWidgetDirty(WID_CS_START_DATE_TEXT);
1182 _settings_newgame.game_creation.starting_year = Clamp(value, MIN_YEAR, MAX_YEAR);
1183 break;
1185 case WID_CS_FLAT_LAND_HEIGHT_TEXT:
1186 this->SetWidgetDirty(WID_CS_FLAT_LAND_HEIGHT_TEXT);
1187 _settings_newgame.game_creation.se_flat_world_height = Clamp(value, 0, _settings_game.construction.max_heightlevel);
1188 break;
1191 this->SetDirty();
1196 static const NWidgetPart _nested_create_scenario_widgets[] = {
1197 NWidget(NWID_HORIZONTAL),
1198 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1199 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SE_MAPGEN_CAPTION, STR_NULL),
1200 EndContainer(),
1201 NWidget(WWT_PANEL, COLOUR_BROWN),
1202 NWidget(NWID_SPACER), SetMinimalSize(0, 10),
1203 /* Landscape style selection. */
1204 NWidget(NWID_HORIZONTAL), SetPIP(10, 3, 10),
1205 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_TEMPERATE), SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
1206 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_ARCTIC), SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
1207 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_TROPICAL), SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
1208 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_TOYLAND), SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
1209 EndContainer(),
1210 NWidget(NWID_HORIZONTAL), SetPIP(10, 8, 10),
1211 /* Green generation type buttons: 'Flat land' and 'Random land'. */
1212 NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
1213 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_CS_EMPTY_WORLD), SetDataTip(STR_SE_MAPGEN_FLAT_WORLD, STR_SE_MAPGEN_FLAT_WORLD_TOOLTIP), SetFill(1, 1),
1214 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_CS_RANDOM_WORLD), SetDataTip(STR_SE_MAPGEN_RANDOM_LAND, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetFill(1, 1),
1215 EndContainer(),
1216 /* Labels + setting drop-downs */
1217 NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
1218 /* Map size. */
1219 NWidget(NWID_HORIZONTAL),
1220 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAPSIZE, STR_NULL), SetPadding(1, 0, 0, 0),
1221 NWidget(NWID_SPACER), SetMinimalSize(6, 0), SetFill(1, 0),
1222 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_CS_MAPSIZE_X_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetPadding(0, 4, 0, 0),
1223 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BY, STR_NULL), SetPadding(1, 2, 0, 0),
1224 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_CS_MAPSIZE_Y_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL),
1225 EndContainer(),
1226 /* Date. */
1227 NWidget(NWID_HORIZONTAL),
1228 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DATE, STR_NULL), SetPadding(1, 0, 0, 0),
1229 NWidget(NWID_SPACER), SetMinimalSize(6, 0), SetFill(1, 0),
1230 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_CS_START_DATE_DOWN), SetFill(0, 1), SetDataTip(SPR_ARROW_DOWN, STR_SCENEDIT_TOOLBAR_TOOLTIP_MOVE_THE_STARTING_DATE_BACKWARD),
1231 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_CS_START_DATE_TEXT), SetDataTip(STR_BLACK_DATE_LONG, STR_NULL),
1232 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_CS_START_DATE_UP), SetFill(0, 1), SetDataTip(SPR_ARROW_UP, STR_SCENEDIT_TOOLBAR_TOOLTIP_MOVE_THE_STARTING_DATE_FORWARD),
1233 EndContainer(),
1234 /* Flat map height. */
1235 NWidget(NWID_HORIZONTAL),
1236 NWidget(WWT_TEXT, COLOUR_ORANGE),
1237 SetDataTip(STR_SE_MAPGEN_FLAT_WORLD_HEIGHT, STR_NULL), SetPadding(1, 0, 0, 0),
1238 NWidget(NWID_SPACER), SetMinimalSize(6, 0), SetFill(1, 0),
1239 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_CS_FLAT_LAND_HEIGHT_DOWN), SetFill(0, 1), SetDataTip(SPR_ARROW_DOWN, STR_SE_MAPGEN_FLAT_WORLD_HEIGHT_DOWN),
1240 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_CS_FLAT_LAND_HEIGHT_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL),
1241 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_CS_FLAT_LAND_HEIGHT_UP), SetFill(0, 1), SetDataTip(SPR_ARROW_UP, STR_SE_MAPGEN_FLAT_WORLD_HEIGHT_UP),
1242 EndContainer(),
1243 EndContainer(),
1244 EndContainer(),
1245 EndContainer(),
1248 static WindowDesc _create_scenario_desc(
1249 WDP_CENTER, NULL, 0, 0,
1250 WC_GENERATE_LANDSCAPE, WC_NONE,
1252 _nested_create_scenario_widgets, lengthof(_nested_create_scenario_widgets)
1255 /** Show the window to create a scenario. */
1256 void ShowCreateScenario()
1258 DeleteWindowByClass(WC_GENERATE_LANDSCAPE);
1259 new CreateScenarioWindow(&_create_scenario_desc, GLWM_SCENARIO);
1262 static const NWidgetPart _nested_generate_progress_widgets[] = {
1263 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GENERATION_WORLD, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1264 NWidget(WWT_PANEL, COLOUR_GREY),
1265 NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
1266 NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
1267 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GP_PROGRESS_BAR), SetFill(1, 0),
1268 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GP_PROGRESS_TEXT), SetFill(1, 0),
1269 NWidget(WWT_TEXTBTN, COLOUR_WHITE, WID_GP_ABORT), SetDataTip(STR_GENERATION_ABORT, STR_NULL), SetFill(1, 0),
1270 EndContainer(),
1271 EndContainer(),
1272 EndContainer(),
1276 static WindowDesc _generate_progress_desc(
1277 WDP_CENTER, NULL, 0, 0,
1278 WC_MODAL_PROGRESS, WC_NONE,
1280 _nested_generate_progress_widgets, lengthof(_nested_generate_progress_widgets)
1283 struct GenWorldStatus {
1284 uint percent;
1285 StringID cls;
1286 uint current;
1287 uint total;
1288 int timer;
1291 static GenWorldStatus _gws;
1293 static const StringID _generation_class_table[] = {
1294 STR_GENERATION_WORLD_GENERATION,
1295 STR_SCENEDIT_TOOLBAR_LANDSCAPE_GENERATION,
1296 STR_GENERATION_RIVER_GENERATION,
1297 STR_GENERATION_CLEARING_TILES,
1298 STR_SCENEDIT_TOOLBAR_TOWN_GENERATION,
1299 STR_SCENEDIT_TOOLBAR_INDUSTRY_GENERATION,
1300 STR_GENERATION_OBJECT_GENERATION,
1301 STR_GENERATION_TREE_GENERATION,
1302 STR_GENERATION_PUBLIC_ROADS_GENERATION,
1303 STR_GENERATION_SETTINGUP_GAME,
1304 STR_GENERATION_PREPARING_TILELOOP,
1305 STR_GENERATION_PREPARING_SCRIPT,
1306 STR_GENERATION_PREPARING_GAME
1308 assert_compile(lengthof(_generation_class_table) == GWP_CLASS_COUNT);
1311 static void AbortGeneratingWorldCallback(Window *w, bool confirmed)
1313 if (confirmed) {
1314 AbortGeneratingWorld();
1315 } else if (HasModalProgress() && !IsGeneratingWorldAborted()) {
1316 SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
1320 struct GenerateProgressWindow : public Window {
1322 GenerateProgressWindow() : Window(&_generate_progress_desc)
1324 this->InitNested();
1327 virtual void OnClick(Point pt, int widget, int click_count)
1329 switch (widget) {
1330 case WID_GP_ABORT:
1331 SetMouseCursorBusy(false);
1332 ShowQuery(
1333 STR_GENERATION_ABORT_CAPTION,
1334 STR_GENERATION_ABORT_MESSAGE,
1335 this,
1336 AbortGeneratingWorldCallback
1338 break;
1342 virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
1344 switch (widget) {
1345 case WID_GP_PROGRESS_BAR: {
1346 SetDParamMaxValue(0, 100);
1347 *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
1348 /* We need some spacing for the 'border' */
1349 size->height += 8;
1350 size->width += 8;
1351 break;
1354 case WID_GP_PROGRESS_TEXT:
1355 for (uint i = 0; i < GWP_CLASS_COUNT; i++) {
1356 size->width = max(size->width, GetStringBoundingBox(_generation_class_table[i]).width);
1358 size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
1359 break;
1363 virtual void DrawWidget(const Rect &r, int widget) const
1365 switch (widget) {
1366 case WID_GP_PROGRESS_BAR:
1367 /* Draw the % complete with a bar and a text */
1368 DrawFrameRect(r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
1369 DrawFrameRect(r.left + 1, r.top + 1, (int)((r.right - r.left - 2) * _gws.percent / 100) + r.left + 1, r.bottom - 1, COLOUR_MAUVE, FR_NONE);
1370 SetDParam(0, _gws.percent);
1371 DrawString(r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
1372 break;
1374 case WID_GP_PROGRESS_TEXT:
1375 /* Tell which class we are generating */
1376 DrawString(r.left, r.right, r.top, _gws.cls, TC_FROMSTRING, SA_HOR_CENTER);
1378 /* And say where we are in that class */
1379 SetDParam(0, _gws.current);
1380 SetDParam(1, _gws.total);
1381 DrawString(r.left, r.right, r.top + FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL, STR_GENERATION_PROGRESS_NUM, TC_FROMSTRING, SA_HOR_CENTER);
1387 * Initializes the progress counters to the starting point.
1389 void PrepareGenerateWorldProgress()
1391 _gws.cls = STR_GENERATION_WORLD_GENERATION;
1392 _gws.current = 0;
1393 _gws.total = 0;
1394 _gws.percent = 0;
1395 _gws.timer = 0; // Forces to paint the progress window immediately
1399 * Show the window where a user can follow the process of the map generation.
1401 void ShowGenerateWorldProgress()
1403 if (BringWindowToFrontById(WC_MODAL_PROGRESS, 0)) return;
1404 new GenerateProgressWindow();
1407 static void _SetGeneratingWorldProgress(GenWorldProgress cls, uint progress, uint total)
1409 static const int percent_table[] = {0, 7, 14, 22, 29, 36, 44, 51, 58, 65, 73, 80, 90, 100 };
1410 assert_compile(lengthof(percent_table) == GWP_CLASS_COUNT + 1);
1411 assert(cls < GWP_CLASS_COUNT);
1413 /* Do not run this function if we aren't in a thread */
1414 if (!IsGenerateWorldThreaded() && !_network_dedicated) return;
1416 if (IsGeneratingWorldAborted()) HandleGeneratingWorldAbortion();
1418 if (total == 0) {
1419 assert(_gws.cls == _generation_class_table[cls]);
1420 _gws.current += progress;
1421 assert(_gws.current <= _gws.total);
1422 } else {
1423 _gws.cls = _generation_class_table[cls];
1424 _gws.current = progress;
1425 _gws.total = total;
1426 _gws.percent = percent_table[cls];
1429 /* Don't update the screen too often. So update it once in every once in a while... */
1430 if (!_network_dedicated && _gws.timer != 0 && _realtime_tick - _gws.timer < MODAL_PROGRESS_REDRAW_TIMEOUT) return;
1432 /* Percentage is about the number of completed tasks, so 'current - 1' */
1433 _gws.percent = percent_table[cls] + (percent_table[cls + 1] - percent_table[cls]) * (_gws.current == 0 ? 0 : _gws.current - 1) / _gws.total;
1435 if (_network_dedicated) {
1436 static uint last_percent = 0;
1438 /* Never display 0% */
1439 if (_gws.percent == 0) return;
1440 /* Reset if percent is lower than the last recorded */
1441 if (_gws.percent < last_percent) last_percent = 0;
1442 /* Display every 5%, but 6% is also very valid.. just not smaller steps than 5% */
1443 if (_gws.percent % 5 != 0 && _gws.percent <= last_percent + 5) return;
1444 /* Never show steps smaller than 2%, even if it is a mod 5% */
1445 if (_gws.percent <= last_percent + 2) return;
1447 DEBUG(net, 1, "Map generation percentage complete: %d", _gws.percent);
1448 last_percent = _gws.percent;
1450 /* Don't continue as dedicated never has a thread running */
1451 return;
1454 SetWindowDirty(WC_MODAL_PROGRESS, 0);
1455 MarkWholeScreenDirty();
1457 /* Release the rights to the map generator, and acquire the rights to the
1458 * paint thread. The 'other' thread already has the paint thread rights so
1459 * this ensures us that we are waiting until the paint thread is done
1460 * before we reacquire the mapgen rights */
1461 _modal_progress_work_mutex->EndCritical();
1462 _modal_progress_paint_mutex->BeginCritical();
1463 _modal_progress_work_mutex->BeginCritical();
1464 _modal_progress_paint_mutex->EndCritical();
1466 _gws.timer = _realtime_tick;
1470 * Set the total of a stage of the world generation.
1471 * @param cls the current class we are in.
1472 * @param total Set the total expected items for this class.
1474 * Warning: this function isn't clever. Don't go from class 4 to 3. Go upwards, always.
1475 * Also, progress works if total is zero, total works if progress is zero.
1477 void SetGeneratingWorldProgress(GenWorldProgress cls, uint total)
1479 if (total == 0) return;
1481 _SetGeneratingWorldProgress(cls, 0, total);
1485 * Increases the current stage of the world generation with one.
1486 * @param cls the current class we are in.
1488 * Warning: this function isn't clever. Don't go from class 4 to 3. Go upwards, always.
1489 * Also, progress works if total is zero, total works if progress is zero.
1491 void IncreaseGeneratingWorldProgress(GenWorldProgress cls)
1493 /* In fact the param 'class' isn't needed.. but for some security reasons, we want it around */
1494 _SetGeneratingWorldProgress(cls, 1, 0);