diff --git a/bh_random/src/bh_random.cpp b/bh_random/src/bh_random.cpp index 88742ec..4934584 100644 --- a/bh_random/src/bh_random.cpp +++ b/bh_random/src/bh_random.cpp @@ -23,6 +23,7 @@ extern "C" { } static std::mt19937 generator; +static std::mt19937 map_generator; static std::uniform_int_distribution 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); +} diff --git a/bh_random/src/bh_random.h b/bh_random/src/bh_random.h index d967631..c6a0de9 100644 --- a/bh_random/src/bh_random.h +++ b/bh_random/src/bh_random.h @@ -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); diff --git a/data/chestgen.lua b/data/chestgen.lua index 6f53c2c..bc22d7a 100644 --- a/data/chestgen.lua +++ b/data/chestgen.lua @@ -1,6 +1,6 @@ local room_builder = require "maproombuilder" local module = {} -local random = math.random +local random = map_random local textures = { "Items/Chest0.png", diff --git a/data/mapgen.lua b/data/mapgen.lua index 5b03d5c..ff7f3c8 100644 --- a/data/mapgen.lua +++ b/data/mapgen.lua @@ -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 diff --git a/data/maproombuilder.lua b/data/maproombuilder.lua index 1ac3b1a..4ad53dc 100644 --- a/data/maproombuilder.lua +++ b/data/maproombuilder.lua @@ -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 diff --git a/data/monstergen.lua b/data/monstergen.lua index a7ea9dd..3faba59 100644 --- a/data/monstergen.lua +++ b/data/monstergen.lua @@ -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", diff --git a/data/trapgen.lua b/data/trapgen.lua index 8e47892..9abdfc9 100644 --- a/data/trapgen.lua +++ b/data/trapgen.lua @@ -1,6 +1,6 @@ local room_builder = require "maproombuilder" local module = {} -local random = math.random +local random = map_random local textures = { "Objects/Trap1.png", diff --git a/src/map_lua.c b/src/map_lua.c index 688cbd5..e2bd492 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -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");