From acabbaacbc91db8c6b711b85e1f68694b42ed829 Mon Sep 17 00:00:00 2001 From: Jude Brown Date: Tue, 12 Jan 2010 12:09:46 +1000 Subject: Some new Lua wrappers for dungeon building and ranges. Introduces util.range(start, stop), returning an array consisting of the numeric values between start and stop inclusive. Also introduces "is_valid_coord" Lua wrapper, which does the same work as _valid_coord but does not generate an error message for invalid coordinates, "find_in_area" (a straight translation of the function in dungeon.cc), and "is_passable_coord" which checks the glyph at that location against traversable_glyphs. --- crawl-ref/source/dat/clua/util.lua | 10 +++++- crawl-ref/source/l_dgnbld.cc | 73 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/crawl-ref/source/dat/clua/util.lua b/crawl-ref/source/dat/clua/util.lua index 8dd0b35eba..ffc8e8c3ae 100644 --- a/crawl-ref/source/dat/clua/util.lua +++ b/crawl-ref/source/dat/clua/util.lua @@ -242,6 +242,14 @@ function util.expand_entity(entity, msg) end) end +function util.range(start, stop) + local rt + for i = start, stop do + table.insert(rt, i) + end + return rt +end + ---------------------------------------------------------- util.Timer = { CLASS = "Timer" } @@ -337,4 +345,4 @@ function util.namespace(table_name) if _G[table_name] == nil then _G[table_name] = { } end -end \ No newline at end of file +end diff --git a/crawl-ref/source/l_dgnbld.cc b/crawl-ref/source/l_dgnbld.cc index 354ba4dad7..218facc9bf 100644 --- a/crawl-ref/source/l_dgnbld.cc +++ b/crawl-ref/source/l_dgnbld.cc @@ -190,6 +190,29 @@ LUAFN(dgn_count_neighbors) PLUARET(number, lines.count_feature_in_box(tl, br, feat)); } +LUAFN(dgn_is_valid_coord) +{ + LINES(ls, 1, lines); + + TABLE_INT(ls, x, -1); + TABLE_INT(ls, y, -1); + + if (x < 0 || x >= lines.width()) + { + lua_pushboolean(ls, false); + return (1); + } + + if (y < 0 || y >= lines.height()) + { + lua_pushboolean(ls, false); + return (1); + } + + lua_pushboolean(ls, true); + return (1); +} + LUAFN(dgn_extend_map) { LINES(ls, 1, lines); @@ -263,6 +286,53 @@ LUAFN(dgn_fill_disconnected) return (0); } +LUAFN(dgn_is_passable_coord) +{ + LINES(ls, 1, lines); + + TABLE_INT(ls, x, -1); + TABLE_INT(ls, y, -1); + TABLE_STR(ls, passable, traversable_glyphs); + + if (!_valid_coord(ls, lines, x, y)) + return (0); + + if (strchr(passable, lines(x, y))) + lua_pushboolean(ls, true); + else + lua_pushboolean(ls, false); + + return (1); +} + +LUAFN(dgn_find_in_area) +{ + LINES(ls, 1, lines); + + TABLE_INT(ls, x1, -1); + TABLE_INT(ls, y1, -1); + TABLE_INT(ls, x2, -1); + TABLE_INT(ls, y2, -1); + + if (!_coords(ls, lines, x1, y1, x2, y2)) + return (0); + + TABLE_CHAR(ls, find, 'x'); + + int x, y; + + for (x = x1; x <= x2; x++) + for (y = y1; y <= y2; y++) + if (lines(x, y) == find) + { + lua_pushboolean(ls, true); + return (1); + } + + lua_pushboolean(ls, false); + return (1); +} + LUAFN(dgn_height) { LINES(ls, 1, lines); @@ -690,9 +760,12 @@ const struct luaL_reg dgn_build_dlib[] = { "count_feature_in_box", &dgn_count_feature_in_box }, { "count_antifeature_in_box", &dgn_count_antifeature_in_box }, { "count_neighbors", &dgn_count_neighbors }, + { "is_valid_coord", &dgn_is_valid_coord }, + { "is_passable_coord", &dgn_is_passable_coord }, { "extend_map", &dgn_extend_map }, { "fill_area", &dgn_fill_area }, { "fill_disconnected", &dgn_fill_disconnected }, + { "find_in_area", &dgn_find_in_area }, { "height", dgn_height }, { "join_the_dots", &dgn_join_the_dots }, { "make_circle", &dgn_make_circle }, -- cgit v1.2.3-54-g00ecf