From 5f6858c379ae7f5b5eb0dc8686149f63800a9c45 Mon Sep 17 00:00:00 2001 From: damfr Date: Mon, 19 Nov 2018 16:03:52 +0100 Subject: [PATCH] Add: Setting to save a unique id for each savegame --- src/genworld.cpp | 5 +++++ src/saveload/afterload.cpp | 6 ++++++ src/saveload/saveload.cpp | 3 ++- src/settings_type.h | 1 + src/table/settings.ini | 9 +++++++++ 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/genworld.cpp b/src/genworld.cpp index 5cdb129b9c..89f13622c2 100644 --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -105,6 +105,11 @@ static void _GenerateWorld(void *) /* 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); + + /* Generates a unique id for the savegame, to avoid accidentally overwriting a save */ + /* We keep id 0 for old savegames that don't have an id */ + _settings_game.game_creation.generation_unique_id = _interactive_random.Next(UINT32_MAX - 1) + 1; /* Generates between [1,UINT32_MAX] */ + SetGeneratingWorldProgress(GWP_MAP_INIT, 2); SetObjectToPlace(SPR_CURSOR_ZZZ, PAL_NONE, HT_NONE, WC_MAIN_WINDOW, 0); diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 077f86bf62..9695f1761d 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -3045,6 +3045,12 @@ bool AfterLoadGame() } } + if (IsSavegameVersionBefore(203)) { + /* Generate a random id for savegames that didn't have one */ + /* We keep id 0 for old savegames that don't have an id */ + _settings_game.game_creation.generation_unique_id = _interactive_random.Next(UINT32_MAX-1) + 1; /* Generates between [1;UINT32_MAX] */ + } + /* Station acceptance is some kind of cache */ if (IsSavegameVersionBefore(127)) { Station *st; diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 8cf5ee36b6..4931168ee2 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -270,8 +270,9 @@ * 200 #6805 Extend railtypes to 64, adding uint16 to map array. * 201 #6885 Extend NewGRF persistant storages. * 202 #6867 Increase industry cargo slots to 16 in, 16 out + * 203 #6973 Add unique id to savegames */ -extern const uint16 SAVEGAME_VERSION = 202; ///< Current savegame version of OpenTTD. +extern const uint16 SAVEGAME_VERSION = 203; ///< Current savegame version of OpenTTD. SavegameType _savegame_type; ///< type of savegame we are loading FileToSaveLoad _file_to_saveload; ///< File to save or load in the openttd loop. diff --git a/src/settings_type.h b/src/settings_type.h index 690f6d8036..bb503da630 100644 --- a/src/settings_type.h +++ b/src/settings_type.h @@ -283,6 +283,7 @@ struct NetworkSettings { /** Settings related to the creation of games. */ struct GameCreationSettings { uint32 generation_seed; ///< noise seed for world generation + uint32 generation_unique_id; ///< random id to differentiate savegames Year starting_year; ///< starting date uint8 map_x; ///< X size of map uint8 map_y; ///< Y size of map diff --git a/src/table/settings.ini b/src/table/settings.ini index c061c394f0..b040e7991a 100644 --- a/src/table/settings.ini +++ b/src/table/settings.ini @@ -2208,6 +2208,15 @@ min = 0 max = UINT32_MAX cat = SC_EXPERT +[SDT_VAR] +base = GameSettings +var = game_creation.generation_unique_id +type = SLE_UINT32 +from = 203 +def = 0 +min = 0 +max = UINT32_MAX + [SDT_VAR] base = GameSettings var = game_creation.tree_placer