The Battle for Wesnoth  1.17.0-dev
lua_rng.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
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 <new>
24 #include <string>
25 
26 #include "lua/lua.h"
27 #include "lua/lauxlib.h"
28 
29 static lg::log_domain log_lua("scripting/lua");
30 #define ERR_LUA LOG_STREAM(err, log_lua)
31 
32 // Begin lua rng bindings
33 namespace lua_rng {
34 
35 using randomness::mt_rng;
36 
37 static const char * Rng = "Rng";
38 
40 {
41  uint32_t seed = lua_kernel_base::get_lua_kernel<lua_kernel_base>(L).get_random_seed();
42  new(L) mt_rng(seed);
43  luaL_setmetatable(L, Rng);
44 
45  return 1;
46 }
47 
49 {
50  mt_rng * d = static_cast< mt_rng *> (luaL_testudata(L, 1, Rng));
51  if (d == nullptr) {
52  ERR_LUA << "rng_destroy called on data of type: " << lua_typename( L, lua_type( L, 1 ) ) << std::endl;
53  ERR_LUA << "This may indicate a memory leak, please report at bugs.wesnoth.org" << std::endl;
54  lua_pushstring(L, "Rng object garbage collection failure");
55  lua_error(L);
56  } else {
57  d->~mt_rng();
58  }
59  return 0;
60 }
61 
63 {
64  mt_rng * rng = static_cast<mt_rng *>(luaL_checkudata(L, 1, Rng));
65  std::string seed = luaL_checkstring(L, 2);
66 
67  rng->seed_random(seed);
68  return 0;
69 }
70 
72 {
73  mt_rng * rng = static_cast<mt_rng *>(luaL_checkudata(L, 1, Rng));
74 
76  return 1;
77 }
78 
79 // End Lua Rng bindings
80 
82 {
83  luaL_newmetatable(L, Rng);
84 
85  static luaL_Reg const callbacks[] {
86  { "create", &impl_rng_create},
87  { "__gc", &impl_rng_destroy},
88  { "seed", &impl_rng_seed},
89  { "draw", &impl_rng_draw},
90  { nullptr, nullptr }
91  };
92  luaL_setfuncs(L, callbacks, 0);
93 
94  lua_pushvalue(L, -1); //make a copy of this table, set it to be its own __index table
95  lua_setfield(L, -2, "__index");
96 
97  lua_setglobal(L, Rng);
98 }
99 
100 } // end namespace lua_map_rng
LUALIB_API void * luaL_checkudata(lua_State *L, int ud, const char *tname)
Definition: lauxlib.cpp:345
LUA_API int lua_type(lua_State *L, int idx)
Definition: lapi.cpp:260
int impl_rng_destroy(lua_State *L)
Definition: lua_rng.cpp:48
uint32_t get_next_random()
Get a new random number.
Definition: mt_rng.cpp:63
#define d
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:324
static lg::log_domain log_lua("scripting/lua")
LUA_API void lua_setglobal(lua_State *L, const char *name)
Definition: lapi.cpp:813
LUALIB_API void * luaL_testudata(lua_State *L, int ud, const char *tname)
Definition: lauxlib.cpp:330
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
Definition: lapi.cpp:481
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:311
static const char * Rng
Definition: lua_rng.cpp:37
int impl_rng_seed(lua_State *L)
Definition: lua_rng.cpp:62
LUA_API void lua_pushvalue(lua_State *L, int idx)
Definition: lapi.cpp:246
#define ERR_LUA
Definition: lua_rng.cpp:30
LUA_API int lua_error(lua_State *L)
Definition: lapi.cpp:1205
Standard logging facilities (interface).
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
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
Definition: lauxlib.cpp:904
int impl_rng_create(lua_State *L)
Implementations for lua callbacks.
Definition: lua_rng.cpp:39
int impl_rng_draw(lua_State *L)
Definition: lua_rng.cpp:71
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:81
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:514
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:837
LUA_API const char * lua_typename(lua_State *L, int t)
Definition: lapi.cpp:266
#define luaL_checkstring(L, n)
Definition: lauxlib.h:138