Generate seeds from the core seed so that levels will look different.

This commit is contained in:
Linus Probert 2019-01-10 19:26:19 +01:00
parent bc9d37eb91
commit ce2a3482d1
5 changed files with 33 additions and 6 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 = get_random_seed() local seed = get_random_seed(CURRENT_LEVEL)
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,7 @@ 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")
local seed = get_random_seed(); local seed = get_random_seed(CURRENT_LEVEL);
info("Map room random seed: " .. seed) info("Map room random seed: " .. seed)
math.randomseed(seed) math.randomseed(seed)
local xo = (random(3) - 1) * 112 local xo = (random(3) - 1) * 112

View File

@ -470,7 +470,8 @@ l_read_file(lua_State *L)
static int static int
l_get_random_seed(lua_State *L) l_get_random_seed(lua_State *L)
{ {
lua_pushnumber(L, get_random_seed()); unsigned int level = (unsigned int) luaL_checkinteger(L, 1);
lua_pushnumber(L, get_random_map_seed(level));
return 1; return 1;
} }

View File

@ -23,14 +23,31 @@
#include "util.h" #include "util.h"
static unsigned int seed = 0; static unsigned int seed = 0;
static unsigned int map_seeds[20];
static unsigned int runtime_seed = 0;
static void
generate_random_seeds(void)
{
// Use seed for generating map seeds
srand(seed);
info("Core random seed: %d", seed);
for (int i = 0; i < 20; ++i) {
map_seeds[i] = rand();
}
// Set a more random seed for runtime random
runtime_seed = (unsigned int) time(NULL);
srand(runtime_seed);
info("Runtime random seed: %d", runtime_seed);
}
static void static void
init_seed(void) init_seed(void)
{ {
if (seed == 0) { if (seed == 0) {
seed = (unsigned int) time(NULL); seed = (unsigned int) time(NULL);
srand(seed); generate_random_seeds();
info("Core random seed: %d", seed);
} }
} }
@ -38,7 +55,7 @@ void
set_random_seed(unsigned int s) set_random_seed(unsigned int s)
{ {
seed = s; seed = s;
info("Core random seed: %d", seed); generate_random_seeds();
} }
unsigned int unsigned int
@ -48,6 +65,12 @@ get_random_seed(void)
return seed; return seed;
} }
unsigned int
get_random_map_seed(unsigned int level)
{
return map_seeds[level-1];
}
unsigned int unsigned int
get_random(unsigned int max) get_random(unsigned int max)
{ {

View File

@ -19,6 +19,9 @@
#ifndef RANDOM_H_ #ifndef RANDOM_H_
#define RANDOM_H_ #define RANDOM_H_
unsigned int
get_random_map_seed(unsigned int level);
unsigned int unsigned int
get_random_seed(void); get_random_seed(void);