]> granicus.if.org Git - nethack/commitdiff
Add some player lua methods
authorPasi Kallinen <paxed@alt.org>
Sat, 4 Jan 2020 14:55:50 +0000 (16:55 +0200)
committerPasi Kallinen <paxed@alt.org>
Sat, 4 Jan 2020 14:55:53 +0000 (16:55 +0200)
Adds u.clear_inventory() and u.giveobj(new.obj("rock")) and some other minor stuff.

include/extern.h
src/nhlobj.c
src/nhlua.c

index 4c757b4b3c2348dde4028c96894f06d3c4f71540..7d6ad328cea235ee3a5005ef2b6f33cd9758f770 100644 (file)
@@ -1676,6 +1676,7 @@ E int FDECL(l_selection_register, (lua_State *));
 /* ### nhlobj.c ### */
 #if !defined(CROSSCOMPILE) || defined(CROSSCOMPILE_TARGET)
 E void FDECL(nhl_push_obj, (lua_State *, struct obj *));
+E int FDECL(nhl_obj_u_giveobj, (lua_State *));
 E int FDECL(l_obj_register, (lua_State *));
 #endif
 
index 44fcc32f526a2f67f6a8d274856bc072ce57b870..1b5f7d82bb0180467dee01ba341ff450c18ae151 100644 (file)
@@ -136,6 +136,32 @@ lua_State *L;
     return 0;
 }
 
+/* Put object into player's inventory */
+/* u.giveobj(obj.new("rock")); */
+int
+nhl_obj_u_giveobj(L)
+lua_State *L;
+{
+    struct _lua_obj *lo = l_obj_check(L, 1);
+    struct obj *otmp;
+    int refs;
+
+    if (!lobj_is_ok(lo) || lo->obj->where == OBJ_INVENT)
+        return 0;
+
+    refs = lo->obj->lua_ref_cnt;
+
+    obj_extract_self(lo->obj);
+    otmp = addinv(lo->obj);
+
+    if (otmp != lo->obj) {
+        lo->obj->lua_ref_cnt += refs;
+        lo->obj = otmp;
+    }
+
+    return 0;
+}
+
 /* Get a table of object class data. */
 /* local odata = obj.class(otbl.otyp); */
 /* local odata = obj.class(obj.new("rock")); */
@@ -413,7 +439,7 @@ lua_State *L;
 {
     struct _lua_obj *lo = l_obj_check(L, 1);
 
-    lua_pushboolean(L, lobj_is_ok(lo));
+    lua_pushboolean(L, !lobj_is_ok(lo));
     return 1;
 }
 
index a4c7e0473bbe45a9c7faf3097061f75ec5b16910..0e0bc2e818b92b6817aafbfb60ccfd81cd045d86 100644 (file)
@@ -830,6 +830,13 @@ lua_State *L;
         { "mh", &(u.mh), ANY_INT },
         { "mhmax", &(u.mhmax), ANY_INT },
         { "mtimedone", &(u.mtimedone), ANY_INT },
+        { "dlevel", &(u.uz.dlevel), ANY_SCHAR }, /* actually xchar */
+        { "dnum", &(u.uz.dnum), ANY_SCHAR }, /* actually xchar */
+        { "uluck", &(u.uluck), ANY_SCHAR },
+        { "uhp", &(u.uhp), ANY_INT },
+        { "uhpmax", &(u.uhpmax), ANY_INT },
+        { "uen", &(u.uen), ANY_INT },
+        { "uenmax", &(u.uenmax), ANY_INT },
     };
     int i;
 
@@ -856,11 +863,35 @@ lua_State *L;
     return 0;
 }
 
+static int
+nhl_u_clear_inventory(L)
+lua_State *L;
+{
+    while (g.invent)
+        useupall(g.invent);
+    return 0;
+}
+
+/* Put object into player's inventory */
+/* u.giveobj(obj.new("rock")); */
+static int
+nhl_u_giveobj(L)
+lua_State *L;
+{
+    return nhl_obj_u_giveobj(L);
+}
+
+static const struct luaL_Reg nhl_u_functions[] = {
+    { "clear_inventory", nhl_u_clear_inventory },
+    { "giveobj", nhl_u_giveobj },
+};
+
 void
 init_u_data(L)
 lua_State *L;
 {
     lua_newtable(L);
+    luaL_setfuncs(L, nhl_u_functions, 0);
     lua_newtable(L);
     lua_pushcfunction(L, nhl_meta_u_index);
     lua_setfield(L, -2, "__index");