From 83b1c7b2cc01d46d8f904b102681e85c044710a0 Mon Sep 17 00:00:00 2001 From: dshaligram Date: Tue, 25 Nov 2008 23:36:07 +0000 Subject: Place external loot chamber for ziggurat levels with jellies. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@7625 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/luadgn.cc | 117 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) (limited to 'crawl-ref/source/luadgn.cc') diff --git a/crawl-ref/source/luadgn.cc b/crawl-ref/source/luadgn.cc index 02a74f96be..3fd5b55e86 100644 --- a/crawl-ref/source/luadgn.cc +++ b/crawl-ref/source/luadgn.cc @@ -10,6 +10,7 @@ #include #include +#include #include #include "branch.h" @@ -726,6 +727,18 @@ static int dgn_map(lua_State *ls) return (0); } + // map(, x, y) = glyph at (x,y), subject to map being + // resolved and normalised. + if (lua_gettop(ls) == 3 && lua_isnumber(ls, 2) && lua_isnumber(ls, 3)) + { + const int gly = map->map.glyph(luaL_checkint(ls, 2), + luaL_checkint(ls, 3)); + char buf[2] = ""; + buf[0] = gly; + lua_pushstring(ls, buf); + return (1); + } + if (lua_isstring(ls, 2)) { map->map.add_line(luaL_checkstring(ls, 2)); @@ -940,6 +953,9 @@ static int dgn_grid(lua_State *ls) PLUARET(number, grd(c)); } +LUARET1(_dgn_is_wall, boolean, + grid_is_wall(static_cast(luaL_checkint(ls, 1)))) + static int dgn_max_bounds(lua_State *ls) { lua_pushnumber(ls, GXM); @@ -2159,6 +2175,103 @@ static int dgn_create_item(lua_State *ls) return (0); } +static std::auto_ptr _dgn_map_bad_bounds_fn; + +static bool _lua_map_place_invalid(const map_def &map, + const coord_def &c, + const coord_def &size) +{ +#ifdef DEBUG_DIAGNOSTICS + mprf(MSGCH_DIAGNOSTICS, "lua_map_place_invalid: (%d,%d) (%d,%d)", + c.x, c.y, size.x, size.y); +#endif + + lua_stack_cleaner clean(_dgn_map_bad_bounds_fn->lua); + + // Push the Lua function onto the stack. + _dgn_map_bad_bounds_fn->push(); + + lua_State *ls = _dgn_map_bad_bounds_fn->lua; + + // Push map, pos.x, pos.y, size.x, size.y + clua_push_map(ls, const_cast(&map)); + clua_push_coord(ls, c); + clua_push_coord(ls, size); + + const int err = lua_pcall(ls, 5, 1, 0); + + // Lua error invalidates place. + if (err) + { + mprf(MSGCH_ERROR, "Lua error: %s", lua_tostring(ls, -1)); + return (true); + } + + return (lua_toboolean(ls, -1)); +} + +LUAFN(dgn_with_map_bounds_fn) +{ + CLua &vm(CLua::get_vm(ls)); + if (lua_gettop(ls) != 2 || !lua_isfunction(ls, 1) || !lua_isfunction(ls, 2)) + luaL_error(ls, "Expected map-bounds check fn and action fn."); + + _dgn_map_bad_bounds_fn.reset(new lua_datum(vm, 1, false)); + + int err = 0; + { + unwind_var mpc(map_place_invalid, + _lua_map_place_invalid); + + // All set, call our friend, the second function. + ASSERT(lua_isfunction(ls, -1)); + + // Copy the function since pcall will pop it off. + lua_pushvalue(ls, -1); + + // Use pcall to catch the error here, else unwind_var won't + // happen when lua_call does its longjmp. + err = lua_pcall(ls, 0, 1, 0); + + _dgn_map_bad_bounds_fn.reset(NULL); + } + + if (err) + lua_error(ls); + + return (1); +} + +// Accepts any number of point coordinates and a function, binds the +// points as anchors that floating vaults must match and calls the +// function, returning the return value of the function. +LUAFN(dgn_with_map_anchors) +{ + const int top = lua_gettop(ls); + int err = 0; + { + unwind_var uanchor(map_anchor_points); + + map_anchor_points.clear(); + + int i; + for (i = 1; i < top; i += 2) + { + if (lua_isnumber(ls, i) && lua_isnumber(ls, i + 1)) + map_anchor_points.push_back( + coord_def( lua_tointeger(ls, i), + lua_tointeger(ls, i + 1) ) ); + } + + ASSERT(lua_isfunction(ls, -1)); + + lua_pushvalue(ls, -1); + err = lua_pcall(ls, 0, 1, 0); + } + if (err) + lua_error(ls); + return (1); +} #define BRANCH(br, pos) \ const char *branch_name = luaL_checkstring(ls, pos); \ @@ -2461,6 +2574,7 @@ static const struct luaL_reg dgn_lib[] = { "mapsize", dgn_map_size }, { "grid", dgn_grid }, + { "is_wall", _dgn_is_wall }, { "max_bounds", dgn_max_bounds }, { "colour_at", dgn_colour_at }, @@ -2524,6 +2638,9 @@ static const struct luaL_reg dgn_lib[] = { "create_monster", dgn_create_monster }, { "create_item", dgn_create_item }, + { "with_map_bounds_fn", dgn_with_map_bounds_fn }, + { "with_map_anchors", dgn_with_map_anchors }, + { "br_floorcol", dgn_br_floorcol }, { "br_rockcol", dgn_br_rockcol }, { "br_has_shops", dgn_br_has_shops }, -- cgit v1.2.3-54-g00ecf