Fix #10117: Decrement object burst limit after build check
[openttd-github.git] / src / genworld_gui.cpp
blob9e915304ea2480798c7e823681b701399e134637
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
8 /** @file genworld_gui.cpp GUI to configure and show progress during map generation. */
10 #include "stdafx.h"
11 #include "heightmap.h"
12 #include "debug.h"
13 #include "genworld.h"
14 #include "network/network.h"
15 #include "strings_func.h"
16 #include "window_func.h"
17 #include "date_func.h"
18 #include "sound_func.h"
19 #include "fios.h"
20 #include "string_func.h"
21 #include "widgets/dropdown_type.h"
22 #include "widgets/dropdown_func.h"
23 #include "querystring_gui.h"
24 #include "town.h"
25 #include "core/geometry_func.hpp"
26 #include "core/random_func.hpp"
27 #include "saveload/saveload.h"
28 #include "progress.h"
29 #include "error.h"
30 #include "newgrf_townname.h"
31 #include "townname_type.h"
32 #include "video/video_driver.hpp"
34 #include "widgets/genworld_widget.h"
36 #include "safeguards.h"
39 extern void MakeNewgameSettingsLive();
41 /** Enum for the modes we can generate in. */
42 enum GenerateLandscapeWindowMode {
43 GLWM_GENERATE, ///< Generate new game.
44 GLWM_HEIGHTMAP, ///< Load from heightmap.
45 GLWM_SCENARIO, ///< Generate flat land.
48 /**
49 * Get the map height limit, or if set to "auto", the absolute limit.
51 static uint GetMapHeightLimit()
53 if (_settings_newgame.construction.map_height_limit == 0) return MAX_MAP_HEIGHT_LIMIT;
54 return _settings_newgame.construction.map_height_limit;
57 /**
58 * Changes landscape type and sets genworld window dirty
59 * @param landscape new landscape type
61 void SetNewLandscapeType(byte landscape)
63 _settings_newgame.game_creation.landscape = landscape;
64 InvalidateWindowClassesData(WC_SELECT_GAME);
65 InvalidateWindowClassesData(WC_GENERATE_LANDSCAPE);
68 /** Widgets of GenerateLandscapeWindow when generating world */
69 static const NWidgetPart _nested_generate_landscape_widgets[] = {
70 NWidget(NWID_HORIZONTAL),
71 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
72 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MAPGEN_WORLD_GENERATION_CAPTION, STR_NULL),
73 EndContainer(),
74 NWidget(WWT_PANEL, COLOUR_BROWN),
75 NWidget(NWID_SPACER), SetMinimalSize(0, 10),
76 /* Landscape selection. */
77 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
78 NWidget(NWID_SPACER), SetFill(1, 0),
79 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TEMPERATE), SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
80 NWidget(NWID_SPACER), SetFill(1, 0),
81 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_ARCTIC), SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
82 NWidget(NWID_SPACER), SetFill(1, 0),
83 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TROPICAL), SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
84 NWidget(NWID_SPACER), SetFill(1, 0),
85 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TOYLAND), SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
86 NWidget(NWID_SPACER), SetFill(1, 0),
87 EndContainer(),
88 NWidget(NWID_SPACER), SetMinimalSize(0, 11),
89 NWidget(NWID_HORIZONTAL), SetPIP(10, 5, 10),
90 NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0),
91 /* Left column with labels. */
92 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
93 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAPSIZE, STR_MAPGEN_MAPSIZE_TOOLTIP), SetFill(1, 1),
94 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_LAND_GENERATOR, STR_NULL), SetFill(1, 1),
95 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_TERRAIN_TYPE, STR_NULL), SetFill(1, 1),
96 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_VARIETY, STR_NULL), SetFill(1, 1),
97 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_QUANTITY_OF_SEA_LAKES, STR_NULL), SetFill(1, 1),
98 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_TOWNS, STR_NULL), SetFill(1, 1),
99 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_INDUSTRIES, STR_NULL), SetFill(1, 1),
100 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BORDER_TYPE, STR_NULL), SetFill(1, 1),
101 EndContainer(),
102 /* Widgets at the right of the labels. */
103 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
104 /* Mapsize X * Y. */
105 NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
106 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_X_PULLDOWN), SetDataTip(STR_JUST_INT, STR_MAPGEN_MAPSIZE_TOOLTIP), SetFill(1, 0),
107 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BY, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(1, 1),
108 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_Y_PULLDOWN), SetDataTip(STR_JUST_INT, STR_MAPGEN_MAPSIZE_TOOLTIP), SetFill(1, 0),
109 EndContainer(),
110 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_LANDSCAPE_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
111 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TERRAIN_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
112 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_VARIETY_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
113 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_WATER_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
114 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TOWN_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
115 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_INDUSTRY_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
116 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_BORDERS_RANDOM), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
117 EndContainer(),
118 EndContainer(),
119 NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
120 NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0),
121 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
122 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_GL_CLIMATE_SEL_LABEL),
123 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SNOW_COVERAGE, STR_NULL), SetFill(1, 1),
124 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DESERT_COVERAGE, STR_NULL), SetFill(1, 1),
125 NWidget(NWID_SPACER),
126 EndContainer(),
127 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DATE, STR_NULL), SetFill(1, 1),
128 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SMOOTHNESS, STR_NULL), SetFill(1, 1),
129 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_QUANTITY_OF_RIVERS, STR_NULL), SetFill(1, 1),
130 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_GAME_OPTIONS_TOWN_NAMES_FRAME, STR_NULL), SetFill(1, 1),
131 EndContainer(),
132 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
133 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_GL_CLIMATE_SEL_SELECTOR),
134 /* Snow coverage. */
135 NWidget(NWID_HORIZONTAL),
136 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_COVERAGE_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_SNOW_COVERAGE_DOWN), SetFill(0, 1),
137 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_SNOW_COVERAGE_TEXT), SetDataTip(STR_MAPGEN_SNOW_COVERAGE_TEXT, STR_NULL), SetFill(1, 0),
138 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_COVERAGE_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_SNOW_COVERAGE_UP), SetFill(0, 1),
139 EndContainer(),
140 /* Desert coverage. */
141 NWidget(NWID_HORIZONTAL),
142 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_DESERT_COVERAGE_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_DESERT_COVERAGE_DOWN), SetFill(0, 1),
143 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_DESERT_COVERAGE_TEXT), SetDataTip(STR_MAPGEN_DESERT_COVERAGE_TEXT, STR_NULL), SetFill(1, 0),
144 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_DESERT_COVERAGE_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_DESERT_COVERAGE_UP), SetFill(0, 1),
145 EndContainer(),
146 /* Temperate/Toyland spacer */
147 NWidget(NWID_SPACER),
148 EndContainer(),
149 /* Starting date. */
150 NWidget(NWID_HORIZONTAL),
151 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),
152 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_GL_START_DATE_TEXT), SetDataTip(STR_BLACK_DATE_LONG, STR_NULL), SetFill(1, 0),
153 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),
154 EndContainer(),
155 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_SMOOTHNESS_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
156 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_RIVER_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
157 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TOWNNAME_DROPDOWN), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_TOWN_NAMES_DROPDOWN_TOOLTIP), SetFill(1, 0),
158 EndContainer(),
159 EndContainer(),
160 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_GL_GENERATE_BUTTON), SetMinimalSize(84, 0), SetDataTip(STR_MAPGEN_GENERATE, STR_NULL), SetFill(1, 1),
161 EndContainer(),
162 EndContainer(),
163 NWidget(NWID_SPACER), SetMinimalSize(0, 4),
164 /* Map borders buttons for each edge. */
165 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
166 NWidget(NWID_HORIZONTAL), SetPIP(0, 0, 3),
167 NWidget(NWID_SPACER), SetFill(1, 1),
168 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NORTHWEST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
169 EndContainer(),
170 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_WATER_NW), SetDataTip(STR_JUST_STRING, STR_MAPGEN_NORTHWEST), SetFill(1, 1),
171 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_WATER_NE), SetDataTip(STR_JUST_STRING, STR_MAPGEN_NORTHEAST), SetFill(1, 1),
172 NWidget(NWID_HORIZONTAL), SetPIP(3, 0, 0),
173 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NORTHEAST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
174 NWidget(NWID_SPACER), SetFill(1, 1),
175 EndContainer(),
176 EndContainer(),
177 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
178 NWidget(NWID_HORIZONTAL), SetPIP(0, 0, 3),
179 NWidget(NWID_SPACER), SetFill(1, 1),
180 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SOUTHWEST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
181 EndContainer(),
182 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_WATER_SW), SetDataTip(STR_JUST_STRING, STR_MAPGEN_SOUTHWEST), SetFill(1, 1),
183 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_WATER_SE), SetDataTip(STR_JUST_STRING, STR_MAPGEN_SOUTHEAST), SetFill(1, 1),
184 NWidget(NWID_HORIZONTAL), SetPIP(3, 0, 0),
185 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SOUTHEAST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
186 NWidget(NWID_SPACER), SetFill(1, 1),
187 EndContainer(),
188 EndContainer(),
189 NWidget(NWID_SPACER), SetMinimalSize(0, 9), SetFill(1, 1),
190 EndContainer(),
193 /** Widgets of GenerateLandscapeWindow when loading heightmap */
194 static const NWidgetPart _nested_heightmap_load_widgets[] = {
195 /* Window header. */
196 NWidget(NWID_HORIZONTAL),
197 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
198 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MAPGEN_WORLD_GENERATION_CAPTION, STR_NULL),
199 EndContainer(),
200 NWidget(WWT_PANEL, COLOUR_BROWN),
201 NWidget(NWID_SPACER), SetMinimalSize(0, 10),
202 /* Landscape selection. */
203 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
204 NWidget(NWID_SPACER), SetFill(1, 0),
205 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TEMPERATE), SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
206 NWidget(NWID_SPACER), SetFill(1, 0),
207 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_ARCTIC), SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
208 NWidget(NWID_SPACER), SetFill(1, 0),
209 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TROPICAL), SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
210 NWidget(NWID_SPACER), SetFill(1, 0),
211 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TOYLAND), SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
212 NWidget(NWID_SPACER), SetFill(1, 0),
213 EndContainer(),
214 NWidget(NWID_SPACER), SetMinimalSize(0, 11), SetFill(0, 1),
215 NWidget(NWID_HORIZONTAL), SetPIP(10, 3, 10),
216 /* Labels at the left side. */
217 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
218 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_NAME, STR_NULL), SetFill(1, 1),
219 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_SIZE_LABEL, STR_NULL), SetFill(1, 1),
220 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAPSIZE, STR_NULL), SetFill(1, 1),
221 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_ROTATION, STR_NULL), SetFill(1, 1),
222 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_TOWNS, STR_NULL), SetFill(1, 1),
223 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_INDUSTRIES, STR_NULL), SetFill(1, 1),
224 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_QUANTITY_OF_RIVERS, STR_NULL), SetFill(1, 1),
225 EndContainer(),
226 /* Widgets at the right of the labels. */
227 NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
228 NWidget(WWT_TEXT, COLOUR_ORANGE, WID_GL_HEIGHTMAP_NAME_TEXT), SetTextColour(TC_ORANGE), SetDataTip(STR_JUST_RAW_STRING, STR_EMPTY), SetFill(1, 0),
229 NWidget(NWID_HORIZONTAL), SetPIP(0, 5, 0),
230 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
231 NWidget(WWT_TEXT, COLOUR_ORANGE, WID_GL_HEIGHTMAP_SIZE_TEXT), SetDataTip(STR_MAPGEN_HEIGHTMAP_SIZE, STR_NULL), SetFill(1, 0),
232 /* Mapsize X * Y. */
233 NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
234 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_X_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetFill(1, 0),
235 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BY, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(1, 1),
236 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_Y_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetFill(1, 0),
237 EndContainer(),
238 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_HEIGHTMAP_ROTATION_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
239 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TOWN_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
240 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_INDUSTRY_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
241 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_RIVER_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
242 EndContainer(),
243 NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
244 NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0),
245 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
246 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_HEIGHT, STR_NULL), SetFill(1, 1),
247 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_GL_CLIMATE_SEL_LABEL),
248 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SNOW_COVERAGE, STR_NULL), SetFill(1, 1),
249 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DESERT_COVERAGE, STR_NULL), SetFill(1, 1),
250 NWidget(NWID_SPACER),
251 EndContainer(),
252 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DATE, STR_NULL), SetFill(1, 1),
253 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_GAME_OPTIONS_TOWN_NAMES_FRAME, STR_NULL), SetFill(1, 1),
254 EndContainer(),
255 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
256 NWidget(NWID_HORIZONTAL),
257 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_HEIGHTMAP_HEIGHT_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_HEIGHTMAP_HEIGHT_DOWN), SetFill(0, 1),
258 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_HEIGHTMAP_HEIGHT_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
259 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_HEIGHTMAP_HEIGHT_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_HEIGHTMAP_HEIGHT_UP), SetFill(0, 1),
260 EndContainer(),
261 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_GL_CLIMATE_SEL_SELECTOR),
262 NWidget(NWID_HORIZONTAL),
263 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_COVERAGE_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_SNOW_COVERAGE_DOWN), SetFill(0, 1),
264 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_SNOW_COVERAGE_TEXT), SetDataTip(STR_MAPGEN_SNOW_COVERAGE_TEXT, STR_NULL), SetFill(1, 0),
265 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_COVERAGE_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_SNOW_COVERAGE_UP), SetFill(0, 1),
266 EndContainer(),
267 NWidget(NWID_HORIZONTAL),
268 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_DESERT_COVERAGE_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_DESERT_COVERAGE_DOWN), SetFill(0, 1),
269 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_DESERT_COVERAGE_TEXT), SetDataTip(STR_MAPGEN_DESERT_COVERAGE_TEXT, STR_NULL), SetFill(1, 0),
270 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_DESERT_COVERAGE_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_DESERT_COVERAGE_UP), SetFill(0, 1),
271 EndContainer(),
272 NWidget(NWID_SPACER),
273 EndContainer(),
274 NWidget(NWID_HORIZONTAL),
275 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),
276 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_GL_START_DATE_TEXT), SetDataTip(STR_BLACK_DATE_LONG, STR_NULL), SetFill(1, 0),
277 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),
278 EndContainer(),
279 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TOWNNAME_DROPDOWN), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_TOWN_NAMES_DROPDOWN_TOOLTIP), SetFill(1, 0),
280 EndContainer(),
281 EndContainer(),
282 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_GL_GENERATE_BUTTON), SetMinimalSize(84, 0), SetDataTip(STR_MAPGEN_GENERATE, STR_NULL), SetFill(1, 1),
283 EndContainer(),
284 EndContainer(),
285 EndContainer(),
286 EndContainer(),
287 NWidget(NWID_SPACER), SetMinimalSize(0, 9), SetFill(1, 1),
288 EndContainer(),
291 static void StartGeneratingLandscape(GenerateLandscapeWindowMode mode)
293 CloseAllNonVitalWindows();
294 ClearErrorMessages();
296 /* Copy all XXX_newgame to XXX when coming from outside the editor */
297 MakeNewgameSettingsLive();
298 ResetGRFConfig(true);
300 if (_settings_client.sound.confirm) SndPlayFx(SND_15_BEEP);
301 switch (mode) {
302 case GLWM_GENERATE: _switch_mode = (_game_mode == GM_EDITOR) ? SM_GENRANDLAND : SM_NEWGAME; break;
303 case GLWM_HEIGHTMAP: _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_HEIGHTMAP : SM_START_HEIGHTMAP; break;
304 case GLWM_SCENARIO: _switch_mode = SM_EDITOR; break;
305 default: NOT_REACHED();
309 static void LandscapeGenerationCallback(Window *w, bool confirmed)
311 if (confirmed) StartGeneratingLandscape((GenerateLandscapeWindowMode)w->window_number);
314 static DropDownList BuildMapsizeDropDown()
316 DropDownList list;
318 for (uint i = MIN_MAP_SIZE_BITS; i <= MAX_MAP_SIZE_BITS; i++) {
319 DropDownListParamStringItem *item = new DropDownListParamStringItem(STR_JUST_INT, i, false);
320 item->SetParam(0, 1LL << i);
321 list.emplace_back(item);
324 return list;
327 static DropDownList BuildTownNameDropDown()
329 DropDownList list;
331 /* Add and sort newgrf townnames generators */
332 const auto &grf_names = GetGRFTownNameList();
333 for (uint i = 0; i < grf_names.size(); i++) {
334 list.emplace_back(new DropDownListStringItem(grf_names[i], BUILTIN_TOWNNAME_GENERATOR_COUNT + i, false));
336 std::sort(list.begin(), list.end(), DropDownListStringItem::NatSortFunc);
338 size_t newgrf_size = list.size();
339 /* Insert newgrf_names at the top of the list */
340 if (newgrf_size > 0) {
341 list.emplace_back(new DropDownListItem(-1, false)); // separator line
342 newgrf_size++;
345 /* Add and sort original townnames generators */
346 for (uint i = 0; i < BUILTIN_TOWNNAME_GENERATOR_COUNT; i++) {
347 list.emplace_back(new DropDownListStringItem(STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + i, i, false));
349 std::sort(list.begin() + newgrf_size, list.end(), DropDownListStringItem::NatSortFunc);
351 return list;
355 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, STR_TERRAIN_TYPE_CUSTOM, INVALID_STRING_ID};
356 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};
357 static const StringID _rivers[] = {STR_RIVERS_NONE, STR_RIVERS_FEW, STR_RIVERS_MODERATE, STR_RIVERS_LOT, INVALID_STRING_ID};
358 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};
359 static const StringID _rotation[] = {STR_CONFIG_SETTING_HEIGHTMAP_ROTATION_COUNTER_CLOCKWISE, STR_CONFIG_SETTING_HEIGHTMAP_ROTATION_CLOCKWISE, INVALID_STRING_ID};
360 static const StringID _landscape[] = {STR_CONFIG_SETTING_LAND_GENERATOR_ORIGINAL, STR_CONFIG_SETTING_LAND_GENERATOR_TERRA_GENESIS, INVALID_STRING_ID};
361 static const StringID _num_towns[] = {STR_NUM_VERY_LOW, STR_NUM_LOW, STR_NUM_NORMAL, STR_NUM_HIGH, STR_NUM_CUSTOM, INVALID_STRING_ID};
362 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};
363 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};
365 static_assert(lengthof(_num_inds) == ID_END + 1);
367 struct GenerateLandscapeWindow : public Window {
368 uint widget_id;
369 uint x;
370 uint y;
371 char name[64];
372 GenerateLandscapeWindowMode mode;
374 GenerateLandscapeWindow(WindowDesc *desc, WindowNumber number = 0) : Window(desc)
376 this->InitNested(number);
378 this->LowerWidget(_settings_newgame.game_creation.landscape + WID_GL_TEMPERATE);
380 this->mode = (GenerateLandscapeWindowMode)this->window_number;
382 /* Disable town and industry in SE */
383 this->SetWidgetDisabledState(WID_GL_TOWN_PULLDOWN, _game_mode == GM_EDITOR);
384 this->SetWidgetDisabledState(WID_GL_INDUSTRY_PULLDOWN, _game_mode == GM_EDITOR);
386 /* In case the map_height_limit is changed, clamp heightmap_height and custom_terrain_type. */
387 _settings_newgame.game_creation.heightmap_height = Clamp(_settings_newgame.game_creation.heightmap_height, MIN_HEIGHTMAP_HEIGHT, GetMapHeightLimit());
388 _settings_newgame.game_creation.custom_terrain_type = Clamp(_settings_newgame.game_creation.custom_terrain_type, MIN_CUSTOM_TERRAIN_TYPE, GetMapHeightLimit());
390 this->OnInvalidateData();
394 void SetStringParameters(int widget) const override
396 switch (widget) {
397 case WID_GL_START_DATE_TEXT: SetDParam(0, ConvertYMDToDate(_settings_newgame.game_creation.starting_year, 0, 1)); break;
398 case WID_GL_MAPSIZE_X_PULLDOWN: SetDParam(0, 1LL << _settings_newgame.game_creation.map_x); break;
399 case WID_GL_MAPSIZE_Y_PULLDOWN: SetDParam(0, 1LL << _settings_newgame.game_creation.map_y); break;
400 case WID_GL_HEIGHTMAP_HEIGHT_TEXT: SetDParam(0, _settings_newgame.game_creation.heightmap_height); break;
401 case WID_GL_SNOW_COVERAGE_TEXT: SetDParam(0, _settings_newgame.game_creation.snow_coverage); break;
402 case WID_GL_DESERT_COVERAGE_TEXT: SetDParam(0, _settings_newgame.game_creation.desert_coverage); break;
404 case WID_GL_TOWN_PULLDOWN:
405 if (_game_mode == GM_EDITOR) {
406 SetDParam(0, STR_CONFIG_SETTING_OFF);
407 } else if (_settings_newgame.difficulty.number_towns == CUSTOM_TOWN_NUMBER_DIFFICULTY) {
408 SetDParam(0, STR_NUM_CUSTOM_NUMBER);
409 SetDParam(1, _settings_newgame.game_creation.custom_town_number);
410 } else {
411 SetDParam(0, _num_towns[_settings_newgame.difficulty.number_towns]);
413 break;
415 case WID_GL_TOWNNAME_DROPDOWN: {
416 uint gen = _settings_newgame.game_creation.town_name;
417 StringID name = gen < BUILTIN_TOWNNAME_GENERATOR_COUNT ?
418 STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + gen :
419 GetGRFTownNameName(gen - BUILTIN_TOWNNAME_GENERATOR_COUNT);
420 SetDParam(0, name);
421 break;
424 case WID_GL_INDUSTRY_PULLDOWN: SetDParam(0, _game_mode == GM_EDITOR ? STR_CONFIG_SETTING_OFF : _num_inds[_settings_newgame.difficulty.industry_density]); break;
425 case WID_GL_LANDSCAPE_PULLDOWN: SetDParam(0, _landscape[_settings_newgame.game_creation.land_generator]); break;
426 case WID_GL_TERRAIN_PULLDOWN:
427 if (_settings_newgame.difficulty.terrain_type == CUSTOM_TERRAIN_TYPE_NUMBER_DIFFICULTY) {
428 SetDParam(0, STR_TERRAIN_TYPE_CUSTOM_VALUE);
429 SetDParam(1, _settings_newgame.game_creation.custom_terrain_type);
430 } else {
431 SetDParam(0, _elevations[_settings_newgame.difficulty.terrain_type]); break;
433 break;
435 case WID_GL_WATER_PULLDOWN:
436 if (_settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
437 SetDParam(0, STR_SEA_LEVEL_CUSTOM_PERCENTAGE);
438 SetDParam(1, _settings_newgame.game_creation.custom_sea_level);
439 } else {
440 SetDParam(0, _sea_lakes[_settings_newgame.difficulty.quantity_sea_lakes]);
442 break;
444 case WID_GL_HEIGHTMAP_NAME_TEXT: SetDParamStr(0, this->name); break;
445 case WID_GL_RIVER_PULLDOWN: SetDParam(0, _rivers[_settings_newgame.game_creation.amount_of_rivers]); break;
446 case WID_GL_SMOOTHNESS_PULLDOWN: SetDParam(0, _smoothness[_settings_newgame.game_creation.tgen_smoothness]); break;
447 case WID_GL_VARIETY_PULLDOWN: SetDParam(0, _variety[_settings_newgame.game_creation.variety]); break;
448 case WID_GL_BORDERS_RANDOM: SetDParam(0, (_settings_newgame.game_creation.water_borders == BORDERS_RANDOM) ? STR_MAPGEN_BORDER_RANDOMIZE : STR_MAPGEN_BORDER_MANUAL); break;
449 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;
450 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;
451 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;
452 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;
453 case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: SetDParam(0, _rotation[_settings_newgame.game_creation.heightmap_rotation]); break;
455 case WID_GL_HEIGHTMAP_SIZE_TEXT:
456 if (_settings_newgame.game_creation.heightmap_rotation == HM_CLOCKWISE) {
457 SetDParam(0, this->y);
458 SetDParam(1, this->x);
459 } else {
460 SetDParam(0, this->x);
461 SetDParam(1, this->y);
463 break;
468 * Some data on this window has become invalid.
469 * @param data Information about the changed data.
470 * @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.
472 void OnInvalidateData(int data = 0, bool gui_scope = true) override
474 if (!gui_scope) return;
475 /* Update the climate buttons */
476 this->SetWidgetLoweredState(WID_GL_TEMPERATE, _settings_newgame.game_creation.landscape == LT_TEMPERATE);
477 this->SetWidgetLoweredState(WID_GL_ARCTIC, _settings_newgame.game_creation.landscape == LT_ARCTIC);
478 this->SetWidgetLoweredState(WID_GL_TROPICAL, _settings_newgame.game_creation.landscape == LT_TROPIC);
479 this->SetWidgetLoweredState(WID_GL_TOYLAND, _settings_newgame.game_creation.landscape == LT_TOYLAND);
481 /* You can't select smoothness / non-water borders if not terragenesis */
482 if (mode == GLWM_GENERATE) {
483 this->SetWidgetDisabledState(WID_GL_SMOOTHNESS_PULLDOWN, _settings_newgame.game_creation.land_generator == LG_ORIGINAL);
484 this->SetWidgetDisabledState(WID_GL_VARIETY_PULLDOWN, _settings_newgame.game_creation.land_generator == LG_ORIGINAL);
485 this->SetWidgetDisabledState(WID_GL_BORDERS_RANDOM, _settings_newgame.game_creation.land_generator == LG_ORIGINAL || !_settings_newgame.construction.freeform_edges);
486 this->SetWidgetsDisabledState(_settings_newgame.game_creation.land_generator == LG_ORIGINAL || !_settings_newgame.construction.freeform_edges || _settings_newgame.game_creation.water_borders == BORDERS_RANDOM,
487 WID_GL_WATER_NW, WID_GL_WATER_NE, WID_GL_WATER_SE, WID_GL_WATER_SW, WIDGET_LIST_END);
489 this->SetWidgetLoweredState(WID_GL_BORDERS_RANDOM, _settings_newgame.game_creation.water_borders == BORDERS_RANDOM);
491 this->SetWidgetLoweredState(WID_GL_WATER_NW, HasBit(_settings_newgame.game_creation.water_borders, BORDER_NW));
492 this->SetWidgetLoweredState(WID_GL_WATER_NE, HasBit(_settings_newgame.game_creation.water_borders, BORDER_NE));
493 this->SetWidgetLoweredState(WID_GL_WATER_SE, HasBit(_settings_newgame.game_creation.water_borders, BORDER_SE));
494 this->SetWidgetLoweredState(WID_GL_WATER_SW, HasBit(_settings_newgame.game_creation.water_borders, BORDER_SW));
496 this->SetWidgetsDisabledState(_settings_newgame.game_creation.land_generator == LG_ORIGINAL && (_settings_newgame.game_creation.landscape == LT_ARCTIC || _settings_newgame.game_creation.landscape == LT_TROPIC),
497 WID_GL_TERRAIN_PULLDOWN, WID_GL_WATER_PULLDOWN, WIDGET_LIST_END);
500 /* Disable snowline if not arctic */
501 this->SetWidgetDisabledState(WID_GL_SNOW_COVERAGE_TEXT, _settings_newgame.game_creation.landscape != LT_ARCTIC);
502 /* Disable desert if not tropic */
503 this->SetWidgetDisabledState(WID_GL_DESERT_COVERAGE_TEXT, _settings_newgame.game_creation.landscape != LT_TROPIC);
505 /* Set snow/rainforest selections */
506 int climate_plane = 0;
507 switch (_settings_newgame.game_creation.landscape) {
508 case LT_TEMPERATE: climate_plane = 2; break;
509 case LT_ARCTIC: climate_plane = 0; break;
510 case LT_TROPIC: climate_plane = 1; break;
511 case LT_TOYLAND: climate_plane = 2; break;
513 this->GetWidget<NWidgetStacked>(WID_GL_CLIMATE_SEL_LABEL)->SetDisplayedPlane(climate_plane);
514 this->GetWidget<NWidgetStacked>(WID_GL_CLIMATE_SEL_SELECTOR)->SetDisplayedPlane(climate_plane);
516 /* Update availability of decreasing / increasing start date and snow level */
517 if (mode == GLWM_HEIGHTMAP) {
518 this->SetWidgetDisabledState(WID_GL_HEIGHTMAP_HEIGHT_DOWN, _settings_newgame.game_creation.heightmap_height <= MIN_HEIGHTMAP_HEIGHT);
519 this->SetWidgetDisabledState(WID_GL_HEIGHTMAP_HEIGHT_UP, _settings_newgame.game_creation.heightmap_height >= GetMapHeightLimit());
521 this->SetWidgetDisabledState(WID_GL_START_DATE_DOWN, _settings_newgame.game_creation.starting_year <= MIN_YEAR);
522 this->SetWidgetDisabledState(WID_GL_START_DATE_UP, _settings_newgame.game_creation.starting_year >= MAX_YEAR);
523 this->SetWidgetDisabledState(WID_GL_SNOW_COVERAGE_DOWN, _settings_newgame.game_creation.snow_coverage <= 0 || _settings_newgame.game_creation.landscape != LT_ARCTIC);
524 this->SetWidgetDisabledState(WID_GL_SNOW_COVERAGE_UP, _settings_newgame.game_creation.snow_coverage >= 100 || _settings_newgame.game_creation.landscape != LT_ARCTIC);
525 this->SetWidgetDisabledState(WID_GL_DESERT_COVERAGE_DOWN, _settings_newgame.game_creation.desert_coverage <= 0 || _settings_newgame.game_creation.landscape != LT_TROPIC);
526 this->SetWidgetDisabledState(WID_GL_DESERT_COVERAGE_UP, _settings_newgame.game_creation.desert_coverage >= 100 || _settings_newgame.game_creation.landscape != LT_TROPIC);
528 /* Do not allow a custom sea level or terrain type with the original land generator. */
529 if (_settings_newgame.game_creation.land_generator == LG_ORIGINAL) {
530 if (_settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
531 _settings_newgame.difficulty.quantity_sea_lakes = 1;
533 if (_settings_newgame.difficulty.terrain_type == CUSTOM_TERRAIN_TYPE_NUMBER_DIFFICULTY) {
534 _settings_newgame.difficulty.terrain_type = 1;
540 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
542 const StringID *strs = nullptr;
543 switch (widget) {
544 case WID_GL_HEIGHTMAP_HEIGHT_TEXT:
545 SetDParam(0, MAX_TILE_HEIGHT);
546 *size = GetStringBoundingBox(STR_JUST_INT);
547 break;
549 case WID_GL_START_DATE_TEXT:
550 SetDParam(0, ConvertYMDToDate(MAX_YEAR, 0, 1));
551 *size = maxdim(*size, GetStringBoundingBox(STR_BLACK_DATE_LONG));
552 break;
554 case WID_GL_MAPSIZE_X_PULLDOWN:
555 case WID_GL_MAPSIZE_Y_PULLDOWN:
556 SetDParamMaxValue(0, MAX_MAP_SIZE);
557 *size = maxdim(*size, GetStringBoundingBox(STR_JUST_INT));
558 break;
560 case WID_GL_SNOW_COVERAGE_TEXT:
561 SetDParamMaxValue(0, MAX_TILE_HEIGHT);
562 *size = maxdim(*size, GetStringBoundingBox(STR_MAPGEN_SNOW_COVERAGE_TEXT));
563 break;
565 case WID_GL_DESERT_COVERAGE_TEXT:
566 SetDParamMaxValue(0, MAX_TILE_HEIGHT);
567 *size = maxdim(*size, GetStringBoundingBox(STR_MAPGEN_DESERT_COVERAGE_TEXT));
568 break;
570 case WID_GL_HEIGHTMAP_SIZE_TEXT:
571 SetDParam(0, this->x);
572 SetDParam(1, this->y);
573 *size = maxdim(*size, GetStringBoundingBox(STR_MAPGEN_HEIGHTMAP_SIZE));
574 break;
576 case WID_GL_TOWN_PULLDOWN:
577 strs = _num_towns;
578 SetDParamMaxValue(0, CUSTOM_TOWN_MAX_NUMBER);
579 *size = maxdim(*size, GetStringBoundingBox(STR_NUM_CUSTOM_NUMBER));
580 break;
582 case WID_GL_INDUSTRY_PULLDOWN: strs = _num_inds; break;
583 case WID_GL_LANDSCAPE_PULLDOWN: strs = _landscape; break;
585 case WID_GL_TERRAIN_PULLDOWN:
586 strs = _elevations;
587 SetDParamMaxValue(0, MAX_MAP_HEIGHT_LIMIT);
588 *size = maxdim(*size, GetStringBoundingBox(STR_TERRAIN_TYPE_CUSTOM_VALUE));
589 break;
591 case WID_GL_WATER_PULLDOWN:
592 strs = _sea_lakes;
593 SetDParamMaxValue(0, CUSTOM_SEA_LEVEL_MAX_PERCENTAGE);
594 *size = maxdim(*size, GetStringBoundingBox(STR_SEA_LEVEL_CUSTOM_PERCENTAGE));
595 break;
597 case WID_GL_RIVER_PULLDOWN: strs = _rivers; break;
598 case WID_GL_SMOOTHNESS_PULLDOWN: strs = _smoothness; break;
599 case WID_GL_VARIETY_PULLDOWN: strs = _variety; break;
600 case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: strs = _rotation; break;
601 case WID_GL_BORDERS_RANDOM:
602 *size = maxdim(GetStringBoundingBox(STR_MAPGEN_BORDER_RANDOMIZE), GetStringBoundingBox(STR_MAPGEN_BORDER_MANUAL));
603 break;
605 case WID_GL_WATER_NE:
606 case WID_GL_WATER_NW:
607 case WID_GL_WATER_SE:
608 case WID_GL_WATER_SW:
609 *size = maxdim(GetStringBoundingBox(STR_MAPGEN_BORDER_RANDOM), maxdim(GetStringBoundingBox(STR_MAPGEN_BORDER_WATER), GetStringBoundingBox(STR_MAPGEN_BORDER_FREEFORM)));
610 break;
612 case WID_GL_HEIGHTMAP_NAME_TEXT:
613 size->width = 0;
614 break;
616 default:
617 return;
619 if (strs != nullptr) {
620 while (*strs != INVALID_STRING_ID) {
621 *size = maxdim(*size, GetStringBoundingBox(*strs++));
624 size->width += padding.width;
625 size->height = std::max(size->height, (uint)(FONT_HEIGHT_NORMAL + WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM));
628 void OnClick(Point pt, int widget, int click_count) override
630 switch (widget) {
631 case WID_GL_TEMPERATE:
632 case WID_GL_ARCTIC:
633 case WID_GL_TROPICAL:
634 case WID_GL_TOYLAND:
635 SetNewLandscapeType(widget - WID_GL_TEMPERATE);
636 break;
638 case WID_GL_MAPSIZE_X_PULLDOWN: // Mapsize X
639 ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_x, WID_GL_MAPSIZE_X_PULLDOWN);
640 break;
642 case WID_GL_MAPSIZE_Y_PULLDOWN: // Mapsize Y
643 ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_y, WID_GL_MAPSIZE_Y_PULLDOWN);
644 break;
646 case WID_GL_TOWN_PULLDOWN: // Number of towns
647 ShowDropDownMenu(this, _num_towns, _settings_newgame.difficulty.number_towns, WID_GL_TOWN_PULLDOWN, 0, 0);
648 break;
650 case WID_GL_TOWNNAME_DROPDOWN: // Townname generator
651 ShowDropDownList(this, BuildTownNameDropDown(), _settings_newgame.game_creation.town_name, WID_GL_TOWNNAME_DROPDOWN);
652 break;
654 case WID_GL_INDUSTRY_PULLDOWN: // Number of industries
655 ShowDropDownMenu(this, _num_inds, _settings_newgame.difficulty.industry_density, WID_GL_INDUSTRY_PULLDOWN, 0, 0);
656 break;
658 case WID_GL_GENERATE_BUTTON: { // Generate
659 /* Get rotated map size. */
660 uint map_x;
661 uint map_y;
662 if (_settings_newgame.game_creation.heightmap_rotation == HM_CLOCKWISE) {
663 map_x = this->y;
664 map_y = this->x;
665 } else {
666 map_x = this->x;
667 map_y = this->y;
669 if (mode == GLWM_HEIGHTMAP &&
670 (map_x * 2 < (1U << _settings_newgame.game_creation.map_x) ||
671 map_x / 2 > (1U << _settings_newgame.game_creation.map_x) ||
672 map_y * 2 < (1U << _settings_newgame.game_creation.map_y) ||
673 map_y / 2 > (1U << _settings_newgame.game_creation.map_y))) {
674 ShowQuery(
675 STR_WARNING_HEIGHTMAP_SCALE_CAPTION,
676 STR_WARNING_HEIGHTMAP_SCALE_MESSAGE,
677 this,
678 LandscapeGenerationCallback);
679 } else {
680 StartGeneratingLandscape(mode);
682 break;
685 case WID_GL_HEIGHTMAP_HEIGHT_DOWN:
686 case WID_GL_HEIGHTMAP_HEIGHT_UP: // Height level buttons
687 /* Don't allow too fast scrolling */
688 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
689 this->HandleButtonClick(widget);
691 _settings_newgame.game_creation.heightmap_height = Clamp(_settings_newgame.game_creation.heightmap_height + widget - WID_GL_HEIGHTMAP_HEIGHT_TEXT, MIN_HEIGHTMAP_HEIGHT, GetMapHeightLimit());
692 this->InvalidateData();
694 _left_button_clicked = false;
695 break;
697 case WID_GL_HEIGHTMAP_HEIGHT_TEXT: // Height level text
698 this->widget_id = WID_GL_HEIGHTMAP_HEIGHT_TEXT;
699 SetDParam(0, _settings_newgame.game_creation.heightmap_height);
700 ShowQueryString(STR_JUST_INT, STR_MAPGEN_HEIGHTMAP_HEIGHT_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
701 break;
704 case WID_GL_START_DATE_DOWN:
705 case WID_GL_START_DATE_UP: // Year buttons
706 /* Don't allow too fast scrolling */
707 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
708 this->HandleButtonClick(widget);
710 _settings_newgame.game_creation.starting_year = Clamp(_settings_newgame.game_creation.starting_year + widget - WID_GL_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
711 this->InvalidateData();
713 _left_button_clicked = false;
714 break;
716 case WID_GL_START_DATE_TEXT: // Year text
717 this->widget_id = WID_GL_START_DATE_TEXT;
718 SetDParam(0, _settings_newgame.game_creation.starting_year);
719 ShowQueryString(STR_JUST_INT, STR_MAPGEN_START_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
720 break;
722 case WID_GL_SNOW_COVERAGE_DOWN:
723 case WID_GL_SNOW_COVERAGE_UP: // Snow coverage buttons
724 /* Don't allow too fast scrolling */
725 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
726 this->HandleButtonClick(widget);
728 _settings_newgame.game_creation.snow_coverage = Clamp(_settings_newgame.game_creation.snow_coverage + (widget - WID_GL_SNOW_COVERAGE_TEXT) * 10, 0, 100);
729 this->InvalidateData();
731 _left_button_clicked = false;
732 break;
734 case WID_GL_SNOW_COVERAGE_TEXT: // Snow coverage text
735 this->widget_id = WID_GL_SNOW_COVERAGE_TEXT;
736 SetDParam(0, _settings_newgame.game_creation.snow_coverage);
737 ShowQueryString(STR_JUST_INT, STR_MAPGEN_SNOW_COVERAGE_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
738 break;
740 case WID_GL_DESERT_COVERAGE_DOWN:
741 case WID_GL_DESERT_COVERAGE_UP: // Desert coverage buttons
742 /* Don't allow too fast scrolling */
743 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
744 this->HandleButtonClick(widget);
746 _settings_newgame.game_creation.desert_coverage = Clamp(_settings_newgame.game_creation.desert_coverage + (widget - WID_GL_DESERT_COVERAGE_TEXT) * 10, 0, 100);
747 this->InvalidateData();
749 _left_button_clicked = false;
750 break;
752 case WID_GL_DESERT_COVERAGE_TEXT: // Desert line text
753 this->widget_id = WID_GL_DESERT_COVERAGE_TEXT;
754 SetDParam(0, _settings_newgame.game_creation.desert_coverage);
755 ShowQueryString(STR_JUST_INT, STR_MAPGEN_DESERT_COVERAGE_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
756 break;
758 case WID_GL_LANDSCAPE_PULLDOWN: // Landscape generator
759 ShowDropDownMenu(this, _landscape, _settings_newgame.game_creation.land_generator, WID_GL_LANDSCAPE_PULLDOWN, 0, 0);
760 break;
762 case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: // Heightmap rotation
763 ShowDropDownMenu(this, _rotation, _settings_newgame.game_creation.heightmap_rotation, WID_GL_HEIGHTMAP_ROTATION_PULLDOWN, 0, 0);
764 break;
766 case WID_GL_TERRAIN_PULLDOWN: // Terrain type
767 /* For the original map generation only the first four are valid. */
768 ShowDropDownMenu(this, _elevations, _settings_newgame.difficulty.terrain_type, WID_GL_TERRAIN_PULLDOWN, 0, _settings_newgame.game_creation.land_generator == LG_ORIGINAL ? ~0xF : 0);
769 break;
771 case WID_GL_WATER_PULLDOWN: { // Water quantity
772 uint32 hidden_mask = 0;
773 /* Disable custom water level when the original map generator is active. */
774 if (_settings_newgame.game_creation.land_generator == LG_ORIGINAL) {
775 SetBit(hidden_mask, CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY);
777 ShowDropDownMenu(this, _sea_lakes, _settings_newgame.difficulty.quantity_sea_lakes, WID_GL_WATER_PULLDOWN, 0, hidden_mask);
778 break;
781 case WID_GL_RIVER_PULLDOWN: // Amount of rivers
782 ShowDropDownMenu(this, _rivers, _settings_newgame.game_creation.amount_of_rivers, WID_GL_RIVER_PULLDOWN, 0, 0);
783 break;
785 case WID_GL_SMOOTHNESS_PULLDOWN: // Map smoothness
786 ShowDropDownMenu(this, _smoothness, _settings_newgame.game_creation.tgen_smoothness, WID_GL_SMOOTHNESS_PULLDOWN, 0, 0);
787 break;
789 case WID_GL_VARIETY_PULLDOWN: // Map variety
790 ShowDropDownMenu(this, _variety, _settings_newgame.game_creation.variety, WID_GL_VARIETY_PULLDOWN, 0, 0);
791 break;
793 /* Freetype map borders */
794 case WID_GL_WATER_NW:
795 _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_NW);
796 this->InvalidateData();
797 break;
799 case WID_GL_WATER_NE:
800 _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_NE);
801 this->InvalidateData();
802 break;
804 case WID_GL_WATER_SE:
805 _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_SE);
806 this->InvalidateData();
807 break;
809 case WID_GL_WATER_SW:
810 _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_SW);
811 this->InvalidateData();
812 break;
814 case WID_GL_BORDERS_RANDOM:
815 _settings_newgame.game_creation.water_borders = (_settings_newgame.game_creation.water_borders == BORDERS_RANDOM) ? 0 : BORDERS_RANDOM;
816 this->InvalidateData();
817 break;
821 void OnTimeout() override
823 static const int newgame_raise_widgets[] = {WID_GL_START_DATE_DOWN, WID_GL_START_DATE_UP, WID_GL_SNOW_COVERAGE_UP, WID_GL_SNOW_COVERAGE_DOWN, WID_GL_DESERT_COVERAGE_UP, WID_GL_DESERT_COVERAGE_DOWN, WIDGET_LIST_END};
824 static const int heightmap_raise_widgets[] = {WID_GL_HEIGHTMAP_HEIGHT_DOWN, WID_GL_HEIGHTMAP_HEIGHT_UP, WID_GL_START_DATE_DOWN, WID_GL_START_DATE_UP, WID_GL_SNOW_COVERAGE_UP, WID_GL_SNOW_COVERAGE_DOWN, WID_GL_DESERT_COVERAGE_UP, WID_GL_DESERT_COVERAGE_DOWN, WIDGET_LIST_END};
826 const int *widget = (mode == GLWM_HEIGHTMAP) ? heightmap_raise_widgets : newgame_raise_widgets;
828 for (; *widget != WIDGET_LIST_END; widget++) {
829 if (this->IsWidgetLowered(*widget)) {
830 this->RaiseWidget(*widget);
831 this->SetWidgetDirty(*widget);
836 void OnDropdownSelect(int widget, int index) override
838 switch (widget) {
839 case WID_GL_MAPSIZE_X_PULLDOWN: _settings_newgame.game_creation.map_x = index; break;
840 case WID_GL_MAPSIZE_Y_PULLDOWN: _settings_newgame.game_creation.map_y = 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_TOWNNAME_DROPDOWN: // Town names
864 if (_game_mode == GM_MENU || Town::GetNumItems() == 0) {
865 _settings_newgame.game_creation.town_name = index;
866 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_OPTIONS);
868 break;
870 case WID_GL_INDUSTRY_PULLDOWN: _settings_newgame.difficulty.industry_density = index; break;
871 case WID_GL_TERRAIN_PULLDOWN: {
872 if ((uint)index == CUSTOM_TERRAIN_TYPE_NUMBER_DIFFICULTY) {
873 this->widget_id = widget;
874 SetDParam(0, _settings_newgame.game_creation.custom_terrain_type);
875 ShowQueryString(STR_JUST_INT, STR_MAPGEN_TERRAIN_TYPE_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_NONE);
877 _settings_newgame.difficulty.terrain_type = index;
878 break;
881 case WID_GL_WATER_PULLDOWN: {
882 if ((uint)index == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
883 this->widget_id = widget;
884 SetDParam(0, _settings_newgame.game_creation.custom_sea_level);
885 ShowQueryString(STR_JUST_INT, STR_MAPGEN_QUANTITY_OF_SEA_LAKES, 3, this, CS_NUMERAL, QSF_NONE);
887 _settings_newgame.difficulty.quantity_sea_lakes = index;
888 break;
891 this->InvalidateData();
894 void OnQueryTextFinished(char *str) override
896 /* Was 'cancel' pressed? */
897 if (str == nullptr) return;
899 int32 value;
900 if (!StrEmpty(str)) {
901 value = atoi(str);
902 } else {
903 /* An empty string means revert to the default */
904 switch (this->widget_id) {
905 case WID_GL_HEIGHTMAP_HEIGHT_TEXT: value = MAP_HEIGHT_LIMIT_AUTO_MINIMUM; break;
906 case WID_GL_START_DATE_TEXT: value = DEF_START_YEAR; break;
907 case WID_GL_SNOW_COVERAGE_TEXT: value = DEF_SNOW_COVERAGE; break;
908 case WID_GL_DESERT_COVERAGE_TEXT: value = DEF_DESERT_COVERAGE; break;
909 case WID_GL_TOWN_PULLDOWN: value = 1; break;
910 case WID_GL_TERRAIN_PULLDOWN: value = MIN_MAP_HEIGHT_LIMIT; break;
911 case WID_GL_WATER_PULLDOWN: value = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE; break;
912 default: NOT_REACHED();
916 switch (this->widget_id) {
917 case WID_GL_HEIGHTMAP_HEIGHT_TEXT:
918 this->SetWidgetDirty(WID_GL_HEIGHTMAP_HEIGHT_TEXT);
919 _settings_newgame.game_creation.heightmap_height = Clamp(value, MIN_HEIGHTMAP_HEIGHT, GetMapHeightLimit());
920 break;
922 case WID_GL_START_DATE_TEXT:
923 this->SetWidgetDirty(WID_GL_START_DATE_TEXT);
924 _settings_newgame.game_creation.starting_year = Clamp(value, MIN_YEAR, MAX_YEAR);
925 break;
927 case WID_GL_SNOW_COVERAGE_TEXT:
928 this->SetWidgetDirty(WID_GL_SNOW_COVERAGE_TEXT);
929 _settings_newgame.game_creation.snow_coverage = Clamp(value, 0, 100);
930 break;
932 case WID_GL_DESERT_COVERAGE_TEXT:
933 this->SetWidgetDirty(WID_GL_DESERT_COVERAGE_TEXT);
934 _settings_newgame.game_creation.desert_coverage = Clamp(value, 0, 100);
935 break;
937 case WID_GL_TOWN_PULLDOWN:
938 _settings_newgame.game_creation.custom_town_number = Clamp(value, 1, CUSTOM_TOWN_MAX_NUMBER);
939 break;
941 case WID_GL_TERRAIN_PULLDOWN:
942 _settings_newgame.game_creation.custom_terrain_type = Clamp(value, MIN_CUSTOM_TERRAIN_TYPE, GetMapHeightLimit());
943 break;
945 case WID_GL_WATER_PULLDOWN:
946 _settings_newgame.game_creation.custom_sea_level = Clamp(value, CUSTOM_SEA_LEVEL_MIN_PERCENTAGE, CUSTOM_SEA_LEVEL_MAX_PERCENTAGE);
947 break;
950 this->InvalidateData();
954 static WindowDesc _generate_landscape_desc(
955 WDP_CENTER, nullptr, 0, 0,
956 WC_GENERATE_LANDSCAPE, WC_NONE,
958 _nested_generate_landscape_widgets, lengthof(_nested_generate_landscape_widgets)
961 static WindowDesc _heightmap_load_desc(
962 WDP_CENTER, nullptr, 0, 0,
963 WC_GENERATE_LANDSCAPE, WC_NONE,
965 _nested_heightmap_load_widgets, lengthof(_nested_heightmap_load_widgets)
968 static void _ShowGenerateLandscape(GenerateLandscapeWindowMode mode)
970 uint x = 0;
971 uint y = 0;
973 CloseWindowByClass(WC_GENERATE_LANDSCAPE);
975 /* Generate a new seed when opening the window */
976 _settings_newgame.game_creation.generation_seed = InteractiveRandom();
978 if (mode == GLWM_HEIGHTMAP) {
979 /* If the function returns negative, it means there was a problem loading the heightmap */
980 if (!GetHeightmapDimensions(_file_to_saveload.detail_ftype, _file_to_saveload.name.c_str(), &x, &y)) return;
983 WindowDesc *desc = (mode == GLWM_HEIGHTMAP) ? &_heightmap_load_desc : &_generate_landscape_desc;
984 GenerateLandscapeWindow *w = AllocateWindowDescFront<GenerateLandscapeWindow>(desc, mode, true);
986 if (mode == GLWM_HEIGHTMAP) {
987 w->x = x;
988 w->y = y;
989 strecpy(w->name, _file_to_saveload.title, lastof(w->name));
992 SetWindowDirty(WC_GENERATE_LANDSCAPE, mode);
995 /** Start with a normal game. */
996 void ShowGenerateLandscape()
998 _ShowGenerateLandscape(GLWM_GENERATE);
1001 /** Start with loading a heightmap. */
1002 void ShowHeightmapLoad()
1004 _ShowGenerateLandscape(GLWM_HEIGHTMAP);
1007 /** Start with a scenario editor. */
1008 void StartScenarioEditor()
1010 StartGeneratingLandscape(GLWM_SCENARIO);
1014 * Start a normal game without the GUI.
1015 * @param seed The seed of the new game.
1017 void StartNewGameWithoutGUI(uint32 seed)
1019 /* GenerateWorld takes care of the possible GENERATE_NEW_SEED value in 'seed' */
1020 _settings_newgame.game_creation.generation_seed = seed;
1022 StartGeneratingLandscape(GLWM_GENERATE);
1025 struct CreateScenarioWindow : public Window
1027 uint widget_id;
1029 CreateScenarioWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
1031 this->InitNested(window_number);
1032 this->LowerWidget(_settings_newgame.game_creation.landscape + WID_CS_TEMPERATE);
1035 void SetStringParameters(int widget) const override
1037 switch (widget) {
1038 case WID_CS_START_DATE_TEXT:
1039 SetDParam(0, ConvertYMDToDate(_settings_newgame.game_creation.starting_year, 0, 1));
1040 break;
1042 case WID_CS_MAPSIZE_X_PULLDOWN:
1043 SetDParam(0, 1LL << _settings_newgame.game_creation.map_x);
1044 break;
1046 case WID_CS_MAPSIZE_Y_PULLDOWN:
1047 SetDParam(0, 1LL << _settings_newgame.game_creation.map_y);
1048 break;
1050 case WID_CS_FLAT_LAND_HEIGHT_TEXT:
1051 SetDParam(0, _settings_newgame.game_creation.se_flat_world_height);
1052 break;
1056 void OnPaint() override
1058 this->SetWidgetDisabledState(WID_CS_START_DATE_DOWN, _settings_newgame.game_creation.starting_year <= MIN_YEAR);
1059 this->SetWidgetDisabledState(WID_CS_START_DATE_UP, _settings_newgame.game_creation.starting_year >= MAX_YEAR);
1060 this->SetWidgetDisabledState(WID_CS_FLAT_LAND_HEIGHT_DOWN, _settings_newgame.game_creation.se_flat_world_height <= 0);
1061 this->SetWidgetDisabledState(WID_CS_FLAT_LAND_HEIGHT_UP, _settings_newgame.game_creation.se_flat_world_height >= GetMapHeightLimit());
1063 this->SetWidgetLoweredState(WID_CS_TEMPERATE, _settings_newgame.game_creation.landscape == LT_TEMPERATE);
1064 this->SetWidgetLoweredState(WID_CS_ARCTIC, _settings_newgame.game_creation.landscape == LT_ARCTIC);
1065 this->SetWidgetLoweredState(WID_CS_TROPICAL, _settings_newgame.game_creation.landscape == LT_TROPIC);
1066 this->SetWidgetLoweredState(WID_CS_TOYLAND, _settings_newgame.game_creation.landscape == LT_TOYLAND);
1068 this->DrawWidgets();
1071 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1073 StringID str = STR_JUST_INT;
1074 switch (widget) {
1075 case WID_CS_START_DATE_TEXT:
1076 SetDParam(0, ConvertYMDToDate(MAX_YEAR, 0, 1));
1077 str = STR_BLACK_DATE_LONG;
1078 break;
1080 case WID_CS_MAPSIZE_X_PULLDOWN:
1081 case WID_CS_MAPSIZE_Y_PULLDOWN:
1082 SetDParamMaxValue(0, MAX_MAP_SIZE);
1083 break;
1085 case WID_CS_FLAT_LAND_HEIGHT_TEXT:
1086 SetDParamMaxValue(0, MAX_TILE_HEIGHT);
1087 break;
1089 default:
1090 return;
1092 *size = maxdim(*size, GetStringBoundingBox(str));
1093 size->width += padding.width;
1094 size->height += padding.height;
1097 void OnClick(Point pt, int widget, int click_count) override
1099 switch (widget) {
1100 case WID_CS_TEMPERATE:
1101 case WID_CS_ARCTIC:
1102 case WID_CS_TROPICAL:
1103 case WID_CS_TOYLAND:
1104 this->RaiseWidget(_settings_newgame.game_creation.landscape + WID_CS_TEMPERATE);
1105 SetNewLandscapeType(widget - WID_CS_TEMPERATE);
1106 break;
1108 case WID_CS_MAPSIZE_X_PULLDOWN: // Mapsize X
1109 ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_x, WID_CS_MAPSIZE_X_PULLDOWN);
1110 break;
1112 case WID_CS_MAPSIZE_Y_PULLDOWN: // Mapsize Y
1113 ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_y, WID_CS_MAPSIZE_Y_PULLDOWN);
1114 break;
1116 case WID_CS_EMPTY_WORLD: // Empty world / flat world
1117 StartGeneratingLandscape(GLWM_SCENARIO);
1118 break;
1120 case WID_CS_RANDOM_WORLD: // Generate
1121 ShowGenerateLandscape();
1122 break;
1124 case WID_CS_START_DATE_DOWN:
1125 case WID_CS_START_DATE_UP: // Year buttons
1126 /* Don't allow too fast scrolling */
1127 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
1128 this->HandleButtonClick(widget);
1129 this->SetDirty();
1131 _settings_newgame.game_creation.starting_year = Clamp(_settings_newgame.game_creation.starting_year + widget - WID_CS_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
1133 _left_button_clicked = false;
1134 break;
1136 case WID_CS_START_DATE_TEXT: // Year text
1137 this->widget_id = WID_CS_START_DATE_TEXT;
1138 SetDParam(0, _settings_newgame.game_creation.starting_year);
1139 ShowQueryString(STR_JUST_INT, STR_MAPGEN_START_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_NONE);
1140 break;
1142 case WID_CS_FLAT_LAND_HEIGHT_DOWN:
1143 case WID_CS_FLAT_LAND_HEIGHT_UP: // Height level buttons
1144 /* Don't allow too fast scrolling */
1145 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
1146 this->HandleButtonClick(widget);
1147 this->SetDirty();
1149 _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, GetMapHeightLimit());
1151 _left_button_clicked = false;
1152 break;
1154 case WID_CS_FLAT_LAND_HEIGHT_TEXT: // Height level text
1155 this->widget_id = WID_CS_FLAT_LAND_HEIGHT_TEXT;
1156 SetDParam(0, _settings_newgame.game_creation.se_flat_world_height);
1157 ShowQueryString(STR_JUST_INT, STR_SE_MAPGEN_FLAT_WORLD_HEIGHT_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_NONE);
1158 break;
1162 void OnTimeout() override
1164 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};
1165 for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
1166 if (this->IsWidgetLowered(*widget)) {
1167 this->RaiseWidget(*widget);
1168 this->SetWidgetDirty(*widget);
1173 void OnDropdownSelect(int widget, int index) override
1175 switch (widget) {
1176 case WID_CS_MAPSIZE_X_PULLDOWN: _settings_newgame.game_creation.map_x = index; break;
1177 case WID_CS_MAPSIZE_Y_PULLDOWN: _settings_newgame.game_creation.map_y = index; break;
1179 this->SetDirty();
1182 void OnQueryTextFinished(char *str) override
1184 if (!StrEmpty(str)) {
1185 int32 value = atoi(str);
1187 switch (this->widget_id) {
1188 case WID_CS_START_DATE_TEXT:
1189 this->SetWidgetDirty(WID_CS_START_DATE_TEXT);
1190 _settings_newgame.game_creation.starting_year = Clamp(value, MIN_YEAR, MAX_YEAR);
1191 break;
1193 case WID_CS_FLAT_LAND_HEIGHT_TEXT:
1194 this->SetWidgetDirty(WID_CS_FLAT_LAND_HEIGHT_TEXT);
1195 _settings_newgame.game_creation.se_flat_world_height = Clamp(value, 0, GetMapHeightLimit());
1196 break;
1199 this->SetDirty();
1204 static const NWidgetPart _nested_create_scenario_widgets[] = {
1205 NWidget(NWID_HORIZONTAL),
1206 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1207 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SE_MAPGEN_CAPTION, STR_NULL),
1208 EndContainer(),
1209 NWidget(WWT_PANEL, COLOUR_BROWN),
1210 NWidget(NWID_SPACER), SetMinimalSize(0, 10),
1211 /* Landscape style selection. */
1212 NWidget(NWID_HORIZONTAL), SetPIP(10, 3, 10),
1213 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_TEMPERATE), SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
1214 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_ARCTIC), SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
1215 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_TROPICAL), SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
1216 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_TOYLAND), SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
1217 EndContainer(),
1218 NWidget(NWID_HORIZONTAL), SetPIP(10, 8, 10),
1219 /* Green generation type buttons: 'Flat land' and 'Random land'. */
1220 NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
1221 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_CS_EMPTY_WORLD), SetDataTip(STR_SE_MAPGEN_FLAT_WORLD, STR_SE_MAPGEN_FLAT_WORLD_TOOLTIP), SetFill(1, 1),
1222 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_CS_RANDOM_WORLD), SetDataTip(STR_SE_MAPGEN_RANDOM_LAND, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetFill(1, 1),
1223 EndContainer(),
1224 /* Labels + setting drop-downs */
1225 NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
1226 /* Map size. */
1227 NWidget(NWID_HORIZONTAL),
1228 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAPSIZE, STR_NULL), SetPadding(1, 0, 0, 0),
1229 NWidget(NWID_SPACER), SetMinimalSize(6, 0), SetFill(1, 0),
1230 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_CS_MAPSIZE_X_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetPadding(0, 4, 0, 0),
1231 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BY, STR_NULL), SetPadding(1, 2, 0, 0),
1232 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_CS_MAPSIZE_Y_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL),
1233 EndContainer(),
1234 /* Date. */
1235 NWidget(NWID_HORIZONTAL),
1236 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DATE, STR_NULL), SetPadding(1, 0, 0, 0),
1237 NWidget(NWID_SPACER), SetMinimalSize(6, 0), SetFill(1, 0),
1238 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),
1239 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_CS_START_DATE_TEXT), SetDataTip(STR_BLACK_DATE_LONG, STR_NULL),
1240 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),
1241 EndContainer(),
1242 /* Flat map height. */
1243 NWidget(NWID_HORIZONTAL),
1244 NWidget(WWT_TEXT, COLOUR_ORANGE),
1245 SetDataTip(STR_SE_MAPGEN_FLAT_WORLD_HEIGHT, STR_NULL), SetPadding(1, 0, 0, 0),
1246 NWidget(NWID_SPACER), SetMinimalSize(6, 0), SetFill(1, 0),
1247 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),
1248 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_CS_FLAT_LAND_HEIGHT_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL),
1249 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),
1250 EndContainer(),
1251 EndContainer(),
1252 EndContainer(),
1253 EndContainer(),
1256 static WindowDesc _create_scenario_desc(
1257 WDP_CENTER, nullptr, 0, 0,
1258 WC_GENERATE_LANDSCAPE, WC_NONE,
1260 _nested_create_scenario_widgets, lengthof(_nested_create_scenario_widgets)
1263 /** Show the window to create a scenario. */
1264 void ShowCreateScenario()
1266 CloseWindowByClass(WC_GENERATE_LANDSCAPE);
1267 new CreateScenarioWindow(&_create_scenario_desc, GLWM_SCENARIO);
1270 static const NWidgetPart _nested_generate_progress_widgets[] = {
1271 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GENERATION_WORLD, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1272 NWidget(WWT_PANEL, COLOUR_GREY),
1273 NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
1274 NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
1275 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GP_PROGRESS_BAR), SetFill(1, 0),
1276 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GP_PROGRESS_TEXT), SetFill(1, 0),
1277 NWidget(WWT_TEXTBTN, COLOUR_WHITE, WID_GP_ABORT), SetDataTip(STR_GENERATION_ABORT, STR_NULL), SetFill(1, 0),
1278 EndContainer(),
1279 EndContainer(),
1280 EndContainer(),
1284 static WindowDesc _generate_progress_desc(
1285 WDP_CENTER, nullptr, 0, 0,
1286 WC_MODAL_PROGRESS, WC_NONE,
1288 _nested_generate_progress_widgets, lengthof(_nested_generate_progress_widgets)
1291 struct GenWorldStatus {
1292 uint percent;
1293 StringID cls;
1294 uint current;
1295 uint total;
1296 std::chrono::steady_clock::time_point next_update;
1299 static GenWorldStatus _gws;
1301 static const StringID _generation_class_table[] = {
1302 STR_GENERATION_WORLD_GENERATION,
1303 STR_SCENEDIT_TOOLBAR_LANDSCAPE_GENERATION,
1304 STR_GENERATION_RIVER_GENERATION,
1305 STR_GENERATION_CLEARING_TILES,
1306 STR_SCENEDIT_TOOLBAR_TOWN_GENERATION,
1307 STR_SCENEDIT_TOOLBAR_INDUSTRY_GENERATION,
1308 STR_GENERATION_OBJECT_GENERATION,
1309 STR_GENERATION_TREE_GENERATION,
1310 STR_GENERATION_SETTINGUP_GAME,
1311 STR_GENERATION_PREPARING_TILELOOP,
1312 STR_GENERATION_PREPARING_SCRIPT,
1313 STR_GENERATION_PREPARING_GAME
1315 static_assert(lengthof(_generation_class_table) == GWP_CLASS_COUNT);
1318 static void AbortGeneratingWorldCallback(Window *w, bool confirmed)
1320 if (confirmed) {
1321 AbortGeneratingWorld();
1322 } else if (HasModalProgress() && !IsGeneratingWorldAborted()) {
1323 SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
1327 struct GenerateProgressWindow : public Window {
1329 GenerateProgressWindow() : Window(&_generate_progress_desc)
1331 this->InitNested();
1334 void OnClick(Point pt, int widget, int click_count) override
1336 switch (widget) {
1337 case WID_GP_ABORT:
1338 SetMouseCursorBusy(false);
1339 ShowQuery(
1340 STR_GENERATION_ABORT_CAPTION,
1341 STR_GENERATION_ABORT_MESSAGE,
1342 this,
1343 AbortGeneratingWorldCallback
1345 break;
1349 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1351 switch (widget) {
1352 case WID_GP_PROGRESS_BAR: {
1353 SetDParamMaxValue(0, 100);
1354 *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
1355 /* We need some spacing for the 'border' */
1356 size->height += 8;
1357 size->width += 8;
1358 break;
1361 case WID_GP_PROGRESS_TEXT:
1362 for (uint i = 0; i < GWP_CLASS_COUNT; i++) {
1363 size->width = std::max(size->width, GetStringBoundingBox(_generation_class_table[i]).width);
1365 size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
1366 break;
1370 void DrawWidget(const Rect &r, int widget) const override
1372 switch (widget) {
1373 case WID_GP_PROGRESS_BAR:
1374 /* Draw the % complete with a bar and a text */
1375 DrawFrameRect(r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
1376 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);
1377 SetDParam(0, _gws.percent);
1378 DrawString(r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
1379 break;
1381 case WID_GP_PROGRESS_TEXT:
1382 /* Tell which class we are generating */
1383 DrawString(r.left, r.right, r.top, _gws.cls, TC_FROMSTRING, SA_HOR_CENTER);
1385 /* And say where we are in that class */
1386 SetDParam(0, _gws.current);
1387 SetDParam(1, _gws.total);
1388 DrawString(r.left, r.right, r.top + FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL, STR_GENERATION_PROGRESS_NUM, TC_FROMSTRING, SA_HOR_CENTER);
1394 * Initializes the progress counters to the starting point.
1396 void PrepareGenerateWorldProgress()
1398 _gws.cls = STR_GENERATION_WORLD_GENERATION;
1399 _gws.current = 0;
1400 _gws.total = 0;
1401 _gws.percent = 0;
1402 _gws.next_update = std::chrono::steady_clock::now();
1406 * Show the window where a user can follow the process of the map generation.
1408 void ShowGenerateWorldProgress()
1410 if (BringWindowToFrontById(WC_MODAL_PROGRESS, 0)) return;
1411 new GenerateProgressWindow();
1414 static void _SetGeneratingWorldProgress(GenWorldProgress cls, uint progress, uint total)
1416 static const int percent_table[] = {0, 5, 14, 17, 20, 40, 60, 65, 80, 85, 95, 99, 100 };
1417 static_assert(lengthof(percent_table) == GWP_CLASS_COUNT + 1);
1418 assert(cls < GWP_CLASS_COUNT);
1420 /* Check if we really are generating the world.
1421 * For example, placing trees via the SE also calls this function, but
1422 * shouldn't try to update the progress.
1424 if (!HasModalProgress()) return;
1426 if (IsGeneratingWorldAborted()) {
1427 HandleGeneratingWorldAbortion();
1428 return;
1431 if (total == 0) {
1432 assert(_gws.cls == _generation_class_table[cls]);
1433 _gws.current += progress;
1434 assert(_gws.current <= _gws.total);
1435 } else {
1436 _gws.cls = _generation_class_table[cls];
1437 _gws.current = progress;
1438 _gws.total = total;
1439 _gws.percent = percent_table[cls];
1442 /* Percentage is about the number of completed tasks, so 'current - 1' */
1443 _gws.percent = percent_table[cls] + (percent_table[cls + 1] - percent_table[cls]) * (_gws.current == 0 ? 0 : _gws.current - 1) / _gws.total;
1445 if (_network_dedicated) {
1446 static uint last_percent = 0;
1448 /* Never display 0% */
1449 if (_gws.percent == 0) return;
1450 /* Reset if percent is lower than the last recorded */
1451 if (_gws.percent < last_percent) last_percent = 0;
1452 /* Display every 5%, but 6% is also very valid.. just not smaller steps than 5% */
1453 if (_gws.percent % 5 != 0 && _gws.percent <= last_percent + 5) return;
1454 /* Never show steps smaller than 2%, even if it is a mod 5% */
1455 if (_gws.percent <= last_percent + 2) return;
1457 Debug(net, 3, "Map generation percentage complete: {}", _gws.percent);
1458 last_percent = _gws.percent;
1460 return;
1463 SetWindowDirty(WC_MODAL_PROGRESS, 0);
1465 VideoDriver::GetInstance()->GameLoopPause();
1469 * Set the total of a stage of the world generation.
1470 * @param cls the current class we are in.
1471 * @param total Set the total expected items for this class.
1473 * Warning: this function isn't clever. Don't go from class 4 to 3. Go upwards, always.
1474 * Also, progress works if total is zero, total works if progress is zero.
1476 void SetGeneratingWorldProgress(GenWorldProgress cls, uint total)
1478 if (total == 0) return;
1480 _SetGeneratingWorldProgress(cls, 0, total);
1484 * Increases the current stage of the world generation with one.
1485 * @param cls the current class we are in.
1487 * Warning: this function isn't clever. Don't go from class 4 to 3. Go upwards, always.
1488 * Also, progress works if total is zero, total works if progress is zero.
1490 void IncreaseGeneratingWorldProgress(GenWorldProgress cls)
1492 /* In fact the param 'class' isn't needed.. but for some security reasons, we want it around */
1493 _SetGeneratingWorldProgress(cls, 1, 0);