From 5194ce66d60829d7658eaf7c4e570bb88077d326 Mon Sep 17 00:00:00 2001 From: Jude Brown Date: Fri, 27 Nov 2009 14:52:31 +1000 Subject: New Lua library: feat. Provides wrappers for all of the feat_is_XXX functions from terrain.cc, as well as a few other functions. Also provides a macro which can wrap a function to take: * a set of co-ordinates (parsed with grd(coord_def(x, y))) * a string (parsed with dungeon_feature_by_name) * an integer (cast into dungeon_feature_type, can be fetched from dgn.grid(x, y)) All of the feat library functions can be used in this manner. For example: * feat.is_wall(x, y) * feat.is_wall(dgn.grid(x, y)) * feat.is_wall(7) * feat.is_wall("rock_wall") --- crawl-ref/source/dat/clua/ziggurat.lua | 2 +- crawl-ref/source/dlua.cc | 1 + crawl-ref/source/l_dgngrd.cc | 15 +----- crawl-ref/source/l_feat.cc | 87 ++++++++++++++++++++++++++++++++++ crawl-ref/source/l_libs.h | 1 + crawl-ref/source/makefile.obj | 1 + crawl-ref/source/test/findray.lua | 2 +- 7 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 crawl-ref/source/l_feat.cc (limited to 'crawl-ref/source') diff --git a/crawl-ref/source/dat/clua/ziggurat.lua b/crawl-ref/source/dat/clua/ziggurat.lua index 49ea42dd15..1eb112cb11 100644 --- a/crawl-ref/source/dat/clua/ziggurat.lua +++ b/crawl-ref/source/dat/clua/ziggurat.lua @@ -553,7 +553,7 @@ local function ziggurat_create_loot_vault(entry, exit) local inc = (exit - entry):sgn() local function find_door_spot(p) - while not dgn.is_wall(dgn.grid(p.x, p.y)) do + while not feat.is_wall(p.x, p.y) do p = p + inc end return p diff --git a/crawl-ref/source/dlua.cc b/crawl-ref/source/dlua.cc index 256f3210bf..12e3da2860 100644 --- a/crawl-ref/source/dlua.cc +++ b/crawl-ref/source/dlua.cc @@ -282,6 +282,7 @@ void init_dungeon_lua() luaL_openlib(dlua, "dgn", dgn_level_dlib, 0); luaL_openlib(dlua, "dgn", dgn_mons_dlib, 0); luaL_openlib(dlua, "dgn", dgn_tile_dlib, 0); + luaL_openlib(dlua, "feat", feat_dlib, 0); luaL_openlib(dlua, "debug", debug_dlib, 0); luaL_openlib(dlua, "los", los_dlib, 0); diff --git a/crawl-ref/source/l_dgngrd.cc b/crawl-ref/source/l_dgngrd.cc index d72dab589c..c5e89b175f 100644 --- a/crawl-ref/source/l_dgngrd.cc +++ b/crawl-ref/source/l_dgngrd.cc @@ -160,10 +160,6 @@ static int dgn_grid(lua_State *ls) PLUARET(number, grd(c)); } -// XXX: these two shouldn't be so different. -LUARET1(_dgn_is_wall, boolean, - feat_is_wall(static_cast(luaL_checkint(ls, 1)))) - LUAFN(dgn_distance) { COORDS(p1, 1, 2); @@ -172,13 +168,6 @@ LUAFN(dgn_distance) return (1); } -LUAFN(_dgn_is_opaque) -{ - COORDS(c, 1, 2); - lua_pushboolean(ls, feat_is_opaque(grd(c))); - return (1); -} - LUAFN(dgn_seen_replace_feat) { dungeon_feature_type f1 = _get_lua_feature(ls, 1); @@ -200,11 +189,11 @@ const struct luaL_reg dgn_grid_dlib[] = { "seen_replace_feat", dgn_seen_replace_feat }, { "grid", dgn_grid }, -{ "is_opaque", _dgn_is_opaque }, -{ "is_wall", _dgn_is_wall }, { "max_bounds", dgn_max_bounds }, { "in_bounds", dgn_in_bounds }, { "distance", dgn_distance }, + + { NULL, NULL } }; diff --git a/crawl-ref/source/l_feat.cc b/crawl-ref/source/l_feat.cc new file mode 100644 index 0000000000..ce5cebce83 --- /dev/null +++ b/crawl-ref/source/l_feat.cc @@ -0,0 +1,87 @@ +/* + * File: l_featfeat.cc + * Summary: Boolean feat-related functions lua library "feat". + */ + +#include "AppHdr.h" + +#include "clua.h" +#include "cluautil.h" +#include "l_libs.h" + +#include "coord.h" +#include "dungeon.h" +#include "terrain.h" + +#define FEATF(name, val) \ + static int name(lua_State *ls) \ + { \ + if (lua_gettop(ls) == 2) \ + { \ + COORDS(c, 1, 2);\ + lua_pushboolean(ls, val(grd(c)));\ + } \ + else if (lua_isnumber(ls, 1)) \ + lua_pushboolean(ls, val(static_cast( \ + luaL_checkint(ls, 1)))); \ + else if (lua_isstring(ls, 1)) \ + lua_pushboolean(ls, val(dungeon_feature_by_name(\ + luaL_checkstring(ls, 1))));\ + return (1); \ + } + +FEATF(_feat_destroys_items, feat_destroys_items) + +FEATF(_feat_is_wall, feat_is_wall) +FEATF(_feat_is_solid, feat_is_solid) +FEATF(_feat_is_opaque, feat_is_opaque) +FEATF(_feat_is_closed_door, feat_is_closed_door) +FEATF(_feat_is_secret_door, feat_is_secret_door) +FEATF(_feat_is_statue_or_idol, feat_is_statue_or_idol) +FEATF(_feat_is_rock, feat_is_rock) +FEATF(_feat_is_permarock, feat_is_permarock) +FEATF(_feat_is_stone_stair, feat_is_stone_stair) +FEATF(_feat_is_staircase, feat_is_staircase) +FEATF(_feat_is_escape_hatch, feat_is_escape_hatch) +FEATF(_feat_is_trap, feat_is_trap) +FEATF(_feat_is_sealable_portal, feat_sealable_portal) +FEATF(_feat_is_portal, feat_is_portal) +FEATF(_feat_is_stair, feat_is_stair) +FEATF(_feat_is_travelable_stair, feat_is_travelable_stair) +FEATF(_feat_is_gate, feat_is_gate) +FEATF(_feat_is_water, feat_is_water) +FEATF(_feat_is_watery, feat_is_watery) +FEATF(_feat_is_altar, feat_is_altar) +FEATF(_feat_is_player_altar, feat_is_player_altar) +FEATF(_feat_is_branch_stairs, feat_is_branch_stairs) +FEATF(_feat_is_critical, is_critical_feature) + +const struct luaL_reg feat_dlib[] = +{ +{ "destroys_items", _feat_destroys_items }, +{ "is_wall", _feat_is_wall }, +{ "is_solid", _feat_is_solid }, +{ "is_opaque", _feat_is_opaque }, +{ "is_closed_door", _feat_is_closed_door }, +{ "is_secret_door", _feat_is_secret_door }, +{ "is_statue_or_idol", _feat_is_statue_or_idol }, +{ "is_rock", _feat_is_rock }, +{ "is_permarock", _feat_is_permarock }, +{ "is_stone_stair", _feat_is_stone_stair }, +{ "is_staircase", _feat_is_staircase }, +{ "is_escape_hatch", _feat_is_escape_hatch }, +{ "is_trap", _feat_is_trap }, +{ "is_sealable_portal", _feat_is_sealable_portal }, +{ "is_portal", _feat_is_portal }, +{ "is_stair", _feat_is_stair }, +{ "is_travelable_stair", _feat_is_travelable_stair }, +{ "is_gate", _feat_is_gate }, +{ "is_water", _feat_is_water }, +{ "is_watery", _feat_is_watery }, +{ "is_altar", _feat_is_altar }, +{ "is_player_altar", _feat_is_player_altar }, +{ "is_branch_stairs", _feat_is_branch_stairs }, +{ "is_critical", _feat_is_critical }, + +{ NULL, NULL } +}; diff --git a/crawl-ref/source/l_libs.h b/crawl-ref/source/l_libs.h index a97eb7dc75..d487ea0bb8 100644 --- a/crawl-ref/source/l_libs.h +++ b/crawl-ref/source/l_libs.h @@ -49,6 +49,7 @@ extern const struct luaL_reg dgn_item_dlib[]; extern const struct luaL_reg dgn_level_dlib[]; extern const struct luaL_reg dgn_mons_dlib[]; extern const struct luaL_reg dgn_tile_dlib[]; +extern const struct luaL_reg feat_dlib[]; extern const struct luaL_reg los_dlib[]; extern const struct luaL_reg mapmarker_dlib[]; diff --git a/crawl-ref/source/makefile.obj b/crawl-ref/source/makefile.obj index 5fe2ea5020..82f1126892 100644 --- a/crawl-ref/source/makefile.obj +++ b/crawl-ref/source/makefile.obj @@ -68,6 +68,7 @@ l_dgnit.o \ l_dgnlvl.o \ l_dgnmon.o \ l_dgntil.o \ +l_feat.o \ l_file.o \ l_food.o \ l_global.o \ diff --git a/crawl-ref/source/test/findray.lua b/crawl-ref/source/test/findray.lua index 9f66461d4e..648b1cb9a7 100644 --- a/crawl-ref/source/test/findray.lua +++ b/crawl-ref/source/test/findray.lua @@ -50,7 +50,7 @@ local function test_findray() rx, ry = ray:pos() ray_p = dgn.point(rx, ry) while(ray_p ~= p) do - if dgn.is_opaque(rx, ry) then + if feat.is_opaque(rx, ry) then dgn.grid(x, y, "floor_special") debug.dump_map(FAILMAP) assert(false, -- cgit v1.2.3-54-g00ecf