# genworld_gui.cpp

Go to the documentation of this file.

    00001 /* $Id: genworld_gui.cpp 13689 2008-07-09 19:30:44Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "heightmap.h"
    00008 #include "gui.h"
    00009 #include "window_gui.h"
    00010 #include "textbuf_gui.h"
    00011 #include "gfxinit.h"
    00012 #include "command_func.h"
    00013 #include "variables.h"
    00014 #include "settings_func.h"
    00015 #include "debug.h"
    00016 #include "genworld.h"
    00017 #include "network/network.h"
    00018 #include "thread.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 "gfx_func.h"
    00027 #include "settings_type.h"
    00028 #include "widgets/dropdown_type.h"
    00029 #include "widgets/dropdown_func.h"
    00030 #include "core/random_func.hpp"
    00031
    00032 #include "table/strings.h"
    00033 #include "table/sprites.h"
    00034
    00038 enum glwp_modes {
    00039   GLWP_GENERATE,
    00040   GLWP_HEIGHTMAP,
    00041   GLWP_SCENARIO,
    00042   GLWP_END
    00043 };
    00044
    00045 struct generate_d {
    00046   uint widget_id;
    00047   uint x;
    00048   uint y;
    00049   char name[64];
    00050 };
    00051
    00052 extern void SwitchMode(int new_mode);
    00053
    00054 static inline void SetNewLandscapeType(byte landscape)
    00055 {
    00056   _opt_newgame.landscape = landscape;
    00057   InvalidateWindowClasses(WC_SELECT_GAME);
    00058   InvalidateWindowClasses(WC_GENERATE_LANDSCAPE);
    00059 }
    00060
    00061 enum GenerateLandscapeWindowWidgets {
    00062   GLAND_TEMPERATE = 3,
    00063   GLAND_ARCTIC,
    00064   GLAND_TROPICAL,
    00065   GLAND_TOYLAND,
    00066
    00067   GLAND_MAPSIZE_X_TEXT,
    00068   GLAND_MAPSIZE_X_PULLDOWN,
    00069   GLAND_MAPSIZE_Y_TEXT,
    00070   GLAND_MAPSIZE_Y_PULLDOWN,
    00071
    00072   GLAND_TOWN_TEXT,
    00073   GLAND_TOWN_PULLDOWN,
    00074   GLAND_INDUSTRY_TEXT,
    00075   GLAND_INDUSTRY_PULLDOWN,
    00076
    00077   GLAND_RANDOM_TEXT,
    00078   GLAND_RANDOM_EDITBOX,
    00079   GLAND_RANDOM_BUTTON,
    00080
    00081   GLAND_GENERATE_BUTTON,
    00082
    00083   GLAND_START_DATE_TEXT1,
    00084   GLAND_START_DATE_DOWN,
    00085   GLAND_START_DATE_TEXT,
    00086   GLAND_START_DATE_UP,
    00087
    00088   GLAND_SNOW_LEVEL_TEXT1,
    00089   GLAND_SNOW_LEVEL_DOWN,
    00090   GLAND_SNOW_LEVEL_TEXT,
    00091   GLAND_SNOW_LEVEL_UP,
    00092
    00093   GLAND_TREE_TEXT,
    00094   GLAND_TREE_PULLDOWN,
    00095   GLAND_LANDSCAPE_TEXT,
    00096   GLAND_LANDSCAPE_PULLDOWN,
    00097   GLAND_HEIGHTMAP_ROTATION_TEXT = GLAND_LANDSCAPE_TEXT,
    00098   GLAND_HEIGHTMAP_ROTATION_PULLDOWN = GLAND_LANDSCAPE_PULLDOWN,
    00099
    00100   GLAND_TERRAIN_TEXT,
    00101   GLAND_TERRAIN_PULLDOWN,
    00102   GLAND_WATER_TEXT,
    00103   GLAND_WATER_PULLDOWN,
    00104   GLAND_SMOOTHNESS_TEXT,
    00105   GLAND_SMOOTHNESS_PULLDOWN
    00106 };
    00107
    00108 static const Widget _generate_landscape_widgets[] = {
    00109 {  WWT_CLOSEBOX,  RESIZE_NONE, 13,   0,  10,   0,  13, STR_00C5,                     STR_018B_CLOSE_WINDOW},
    00110 {    WWT_CAPTION, RESIZE_NONE, 13,  11, 337,   0,  13, STR_WORLD_GENERATION_CAPTION, STR_NULL},
    00111 {      WWT_PANEL, RESIZE_NONE, 13,   0, 337,  14, 267, 0x0,                          STR_NULL},
    00112
    00113 {   WWT_IMGBTN_2, RESIZE_NONE, 12,  10,  86,  24,  78, SPR_SELECT_TEMPERATE,         STR_030E_SELECT_TEMPERATE_LANDSCAPE},
    00114 {   WWT_IMGBTN_2, RESIZE_NONE, 12,  90, 166,  24,  78, SPR_SELECT_SUB_ARCTIC,        STR_030F_SELECT_SUB_ARCTIC_LANDSCAPE},
    00115 {   WWT_IMGBTN_2, RESIZE_NONE, 12, 170, 246,  24,  78, SPR_SELECT_SUB_TROPICAL,      STR_0310_SELECT_SUB_TROPICAL_LANDSCAPE},
    00116 {   WWT_IMGBTN_2, RESIZE_NONE, 12, 250, 326,  24,  78, SPR_SELECT_TOYLAND,           STR_0311_SELECT_TOYLAND_LANDSCAPE},
    00117
    00118 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110,  91, 101, STR_MAPSIZE,                  STR_NULL},
    00119 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 161,  90, 101, STR_NUM_1,                    STR_NULL}, // Mapsize X
    00120 {       WWT_TEXT, RESIZE_NONE,  0, 168, 176,  91, 101, STR_BY,                       STR_NULL},
    00121 {   WWT_DROPDOWN, RESIZE_NONE, 12, 180, 227,  90, 101, STR_NUM_2,                    STR_NULL}, // Mapsize Y
    00122
    00123 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 113, 123, STR_NUMBER_OF_TOWNS,          STR_NULL},
    00124 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 175, 112, 123, 0x0,                          STR_NULL}, // Number of towns
    00125
    00126 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 131, 141, STR_NUMBER_OF_INDUSTRIES,     STR_NULL},
    00127 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 175, 130, 141, 0x0,                          STR_NULL}, // Number of industries
    00128
    00129 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 153, 163, STR_RANDOM_SEED,              STR_NULL},
    00130 {      WWT_PANEL, RESIZE_NONE, 15, 114, 207, 152, 163, 0x0,                          STR_RANDOM_SEED_HELP}, // Edit box for seed
    00131 {    WWT_TEXTBTN, RESIZE_NONE, 12, 216, 326, 152, 163, STR_RANDOM,                   STR_RANDOM_HELP},
    00132
    00133 {    WWT_TEXTBTN, RESIZE_NONE,  6, 243, 326, 228, 257, STR_GENERATE,                 STR_NULL}, // Generate button
    00134
    00135 {       WWT_TEXT, RESIZE_NONE,  0, 182, 212, 113, 123, STR_DATE,                     STR_NULL},
    00136 {     WWT_IMGBTN, RESIZE_NONE, 12, 216, 227, 112, 123, SPR_ARROW_DOWN,               STR_029E_MOVE_THE_STARTING_DATE},
    00137 {    WWT_TEXTBTN, RESIZE_NONE, 12, 228, 314, 112, 123, STR_GENERATE_DATE,            STR_NULL},
    00138 {     WWT_IMGBTN, RESIZE_NONE, 12, 315, 326, 112, 123, SPR_ARROW_UP,                 STR_029F_MOVE_THE_STARTING_DATE},
    00139
    00140 {       WWT_TEXT, RESIZE_NONE,  0, 182, 278, 131, 141, STR_SNOW_LINE_HEIGHT,         STR_NULL},
    00141 {     WWT_IMGBTN, RESIZE_NONE, 12, 282, 293, 130, 141, SPR_ARROW_DOWN,               STR_SNOW_LINE_DOWN},
    00142 {    WWT_TEXTBTN, RESIZE_NONE, 12, 294, 314, 130, 141, STR_NUM_3,                    STR_NULL},
    00143 {     WWT_IMGBTN, RESIZE_NONE, 12, 315, 326, 130, 141, SPR_ARROW_UP,                 STR_SNOW_LINE_UP},
    00144
    00145 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 193, 203, STR_TREE_PLACER,              STR_NULL},
    00146 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 231, 192, 203, 0x0,                          STR_NULL}, // Tree placer
    00147
    00148 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 175, 185, STR_LAND_GENERATOR,           STR_NULL},
    00149 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 231, 174, 185, 0x0,                          STR_NULL}, // Landscape generator
    00150
    00151 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 211, 221, STR_TERRAIN_TYPE,             STR_NULL},
    00152 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 231, 210, 221, 0x0,                          STR_NULL}, // Terrain type
    00153
    00154 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 229, 239, STR_QUANTITY_OF_SEA_LAKES,    STR_NULL},
    00155 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 231, 228, 239, 0x0,                          STR_NULL}, // Water quantity
    00156
    00157 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 245, 257, STR_SMOOTHNESS,               STR_NULL},
    00158 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 231, 246, 257, 0x0,                          STR_NULL}, // Map smoothness
    00159 {   WIDGETS_END},
    00160 };
    00161
    00162 static const Widget _heightmap_load_widgets[] = {
    00163 {   WWT_CLOSEBOX, RESIZE_NONE, 13,   0,  10,   0,  13, STR_00C5,                     STR_018B_CLOSE_WINDOW},
    00164 {    WWT_CAPTION, RESIZE_NONE, 13,  11, 337,   0,  13, STR_WORLD_GENERATION_CAPTION, STR_NULL},
    00165 {      WWT_PANEL, RESIZE_NONE, 13,   0, 337,  14, 235, 0x0,                          STR_NULL},
    00166
    00167 {   WWT_IMGBTN_2, RESIZE_NONE, 12,  10,  86,  24,  78, SPR_SELECT_TEMPERATE,        STR_030E_SELECT_TEMPERATE_LANDSCAPE},
    00168 {   WWT_IMGBTN_2, RESIZE_NONE, 12,  90, 166,  24,  78, SPR_SELECT_SUB_ARCTIC,       STR_030F_SELECT_SUB_ARCTIC_LANDSCAPE},
    00169 {   WWT_IMGBTN_2, RESIZE_NONE, 12, 170, 246,  24,  78, SPR_SELECT_SUB_TROPICAL,     STR_0310_SELECT_SUB_TROPICAL_LANDSCAPE},
    00170 {   WWT_IMGBTN_2, RESIZE_NONE, 12, 250, 326,  24,  78, SPR_SELECT_TOYLAND,          STR_0311_SELECT_TOYLAND_LANDSCAPE},
    00171
    00172 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 113, 123, STR_MAPSIZE,                  STR_NULL},
    00173 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 161, 112, 123, STR_NUM_1,                    STR_NULL}, // Mapsize X
    00174 {       WWT_TEXT, RESIZE_NONE,  0, 168, 176, 113, 123, STR_BY,                       STR_NULL},
    00175 {   WWT_DROPDOWN, RESIZE_NONE, 12, 180, 227, 112, 123, STR_NUM_2,                    STR_NULL}, // Mapsize Y
    00176
    00177 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 135, 145, STR_NUMBER_OF_TOWNS,          STR_NULL},
    00178 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 175, 134, 145, 0x0,                          STR_NULL}, // Number of towns
    00179
    00180 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 153, 163, STR_NUMBER_OF_INDUSTRIES,     STR_NULL},
    00181 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 175, 152, 163, 0x0,                          STR_NULL}, // Number of industries
    00182
    00183 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 175, 185, STR_RANDOM_SEED,              STR_NULL},
    00184 {      WWT_PANEL, RESIZE_NONE, 15, 114, 207, 174, 185, 0x0,                          STR_RANDOM_SEED_HELP}, // Edit box for seed
    00185 {    WWT_TEXTBTN, RESIZE_NONE, 12, 216, 326, 174, 185, STR_RANDOM,                   STR_RANDOM_HELP},
    00186
    00187 {    WWT_TEXTBTN, RESIZE_NONE,  6, 243, 326, 196, 225, STR_GENERATE,                 STR_NULL}, // Generate button
    00188
    00189 {       WWT_TEXT, RESIZE_NONE,  0, 182, 212, 135, 145, STR_DATE,                     STR_NULL},
    00190 {     WWT_IMGBTN, RESIZE_NONE, 12, 216, 227, 134, 145, SPR_ARROW_DOWN,               STR_029E_MOVE_THE_STARTING_DATE},
    00191 {    WWT_TEXTBTN, RESIZE_NONE, 12, 228, 314, 134, 145, STR_GENERATE_DATE,            STR_NULL},
    00192 {     WWT_IMGBTN, RESIZE_NONE, 12, 315, 326, 134, 145, SPR_ARROW_UP,                 STR_029F_MOVE_THE_STARTING_DATE},
    00193
    00194 {       WWT_TEXT, RESIZE_NONE,  0, 182, 278, 153, 163, STR_SNOW_LINE_HEIGHT,         STR_NULL},
    00195 {     WWT_IMGBTN, RESIZE_NONE, 12, 282, 293, 152, 163, SPR_ARROW_DOWN,               STR_SNOW_LINE_DOWN},
    00196 {    WWT_TEXTBTN, RESIZE_NONE, 12, 294, 314, 152, 163, STR_NUM_3,                    STR_NULL},
    00197 {     WWT_IMGBTN, RESIZE_NONE, 12, 315, 326, 152, 163, SPR_ARROW_UP,                 STR_SNOW_LINE_UP},
    00198
    00199 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 197, 207, STR_TREE_PLACER,              STR_NULL},
    00200 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 231, 196, 207, STR_0225,                     STR_NULL}, // Tree placer
    00201
    00202 {       WWT_TEXT, RESIZE_NONE,  0,  12, 110, 215, 225, STR_HEIGHTMAP_ROTATION,       STR_NULL},
    00203 {   WWT_DROPDOWN, RESIZE_NONE, 12, 114, 231, 214, 225, STR_0225,                     STR_NULL}, // Heightmap rotation
    00204
    00205 {   WIDGETS_END},
    00206 };
    00207
    00208 void StartGeneratingLandscape(glwp_modes mode)
    00209 {
    00210   DeleteAllNonVitalWindows();
    00211
    00212   /* Copy all XXX_newgame to XXX when coming from outside the editor */
    00213   UpdatePatches();
    00214   _opt = _opt_newgame;
    00215   _opt_ptr = &_opt;
    00216   ResetGRFConfig(true);
    00217
    00218   SndPlayFx(SND_15_BEEP);
    00219   switch (mode) {
    00220     case GLWP_GENERATE:  _switch_mode = (_game_mode == GM_EDITOR) ? SM_GENRANDLAND    : SM_NEWGAME;         break;
    00221     case GLWP_HEIGHTMAP: _switch_mode = (_game_mode == GM_EDITOR) ? SM_LOAD_HEIGHTMAP : SM_START_HEIGHTMAP; break;
    00222     case GLWP_SCENARIO:  _switch_mode = SM_EDITOR; break;
    00223     default: NOT_REACHED();
    00224   }
    00225 }
    00226
    00227 static void LandscapeGenerationCallback(Window *w, bool confirmed)
    00228 {
    00229   if (confirmed) StartGeneratingLandscape((glwp_modes)w->window_number);
    00230 }
    00231
    00232 static DropDownList *BuildMapsizeDropDown()
    00233 {
    00234   DropDownList *list = new DropDownList();
    00235
    00236   for (uint i = 6; i <= 11; i++) {
    00237     DropDownListParamStringItem *item = new DropDownListParamStringItem(STR_JUST_INT, i, false);
    00238     item->SetParam(0, 1 << i);
    00239     list->push_back(item);
    00240   }
    00241
    00242   return list;
    00243 }
    00244
    00245 static void GenerateLandscapeWndProc(Window *w, WindowEvent *e)
    00246 {
    00247   static const StringID elevations[]  = {STR_682A_VERY_FLAT, STR_682B_FLAT, STR_682C_HILLY, STR_682D_MOUNTAINOUS, INVALID_STRING_ID};
    00248   static const StringID sea_lakes[]   = {STR_VERY_LOW, STR_6820_LOW, STR_6821_MEDIUM, STR_6822_HIGH, INVALID_STRING_ID};
    00249   static const StringID smoothness[]  = {STR_CONFIG_PATCHES_ROUGHNESS_OF_TERRAIN_VERY_SMOOTH, STR_CONFIG_PATCHES_ROUGHNESS_OF_TERRAIN_SMOOTH, STR_CONFIG_PATCHES_ROUGHNESS_OF_TERRAIN_ROUGH, STR_CONFIG_PATCHES_ROUGHNESS_OF_TERRAIN_VERY_ROUGH, INVALID_STRING_ID};
    00250   static const StringID tree_placer[] = {STR_CONFIG_PATCHES_TREE_PLACER_NONE, STR_CONFIG_PATCHES_TREE_PLACER_ORIGINAL, STR_CONFIG_PATCHES_TREE_PLACER_IMPROVED, INVALID_STRING_ID};
    00251   static const StringID rotation[]    = {STR_CONFIG_PATCHES_HEIGHTMAP_ROTATION_COUNTER_CLOCKWISE, STR_CONFIG_PATCHES_HEIGHTMAP_ROTATION_CLOCKWISE, INVALID_STRING_ID};
    00252   static const StringID landscape[]   = {STR_CONFIG_PATCHES_LAND_GENERATOR_ORIGINAL, STR_CONFIG_PATCHES_LAND_GENERATOR_TERRA_GENESIS, INVALID_STRING_ID};
    00253   static const StringID num_towns[]   = {STR_NUM_VERY_LOW, STR_6816_LOW, STR_6817_NORMAL, STR_6818_HIGH, INVALID_STRING_ID};
    00254   static const StringID num_inds[]    = {STR_NONE, STR_NUM_VERY_LOW, STR_6816_LOW, STR_6817_NORMAL, STR_6818_HIGH, INVALID_STRING_ID};
    00255
    00256   /* Data used for the generate seed edit box */
    00257   static querystr_d _genseed_query;
    00258   static char _genseed_buffer[11];
    00259
    00260   glwp_modes mode = (glwp_modes)w->window_number;
    00261
    00262   switch (e->event) {
    00263     case WE_CREATE:
    00264       w->LowerWidget(_opt_newgame.landscape + GLAND_TEMPERATE);
    00265
    00266       snprintf(_genseed_buffer, sizeof(_genseed_buffer), "%u", _patches_newgame.generation_seed);
    00267       InitializeTextBuffer(&_genseed_query.text, _genseed_buffer, lengthof(_genseed_buffer), 120);
    00268       _genseed_query.caption = STR_NULL;
    00269       _genseed_query.afilter = CS_NUMERAL;
    00270       break;
    00271
    00272     case WE_PAINT:
    00273       /* You can't select smoothness if not terragenesis */
    00274       if (mode == GLWP_GENERATE) {
    00275         w->SetWidgetDisabledState(GLAND_SMOOTHNESS_PULLDOWN, _patches_newgame.land_generator == 0);
    00276       }
    00277       /* Disable snowline if not hilly */
    00278       w->SetWidgetDisabledState(GLAND_SNOW_LEVEL_TEXT, _opt_newgame.landscape != LT_ARCTIC);
    00279       /* Disable town, industry and trees in SE */
    00280       w->SetWidgetDisabledState(GLAND_TOWN_PULLDOWN,     _game_mode == GM_EDITOR);
    00281       w->SetWidgetDisabledState(GLAND_INDUSTRY_PULLDOWN, _game_mode == GM_EDITOR);
    00282       w->SetWidgetDisabledState(GLAND_TREE_PULLDOWN,     _game_mode == GM_EDITOR);
    00283
    00284       w->SetWidgetDisabledState(GLAND_START_DATE_DOWN, _patches_newgame.starting_year <= MIN_YEAR);
    00285       w->SetWidgetDisabledState(GLAND_START_DATE_UP,   _patches_newgame.starting_year >= MAX_YEAR);
    00286       w->SetWidgetDisabledState(GLAND_SNOW_LEVEL_DOWN, _patches_newgame.snow_line_height <= 2 || _opt_newgame.landscape != LT_ARCTIC);
    00287       w->SetWidgetDisabledState(GLAND_SNOW_LEVEL_UP,   _patches_newgame.snow_line_height >= MAX_SNOWLINE_HEIGHT || _opt_newgame.landscape != LT_ARCTIC);
    00288
    00289       w->SetWidgetLoweredState(GLAND_TEMPERATE, _opt_newgame.landscape == LT_TEMPERATE);
    00290       w->SetWidgetLoweredState(GLAND_ARCTIC,    _opt_newgame.landscape == LT_ARCTIC);
    00291       w->SetWidgetLoweredState(GLAND_TROPICAL,  _opt_newgame.landscape == LT_TROPIC);
    00292       w->SetWidgetLoweredState(GLAND_TOYLAND,   _opt_newgame.landscape == LT_TOYLAND);
    00293
    00294       if (_game_mode == GM_EDITOR) {
    00295         w->widget[GLAND_TOWN_PULLDOWN].data     = STR_6836_OFF;
    00296         w->widget[GLAND_INDUSTRY_PULLDOWN].data = STR_6836_OFF;
    00297       } else {
    00298         w->widget[GLAND_TOWN_PULLDOWN].data     = num_towns[_opt_newgame.diff.number_towns];
    00299         w->widget[GLAND_INDUSTRY_PULLDOWN].data = num_inds[_opt_newgame.diff.number_industries];
    00300       }
    00301
    00302       if (mode == GLWP_GENERATE) {
    00303         w->widget[GLAND_LANDSCAPE_PULLDOWN].data  = landscape[_patches_newgame.land_generator];
    00304         w->widget[GLAND_TREE_PULLDOWN].data       = tree_placer[_patches_newgame.tree_placer];
    00305         w->widget[GLAND_TERRAIN_PULLDOWN].data    = elevations[_opt_newgame.diff.terrain_type];
    00306         w->widget[GLAND_WATER_PULLDOWN].data      = sea_lakes[_opt_newgame.diff.quantity_sea_lakes];
    00307         w->widget[GLAND_SMOOTHNESS_PULLDOWN].data = smoothness[_patches_newgame.tgen_smoothness];
    00308       } else {
    00309         w->widget[GLAND_TREE_PULLDOWN].data               = tree_placer[_patches_newgame.tree_placer];
    00310         w->widget[GLAND_HEIGHTMAP_ROTATION_PULLDOWN].data = rotation[_patches_newgame.heightmap_rotation];
    00311       }
    00312
    00313       /* Set parameters for widget text that requires them. */
    00314       SetDParam(0, ConvertYMDToDate(_patches_newgame.starting_year, 0, 1)); // GLAND_START_DATE_TEXT
    00315       SetDParam(1, 1 << _patches_newgame.map_x); // GLAND_MAPSIZE_X_PULLDOWN
    00316       SetDParam(2, 1 << _patches_newgame.map_y); // GLAND_MAPSIZE_Y_PULLDOWN
    00317       SetDParam(3, _patches_newgame.snow_line_height); // GLAND_SNOW_LEVEL_TEXT
    00318
    00319       DrawWindowWidgets(w);
    00320
    00321       DrawEditBox(w, &_genseed_query, GLAND_RANDOM_EDITBOX);
    00322
    00323       if (mode != GLWP_GENERATE) {
    00324         char buffer[512];
    00325
    00326         if (_patches_newgame.heightmap_rotation == HM_CLOCKWISE) {
    00327           SetDParam(0, WP(w, generate_d).y);
    00328           SetDParam(1, WP(w, generate_d).x);
    00329         } else {
    00330           SetDParam(0, WP(w, generate_d).x);
    00331           SetDParam(1, WP(w, generate_d).y);
    00332         }
    00333         GetString(buffer, STR_HEIGHTMAP_SIZE, lastof(buffer));
    00334         DrawStringRightAligned(326, 91, STR_HEIGHTMAP_SIZE, TC_BLACK);
    00335
    00336         DrawString( 12,  91, STR_HEIGHTMAP_NAME, TC_BLACK);
    00337         SetDParamStr(0, WP(w, generate_d).name);
    00338         DrawStringTruncated(114,  91, STR_ORANGE, TC_BLACK, 326 - 114 - GetStringBoundingBox(buffer).width - 5);
    00339       }
    00340       break;
    00341
    00342     case WE_CLICK:
    00343       switch (e->we.click.widget) {
    00344         case 0: DeleteWindow(w); break;
    00345
    00346         case GLAND_TEMPERATE:
    00347         case GLAND_ARCTIC:
    00348         case GLAND_TROPICAL:
    00349         case GLAND_TOYLAND:
    00350           w->RaiseWidget(_opt_newgame.landscape + GLAND_TEMPERATE);
    00351           SetNewLandscapeType(e->we.click.widget - GLAND_TEMPERATE);
    00352           break;
    00353
    00354         case GLAND_MAPSIZE_X_PULLDOWN: // Mapsize X
    00355           ShowDropDownList(w, BuildMapsizeDropDown(), _patches_newgame.map_x, GLAND_MAPSIZE_X_PULLDOWN);
    00356           break;
    00357
    00358         case GLAND_MAPSIZE_Y_PULLDOWN: // Mapsize Y
    00359           ShowDropDownList(w, BuildMapsizeDropDown(), _patches_newgame.map_y, GLAND_MAPSIZE_Y_PULLDOWN);
    00360           break;
    00361
    00362         case GLAND_TOWN_PULLDOWN: // Number of towns
    00363           ShowDropDownMenu(w, num_towns, _opt_newgame.diff.number_towns, GLAND_TOWN_PULLDOWN, 0, 0);
    00364           break;
    00365
    00366         case GLAND_INDUSTRY_PULLDOWN: // Number of industries
    00367           ShowDropDownMenu(w, num_inds, _opt_newgame.diff.number_industries, GLAND_INDUSTRY_PULLDOWN, 0, 0);
    00368           break;
    00369
    00370         case GLAND_RANDOM_BUTTON: // Random seed
    00371           _patches_newgame.generation_seed = InteractiveRandom();
    00372           snprintf(_genseed_buffer, lengthof(_genseed_buffer), "%u", _patches_newgame.generation_seed);
    00373           UpdateTextBufferSize(&_genseed_query.text);
    00374           SetWindowDirty(w);
    00375           break;
    00376
    00377         case GLAND_GENERATE_BUTTON: // Generate
    00378
    00379           UpdatePatches();
    00380
    00381           if (_patches.town_layout == TL_NO_ROADS) {
    00382             ShowQuery(
    00383               STR_TOWN_LAYOUT_WARNING_CAPTION,
    00384               STR_TOWN_LAYOUT_WARNING_MESSAGE,
    00385               w,
    00386               LandscapeGenerationCallback);
    00387           } else if (mode == GLWP_HEIGHTMAP &&
    00388               (WP(w, generate_d).x * 2 < (1U << _patches_newgame.map_x) ||
    00389               WP(w, generate_d).x / 2 > (1U << _patches_newgame.map_x) ||
    00390               WP(w, generate_d).y * 2 < (1U << _patches_newgame.map_y) ||
    00391               WP(w, generate_d).y / 2 > (1U << _patches_newgame.map_y))) {
    00392             ShowQuery(
    00393               STR_HEIGHTMAP_SCALE_WARNING_CAPTION,
    00394               STR_HEIGHTMAP_SCALE_WARNING_MESSAGE,
    00395               w,
    00396               LandscapeGenerationCallback);
    00397           } else {
    00398             StartGeneratingLandscape(mode);
    00399           }
    00400           break;
    00401
    00402         case GLAND_START_DATE_DOWN:
    00403         case GLAND_START_DATE_UP: // Year buttons
    00404           /* Don't allow too fast scrolling */
    00405           if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
    00406             w->HandleButtonClick(e->we.click.widget);
    00407             SetWindowDirty(w);
    00408
    00409             _patches_newgame.starting_year = Clamp(_patches_newgame.starting_year + e->we.click.widget - GLAND_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
    00410           }
    00411           _left_button_clicked = false;
    00412           break;
    00413
    00414         case GLAND_START_DATE_TEXT: // Year text
    00415           WP(w, generate_d).widget_id = GLAND_START_DATE_TEXT;
    00416           SetDParam(0, _patches_newgame.starting_year);
    00417           ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_START_DATE_QUERY_CAPT, 8, 100, w, CS_NUMERAL);
    00418           break;
    00419
    00420         case GLAND_SNOW_LEVEL_DOWN:
    00421         case GLAND_SNOW_LEVEL_UP: // Snow line buttons
    00422           /* Don't allow too fast scrolling */
    00423           if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
    00424             w->HandleButtonClick(e->we.click.widget);
    00425             SetWindowDirty(w);
    00426
    00427             _patches_newgame.snow_line_height = Clamp(_patches_newgame.snow_line_height + e->we.click.widget - GLAND_SNOW_LEVEL_TEXT, 2, MAX_SNOWLINE_HEIGHT);
    00428           }
    00429           _left_button_clicked = false;
    00430           break;
    00431
    00432         case GLAND_SNOW_LEVEL_TEXT: // Snow line text
    00433           WP(w, generate_d).widget_id = GLAND_SNOW_LEVEL_TEXT;
    00434           SetDParam(0, _patches_newgame.snow_line_height);
    00435           ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_SNOW_LINE_QUERY_CAPT, 3, 100, w, CS_NUMERAL);
    00436           break;
    00437
    00438         case GLAND_TREE_PULLDOWN: // Tree placer
    00439           ShowDropDownMenu(w, tree_placer, _patches_newgame.tree_placer, GLAND_TREE_PULLDOWN, 0, 0);
    00440           break;
    00441
    00442         case GLAND_LANDSCAPE_PULLDOWN: // Landscape generator OR Heightmap rotation
    00443         /* case GLAND_HEIGHTMAP_ROTATION_TEXT: case GLAND_HEIGHTMAP_ROTATION_PULLDOWN:*/
    00444           if (mode == GLWP_HEIGHTMAP) {
    00445             ShowDropDownMenu(w, rotation, _patches_newgame.heightmap_rotation, GLAND_HEIGHTMAP_ROTATION_PULLDOWN, 0, 0);
    00446           } else {
    00447             ShowDropDownMenu(w, landscape, _patches_newgame.land_generator, GLAND_LANDSCAPE_PULLDOWN, 0, 0);
    00448           }
    00449           break;
    00450
    00451         case GLAND_TERRAIN_PULLDOWN: // Terrain type
    00452           ShowDropDownMenu(w, elevations, _opt_newgame.diff.terrain_type, GLAND_TERRAIN_PULLDOWN, 0, 0);
    00453           break;
    00454
    00455         case GLAND_WATER_PULLDOWN: // Water quantity
    00456           ShowDropDownMenu(w, sea_lakes, _opt_newgame.diff.quantity_sea_lakes, GLAND_WATER_PULLDOWN, 0, 0);
    00457           break;
    00458
    00459         case GLAND_SMOOTHNESS_PULLDOWN: // Map smoothness
    00460           ShowDropDownMenu(w, smoothness, _patches_newgame.tgen_smoothness, GLAND_SMOOTHNESS_PULLDOWN, 0, 0);
    00461           break;
    00462       }
    00463       break;
    00464
    00465     case WE_MOUSELOOP:
    00466       HandleEditBox(w, &_genseed_query, GLAND_RANDOM_EDITBOX);
    00467       break;
    00468
    00469     case WE_KEYPRESS:
    00470       HandleEditBoxKey(w, &_genseed_query, GLAND_RANDOM_EDITBOX, e);
    00471       /* the seed is unsigned, therefore atoi cannot be used.
    00472        * As 2^32 - 1 (MAX_UVALUE(uint32)) is a 'magic' value
    00473        * (use random seed) it should not be possible to be
    00474        * entered into the input field; the generate seed
    00475        * button can be used instead. */
    00476       _patches_newgame.generation_seed = minu(strtoul(_genseed_buffer, NULL, 10), MAX_UVALUE(uint32) - 1);
    00477       break;
    00478
    00479     case WE_DROPDOWN_SELECT:
    00480       switch (e->we.dropdown.button) {
    00481         case GLAND_MAPSIZE_X_PULLDOWN:  _patches_newgame.map_x = e->we.dropdown.index; break;
    00482         case GLAND_MAPSIZE_Y_PULLDOWN:  _patches_newgame.map_y = e->we.dropdown.index; break;
    00483         case GLAND_TREE_PULLDOWN:       _patches_newgame.tree_placer = e->we.dropdown.index; break;
    00484         case GLAND_SMOOTHNESS_PULLDOWN: _patches_newgame.tgen_smoothness = e->we.dropdown.index;  break;
    00485
    00486         case GLAND_TOWN_PULLDOWN:
    00487           _opt_newgame.diff.number_towns = e->we.dropdown.index;
    00488           if (_opt_newgame.diff_level != 3) ShowErrorMessage(INVALID_STRING_ID, STR_DIFFICULTY_TO_CUSTOM, 0, 0);
    00489           DoCommandP(0, 2, _opt_newgame.diff.number_towns, NULL, CMD_CHANGE_DIFFICULTY_LEVEL);
    00490           break;
    00491
    00492         case GLAND_INDUSTRY_PULLDOWN:
    00493           _opt_newgame.diff.number_industries = e->we.dropdown.index;
    00494           if (_opt_newgame.diff_level != 3) ShowErrorMessage(INVALID_STRING_ID, STR_DIFFICULTY_TO_CUSTOM, 0, 0);
    00495           DoCommandP(0, 3, _opt_newgame.diff.number_industries, NULL, CMD_CHANGE_DIFFICULTY_LEVEL);
    00496           break;
    00497
    00498         case GLAND_LANDSCAPE_PULLDOWN:
    00499         /* case GLAND_HEIGHTMAP_PULLDOWN: */
    00500           if (mode == GLWP_HEIGHTMAP) {
    00501             _patches_newgame.heightmap_rotation = e->we.dropdown.index;
    00502           } else {
    00503             _patches_newgame.land_generator = e->we.dropdown.index;
    00504           }
    00505           break;
    00506
    00507         case GLAND_TERRAIN_PULLDOWN:
    00508           _opt_newgame.diff.terrain_type = e->we.dropdown.index;
    00509           if (_opt_newgame.diff_level != 3) ShowErrorMessage(INVALID_STRING_ID, STR_DIFFICULTY_TO_CUSTOM, 0, 0);
    00510           DoCommandP(0, 12, _opt_newgame.diff.terrain_type, NULL, CMD_CHANGE_DIFFICULTY_LEVEL);
    00511           break;
    00512
    00513         case GLAND_WATER_PULLDOWN:
    00514           _opt_newgame.diff.quantity_sea_lakes = e->we.dropdown.index;
    00515           if (_opt_newgame.diff_level != 3) ShowErrorMessage(INVALID_STRING_ID, STR_DIFFICULTY_TO_CUSTOM, 0, 0);
    00516           DoCommandP(0, 13, _opt_newgame.diff.quantity_sea_lakes, NULL, CMD_CHANGE_DIFFICULTY_LEVEL);
    00517           break;
    00518       }
    00519       SetWindowDirty(w);
    00520       break;
    00521
    00522     case WE_ON_EDIT_TEXT:
    00523       if (e->we.edittext.str != NULL) {
    00524         int32 value = atoi(e->we.edittext.str);
    00525
    00526         switch (WP(w, generate_d).widget_id) {
    00527           case GLAND_START_DATE_TEXT:
    00528             w->InvalidateWidget(GLAND_START_DATE_TEXT);
    00529             _patches_newgame.starting_year = Clamp(value, MIN_YEAR, MAX_YEAR);
    00530             break;
    00531
    00532           case GLAND_SNOW_LEVEL_TEXT:
    00533             w->InvalidateWidget(GLAND_SNOW_LEVEL_TEXT);
    00534             _patches_newgame.snow_line_height = Clamp(value, 2, MAX_SNOWLINE_HEIGHT);
    00535             break;
    00536         }
    00537
    00538         SetWindowDirty(w);
    00539       }
    00540       break;
    00541   }
    00542 }
    00543
    00544 static const WindowDesc _generate_landscape_desc = {
    00545   WDP_CENTER, WDP_CENTER, 338, 268, 338, 268,
    00546   WC_GENERATE_LANDSCAPE, WC_NONE,
    00547   WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
    00548   _generate_landscape_widgets,
    00549   GenerateLandscapeWndProc,
    00550 };
    00551
    00552 static const WindowDesc _heightmap_load_desc = {
    00553   WDP_CENTER, WDP_CENTER, 338, 236, 338, 236,
    00554   WC_GENERATE_LANDSCAPE, WC_NONE,
    00555   WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_STD_BTN | WDF_UNCLICK_BUTTONS,
    00556   _heightmap_load_widgets,
    00557   GenerateLandscapeWndProc,
    00558 };
    00559
    00560 static void _ShowGenerateLandscape(glwp_modes mode)
    00561 {
    00562   uint x = 0;
    00563   uint y = 0;
    00564
    00565   DeleteWindowByClass(WC_GENERATE_LANDSCAPE);
    00566
    00567   /* Always give a new seed if not editor */
    00568   if (_game_mode != GM_EDITOR) _patches_newgame.generation_seed = InteractiveRandom();
    00569
    00570   if (mode == GLWP_HEIGHTMAP) {
    00571     /* If the function returns negative, it means there was a problem loading the heightmap */
    00572     if (!GetHeightmapDimensions(_file_to_saveload.name, &x, &y)) return;
    00573   }
    00574
    00575   Window *w = AllocateWindowDescFront((mode == GLWP_HEIGHTMAP) ? &_heightmap_load_desc : &_generate_landscape_desc, mode);
    00576
    00577   if (w == NULL) return;
    00578
    00579   if (mode == GLWP_HEIGHTMAP) {
    00580     WP(w, generate_d).x = x;
    00581     WP(w, generate_d).y = y;
    00582     strecpy(WP(w, generate_d).name, _file_to_saveload.title, lastof(WP(w, generate_d).name));
    00583   }
    00584
    00585   InvalidateWindow(WC_GENERATE_LANDSCAPE, mode);
    00586 }
    00587
    00588 void ShowGenerateLandscape()
    00589 {
    00590   _ShowGenerateLandscape(GLWP_GENERATE);
    00591 }
    00592
    00593 void ShowHeightmapLoad()
    00594 {
    00595   _ShowGenerateLandscape(GLWP_HEIGHTMAP);
    00596 }
    00597
    00598 void StartScenarioEditor()
    00599 {
    00600   if (_patches_newgame.town_layout == TL_NO_ROADS) {
    00601     _patches_newgame.town_layout = TL_ORIGINAL;
    00602   }
    00603
    00604   StartGeneratingLandscape(GLWP_SCENARIO);
    00605 }
    00606
    00607 void StartNewGameWithoutGUI(uint seed)
    00608 {
    00609   /* GenerateWorld takes care of the possible GENERATE_NEW_SEED value in 'seed' */
    00610   _patches_newgame.generation_seed = seed;
    00611
    00612   StartGeneratingLandscape(GLWP_GENERATE);
    00613 }
    00614
    00615 enum CreateScenarioWindowWidgets {
    00616   CSCEN_TEMPERATE = 3,
    00617   CSCEN_ARCTIC,
    00618   CSCEN_TROPICAL,
    00619   CSCEN_TOYLAND,
    00620   CSCEN_EMPTY_WORLD,
    00621   CSCEN_RANDOM_WORLD,
    00622   CSCEN_MAPSIZE_X_TEXT,
    00623   CSCEN_MAPSIZE_X_PULLDOWN,
    00624   CSCEN_MAPSIZE_Y_TEXT,
    00625   CSCEN_MAPSIZE_Y_PULLDOWN,
    00626   CSCEN_START_DATE_LABEL,
    00627   CSCEN_START_DATE_DOWN,
    00628   CSCEN_START_DATE_TEXT,
    00629   CSCEN_START_DATE_UP,
    00630   CSCEN_FLAT_LAND_HEIGHT_LABEL,
    00631   CSCEN_FLAT_LAND_HEIGHT_DOWN,
    00632   CSCEN_FLAT_LAND_HEIGHT_TEXT,
    00633   CSCEN_FLAT_LAND_HEIGHT_UP
    00634 };
    00635
    00636
    00637 static void CreateScenarioWndProc(Window *w, WindowEvent *e)
    00638 {
    00639   switch (e->event) {
    00640     case WE_CREATE:
    00641       w->LowerWidget(_opt_newgame.landscape + CSCEN_TEMPERATE);
    00642       break;
    00643
    00644     case WE_PAINT:
    00645       w->SetWidgetDisabledState(CSCEN_START_DATE_DOWN,       _patches_newgame.starting_year <= MIN_YEAR);
    00646       w->SetWidgetDisabledState(CSCEN_START_DATE_UP,         _patches_newgame.starting_year >= MAX_YEAR);
    00647       w->SetWidgetDisabledState(CSCEN_FLAT_LAND_HEIGHT_DOWN, _patches_newgame.se_flat_world_height <= 0);
    00648       w->SetWidgetDisabledState(CSCEN_FLAT_LAND_HEIGHT_UP,   _patches_newgame.se_flat_world_height >= MAX_TILE_HEIGHT);
    00649
    00650       w->SetWidgetLoweredState(CSCEN_TEMPERATE, _opt_newgame.landscape == LT_TEMPERATE);
    00651       w->SetWidgetLoweredState(CSCEN_ARCTIC,    _opt_newgame.landscape == LT_ARCTIC);
    00652       w->SetWidgetLoweredState(CSCEN_TROPICAL,  _opt_newgame.landscape == LT_TROPIC);
    00653       w->SetWidgetLoweredState(CSCEN_TOYLAND,   _opt_newgame.landscape == LT_TOYLAND);
    00654
    00655       /* Set parameters for widget text that requires them */
    00656       SetDParam(0, ConvertYMDToDate(_patches_newgame.starting_year, 0, 1)); // CSCEN_START_DATE_TEXT
    00657       SetDParam(1, 1 << _patches_newgame.map_x); // CSCEN_MAPSIZE_X_PULLDOWN
    00658       SetDParam(2, 1 << _patches_newgame.map_y); // CSCEN_MAPSIZE_Y_PULLDOWN
    00659       SetDParam(3, _patches_newgame.se_flat_world_height); // CSCEN_FLAT_LAND_HEIGHT_TEXT
    00660
    00661       DrawWindowWidgets(w);
    00662
    00663       break;
    00664
    00665     case WE_CLICK:
    00666       switch (e->we.click.widget) {
    00667         case CSCEN_TEMPERATE:
    00668         case CSCEN_ARCTIC:
    00669         case CSCEN_TROPICAL:
    00670         case CSCEN_TOYLAND:
    00671           w->RaiseWidget(_opt_newgame.landscape + CSCEN_TEMPERATE);
    00672           SetNewLandscapeType(e->we.click.widget - CSCEN_TEMPERATE);
    00673           break;
    00674
    00675         case CSCEN_MAPSIZE_X_PULLDOWN: // Mapsize X
    00676           ShowDropDownList(w, BuildMapsizeDropDown(), _patches_newgame.map_x, CSCEN_MAPSIZE_X_PULLDOWN);
    00677           break;
    00678
    00679         case CSCEN_MAPSIZE_Y_PULLDOWN: // Mapsize Y
    00680           ShowDropDownList(w, BuildMapsizeDropDown(), _patches_newgame.map_y, CSCEN_MAPSIZE_Y_PULLDOWN);
    00681           break;
    00682
    00683         case CSCEN_EMPTY_WORLD: // Empty world / flat world
    00684           StartGeneratingLandscape(GLWP_SCENARIO);
    00685           break;
    00686
    00687         case CSCEN_RANDOM_WORLD: // Generate
    00688           ShowGenerateLandscape();
    00689           break;
    00690
    00691         case CSCEN_START_DATE_DOWN:
    00692         case CSCEN_START_DATE_UP: // Year buttons
    00693           /* Don't allow too fast scrolling */
    00694           if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
    00695             w->HandleButtonClick(e->we.click.widget);
    00696             SetWindowDirty(w);
    00697
    00698             _patches_newgame.starting_year = Clamp(_patches_newgame.starting_year + e->we.click.widget - CSCEN_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
    00699           }
    00700           _left_button_clicked = false;
    00701           break;
    00702
    00703         case CSCEN_START_DATE_TEXT: // Year text
    00704           WP(w, generate_d).widget_id = CSCEN_START_DATE_TEXT;
    00705           SetDParam(0, _patches_newgame.starting_year);
    00706           ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_START_DATE_QUERY_CAPT, 8, 100, w, CS_NUMERAL);
    00707           break;
    00708
    00709         case CSCEN_FLAT_LAND_HEIGHT_DOWN:
    00710         case CSCEN_FLAT_LAND_HEIGHT_UP: // Height level buttons
    00711           /* Don't allow too fast scrolling */
    00712           if ((w->flags4 & WF_TIMEOUT_MASK) <= 2 << WF_TIMEOUT_SHL) {
    00713             w->HandleButtonClick(e->we.click.widget);
    00714             SetWindowDirty(w);
    00715
    00716             _patches_newgame.se_flat_world_height = Clamp(_patches_newgame.se_flat_world_height + e->we.click.widget - CSCEN_FLAT_LAND_HEIGHT_TEXT, 0, MAX_TILE_HEIGHT);
    00717           }
    00718           _left_button_clicked = false;
    00719           break;
    00720
    00721         case CSCEN_FLAT_LAND_HEIGHT_TEXT: // Height level text
    00722           WP(w, generate_d).widget_id = CSCEN_FLAT_LAND_HEIGHT_TEXT;
    00723           SetDParam(0, _patches_newgame.se_flat_world_height);
    00724           ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_FLAT_WORLD_HEIGHT_QUERY_CAPT, 3, 100, w, CS_NUMERAL);
    00725           break;
    00726       }
    00727       break;
    00728
    00729     case WE_DROPDOWN_SELECT:
    00730       switch (e->we.dropdown.button) {
    00731         case CSCEN_MAPSIZE_X_PULLDOWN: _patches_newgame.map_x = e->we.dropdown.index; break;
    00732         case CSCEN_MAPSIZE_Y_PULLDOWN: _patches_newgame.map_y = e->we.dropdown.index; break;
    00733       }
    00734       SetWindowDirty(w);
    00735       break;
    00736
    00737     case WE_ON_EDIT_TEXT:
    00738       if (e->we.edittext.str != NULL) {
    00739         int32 value = atoi(e->we.edittext.str);
    00740
    00741         switch (WP(w, generate_d).widget_id) {
    00742           case CSCEN_START_DATE_TEXT:
    00743             w->InvalidateWidget(CSCEN_START_DATE_TEXT);
    00744             _patches_newgame.starting_year = Clamp(value, MIN_YEAR, MAX_YEAR);
    00745             break;
    00746
    00747           case CSCEN_FLAT_LAND_HEIGHT_TEXT:
    00748             w->InvalidateWidget(CSCEN_FLAT_LAND_HEIGHT_TEXT);
    00749             _patches_newgame.se_flat_world_height = Clamp(value, 0, MAX_TILE_HEIGHT);
    00750             break;
    00751         }
    00752
    00753         SetWindowDirty(w);
    00754       }
    00755       break;
    00756   }
    00757 }
    00758
    00759 static const Widget _create_scenario_widgets[] = {
    00760 {   WWT_CLOSEBOX, RESIZE_NONE, 13,   0,  10,   0,  13, STR_00C5,                STR_018B_CLOSE_WINDOW},
    00761 {    WWT_CAPTION, RESIZE_NONE, 13,  11, 337,   0,  13, STR_SE_CAPTION,          STR_NULL},
    00762 {      WWT_PANEL, RESIZE_NONE, 13,   0, 337,  14, 169, 0x0,                     STR_NULL},
    00763
    00764 {   WWT_IMGBTN_2, RESIZE_NONE, 12,  10,  86,  24,  78, SPR_SELECT_TEMPERATE,    STR_030E_SELECT_TEMPERATE_LANDSCAPE},
    00765 {   WWT_IMGBTN_2, RESIZE_NONE, 12,  90, 166,  24,  78, SPR_SELECT_SUB_ARCTIC,   STR_030F_SELECT_SUB_ARCTIC_LANDSCAPE},
    00766 {   WWT_IMGBTN_2, RESIZE_NONE, 12, 170, 246,  24,  78, SPR_SELECT_SUB_TROPICAL, STR_0310_SELECT_SUB_TROPICAL_LANDSCAPE},
    00767 {   WWT_IMGBTN_2, RESIZE_NONE, 12, 250, 326,  24,  78, SPR_SELECT_TOYLAND,      STR_0311_SELECT_TOYLAND_LANDSCAPE},
    00768
    00769 {    WWT_TEXTBTN, RESIZE_NONE,  6,  12, 115,  95, 124, STR_SE_FLAT_WORLD,       STR_SE_FLAT_WORLD_TIP},         // Empty (sea-level) map
    00770 {    WWT_TEXTBTN, RESIZE_NONE,  6,  12, 115, 131, 160, STR_SE_RANDOM_LAND,      STR_022A_GENERATE_RANDOM_LAND}, // Generate
    00771
    00772 {       WWT_TEXT, RESIZE_NONE, 12, 182, 212,  96, 106, STR_MAPSIZE,             STR_NULL},
    00773 {   WWT_DROPDOWN, RESIZE_NONE, 12, 216, 263,  95, 106, STR_NUM_1,               STR_NULL}, // Mapsize X
    00774 {       WWT_TEXT, RESIZE_NONE, 12, 268, 276,  96, 106, STR_BY,                  STR_NULL},
    00775 {   WWT_DROPDOWN, RESIZE_NONE, 12, 279, 326,  95, 106, STR_NUM_2,               STR_NULL}, // Mapsize Y
    00776
    00777 {       WWT_TEXT, RESIZE_NONE,  0, 182, 212, 114, 124, STR_DATE,                STR_NULL},
    00778 {     WWT_IMGBTN, RESIZE_NONE, 12, 216, 227, 113, 124, SPR_ARROW_DOWN,          STR_029E_MOVE_THE_STARTING_DATE},
    00779 {    WWT_TEXTBTN, RESIZE_NONE, 12, 228, 314, 113, 124, STR_GENERATE_DATE,       STR_NULL},
    00780 {     WWT_IMGBTN, RESIZE_NONE, 12, 315, 326, 113, 124, SPR_ARROW_UP,            STR_029F_MOVE_THE_STARTING_DATE},
    00781
    00782 {       WWT_TEXT, RESIZE_NONE,  0, 182, 278, 132, 142, STR_FLAT_WORLD_HEIGHT,   STR_NULL},
    00783 {     WWT_IMGBTN, RESIZE_NONE, 12, 282, 293, 131, 142, SPR_ARROW_DOWN,          STR_FLAT_WORLD_HEIGHT_DOWN},
    00784 {    WWT_TEXTBTN, RESIZE_NONE, 12, 294, 314, 131, 142, STR_NUM_3,               STR_NULL},
    00785 {     WWT_IMGBTN, RESIZE_NONE, 12, 315, 326, 131, 142, SPR_ARROW_UP,            STR_FLAT_WORLD_HEIGHT_UP},
    00786 {   WIDGETS_END},
    00787 };
    00788
    00789 static const WindowDesc _create_scenario_desc = {
    00790   WDP_CENTER, WDP_CENTER, 338, 170, 338, 170,
    00791   WC_GENERATE_LANDSCAPE, WC_NONE,
    00792   WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_STD_BTN | WDF_UNCLICK_BUTTONS,
    00793   _create_scenario_widgets,
    00794   CreateScenarioWndProc,
    00795 };
    00796
    00797 void ShowCreateScenario()
    00798 {
    00799   DeleteWindowByClass(WC_GENERATE_LANDSCAPE);
    00800   AllocateWindowDescFront(&_create_scenario_desc, GLWP_SCENARIO);
    00801 }
    00802
    00803
    00804 static const Widget _show_terrain_progress_widgets[] = {
    00805 {    WWT_CAPTION,   RESIZE_NONE,    14,     0,   180,     0,    13, STR_GENERATION_WORLD,   STR_018C_WINDOW_TITLE_DRAG_THIS},
    00806 {      WWT_PANEL,   RESIZE_NONE,    14,     0,   180,    14,    96, 0x0,                    STR_NULL},
    00807 {    WWT_TEXTBTN,   RESIZE_NONE,    15,    20,   161,    74,    85, STR_GENERATION_ABORT,   STR_NULL}, // Abort button
    00808 {   WIDGETS_END},
    00809 };
    00810
    00811 struct tp_info {
    00812   uint percent;
    00813   StringID cls;
    00814   uint current;
    00815   uint total;
    00816   int timer;
    00817 };
    00818
    00819 static tp_info _tp;
    00820
    00821 static void AbortGeneratingWorldCallback(Window *w, bool confirmed)
    00822 {
    00823   if (confirmed) {
    00824     AbortGeneratingWorld();
    00825   } else if (IsGeneratingWorld() && !IsGeneratingWorldAborted()) {
    00826     SetMouseCursor(SPR_CURSOR_ZZZ, PAL_NONE);
    00827   }
    00828 }
    00829
    00830 static void ShowTerrainProgressProc(Window* w, WindowEvent* e)
    00831 {
    00832   switch (e->event) {
    00833     case WE_CLICK:
    00834       switch (e->we.click.widget) {
    00835         case 2:
    00836           if (_cursor.sprite == SPR_CURSOR_ZZZ) SetMouseCursor(SPR_CURSOR_MOUSE, PAL_NONE);
    00837           ShowQuery(
    00838             STR_GENERATION_ABORT_CAPTION,
    00839             STR_GENERATION_ABORT_MESSAGE,
    00840             w,
    00841             AbortGeneratingWorldCallback
    00842           );
    00843           break;
    00844       }
    00845       break;
    00846
    00847     case WE_PAINT:
    00848       DrawWindowWidgets(w);
    00849
    00850       /* Draw the % complete with a bar and a text */
    00851       DrawFrameRect(19, 20, (w->width - 18), 37, 14, FR_BORDERONLY);
    00852       DrawFrameRect(20, 21, (int)((w->width - 40) * _tp.percent / 100) + 20, 36, 10, FR_NONE);
    00853       SetDParam(0, _tp.percent);
    00854       DrawStringCentered(90, 25, STR_PROGRESS, TC_FROMSTRING);
    00855
    00856       /* Tell which class we are generating */
    00857       DrawStringCentered(90, 46, _tp.cls, TC_FROMSTRING);
    00858
    00859       /* And say where we are in that class */
    00860       SetDParam(0, _tp.current);
    00861       SetDParam(1, _tp.total);
    00862       DrawStringCentered(90, 58, STR_GENERATION_PROGRESS, TC_FROMSTRING);
    00863
    00864       SetWindowDirty(w);
    00865       break;
    00866   }
    00867 }
    00868
    00869 static const WindowDesc _show_terrain_progress_desc = {
    00870   WDP_CENTER, WDP_CENTER, 181, 97, 181, 97,
    00871   WC_GENERATE_PROGRESS_WINDOW, WC_NONE,
    00872   WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS,
    00873   _show_terrain_progress_widgets,
    00874   ShowTerrainProgressProc
    00875 };
    00876
    00880 void PrepareGenerateWorldProgress()
    00881 {
    00882   _tp.cls   = STR_WORLD_GENERATION;
    00883   _tp.current = 0;
    00884   _tp.total   = 0;
    00885   _tp.percent = 0;
    00886   _tp.timer   = 0; // Forces to paint the progress window immediatelly
    00887 }
    00888
    00892 void ShowGenerateWorldProgress()
    00893 {
    00894   AllocateWindowDescFront(&_show_terrain_progress_desc, 0);
    00895 }
    00896
    00897 static void _SetGeneratingWorldProgress(gwp_class cls, uint progress, uint total)
    00898 {
    00899   static const int percent_table[GWP_CLASS_COUNT + 1] = {0, 5, 15, 20, 40, 60, 65, 80, 85, 99, 100 };
    00900   static const StringID class_table[GWP_CLASS_COUNT]  = {
    00901     STR_WORLD_GENERATION,
    00902     STR_022E_LANDSCAPE_GENERATION,
    00903     STR_CLEARING_TILES,
    00904     STR_022F_TOWN_GENERATION,
    00905     STR_0230_INDUSTRY_GENERATION,
    00906     STR_UNMOVABLE_GENERATION,
    00907     STR_TREE_GENERATION,
    00908     STR_SETTINGUP_GAME,
    00909     STR_PREPARING_TILELOOP,
    00910     STR_PREPARING_GAME
    00911   };
    00912
    00913   assert(cls < GWP_CLASS_COUNT);
    00914
    00915   /* Do not run this function if we aren't in a thread */
    00916   if (!IsGenerateWorldThreaded() && !_network_dedicated) return;
    00917
    00918   if (IsGeneratingWorldAborted()) HandleGeneratingWorldAbortion();
    00919
    00920   if (total == 0) {
    00921     assert(_tp.cls == class_table[cls]);
    00922     _tp.current += progress;
    00923   } else {
    00924     _tp.cls   = class_table[cls];
    00925     _tp.current = progress;
    00926     _tp.total   = total;
    00927     _tp.percent = percent_table[cls];
    00928   }
    00929
    00930   /* Don't update the screen too often. So update it once in every 200ms */
    00931   if (!_network_dedicated && _tp.timer != 0 && _realtime_tick - _tp.timer < 200) return;
    00932
    00933   /* Percentage is about the number of completed tasks, so 'current - 1' */
    00934   _tp.percent = percent_table[cls] + (percent_table[cls + 1] - percent_table[cls]) * (_tp.current == 0 ? 0 : _tp.current - 1) / _tp.total;
    00935
    00936   if (_network_dedicated) {
    00937     static uint last_percent = 0;
    00938
    00939     /* Never display 0% */
    00940     if (_tp.percent == 0) return;
    00941     /* Reset if percent is lower then the last recorded */
    00942     if (_tp.percent < last_percent) last_percent = 0;
    00943     /* Display every 5%, but 6% is also very valid.. just not smaller steps then 5% */
    00944     if (_tp.percent % 5 != 0 && _tp.percent <= last_percent + 5) return;
    00945     /* Never show steps smaller then 2%, even if it is a mod 5% */
    00946     if (_tp.percent <= last_percent + 2) return;
    00947
    00948     DEBUG(net, 1, "Map generation percentage complete: %d", _tp.percent);
    00949     last_percent = _tp.percent;
    00950
    00951     /* Don't continue as dedicated never has a thread running */
    00952     return;
    00953   }
    00954
    00955   InvalidateWindow(WC_GENERATE_PROGRESS_WINDOW, 0);
    00956   MarkWholeScreenDirty();
    00957   SetGeneratingWorldPaintStatus(true);
    00958
    00959   /* We wait here till the paint is done, so we don't read and write
    00960    *  on the same tile at the same moment. Nasty hack, but that happens
    00961    *  if you implement threading afterwards */
    00962   while (IsGeneratingWorldReadyForPaint()) { CSleep(10); }
    00963
    00964   _tp.timer = _realtime_tick;
    00965 }
    00966
    00975 void SetGeneratingWorldProgress(gwp_class cls, uint total)
    00976 {
    00977   if (total == 0) return;
    00978
    00979   _SetGeneratingWorldProgress(cls, 0, total);
    00980 }
    00981
    00989 void IncreaseGeneratingWorldProgress(gwp_class cls)
    00990 {
    00991   /* In fact the param 'class' isn't needed.. but for some security reasons, we want it around */
    00992   _SetGeneratingWorldProgress(cls, 1, 0);
    00993 }