Files @ r23882:d683a0787bc9
Branch filter:

Location: cpp/openttd-patchpack/source/src/genworld.cpp - annotation

Nikolas Nyby
Codechange: Don't use SDL_CreateRGBSurfaceWithFormat()

This function requires libSDL 2.0.5 or higher. It looks like we don't
need to use it, and can just use the original SDL_CreateRGBSurface(),
with the masks set to 0, to trigger the default 8-bit format, which is
SDL_PIXELFORMAT_INDEX8.

Closes #7785

Note: this code path is activated by using an 8-bit blitter, like:

./bin/openttd -b 8bpp-simple
r5584:545d748cc681
r5584:545d748cc681
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6179:c0508e7aefec
r5584:545d748cc681
r6453:b0b56773284a
r10208:ef8fcc3dc4ca
r5584:545d748cc681
r5584:545d748cc681
r8107:82461791b7a2
r5584:545d748cc681
r5584:545d748cc681
r8225:491804378826
r8140:9424f012f6a2
r8786:f24a6d1fba34
r8380:6b3d1e5a10ed
r17999:efe4e10ba6ab
r9127:933c13888121
r10571:99cb9a95b4cf
r10855:5a3c2f38f98e
r11002:ca78de6e5555
r13958:4df52fd52d03
r14248:a9050881acd7
r15284:a975e22a01ee
r17969:8bce9c292da9
r18623:86f5b14d45a4
r18751:3a554214098d
r18753:19e74c935c2d
r21387:4ac1de161213
r23587:821735a25dec
r5584:545d748cc681
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r8264:d493cb51fe8a
r6247:96e840dbefcc
r6247:96e840dbefcc
r15734:ae8e2e185874
r6247:96e840dbefcc
r5584:545d748cc681
r6247:96e840dbefcc
r10207:a1fc2f2a33db
r6247:96e840dbefcc
r5584:545d748cc681
r12635:3d67c34135d4
r5584:545d748cc681
r14299:dd117ea5dfbf
r14299:dd117ea5dfbf
r5584:545d748cc681
r5584:545d748cc681
r18522:a53f2541ddf4
r14299:dd117ea5dfbf
r14291:b7b327a367bf
r5584:545d748cc681
r15521:545d58d62089
r15521:545d58d62089
r15521:545d58d62089
r5584:545d748cc681
r5584:545d748cc681
r14299:dd117ea5dfbf
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r15645:b579711f0ab7
r15645:b579711f0ab7
r11333:7fe1ca4e3964
r11333:7fe1ca4e3964
r11333:7fe1ca4e3964
r11333:7fe1ca4e3964
r11333:7fe1ca4e3964
r22420:c59d84ae4aab
r11333:7fe1ca4e3964
r11333:7fe1ca4e3964
r17969:8bce9c292da9
r23607:36c15679007d
r23607:36c15679007d
r11333:7fe1ca4e3964
r11333:7fe1ca4e3964
r18743:bbdd9a165607
r18623:86f5b14d45a4
r11333:7fe1ca4e3964
r11333:7fe1ca4e3964
r11333:7fe1ca4e3964
r11333:7fe1ca4e3964
r5584:545d748cc681
r5584:545d748cc681
r23587:821735a25dec
r5584:545d748cc681
r15284:a975e22a01ee
r23674:a5bafa1a02c5
r15284:a975e22a01ee
r23586:11b470d4cee6
r8963:f346663a84f1
r8963:f346663a84f1
r23586:11b470d4cee6
r21417:92c83070c286
r8963:f346663a84f1
r9413:fcf267325763
r9413:fcf267325763
r8963:f346663a84f1
r11697:53072e575f0b
r5584:545d748cc681
r21283:ba592c088a09
r21283:ba592c088a09
r8963:f346663a84f1
r8963:f346663a84f1
r8963:f346663a84f1
r5584:545d748cc681
r8963:f346663a84f1
r14291:b7b327a367bf
r15734:ae8e2e185874
r5584:545d748cc681
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r23288:b17c876b87ca
r23288:b17c876b87ca
r10855:5a3c2f38f98e
r10855:5a3c2f38f98e
r11069:3f09c162966b
r9413:fcf267325763
r5584:545d748cc681
r8963:f346663a84f1
r15734:ae8e2e185874
r8963:f346663a84f1
r8963:f346663a84f1
r8963:f346663a84f1
r5584:545d748cc681
r8963:f346663a84f1
r8963:f346663a84f1
r11334:9b432ad28773
r15284:a975e22a01ee
r11334:9b432ad28773
r11334:9b432ad28773
r11334:9b432ad28773
r8963:f346663a84f1
r15734:ae8e2e185874
r8963:f346663a84f1
r8963:f346663a84f1
r5584:545d748cc681
r5584:545d748cc681
r8963:f346663a84f1
r8963:f346663a84f1
r10207:a1fc2f2a33db
r8963:f346663a84f1
r8963:f346663a84f1
r8963:f346663a84f1
r8963:f346663a84f1
r8963:f346663a84f1
r5584:545d748cc681
r8963:f346663a84f1
r14291:b7b327a367bf
r8963:f346663a84f1
r5584:545d748cc681
r8963:f346663a84f1
r8963:f346663a84f1
r8963:f346663a84f1
r19229:5550f5cdc59d
r8963:f346663a84f1
r8963:f346663a84f1
r18751:3a554214098d
r18751:3a554214098d
r18751:3a554214098d
r18753:19e74c935c2d
r23607:36c15679007d
r18753:19e74c935c2d
r18753:19e74c935c2d
r18753:19e74c935c2d
r18753:19e74c935c2d
r18753:19e74c935c2d
r18753:19e74c935c2d
r18753:19e74c935c2d
r18753:19e74c935c2d
r18753:19e74c935c2d
r18751:3a554214098d
r5584:545d748cc681
r8963:f346663a84f1
r21283:ba592c088a09
r21283:ba592c088a09
r8963:f346663a84f1
r15284:a975e22a01ee
r15284:a975e22a01ee
r5584:545d748cc681
r8963:f346663a84f1
r8963:f346663a84f1
r23607:36c15679007d
r8963:f346663a84f1
r5584:545d748cc681
r11333:7fe1ca4e3964
r23586:11b470d4cee6
r8963:f346663a84f1
r13958:4df52fd52d03
r13958:4df52fd52d03
r21417:92c83070c286
r14968:7248d069d855
r5584:545d748cc681
r10515:c73815918fba
r10515:c73815918fba
r21387:4ac1de161213
r22446:e215802a4b89
r10515:c73815918fba
r8963:f346663a84f1
r21283:ba592c088a09
r15284:a975e22a01ee
r8963:f346663a84f1
r8963:f346663a84f1
r8963:f346663a84f1
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r14299:dd117ea5dfbf
r14299:dd117ea5dfbf
r5584:545d748cc681
r14291:b7b327a367bf
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r14299:dd117ea5dfbf
r14299:dd117ea5dfbf
r5584:545d748cc681
r14291:b7b327a367bf
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r14299:dd117ea5dfbf
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r23587:821735a25dec
r11825:adc3045ab0f2
r23586:11b470d4cee6
r23586:11b470d4cee6
r5584:545d748cc681
r23587:821735a25dec
r5584:545d748cc681
r23586:11b470d4cee6
r23586:11b470d4cee6
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r14299:dd117ea5dfbf
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r23607:36c15679007d
r5584:545d748cc681
r11333:7fe1ca4e3964
r5584:545d748cc681
r23587:821735a25dec
r11334:9b432ad28773
r11334:9b432ad28773
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r14299:dd117ea5dfbf
r5584:545d748cc681
r5584:545d748cc681
r13060:e57594b0ca84
r5584:545d748cc681
r14291:b7b327a367bf
r5584:545d748cc681
r17969:8bce9c292da9
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r17969:8bce9c292da9
r5584:545d748cc681
r23607:36c15679007d
r10207:a1fc2f2a33db
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10207:a1fc2f2a33db
r5584:545d748cc681
r15897:d700bcc8403e
r13672:cbb70d26ff69
r13672:cbb70d26ff69
r15897:d700bcc8403e
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r11085:c87a330fb4c2
r13672:cbb70d26ff69
r5584:545d748cc681
r23587:821735a25dec
r9479:31c691398ec3
r23587:821735a25dec
r5584:545d748cc681
r5584:545d748cc681
r23586:11b470d4cee6
r23587:821735a25dec
r23586:11b470d4cee6
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r18623:86f5b14d45a4
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r23607:36c15679007d
r6760:78820a345270
r5584:545d748cc681
r5584:545d748cc681
/* $Id$ */

/*
 * This file is part of OpenTTD.
 * 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.
 * 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.
 * 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/>.
 */

/** @file genworld.cpp Functions to generate a map. */

#include "stdafx.h"
#include "landscape.h"
#include "company_func.h"
#include "genworld.h"
#include "gfxinit.h"
#include "window_func.h"
#include "network/network.h"
#include "heightmap.h"
#include "viewport_func.h"
#include "date_func.h"
#include "engine_func.h"
#include "water.h"
#include "video/video_driver.hpp"
#include "tilehighlight_func.h"
#include "saveload/saveload.h"
#include "void_map.h"
#include "town.h"
#include "newgrf.h"
#include "core/random_func.hpp"
#include "core/backup_type.hpp"
#include "progress.h"
#include "error.h"
#include "game/game.hpp"
#include "game/game_instance.hpp"
#include "string_func.h"
#include "thread.h"

#include "safeguards.h"


void GenerateClearTile();
void GenerateIndustries();
void GenerateObjects();
void GenerateTrees();

void StartupEconomy();
void StartupCompanies();
void StartupDisasters();

void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settings);

/**
 * Please only use this variable in genworld.h and genworld.cpp and
 *  nowhere else. For speed improvements we need it to be global, but
 *  in no way the meaning of it is to use it anywhere else besides
 *  in the genworld.h and genworld.cpp!
 */
GenWorldInfo _gw;

/** Whether we are generating the map or not. */
bool _generating_world;

/**
 * Tells if the world generation is done in a thread or not.
 * @return the 'threaded' status
 */
bool IsGenerateWorldThreaded()
{
	return _gw.threaded && !_gw.quit_thread;
}

/**
 * Clean up the 'mess' of generation. That is, show windows again, reset
 * thread variables, and delete the progress window.
 */
static void CleanupGeneration()
{
	_generating_world = false;

	SetMouseCursorBusy(false);
	/* Show all vital windows again, because we have hidden them */
	if (_gw.threaded && _game_mode != GM_MENU) ShowVitalWindows();
	SetModalProgress(false);
	_gw.proc     = nullptr;
	_gw.abortp   = nullptr;
	_gw.threaded = false;

	DeleteWindowByClass(WC_MODAL_PROGRESS);
	ShowFirstError();
	MarkWholeScreenDirty();
}

/**
 * The internal, real, generate function.
 */
static void _GenerateWorld()
{
	/* Make sure everything is done via OWNER_NONE. */
	Backup<CompanyID> _cur_company(_current_company, OWNER_NONE, FILE_LINE);

	std::unique_lock<std::mutex> lock(_modal_progress_work_mutex, std::defer_lock);
	try {
		_generating_world = true;
		lock.lock();
		if (_network_dedicated) DEBUG(net, 1, "Generating map, please wait...");
		/* Set the Random() seed to generation_seed so we produce the same map with the same seed */
		if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = _settings_newgame.game_creation.generation_seed = InteractiveRandom();
		_random.SetSeed(_settings_game.game_creation.generation_seed);
		SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
		SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);

		BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP);

		IncreaseGeneratingWorldProgress(GWP_MAP_INIT);
		/* Must start economy early because of the costs. */
		StartupEconomy();

		/* Don't generate landscape items when in the scenario editor. */
		if (_gw.mode == GWM_EMPTY) {
			SetGeneratingWorldProgress(GWP_OBJECT, 1);

			/* Make sure the tiles at the north border are void tiles if needed. */
			if (_settings_game.construction.freeform_edges) {
				for (uint x = 0; x < MapSizeX(); x++) MakeVoid(TileXY(x, 0));
				for (uint y = 0; y < MapSizeY(); y++) MakeVoid(TileXY(0, y));
			}

			/* Make the map the height of the setting */
			if (_game_mode != GM_MENU) FlatEmptyWorld(_settings_game.game_creation.se_flat_world_height);

			ConvertGroundTilesIntoWaterTiles();
			IncreaseGeneratingWorldProgress(GWP_OBJECT);
		} else {
			GenerateLandscape(_gw.mode);
			GenerateClearTile();

			/* only generate towns, tree and industries in newgame mode. */
			if (_game_mode != GM_EDITOR) {
				if (!GenerateTowns(_settings_game.economy.town_layout)) {
					_cur_company.Restore();
					HandleGeneratingWorldAbortion();
					return;
				}
				GenerateIndustries();
				GenerateObjects();
				GenerateTrees();
			}
		}

		/* These are probably pointless when inside the scenario editor. */
		SetGeneratingWorldProgress(GWP_GAME_INIT, 3);
		StartupCompanies();
		IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
		StartupEngines();
		IncreaseGeneratingWorldProgress(GWP_GAME_INIT);
		StartupDisasters();
		_generating_world = false;

		/* No need to run the tile loop in the scenario editor. */
		if (_gw.mode != GWM_EMPTY) {
			uint i;

			SetGeneratingWorldProgress(GWP_RUNTILELOOP, 0x500);
			for (i = 0; i < 0x500; i++) {
				RunTileLoop();
				_tick_counter++;
				IncreaseGeneratingWorldProgress(GWP_RUNTILELOOP);
			}

			if (_game_mode != GM_EDITOR) {
				Game::StartNew();

				if (Game::GetInstance() != nullptr) {
					SetGeneratingWorldProgress(GWP_RUNSCRIPT, 2500);
					_generating_world = true;
					for (i = 0; i < 2500; i++) {
						Game::GameLoop();
						IncreaseGeneratingWorldProgress(GWP_RUNSCRIPT);
						if (Game::GetInstance()->IsSleeping()) break;
					}
					_generating_world = false;
				}
			}
		}

		BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP);

		ResetObjectToPlace();
		_cur_company.Trash();
		_current_company = _local_company = _gw.lc;

		SetGeneratingWorldProgress(GWP_GAME_START, 1);
		/* Call any callback */
		if (_gw.proc != nullptr) _gw.proc();
		IncreaseGeneratingWorldProgress(GWP_GAME_START);

		CleanupGeneration();
		lock.unlock();

		ShowNewGRFError();

		if (_network_dedicated) DEBUG(net, 1, "Map generated, starting game");
		DEBUG(desync, 1, "new_map: %08x", _settings_game.game_creation.generation_seed);

		if (_debug_desync_level > 0) {
			char name[MAX_PATH];
			seprintf(name, lastof(name), "dmp_cmds_%08x_%08x.sav", _settings_game.game_creation.generation_seed, _date);
			SaveOrLoad(name, SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR, false);
		}
	} catch (...) {
		BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP, true);
		if (_cur_company.IsValid()) _cur_company.Restore();
		_generating_world = false;
		throw;
	}
}

/**
 * Set here the function, if any, that you want to be called when landscape
 * generation is done.
 * @param proc callback procedure
 */
void GenerateWorldSetCallback(GWDoneProc *proc)
{
	_gw.proc = proc;
}

/**
 * Set here the function, if any, that you want to be called when landscape
 * generation is aborted.
 * @param proc callback procedure
 */
void GenerateWorldSetAbortCallback(GWAbortProc *proc)
{
	_gw.abortp = proc;
}

/**
 * This will wait for the thread to finish up his work. It will not continue
 * till the work is done.
 */
void WaitTillGeneratedWorld()
{
	if (!_gw.thread.joinable()) return;

	_modal_progress_work_mutex.unlock();
	_modal_progress_paint_mutex.unlock();
	_gw.quit_thread = true;
	_gw.thread.join();
	_gw.threaded = false;
	_modal_progress_work_mutex.lock();
	_modal_progress_paint_mutex.lock();
}

/**
 * Initializes the abortion process
 */
void AbortGeneratingWorld()
{
	_gw.abort = true;
}

/**
 * Is the generation being aborted?
 * @return the 'aborted' status
 */
bool IsGeneratingWorldAborted()
{
	return _gw.abort;
}

/**
 * Really handle the abortion, i.e. clean up some of the mess
 */
void HandleGeneratingWorldAbortion()
{
	/* Clean up - in SE create an empty map, otherwise, go to intro menu */
	_switch_mode = (_game_mode == GM_EDITOR) ? SM_EDITOR : SM_MENU;

	if (_gw.abortp != nullptr) _gw.abortp();

	CleanupGeneration();

	if (_gw.thread.joinable() && _gw.thread.get_id() == std::this_thread::get_id()) throw OTTDThreadExitSignal();

	SwitchToMode(_switch_mode);
}

/**
 * Generate a world.
 * @param mode The mode of world generation (see GenWorldMode).
 * @param size_x The X-size of the map.
 * @param size_y The Y-size of the map.
 * @param reset_settings Whether to reset the game configuration (used for restart)
 */
void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_settings)
{
	if (HasModalProgress()) return;
	_gw.mode   = mode;
	_gw.size_x = size_x;
	_gw.size_y = size_y;
	SetModalProgress(true);
	_gw.abort  = false;
	_gw.abortp = nullptr;
	_gw.lc     = _local_company;
	_gw.quit_thread   = false;
	_gw.threaded      = true;

	/* This disables some commands and stuff */
	SetLocalCompany(COMPANY_SPECTATOR);

	InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
	PrepareGenerateWorldProgress();

	/* Load the right landscape stuff, and the NewGRFs! */
	GfxLoadSprites();
	LoadStringWidthTable();

	/* Re-init the windowing system */
	ResetWindowSystem();

	/* Create toolbars */
	SetupColoursAndInitialWindow();
	SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);

	if (_gw.thread.joinable()) _gw.thread.join();

	if (!UseThreadedModelProgress() || !VideoDriver::GetInstance()->HasGUI() || !StartNewThread(&_gw.thread, "ottd:genworld", &_GenerateWorld)) {
		DEBUG(misc, 1, "Cannot create genworld thread, reverting to single-threaded mode");
		_gw.threaded = false;
		_modal_progress_work_mutex.unlock();
		_GenerateWorld();
		_modal_progress_work_mutex.lock();
		return;
	}

	UnshowCriticalError();
	/* Remove any open window */
	DeleteAllNonVitalWindows();
	/* Hide vital windows, because we don't allow to use them */
	HideVitalWindows();

	/* Don't show the dialog if we don't have a thread */
	ShowGenerateWorldProgress();

	/* Centre the view on the map */
	if (FindWindowById(WC_MAIN_WINDOW, 0) != nullptr) {
		ScrollMainWindowToTile(TileXY(MapSizeX() / 2, MapSizeY() / 2), true);
	}
}