genworld_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "heightmap.h"
00014 #include "debug.h"
00015 #include "genworld.h"
00016 #include "network/network.h"
00017 #include "strings_func.h"
00018 #include "window_func.h"
00019 #include "date_func.h"
00020 #include "sound_func.h"
00021 #include "fios.h"
00022 #include "string_func.h"
00023 #include "widgets/dropdown_type.h"
00024 #include "widgets/dropdown_func.h"
00025 #include "landscape_type.h"
00026 #include "querystring_gui.h"
00027 #include "town.h"
00028 #include "settings_func.h"
00029 #include "core/geometry_func.hpp"
00030 #include "core/random_func.hpp"
00031 #include "progress.h"
00032 #include "error.h"
00033 
00034 #include "widgets/genworld_widget.h"
00035 
00036 #include "table/strings.h"
00037 #include "table/sprites.h"
00038 
00039 extern void MakeNewgameSettingsLive();
00040 
00042 enum GenenerateLandscapeWindowMode {
00043   GLWM_GENERATE,  
00044   GLWM_HEIGHTMAP, 
00045   GLWM_SCENARIO,  
00046 };
00047 
00052 void SetNewLandscapeType(byte landscape)
00053 {
00054   _settings_newgame.game_creation.landscape = landscape;
00055   InvalidateWindowClassesData(WC_SELECT_GAME);
00056   InvalidateWindowClassesData(WC_GENERATE_LANDSCAPE);
00057 }
00058 
00060 static const NWidgetPart _nested_generate_landscape_widgets[] = {
00061   NWidget(NWID_HORIZONTAL),
00062     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00063     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MAPGEN_WORLD_GENERATION_CAPTION, STR_NULL),
00064   EndContainer(),
00065   NWidget(WWT_PANEL, COLOUR_BROWN),
00066     NWidget(NWID_SPACER), SetMinimalSize(0, 10),
00067     /* Landscape selection. */
00068     NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
00069       NWidget(NWID_SPACER), SetFill(1, 0),
00070       NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TEMPERATE), SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
00071       NWidget(NWID_SPACER), SetFill(1, 0),
00072       NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_ARCTIC), SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
00073       NWidget(NWID_SPACER), SetFill(1, 0),
00074       NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TROPICAL), SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
00075       NWidget(NWID_SPACER), SetFill(1, 0),
00076       NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TOYLAND), SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
00077       NWidget(NWID_SPACER), SetFill(1, 0),
00078     EndContainer(),
00079     NWidget(NWID_SPACER), SetMinimalSize(0, 11),
00080     NWidget(NWID_HORIZONTAL), SetPIP(10, 5, 10),
00081       NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0),
00082         /* Left column with labels. */
00083         NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
00084           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAPSIZE, STR_NULL), SetFill(1, 1),
00085           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_LAND_GENERATOR, STR_NULL), SetFill(1, 1),
00086           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_TOWNS, STR_NULL), SetFill(1, 1),
00087           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_TERRAIN_TYPE, STR_NULL), SetFill(1, 1),
00088           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_RANDOM_SEED, STR_NULL), SetFill(1, 1),
00089           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_QUANTITY_OF_SEA_LAKES, STR_NULL), SetFill(1, 1),
00090           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_TREE_PLACER, STR_NULL), SetFill(1, 1),
00091           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_VARIETY, STR_NULL), SetFill(1, 1),
00092           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BORDER_TYPE, STR_NULL), SetFill(1, 1),
00093         EndContainer(),
00094         /* Widgets at the right of the labels. */
00095         NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
00096           /* Mapsize X * Y. */
00097           NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
00098             NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_X_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetFill(1, 0),
00099             NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BY, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(1, 1),
00100             NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_Y_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetFill(1, 0),
00101           EndContainer(),
00102           NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_LANDSCAPE_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00103           NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TOWN_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00104           NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TERRAIN_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00105           NWidget(WWT_EDITBOX, COLOUR_WHITE, WID_GL_RANDOM_EDITBOX), SetDataTip(STR_MAPGEN_RANDOM_SEED_OSKTITLE, STR_MAPGEN_RANDOM_SEED_HELP), SetFill(1, 1),
00106           NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_WATER_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00107           NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TREE_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00108           NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_VARIETY_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00109           NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_GL_BORDERS_RANDOM), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00110         EndContainer(),
00111       EndContainer(),
00112       NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
00113         NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0),
00114           NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
00115             NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DATE, STR_NULL), SetFill(1, 1),
00116             NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SNOW_LINE_HEIGHT, STR_NULL), SetFill(1, 1),
00117             NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_INDUSTRIES, STR_NULL), SetFill(1, 1),
00118             NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SMOOTHNESS, STR_NULL), SetFill(1, 1),
00119           EndContainer(),
00120           NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
00121             /* Starting date. */
00122             NWidget(NWID_HORIZONTAL),
00123               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),
00124               NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_START_DATE_TEXT), SetDataTip(STR_BLACK_DATE_LONG, STR_NULL), SetFill(1, 0),
00125               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),
00126             EndContainer(),
00127             /* Snow line. */
00128             NWidget(NWID_HORIZONTAL),
00129               NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_LEVEL_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_SNOW_LINE_DOWN), SetFill(0, 1),
00130               NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_SNOW_LEVEL_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
00131               NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_LEVEL_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_SNOW_LINE_UP), SetFill(0, 1),
00132             EndContainer(),
00133             NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_INDUSTRY_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00134             NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_SMOOTHNESS_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00135           EndContainer(),
00136         EndContainer(),
00137         NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_RANDOM_BUTTON), SetDataTip(STR_MAPGEN_RANDOM, STR_MAPGEN_RANDOM_HELP), SetFill(1, 0),
00138         NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0),
00139           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_QUANTITY_OF_RIVERS, STR_NULL), SetFill(1, 1),
00140           NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_RIVER_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00141         EndContainer(),
00142         NWidget(NWID_SPACER), SetFill(1, 1),
00143         NWidget(WWT_TEXTBTN, COLOUR_GREEN, WID_GL_GENERATE_BUTTON), SetMinimalSize(84, 30), SetDataTip(STR_MAPGEN_GENERATE, STR_NULL), SetFill(1, 0),
00144       EndContainer(),
00145     EndContainer(),
00146     NWidget(NWID_SPACER), SetMinimalSize(0, 4),
00147     /* Map borders buttons for each edge. */
00148     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
00149       NWidget(NWID_HORIZONTAL), SetPIP(0, 0, 3),
00150         NWidget(NWID_SPACER), SetFill(1, 1),
00151         NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NORTHWEST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
00152       EndContainer(),
00153       NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_GL_WATER_NW), SetDataTip(STR_JUST_STRING, STR_MAPGEN_NORTHWEST), SetFill(1, 1),
00154       NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_GL_WATER_NE), SetDataTip(STR_JUST_STRING, STR_MAPGEN_NORTHEAST), SetFill(1, 1),
00155       NWidget(NWID_HORIZONTAL), SetPIP(3, 0, 0),
00156         NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NORTHEAST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
00157         NWidget(NWID_SPACER), SetFill(1, 1),
00158       EndContainer(),
00159     EndContainer(),
00160     NWidget(NWID_HORIZONTAL, NC_EQUALSIZE), SetPIP(10, 0, 10),
00161       NWidget(NWID_HORIZONTAL), SetPIP(0, 0, 3),
00162         NWidget(NWID_SPACER), SetFill(1, 1),
00163         NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SOUTHWEST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
00164       EndContainer(),
00165       NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_GL_WATER_SW), SetDataTip(STR_JUST_STRING, STR_MAPGEN_SOUTHWEST), SetFill(1, 1),
00166       NWidget(WWT_PUSHTXTBTN, COLOUR_ORANGE, WID_GL_WATER_SE), SetDataTip(STR_JUST_STRING, STR_MAPGEN_SOUTHEAST), SetFill(1, 1),
00167       NWidget(NWID_HORIZONTAL), SetPIP(3, 0, 0),
00168         NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SOUTHEAST, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(0, 1),
00169         NWidget(NWID_SPACER), SetFill(1, 1),
00170       EndContainer(),
00171     EndContainer(),
00172     NWidget(NWID_SPACER), SetMinimalSize(0, 9), SetFill(1, 1),
00173   EndContainer(),
00174 };
00175 
00177 static const NWidgetPart _nested_heightmap_load_widgets[] = {
00178   /* Window header. */
00179   NWidget(NWID_HORIZONTAL),
00180     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
00181     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_MAPGEN_WORLD_GENERATION_CAPTION, STR_NULL),
00182   EndContainer(),
00183   NWidget(WWT_PANEL, COLOUR_BROWN),
00184     NWidget(NWID_SPACER), SetMinimalSize(0, 10),
00185     /* Landscape selection. */
00186     NWidget(NWID_HORIZONTAL), SetPIP(10, 0, 10),
00187       NWidget(NWID_SPACER), SetFill(1, 0),
00188       NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TEMPERATE), SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
00189       NWidget(NWID_SPACER), SetFill(1, 0),
00190       NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_ARCTIC), SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
00191       NWidget(NWID_SPACER), SetFill(1, 0),
00192       NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TROPICAL), SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
00193       NWidget(NWID_SPACER), SetFill(1, 0),
00194       NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_GL_TOYLAND), SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
00195       NWidget(NWID_SPACER), SetFill(1, 0),
00196     EndContainer(),
00197     NWidget(NWID_SPACER), SetMinimalSize(0, 11), SetFill(0, 1),
00198     NWidget(NWID_HORIZONTAL), SetPIP(10, 3, 10),
00199       /* Labels at the left side. */
00200       NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
00201         NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_NAME, STR_NULL), SetFill(1, 1),
00202         NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAPSIZE, STR_NULL), SetFill(1, 1),
00203         NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_TOWNS, STR_NULL), SetFill(1, 1),
00204         NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_NUMBER_OF_INDUSTRIES, STR_NULL), SetFill(1, 1),
00205         NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_RANDOM_SEED, STR_NULL), SetFill(1, 1),
00206         NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_QUANTITY_OF_RIVERS, STR_NULL), SetFill(1, 1),
00207         NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_TREE_PLACER, STR_NULL), SetFill(1, 1),
00208         NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_ROTATION, STR_NULL), SetFill(1, 1),
00209       EndContainer(),
00210       /* Widgets at the right of the labels. */
00211       NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
00212         NWidget(WWT_EMPTY, COLOUR_ORANGE, WID_GL_HEIGHTMAP_NAME_TEXT), SetFill(1, 0),
00213         NWidget(NWID_HORIZONTAL), SetPIP(0, 5, 0),
00214           NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
00215             /* Mapsize X * Y. */
00216             NWidget(NWID_HORIZONTAL), SetPIP(0, 4, 0),
00217               NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_X_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetFill(1, 0),
00218               NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BY, STR_NULL), SetPadding(1, 0, 0, 0), SetFill(1, 1),
00219               NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_MAPSIZE_Y_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetFill(1, 0),
00220             EndContainer(),
00221             NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TOWN_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00222             NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_INDUSTRY_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00223             NWidget(WWT_EDITBOX, COLOUR_WHITE, WID_GL_RANDOM_EDITBOX), SetDataTip(STR_MAPGEN_RANDOM_SEED_OSKTITLE, STR_MAPGEN_RANDOM_SEED_HELP), SetFill(1, 1),
00224             NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_RIVER_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00225             NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_TREE_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00226             NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_GL_HEIGHTMAP_ROTATION_PULLDOWN), SetDataTip(STR_JUST_STRING, STR_NULL), SetFill(1, 0),
00227           EndContainer(),
00228           NWidget(NWID_VERTICAL), SetPIP(0, 4, 0),
00229             NWidget(NWID_HORIZONTAL), SetPIP(0, 3, 0),
00230               NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
00231                 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_HEIGHTMAP_SIZE_LABEL, STR_NULL), SetFill(1, 1),
00232                 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DATE, STR_NULL), SetFill(1, 1),
00233                 NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_SNOW_LINE_HEIGHT, STR_NULL), SetFill(1, 1),
00234               EndContainer(),
00235               NWidget(NWID_VERTICAL, NC_EQUALSIZE), SetPIP(0, 4, 0),
00236                 NWidget(WWT_TEXT, COLOUR_ORANGE, WID_GL_HEIGHTMAP_SIZE_TEXT), SetDataTip(STR_MAPGEN_HEIGHTMAP_SIZE, STR_NULL), SetFill(1, 0),
00237                 NWidget(NWID_HORIZONTAL),
00238                   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),
00239                   NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_START_DATE_TEXT), SetDataTip(STR_BLACK_DATE_LONG, STR_NULL), SetFill(1, 0),
00240                   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),
00241                 EndContainer(),
00242                 NWidget(NWID_HORIZONTAL),
00243                   NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_LEVEL_DOWN), SetDataTip(SPR_ARROW_DOWN, STR_MAPGEN_SNOW_LINE_DOWN), SetFill(0, 1),
00244                   NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_SNOW_LEVEL_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL), SetFill(1, 0),
00245                   NWidget(WWT_IMGBTN, COLOUR_ORANGE, WID_GL_SNOW_LEVEL_UP), SetDataTip(SPR_ARROW_UP, STR_MAPGEN_SNOW_LINE_UP), SetFill(0, 1),
00246                 EndContainer(),
00247               EndContainer(),
00248             EndContainer(),
00249             NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_GL_RANDOM_BUTTON), SetDataTip(STR_MAPGEN_RANDOM, STR_MAPGEN_RANDOM_HELP), SetFill(1, 0),
00250             NWidget(NWID_SPACER), SetFill(1, 1),
00251             NWidget(WWT_TEXTBTN, COLOUR_GREEN, WID_GL_GENERATE_BUTTON), SetMinimalSize(84, 30), SetDataTip(STR_MAPGEN_GENERATE, STR_NULL), SetFill(1, 0),
00252           EndContainer(),
00253         EndContainer(),
00254       EndContainer(),
00255     EndContainer(),
00256     NWidget(NWID_SPACER), SetMinimalSize(0, 9), SetFill(1, 1),
00257   EndContainer(),
00258 };
00259 
00260 static void StartGeneratingLandscape(GenenerateLandscapeWindowMode mode)
00261 {
00262   DeleteAllNonVitalWindows();
00263   ClearErrorMessages();
00264 
00265   /* Copy all XXX_newgame to XXX when coming from outside the editor */
00266   MakeNewgameSettingsLive();
00267   ResetGRFConfig(true);
00268 
00269   SndPlayFx(SND_15_BEEP);
00270   switch (mode) {
00271     case GLWM_GENERATE:  _switch_mode = (_game_mode == GM_EDITOR) ? SM_GENRANDLAND    : SM_NEWGAME;         break;
00272     case GLWM_HEIGHTMAP: _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_HEIGHTMAP : SM_START_HEIGHTMAP; break;
00273     case GLWM_SCENARIO:  _switch_mode = SM_EDITOR; break;
00274     default: NOT_REACHED();
00275   }
00276 }
00277 
00278 static void LandscapeGenerationCallback(Window *w, bool confirmed)
00279 {
00280   if (confirmed) StartGeneratingLandscape((GenenerateLandscapeWindowMode)w->window_number);
00281 }
00282 
00283 static DropDownList *BuildMapsizeDropDown()
00284 {
00285   DropDownList *list = new DropDownList();
00286 
00287   for (uint i = MIN_MAP_SIZE_BITS; i <= MAX_MAP_SIZE_BITS; i++) {
00288     DropDownListParamStringItem *item = new DropDownListParamStringItem(STR_JUST_INT, i, false);
00289     item->SetParam(0, 1 << i);
00290     list->push_back(item);
00291   }
00292 
00293   return list;
00294 }
00295 
00296 static const StringID _elevations[]  = {STR_TERRAIN_TYPE_VERY_FLAT, STR_TERRAIN_TYPE_FLAT, STR_TERRAIN_TYPE_HILLY, STR_TERRAIN_TYPE_MOUNTAINOUS, INVALID_STRING_ID};
00297 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};
00298 static const StringID _rivers[]      = {STR_RIVERS_NONE, STR_RIVERS_FEW, STR_RIVERS_MODERATE, STR_RIVERS_LOT, INVALID_STRING_ID};
00299 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};
00300 static const StringID _tree_placer[] = {STR_CONFIG_SETTING_TREE_PLACER_NONE, STR_CONFIG_SETTING_TREE_PLACER_ORIGINAL, STR_CONFIG_SETTING_TREE_PLACER_IMPROVED, INVALID_STRING_ID};
00301 static const StringID _rotation[]    = {STR_CONFIG_SETTING_HEIGHTMAP_ROTATION_COUNTER_CLOCKWISE, STR_CONFIG_SETTING_HEIGHTMAP_ROTATION_CLOCKWISE, INVALID_STRING_ID};
00302 static const StringID _landscape[]   = {STR_CONFIG_SETTING_LAND_GENERATOR_ORIGINAL, STR_CONFIG_SETTING_LAND_GENERATOR_TERRA_GENESIS, INVALID_STRING_ID};
00303 static const StringID _num_towns[]   = {STR_NUM_VERY_LOW, STR_NUM_LOW, STR_NUM_NORMAL, STR_NUM_HIGH, STR_NUM_CUSTOM, INVALID_STRING_ID};
00304 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};
00305 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};
00306 
00307 assert_compile(lengthof(_num_inds) == ID_END + 1);
00308 
00309 struct GenerateLandscapeWindow : public QueryStringBaseWindow {
00310   uint widget_id;
00311   uint x;
00312   uint y;
00313   char name[64];
00314   GenenerateLandscapeWindowMode mode;
00315 
00316   GenerateLandscapeWindow(const WindowDesc *desc, WindowNumber number = 0) : QueryStringBaseWindow(11)
00317   {
00318     this->InitNested(desc, number);
00319 
00320     this->LowerWidget(_settings_newgame.game_creation.landscape + WID_GL_TEMPERATE);
00321 
00322     /* snprintf() always outputs trailing '\0', so whole buffer can be used */
00323     snprintf(this->edit_str_buf, this->edit_str_size, "%u", _settings_newgame.game_creation.generation_seed);
00324     InitializeTextBuffer(&this->text, this->edit_str_buf, this->edit_str_size);
00325     this->caption = STR_NULL;
00326     this->afilter = CS_NUMERAL;
00327 
00328     this->mode = (GenenerateLandscapeWindowMode)this->window_number;
00329 
00330     /* Disable town, industry and trees in SE */
00331     this->SetWidgetDisabledState(WID_GL_TOWN_PULLDOWN,     _game_mode == GM_EDITOR);
00332     this->SetWidgetDisabledState(WID_GL_INDUSTRY_PULLDOWN, _game_mode == GM_EDITOR);
00333     this->SetWidgetDisabledState(WID_GL_TREE_PULLDOWN,     _game_mode == GM_EDITOR);
00334 
00335     this->OnInvalidateData();
00336   }
00337 
00338 
00339   virtual void SetStringParameters(int widget) const
00340   {
00341     switch (widget) {
00342       case WID_GL_START_DATE_TEXT:     SetDParam(0, ConvertYMDToDate(_settings_newgame.game_creation.starting_year, 0, 1)); break;
00343       case WID_GL_MAPSIZE_X_PULLDOWN:  SetDParam(0, 1 << _settings_newgame.game_creation.map_x); break;
00344       case WID_GL_MAPSIZE_Y_PULLDOWN:  SetDParam(0, 1 << _settings_newgame.game_creation.map_y); break;
00345       case WID_GL_SNOW_LEVEL_TEXT:     SetDParam(0, _settings_newgame.game_creation.snow_line_height); break;
00346 
00347       case WID_GL_TOWN_PULLDOWN:
00348         if (_game_mode == GM_EDITOR) {
00349           SetDParam(0, STR_CONFIG_SETTING_OFF);
00350         } else if (_settings_newgame.difficulty.number_towns == CUSTOM_TOWN_NUMBER_DIFFICULTY) {
00351           SetDParam(0, STR_NUM_CUSTOM_NUMBER);
00352           SetDParam(1, _settings_newgame.game_creation.custom_town_number);
00353         } else {
00354           SetDParam(0, _num_towns[_settings_newgame.difficulty.number_towns]);
00355         }
00356         break;
00357 
00358       case WID_GL_INDUSTRY_PULLDOWN:   SetDParam(0, _game_mode == GM_EDITOR ? STR_CONFIG_SETTING_OFF : _num_inds[_settings_newgame.difficulty.industry_density]); break;
00359       case WID_GL_LANDSCAPE_PULLDOWN:  SetDParam(0, _landscape[_settings_newgame.game_creation.land_generator]); break;
00360       case WID_GL_TREE_PULLDOWN:       SetDParam(0, _tree_placer[_settings_newgame.game_creation.tree_placer]); break;
00361       case WID_GL_TERRAIN_PULLDOWN:    SetDParam(0, _elevations[_settings_newgame.difficulty.terrain_type]); break;
00362 
00363       case WID_GL_WATER_PULLDOWN:
00364         if (_settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
00365           SetDParam(0, STR_SEA_LEVEL_CUSTOM_PERCENTAGE);
00366           SetDParam(1, _settings_newgame.game_creation.custom_sea_level);
00367         } else {
00368           SetDParam(0, _sea_lakes[_settings_newgame.difficulty.quantity_sea_lakes]);
00369         }
00370         break;
00371 
00372       case WID_GL_RIVER_PULLDOWN:      SetDParam(0, _rivers[_settings_newgame.game_creation.amount_of_rivers]); break;
00373       case WID_GL_SMOOTHNESS_PULLDOWN: SetDParam(0, _smoothness[_settings_newgame.game_creation.tgen_smoothness]); break;
00374       case WID_GL_VARIETY_PULLDOWN:    SetDParam(0, _variety[_settings_newgame.game_creation.variety]); break;
00375       case WID_GL_BORDERS_RANDOM:      SetDParam(0, (_settings_newgame.game_creation.water_borders == BORDERS_RANDOM) ? STR_MAPGEN_BORDER_RANDOMIZE : STR_MAPGEN_BORDER_MANUAL); break;
00376       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;
00377       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;
00378       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;
00379       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;
00380       case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: SetDParam(0, _rotation[_settings_newgame.game_creation.heightmap_rotation]); break;
00381 
00382       case WID_GL_HEIGHTMAP_SIZE_TEXT:
00383         if (_settings_newgame.game_creation.heightmap_rotation == HM_CLOCKWISE) {
00384           SetDParam(0, this->y);
00385           SetDParam(1, this->x);
00386         } else {
00387           SetDParam(0, this->x);
00388           SetDParam(1, this->y);
00389         }
00390         break;
00391     }
00392   }
00393 
00399   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
00400   {
00401     if (!gui_scope) return;
00402     /* Update the climate buttons */
00403     this->SetWidgetLoweredState(WID_GL_TEMPERATE, _settings_newgame.game_creation.landscape == LT_TEMPERATE);
00404     this->SetWidgetLoweredState(WID_GL_ARCTIC,    _settings_newgame.game_creation.landscape == LT_ARCTIC);
00405     this->SetWidgetLoweredState(WID_GL_TROPICAL,  _settings_newgame.game_creation.landscape == LT_TROPIC);
00406     this->SetWidgetLoweredState(WID_GL_TOYLAND,   _settings_newgame.game_creation.landscape == LT_TOYLAND);
00407 
00408     /* You can't select smoothness / non-water borders if not terragenesis */
00409     if (mode == GLWM_GENERATE) {
00410       this->SetWidgetDisabledState(WID_GL_SMOOTHNESS_PULLDOWN, _settings_newgame.game_creation.land_generator == 0);
00411       this->SetWidgetDisabledState(WID_GL_VARIETY_PULLDOWN, _settings_newgame.game_creation.land_generator == 0);
00412       this->SetWidgetDisabledState(WID_GL_BORDERS_RANDOM, _settings_newgame.game_creation.land_generator == 0 || !_settings_newgame.construction.freeform_edges);
00413       this->SetWidgetsDisabledState(_settings_newgame.game_creation.land_generator == 0 || !_settings_newgame.construction.freeform_edges || _settings_newgame.game_creation.water_borders == BORDERS_RANDOM,
00414           WID_GL_WATER_NW, WID_GL_WATER_NE, WID_GL_WATER_SE, WID_GL_WATER_SW, WIDGET_LIST_END);
00415 
00416       this->SetWidgetLoweredState(WID_GL_BORDERS_RANDOM, _settings_newgame.game_creation.water_borders == BORDERS_RANDOM);
00417 
00418       this->SetWidgetLoweredState(WID_GL_WATER_NW, HasBit(_settings_newgame.game_creation.water_borders, BORDER_NW));
00419       this->SetWidgetLoweredState(WID_GL_WATER_NE, HasBit(_settings_newgame.game_creation.water_borders, BORDER_NE));
00420       this->SetWidgetLoweredState(WID_GL_WATER_SE, HasBit(_settings_newgame.game_creation.water_borders, BORDER_SE));
00421       this->SetWidgetLoweredState(WID_GL_WATER_SW, HasBit(_settings_newgame.game_creation.water_borders, BORDER_SW));
00422 
00423       this->SetWidgetsDisabledState(_settings_newgame.game_creation.land_generator == 0 && (_settings_newgame.game_creation.landscape == LT_ARCTIC || _settings_newgame.game_creation.landscape == LT_TROPIC),
00424           WID_GL_TERRAIN_PULLDOWN, WID_GL_WATER_PULLDOWN, WIDGET_LIST_END);
00425     }
00426 
00427     /* Disable snowline if not arctic */
00428     this->SetWidgetDisabledState(WID_GL_SNOW_LEVEL_TEXT, _settings_newgame.game_creation.landscape != LT_ARCTIC);
00429 
00430     /* Update availability of decreasing / increasing start date and snow level */
00431     this->SetWidgetDisabledState(WID_GL_START_DATE_DOWN, _settings_newgame.game_creation.starting_year <= MIN_YEAR);
00432     this->SetWidgetDisabledState(WID_GL_START_DATE_UP,   _settings_newgame.game_creation.starting_year >= MAX_YEAR);
00433     this->SetWidgetDisabledState(WID_GL_SNOW_LEVEL_DOWN, _settings_newgame.game_creation.snow_line_height <= MIN_SNOWLINE_HEIGHT || _settings_newgame.game_creation.landscape != LT_ARCTIC);
00434     this->SetWidgetDisabledState(WID_GL_SNOW_LEVEL_UP,   _settings_newgame.game_creation.snow_line_height >= MAX_SNOWLINE_HEIGHT || _settings_newgame.game_creation.landscape != LT_ARCTIC);
00435 
00436     /* Do not allow a custom sea level with the original land generator. */
00437     if (_settings_newgame.game_creation.land_generator == 0 &&
00438         _settings_newgame.difficulty.quantity_sea_lakes == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
00439       _settings_newgame.difficulty.quantity_sea_lakes = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE;
00440     }
00441 
00442   }
00443 
00444   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00445   {
00446     const StringID *strs = NULL;
00447     switch (widget) {
00448       case WID_GL_START_DATE_TEXT:
00449         SetDParam(0, ConvertYMDToDate(MAX_YEAR, 0, 1));
00450         *size = GetStringBoundingBox(STR_BLACK_DATE_LONG);
00451         break;
00452 
00453       case WID_GL_MAPSIZE_X_PULLDOWN:
00454       case WID_GL_MAPSIZE_Y_PULLDOWN:
00455         SetDParam(0, MAX_MAP_SIZE);
00456         *size = GetStringBoundingBox(STR_JUST_INT);
00457         break;
00458 
00459       case WID_GL_SNOW_LEVEL_TEXT:
00460         SetDParam(0, MAX_TILE_HEIGHT);
00461         *size = GetStringBoundingBox(STR_JUST_INT);
00462         break;
00463 
00464       case WID_GL_HEIGHTMAP_SIZE_TEXT:
00465         SetDParam(0, this->x);
00466         SetDParam(1, this->y);
00467         *size = GetStringBoundingBox(STR_MAPGEN_HEIGHTMAP_SIZE);
00468         break;
00469 
00470       case WID_GL_TOWN_PULLDOWN:
00471         strs = _num_towns;
00472         SetDParam(0, CUSTOM_TOWN_MAX_NUMBER);
00473         *size = GetStringBoundingBox(STR_NUM_CUSTOM_NUMBER);
00474         break;
00475 
00476       case WID_GL_INDUSTRY_PULLDOWN:   strs = _num_inds; break;
00477       case WID_GL_LANDSCAPE_PULLDOWN:  strs = _landscape; break;
00478       case WID_GL_TREE_PULLDOWN:       strs = _tree_placer; break;
00479       case WID_GL_TERRAIN_PULLDOWN:    strs = _elevations; break;
00480       case WID_GL_WATER_PULLDOWN:
00481         strs = _sea_lakes;
00482         SetDParam(0, CUSTOM_SEA_LEVEL_MAX_PERCENTAGE);
00483         *size = GetStringBoundingBox(STR_SEA_LEVEL_CUSTOM_PERCENTAGE);
00484         break;
00485 
00486       case WID_GL_RIVER_PULLDOWN:      strs = _rivers; break;
00487       case WID_GL_SMOOTHNESS_PULLDOWN: strs = _smoothness; break;
00488       case WID_GL_VARIETY_PULLDOWN:    strs = _variety; break;
00489       case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: strs = _rotation; break;
00490       case WID_GL_BORDERS_RANDOM:
00491         *size = maxdim(GetStringBoundingBox(STR_MAPGEN_BORDER_RANDOMIZE), GetStringBoundingBox(STR_MAPGEN_BORDER_MANUAL));
00492         break;
00493 
00494       case WID_GL_WATER_NE:
00495       case WID_GL_WATER_NW:
00496       case WID_GL_WATER_SE:
00497       case WID_GL_WATER_SW:
00498         *size = maxdim(GetStringBoundingBox(STR_MAPGEN_BORDER_RANDOM), maxdim(GetStringBoundingBox(STR_MAPGEN_BORDER_WATER), GetStringBoundingBox(STR_MAPGEN_BORDER_FREEFORM)));
00499         break;
00500 
00501       case WID_GL_HEIGHTMAP_NAME_TEXT:
00502         size->width = 0;
00503         break;
00504 
00505       default:
00506         return;
00507     }
00508     if (strs != NULL) {
00509       while (*strs != INVALID_STRING_ID) {
00510         *size = maxdim(*size, GetStringBoundingBox(*strs++));
00511       }
00512     }
00513     size->width += padding.width;
00514     size->height = FONT_HEIGHT_NORMAL + WD_DROPDOWNTEXT_TOP + WD_DROPDOWNTEXT_BOTTOM;
00515   }
00516 
00517   virtual void DrawWidget(const Rect &r, int widget) const
00518   {
00519     switch (widget) {
00520       case WID_GL_HEIGHTMAP_NAME_TEXT: {
00521         DrawString(r.left, r.right, r.top, this->name, TC_ORANGE);
00522         break;
00523       }
00524     }
00525   }
00526 
00527   virtual void OnPaint()
00528   {
00529     this->DrawWidgets();
00530     this->DrawEditBox(WID_GL_RANDOM_EDITBOX);
00531   }
00532 
00533   virtual void OnClick(Point pt, int widget, int click_count)
00534   {
00535     switch (widget) {
00536       case WID_GL_TEMPERATE:
00537       case WID_GL_ARCTIC:
00538       case WID_GL_TROPICAL:
00539       case WID_GL_TOYLAND:
00540         SetNewLandscapeType(widget - WID_GL_TEMPERATE);
00541         break;
00542 
00543       case WID_GL_MAPSIZE_X_PULLDOWN: // Mapsize X
00544         ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_x, WID_GL_MAPSIZE_X_PULLDOWN);
00545         break;
00546 
00547       case WID_GL_MAPSIZE_Y_PULLDOWN: // Mapsize Y
00548         ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_y, WID_GL_MAPSIZE_Y_PULLDOWN);
00549         break;
00550 
00551       case WID_GL_TOWN_PULLDOWN: // Number of towns
00552         ShowDropDownMenu(this, _num_towns, _settings_newgame.difficulty.number_towns, WID_GL_TOWN_PULLDOWN, 0, 0);
00553         break;
00554 
00555       case WID_GL_INDUSTRY_PULLDOWN: // Number of industries
00556         ShowDropDownMenu(this, _num_inds, _settings_newgame.difficulty.industry_density, WID_GL_INDUSTRY_PULLDOWN, 0, 0);
00557         break;
00558 
00559       case WID_GL_RANDOM_BUTTON: // Random seed
00560         _settings_newgame.game_creation.generation_seed = InteractiveRandom();
00561         snprintf(this->edit_str_buf, this->edit_str_size, "%u", _settings_newgame.game_creation.generation_seed);
00562         UpdateTextBufferSize(&this->text);
00563         this->SetDirty();
00564         break;
00565 
00566       case WID_GL_GENERATE_BUTTON: { // Generate
00567         /* Get rotated map size. */
00568         uint map_x;
00569         uint map_y;
00570         if (_settings_newgame.game_creation.heightmap_rotation == HM_CLOCKWISE) {
00571           map_x = this->y;
00572           map_y = this->x;
00573         } else {
00574           map_x = this->x;
00575           map_y = this->y;
00576         }
00577         if (mode == GLWM_HEIGHTMAP &&
00578             (map_x * 2 < (1U << _settings_newgame.game_creation.map_x) ||
00579             map_x / 2 > (1U << _settings_newgame.game_creation.map_x) ||
00580             map_y * 2 < (1U << _settings_newgame.game_creation.map_y) ||
00581             map_y / 2 > (1U << _settings_newgame.game_creation.map_y))) {
00582           ShowQuery(
00583             STR_WARNING_HEIGHTMAP_SCALE_CAPTION,
00584             STR_WARNING_HEIGHTMAP_SCALE_MESSAGE,
00585             this,
00586             LandscapeGenerationCallback);
00587         } else {
00588           StartGeneratingLandscape(mode);
00589         }
00590         break;
00591       }
00592 
00593       case WID_GL_START_DATE_DOWN:
00594       case WID_GL_START_DATE_UP: // Year buttons
00595         /* Don't allow too fast scrolling */
00596         if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
00597           this->HandleButtonClick(widget);
00598 
00599           _settings_newgame.game_creation.starting_year = Clamp(_settings_newgame.game_creation.starting_year + widget - WID_GL_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
00600           this->InvalidateData();
00601         }
00602         _left_button_clicked = false;
00603         break;
00604 
00605       case WID_GL_START_DATE_TEXT: // Year text
00606         this->widget_id = WID_GL_START_DATE_TEXT;
00607         SetDParam(0, _settings_newgame.game_creation.starting_year);
00608         ShowQueryString(STR_JUST_INT, STR_MAPGEN_START_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
00609         break;
00610 
00611       case WID_GL_SNOW_LEVEL_DOWN:
00612       case WID_GL_SNOW_LEVEL_UP: // Snow line buttons
00613         /* Don't allow too fast scrolling */
00614         if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
00615           this->HandleButtonClick(widget);
00616 
00617           _settings_newgame.game_creation.snow_line_height = Clamp(_settings_newgame.game_creation.snow_line_height + widget - WID_GL_SNOW_LEVEL_TEXT, MIN_SNOWLINE_HEIGHT, MAX_SNOWLINE_HEIGHT);
00618           this->InvalidateData();
00619         }
00620         _left_button_clicked = false;
00621         break;
00622 
00623       case WID_GL_SNOW_LEVEL_TEXT: // Snow line text
00624         this->widget_id = WID_GL_SNOW_LEVEL_TEXT;
00625         SetDParam(0, _settings_newgame.game_creation.snow_line_height);
00626         ShowQueryString(STR_JUST_INT, STR_MAPGEN_SNOW_LINE_QUERY_CAPT, 3, this, CS_NUMERAL, QSF_ENABLE_DEFAULT);
00627         break;
00628 
00629       case WID_GL_TREE_PULLDOWN: // Tree placer
00630         ShowDropDownMenu(this, _tree_placer, _settings_newgame.game_creation.tree_placer, WID_GL_TREE_PULLDOWN, 0, 0);
00631         break;
00632 
00633       case WID_GL_LANDSCAPE_PULLDOWN: // Landscape generator
00634         ShowDropDownMenu(this, _landscape, _settings_newgame.game_creation.land_generator, WID_GL_LANDSCAPE_PULLDOWN, 0, 0);
00635         break;
00636 
00637       case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: // Heightmap rotation
00638         ShowDropDownMenu(this, _rotation, _settings_newgame.game_creation.heightmap_rotation, WID_GL_HEIGHTMAP_ROTATION_PULLDOWN, 0, 0);
00639         break;
00640 
00641       case WID_GL_TERRAIN_PULLDOWN: // Terrain type
00642         ShowDropDownMenu(this, _elevations, _settings_newgame.difficulty.terrain_type, WID_GL_TERRAIN_PULLDOWN, 0, 0);
00643         break;
00644 
00645       case WID_GL_WATER_PULLDOWN: { // Water quantity
00646         uint32 hidden_mask = 0;
00647         /* Disable custom water level when the original map generator is active. */
00648         if (_settings_newgame.game_creation.land_generator == 0) {
00649           SetBit(hidden_mask, CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY);
00650         }
00651         ShowDropDownMenu(this, _sea_lakes, _settings_newgame.difficulty.quantity_sea_lakes, WID_GL_WATER_PULLDOWN, 0, hidden_mask);
00652         break;
00653       }
00654 
00655       case WID_GL_RIVER_PULLDOWN: // Amount of rivers
00656         ShowDropDownMenu(this, _rivers, _settings_newgame.game_creation.amount_of_rivers, WID_GL_RIVER_PULLDOWN, 0, 0);
00657         break;
00658 
00659       case WID_GL_SMOOTHNESS_PULLDOWN: // Map smoothness
00660         ShowDropDownMenu(this, _smoothness, _settings_newgame.game_creation.tgen_smoothness, WID_GL_SMOOTHNESS_PULLDOWN, 0, 0);
00661         break;
00662 
00663       case WID_GL_VARIETY_PULLDOWN: // Map variety
00664         ShowDropDownMenu(this, _variety, _settings_newgame.game_creation.variety, WID_GL_VARIETY_PULLDOWN, 0, 0);
00665         break;
00666 
00667       /* Freetype map borders */
00668       case WID_GL_WATER_NW:
00669         _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_NW);
00670         this->InvalidateData();
00671         break;
00672 
00673       case WID_GL_WATER_NE:
00674         _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_NE);
00675         this->InvalidateData();
00676         break;
00677 
00678       case WID_GL_WATER_SE:
00679         _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_SE);
00680         this->InvalidateData();
00681         break;
00682 
00683       case WID_GL_WATER_SW:
00684         _settings_newgame.game_creation.water_borders = ToggleBit(_settings_newgame.game_creation.water_borders, BORDER_SW);
00685         this->InvalidateData();
00686         break;
00687 
00688       case WID_GL_BORDERS_RANDOM:
00689         _settings_newgame.game_creation.water_borders = (_settings_newgame.game_creation.water_borders == BORDERS_RANDOM) ? 0 : BORDERS_RANDOM;
00690         this->InvalidateData();
00691         break;
00692     }
00693   }
00694 
00695   virtual void OnTimeout()
00696   {
00697     static const int raise_widgets[] = {WID_GL_START_DATE_DOWN, WID_GL_START_DATE_UP, WID_GL_SNOW_LEVEL_UP, WID_GL_SNOW_LEVEL_DOWN, WIDGET_LIST_END};
00698     for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
00699       if (this->IsWidgetLowered(*widget)) {
00700         this->RaiseWidget(*widget);
00701         this->SetWidgetDirty(*widget);
00702       }
00703     }
00704   }
00705 
00706   virtual void OnMouseLoop()
00707   {
00708     this->HandleEditBox(WID_GL_RANDOM_EDITBOX);
00709   }
00710 
00711   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00712   {
00713     EventState state = ES_NOT_HANDLED;
00714     this->HandleEditBoxKey(WID_GL_RANDOM_EDITBOX, key, keycode, state);
00715     /* the seed is unsigned, therefore atoi cannot be used.
00716      * As UINT32_MAX is a 'magic' value (use random seed) it
00717      * should not be possible to be entered into the input
00718      * field; the generate seed button can be used instead. */
00719     _settings_newgame.game_creation.generation_seed = minu(strtoul(this->edit_str_buf, NULL, 10), UINT32_MAX - 1);
00720     return state;
00721   }
00722 
00723   virtual void OnDropdownSelect(int widget, int index)
00724   {
00725     switch (widget) {
00726       case WID_GL_MAPSIZE_X_PULLDOWN:     _settings_newgame.game_creation.map_x = index; break;
00727       case WID_GL_MAPSIZE_Y_PULLDOWN:     _settings_newgame.game_creation.map_y = index; break;
00728       case WID_GL_TREE_PULLDOWN:          _settings_newgame.game_creation.tree_placer = index; break;
00729       case WID_GL_RIVER_PULLDOWN:         _settings_newgame.game_creation.amount_of_rivers = index; break;
00730       case WID_GL_SMOOTHNESS_PULLDOWN:    _settings_newgame.game_creation.tgen_smoothness = index;  break;
00731       case WID_GL_VARIETY_PULLDOWN:       _settings_newgame.game_creation.variety = index; break;
00732       case WID_GL_LANDSCAPE_PULLDOWN:     _settings_newgame.game_creation.land_generator = index; break;
00733       case WID_GL_HEIGHTMAP_ROTATION_PULLDOWN: _settings_newgame.game_creation.heightmap_rotation = index; break;
00734 
00735       case WID_GL_TOWN_PULLDOWN:
00736         if ((uint)index == CUSTOM_TOWN_NUMBER_DIFFICULTY) {
00737           this->widget_id = widget;
00738           SetDParam(0, _settings_newgame.game_creation.custom_town_number);
00739           ShowQueryString(STR_JUST_INT, STR_MAPGEN_NUMBER_OF_TOWNS, 5, this, CS_NUMERAL, QSF_NONE);
00740         }
00741         IConsoleSetSetting("difficulty.number_towns", index);
00742         break;
00743 
00744       case WID_GL_INDUSTRY_PULLDOWN:
00745         IConsoleSetSetting("difficulty.industry_density", index);
00746         break;
00747 
00748       case WID_GL_TERRAIN_PULLDOWN: {
00749         GameMode old_gm = _game_mode;
00750         _game_mode = GM_MENU;
00751         IConsoleSetSetting("difficulty.terrain_type", index);
00752         _game_mode = old_gm;
00753         break;
00754       }
00755 
00756       case WID_GL_WATER_PULLDOWN: {
00757         if ((uint)index == CUSTOM_SEA_LEVEL_NUMBER_DIFFICULTY) {
00758           this->widget_id = widget;
00759           SetDParam(0, _settings_newgame.game_creation.custom_sea_level);
00760           ShowQueryString(STR_JUST_INT, STR_MAPGEN_QUANTITY_OF_SEA_LAKES, 3, this, CS_NUMERAL, QSF_NONE);
00761         }
00762         GameMode old_gm = _game_mode;
00763         _game_mode = GM_MENU;
00764         IConsoleSetSetting("difficulty.quantity_sea_lakes", index);
00765         _game_mode = old_gm;
00766         break;
00767       }
00768     }
00769     this->InvalidateData();
00770   }
00771 
00772   virtual void OnQueryTextFinished(char *str)
00773   {
00774     /* Was 'cancel' pressed? */
00775     if (str == NULL) return;
00776 
00777     int32 value;
00778     if (!StrEmpty(str)) {
00779       value = atoi(str);
00780     } else {
00781       /* An empty string means revert to the default */
00782       switch (this->widget_id) {
00783         case WID_GL_START_DATE_TEXT: value = DEF_START_YEAR; break;
00784         case WID_GL_SNOW_LEVEL_TEXT: value = DEF_SNOWLINE_HEIGHT; break;
00785         case WID_GL_TOWN_PULLDOWN:   value = 1; break;
00786         case WID_GL_WATER_PULLDOWN:  value = CUSTOM_SEA_LEVEL_MIN_PERCENTAGE; break;
00787         default: NOT_REACHED();
00788       }
00789     }
00790 
00791     switch (this->widget_id) {
00792       case WID_GL_START_DATE_TEXT:
00793         this->SetWidgetDirty(WID_GL_START_DATE_TEXT);
00794         _settings_newgame.game_creation.starting_year = Clamp(value, MIN_YEAR, MAX_YEAR);
00795         break;
00796 
00797       case WID_GL_SNOW_LEVEL_TEXT:
00798         this->SetWidgetDirty(WID_GL_SNOW_LEVEL_TEXT);
00799         _settings_newgame.game_creation.snow_line_height = Clamp(value, MIN_SNOWLINE_HEIGHT, MAX_SNOWLINE_HEIGHT);
00800         break;
00801 
00802       case WID_GL_TOWN_PULLDOWN:
00803         _settings_newgame.game_creation.custom_town_number = Clamp(value, 1, CUSTOM_TOWN_MAX_NUMBER);
00804         break;
00805 
00806       case WID_GL_WATER_PULLDOWN:
00807         _settings_newgame.game_creation.custom_sea_level = Clamp(value, CUSTOM_SEA_LEVEL_MIN_PERCENTAGE, CUSTOM_SEA_LEVEL_MAX_PERCENTAGE);
00808         break;
00809     }
00810 
00811     this->InvalidateData();
00812   }
00813 };
00814 
00815 static const WindowDesc _generate_landscape_desc(
00816   WDP_CENTER, 0, 0,
00817   WC_GENERATE_LANDSCAPE, WC_NONE,
00818   0,
00819   _nested_generate_landscape_widgets, lengthof(_nested_generate_landscape_widgets)
00820 );
00821 
00822 static const WindowDesc _heightmap_load_desc(
00823   WDP_CENTER, 0, 0,
00824   WC_GENERATE_LANDSCAPE, WC_NONE,
00825   0,
00826   _nested_heightmap_load_widgets, lengthof(_nested_heightmap_load_widgets)
00827 );
00828 
00829 static void _ShowGenerateLandscape(GenenerateLandscapeWindowMode mode)
00830 {
00831   uint x = 0;
00832   uint y = 0;
00833 
00834   DeleteWindowByClass(WC_GENERATE_LANDSCAPE);
00835 
00836   /* Always give a new seed if not editor */
00837   if (_game_mode != GM_EDITOR) _settings_newgame.game_creation.generation_seed = InteractiveRandom();
00838 
00839   if (mode == GLWM_HEIGHTMAP) {
00840     /* If the function returns negative, it means there was a problem loading the heightmap */
00841     if (!GetHeightmapDimensions(_file_to_saveload.name, &x, &y)) return;
00842   }
00843 
00844   GenerateLandscapeWindow *w = AllocateWindowDescFront<GenerateLandscapeWindow>((mode == GLWM_HEIGHTMAP) ? &_heightmap_load_desc : &_generate_landscape_desc, mode);
00845 
00846   if (mode == GLWM_HEIGHTMAP) {
00847     w->x = x;
00848     w->y = y;
00849     strecpy(w->name, _file_to_saveload.title, lastof(w->name));
00850   }
00851 
00852   SetWindowDirty(WC_GENERATE_LANDSCAPE, mode);
00853 }
00854 
00856 void ShowGenerateLandscape()
00857 {
00858   _ShowGenerateLandscape(GLWM_GENERATE);
00859 }
00860 
00862 void ShowHeightmapLoad()
00863 {
00864   _ShowGenerateLandscape(GLWM_HEIGHTMAP);
00865 }
00866 
00868 void StartScenarioEditor()
00869 {
00870   StartGeneratingLandscape(GLWM_SCENARIO);
00871 }
00872 
00877 void StartNewGameWithoutGUI(uint seed)
00878 {
00879   /* GenerateWorld takes care of the possible GENERATE_NEW_SEED value in 'seed' */
00880   _settings_newgame.game_creation.generation_seed = seed;
00881 
00882   StartGeneratingLandscape(GLWM_GENERATE);
00883 }
00884 
00885 struct CreateScenarioWindow : public Window
00886 {
00887   uint widget_id;
00888 
00889   CreateScenarioWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
00890   {
00891     this->InitNested(desc, window_number);
00892     this->LowerWidget(_settings_newgame.game_creation.landscape + WID_CS_TEMPERATE);
00893   }
00894 
00895   virtual void SetStringParameters(int widget) const
00896   {
00897     switch (widget) {
00898       case WID_CS_START_DATE_TEXT:
00899         SetDParam(0, ConvertYMDToDate(_settings_newgame.game_creation.starting_year, 0, 1));
00900         break;
00901 
00902       case WID_CS_MAPSIZE_X_PULLDOWN:
00903         SetDParam(0, 1 << _settings_newgame.game_creation.map_x);
00904         break;
00905 
00906       case WID_CS_MAPSIZE_Y_PULLDOWN:
00907         SetDParam(0, 1 << _settings_newgame.game_creation.map_y);
00908         break;
00909 
00910       case WID_CS_FLAT_LAND_HEIGHT_TEXT:
00911         SetDParam(0, _settings_newgame.game_creation.se_flat_world_height);
00912         break;
00913     }
00914   }
00915 
00916   virtual void OnPaint()
00917   {
00918     this->SetWidgetDisabledState(WID_CS_START_DATE_DOWN,       _settings_newgame.game_creation.starting_year <= MIN_YEAR);
00919     this->SetWidgetDisabledState(WID_CS_START_DATE_UP,         _settings_newgame.game_creation.starting_year >= MAX_YEAR);
00920     this->SetWidgetDisabledState(WID_CS_FLAT_LAND_HEIGHT_DOWN, _settings_newgame.game_creation.se_flat_world_height <= 0);
00921     this->SetWidgetDisabledState(WID_CS_FLAT_LAND_HEIGHT_UP,   _settings_newgame.game_creation.se_flat_world_height >= MAX_TILE_HEIGHT);
00922 
00923     this->SetWidgetLoweredState(WID_CS_TEMPERATE, _settings_newgame.game_creation.landscape == LT_TEMPERATE);
00924     this->SetWidgetLoweredState(WID_CS_ARCTIC,    _settings_newgame.game_creation.landscape == LT_ARCTIC);
00925     this->SetWidgetLoweredState(WID_CS_TROPICAL,  _settings_newgame.game_creation.landscape == LT_TROPIC);
00926     this->SetWidgetLoweredState(WID_CS_TOYLAND,   _settings_newgame.game_creation.landscape == LT_TOYLAND);
00927 
00928     this->DrawWidgets();
00929   }
00930 
00931   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
00932   {
00933     StringID str = STR_JUST_INT;
00934     switch (widget) {
00935       case WID_CS_START_DATE_TEXT:
00936         SetDParam(0, ConvertYMDToDate(MAX_YEAR, 0, 1));
00937         str = STR_BLACK_DATE_LONG;
00938         break;
00939 
00940       case WID_CS_MAPSIZE_X_PULLDOWN:
00941       case WID_CS_MAPSIZE_Y_PULLDOWN:
00942         SetDParam(0, MAX_MAP_SIZE);
00943         break;
00944 
00945       case WID_CS_FLAT_LAND_HEIGHT_TEXT:
00946         SetDParam(0, MAX_TILE_HEIGHT);
00947         break;
00948 
00949       default:
00950         return;
00951     }
00952     *size = GetStringBoundingBox(str);
00953     size->width += padding.width;
00954     size->height += padding.height;
00955   }
00956 
00957   virtual void OnClick(Point pt, int widget, int click_count)
00958   {
00959     switch (widget) {
00960       case WID_CS_TEMPERATE:
00961       case WID_CS_ARCTIC:
00962       case WID_CS_TROPICAL:
00963       case WID_CS_TOYLAND:
00964         this->RaiseWidget(_settings_newgame.game_creation.landscape + WID_CS_TEMPERATE);
00965         SetNewLandscapeType(widget - WID_CS_TEMPERATE);
00966         break;
00967 
00968       case WID_CS_MAPSIZE_X_PULLDOWN: // Mapsize X
00969         ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_x, WID_CS_MAPSIZE_X_PULLDOWN);
00970         break;
00971 
00972       case WID_CS_MAPSIZE_Y_PULLDOWN: // Mapsize Y
00973         ShowDropDownList(this, BuildMapsizeDropDown(), _settings_newgame.game_creation.map_y, WID_CS_MAPSIZE_Y_PULLDOWN);
00974         break;
00975 
00976       case WID_CS_EMPTY_WORLD: // Empty world / flat world
00977         StartGeneratingLandscape(GLWM_SCENARIO);
00978         break;
00979 
00980       case WID_CS_RANDOM_WORLD: // Generate
00981         ShowGenerateLandscape();
00982         break;
00983 
00984       case WID_CS_START_DATE_DOWN:
00985       case WID_CS_START_DATE_UP: // Year buttons
00986         /* Don't allow too fast scrolling */
00987         if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
00988           this->HandleButtonClick(widget);
00989           this->SetDirty();
00990 
00991           _settings_newgame.game_creation.starting_year = Clamp(_settings_newgame.game_creation.starting_year + widget - WID_CS_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
00992         }
00993         _left_button_clicked = false;
00994         break;
00995 
00996       case WID_CS_START_DATE_TEXT: // Year text
00997         this->widget_id = WID_CS_START_DATE_TEXT;
00998         SetDParam(0, _settings_newgame.game_creation.starting_year);
00999         ShowQueryString(STR_JUST_INT, STR_MAPGEN_START_DATE_QUERY_CAPT, 8, this, CS_NUMERAL, QSF_NONE);
01000         break;
01001 
01002       case WID_CS_FLAT_LAND_HEIGHT_DOWN:
01003       case WID_CS_FLAT_LAND_HEIGHT_UP: // Height level buttons
01004         /* Don't allow too fast scrolling */
01005         if (!(this->flags & WF_TIMEOUT) || this->timeout_timer <= 1) {
01006           this->HandleButtonClick(widget);
01007           this->SetDirty();
01008 
01009           _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, MAX_TILE_HEIGHT);
01010         }
01011         _left_button_clicked = false;
01012         break;
01013 
01014       case WID_CS_FLAT_LAND_HEIGHT_TEXT: // Height level text
01015         this->widget_id = WID_CS_FLAT_LAND_HEIGHT_TEXT;
01016         SetDParam(0, _settings_newgame.game_creation.se_flat_world_height);
01017         ShowQueryString(STR_JUST_INT, STR_SE_MAPGEN_FLAT_WORLD_HEIGHT_QUERY_CAPT, 3, this, CS_NUMERAL, QSF_NONE);
01018         break;
01019     }
01020   }
01021 
01022   virtual void OnTimeout()
01023   {
01024     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};
01025     for (const int *widget = raise_widgets; *widget != WIDGET_LIST_END; widget++) {
01026       if (this->IsWidgetLowered(*widget)) {
01027         this->RaiseWidget(*widget);
01028         this->SetWidgetDirty(*widget);
01029       }
01030     }
01031   }
01032 
01033   virtual void OnDropdownSelect(int widget, int index)
01034   {
01035     switch (widget) {
01036       case WID_CS_MAPSIZE_X_PULLDOWN: _settings_newgame.game_creation.map_x = index; break;
01037       case WID_CS_MAPSIZE_Y_PULLDOWN: _settings_newgame.game_creation.map_y = index; break;
01038     }
01039     this->SetDirty();
01040   }
01041 
01042   virtual void OnQueryTextFinished(char *str)
01043   {
01044     if (!StrEmpty(str)) {
01045       int32 value = atoi(str);
01046 
01047       switch (this->widget_id) {
01048         case WID_CS_START_DATE_TEXT:
01049           this->SetWidgetDirty(WID_CS_START_DATE_TEXT);
01050           _settings_newgame.game_creation.starting_year = Clamp(value, MIN_YEAR, MAX_YEAR);
01051           break;
01052 
01053         case WID_CS_FLAT_LAND_HEIGHT_TEXT:
01054           this->SetWidgetDirty(WID_CS_FLAT_LAND_HEIGHT_TEXT);
01055           _settings_newgame.game_creation.se_flat_world_height = Clamp(value, 0, MAX_TILE_HEIGHT);
01056           break;
01057       }
01058 
01059       this->SetDirty();
01060     }
01061   }
01062 };
01063 
01064 static const NWidgetPart _nested_create_scenario_widgets[] = {
01065   NWidget(NWID_HORIZONTAL),
01066     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01067     NWidget(WWT_CAPTION, COLOUR_BROWN), SetDataTip(STR_SE_MAPGEN_CAPTION, STR_NULL),
01068   EndContainer(),
01069   NWidget(WWT_PANEL, COLOUR_BROWN),
01070     NWidget(NWID_SPACER), SetMinimalSize(0, 10),
01071     /* Landscape style selection. */
01072     NWidget(NWID_HORIZONTAL), SetPIP(10, 3, 10),
01073       NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_TEMPERATE), SetDataTip(SPR_SELECT_TEMPERATE, STR_INTRO_TOOLTIP_TEMPERATE),
01074       NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_ARCTIC), SetDataTip(SPR_SELECT_SUB_ARCTIC, STR_INTRO_TOOLTIP_SUB_ARCTIC_LANDSCAPE),
01075       NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_TROPICAL), SetDataTip(SPR_SELECT_SUB_TROPICAL, STR_INTRO_TOOLTIP_SUB_TROPICAL_LANDSCAPE),
01076       NWidget(WWT_IMGBTN_2, COLOUR_ORANGE, WID_CS_TOYLAND), SetDataTip(SPR_SELECT_TOYLAND, STR_INTRO_TOOLTIP_TOYLAND_LANDSCAPE),
01077     EndContainer(),
01078     NWidget(NWID_HORIZONTAL), SetPIP(10, 8, 10),
01079       /* Green generation type buttons: 'Flat land' and 'Random land'. */
01080       NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
01081         NWidget(WWT_TEXTBTN, COLOUR_GREEN, WID_CS_EMPTY_WORLD), SetDataTip(STR_SE_MAPGEN_FLAT_WORLD, STR_SE_MAPGEN_FLAT_WORLD_TOOLTIP), SetFill(1, 1),
01082         NWidget(WWT_TEXTBTN, COLOUR_GREEN, WID_CS_RANDOM_WORLD), SetDataTip(STR_SE_MAPGEN_RANDOM_LAND, STR_TERRAFORM_TOOLTIP_GENERATE_RANDOM_LAND), SetFill(1, 1),
01083       EndContainer(),
01084       /* Labels + setting drop-downs */
01085       NWidget(NWID_VERTICAL), SetPIP(10, 6, 10),
01086         /* Map size. */
01087         NWidget(NWID_HORIZONTAL),
01088           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_MAPSIZE, STR_NULL), SetPadding(1, 0, 0, 0),
01089           NWidget(NWID_SPACER), SetMinimalSize(6, 0), SetFill(1, 0),
01090           NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_CS_MAPSIZE_X_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL), SetPadding(0, 4, 0, 0),
01091           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_BY, STR_NULL), SetPadding(1, 2, 0, 0),
01092           NWidget(WWT_DROPDOWN, COLOUR_ORANGE, WID_CS_MAPSIZE_Y_PULLDOWN), SetDataTip(STR_JUST_INT, STR_NULL),
01093         EndContainer(),
01094         /* Date. */
01095         NWidget(NWID_HORIZONTAL),
01096           NWidget(WWT_TEXT, COLOUR_ORANGE), SetDataTip(STR_MAPGEN_DATE, STR_NULL), SetPadding(1, 0, 0, 0),
01097           NWidget(NWID_SPACER), SetMinimalSize(6, 0), SetFill(1, 0),
01098           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),
01099           NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_CS_START_DATE_TEXT), SetDataTip(STR_BLACK_DATE_LONG, STR_NULL),
01100           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),
01101         EndContainer(),
01102         /* Flat map height. */
01103         NWidget(NWID_HORIZONTAL),
01104           NWidget(WWT_TEXT, COLOUR_ORANGE),
01105                         SetDataTip(STR_SE_MAPGEN_FLAT_WORLD_HEIGHT, STR_NULL), SetPadding(1, 0, 0, 0),
01106           NWidget(NWID_SPACER), SetMinimalSize(6, 0), SetFill(1, 0),
01107           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),
01108           NWidget(WWT_TEXTBTN, COLOUR_ORANGE, WID_CS_FLAT_LAND_HEIGHT_TEXT), SetDataTip(STR_BLACK_INT, STR_NULL),
01109           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),
01110         EndContainer(),
01111       EndContainer(),
01112     EndContainer(),
01113   EndContainer(),
01114 };
01115 
01116 static const WindowDesc _create_scenario_desc(
01117   WDP_CENTER, 0, 0,
01118   WC_GENERATE_LANDSCAPE, WC_NONE,
01119   WDF_UNCLICK_BUTTONS,
01120   _nested_create_scenario_widgets, lengthof(_nested_create_scenario_widgets)
01121 );
01122 
01124 void ShowCreateScenario()
01125 {
01126   DeleteWindowByClass(WC_GENERATE_LANDSCAPE);
01127   new CreateScenarioWindow(&_create_scenario_desc, GLWM_SCENARIO);
01128 }
01129 
01130 static const NWidgetPart _nested_generate_progress_widgets[] = {
01131   NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_GENERATION_WORLD, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01132   NWidget(WWT_PANEL, COLOUR_GREY),
01133     NWidget(NWID_HORIZONTAL), SetPIP(20, 0, 20),
01134       NWidget(NWID_VERTICAL), SetPIP(11, 8, 11),
01135         NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GP_PROGRESS_BAR), SetFill(1, 0),
01136         NWidget(WWT_EMPTY, INVALID_COLOUR, WID_GP_PROGRESS_TEXT), SetFill(1, 0),
01137         NWidget(WWT_TEXTBTN, COLOUR_WHITE, WID_GP_ABORT), SetDataTip(STR_GENERATION_ABORT, STR_NULL), SetFill(1, 0),
01138       EndContainer(),
01139     EndContainer(),
01140   EndContainer(),
01141 };
01142 
01143 
01144 static const WindowDesc _generate_progress_desc(
01145   WDP_CENTER, 0, 0,
01146   WC_MODAL_PROGRESS, WC_NONE,
01147   WDF_UNCLICK_BUTTONS,
01148   _nested_generate_progress_widgets, lengthof(_nested_generate_progress_widgets)
01149 );
01150 
01151 struct GenWorldStatus {
01152   uint percent;
01153   StringID cls;
01154   uint current;
01155   uint total;
01156   int timer;
01157 };
01158 
01159 static GenWorldStatus _gws;
01160 
01161 static const StringID _generation_class_table[]  = {
01162   STR_GENERATION_WORLD_GENERATION,
01163   STR_SCENEDIT_TOOLBAR_LANDSCAPE_GENERATION,
01164   STR_GENERATION_RIVER_GENERATION,
01165   STR_GENERATION_CLEARING_TILES,
01166   STR_SCENEDIT_TOOLBAR_TOWN_GENERATION,
01167   STR_SCENEDIT_TOOLBAR_INDUSTRY_GENERATION,
01168   STR_GENERATION_OBJECT_GENERATION,
01169   STR_GENERATION_TREE_GENERATION,
01170   STR_GENERATION_SETTINGUP_GAME,
01171   STR_GENERATION_PREPARING_TILELOOP,
01172   STR_GENERATION_PREPARING_SCRIPT,
01173   STR_GENERATION_PREPARING_GAME
01174 };
01175 assert_compile(lengthof(_generation_class_table) == GWP_CLASS_COUNT);
01176 
01177 
01178 static void AbortGeneratingWorldCallback(Window *w, bool confirmed)
01179 {
01180   if (confirmed) {
01181     AbortGeneratingWorld();
01182   } else if (HasModalProgress() && !IsGeneratingWorldAborted()) {
01183     SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
01184   }
01185 }
01186 
01187 struct GenerateProgressWindow : public Window {
01188 
01189   GenerateProgressWindow() : Window()
01190   {
01191     this->InitNested(&_generate_progress_desc);
01192   }
01193 
01194   virtual void OnClick(Point pt, int widget, int click_count)
01195   {
01196     switch (widget) {
01197       case WID_GP_ABORT:
01198         if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
01199         ShowQuery(
01200           STR_GENERATION_ABORT_CAPTION,
01201           STR_GENERATION_ABORT_MESSAGE,
01202           this,
01203           AbortGeneratingWorldCallback
01204         );
01205         break;
01206     }
01207   }
01208 
01209   virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
01210   {
01211     switch (widget) {
01212       case WID_GP_PROGRESS_BAR: {
01213         SetDParam(0, 100);
01214         *size = GetStringBoundingBox(STR_GENERATION_PROGRESS);
01215         /* We need some spacing for the 'border' */
01216         size->height += 8;
01217         size->width += 8;
01218         break;
01219       }
01220 
01221       case WID_GP_PROGRESS_TEXT:
01222         for (uint i = 0; i < GWP_CLASS_COUNT; i++) {
01223           size->width = max(size->width, GetStringBoundingBox(_generation_class_table[i]).width);
01224         }
01225         size->height = FONT_HEIGHT_NORMAL * 2 + WD_PAR_VSEP_NORMAL;
01226         break;
01227     }
01228   }
01229 
01230   virtual void DrawWidget(const Rect &r, int widget) const
01231   {
01232     switch (widget) {
01233       case WID_GP_PROGRESS_BAR:
01234         /* Draw the % complete with a bar and a text */
01235         DrawFrameRect(r.left, r.top, r.right, r.bottom, COLOUR_GREY, FR_BORDERONLY);
01236         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);
01237         SetDParam(0, _gws.percent);
01238         DrawString(r.left, r.right, r.top + 5, STR_GENERATION_PROGRESS, TC_FROMSTRING, SA_HOR_CENTER);
01239         break;
01240 
01241       case WID_GP_PROGRESS_TEXT:
01242         /* Tell which class we are generating */
01243         DrawString(r.left, r.right, r.top, _gws.cls, TC_FROMSTRING, SA_HOR_CENTER);
01244 
01245         /* And say where we are in that class */
01246         SetDParam(0, _gws.current);
01247         SetDParam(1, _gws.total);
01248         DrawString(r.left, r.right, r.top + FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL, STR_GENERATION_PROGRESS_NUM, TC_FROMSTRING, SA_HOR_CENTER);
01249     }
01250   }
01251 };
01252 
01256 void PrepareGenerateWorldProgress()
01257 {
01258   _gws.cls     = STR_GENERATION_WORLD_GENERATION;
01259   _gws.current = 0;
01260   _gws.total   = 0;
01261   _gws.percent = 0;
01262   _gws.timer   = 0; // Forces to paint the progress window immediately
01263 }
01264 
01268 void ShowGenerateWorldProgress()
01269 {
01270   if (BringWindowToFrontById(WC_MODAL_PROGRESS, 0)) return;
01271   new GenerateProgressWindow();
01272 }
01273 
01274 static void _SetGeneratingWorldProgress(GenWorldProgress cls, uint progress, uint total)
01275 {
01276   static const int percent_table[] = {0, 5, 14, 17, 20, 40, 60, 65, 80, 85, 95, 99, 100 };
01277   assert_compile(lengthof(percent_table) == GWP_CLASS_COUNT + 1);
01278   assert(cls < GWP_CLASS_COUNT);
01279 
01280   /* Do not run this function if we aren't in a thread */
01281   if (!IsGenerateWorldThreaded() && !_network_dedicated) return;
01282 
01283   if (IsGeneratingWorldAborted()) HandleGeneratingWorldAbortion();
01284 
01285   if (total == 0) {
01286     assert(_gws.cls == _generation_class_table[cls]);
01287     _gws.current += progress;
01288     assert(_gws.current <= _gws.total);
01289   } else {
01290     _gws.cls     = _generation_class_table[cls];
01291     _gws.current = progress;
01292     _gws.total   = total;
01293     _gws.percent = percent_table[cls];
01294   }
01295 
01296   /* Don't update the screen too often. So update it once in every once in a while... */
01297   if (!_network_dedicated && _gws.timer != 0 && _realtime_tick - _gws.timer < MODAL_PROGRESS_REDRAW_TIMEOUT) return;
01298 
01299   /* Percentage is about the number of completed tasks, so 'current - 1' */
01300   _gws.percent = percent_table[cls] + (percent_table[cls + 1] - percent_table[cls]) * (_gws.current == 0 ? 0 : _gws.current - 1) / _gws.total;
01301 
01302   if (_network_dedicated) {
01303     static uint last_percent = 0;
01304 
01305     /* Never display 0% */
01306     if (_gws.percent == 0) return;
01307     /* Reset if percent is lower than the last recorded */
01308     if (_gws.percent < last_percent) last_percent = 0;
01309     /* Display every 5%, but 6% is also very valid.. just not smaller steps than 5% */
01310     if (_gws.percent % 5 != 0 && _gws.percent <= last_percent + 5) return;
01311     /* Never show steps smaller than 2%, even if it is a mod 5% */
01312     if (_gws.percent <= last_percent + 2) return;
01313 
01314     DEBUG(net, 1, "Map generation percentage complete: %d", _gws.percent);
01315     last_percent = _gws.percent;
01316 
01317     /* Don't continue as dedicated never has a thread running */
01318     return;
01319   }
01320 
01321   SetWindowDirty(WC_MODAL_PROGRESS, 0);
01322   MarkWholeScreenDirty();
01323 
01324   /* Release the rights to the map generator, and acquire the rights to the
01325    * paint thread. The 'other' thread already has the paint thread rights so
01326    * this ensures us that we are waiting until the paint thread is done
01327    * before we reacquire the mapgen rights */
01328   _modal_progress_work_mutex->EndCritical();
01329   _modal_progress_paint_mutex->BeginCritical();
01330   _modal_progress_work_mutex->BeginCritical();
01331   _modal_progress_paint_mutex->EndCritical();
01332 
01333   _gws.timer = _realtime_tick;
01334 }
01335 
01344 void SetGeneratingWorldProgress(GenWorldProgress cls, uint total)
01345 {
01346   if (total == 0) return;
01347 
01348   _SetGeneratingWorldProgress(cls, 0, total);
01349 }
01350 
01358 void IncreaseGeneratingWorldProgress(GenWorldProgress cls)
01359 {
01360   /* In fact the param 'class' isn't needed.. but for some security reasons, we want it around */
01361   _SetGeneratingWorldProgress(cls, 1, 0);
01362 }