]> granicus.if.org Git - nethack/commitdiff
Lua: allow obj chain iteration
authorPasi Kallinen <paxed@alt.org>
Tue, 15 Mar 2022 16:24:49 +0000 (18:24 +0200)
committerPasi Kallinen <paxed@alt.org>
Tue, 15 Mar 2022 16:24:49 +0000 (18:24 +0200)
doc/lua.adoc
src/nhlobj.c

index cf31eeaa3b8329dd2da9b13ad64e71643294dfc0..3e05966afc31e377a5e1ec97e44030003b007ef6 100644 (file)
@@ -1068,11 +1068,17 @@ Example:
 === next
 
 Get the next object in the object chain.
+When called without an object, returns the first object in the object chain.
+When called with an object, an optional boolean parameter can be given. When
+it is true, and the object is on the map, the next object at the same location
+is returned. Otherwise the normal object chain is followed.
 
 Example:
 
- local o = obj.at(x, y);
- local o2 = o:next();
+ local first = obj.next();
+ local second = first:next();
+ local o_at_xy = obj.at(x, y);
+ local next_at_xy = o_at_xy:next(true);
 
 
 === totable
index e0f807e3562b4c570addd30eb1044540e62b1e8b..fa574ea2d21b589c0ab655dc295acdda7a40ef85 100644 (file)
@@ -399,15 +399,28 @@ l_obj_placeobj(lua_State *L)
 
 /* Get the next object in the object chain */
 /* local o = obj.at(x, y);
-   local o2 = o:next();
+   local o2 = o:next(true);
+   local firstobj = obj.next();
 */
 static int
 l_obj_nextobj(lua_State *L)
 {
-    struct _lua_obj *lo = l_obj_check(L, 1);
+    int argc = lua_gettop(L);
+
+    if (argc == 0) {
+        (void) l_obj_push(L, fobj);
+    } else {
+        struct _lua_obj *lo = l_obj_check(L, 1);
+        boolean use_nexthere = FALSE;
 
-    if (lo && lo->obj)
-        (void) l_obj_push(L, lo->obj->where == OBJ_FLOOR ? lo->obj->nexthere : lo->obj->nobj);
+        if (argc == 2)
+            use_nexthere = lua_toboolean(L, 2);
+
+        if (lo && lo->obj)
+            (void) l_obj_push(L, (use_nexthere && lo->obj->where == OBJ_FLOOR)
+                                  ? lo->obj->nexthere
+                                  : lo->obj->nobj);
+    }
     return 1;
 }