Lua: location-specific timers

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".
This commit is contained in:
Pasi Kallinen
2022-03-15 13:46:50 +02:00
parent 006ae67029
commit 957c0fbee0
3 changed files with 131 additions and 0 deletions

View 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.

View 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 \

View 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},