summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/clua.cc
diff options
context:
space:
mode:
authorEnne Walker <ennewalker@users.sourceforge.net>2009-10-18 16:32:37 -0400
committerEnne Walker <ennewalker@users.sourceforge.net>2009-10-18 16:52:02 -0400
commitdf0b2a5165b84d52d9c43d76d14adea095b671c8 (patch)
tree6e1587fae77a2c020b8c7e5498e5eb8caab88cdf /crawl-ref/source/clua.cc
parent52fce0a7785eccba94e59c7ec8375019780eddcc (diff)
downloadcrawl-ref-df0b2a5165b84d52d9c43d76d14adea095b671c8.tar.gz
crawl-ref-df0b2a5165b84d52d9c43d76d14adea095b671c8.zip
Lua map improvements.
The previous dungeon layout.des functions all apply directly to the grid and so are unsuitable for use in vaults. This aims to correct that by providing lua functions that can manipulate maps (of glyphs). grd[x][y] in a .des file can now be used to get and set glyphs in the current map. This should allow for less cumbersome map variations than what you can do with just SUBST and SHUFFLE. To support that, map_def no longer batches up transforms--it applies them all immediately. This resulted in a good bit of refactoring. FTILE/RTILE map commands now support setting the tile for multiple features at once. There are also a small number of new lua functions that apply to maps (map_octa_room, map_smear, and map_extend). Ideally, these will eventually replace the existing builder funcs that work on grd.
Diffstat (limited to 'crawl-ref/source/clua.cc')
-rw-r--r--crawl-ref/source/clua.cc105
1 files changed, 105 insertions, 0 deletions
diff --git a/crawl-ref/source/clua.cc b/crawl-ref/source/clua.cc
index 9743019e9d..44f4cd0393 100644
--- a/crawl-ref/source/clua.cc
+++ b/crawl-ref/source/clua.cc
@@ -597,6 +597,7 @@ void luaopen_crawl(lua_State *ls);
void luaopen_file(lua_State *ls);
void luaopen_options(lua_State *ls);
void luaopen_monsters(lua_State *ls);
+void luaopen_grd(lua_State *ls);
void luaopen_globals(lua_State *ls);
void CLua::init_lua()
@@ -626,6 +627,7 @@ void CLua::init_lua()
luaopen_file(_state);
luaopen_options(_state);
luaopen_monsters(_state);
+ luaopen_grd(_state);
luaopen_globals(_state);
@@ -2788,6 +2790,109 @@ void luaopen_monsters(lua_State *ls)
luaL_openlib(ls, "mons", mons_lib, 0);
}
+/////////////////////////////////////////////////////////////////////
+// grd and grd_col handling (i.e. map_lines in a metatable)
+
+struct mapcolumn
+{
+ map_def* map;
+ int col;
+};
+
+static int grd_get(lua_State *ls)
+{
+ // Return a metatable for this column in the map grid.
+ map_def *map = *(map_def **) luaL_checkudata(ls, 1, GRD_METATABLE);
+
+ int column = luaL_checkint(ls, 2);
+
+ mapcolumn *mapref = clua_new_userdata<mapcolumn>(ls, GRD_COL_METATABLE);
+ mapref->map = map;
+ mapref->col = column;
+
+ return (1);
+}
+
+static int grd_set(lua_State *ls)
+{
+ return (luaL_error(ls, "%s", "Cannot assign to read-only table."));
+}
+
+static char* grd_glyph(lua_State *ls, int &col, int &row)
+{
+ mapcolumn *mapc = (mapcolumn *)luaL_checkudata(ls, 1, GRD_COL_METATABLE);
+ row = luaL_checkint(ls, 2);
+ col = mapc->col;
+
+ map_lines &lines = mapc->map->map;
+ if (row < 1 || col < 1 || col > lines.width() || row > lines.height())
+ {
+ return (NULL);
+ }
+
+ coord_def mc(col - 1, row - 1);
+ return (&lines(mc));
+}
+
+static int grd_col_get(lua_State *ls)
+{
+ int col, row;
+ char *gly = grd_glyph(ls, col, row);
+ if (!gly)
+ return (luaL_error(ls, "Invalid coords: %d, %d", col, row));
+
+ char buf[2];
+ buf[0] = *gly;
+ buf[1] = '\0';
+
+ lua_pushstring(ls, buf);
+
+ return (1);
+}
+
+static int grd_col_set(lua_State *ls)
+{
+ int col, row;
+ char *gly = grd_glyph(ls, col, row);
+ if (!gly)
+ return (luaL_error(ls, "Invalid coords: %d, %d", col, row));
+
+ const char *str = luaL_checkstring(ls, 3);
+ if (!str[0] || str[1])
+ return (luaL_error(ls, "%s", "grd must be set to a single char."));
+
+ (*gly) = str[0];
+
+ return (0);
+}
+
+void luaopen_grd(lua_State *ls)
+{
+ // grd table
+ luaL_newmetatable(ls, GRD_METATABLE);
+ lua_pushstring(ls, "__index");
+ lua_pushcfunction(ls, grd_get);
+ lua_settable(ls, -3);
+
+ lua_pushstring(ls, "__newindex");
+ lua_pushcfunction(ls, grd_set);
+ lua_settable(ls, -3);
+
+ lua_pop(ls, 1);
+
+ // grd col table
+ luaL_newmetatable(ls, GRD_COL_METATABLE);
+ lua_pushstring(ls, "__index");
+ lua_pushcfunction(ls, grd_col_get);
+ lua_settable(ls, -3);
+
+ lua_pushstring(ls, "__newindex");
+ lua_pushcfunction(ls, grd_col_set);
+ lua_settable(ls, -3);
+
+ lua_pop(ls, 1);
+}
+
//////////////////////////////////////////////////////////////////////
// Miscellaneous globals