The Battle for Wesnoth  1.17.0-dev
lua_mathx.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
3  Part of the Battle for Wesnoth Project https://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 "scripting/lua_mathx.hpp"
17 #include "scripting/lua_common.hpp"
18 #include "scripting/push_check.hpp"
19 #include "random.hpp"
20 #include "SDL2/SDL_timer.h" // for SDL_GetTicks
21 
22 #include "lua/lauxlib.h"
23 #include "lua/lua.h"
24 #include "lua/lualib.h"
25 
26 #include <cstdio>
27 #include <cmath>
28 
29 namespace lua_mathx {
30 
31 /**
32 * Returns a random number, same interface as math.random.
33 */
34 static int intf_random(lua_State* L)
35 {
36  if (lua_isnoneornil(L, 1)) {
37  double r = static_cast<double>(randomness::generator->next_random());
38  double r_max = static_cast<double>(std::numeric_limits<uint32_t>::max());
39  lua_push(L, r / (r_max + 1));
40  return 1;
41  }
42  else {
43  int32_t min;
44  int32_t max;
45  if (lua_isnumber(L, 2)) {
46  min = lua_check<int32_t>(L, 1);
47  max = lua_check<int32_t>(L, 2);
48  }
49  else {
50  min = 1;
51  max = lua_check<int32_t>(L, 1);
52  }
53  if (min > max) {
54  return luaL_argerror(L, 1, "min > max");
55  }
56  lua_push(L, randomness::generator->get_random_int(min, max));
57  return 1;
58  }
59 }
60 
61 static int intf_round(lua_State* L) {
62  double n = lua_tonumber(L, 1);
63  lua_pushinteger(L, std::round(n));
64  return 1;
65 }
66 
68  auto& lk = lua_kernel_base::get_lua_kernel<lua_kernel_base>(L);
69  lk.add_log("Adding mathx module...\n");
70  static luaL_Reg const math_callbacks[] = {
71  { "random", &intf_random },
72  { "round", &intf_round },
73  { nullptr, nullptr },
74  };
75  lua_newtable(L);
76  luaL_setfuncs(L, math_callbacks, 0);
77  // Set the mathx metatable to index the math module
78  lua_createtable(L, 0, 1);
79  lua_getglobal(L, "math");
80  lua_setfield(L, -2, "__index");
81  lua_setmetatable(L, -2);
82  return 1;
83 }
84 
85 }
#define lua_isnoneornil(L, n)
Definition: lua.h:379
static int intf_round(lua_State *L)
Definition: lua_mathx.cpp:61
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:385
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:34
#define lua_newtable(L)
Definition: lua.h:366
int luaW_open(lua_State *L)
Definition: lua_mathx.cpp:67
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:61
uint32_t next_random()
Provides the next random draw.
Definition: random.cpp:85
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