From 9841ce45ea91c1ad394d706fd748ffc1fd9d9de0 Mon Sep 17 00:00:00 2001 From: Darshan Shaligram Date: Sat, 9 Jan 2010 01:21:44 +0530 Subject: Add -script option to Crawl to run a Lua script. Scripts are similar to tests, but can be parameterised. Add a script to generate 150 level at a named place and report on all the monsters generated there. --- crawl-ref/source/dat/clua/test.lua | 45 ++++++++++++++++++++++++++++++++++++++ crawl-ref/source/dat/clua/util.lua | 14 ++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) (limited to 'crawl-ref/source/dat') diff --git a/crawl-ref/source/dat/clua/test.lua b/crawl-ref/source/dat/clua/test.lua index 22a4005d20..03a23842e5 100644 --- a/crawl-ref/source/dat/clua/test.lua +++ b/crawl-ref/source/dat/clua/test.lua @@ -31,4 +31,49 @@ function test.level_contains_item(item) end end return false +end + +function test.level_monster_iterator(filter) + return iter.mons_rect_iterator(dgn.point(1, 1), + dgn.point(dgn.GXM - 2, dgn.GYM - 2), + filter) +end + +test.is_down_stair = dgn.feature_set_fn("stone_stairs_down_i", + "stone_stairs_down_ii", + "stone_stairs_down_iii") + +function test.level_has_down_stair() + for y = 1, dgn.GYM - 2 do + for x = 1, dgn.GXM - 2 do + local dfeat = dgn.grid(x, y) + if test.is_down_stair(dfeat) then + return true + end + end + end + return false +end + +function test.deeper_place_from(place) + if test.level_has_down_stair() then + local _, _, branch, depth = string.find(place, "(%w+):(%d+)") + return branch .. ":" .. (tonumber(depth) + 1) + end + return nil +end + +util.namespace('script') + +function script.simple_args() + local args = crawl.script_args() + return util.filter(function (arg) + return string.find(arg, '-') ~= 1 + end, + args) +end + +function script.usage(ustr) + ustr = string.gsub(string.gsub(ustr, "^%s+", ""), "%s+$", "") + error("\n" .. ustr) end \ No newline at end of file diff --git a/crawl-ref/source/dat/clua/util.lua b/crawl-ref/source/dat/clua/util.lua index d6e842fa7e..8dd0b35eba 100644 --- a/crawl-ref/source/dat/clua/util.lua +++ b/crawl-ref/source/dat/clua/util.lua @@ -56,7 +56,7 @@ end -- Returns a list of the keys in the given map. function util.keys(map) local keys = { } - for key, _ in pairs(ziggurat_builder_map) do + for key, _ in pairs(map) do table.insert(keys, key) end return keys @@ -65,12 +65,22 @@ end -- Returns a list of the values in the given map. function util.values(map) local values = { } - for _, value in pairs(ziggurat_builder_map) do + for _, value in pairs(map) do table.insert(values, value) end return values end +-- Returns a list of lists built from the given map, each sublist being +-- in the form { key, value } for each key-value pair in the map. +function util.pairs(map) + local mappairs = { } + for key, value in pairs(map) do + table.insert(mappairs, { key, value }) + end + return mappairs +end + -- Creates a string of the elements in list joined by separator. function util.join(sep, list) return table.concat(list, sep) -- cgit v1.2.3-54-g00ecf