The Battle for Wesnoth  1.19.5+dev
lua_rng.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by Chris Beck <render787@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "scripting/lua_rng.hpp"
17 
18 #include "log.hpp"
19 #include "mt_rng.hpp"
21 #include "scripting/lua_common.hpp" // for new(L)
22 
23 #include <string>
24 
25 #include "lua/wrapper_lauxlib.h"
26 
27 static lg::log_domain log_lua("scripting/lua");
28 #define ERR_LUA LOG_STREAM(err, log_lua)
29 
30 // Begin lua rng bindings
31 namespace lua_rng {
32 
33 using randomness::mt_rng;
34 
35 static const char * Rng = "Rng";
36 
37 int impl_rng_create(lua_State* L)
38 {
39  uint32_t seed = lua_kernel_base::get_lua_kernel<lua_kernel_base>(L).get_random_seed();
40  new(L) mt_rng(seed);
41  luaL_setmetatable(L, Rng);
42 
43  return 1;
44 }
45 
46 int impl_rng_destroy(lua_State* L)
47 {
48  mt_rng * d = static_cast< mt_rng *> (luaL_testudata(L, 1, Rng));
49  if (d == nullptr) {
50  ERR_LUA << "rng_destroy called on data of type: " << lua_typename( L, lua_type( L, 1 ) );
51  ERR_LUA << "This may indicate a memory leak, please report at bugs.wesnoth.org";
52  lua_pushstring(L, "Rng object garbage collection failure");
53  lua_error(L);
54  } else {
55  d->~mt_rng();
56  }
57  return 0;
58 }
59 
60 int impl_rng_seed(lua_State* L)
61 {
62  mt_rng * rng = static_cast<mt_rng *>(luaL_checkudata(L, 1, Rng));
63  std::string seed = luaL_checkstring(L, 2);
64 
65  rng->seed_random(seed);
66  return 0;
67 }
68 
69 int impl_rng_draw(lua_State* L)
70 {
71  mt_rng * rng = static_cast<mt_rng *>(luaL_checkudata(L, 1, Rng));
72 
73  lua_pushnumber(L, rng->get_next_random());
74  return 1;
75 }
76 
77 // End Lua Rng bindings
78 
79 void load_tables(lua_State* L)
80 {
81  luaL_newmetatable(L, Rng);
82 
83  static luaL_Reg const callbacks[] {
84  { "create", &impl_rng_create},
85  { "__gc", &impl_rng_destroy},
86  { "seed", &impl_rng_seed},
87  { "draw", &impl_rng_draw},
88  { nullptr, nullptr }
89  };
90  luaL_setfuncs(L, callbacks, 0);
91 
92  lua_pushvalue(L, -1); //make a copy of this table, set it to be its own __index table
93  lua_setfield(L, -2, "__index");
94 
95  lua_setglobal(L, Rng);
96 }
97 
98 } // end namespace lua_map_rng
uint32_t get_next_random()
Get a new random number.
Definition: mt_rng.cpp:63
void seed_random(const std::string &seed, const unsigned int call_count=0)
Same as uint32_t version, but uses a stringstream to convert given hex string.
Definition: mt_rng.cpp:89
Standard logging facilities (interface).
static lg::log_domain log_lua("scripting/lua")
#define ERR_LUA
Definition: lua_rng.cpp:28
int impl_rng_create(lua_State *L)
Implementations for lua callbacks.
Definition: lua_rng.cpp:37
int impl_rng_draw(lua_State *L)
Definition: lua_rng.cpp:69
int impl_rng_seed(lua_State *L)
Definition: lua_rng.cpp:60
static const char * Rng
Definition: lua_rng.cpp:35
int impl_rng_destroy(lua_State *L)
Definition: lua_rng.cpp:46
void load_tables(lua_State *L)
Creates the metatable for RNG objects, and adds the Rng table which contains the constructor.
Definition: lua_rng.cpp:79
#define d