]> granicus.if.org Git - nethack/commitdiff
Lua: accept different params for gold function
authorPasi Kallinen <paxed@alt.org>
Wed, 26 Feb 2020 15:57:47 +0000 (17:57 +0200)
committerPasi Kallinen <paxed@alt.org>
Wed, 26 Feb 2020 15:57:47 +0000 (17:57 +0200)
src/sp_lev.c
test/test_des.lua

index 811dd1685f41aedfdfd3ade73fe44e453d7fb68f..8b5a253d44bfd918b9a05624aa9731a50856c3b9 100755 (executable)
@@ -3935,11 +3935,15 @@ lua_State *L;
     return 0;
 }
 
+/* gold(500, 3,5); */
+/* gold(500, {5, 6}); */
 /* gold({ amount = 500, x = 2, y = 5 });*/
+/* gold(); */
 int
 lspo_gold(L)
 lua_State *L;
 {
+    int argc = lua_gettop(L);
     schar x, y;
     long amount;
     long gcoord;
@@ -3947,16 +3951,31 @@ lua_State *L;
 
     create_des_coder();
 
-    lcheck_param_table(L);
+    if (argc == 3) {
+        amount = luaL_checkinteger(L, 1);
+        x = gx = luaL_checkinteger(L, 2);
+        y = gy = luaL_checkinteger(L, 2);
+    } else if (argc == 2 && lua_type(L, 2) == LUA_TTABLE) {
+        amount = luaL_checkinteger(L, 1);
+        get_coord(L, 2, &gx, &gy);
+        x = gx;
+        y = gy;
+    } else if (argc == 0 || (argc == 1 && lua_type(L, 1) == LUA_TTABLE)) {
+        lcheck_param_table(L);
 
-    x = gx = get_table_int_opt(L, "x", -1);
-    y = gy = get_table_int_opt(L, "y", -1);
+        amount = get_table_int_opt(L, "amount", -1);
+        x = gx = get_table_int_opt(L, "x", -1);
+        y = gy = get_table_int_opt(L, "y", -1);
+    } else {
+        nhl_error(L, "Wrong parameters");
+        return 0;
+    }
 
     if (x == -1 && y == -1)
         gcoord = SP_COORD_PACK_RANDOM(0);
     else
         gcoord = SP_COORD_PACK(gx, gy);
-    amount = get_table_int_opt(L, "amount", -1);
+
     get_location_coord(&x, &y, DRY, g.coder->croom, gcoord);
     if (amount < 0)
         amount = rnd(200);
index 1e848753061389423e36c3a70f78e4e95da14fb7..c006cb057b1c24245e11df310f47992737fdd0b8 100644 (file)
@@ -211,6 +211,9 @@ end
 
 function test_gold()
    des.gold({ amount = 999, x = 40, y = 07 });
+   des.gold();
+   des.gold(666, 41,07);
+   des.gold(123, {42,07});
 end
 
 function test_trap()