# genworld_gui.cpp

Go to the documentation of this file.

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