Implements control over lua random numbers

This commit is contained in:
Linus Probert 2019-02-22 00:09:31 +01:00
parent 1205856d00
commit cc375bba4e
8 changed files with 50 additions and 7 deletions

View File

@ -23,6 +23,7 @@ extern "C" {
}
static std::mt19937 generator;
static std::mt19937 map_generator;
static std::uniform_int_distribution<int> distribution(0, INT_MAX);
extern "C" void
@ -37,3 +38,16 @@ bh_rand(void)
{
return distribution(generator);
}
extern "C" void
bh_map_srand(unsigned int seed)
{
map_generator.seed(seed);
}
extern "C" unsigned int
bh_map_rand(void)
{
return distribution(map_generator);
}

View File

@ -23,3 +23,9 @@ bh_srand(unsigned int);
unsigned int
bh_rand(void);
void
bh_map_srand(unsigned int);
unsigned int
bh_map_rand(void);

View File

@ -1,6 +1,6 @@
local room_builder = require "maproombuilder"
local module = {}
local random = math.random
local random = map_random
local textures = {
"Items/Chest0.png",

View File

@ -5,8 +5,8 @@ local chest_gen = require "chestgen"
-- Setting up some functions
local time = os.time
local random = math.random
local randomseed = math.randomseed
local random = map_random
local randomseed = map_randomseed
-- CONSTANTS
local UP = 1

View File

@ -1,5 +1,5 @@
-- FUNCTIONS
local random = math.random
local random = map_random
-- CONSTANTS
local UP = 1
@ -536,7 +536,7 @@ function module.load_textures(map)
local seed = get_random_seed(CURRENT_LEVEL);
info("Map room random seed: " .. seed)
math.randomseed(seed)
map_randomseed(seed)
local xo = (random(3) - 1) * 112
local yo = (random(8)) * 48

View File

@ -1,6 +1,6 @@
local room_builder = require "maproombuilder"
local module = {}
local random = math.random
local random = map_random
local texturePaths = {
aquatic0 = "Characters/Aquatic0.png",

View File

@ -1,6 +1,6 @@
local room_builder = require "maproombuilder"
local module = {}
local random = math.random
local random = map_random
local textures = {
"Objects/Trap1.png",

View File

@ -36,6 +36,7 @@
#include "item.h"
#include "item_builder.h"
#include "random.h"
#include "bh_random.h"
static
lua_State* load_lua_state(void)
@ -475,6 +476,22 @@ l_get_random_seed(lua_State *L)
return 1;
}
static int
l_set_random_seed(lua_State *L)
{
unsigned int seed = (unsigned int) luaL_checkinteger(L, 1);
bh_map_srand(seed);
return 0;
}
static int
l_get_random(lua_State *L)
{
unsigned int max = (unsigned int) luaL_checkinteger(L, 1);
lua_pushnumber(L, (bh_map_rand() % max) + 1);
return 1;
}
static Map*
generate_map(unsigned int level, const char *file, GameMode gameMode, SDL_Renderer *renderer)
{
@ -534,6 +551,12 @@ generate_map(unsigned int level, const char *file, GameMode gameMode, SDL_Render
lua_pushcfunction(L, l_get_random_seed);
lua_setglobal(L, "get_random_seed");
lua_pushcfunction(L, l_set_random_seed);
lua_setglobal(L, "map_randomseed");
lua_pushcfunction(L, l_get_random);
lua_setglobal(L, "map_random");
lua_pushinteger(L, level);
lua_setglobal(L, "CURRENT_LEVEL");