From eb760e1776e4f09690d5d3a2fb7ebaf93e3538c1 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Tue, 16 Sep 2025 19:01:47 +0300 Subject: [PATCH] add 'random' library --- src/logic/scripting/lua/libs/api_lua.hpp | 1 + src/logic/scripting/lua/libs/librandom.cpp | 46 ++++++++++++++++++++++ src/logic/scripting/lua/lua_engine.cpp | 1 + src/util/random.cpp | 44 +++++++++++++++++++++ src/util/random.hpp | 19 +++++++++ 5 files changed, 111 insertions(+) create mode 100644 src/logic/scripting/lua/libs/librandom.cpp create mode 100644 src/util/random.cpp create mode 100644 src/util/random.hpp diff --git a/src/logic/scripting/lua/libs/api_lua.hpp b/src/logic/scripting/lua/libs/api_lua.hpp index c1d9f933..6c24d9d4 100644 --- a/src/logic/scripting/lua/libs/api_lua.hpp +++ b/src/logic/scripting/lua/libs/api_lua.hpp @@ -42,6 +42,7 @@ extern const luaL_Reg pathfindinglib[]; extern const luaL_Reg playerlib[]; extern const luaL_Reg posteffectslib[]; // gfx.posteffects extern const luaL_Reg quatlib[]; +extern const luaL_Reg randomlib[]; extern const luaL_Reg text3dlib[]; // gfx.text3d extern const luaL_Reg timelib[]; extern const luaL_Reg tomllib[]; diff --git a/src/logic/scripting/lua/libs/librandom.cpp b/src/logic/scripting/lua/libs/librandom.cpp new file mode 100644 index 00000000..fce0d62b --- /dev/null +++ b/src/logic/scripting/lua/libs/librandom.cpp @@ -0,0 +1,46 @@ +#include "api_lua.hpp" + +#include "util/random.hpp" + +static std::random_device random_device; + +static int l_random(lua::State* L) { + int argc = lua::gettop(L); + + auto randomEngine = util::seeded_random_engine(random_device); + if (argc == 0) { + std::uniform_real_distribution<> dist(0.0, 1.0); + return lua::pushnumber(L, dist(randomEngine)); + } else if (argc == 1) { + std::uniform_int_distribution dist(0, lua::tointeger(L, 1) - 1); + return lua::pushinteger(L, dist(randomEngine)); + } else { + std::uniform_int_distribution dist( + lua::tointeger(L, 1), lua::tointeger(L, 2) - 1 + ); + return lua::pushinteger(L, dist(randomEngine)); + } +} + +static int l_bytes(lua::State* L) { + size_t size = lua::tointeger(L, 1); + + auto randomEngine = util::seeded_random_engine(random_device); + static std::uniform_int_distribution dist(0, 0xFF); + std::vector bytes (size); + for (size_t i = 0; i < bytes.size(); i++) { + bytes[i] = dist(randomEngine); + } + return lua::create_bytearray(L, bytes); +} + +static int l_uuid(lua::State* L) { + return lua::pushlstring(L, util::generate_uuid()); +} + +const luaL_Reg randomlib[] = { + {"random", lua::wrap}, + {"bytes", lua::wrap}, + {"uuid", lua::wrap}, + {NULL, NULL} +}; diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index f6ce4fe3..891614f0 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -51,6 +51,7 @@ static void create_libs(State* L, StateType stateType) { openlib(L, "mat4", mat4lib); openlib(L, "pack", packlib); openlib(L, "quat", quatlib); + openlib(L, "random", randomlib); openlib(L, "toml", tomllib); openlib(L, "utf8", utf8lib); openlib(L, "vec2", vec2lib); diff --git a/src/util/random.cpp b/src/util/random.cpp new file mode 100644 index 00000000..a1ffb66c --- /dev/null +++ b/src/util/random.cpp @@ -0,0 +1,44 @@ +#include "random.hpp" + +#include + +static std::random_device random_device; + +static const char* uuid_hex_chars = "0123456789abcdef"; +static const char* uuid_hex_variant_chars = "89ab"; + +std::string util::generate_uuid() { + auto randomEngine = seeded_random_engine(random_device); + static std::uniform_int_distribution<> dist(0, 15); + static std::uniform_int_distribution<> dist2(0, 3); + + std::string uuid; + uuid.resize(36); + + for (int i = 0; i < 8; i++) { + uuid[i] = uuid_hex_chars[dist(randomEngine)]; + } + uuid[8] = '-'; + for (int i = 9; i < 13; i++) { + uuid[i] = uuid_hex_chars[dist(randomEngine)]; + } + uuid[13] = '-'; + uuid[14] = '4'; + + for (int i = 15; i < 18; i++) { + uuid[i] = uuid_hex_chars[dist(randomEngine)]; + } + + uuid[18] = '-'; + uuid[19] = uuid_hex_variant_chars[dist2(randomEngine)]; + + for (int i = 20; i < 23; i++) { + uuid[i] = uuid_hex_chars[dist(randomEngine)]; + } + + uuid[23] = '-'; + for (int i = 24; i < 36; i++) { + uuid[i] = uuid_hex_chars[dist(randomEngine)]; + } + return uuid; +} diff --git a/src/util/random.hpp b/src/util/random.hpp new file mode 100644 index 00000000..fc79317d --- /dev/null +++ b/src/util/random.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include +#include +#include + +namespace util { + template + typename std::enable_if::type seeded_random_engine( + std::random_device& source + ) { + std::random_device::result_type randomData[(N - 1) / sizeof(source()) + 1]; + std::generate(std::begin(randomData), std::end(randomData), std::ref(source)); + std::seed_seq seeds(std::begin(randomData), std::end(randomData)); + return T(seeds); + } + + std::string generate_uuid(); +}