The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lua_rng.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "lua_rng.hpp"
16 
17 #include "log.hpp"
18 #include "mt_rng.hpp"
19 #include "lua_kernel_base.hpp"
20 
21 #include <new>
22 #include <string>
23 
24 #include "lua/lua.h"
25 #include "lua/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 rand_rng::mt_rng;
34 
35 static const char * Rng = "Rng";
36 
38 {
39  boost::uint32_t seed = lua_kernel_base::get_lua_kernel<lua_kernel_base>(L).get_random_seed();
40  new ( lua_newuserdata(L, sizeof(mt_rng)) ) mt_rng(seed);
41  luaL_setmetatable(L, Rng);
42 
43  return 1;
44 }
45 
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 ) ) << std::endl;
51  ERR_LUA << "This may indicate a memory leak, please report at bugs.wesnoth.org" << std::endl;
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 
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 
70 {
71  mt_rng * rng = static_cast<mt_rng *>(luaL_checkudata(L, 1, Rng));
72 
74  return 1;
75 }
76 
77 // End Lua Rng bindings
78 
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
LUALIB_API void * luaL_checkudata(lua_State *L, int ud, const char *tname)
Definition: lauxlib.cpp:307
LUA_API int lua_type(lua_State *L, int idx)
Definition: lapi.cpp:243
boost::uint32_t uint32_t
Definition: xbrz.hpp:45
uint32_t get_next_random()
Get a new random number.
Definition: mt_rng.cpp:63
int impl_rng_destroy(lua_State *L)
Definition: lua_rng.cpp:46
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:88
#define d
LUALIB_API void luaL_setmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:286
LUA_API void * lua_newuserdata(lua_State *L, size_t size)
Definition: lapi.cpp:1169
static lg::log_domain log_lua("scripting/lua")
LUA_API void lua_setglobal(lua_State *L, const char *var)
Definition: lapi.cpp:728
LUALIB_API void * luaL_testudata(lua_State *L, int ud, const char *tname)
Definition: lauxlib.cpp:292
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
Definition: lapi.cpp:467
LUALIB_API int luaL_newmetatable(lua_State *L, const char *tname)
Definition: lauxlib.cpp:274
static const char * Rng
Definition: lua_rng.cpp:35
int impl_rng_seed(lua_State *L)
Definition: lua_rng.cpp:60
LUA_API void lua_pushvalue(lua_State *L, int idx)
Definition: lapi.cpp:229
#define ERR_LUA
Definition: lua_rng.cpp:28
LUA_API int lua_error(lua_State *L)
Definition: lapi.cpp:1099
Standard logging facilities (interface).
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
Definition: lauxlib.cpp:850
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
GLsizei const GLcharARB ** string
Definition: glew.h:4503
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
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:507
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:752
LUA_API const char * lua_typename(lua_State *L, int t)
Definition: lapi.cpp:249
#define luaL_checkstring(L, n)
Definition: lauxlib.h:115