]> granicus.if.org Git - nethack/commitdiff
Lua: location-specific timers
authorPasi Kallinen <paxed@alt.org>
Tue, 15 Mar 2022 11:46:50 +0000 (13:46 +0200)
committerPasi Kallinen <paxed@alt.org>
Tue, 15 Mar 2022 11:46:56 +0000 (13:46 +0200)
Expose map-location specific timers to lua scripts. For example:

  nh.start_timer_at(x,y, "melt-ice", 10);

Currently only available timer type is "melt-ice".

doc/lua.adoc
include/timeout.h
src/nhlua.c

index b024a9d37cc331211091e9e76e20faa4d8842ca6..a5081dede207d9b857254a908739d84e91f2fcd9 100644 (file)
@@ -158,6 +158,15 @@ Example:
  local t = nh.gettrap(x, y);
 
 
+=== has_timer_at
+
+Does location at x,y have a timer?
+
+Example:
+
+ local has_melttimer = nh.has_timer_at(x,y, "melt-ice");
+
+
 === deltrap
 
 Delete a trap at x,y
@@ -245,6 +254,15 @@ Example:
  nh.pline("Line: " .. errors[1].line .. ", " .. errors[1].error);
 
 
+=== peek_timer_at
+
+When does timer at location at x,y trigger?
+
+Example:
+
+ local melttime = nh.peek_timer_at(x,y, "melt-ice");
+
+
 === pline
 
 Show the text in the message area.
@@ -308,6 +326,24 @@ Example:
  end
 
 
+=== start_timer_at
+
+Start a timer at location x,y, with trigger time of `when` - relative to current turn.
+
+Example:
+
+ nh.start_timer_at(x,y, "melt-ice", when);
+
+
+=== stop_timer_at
+
+Stop a timer at location x,y.
+
+Example:
+
+ nh.stop_timer_at(x,y, "melt-ice");
+
+
 === verbalize
 
 Show the text in the message area as if someone said it, obeying eg. hero's deafness.
index a36283cf31421e96283bc7124ab7e3a9e8fa6113..45aab9545d869aad667c94a1f6052b4532903dd9 100644 (file)
@@ -39,6 +39,7 @@ enum timeout_types {
     NUM_TIME_FUNCS
 };
 
+#define timer_is_pos(ttype) ((ttype) == MELT_ICE_AWAY)
 #define timer_is_obj(ttype) ((ttype) == ROT_ORGANIC      \
                              || (ttype) == ROT_CORPSE    \
                              || (ttype) == REVIVE_MON    \
index 9afce562979ccbb7b2d56813780a5fc15f7c0e0a..36bf59343476be5a207b277a0afdc3d0cc933fee 100644 (file)
@@ -22,6 +22,10 @@ static int nhl_stairways(lua_State *);
 static int nhl_pushkey(lua_State *);
 static int nhl_doturn(lua_State *);
 static int nhl_debug_flags(lua_State *);
+static int nhl_timer_has_at(lua_State *);
+static int nhl_timer_peek_at(lua_State *);
+static int nhl_timer_stop_at(lua_State *);
+static int nhl_timer_start_at(lua_State *);
 static int nhl_test(lua_State *);
 static int nhl_getmap(lua_State *);
 static char splev_typ2chr(schar);
@@ -1047,6 +1051,91 @@ nhl_debug_flags(lua_State *L)
     return 0;
 }
 
+/* does location at x,y have timer? */
+/* local has_melttimer = nh.has_timer_at(x,y, "melt-ice"); */
+static int
+nhl_timer_has_at(lua_State *L)
+{
+    int argc = lua_gettop(L);
+    boolean ret = FALSE;
+
+    if (argc == 3) {
+        xchar x = (xchar) lua_tointeger(L, 1);
+        xchar y = (xchar) lua_tointeger(L, 2);
+        short timertype = nhl_get_timertype(L, 3);
+        long when = spot_time_expires(x, y, timertype);
+
+        ret = (when > 0L);
+    } else
+        nhl_error(L, "nhl_timer_has_at: Wrong args");
+    lua_pushboolean(L, ret);
+    return 1;
+}
+
+/* when does location at x,y timer trigger? */
+/* local melttime = nh.peek_timer_at(x,y, "melt-ice"); */
+static int
+nhl_timer_peek_at(lua_State *L)
+{
+    int argc = lua_gettop(L);
+    long when = 0L;
+
+    if (argc == 3) {
+        xchar x = (xchar) lua_tointeger(L, 1);
+        xchar y = (xchar) lua_tointeger(L, 2);
+        short timertype = nhl_get_timertype(L, 3);
+
+        if (timer_is_pos(timertype))
+            when = spot_time_expires(x, y, timertype);
+    } else
+        nhl_error(L, "nhl_timer_peek_at: Wrong args");
+    lua_pushinteger(L, when);
+    return 1;
+}
+
+/* stop timer at location x,y */
+/* nh.stop_timer_at(x,y, "melt-ice"); */
+static int
+nhl_timer_stop_at(lua_State *L)
+{
+    int argc = lua_gettop(L);
+
+    if (argc == 3) {
+        xchar x = (xchar) lua_tointeger(L, 1);
+        xchar y = (xchar) lua_tointeger(L, 2);
+        short timertype = nhl_get_timertype(L, 3);
+
+        if (timer_is_pos(timertype))
+            spot_stop_timers(x, y, timertype);
+    } else
+        nhl_error(L, "nhl_timer_stop_at: Wrong args");
+    return 0;
+}
+
+/* start timer at location x,y */
+/* nh.start_timer_at(x,y, "melt-ice", 10); */
+static int
+nhl_timer_start_at(lua_State *L)
+{
+    int argc = lua_gettop(L);
+
+    if (argc == 4) {
+        xchar x = (xchar) lua_tointeger(L, 1);
+        xchar y = (xchar) lua_tointeger(L, 2);
+        short timertype = nhl_get_timertype(L, 3);
+        long when = lua_tointeger(L, 4);
+
+        if (timer_is_pos(timertype)) {
+            long where = ((long) x << 16) | (long) y;
+
+            spot_stop_timers(x, y, timertype);
+            (void) start_timer((long) when, TIMER_LEVEL, MELT_ICE_AWAY,
+                               long_to_any(where));
+        }
+    } else
+        nhl_error(L, "nhl_timer_stop_at: Wrong args");
+    return 0;
+}
 
 static const struct luaL_Reg nhl_functions[] = {
     {"test", nhl_test},
@@ -1058,6 +1147,11 @@ static const struct luaL_Reg nhl_functions[] = {
     {"gettrap", nhl_gettrap},
     {"deltrap", nhl_deltrap},
 
+    {"has_timer_at", nhl_timer_has_at},
+    {"peek_timer_at", nhl_timer_peek_at},
+    {"stop_timer_at", nhl_timer_stop_at},
+    {"start_timer_at", nhl_timer_start_at},
+
     {"pline", nhl_pline},
     {"verbalize", nhl_verbalize},
     {"menu", nhl_menu},