The Battle for Wesnoth  1.17.0-dev
lua_mathx.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2020 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #include "scripting/lua_mathx.hpp"
16 #include "scripting/lua_common.hpp"
17 #include "scripting/push_check.hpp"
18 #include "random.hpp"
19 #include "SDL2/SDL_timer.h" // for SDL_GetTicks
20 
21 #include "lua/lauxlib.h"
22 #include "lua/lua.h"
23 #include "lua/lualib.h"
24 
25 #include <cstdio>
26 #include <cmath>
27 
28 namespace lua_mathx {
29 
30 /**
31 * Returns a random number, same interface as math.random.
32 */
33 static int intf_random(lua_State* L)
34 {
35  if (lua_isnoneornil(L, 1)) {
36  double r = static_cast<double>(randomness::generator->next_random());
37  double r_max = static_cast<double>(std::numeric_limits<uint32_t>::max());
38  lua_push(L, r / (r_max + 1));
39  return 1;
40  }
41  else {
42  int32_t min;
43  int32_t max;
44  if (lua_isnumber(L, 2)) {
45  min = lua_check<int32_t>(L, 1);
46  max = lua_check<int32_t>(L, 2);
47  }
48  else {
49  min = 1;
50  max = lua_check<int32_t>(L, 1);
51  }
52  if (min > max) {
53  return luaL_argerror(L, 1, "min > max");
54  }
55  lua_push(L, randomness::generator->get_random_int(min, max));
56  return 1;
57  }
58 }
59 
60 static int intf_round(lua_State* L) {
61  double n = lua_tonumber(L, 1);
62  lua_pushinteger(L, std::round(n));
63  return 1;
64 }
65 
67  auto& lk = lua_kernel_base::get_lua_kernel<lua_kernel_base>(L);
68  lk.add_log("Adding mathx module...\n");
69  static luaL_Reg const math_callbacks[] = {
70  { "random", &intf_random },
71  { "round", &intf_round },
72  { nullptr, nullptr },
73  };
74  lua_newtable(L);
75  luaL_setfuncs(L, math_callbacks, 0);
76  // Set the mathx metatable to index the math module
77  lua_createtable(L, 0, 1);
78  lua_getglobal(L, "math");
79  lua_setfield(L, -2, "__index");
80  lua_setmetatable(L, -2);
81  return 1;
82 }
83 
84 }
#define lua_isnoneornil(L, n)
Definition: lua.h:379
static int intf_round(lua_State *L)
Definition: lua_mathx.cpp:60
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.cpp:728
LUA_API int lua_getglobal(lua_State *L, const char *name)
Definition: lapi.cpp:632
#define lua_tonumber(L, i)
Definition: lua.h:361
void lua_push(lua_State *L, const T &val)
Definition: push_check.hpp:384
LUALIB_API int luaL_argerror(lua_State *L, int arg, const char *extramsg)
Definition: lauxlib.cpp:175
LUA_API int lua_setmetatable(lua_State *L, int objindex)
Definition: lapi.cpp:901
static int intf_random(lua_State *L)
Returns a random number, same interface as math.random.
Definition: lua_mathx.cpp:33
#define lua_newtable(L)
Definition: lua.h:366
int luaW_open(lua_State *L)
Definition: lua_mathx.cpp:66
LUA_API int lua_isnumber(lua_State *L, int idx)
Definition: lapi.cpp:285
rng * generator
This generator is automatically synced during synced context.
Definition: random.cpp:60
uint32_t next_random()
Provides the next random draw.
Definition: random.cpp:84
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
Definition: lauxlib.cpp:904
static map_location::DIRECTION n
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:489
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:837