src/genworld_gui.cpp
branchNewGRF_ports
changeset 6720 35756db7e577
parent 6719 4cc327ad39d5
child 6743 cabfaa4a0295
equal deleted inserted replaced
6719:4cc327ad39d5 6720:35756db7e577
    34 	GLWP_HEIGHTMAP,
    34 	GLWP_HEIGHTMAP,
    35 	GLWP_SCENARIO,
    35 	GLWP_SCENARIO,
    36 	GLWP_END
    36 	GLWP_END
    37 };
    37 };
    38 
    38 
    39 static uint _heightmap_x = 0;
    39 struct generate_d {
    40 static uint _heightmap_y = 0;
    40 	uint widget_id;
    41 static StringID _heightmap_str = STR_NULL;
    41 	uint x;
       
    42 	uint y;
       
    43 	char name[64];
       
    44 };
    42 
    45 
    43 extern void SwitchMode(int new_mode);
    46 extern void SwitchMode(int new_mode);
    44 
    47 
    45 static inline void SetNewLandscapeType(byte landscape)
    48 static inline void SetNewLandscapeType(byte landscape)
    46 {
    49 {
   305 			DrawString(118, 247, smoothness[_patches_newgame.tgen_smoothness], 0x10);
   308 			DrawString(118, 247, smoothness[_patches_newgame.tgen_smoothness], 0x10);
   306 		} else {
   309 		} else {
   307 			char buffer[512];
   310 			char buffer[512];
   308 
   311 
   309 			if (_patches_newgame.heightmap_rotation == HM_CLOCKWISE) {
   312 			if (_patches_newgame.heightmap_rotation == HM_CLOCKWISE) {
   310 				SetDParam(0, _heightmap_y);
   313 				SetDParam(0, WP(w, generate_d).y);
   311 				SetDParam(1, _heightmap_x);
   314 				SetDParam(1, WP(w, generate_d).x);
   312 			} else {
   315 			} else {
   313 				SetDParam(0, _heightmap_x);
   316 				SetDParam(0, WP(w, generate_d).x);
   314 				SetDParam(1, _heightmap_y);
   317 				SetDParam(1, WP(w, generate_d).y);
   315 			}
   318 			}
   316 			GetString(buffer, STR_HEIGHTMAP_SIZE, lastof(buffer));
   319 			GetString(buffer, STR_HEIGHTMAP_SIZE, lastof(buffer));
   317 			DrawStringRightAligned(326, 91, STR_HEIGHTMAP_SIZE, 0x10);
   320 			DrawStringRightAligned(326, 91, STR_HEIGHTMAP_SIZE, 0x10);
   318 
   321 
   319 			DrawString( 12,  91, STR_HEIGHTMAP_NAME, 0x10);
   322 			DrawString( 12,  91, STR_HEIGHTMAP_NAME, 0x10);
   320 			SetDParam(0, _heightmap_str);
   323 			SetDParamStr(0, WP(w, generate_d).name);
   321 			DrawStringTruncated(114,  91, STR_ORANGE, 0x10, 326 - 114 - GetStringBoundingBox(buffer).width - 5);
   324 			DrawStringTruncated(114,  91, STR_ORANGE, 0x10, 326 - 114 - GetStringBoundingBox(buffer).width - 5);
   322 
   325 
   323 			DrawString( 12, 197, STR_TREE_PLACER, 0);
   326 			DrawString( 12, 197, STR_TREE_PLACER, 0);
   324 			DrawString(118, 197, tree_placer[_patches_newgame.tree_placer], 0x10);
   327 			DrawString(118, 197, tree_placer[_patches_newgame.tree_placer], 0x10);
   325 
   328 
   362 					STR_TOWN_LAYOUT_WARNING_CAPTION,
   365 					STR_TOWN_LAYOUT_WARNING_CAPTION,
   363 					STR_TOWN_LAYOUT_WARNING_MESSAGE,
   366 					STR_TOWN_LAYOUT_WARNING_MESSAGE,
   364 					w,
   367 					w,
   365 					LandscapeGenerationCallback);
   368 					LandscapeGenerationCallback);
   366 			} else if (mode == GLWP_HEIGHTMAP &&
   369 			} else if (mode == GLWP_HEIGHTMAP &&
   367 					(_heightmap_x * 2 < (1U << _patches_newgame.map_x) ||
   370 					(WP(w, generate_d).x * 2 < (1U << _patches_newgame.map_x) ||
   368 					_heightmap_x / 2 > (1U << _patches_newgame.map_x) ||
   371 					WP(w, generate_d).x / 2 > (1U << _patches_newgame.map_x) ||
   369 					_heightmap_y * 2 < (1U << _patches_newgame.map_y) ||
   372 					WP(w, generate_d).y * 2 < (1U << _patches_newgame.map_y) ||
   370 					_heightmap_y / 2 > (1U << _patches_newgame.map_y))) {
   373 					WP(w, generate_d).y / 2 > (1U << _patches_newgame.map_y))) {
   371 				ShowQuery(
   374 				ShowQuery(
   372 					STR_HEIGHTMAP_SCALE_WARNING_CAPTION,
   375 					STR_HEIGHTMAP_SCALE_WARNING_CAPTION,
   373 					STR_HEIGHTMAP_SCALE_WARNING_MESSAGE,
   376 					STR_HEIGHTMAP_SCALE_WARNING_MESSAGE,
   374 					w,
   377 					w,
   375 					LandscapeGenerationCallback);
   378 					LandscapeGenerationCallback);
   387 				_patches_newgame.starting_year = clamp(_patches_newgame.starting_year + e->we.click.widget - GLAND_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
   390 				_patches_newgame.starting_year = clamp(_patches_newgame.starting_year + e->we.click.widget - GLAND_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
   388 			}
   391 			}
   389 			_left_button_clicked = false;
   392 			_left_button_clicked = false;
   390 			break;
   393 			break;
   391 		case GLAND_START_DATE_TEXT: // Year text
   394 		case GLAND_START_DATE_TEXT: // Year text
   392 			WP(w, def_d).data_3 = GLAND_START_DATE_TEXT;
   395 			WP(w, generate_d).widget_id = GLAND_START_DATE_TEXT;
   393 			SetDParam(0, _patches_newgame.starting_year);
   396 			SetDParam(0, _patches_newgame.starting_year);
   394 			ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_START_DATE_QUERY_CAPT, 8, 100, w, CS_NUMERAL);
   397 			ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_START_DATE_QUERY_CAPT, 8, 100, w, CS_NUMERAL);
   395 			break;
   398 			break;
   396 		case GLAND_SNOW_LEVEL_DOWN: case GLAND_SNOW_LEVEL_UP: // Snow line buttons
   399 		case GLAND_SNOW_LEVEL_DOWN: case GLAND_SNOW_LEVEL_UP: // Snow line buttons
   397 			/* Don't allow too fast scrolling */
   400 			/* Don't allow too fast scrolling */
   402 				_patches_newgame.snow_line_height = clamp(_patches_newgame.snow_line_height + e->we.click.widget - GLAND_SNOW_LEVEL_TEXT, 2, 13);
   405 				_patches_newgame.snow_line_height = clamp(_patches_newgame.snow_line_height + e->we.click.widget - GLAND_SNOW_LEVEL_TEXT, 2, 13);
   403 			}
   406 			}
   404 			_left_button_clicked = false;
   407 			_left_button_clicked = false;
   405 			break;
   408 			break;
   406 		case GLAND_SNOW_LEVEL_TEXT: // Snow line text
   409 		case GLAND_SNOW_LEVEL_TEXT: // Snow line text
   407 			WP(w, def_d).data_3 = GLAND_SNOW_LEVEL_TEXT;
   410 			WP(w, generate_d).widget_id = GLAND_SNOW_LEVEL_TEXT;
   408 			SetDParam(0, _patches_newgame.snow_line_height);
   411 			SetDParam(0, _patches_newgame.snow_line_height);
   409 			ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_SNOW_LINE_QUERY_CAPT, 3, 100, w, CS_NUMERAL);
   412 			ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_SNOW_LINE_QUERY_CAPT, 3, 100, w, CS_NUMERAL);
   410 			break;
   413 			break;
   411 		case GLAND_TREE_TEXT: case GLAND_TREE_PULLDOWN: // Tree placer
   414 		case GLAND_TREE_TEXT: case GLAND_TREE_PULLDOWN: // Tree placer
   412 			ShowDropDownMenu(w, tree_placer, _patches_newgame.tree_placer, GLAND_TREE_PULLDOWN, 0, 0);
   415 			ShowDropDownMenu(w, tree_placer, _patches_newgame.tree_placer, GLAND_TREE_PULLDOWN, 0, 0);
   486 
   489 
   487 	case WE_ON_EDIT_TEXT: {
   490 	case WE_ON_EDIT_TEXT: {
   488 		if (e->we.edittext.str != NULL) {
   491 		if (e->we.edittext.str != NULL) {
   489 			int32 value = atoi(e->we.edittext.str);
   492 			int32 value = atoi(e->we.edittext.str);
   490 
   493 
   491 			switch (WP(w, def_d).data_3) {
   494 			switch (WP(w, generate_d).widget_id) {
   492 			case GLAND_START_DATE_TEXT:
   495 			case GLAND_START_DATE_TEXT:
   493 				InvalidateWidget(w, GLAND_START_DATE_TEXT);
   496 				InvalidateWidget(w, GLAND_START_DATE_TEXT);
   494 				_patches_newgame.starting_year = clamp(value, MIN_YEAR, MAX_YEAR);
   497 				_patches_newgame.starting_year = clamp(value, MIN_YEAR, MAX_YEAR);
   495 				break;
   498 				break;
   496 			case GLAND_SNOW_LEVEL_TEXT:
   499 			case GLAND_SNOW_LEVEL_TEXT:
   522 	GenerateLandscapeWndProc,
   525 	GenerateLandscapeWndProc,
   523 };
   526 };
   524 
   527 
   525 static void _ShowGenerateLandscape(glwp_modes mode)
   528 static void _ShowGenerateLandscape(glwp_modes mode)
   526 {
   529 {
       
   530 	uint x = 0;
       
   531 	uint y = 0;
       
   532 
   527 	DeleteWindowByClass(WC_GENERATE_LANDSCAPE);
   533 	DeleteWindowByClass(WC_GENERATE_LANDSCAPE);
   528 
   534 
   529 	/* Always give a new seed if not editor */
   535 	/* Always give a new seed if not editor */
   530 	if (_game_mode != GM_EDITOR) _patches_newgame.generation_seed = InteractiveRandom();
   536 	if (_game_mode != GM_EDITOR) _patches_newgame.generation_seed = InteractiveRandom();
   531 
   537 
   532 	if (mode == GLWP_HEIGHTMAP) {
   538 	if (mode == GLWP_HEIGHTMAP) {
   533 		if (_heightmap_str != STR_NULL) DeleteName(_heightmap_str);
       
   534 
       
   535 		_heightmap_x = 0;
       
   536 		_heightmap_y = 0;
       
   537 		_heightmap_str = AllocateName(_file_to_saveload.title, 0);
       
   538 		/* If the function returns negative, it means there was a problem loading the heightmap */
   539 		/* If the function returns negative, it means there was a problem loading the heightmap */
   539 		if (!GetHeightmapDimensions(_file_to_saveload.name, &_heightmap_x, &_heightmap_y))
   540 		if (!GetHeightmapDimensions(_file_to_saveload.name, &x, &y)) return;
   540 			return;
       
   541 	}
   541 	}
   542 
   542 
   543 	Window *w = AllocateWindowDescFront((mode == GLWP_HEIGHTMAP) ? &_heightmap_load_desc : &_generate_landscape_desc, mode);
   543 	Window *w = AllocateWindowDescFront((mode == GLWP_HEIGHTMAP) ? &_heightmap_load_desc : &_generate_landscape_desc, mode);
   544 
   544 
   545 	if (w != NULL) InvalidateWindow(WC_GENERATE_LANDSCAPE, mode);
   545 	if (w == NULL) return;
       
   546 
       
   547 	if (mode == GLWP_HEIGHTMAP) {
       
   548 		WP(w, generate_d).x = x;
       
   549 		WP(w, generate_d).y = y;
       
   550 		strecpy(WP(w, generate_d).name, _file_to_saveload.title, lastof(WP(w, generate_d).name));
       
   551 	}
       
   552 
       
   553 	InvalidateWindow(WC_GENERATE_LANDSCAPE, mode);
   546 }
   554 }
   547 
   555 
   548 void ShowGenerateLandscape()
   556 void ShowGenerateLandscape()
   549 {
   557 {
   550 	_ShowGenerateLandscape(GLWP_GENERATE);
   558 	_ShowGenerateLandscape(GLWP_GENERATE);
   652 				_patches_newgame.starting_year = clamp(_patches_newgame.starting_year + e->we.click.widget - CSCEN_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
   660 				_patches_newgame.starting_year = clamp(_patches_newgame.starting_year + e->we.click.widget - CSCEN_START_DATE_TEXT, MIN_YEAR, MAX_YEAR);
   653 			}
   661 			}
   654 			_left_button_clicked = false;
   662 			_left_button_clicked = false;
   655 			break;
   663 			break;
   656 		case CSCEN_START_DATE_TEXT: // Year text
   664 		case CSCEN_START_DATE_TEXT: // Year text
   657 			WP(w, def_d).data_3 = CSCEN_START_DATE_TEXT;
   665 			WP(w, generate_d).widget_id = CSCEN_START_DATE_TEXT;
   658 			SetDParam(0, _patches_newgame.starting_year);
   666 			SetDParam(0, _patches_newgame.starting_year);
   659 			ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_START_DATE_QUERY_CAPT, 8, 100, w, CS_NUMERAL);
   667 			ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_START_DATE_QUERY_CAPT, 8, 100, w, CS_NUMERAL);
   660 			break;
   668 			break;
   661 		case CSCEN_FLAT_LAND_HEIGHT_DOWN: case CSCEN_FLAT_LAND_HEIGHT_UP: // Height level buttons
   669 		case CSCEN_FLAT_LAND_HEIGHT_DOWN: case CSCEN_FLAT_LAND_HEIGHT_UP: // Height level buttons
   662 			/* Don't allow too fast scrolling */
   670 			/* Don't allow too fast scrolling */
   667 				_patches_newgame.se_flat_world_height = clamp(_patches_newgame.se_flat_world_height + e->we.click.widget - CSCEN_FLAT_LAND_HEIGHT_TEXT, 0, 15);
   675 				_patches_newgame.se_flat_world_height = clamp(_patches_newgame.se_flat_world_height + e->we.click.widget - CSCEN_FLAT_LAND_HEIGHT_TEXT, 0, 15);
   668 			}
   676 			}
   669 			_left_button_clicked = false;
   677 			_left_button_clicked = false;
   670 			break;
   678 			break;
   671 		case CSCEN_FLAT_LAND_HEIGHT_TEXT: // Height level text
   679 		case CSCEN_FLAT_LAND_HEIGHT_TEXT: // Height level text
   672 			WP(w, def_d).data_3 = CSCEN_FLAT_LAND_HEIGHT_TEXT;
   680 			WP(w, generate_d).widget_id = CSCEN_FLAT_LAND_HEIGHT_TEXT;
   673 			SetDParam(0, _patches_newgame.se_flat_world_height);
   681 			SetDParam(0, _patches_newgame.se_flat_world_height);
   674 			ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_FLAT_WORLD_HEIGHT_QUERY_CAPT, 3, 100, w, CS_NUMERAL);
   682 			ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_FLAT_WORLD_HEIGHT_QUERY_CAPT, 3, 100, w, CS_NUMERAL);
   675 			break;
   683 			break;
   676 		}
   684 		}
   677 		break;
   685 		break;
   686 
   694 
   687 	case WE_ON_EDIT_TEXT: {
   695 	case WE_ON_EDIT_TEXT: {
   688 		if (e->we.edittext.str != NULL) {
   696 		if (e->we.edittext.str != NULL) {
   689 			int32 value = atoi(e->we.edittext.str);
   697 			int32 value = atoi(e->we.edittext.str);
   690 
   698 
   691 			switch (WP(w, def_d).data_3) {
   699 			switch (WP(w, generate_d).widget_id) {
   692 			case CSCEN_START_DATE_TEXT:
   700 			case CSCEN_START_DATE_TEXT:
   693 				InvalidateWidget(w, CSCEN_START_DATE_TEXT);
   701 				InvalidateWidget(w, CSCEN_START_DATE_TEXT);
   694 				_patches_newgame.starting_year = clamp(value, MIN_YEAR, MAX_YEAR);
   702 				_patches_newgame.starting_year = clamp(value, MIN_YEAR, MAX_YEAR);
   695 				break;
   703 				break;
   696 			case CSCEN_FLAT_LAND_HEIGHT_TEXT:
   704 			case CSCEN_FLAT_LAND_HEIGHT_TEXT:
   873 		_tp.current = progress;
   881 		_tp.current = progress;
   874 		_tp.total   = total;
   882 		_tp.total   = total;
   875 		_tp.percent = percent_table[cls];
   883 		_tp.percent = percent_table[cls];
   876 	}
   884 	}
   877 
   885 
   878 	/* Don't update the screen too often. So update it once in every 200ms.
   886 	/* Don't update the screen too often. So update it once in every 200ms */
   879 	 * However, the _tick_counter increases by 8 every 30ms, so compensate
   887 	if (!_network_dedicated && _tp.timer != 0 && _realtime_tick - _tp.timer < 200) return;
   880 	 * for that. */
       
   881 	if (!_network_dedicated && _tp.timer != 0 && _timer_counter - _tp.timer < (200 * 8 / 30)) return;
       
   882 
   888 
   883 	/* Percentage is about the number of completed tasks, so 'current - 1' */
   889 	/* Percentage is about the number of completed tasks, so 'current - 1' */
   884 	_tp.percent = percent_table[cls] + (percent_table[cls + 1] - percent_table[cls]) * (_tp.current == 0 ? 0 : _tp.current - 1) / _tp.total;
   890 	_tp.percent = percent_table[cls] + (percent_table[cls + 1] - percent_table[cls]) * (_tp.current == 0 ? 0 : _tp.current - 1) / _tp.total;
   885 	_tp.timer = _timer_counter;
       
   886 
   891 
   887 	if (_network_dedicated) {
   892 	if (_network_dedicated) {
   888 		static uint last_percent = 0;
   893 		static uint last_percent = 0;
   889 
   894 
   890 		/* Never display 0% */
   895 		/* Never display 0% */
   909 
   914 
   910 	/* We wait here till the paint is done, so we don't read and write
   915 	/* We wait here till the paint is done, so we don't read and write
   911 	 *  on the same tile at the same moment. Nasty hack, but that happens
   916 	 *  on the same tile at the same moment. Nasty hack, but that happens
   912 	 *  if you implement threading afterwards */
   917 	 *  if you implement threading afterwards */
   913 	while (IsGeneratingWorldReadyForPaint()) { CSleep(10); }
   918 	while (IsGeneratingWorldReadyForPaint()) { CSleep(10); }
       
   919 
       
   920 	_tp.timer = _realtime_tick;
   914 }
   921 }
   915 
   922 
   916 /**
   923 /**
   917  * Set the total of a stage of the world generation.
   924  * Set the total of a stage of the world generation.
   918  * @param cls the current class we are in.
   925  * @param cls the current class we are in.