From bc9d37eb91bd7f980da3396920a26f8ac4bd021d Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Mon, 17 Dec 2018 13:10:11 +0100 Subject: [PATCH] Connects the random seed through all the generators This is very untested and I'm guessing that every level will look identical with this implementation. Need to extend this so that generation seeds are created on boot for every level based on the original seed. Perhaps the "attack"/"defend" seed should also differ from the generation seed to prevent playthroughs from becoming completely identical. --- data/mapgen.lua | 2 +- data/maproombuilder.lua | 4 +++- src/main.c | 4 ++++ src/map_lua.c | 10 ++++++++++ src/random.c | 12 ++++++++++-- src/random.h | 5 ++++- 6 files changed, 32 insertions(+), 5 deletions(-) diff --git a/data/mapgen.lua b/data/mapgen.lua index 4c7a0ef..40ba7a6 100644 --- a/data/mapgen.lua +++ b/data/mapgen.lua @@ -43,7 +43,7 @@ local function generate_path () end local cx, cy = 1, 1 - local seed = time(); + local seed = get_random_seed() info("Map generation seed: " .. seed) randomseed(seed) local direction = 0 diff --git a/data/maproombuilder.lua b/data/maproombuilder.lua index bbe3c8f..6a0f746 100644 --- a/data/maproombuilder.lua +++ b/data/maproombuilder.lua @@ -534,7 +534,9 @@ function module.load_textures(map) t_pit0 = add_texture(map, "Objects/Pit0.png") t_pit1 = add_texture(map, "Objects/Pit1.png") - math.randomseed(os.time()) + local seed = get_random_seed(); + info("Map room random seed: " .. seed) + math.randomseed(seed) local xo = (random(3) - 1) * 112 local yo = (random(8)) * 48 diff --git a/src/main.c b/src/main.c index 8e0ec75..c85584b 100644 --- a/src/main.c +++ b/src/main.c @@ -1282,6 +1282,10 @@ int main(int argc, char *argv[]) PHYSFS_mount("data", NULL, 0); #endif // DEBUG + if (argc > 1) { + set_random_seed(atoi(argv[1])); + } + if (!init()) return 1; diff --git a/src/map_lua.c b/src/map_lua.c index 2d15bd5..82c85c7 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -467,6 +467,13 @@ l_read_file(lua_State *L) return 1; } +static int +l_get_random_seed(lua_State *L) +{ + lua_pushnumber(L, get_random_seed()); + return 1; +} + static Map* generate_map(unsigned int level, const char *file, GameMode gameMode, SDL_Renderer *renderer) { @@ -523,6 +530,9 @@ generate_map(unsigned int level, const char *file, GameMode gameMode, SDL_Render lua_pushcfunction(L, l_add_monster); lua_setglobal(L, "add_monster"); + lua_pushcfunction(L, l_get_random_seed); + lua_setglobal(L, "get_random_seed"); + lua_pushinteger(L, level); lua_setglobal(L, "CURRENT_LEVEL"); diff --git a/src/random.c b/src/random.c index 2b0f6f4..c9d2431 100644 --- a/src/random.c +++ b/src/random.c @@ -20,6 +20,7 @@ #include #include #include "random.h" +#include "util.h" static unsigned int seed = 0; @@ -29,11 +30,19 @@ init_seed(void) if (seed == 0) { seed = (unsigned int) time(NULL); srand(seed); + info("Core random seed: %d", seed); } } +void +set_random_seed(unsigned int s) +{ + seed = s; + info("Core random seed: %d", seed); +} + unsigned int -get_seed(void) +get_random_seed(void) { init_seed(); return seed; @@ -45,4 +54,3 @@ get_random(unsigned int max) init_seed(); return rand() % (max + 1); } - diff --git a/src/random.h b/src/random.h index d74ccb7..2e3587e 100644 --- a/src/random.h +++ b/src/random.h @@ -20,7 +20,10 @@ #define RANDOM_H_ unsigned int -get_seed(void); +get_random_seed(void); + +void +set_random_seed(unsigned int s); unsigned int get_random(unsigned int max);