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.
This commit is contained in:
Linus Probert 2018-12-17 13:10:11 +01:00
parent 3c7dcb5ea1
commit bc9d37eb91
6 changed files with 32 additions and 5 deletions

View File

@ -43,7 +43,7 @@ local function generate_path ()
end end
local cx, cy = 1, 1 local cx, cy = 1, 1
local seed = time(); local seed = get_random_seed()
info("Map generation seed: " .. seed) info("Map generation seed: " .. seed)
randomseed(seed) randomseed(seed)
local direction = 0 local direction = 0

View File

@ -534,7 +534,9 @@ function module.load_textures(map)
t_pit0 = add_texture(map, "Objects/Pit0.png") t_pit0 = add_texture(map, "Objects/Pit0.png")
t_pit1 = add_texture(map, "Objects/Pit1.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 xo = (random(3) - 1) * 112
local yo = (random(8)) * 48 local yo = (random(8)) * 48

View File

@ -1282,6 +1282,10 @@ int main(int argc, char *argv[])
PHYSFS_mount("data", NULL, 0); PHYSFS_mount("data", NULL, 0);
#endif // DEBUG #endif // DEBUG
if (argc > 1) {
set_random_seed(atoi(argv[1]));
}
if (!init()) if (!init())
return 1; return 1;

View File

@ -467,6 +467,13 @@ l_read_file(lua_State *L)
return 1; return 1;
} }
static int
l_get_random_seed(lua_State *L)
{
lua_pushnumber(L, get_random_seed());
return 1;
}
static Map* static Map*
generate_map(unsigned int level, const char *file, GameMode gameMode, SDL_Renderer *renderer) 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_pushcfunction(L, l_add_monster);
lua_setglobal(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_pushinteger(L, level);
lua_setglobal(L, "CURRENT_LEVEL"); lua_setglobal(L, "CURRENT_LEVEL");

View File

@ -20,6 +20,7 @@
#include <time.h> #include <time.h>
#include <stdbool.h> #include <stdbool.h>
#include "random.h" #include "random.h"
#include "util.h"
static unsigned int seed = 0; static unsigned int seed = 0;
@ -29,11 +30,19 @@ init_seed(void)
if (seed == 0) { if (seed == 0) {
seed = (unsigned int) time(NULL); seed = (unsigned int) time(NULL);
srand(seed); 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 unsigned int
get_seed(void) get_random_seed(void)
{ {
init_seed(); init_seed();
return seed; return seed;
@ -45,4 +54,3 @@ get_random(unsigned int max)
init_seed(); init_seed();
return rand() % (max + 1); return rand() % (max + 1);
} }

View File

@ -20,7 +20,10 @@
#define RANDOM_H_ #define RANDOM_H_
unsigned int unsigned int
get_seed(void); get_random_seed(void);
void
set_random_seed(unsigned int s);
unsigned int unsigned int
get_random(unsigned int max); get_random(unsigned int max);