mirror of https://github.com/OpenTTD/OpenTTD
Fix: Initialise random seed earlier in new game process.
This prevents setting up script settings with the seed not initialised.pull/11937/head
parent
232151788d
commit
3f3e42e56e
|
@ -45,6 +45,19 @@ inline void SaveRandomSeeds(SavedRandomSeeds *storage)
|
||||||
storage->interactive_random = _interactive_random;
|
storage->interactive_random = _interactive_random;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if random seeds have been changed.
|
||||||
|
* @param saved_seeds Saved random seeds.
|
||||||
|
* @returns true if the saved seeds match the current random seeds.
|
||||||
|
*/
|
||||||
|
inline bool CheckRandomSeeds(const SavedRandomSeeds &saved_seeds)
|
||||||
|
{
|
||||||
|
return (saved_seeds.random.state[0] == _random.state[0])
|
||||||
|
&& (saved_seeds.random.state[1] == _random.state[1])
|
||||||
|
&& (saved_seeds.interactive_random.state[0] == _interactive_random.state[0])
|
||||||
|
&& (saved_seeds.interactive_random.state[1] == _interactive_random.state[1]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Restores previously saved seeds
|
* Restores previously saved seeds
|
||||||
* @param storage Storage where SaveRandomSeeds() stored th seeds
|
* @param storage Storage where SaveRandomSeeds() stored th seeds
|
||||||
|
|
|
@ -91,11 +91,9 @@ static void _GenerateWorld()
|
||||||
try {
|
try {
|
||||||
_generating_world = true;
|
_generating_world = true;
|
||||||
if (_network_dedicated) Debug(net, 3, "Generating map, please wait...");
|
if (_network_dedicated) Debug(net, 3, "Generating map, please wait...");
|
||||||
/* Set the Random() seed to generation_seed so we produce the same map with the same seed */
|
|
||||||
_random.SetSeed(_settings_game.game_creation.generation_seed);
|
|
||||||
SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
|
SetGeneratingWorldProgress(GWP_MAP_INIT, 2);
|
||||||
SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
|
SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0);
|
||||||
ScriptObject::InitializeRandomizers();
|
|
||||||
|
|
||||||
BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP);
|
BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP);
|
||||||
|
|
||||||
|
@ -289,6 +287,10 @@ void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_setti
|
||||||
SetLocalCompany(COMPANY_SPECTATOR);
|
SetLocalCompany(COMPANY_SPECTATOR);
|
||||||
|
|
||||||
InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
|
InitializeGame(_gw.size_x, _gw.size_y, true, reset_settings);
|
||||||
|
|
||||||
|
SavedRandomSeeds saved_seeds;
|
||||||
|
SaveRandomSeeds(&saved_seeds);
|
||||||
|
|
||||||
PrepareGenerateWorldProgress();
|
PrepareGenerateWorldProgress();
|
||||||
|
|
||||||
if (_settings_game.construction.map_height_limit == 0) {
|
if (_settings_game.construction.map_height_limit == 0) {
|
||||||
|
@ -307,8 +309,6 @@ void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_setti
|
||||||
_settings_game.construction.map_height_limit = std::max(MAP_HEIGHT_LIMIT_AUTO_MINIMUM, std::min(MAX_MAP_HEIGHT_LIMIT, estimated_height + MAP_HEIGHT_LIMIT_AUTO_CEILING_ROOM));
|
_settings_game.construction.map_height_limit = std::max(MAP_HEIGHT_LIMIT_AUTO_MINIMUM, std::min(MAX_MAP_HEIGHT_LIMIT, estimated_height + MAP_HEIGHT_LIMIT_AUTO_CEILING_ROOM));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_settings_game.game_creation.generation_seed == GENERATE_NEW_SEED) _settings_game.game_creation.generation_seed = InteractiveRandom();
|
|
||||||
|
|
||||||
/* Load the right landscape stuff, and the NewGRFs! */
|
/* Load the right landscape stuff, and the NewGRFs! */
|
||||||
GfxLoadSprites();
|
GfxLoadSprites();
|
||||||
LoadStringWidthTable();
|
LoadStringWidthTable();
|
||||||
|
@ -329,5 +329,9 @@ void GenerateWorld(GenWorldMode mode, uint size_x, uint size_y, bool reset_setti
|
||||||
/* Centre the view on the map */
|
/* Centre the view on the map */
|
||||||
ScrollMainWindowToTile(TileXY(Map::SizeX() / 2, Map::SizeY() / 2), true);
|
ScrollMainWindowToTile(TileXY(Map::SizeX() / 2, Map::SizeY() / 2), true);
|
||||||
|
|
||||||
|
/* Initialization should use Random() as that could affect the ability to recreate the same map with the
|
||||||
|
* same settings. (This is not guaranteed between different versions though.) */
|
||||||
|
assert(CheckRandomSeeds(saved_seeds));
|
||||||
|
|
||||||
_GenerateWorld();
|
_GenerateWorld();
|
||||||
}
|
}
|
||||||
|
|
16
src/misc.cpp
16
src/misc.cpp
|
@ -8,6 +8,10 @@
|
||||||
/** @file misc.cpp Misc functions that shouldn't be here. */
|
/** @file misc.cpp Misc functions that shouldn't be here. */
|
||||||
|
|
||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
|
#include "core/random_func.hpp"
|
||||||
|
#include "ai/ai_config.hpp"
|
||||||
|
#include "game/game_config.hpp"
|
||||||
|
#include "genworld.h"
|
||||||
#include "landscape.h"
|
#include "landscape.h"
|
||||||
#include "news_func.h"
|
#include "news_func.h"
|
||||||
#include "ai/ai.hpp"
|
#include "ai/ai.hpp"
|
||||||
|
@ -173,4 +177,16 @@ void InitializeGame(uint size_x, uint size_y, bool reset_date, bool reset_settin
|
||||||
_gamelog.Mode();
|
_gamelog.Mode();
|
||||||
_gamelog.GRFAddList(_grfconfig);
|
_gamelog.GRFAddList(_grfconfig);
|
||||||
_gamelog.StopAction();
|
_gamelog.StopAction();
|
||||||
|
|
||||||
|
/* 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 = InteractiveRandom();
|
||||||
|
|
||||||
|
_random.SetSeed(_settings_game.game_creation.generation_seed);
|
||||||
|
ScriptObject::InitializeRandomizers();
|
||||||
|
|
||||||
|
/* Set random deviation for scripts. */
|
||||||
|
for (auto &ai_config : _settings_game.ai_config) {
|
||||||
|
if (ai_config != nullptr) ai_config->AddRandomDeviation();
|
||||||
|
}
|
||||||
|
if (_settings_game.game_config != nullptr) _settings_game.game_config->AddRandomDeviation();
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,9 +57,6 @@ ScriptConfig::ScriptConfig(const ScriptConfig *config)
|
||||||
for (const auto &item : config->settings) {
|
for (const auto &item : config->settings) {
|
||||||
this->settings[item.first] = item.second;
|
this->settings[item.first] = item.second;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Virtual functions get called statically in constructors, so make it explicit to remove any confusion. */
|
|
||||||
this->ScriptConfig::AddRandomDeviation();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptConfig::~ScriptConfig()
|
ScriptConfig::~ScriptConfig()
|
||||||
|
|
Loading…
Reference in New Issue