]> granicus.if.org Git - nethack/commitdiff
Expose core random number functions to lua
authorPasi Kallinen <paxed@alt.org>
Wed, 25 Mar 2020 10:24:02 +0000 (12:24 +0200)
committerPasi Kallinen <paxed@alt.org>
Wed, 25 Mar 2020 10:24:32 +0000 (12:24 +0200)
Expose nh.rn2() and nh.random() to lua.
Add a math.random() compatibility shim to nhlib.lua

dat/nhlib.lua
doc/lua.adoc
src/nhlua.c

index 5cd1e6cf2511593ca3824e00fa43a4dbbde61223..fb35acd1c5eae2b7ea5105391a2c339b3427a622 100644 (file)
@@ -1,5 +1,16 @@
 
-math.randomseed( os.time() )
+-- compatibility shim
+math.random = function(...)
+   local arg = {...};
+   if (#arg == 1) then
+      return 1 + nh.rn2(arg[1]);
+   elseif (#arg == 2) then
+      return nh.random(arg[1], arg[2] + 1 - arg[1]);
+   else
+      -- we don't support reals
+      error("NetHack math.random requires at least one parameter");
+   end
+end
 
 function shuffle(list)
    for i = #list, 2, -1 do
index 0677a921113845f4b74aa6a270f47cf53adf25ae..dff865cd73d1ba88d0d3c62bffc9e694141138d7 100644 (file)
@@ -158,6 +158,23 @@ Example:
  nh.pline("Message text to show.");
 
 
+=== random
+
+Generate a random number.
+
+Example:
+
+ nh.random(10);  -- returns a number between 0 and 9, inclusive.
+ nh.random(1,5); -- same as 1 + nh.random(5);
+
+=== rn2
+
+Generate a random number.
+
+Example:
+
+ nh.rn2(10); -- returns a number between 0 and 9, inclusive.
+
 === s_suffix
 
 Return a string converted to possessive.
index 358d509250396a9a4f199657f31ce87e1c3ebf45..0e5fb08dec64ab07c75d3c4dab18bdae65bceaea 100644 (file)
@@ -30,6 +30,8 @@ static int FDECL(nhl_makesingular, (lua_State *));
 static int FDECL(nhl_s_suffix, (lua_State *));
 static int FDECL(nhl_ing_suffix, (lua_State *));
 static int FDECL(nhl_an, (lua_State *));
+static int FDECL(nhl_rn2, (lua_State *));
+static int FDECL(nhl_random, (lua_State *));
 static int FDECL(nhl_meta_u_index, (lua_State *));
 static int FDECL(nhl_meta_u_newindex, (lua_State *));
 static int FDECL(nhl_u_clear_inventory, (lua_State *));
@@ -615,6 +617,39 @@ lua_State *L;
     return 1;
 }
 
+/* rn2(10) */
+static int
+nhl_rn2(L)
+lua_State *L;
+{
+    int argc = lua_gettop(L);
+
+    if (argc == 1)
+        lua_pushinteger(L, rn2(luaL_checkinteger(L, 1)));
+    else
+        nhl_error(L, "Wrong args");
+
+    return 1;
+}
+
+/* random(10);  -- is the same as rn2(10); */
+/* random(5,8); -- same as 5 + rn2(8); */
+static int
+nhl_random(L)
+lua_State *L;
+{
+    int argc = lua_gettop(L);
+
+    if (argc == 1)
+        lua_pushinteger(L, rn2(luaL_checkinteger(L, 1)));
+    else if (argc == 2)
+        lua_pushinteger(L, luaL_checkinteger(L, 1) + rn2(luaL_checkinteger(L, 2)));
+    else
+        nhl_error(L, "Wrong args");
+
+    return 1;
+}
+
 /* get mandatory integer value from table */
 int
 get_table_int(L, name)
@@ -787,6 +822,8 @@ static const struct luaL_Reg nhl_functions[] = {
     {"s_suffix", nhl_s_suffix},
     {"ing_suffix", nhl_ing_suffix},
     {"an", nhl_an},
+    {"rn2", nhl_rn2},
+    {"random", nhl_random},
     {NULL, NULL}
 };