diff --git a/doc/lua.adoc b/doc/lua.adoc index 859cf922d..6d028085e 100644 --- a/doc/lua.adoc +++ b/doc/lua.adoc @@ -259,6 +259,15 @@ Example: local str = nh.ing_suffix("foo"); +=== int_to_pmname + +Convert integer value to monster type name. + +Example: + + local pmname = nh.int_to_pmname(12); + + === is_genocided Is specific monster type genocided? Returns a boolean value. @@ -1564,6 +1573,9 @@ These constants are in the `nhc` table. |=== | COLNO | Number of map columns | ROWNO | Number of map rows +| NUMMONS | Number of different monster types +| LOW_PM | First monster type id. See <<_int_to_pmname>>. +| HIGH_PM | Last monster type id. See <<_int_to_pmname>>. | DLB | 1 or 0, depending if NetHack is compiled with DLB |=== diff --git a/src/nhlua.c b/src/nhlua.c index 767726277..c83e81865 100644 --- a/src/nhlua.c +++ b/src/nhlua.c @@ -31,6 +31,7 @@ struct e; staticfn int nhl_dump_fmtstr(lua_State *); #endif /* DUMPLOG */ staticfn int nhl_dnum_name(lua_State *); +staticfn int nhl_int_to_pm_name(lua_State *); staticfn int nhl_stairways(lua_State *); staticfn int nhl_pushkey(lua_State *); staticfn int nhl_doturn(lua_State *); @@ -1164,6 +1165,26 @@ nhl_dnum_name(lua_State *L) return 1; } +/* return gender-neutral monster type name by integer value, + or empty string if outside LOW_PM - HIGH_PM range */ +/* local montypename = int_to_pmname(12); */ +staticfn int +nhl_int_to_pm_name(lua_State *L) +{ + int argc = lua_gettop(L); + + if (argc == 1) { + lua_Integer i = luaL_checkinteger(L, 1); + + if (i >= LOW_PM && i <= HIGH_PM) + lua_pushstring(L, mons[i].pmnames[NEUTRAL]); + else + lua_pushstring(L, ""); + } else + nhl_error(L, "Expected an integer parameter"); + return 1; +} + DISABLE_WARNING_UNREACHABLE_CODE /* because nhl_error() does not return */ @@ -1836,6 +1857,7 @@ static const struct luaL_Reg nhl_functions[] = { { "dump_fmtstr", nhl_dump_fmtstr }, #endif /* DUMPLOG */ { "dnum_name", nhl_dnum_name }, + { "int_to_pmname", nhl_int_to_pm_name }, { "variable", nhl_variable }, { "stairways", nhl_stairways }, { "pushkey", nhl_pushkey }, @@ -1851,6 +1873,9 @@ static const struct { } nhl_consts[] = { { "COLNO", COLNO }, { "ROWNO", ROWNO }, + { "NUMMONS", NUMMONS }, + { "LOW_PM", LOW_PM }, + { "HIGH_PM", HIGH_PM }, #ifdef DLB { "DLB", 1 }, #else diff --git a/test/test_des.lua b/test/test_des.lua index 6f4ba5f95..b75f04c1c 100644 --- a/test/test_des.lua +++ b/test/test_des.lua @@ -124,6 +124,11 @@ function test_monster() des.monster({ id = "Angel", align = "law" }); des.monster({ id = "archeologist" }); des.monster({ id = "wizard", name = "Rincewind", peaceful = true }); + + for i = nhc.LOW_PM, nhc.HIGH_PM do + des.monster({ id = nh.int_to_pmname(i) }); + end + des.reset_level(); des.level_init(); end