Fix: Don't show screenshot GUI in screenshots (#9674)
[openttd-github.git] / src / genworld_gui.cpp
blob2489105c23e7b7c6b48ef91c55714c7c985e195a
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 EndContainer(),
126 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DATE, STR_NULL), SetFill(1, 1),
127 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SMOOTHNESS, STR_NULL), SetFill(1, 1),
128 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_QUANTITY_OF_RIVERS, STR_NULL), SetFill(1, 1),
129 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_GAME_OPTIONS_TOWN_NAMES_FRAME, STR_NULL), SetFill(1, 1),
130 EndContainer(),
131 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
132 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_GL_CLIMATE_SEL_SELECTOR),
133 /* Snow coverage. */
134 NWidget(NWID_HORIZONTAL),
135 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_COVERAGE_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_SNOW_COVERAGE_DOWN), SetFill(0, 1),
136 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_SNOW_COVERAGE_TEXT), SetDataTip(STR_MAPGEN_SNOW_COVERAGE_TEXT, STR_NULL), SetFill(1, 0),
137 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_COVERAGE_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_SNOW_COVERAGE_UP), SetFill(0, 1),
138 EndContainer(),
139 /* Desert coverage. */
140 NWidget(NWID_HORIZONTAL),
141 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_DESERT_COVERAGE_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_DESERT_COVERAGE_DOWN), SetFill(0, 1),
142 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_DESERT_COVERAGE_TEXT), SetDataTip(STR_MAPGEN_DESERT_COVERAGE_TEXT, STR_NULL), SetFill(1, 0),
143 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_DESERT_COVERAGE_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_DESERT_COVERAGE_UP), SetFill(0, 1),
144 EndContainer(),
145 EndContainer(),
146 /* Starting date. */
147 NWidget(NWID_HORIZONTAL),
148 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),
149 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_GL_START_DATE_TEXT), SetDataTip(STR_BLACK_DATE_LONG, STR_NULL), SetFill(1, 0),
150 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),
151 EndContainer(),
152 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_SMOOTHNESS_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
153 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_RIVER_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
154 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TOWNNAME_DROPDOWN), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_TOWN_NAMES_DROPDOWN_TOOLTIP), SetFill(1, 0),
155 EndContainer(),
156 EndContainer(),
157 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_GL_GENERATE_BUTTON), SetMinimalSize(84, 0), SetDataTip(STR_MAPGEN_GENERATE, STR_NULL), SetFill(1, 1),
158 EndContainer(),
159 EndContainer(),
160 NWidget(NWID_SPACER), SetMinimalSize(0, 4),
161 /* Map borders buttons for each edge. */
162 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
163 NWidget(NWID_HORIZONTAL), SetPIP(0, 0, 3),
164 NWidget(NWID_SPACER), SetFill(1, 1),
165 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NORTHWEST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
166 EndContainer(),
167 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_WATER_NW), SetDataTip(STR_JUST_STRING, STR_MAPGEN_NORTHWEST), SetFill(1, 1),
168 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_WATER_NE), SetDataTip(STR_JUST_STRING, STR_MAPGEN_NORTHEAST), SetFill(1, 1),
169 NWidget(NWID_HORIZONTAL), SetPIP(3, 0, 0),
170 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NORTHEAST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
171 NWidget(NWID_SPACER), SetFill(1, 1),
172 EndContainer(),
173 EndContainer(),
174 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
175 NWidget(NWID_HORIZONTAL), SetPIP(0, 0, 3),
176 NWidget(NWID_SPACER), SetFill(1, 1),
177 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SOUTHWEST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
178 EndContainer(),
179 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_WATER_SW), SetDataTip(STR_JUST_STRING, STR_MAPGEN_SOUTHWEST), SetFill(1, 1),
180 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_WATER_SE), SetDataTip(STR_JUST_STRING, STR_MAPGEN_SOUTHEAST), SetFill(1, 1),
181 NWidget(NWID_HORIZONTAL), SetPIP(3, 0, 0),
182 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SOUTHEAST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
183 NWidget(NWID_SPACER), SetFill(1, 1),
184 EndContainer(),
185 EndContainer(),
186 NWidget(NWID_SPACER), SetMinimalSize(0, 9), SetFill(1, 1),
187 EndContainer(),
190 /** Widgets of GenerateLandscapeWindow when loading heightmap */
191 static const NWidgetPart _nested_heightmap_load_widgets[] = {
192 /* Window header. */
193 NWidget(NWID_HORIZONTAL),
194 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
195 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MAPGEN_WORLD_GENERATION_CAPTION, STR_NULL),
196 EndContainer(),
197 NWidget(WWT_PANEL, COLOUR_BROWN),
198 NWidget(NWID_SPACER), SetMinimalSize(0, 10),
199 /* Landscape selection. */
200 NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
201 NWidget(NWID_SPACER), SetFill(1, 0),
202 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TEMPERATE), SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
203 NWidget(NWID_SPACER), SetFill(1, 0),
204 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_ARCTIC), SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
205 NWidget(NWID_SPACER), SetFill(1, 0),
206 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TROPICAL), SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
207 NWidget(NWID_SPACER), SetFill(1, 0),
208 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TOYLAND), SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
209 NWidget(NWID_SPACER), SetFill(1, 0),
210 EndContainer(),
211 NWidget(NWID_SPACER), SetMinimalSize(0, 11), SetFill(0, 1),
212 NWidget(NWID_HORIZONTAL), SetPIP(10, 3, 10),
213 /* Labels at the left side. */
214 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
215 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_NAME, STR_NULL), SetFill(1, 1),
216 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_SIZE_LABEL, STR_NULL), SetFill(1, 1),
217 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAPSIZE, STR_NULL), SetFill(1, 1),
218 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_ROTATION, STR_NULL), SetFill(1, 1),
219 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_TOWNS, STR_NULL), SetFill(1, 1),
220 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_INDUSTRIES, STR_NULL), SetFill(1, 1),
221 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_QUANTITY_OF_RIVERS, STR_NULL), SetFill(1, 1),
222 EndContainer(),
223 /* Widgets at the right of the labels. */
224 NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
225 NWidget(WWT_EMPTY, COLOUR_ORANGE, WID_GL_HEIGHTMAP_NAME_TEXT), SetFill(1, 0),
226 NWidget(NWID_HORIZONTAL), SetPIP(0, 5, 0),
227 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
228 NWidget(WWT_TEXT, COLOUR_ORANGE, WID_GL_HEIGHTMAP_SIZE_TEXT), SetDataTip(STR_MAPGEN_HEIGHTMAP_SIZE, STR_NULL), SetFill(1, 0),
229 /* Mapsize X * Y. */
230 NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
231 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_X_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetFill(1, 0),
232 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BY, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(1, 1),
233 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_Y_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetFill(1, 0),
234 EndContainer(),
235 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_HEIGHTMAP_ROTATION_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
236 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TOWN_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
237 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_INDUSTRY_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
238 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_RIVER_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
239 EndContainer(),
240 NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
241 NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0),
242 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
243 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_HEIGHT, STR_NULL), SetFill(1, 1),
244 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_GL_CLIMATE_SEL_LABEL),
245 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SNOW_COVERAGE, STR_NULL), SetFill(1, 1),
246 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DESERT_COVERAGE, STR_NULL), SetFill(1, 1),
247 EndContainer(),
248 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DATE, STR_NULL), SetFill(1, 1),
249 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_GAME_OPTIONS_TOWN_NAMES_FRAME, STR_NULL), SetFill(1, 1),
250 EndContainer(),
251 NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
252 NWidget(NWID_HORIZONTAL),
253 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_HEIGHTMAP_HEIGHT_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_HEIGHTMAP_HEIGHT_DOWN), SetFill(0, 1),
254 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_HEIGHTMAP_HEIGHT_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
255 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_HEIGHTMAP_HEIGHT_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_HEIGHTMAP_HEIGHT_UP), SetFill(0, 1),
256 EndContainer(),
257 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_GL_CLIMATE_SEL_SELECTOR),
258 NWidget(NWID_HORIZONTAL),
259 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_COVERAGE_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_SNOW_COVERAGE_DOWN), SetFill(0, 1),
260 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_SNOW_COVERAGE_TEXT), SetDataTip(STR_MAPGEN_SNOW_COVERAGE_TEXT, STR_NULL), SetFill(1, 0),
261 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_COVERAGE_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_SNOW_COVERAGE_UP), SetFill(0, 1),
262 EndContainer(),
263 NWidget(NWID_HORIZONTAL),
264 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_DESERT_COVERAGE_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_DESERT_COVERAGE_DOWN), SetFill(0, 1),
265 NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_DESERT_COVERAGE_TEXT), SetDataTip(STR_MAPGEN_DESERT_COVERAGE_TEXT, STR_NULL), SetFill(1, 0),
266 NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_DESERT_COVERAGE_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_DESERT_COVERAGE_UP), SetFill(0, 1),
267 EndContainer(),
268 EndContainer(),
269 NWidget(NWID_HORIZONTAL),
270 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),
271 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_GL_START_DATE_TEXT), SetDataTip(STR_BLACK_DATE_LONG, STR_NULL), SetFill(1, 0),
272 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),
273 EndContainer(),
274 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TOWNNAME_DROPDOWN), SetDataTip(STR_BLACK_STRING, STR_GAME_OPTIONS_TOWN_NAMES_DROPDOWN_TOOLTIP), SetFill(1, 0),
275 EndContainer(),
276 EndContainer(),
277 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_GL_GENERATE_BUTTON), SetMinimalSize(84, 0), SetDataTip(STR_MAPGEN_GENERATE, STR_NULL), SetFill(1, 1),
278 EndContainer(),
279 EndContainer(),
280 EndContainer(),
281 EndContainer(),
282 NWidget(NWID_SPACER), SetMinimalSize(0, 9), SetFill(1, 1),
283 EndContainer(),
286 static void StartGeneratingLandscape(GenerateLandscapeWindowMode mode)
288 CloseAllNonVitalWindows();
289 ClearErrorMessages();
291 /* Copy all XXX_newgame to XXX when coming from outside the editor */
292 MakeNewgameSettingsLive();
293 ResetGRFConfig(true);
295 if (_settings_client.sound.confirm) SndPlayFx(SND_15_BEEP);
296 switch (mode) {
297 case GLWM_GENERATE: _switch_mode = (_game_mode == GM_EDITOR) ? SM_GENRANDLAND : SM_NEWGAME; break;
298 case GLWM_HEIGHTMAP: _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_HEIGHTMAP : SM_START_HEIGHTMAP; break;
299 case GLWM_SCENARIO: _switch_mode = SM_EDITOR; break;
300 default: NOT_REACHED();
304 static void LandscapeGenerationCallback(Window *w, bool confirmed)
306 if (confirmed) StartGeneratingLandscape((GenerateLandscapeWindowMode)w->window_number);
309 static DropDownList BuildMapsizeDropDown()
311 DropDownList list;
313 for (uint i = MIN_MAP_SIZE_BITS; i <= MAX_MAP_SIZE_BITS; i++) {
314 DropDownListParamStringItem *item = new DropDownListParamStringItem(STR_JUST_INT, i, false);
315 item->SetParam(0, 1LL << i);
316 list.emplace_back(item);
319 return list;
322 static DropDownList BuildTownNameDropDown()
324 DropDownList list;
326 /* Add and sort newgrf townnames generators */
327 const auto &grf_names = GetGRFTownNameList();
328 for (uint i = 0; i < grf_names.size(); i++) {
329 list.emplace_back(new DropDownListStringItem(grf_names[i], BUILTIN_TOWNNAME_GENERATOR_COUNT + i, false));
331 std::sort(list.begin(), list.end(), DropDownListStringItem::NatSortFunc);
333 size_t newgrf_size = list.size();
334 /* Insert newgrf_names at the top of the list */
335 if (newgrf_size > 0) {
336 list.emplace_back(new DropDownListItem(-1, false)); // separator line
337 newgrf_size++;
340 /* Add and sort original townnames generators */
341 for (uint i = 0; i < BUILTIN_TOWNNAME_GENERATOR_COUNT; i++) {
342 list.emplace_back(new DropDownListStringItem(STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + i, i, false));
344 std::sort(list.begin() + newgrf_size, list.end(), DropDownListStringItem::NatSortFunc);
346 return list;
350 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};
351 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};
352 static const StringID _rivers[] = {STR_RIVERS_NONE, STR_RIVERS_FEW, STR_RIVERS_MODERATE, STR_RIVERS_LOT, INVALID_STRING_ID};
353 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};
354 static const StringID _rotation[] = {STR_CONFIG_SETTING_HEIGHTMAP_ROTATION_COUNTER_CLOCKWISE, STR_CONFIG_SETTING_HEIGHTMAP_ROTATION_CLOCKWISE, INVALID_STRING_ID};
355 static const StringID _landscape[] = {STR_CONFIG_SETTING_LAND_GENERATOR_ORIGINAL, STR_CONFIG_SETTING_LAND_GENERATOR_TERRA_GENESIS, INVALID_STRING_ID};
356 static const StringID _num_towns[] = {STR_NUM_VERY_LOW, STR_NUM_LOW, STR_NUM_NORMAL, STR_NUM_HIGH, STR_NUM_CUSTOM, INVALID_STRING_ID};
357 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};
358 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};
360 static_assert(lengthof(_num_inds) == ID_END + 1);
362 struct GenerateLandscapeWindow : public Window {
363 uint widget_id;
364 uint x;
365 uint y;
366 char name[64];
367 GenerateLandscapeWindowMode mode;
369 GenerateLandscapeWindow(WindowDesc *desc, WindowNumber number = 0) : Window(desc)
371 this->InitNested(number);
373 this->LowerWidget(_settings_newgame.game_creation.landscape + WID_GL_TEMPERATE);
375 this->mode = (GenerateLandscapeWindowMode)this->window_number;
377 /* Disable town and industry in SE */
378 this->SetWidgetDisabledState(WID_GL_TOWN_PULLDOWN, _game_mode == GM_EDITOR);
379 this->SetWidgetDisabledState(WID_GL_INDUSTRY_PULLDOWN, _game_mode == GM_EDITOR);
381 /* In case the map_height_limit is changed, clamp heightmap_height and custom_terrain_type. */
382 _settings_newgame.game_creation.heightmap_height = Clamp(_settings_newgame.game_creation.heightmap_height, MIN_HEIGHTMAP_HEIGHT, GetMapHeightLimit());
383 _settings_newgame.game_creation.custom_terrain_type = Clamp(_settings_newgame.game_creation.custom_terrain_type, MIN_CUSTOM_TERRAIN_TYPE, GetMapHeightLimit());
385 this->OnInvalidateData();
389 void SetStringParameters(int widget) const override
391 switch (widget) {
392 case WID_GL_START_DATE_TEXT: SetDParam(0, ConvertYMDToDate(_settings_newgame.game_creation.starting_year, 0, 1)); break;
393 case WID_GL_MAPSIZE_X_PULLDOWN: SetDParam(0, 1LL << _settings_newgame.game_creation.map_x); break;
394 case WID_GL_MAPSIZE_Y_PULLDOWN: SetDParam(0, 1LL << _settings_newgame.game_creation.map_y); break;
395 case WID_GL_HEIGHTMAP_HEIGHT_TEXT: SetDParam(0, _settings_newgame.game_creation.heightmap_height); break;
396 case WID_GL_SNOW_COVERAGE_TEXT: SetDParam(0, _settings_newgame.game_creation.snow_coverage); break;
397 case WID_GL_DESERT_COVERAGE_TEXT: SetDParam(0, _settings_newgame.game_creation.desert_coverage); break;
399 case WID_GL_TOWN_PULLDOWN:
400 if (_game_mode == GM_EDITOR) {
401 SetDParam(0, STR_CONFIG_SETTING_OFF);
402 } else if (_settings_newgame.difficulty.number_towns == CUSTOM_TOWN_NUMBER_DIFFICULTY) {
403 SetDParam(0, STR_NUM_CUSTOM_NUMBER);
404 SetDParam(1, _settings_newgame.game_creation.custom_town_number);
405 } else {
406 SetDParam(0, _num_towns[_settings_newgame.difficulty.number_towns]);
408 break;
410 case WID_GL_TOWNNAME_DROPDOWN: {
411 uint gen = _settings_newgame.game_creation.town_name;
412 StringID name = gen < BUILTIN_TOWNNAME_GENERATOR_COUNT ?
413 STR_GAME_OPTIONS_TOWN_NAME_ORIGINAL_ENGLISH + gen :
414 GetGRFTownNameName(gen - BUILTIN_TOWNNAME_GENERATOR_COUNT);
415 SetDParam(0, name);
416 break;
419 case WID_GL_INDUSTRY_PULLDOWN: SetDParam(0, _game_mode == GM_EDITOR ? STR_CONFIG_SETTING_OFF : _num_inds[_settings_newgame.difficulty.industry_density]); break;
420 case WID_GL_LANDSCAPE_PULLDOWN: SetDParam(0, _landscape[_settings_newgame.game_creation.land_generator]); break;
421 case WID_GL_TERRAIN_PULLDOWN:
422 if (_settings_newgame.difficulty.terrain_type == CUSTOM_TERRAIN_TYPE_NUMBER_DIFFICULTY) {
423 SetDParam(0, STR_TERRAIN_TYPE_CUSTOM_VALUE);
424 SetDParam(1, _settings_newgame.game_creation.custom_terrain_type);
425 } else {
426 SetDParam(0, _elevations[_settings_newgame.difficulty.terrain_type]); break;
428 break;
430 case WID_GL_WATER_PULLDOWN:
431 if (_settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
432 SetDParam(0, STR_SEA_LEVEL_CUSTOM_PERCENTAGE);
433 SetDParam(1, _settings_newgame.game_creation.custom_sea_level);
434 } else {
435 SetDParam(0, _sea_lakes[_settings_newgame.difficulty.quantity_sea_lakes]);
437 break;
439 case WID_GL_RIVER_PULLDOWN: SetDParam(0, _rivers[_settings_newgame.game_creation.amount_of_rivers]); break;
440 case WID_GL_SMOOTHNESS_PULLDOWN: SetDParam(0, _smoothness[_settings_newgame.game_creation.tgen_smoothness]); break;
441 case WID_GL_VARIETY_PULLDOWN: SetDParam(0, _variety[_settings_newgame.game_creation.variety]); break;
442 case WID_GL_BORDERS_RANDOM: SetDParam(0, (_settings_newgame.game_creation.water_borders == BORDERS_RANDOM) ? STR_MAPGEN_BORDER_RANDOMIZE : STR_MAPGEN_BORDER_MANUAL); break;
443 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;
444 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;
445 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;
446 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;
447 case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: SetDParam(0, _rotation[_settings_newgame.game_creation.heightmap_rotation]); break;
449 case WID_GL_HEIGHTMAP_SIZE_TEXT:
450 if (_settings_newgame.game_creation.heightmap_rotation == HM_CLOCKWISE) {
451 SetDParam(0, this->y);
452 SetDParam(1, this->x);
453 } else {
454 SetDParam(0, this->x);
455 SetDParam(1, this->y);
457 break;
462 * Some data on this window has become invalid.
463 * @param data Information about the changed data.
464 * @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.
466 void OnInvalidateData(int data = 0, bool gui_scope = true) override
468 if (!gui_scope) return;
469 /* Update the climate buttons */
470 this->SetWidgetLoweredState(WID_GL_TEMPERATE, _settings_newgame.game_creation.landscape == LT_TEMPERATE);
471 this->SetWidgetLoweredState(WID_GL_ARCTIC, _settings_newgame.game_creation.landscape == LT_ARCTIC);
472 this->SetWidgetLoweredState(WID_GL_TROPICAL, _settings_newgame.game_creation.landscape == LT_TROPIC);
473 this->SetWidgetLoweredState(WID_GL_TOYLAND, _settings_newgame.game_creation.landscape == LT_TOYLAND);
475 /* You can't select smoothness / non-water borders if not terragenesis */
476 if (mode == GLWM_GENERATE) {
477 this->SetWidgetDisabledState(WID_GL_SMOOTHNESS_PULLDOWN, _settings_newgame.game_creation.land_generator == LG_ORIGINAL);
478 this->SetWidgetDisabledState(WID_GL_VARIETY_PULLDOWN, _settings_newgame.game_creation.land_generator == LG_ORIGINAL);
479 this->SetWidgetDisabledState(WID_GL_BORDERS_RANDOM, _settings_newgame.game_creation.land_generator == LG_ORIGINAL || !_settings_newgame.construction.freeform_edges);
480 this->SetWidgetsDisabledState(_settings_newgame.game_creation.land_generator == LG_ORIGINAL || !_settings_newgame.construction.freeform_edges || _settings_newgame.game_creation.water_borders == BORDERS_RANDOM,
481 WID_GL_WATER_NW, WID_GL_WATER_NE, WID_GL_WATER_SE, WID_GL_WATER_SW, WIDGET_LIST_END);
483 this->SetWidgetLoweredState(WID_GL_BORDERS_RANDOM, _settings_newgame.game_creation.water_borders == BORDERS_RANDOM);
485 this->SetWidgetLoweredState(WID_GL_WATER_NW, HasBit(_settings_newgame.game_creation.water_borders, BORDER_NW));
486 this->SetWidgetLoweredState(WID_GL_WATER_NE, HasBit(_settings_newgame.game_creation.water_borders, BORDER_NE));
487 this->SetWidgetLoweredState(WID_GL_WATER_SE, HasBit(_settings_newgame.game_creation.water_borders, BORDER_SE));
488 this->SetWidgetLoweredState(WID_GL_WATER_SW, HasBit(_settings_newgame.game_creation.water_borders, BORDER_SW));
490 this->SetWidgetsDisabledState(_settings_newgame.game_creation.land_generator == LG_ORIGINAL && (_settings_newgame.game_creation.landscape == LT_ARCTIC || _settings_newgame.game_creation.landscape == LT_TROPIC),
491 WID_GL_TERRAIN_PULLDOWN, WID_GL_WATER_PULLDOWN, WIDGET_LIST_END);
494 /* Disable snowline if not arctic */
495 this->SetWidgetDisabledState(WID_GL_SNOW_COVERAGE_TEXT, _settings_newgame.game_creation.landscape != LT_ARCTIC);
496 /* Disable desert if not tropic */
497 this->SetWidgetDisabledState(WID_GL_DESERT_COVERAGE_TEXT, _settings_newgame.game_creation.landscape != LT_TROPIC);
499 /* Set snow/rainforest selections */
500 int climate_plane = 0;
501 switch (_settings_newgame.game_creation.landscape) {
502 case LT_TEMPERATE: climate_plane = SZSP_VERTICAL; break;
503 case LT_ARCTIC: climate_plane = 0; break;
504 case LT_TROPIC: climate_plane = 1; break;
505 case LT_TOYLAND: climate_plane = SZSP_VERTICAL; break;
507 this->GetWidget<NWidgetStacked>(WID_GL_CLIMATE_SEL_LABEL)->SetDisplayedPlane(climate_plane);
508 this->GetWidget<NWidgetStacked>(WID_GL_CLIMATE_SEL_SELECTOR)->SetDisplayedPlane(climate_plane);
510 /* Update availability of decreasing / increasing start date and snow level */
511 if (mode == GLWM_HEIGHTMAP) {
512 this->SetWidgetDisabledState(WID_GL_HEIGHTMAP_HEIGHT_DOWN, _settings_newgame.game_creation.heightmap_height <= MIN_HEIGHTMAP_HEIGHT);
513 this->SetWidgetDisabledState(WID_GL_HEIGHTMAP_HEIGHT_UP, _settings_newgame.game_creation.heightmap_height >= GetMapHeightLimit());
515 this->SetWidgetDisabledState(WID_GL_START_DATE_DOWN, _settings_newgame.game_creation.starting_year <= MIN_YEAR);
516 this->SetWidgetDisabledState(WID_GL_START_DATE_UP, _settings_newgame.game_creation.starting_year >= MAX_YEAR);
517 this->SetWidgetDisabledState(WID_GL_SNOW_COVERAGE_DOWN, _settings_newgame.game_creation.snow_coverage <= 0 || _settings_newgame.game_creation.landscape != LT_ARCTIC);
518 this->SetWidgetDisabledState(WID_GL_SNOW_COVERAGE_UP, _settings_newgame.game_creation.snow_coverage >= 100 || _settings_newgame.game_creation.landscape != LT_ARCTIC);
519 this->SetWidgetDisabledState(WID_GL_DESERT_COVERAGE_DOWN, _settings_newgame.game_creation.desert_coverage <= 0 || _settings_newgame.game_creation.landscape != LT_TROPIC);
520 this->SetWidgetDisabledState(WID_GL_DESERT_COVERAGE_UP, _settings_newgame.game_creation.desert_coverage >= 100 || _settings_newgame.game_creation.landscape != LT_TROPIC);
522 /* Do not allow a custom sea level or terrain type with the original land generator. */
523 if (_settings_newgame.game_creation.land_generator == LG_ORIGINAL) {
524 if (_settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
525 _settings_newgame.difficulty.quantity_sea_lakes = 1;
527 if (_settings_newgame.difficulty.terrain_type == CUSTOM_TERRAIN_TYPE_NUMBER_DIFFICULTY) {
528 _settings_newgame.difficulty.terrain_type = 1;
534 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
536 const StringID *strs = nullptr;
537 switch (widget) {
538 case WID_GL_HEIGHTMAP_HEIGHT_TEXT:
539 SetDParam(0, MAX_TILE_HEIGHT);
540 *size = GetStringBoundingBox(STR_JUST_INT);
541 break;
543 case WID_GL_START_DATE_TEXT:
544 SetDParam(0, ConvertYMDToDate(MAX_YEAR, 0, 1));
545 *size = maxdim(*size, GetStringBoundingBox(STR_BLACK_DATE_LONG));
546 break;
548 case WID_GL_MAPSIZE_X_PULLDOWN:
549 case WID_GL_MAPSIZE_Y_PULLDOWN:
550 SetDParamMaxValue(0, MAX_MAP_SIZE);
551 *size = maxdim(*size, GetStringBoundingBox(STR_JUST_INT));
552 break;
554 case WID_GL_SNOW_COVERAGE_TEXT:
555 SetDParamMaxValue(0, MAX_TILE_HEIGHT);
556 *size = maxdim(*size, GetStringBoundingBox(STR_MAPGEN_SNOW_COVERAGE_TEXT));
557 break;
559 case WID_GL_DESERT_COVERAGE_TEXT:
560 SetDParamMaxValue(0, MAX_TILE_HEIGHT);
561 *size = maxdim(*size, GetStringBoundingBox(STR_MAPGEN_DESERT_COVERAGE_TEXT));
562 break;
564 case WID_GL_HEIGHTMAP_SIZE_TEXT:
565 SetDParam(0, this->x);
566 SetDParam(1, this->y);
567 *size = maxdim(*size, GetStringBoundingBox(STR_MAPGEN_HEIGHTMAP_SIZE));
568 break;
570 case WID_GL_TOWN_PULLDOWN:
571 strs = _num_towns;
572 SetDParamMaxValue(0, CUSTOM_TOWN_MAX_NUMBER);
573 *size = maxdim(*size, GetStringBoundingBox(STR_NUM_CUSTOM_NUMBER));
574 break;
576 case WID_GL_INDUSTRY_PULLDOWN: strs = _num_inds; break;
577 case WID_GL_LANDSCAPE_PULLDOWN: strs = _landscape; break;
579 case WID_GL_TERRAIN_PULLDOWN:
580 strs = _elevations;
581 SetDParamMaxValue(0, MAX_MAP_HEIGHT_LIMIT);
582 *size = maxdim(*size, GetStringBoundingBox(STR_TERRAIN_TYPE_CUSTOM_VALUE));
583 break;
585 case WID_GL_WATER_PULLDOWN:
586 strs = _sea_lakes;
587 SetDParamMaxValue(0, CUSTOM_SEA_LEVEL_MAX_PERCENTAGE);
588 *size = maxdim(*size, GetStringBoundingBox(STR_SEA_LEVEL_CUSTOM_PERCENTAGE));
589 break;
591 case WID_GL_RIVER_PULLDOWN: strs = _rivers; break;
592 case WID_GL_SMOOTHNESS_PULLDOWN: strs = _smoothness; break;
593 case WID_GL_VARIETY_PULLDOWN: strs = _variety; break;
594 case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: strs = _rotation; break;
595 case WID_GL_BORDERS_RANDOM:
596 *size = maxdim(GetStringBoundingBox(STR_MAPGEN_BORDER_RANDOMIZE), GetStringBoundingBox(STR_MAPGEN_BORDER_MANUAL));
597 break;
599 case WID_GL_WATER_NE:
600 case WID_GL_WATER_NW:
601 case WID_GL_WATER_SE:
602 case WID_GL_WATER_SW:
603 *size = maxdim(GetStringBoundingBox(STR_MAPGEN_BORDER_RANDOM), maxdim(GetStringBoundingBox(STR_MAPGEN_BORDER_WATER), GetStringBoundingBox(STR_MAPGEN_BORDER_FREEFORM)));
604 break;
606 case WID_GL_HEIGHTMAP_NAME_TEXT:
607 size->width = 0;
608 break;
610 default:
611 return;
613 if (strs != nullptr) {
614 while (*strs != INVALID_STRING_ID) {
615 *size = maxdim(*size, GetStringBoundingBox(*strs++));
618 size->width += padding.width;
619 size->height = std::max(size->height, (uint)(FONT_HEIGHT_NORMAL + WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM));
622 void DrawWidget(const Rect &r, int widget) const override
624 switch (widget) {
625 case WID_GL_HEIGHTMAP_NAME_TEXT: {
626 DrawString(r.left, r.right, r.top, this->name, TC_ORANGE);
627 break;
632 void OnClick(Point pt, int widget, int click_count) override
634 switch (widget) {
635 case WID_GL_TEMPERATE:
636 case WID_GL_ARCTIC:
637 case WID_GL_TROPICAL:
638 case WID_GL_TOYLAND:
639 SetNewLandscapeType(widget - WID_GL_TEMPERATE);
640 break;
642 case WID_GL_MAPSIZE_X_PULLDOWN: // Mapsize X
643 ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_x, WID_GL_MAPSIZE_X_PULLDOWN);
644 break;
646 case WID_GL_MAPSIZE_Y_PULLDOWN: // Mapsize Y
647 ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_y, WID_GL_MAPSIZE_Y_PULLDOWN);
648 break;
650 case WID_GL_TOWN_PULLDOWN: // Number of towns
651 ShowDropDownMenu(this, _num_towns, _settings_newgame.difficulty.number_towns, WID_GL_TOWN_PULLDOWN, 0, 0);
652 break;
654 case WID_GL_TOWNNAME_DROPDOWN: // Townname generator
655 ShowDropDownList(this, BuildTownNameDropDown(), _settings_newgame.game_creation.town_name, WID_GL_TOWNNAME_DROPDOWN);
656 break;
658 case WID_GL_INDUSTRY_PULLDOWN: // Number of industries
659 ShowDropDownMenu(this, _num_inds, _settings_newgame.difficulty.industry_density, WID_GL_INDUSTRY_PULLDOWN, 0, 0);
660 break;
662 case WID_GL_GENERATE_BUTTON: { // Generate
663 /* Get rotated map size. */
664 uint map_x;
665 uint map_y;
666 if (_settings_newgame.game_creation.heightmap_rotation == HM_CLOCKWISE) {
667 map_x = this->y;
668 map_y = this->x;
669 } else {
670 map_x = this->x;
671 map_y = this->y;
673 if (mode == GLWM_HEIGHTMAP &&
674 (map_x * 2 < (1U << _settings_newgame.game_creation.map_x) ||
675 map_x / 2 > (1U << _settings_newgame.game_creation.map_x) ||
676 map_y * 2 < (1U << _settings_newgame.game_creation.map_y) ||
677 map_y / 2 > (1U << _settings_newgame.game_creation.map_y))) {
678 ShowQuery(
679 STR_WARNING_HEIGHTMAP_SCALE_CAPTION,
680 STR_WARNING_HEIGHTMAP_SCALE_MESSAGE,
681 this,
682 LandscapeGenerationCallback);
683 } else {
684 StartGeneratingLandscape(mode);
686 break;
689 case WID_GL_HEIGHTMAP_HEIGHT_DOWN:
690 case WID_GL_HEIGHTMAP_HEIGHT_UP: // Height level buttons
691 /* Don't allow too fast scrolling */
692 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
693 this->HandleButtonClick(widget);
695 _settings_newgame.game_creation.heightmap_height = Clamp(_settings_newgame.game_creation.heightmap_height + widget - WID_GL_HEIGHTMAP_HEIGHT_TEXT, MIN_HEIGHTMAP_HEIGHT, GetMapHeightLimit());
696 this->InvalidateData();
698 _left_button_clicked = false;
699 break;
701 case WID_GL_HEIGHTMAP_HEIGHT_TEXT: // Height level text
702 this->widget_id = WID_GL_HEIGHTMAP_HEIGHT_TEXT;
703 SetDParam(0, _settings_newgame.game_creation.heightmap_height);
704 ShowQueryString(STR_JUST_INT, STR_MAPGEN_HEIGHTMAP_HEIGHT_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
705 break;
708 case WID_GL_START_DATE_DOWN:
709 case WID_GL_START_DATE_UP: // Year buttons
710 /* Don't allow too fast scrolling */
711 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
712 this->HandleButtonClick(widget);
714 _settings_newgame.game_creation.starting_year = Clamp(_settings_newgame.game_creation.starting_year + widget - WID_GL_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
715 this->InvalidateData();
717 _left_button_clicked = false;
718 break;
720 case WID_GL_START_DATE_TEXT: // Year text
721 this->widget_id = WID_GL_START_DATE_TEXT;
722 SetDParam(0, _settings_newgame.game_creation.starting_year);
723 ShowQueryString(STR_JUST_INT, STR_MAPGEN_START_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
724 break;
726 case WID_GL_SNOW_COVERAGE_DOWN:
727 case WID_GL_SNOW_COVERAGE_UP: // Snow coverage buttons
728 /* Don't allow too fast scrolling */
729 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
730 this->HandleButtonClick(widget);
732 _settings_newgame.game_creation.snow_coverage = Clamp(_settings_newgame.game_creation.snow_coverage + (widget - WID_GL_SNOW_COVERAGE_TEXT) * 10, 0, 100);
733 this->InvalidateData();
735 _left_button_clicked = false;
736 break;
738 case WID_GL_SNOW_COVERAGE_TEXT: // Snow coverage text
739 this->widget_id = WID_GL_SNOW_COVERAGE_TEXT;
740 SetDParam(0, _settings_newgame.game_creation.snow_coverage);
741 ShowQueryString(STR_JUST_INT, STR_MAPGEN_SNOW_COVERAGE_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
742 break;
744 case WID_GL_DESERT_COVERAGE_DOWN:
745 case WID_GL_DESERT_COVERAGE_UP: // Desert coverage buttons
746 /* Don't allow too fast scrolling */
747 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
748 this->HandleButtonClick(widget);
750 _settings_newgame.game_creation.desert_coverage = Clamp(_settings_newgame.game_creation.desert_coverage + (widget - WID_GL_DESERT_COVERAGE_TEXT) * 10, 0, 100);
751 this->InvalidateData();
753 _left_button_clicked = false;
754 break;
756 case WID_GL_DESERT_COVERAGE_TEXT: // Desert line text
757 this->widget_id = WID_GL_DESERT_COVERAGE_TEXT;
758 SetDParam(0, _settings_newgame.game_creation.desert_coverage);
759 ShowQueryString(STR_JUST_INT, STR_MAPGEN_DESERT_COVERAGE_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
760 break;
762 case WID_GL_LANDSCAPE_PULLDOWN: // Landscape generator
763 ShowDropDownMenu(this, _landscape, _settings_newgame.game_creation.land_generator, WID_GL_LANDSCAPE_PULLDOWN, 0, 0);
764 break;
766 case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: // Heightmap rotation
767 ShowDropDownMenu(this, _rotation, _settings_newgame.game_creation.heightmap_rotation, WID_GL_HEIGHTMAP_ROTATION_PULLDOWN, 0, 0);
768 break;
770 case WID_GL_TERRAIN_PULLDOWN: // Terrain type
771 /* For the original map generation only the first four are valid. */
772 ShowDropDownMenu(this, _elevations, _settings_newgame.difficulty.terrain_type, WID_GL_TERRAIN_PULLDOWN, 0, _settings_newgame.game_creation.land_generator == LG_ORIGINAL ? ~0xF : 0);
773 break;
775 case WID_GL_WATER_PULLDOWN: { // Water quantity
776 uint32 hidden_mask = 0;
777 /* Disable custom water level when the original map generator is active. */
778 if (_settings_newgame.game_creation.land_generator == LG_ORIGINAL) {
779 SetBit(hidden_mask, CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY);
781 ShowDropDownMenu(this, _sea_lakes, _settings_newgame.difficulty.quantity_sea_lakes, WID_GL_WATER_PULLDOWN, 0, hidden_mask);
782 break;
785 case WID_GL_RIVER_PULLDOWN: // Amount of rivers
786 ShowDropDownMenu(this, _rivers, _settings_newgame.game_creation.amount_of_rivers, WID_GL_RIVER_PULLDOWN, 0, 0);
787 break;
789 case WID_GL_SMOOTHNESS_PULLDOWN: // Map smoothness
790 ShowDropDownMenu(this, _smoothness, _settings_newgame.game_creation.tgen_smoothness, WID_GL_SMOOTHNESS_PULLDOWN, 0, 0);
791 break;
793 case WID_GL_VARIETY_PULLDOWN: // Map variety
794 ShowDropDownMenu(this, _variety, _settings_newgame.game_creation.variety, WID_GL_VARIETY_PULLDOWN, 0, 0);
795 break;
797 /* Freetype map borders */
798 case WID_GL_WATER_NW:
799 _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_NW);
800 this->InvalidateData();
801 break;
803 case WID_GL_WATER_NE:
804 _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_NE);
805 this->InvalidateData();
806 break;
808 case WID_GL_WATER_SE:
809 _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_SE);
810 this->InvalidateData();
811 break;
813 case WID_GL_WATER_SW:
814 _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_SW);
815 this->InvalidateData();
816 break;
818 case WID_GL_BORDERS_RANDOM:
819 _settings_newgame.game_creation.water_borders = (_settings_newgame.game_creation.water_borders == BORDERS_RANDOM) ? 0 : BORDERS_RANDOM;
820 this->InvalidateData();
821 break;
825 void OnTimeout() override
827 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};
828 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};
830 const int *widget = (mode == GLWM_HEIGHTMAP) ? heightmap_raise_widgets : newgame_raise_widgets;
832 for (; *widget != WIDGET_LIST_END; widget++) {
833 if (this->IsWidgetLowered(*widget)) {
834 this->RaiseWidget(*widget);
835 this->SetWidgetDirty(*widget);
840 void OnDropdownSelect(int widget, int index) override
842 switch (widget) {
843 case WID_GL_MAPSIZE_X_PULLDOWN: _settings_newgame.game_creation.map_x = index; break;
844 case WID_GL_MAPSIZE_Y_PULLDOWN: _settings_newgame.game_creation.map_y = index; break;
845 case WID_GL_RIVER_PULLDOWN: _settings_newgame.game_creation.amount_of_rivers = index; break;
846 case WID_GL_SMOOTHNESS_PULLDOWN: _settings_newgame.game_creation.tgen_smoothness = index; break;
847 case WID_GL_VARIETY_PULLDOWN: _settings_newgame.game_creation.variety = index; break;
849 case WID_GL_LANDSCAPE_PULLDOWN: _settings_newgame.game_creation.land_generator = index;
850 /* If original landgenerator is selected and alpinist terrain_type was selected, revert to mountainous. */
851 if (_settings_newgame.game_creation.land_generator == LG_ORIGINAL) {
852 _settings_newgame.difficulty.terrain_type = Clamp(_settings_newgame.difficulty.terrain_type, 0, 3);
854 break;
856 case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: _settings_newgame.game_creation.heightmap_rotation = index; break;
858 case WID_GL_TOWN_PULLDOWN:
859 if ((uint)index == CUSTOM_TOWN_NUMBER_DIFFICULTY) {
860 this->widget_id = widget;
861 SetDParam(0, _settings_newgame.game_creation.custom_town_number);
862 ShowQueryString(STR_JUST_INT, STR_MAPGEN_NUMBER_OF_TOWNS, 5, this, CS_NUMERAL, QSF_NONE);
864 _settings_newgame.difficulty.number_towns = index;
865 break;
867 case WID_GL_TOWNNAME_DROPDOWN: // Town names
868 if (_game_mode == GM_MENU || Town::GetNumItems() == 0) {
869 _settings_newgame.game_creation.town_name = index;
870 SetWindowDirty(WC_GAME_OPTIONS, WN_GAME_OPTIONS_GAME_OPTIONS);
872 break;
874 case WID_GL_INDUSTRY_PULLDOWN: _settings_newgame.difficulty.industry_density = index; break;
875 case WID_GL_TERRAIN_PULLDOWN: {
876 if ((uint)index == CUSTOM_TERRAIN_TYPE_NUMBER_DIFFICULTY) {
877 this->widget_id = widget;
878 SetDParam(0, _settings_newgame.game_creation.custom_terrain_type);
879 ShowQueryString(STR_JUST_INT, STR_MAPGEN_TERRAIN_TYPE_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_NONE);
881 _settings_newgame.difficulty.terrain_type = index;
882 break;
885 case WID_GL_WATER_PULLDOWN: {
886 if ((uint)index == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
887 this->widget_id = widget;
888 SetDParam(0, _settings_newgame.game_creation.custom_sea_level);
889 ShowQueryString(STR_JUST_INT, STR_MAPGEN_QUANTITY_OF_SEA_LAKES, 3, this, CS_NUMERAL, QSF_NONE);
891 _settings_newgame.difficulty.quantity_sea_lakes = index;
892 break;
895 this->InvalidateData();
898 void OnQueryTextFinished(char *str) override
900 /* Was 'cancel' pressed? */
901 if (str == nullptr) return;
903 int32 value;
904 if (!StrEmpty(str)) {
905 value = atoi(str);
906 } else {
907 /* An empty string means revert to the default */
908 switch (this->widget_id) {
909 case WID_GL_HEIGHTMAP_HEIGHT_TEXT: value = MAP_HEIGHT_LIMIT_AUTO_MINIMUM; break;
910 case WID_GL_START_DATE_TEXT: value = DEF_START_YEAR; break;
911 case WID_GL_SNOW_COVERAGE_TEXT: value = DEF_SNOW_COVERAGE; break;
912 case WID_GL_DESERT_COVERAGE_TEXT: value = DEF_DESERT_COVERAGE; break;
913 case WID_GL_TOWN_PULLDOWN: value = 1; break;
914 case WID_GL_TERRAIN_PULLDOWN: value = MIN_MAP_HEIGHT_LIMIT; break;
915 case WID_GL_WATER_PULLDOWN: value = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE; break;
916 default: NOT_REACHED();
920 switch (this->widget_id) {
921 case WID_GL_HEIGHTMAP_HEIGHT_TEXT:
922 this->SetWidgetDirty(WID_GL_HEIGHTMAP_HEIGHT_TEXT);
923 _settings_newgame.game_creation.heightmap_height = Clamp(value, MIN_HEIGHTMAP_HEIGHT, GetMapHeightLimit());
924 break;
926 case WID_GL_START_DATE_TEXT:
927 this->SetWidgetDirty(WID_GL_START_DATE_TEXT);
928 _settings_newgame.game_creation.starting_year = Clamp(value, MIN_YEAR, MAX_YEAR);
929 break;
931 case WID_GL_SNOW_COVERAGE_TEXT:
932 this->SetWidgetDirty(WID_GL_SNOW_COVERAGE_TEXT);
933 _settings_newgame.game_creation.snow_coverage = Clamp(value, 0, 100);
934 break;
936 case WID_GL_DESERT_COVERAGE_TEXT:
937 this->SetWidgetDirty(WID_GL_DESERT_COVERAGE_TEXT);
938 _settings_newgame.game_creation.desert_coverage = Clamp(value, 0, 100);
939 break;
941 case WID_GL_TOWN_PULLDOWN:
942 _settings_newgame.game_creation.custom_town_number = Clamp(value, 1, CUSTOM_TOWN_MAX_NUMBER);
943 break;
945 case WID_GL_TERRAIN_PULLDOWN:
946 _settings_newgame.game_creation.custom_terrain_type = Clamp(value, MIN_CUSTOM_TERRAIN_TYPE, GetMapHeightLimit());
947 break;
949 case WID_GL_WATER_PULLDOWN:
950 _settings_newgame.game_creation.custom_sea_level = Clamp(value, CUSTOM_SEA_LEVEL_MIN_PERCENTAGE, CUSTOM_SEA_LEVEL_MAX_PERCENTAGE);
951 break;
954 this->InvalidateData();
958 static WindowDesc _generate_landscape_desc(
959 WDP_CENTER, nullptr, 0, 0,
960 WC_GENERATE_LANDSCAPE, WC_NONE,
962 _nested_generate_landscape_widgets, lengthof(_nested_generate_landscape_widgets)
965 static WindowDesc _heightmap_load_desc(
966 WDP_CENTER, nullptr, 0, 0,
967 WC_GENERATE_LANDSCAPE, WC_NONE,
969 _nested_heightmap_load_widgets, lengthof(_nested_heightmap_load_widgets)
972 static void _ShowGenerateLandscape(GenerateLandscapeWindowMode mode)
974 uint x = 0;
975 uint y = 0;
977 CloseWindowByClass(WC_GENERATE_LANDSCAPE);
979 /* Generate a new seed when opening the window */
980 _settings_newgame.game_creation.generation_seed = InteractiveRandom();
982 if (mode == GLWM_HEIGHTMAP) {
983 /* If the function returns negative, it means there was a problem loading the heightmap */
984 if (!GetHeightmapDimensions(_file_to_saveload.detail_ftype, _file_to_saveload.name.c_str(), &x, &y)) return;
987 WindowDesc *desc = (mode == GLWM_HEIGHTMAP) ? &_heightmap_load_desc : &_generate_landscape_desc;
988 GenerateLandscapeWindow *w = AllocateWindowDescFront<GenerateLandscapeWindow>(desc, mode, true);
990 if (mode == GLWM_HEIGHTMAP) {
991 w->x = x;
992 w->y = y;
993 strecpy(w->name, _file_to_saveload.title, lastof(w->name));
996 SetWindowDirty(WC_GENERATE_LANDSCAPE, mode);
999 /** Start with a normal game. */
1000 void ShowGenerateLandscape()
1002 _ShowGenerateLandscape(GLWM_GENERATE);
1005 /** Start with loading a heightmap. */
1006 void ShowHeightmapLoad()
1008 _ShowGenerateLandscape(GLWM_HEIGHTMAP);
1011 /** Start with a scenario editor. */
1012 void StartScenarioEditor()
1014 StartGeneratingLandscape(GLWM_SCENARIO);
1018 * Start a normal game without the GUI.
1019 * @param seed The seed of the new game.
1021 void StartNewGameWithoutGUI(uint32 seed)
1023 /* GenerateWorld takes care of the possible GENERATE_NEW_SEED value in 'seed' */
1024 _settings_newgame.game_creation.generation_seed = seed;
1026 StartGeneratingLandscape(GLWM_GENERATE);
1029 struct CreateScenarioWindow : public Window
1031 uint widget_id;
1033 CreateScenarioWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
1035 this->InitNested(window_number);
1036 this->LowerWidget(_settings_newgame.game_creation.landscape + WID_CS_TEMPERATE);
1039 void SetStringParameters(int widget) const override
1041 switch (widget) {
1042 case WID_CS_START_DATE_TEXT:
1043 SetDParam(0, ConvertYMDToDate(_settings_newgame.game_creation.starting_year, 0, 1));
1044 break;
1046 case WID_CS_MAPSIZE_X_PULLDOWN:
1047 SetDParam(0, 1LL << _settings_newgame.game_creation.map_x);
1048 break;
1050 case WID_CS_MAPSIZE_Y_PULLDOWN:
1051 SetDParam(0, 1LL << _settings_newgame.game_creation.map_y);
1052 break;
1054 case WID_CS_FLAT_LAND_HEIGHT_TEXT:
1055 SetDParam(0, _settings_newgame.game_creation.se_flat_world_height);
1056 break;
1060 void OnPaint() override
1062 this->SetWidgetDisabledState(WID_CS_START_DATE_DOWN, _settings_newgame.game_creation.starting_year <= MIN_YEAR);
1063 this->SetWidgetDisabledState(WID_CS_START_DATE_UP, _settings_newgame.game_creation.starting_year >= MAX_YEAR);
1064 this->SetWidgetDisabledState(WID_CS_FLAT_LAND_HEIGHT_DOWN, _settings_newgame.game_creation.se_flat_world_height <= 0);
1065 this->SetWidgetDisabledState(WID_CS_FLAT_LAND_HEIGHT_UP, _settings_newgame.game_creation.se_flat_world_height >= GetMapHeightLimit());
1067 this->SetWidgetLoweredState(WID_CS_TEMPERATE, _settings_newgame.game_creation.landscape == LT_TEMPERATE);
1068 this->SetWidgetLoweredState(WID_CS_ARCTIC, _settings_newgame.game_creation.landscape == LT_ARCTIC);
1069 this->SetWidgetLoweredState(WID_CS_TROPICAL, _settings_newgame.game_creation.landscape == LT_TROPIC);
1070 this->SetWidgetLoweredState(WID_CS_TOYLAND, _settings_newgame.game_creation.landscape == LT_TOYLAND);
1072 this->DrawWidgets();
1075 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1077 StringID str = STR_JUST_INT;
1078 switch (widget) {
1079 case WID_CS_START_DATE_TEXT:
1080 SetDParam(0, ConvertYMDToDate(MAX_YEAR, 0, 1));
1081 str = STR_BLACK_DATE_LONG;
1082 break;
1084 case WID_CS_MAPSIZE_X_PULLDOWN:
1085 case WID_CS_MAPSIZE_Y_PULLDOWN:
1086 SetDParamMaxValue(0, MAX_MAP_SIZE);
1087 break;
1089 case WID_CS_FLAT_LAND_HEIGHT_TEXT:
1090 SetDParamMaxValue(0, MAX_TILE_HEIGHT);
1091 break;
1093 default:
1094 return;
1096 *size = maxdim(*size, GetStringBoundingBox(str));
1097 size->width += padding.width;
1098 size->height += padding.height;
1101 void OnClick(Point pt, int widget, int click_count) override
1103 switch (widget) {
1104 case WID_CS_TEMPERATE:
1105 case WID_CS_ARCTIC:
1106 case WID_CS_TROPICAL:
1107 case WID_CS_TOYLAND:
1108 this->RaiseWidget(_settings_newgame.game_creation.landscape + WID_CS_TEMPERATE);
1109 SetNewLandscapeType(widget - WID_CS_TEMPERATE);
1110 break;
1112 case WID_CS_MAPSIZE_X_PULLDOWN: // Mapsize X
1113 ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_x, WID_CS_MAPSIZE_X_PULLDOWN);
1114 break;
1116 case WID_CS_MAPSIZE_Y_PULLDOWN: // Mapsize Y
1117 ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_y, WID_CS_MAPSIZE_Y_PULLDOWN);
1118 break;
1120 case WID_CS_EMPTY_WORLD: // Empty world / flat world
1121 StartGeneratingLandscape(GLWM_SCENARIO);
1122 break;
1124 case WID_CS_RANDOM_WORLD: // Generate
1125 ShowGenerateLandscape();
1126 break;
1128 case WID_CS_START_DATE_DOWN:
1129 case WID_CS_START_DATE_UP: // Year buttons
1130 /* Don't allow too fast scrolling */
1131 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
1132 this->HandleButtonClick(widget);
1133 this->SetDirty();
1135 _settings_newgame.game_creation.starting_year = Clamp(_settings_newgame.game_creation.starting_year + widget - WID_CS_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
1137 _left_button_clicked = false;
1138 break;
1140 case WID_CS_START_DATE_TEXT: // Year text
1141 this->widget_id = WID_CS_START_DATE_TEXT;
1142 SetDParam(0, _settings_newgame.game_creation.starting_year);
1143 ShowQueryString(STR_JUST_INT, STR_MAPGEN_START_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_NONE);
1144 break;
1146 case WID_CS_FLAT_LAND_HEIGHT_DOWN:
1147 case WID_CS_FLAT_LAND_HEIGHT_UP: // Height level buttons
1148 /* Don't allow too fast scrolling */
1149 if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
1150 this->HandleButtonClick(widget);
1151 this->SetDirty();
1153 _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());
1155 _left_button_clicked = false;
1156 break;
1158 case WID_CS_FLAT_LAND_HEIGHT_TEXT: // Height level text
1159 this->widget_id = WID_CS_FLAT_LAND_HEIGHT_TEXT;
1160 SetDParam(0, _settings_newgame.game_creation.se_flat_world_height);
1161 ShowQueryString(STR_JUST_INT, STR_SE_MAPGEN_FLAT_WORLD_HEIGHT_QUERY_CAPT, 4, this, CS_NUMERAL, QSF_NONE);
1162 break;
1166 void OnTimeout() override
1168 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};
1169 for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
1170 if (this->IsWidgetLowered(*widget)) {
1171 this->RaiseWidget(*widget);
1172 this->SetWidgetDirty(*widget);
1177 void OnDropdownSelect(int widget, int index) override
1179 switch (widget) {
1180 case WID_CS_MAPSIZE_X_PULLDOWN: _settings_newgame.game_creation.map_x = index; break;
1181 case WID_CS_MAPSIZE_Y_PULLDOWN: _settings_newgame.game_creation.map_y = index; break;
1183 this->SetDirty();
1186 void OnQueryTextFinished(char *str) override
1188 if (!StrEmpty(str)) {
1189 int32 value = atoi(str);
1191 switch (this->widget_id) {
1192 case WID_CS_START_DATE_TEXT:
1193 this->SetWidgetDirty(WID_CS_START_DATE_TEXT);
1194 _settings_newgame.game_creation.starting_year = Clamp(value, MIN_YEAR, MAX_YEAR);
1195 break;
1197 case WID_CS_FLAT_LAND_HEIGHT_TEXT:
1198 this->SetWidgetDirty(WID_CS_FLAT_LAND_HEIGHT_TEXT);
1199 _settings_newgame.game_creation.se_flat_world_height = Clamp(value, 0, GetMapHeightLimit());
1200 break;
1203 this->SetDirty();
1208 static const NWidgetPart _nested_create_scenario_widgets[] = {
1209 NWidget(NWID_HORIZONTAL),
1210 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1211 NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SE_MAPGEN_CAPTION, STR_NULL),
1212 EndContainer(),
1213 NWidget(WWT_PANEL, COLOUR_BROWN),
1214 NWidget(NWID_SPACER), SetMinimalSize(0, 10),
1215 /* Landscape style selection. */
1216 NWidget(NWID_HORIZONTAL), SetPIP(10, 3, 10),
1217 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_TEMPERATE), SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
1218 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_ARCTIC), SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
1219 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_TROPICAL), SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
1220 NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_TOYLAND), SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
1221 EndContainer(),
1222 NWidget(NWID_HORIZONTAL), SetPIP(10, 8, 10),
1223 /* Green generation type buttons: 'Flat land' and 'Random land'. */
1224 NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
1225 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_CS_EMPTY_WORLD), SetDataTip(STR_SE_MAPGEN_FLAT_WORLD, STR_SE_MAPGEN_FLAT_WORLD_TOOLTIP), SetFill(1, 1),
1226 NWidget(WWT_PUSHTXTBTN, COLOUR_GREEN, WID_CS_RANDOM_WORLD), SetDataTip(STR_SE_MAPGEN_RANDOM_LAND, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetFill(1, 1),
1227 EndContainer(),
1228 /* Labels + setting drop-downs */
1229 NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
1230 /* Map size. */
1231 NWidget(NWID_HORIZONTAL),
1232 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAPSIZE, STR_NULL), SetPadding(1, 0, 0, 0),
1233 NWidget(NWID_SPACER), SetMinimalSize(6, 0), SetFill(1, 0),
1234 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_CS_MAPSIZE_X_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetPadding(0, 4, 0, 0),
1235 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BY, STR_NULL), SetPadding(1, 2, 0, 0),
1236 NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_CS_MAPSIZE_Y_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL),
1237 EndContainer(),
1238 /* Date. */
1239 NWidget(NWID_HORIZONTAL),
1240 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DATE, STR_NULL), SetPadding(1, 0, 0, 0),
1241 NWidget(NWID_SPACER), SetMinimalSize(6, 0), SetFill(1, 0),
1242 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),
1243 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_CS_START_DATE_TEXT), SetDataTip(STR_BLACK_DATE_LONG, STR_NULL),
1244 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),
1245 EndContainer(),
1246 /* Flat map height. */
1247 NWidget(NWID_HORIZONTAL),
1248 NWidget(WWT_TEXT, COLOUR_ORANGE),
1249 SetDataTip(STR_SE_MAPGEN_FLAT_WORLD_HEIGHT, STR_NULL), SetPadding(1, 0, 0, 0),
1250 NWidget(NWID_SPACER), SetMinimalSize(6, 0), SetFill(1, 0),
1251 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),
1252 NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_CS_FLAT_LAND_HEIGHT_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL),
1253 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),
1254 EndContainer(),
1255 EndContainer(),
1256 EndContainer(),
1257 EndContainer(),
1260 static WindowDesc _create_scenario_desc(
1261 WDP_CENTER, nullptr, 0, 0,
1262 WC_GENERATE_LANDSCAPE, WC_NONE,
1264 _nested_create_scenario_widgets, lengthof(_nested_create_scenario_widgets)
1267 /** Show the window to create a scenario. */
1268 void ShowCreateScenario()
1270 CloseWindowByClass(WC_GENERATE_LANDSCAPE);
1271 new CreateScenarioWindow(&_create_scenario_desc, GLWM_SCENARIO);
1274 static const NWidgetPart _nested_generate_progress_widgets[] = {
1275 NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GENERATION_WORLD, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1276 NWidget(WWT_PANEL, COLOUR_GREY),
1277 NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
1278 NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
1279 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GP_PROGRESS_BAR), SetFill(1, 0),
1280 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GP_PROGRESS_TEXT), SetFill(1, 0),
1281 NWidget(WWT_TEXTBTN, COLOUR_WHITE, WID_GP_ABORT), SetDataTip(STR_GENERATION_ABORT, STR_NULL), SetFill(1, 0),
1282 EndContainer(),
1283 EndContainer(),
1284 EndContainer(),
1288 static WindowDesc _generate_progress_desc(
1289 WDP_CENTER, nullptr, 0, 0,
1290 WC_MODAL_PROGRESS, WC_NONE,
1292 _nested_generate_progress_widgets, lengthof(_nested_generate_progress_widgets)
1295 struct GenWorldStatus {
1296 uint percent;
1297 StringID cls;
1298 uint current;
1299 uint total;
1300 std::chrono::steady_clock::time_point next_update;
1303 static GenWorldStatus _gws;
1305 static const StringID _generation_class_table[] = {
1306 STR_GENERATION_WORLD_GENERATION,
1307 STR_SCENEDIT_TOOLBAR_LANDSCAPE_GENERATION,
1308 STR_GENERATION_RIVER_GENERATION,
1309 STR_GENERATION_CLEARING_TILES,
1310 STR_SCENEDIT_TOOLBAR_TOWN_GENERATION,
1311 STR_SCENEDIT_TOOLBAR_INDUSTRY_GENERATION,
1312 STR_GENERATION_OBJECT_GENERATION,
1313 STR_GENERATION_TREE_GENERATION,
1314 STR_GENERATION_SETTINGUP_GAME,
1315 STR_GENERATION_PREPARING_TILELOOP,
1316 STR_GENERATION_PREPARING_SCRIPT,
1317 STR_GENERATION_PREPARING_GAME
1319 static_assert(lengthof(_generation_class_table) == GWP_CLASS_COUNT);
1322 static void AbortGeneratingWorldCallback(Window *w, bool confirmed)
1324 if (confirmed) {
1325 AbortGeneratingWorld();
1326 } else if (HasModalProgress() && !IsGeneratingWorldAborted()) {
1327 SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
1331 struct GenerateProgressWindow : public Window {
1333 GenerateProgressWindow() : Window(&_generate_progress_desc)
1335 this->InitNested();
1338 void OnClick(Point pt, int widget, int click_count) override
1340 switch (widget) {
1341 case WID_GP_ABORT:
1342 SetMouseCursorBusy(false);
1343 ShowQuery(
1344 STR_GENERATION_ABORT_CAPTION,
1345 STR_GENERATION_ABORT_MESSAGE,
1346 this,
1347 AbortGeneratingWorldCallback
1349 break;
1353 void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
1355 switch (widget) {
1356 case WID_GP_PROGRESS_BAR: {
1357 SetDParamMaxValue(0, 100);
1358 *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
1359 /* We need some spacing for the 'border' */
1360 size->height += 8;
1361 size->width += 8;
1362 break;
1365 case WID_GP_PROGRESS_TEXT:
1366 for (uint i = 0; i < GWP_CLASS_COUNT; i++) {
1367 size->width = std::max(size->width, GetStringBoundingBox(_generation_class_table[i]).width);
1369 size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
1370 break;
1374 void DrawWidget(const Rect &r, int widget) const override
1376 switch (widget) {
1377 case WID_GP_PROGRESS_BAR:
1378 /* Draw the % complete with a bar and a text */
1379 DrawFrameRect(r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
1380 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);
1381 SetDParam(0, _gws.percent);
1382 DrawString(r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
1383 break;
1385 case WID_GP_PROGRESS_TEXT:
1386 /* Tell which class we are generating */
1387 DrawString(r.left, r.right, r.top, _gws.cls, TC_FROMSTRING, SA_HOR_CENTER);
1389 /* And say where we are in that class */
1390 SetDParam(0, _gws.current);
1391 SetDParam(1, _gws.total);
1392 DrawString(r.left, r.right, r.top + FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL, STR_GENERATION_PROGRESS_NUM, TC_FROMSTRING, SA_HOR_CENTER);
1398 * Initializes the progress counters to the starting point.
1400 void PrepareGenerateWorldProgress()
1402 _gws.cls = STR_GENERATION_WORLD_GENERATION;
1403 _gws.current = 0;
1404 _gws.total = 0;
1405 _gws.percent = 0;
1406 _gws.next_update = std::chrono::steady_clock::now();
1410 * Show the window where a user can follow the process of the map generation.
1412 void ShowGenerateWorldProgress()
1414 if (BringWindowToFrontById(WC_MODAL_PROGRESS, 0)) return;
1415 new GenerateProgressWindow();
1418 static void _SetGeneratingWorldProgress(GenWorldProgress cls, uint progress, uint total)
1420 static const int percent_table[] = {0, 5, 14, 17, 20, 40, 60, 65, 80, 85, 95, 99, 100 };
1421 static_assert(lengthof(percent_table) == GWP_CLASS_COUNT + 1);
1422 assert(cls < GWP_CLASS_COUNT);
1424 /* Check if we really are generating the world.
1425 * For example, placing trees via the SE also calls this function, but
1426 * shouldn't try to update the progress.
1428 if (!HasModalProgress()) return;
1430 if (IsGeneratingWorldAborted()) {
1431 HandleGeneratingWorldAbortion();
1432 return;
1435 if (total == 0) {
1436 assert(_gws.cls == _generation_class_table[cls]);
1437 _gws.current += progress;
1438 assert(_gws.current <= _gws.total);
1439 } else {
1440 _gws.cls = _generation_class_table[cls];
1441 _gws.current = progress;
1442 _gws.total = total;
1443 _gws.percent = percent_table[cls];
1446 /* Percentage is about the number of completed tasks, so 'current - 1' */
1447 _gws.percent = percent_table[cls] + (percent_table[cls + 1] - percent_table[cls]) * (_gws.current == 0 ? 0 : _gws.current - 1) / _gws.total;
1449 if (_network_dedicated) {
1450 static uint last_percent = 0;
1452 /* Never display 0% */
1453 if (_gws.percent == 0) return;
1454 /* Reset if percent is lower than the last recorded */
1455 if (_gws.percent < last_percent) last_percent = 0;
1456 /* Display every 5%, but 6% is also very valid.. just not smaller steps than 5% */
1457 if (_gws.percent % 5 != 0 && _gws.percent <= last_percent + 5) return;
1458 /* Never show steps smaller than 2%, even if it is a mod 5% */
1459 if (_gws.percent <= last_percent + 2) return;
1461 Debug(net, 3, "Map generation percentage complete: {}", _gws.percent);
1462 last_percent = _gws.percent;
1464 return;
1467 SetWindowDirty(WC_MODAL_PROGRESS, 0);
1469 VideoDriver::GetInstance()->GameLoopPause();
1473 * Set the total of a stage of the world generation.
1474 * @param cls the current class we are in.
1475 * @param total Set the total expected items for this class.
1477 * Warning: this function isn't clever. Don't go from class 4 to 3. Go upwards, always.
1478 * Also, progress works if total is zero, total works if progress is zero.
1480 void SetGeneratingWorldProgress(GenWorldProgress cls, uint total)
1482 if (total == 0) return;
1484 _SetGeneratingWorldProgress(cls, 0, total);
1488 * Increases the current stage of the world generation with one.
1489 * @param cls the current class we are in.
1491 * Warning: this function isn't clever. Don't go from class 4 to 3. Go upwards, always.
1492 * Also, progress works if total is zero, total works if progress is zero.
1494 void IncreaseGeneratingWorldProgress(GenWorldProgress cls)
1496 /* In fact the param 'class' isn't needed.. but for some security reasons, we want it around */
1497 _SetGeneratingWorldProgress(cls, 1, 0);