New test cases can be added by adding appropriate maps to debug-los.des.
Also separate the LOS tests into three separate files.
XUPMPYQWJITYIE3G2CCYVQMHYBGGXS32EIQJG42GNZS6BLE4NFAAC endendendlocal function test_gridseegrid_symmetry()-- Clear messages to prevent them accumulating and forcing a --more--crawl.mesclr()-- Send the player to a random spot on the level.you.random_teleport()checks = checks + 1local you_x, you_y = you.pos()for y = -9, 9 dofor x = -9, 9 dolocal px, py = x + you_x, y + you_yif (x ~= 0 or y ~= 0) and dgn.in_bounds(px, py) thenlocal foreward = dgn.grid_see_grid(you_x, you_y, px, py)local backward = dgn.grid_see_grid(px, py, you_x, you_y)this_p = dgn.point(you_x, you_y)other_p = dgn.point(px, py)if not forward thenyou.moveto(px, py)local temp = this_pthis_p = other_pother_p = tempendif forward ~= backward thendgn.grid(other_p.x, other_p.y, "floor_special")dgn.dbg_dump_map(FAILMAP)assert(false,"grid_see_grid asymmetry detected (iter #" .. checks .. "): ".. this_p .. " sees " .. other_p .. ", but not vice versa.".. " Map saved to " .. FAILMAP)endend
-- check LOS according to pre-defined mapslocal checks = 0-- visible:local rock_wall = dgn.find_feature_number("rock_wall")local floor = dgn.find_feature_number("floor")-- invisible (not needed):local stone_wall = dgn.find_feature_number("stone_wall")local water = dgn.find_feature_number("deep_water")local function test_los_map(map)crawl.mesclr()-- choose random debug map; better choose alldgn.tags(map, "no_rotate no_vmirror no_hmirror no_pool_fixup")local name = dgn.name(map)-- local width, height = dgn.mapsize(map) -- returns (0,0)local function place_map()return dgn.place_map(map, true, true)enddgn.with_map_anchors(30, 30, place_map)you.moveto(30, 30)you.losight()crawl.redraw_view()for x = 0, 9 dofor y = 0, 9 dolocal xa = 30 + xlocal ya = 30 + ylocal p = dgn.point(x, y)local should_see = ((x == 0 and y == 0) ordgn.grid(xa, ya) == rock_wall ordgn.grid(xa, ya) == floor)local can_see = you.see_grid(xa, ya)if can_see and (not should_see) thenassert(false, "los error in " .. name .."(iter #" .. checks .."): can see " .. p .. " but shouldn't.")elseif (not can_see) and should_see thenassert(false, "los error in " .. name .. "(iter #" .. checks .."): should see " .. p .. " but can't.")endendendendlocal function test_los_maps()dgn.load_des_file("debug-los.des")local map = dgn.map_by_tag("debug_los")while map dotest_los_map(map)checks = checks + 1map = dgn.map_by_tag("debug_los")endendtest_los_maps()
-- Vet LOS for symmetry.local FAILMAP = 'losfail.map'local checks = 0local function test_gridseegrid_symmetry()-- Clear messages to prevent them accumulating and forcing a --more--crawl.mesclr()-- Send the player to a random spot on the level.you.random_teleport()checks = checks + 1local you_x, you_y = you.pos()for y = -9, 9 dofor x = -9, 9 dolocal px, py = x + you_x, y + you_yif (x ~= 0 or y ~= 0) and dgn.in_bounds(px, py) thenlocal foreward = dgn.grid_see_grid(you_x, you_y, px, py)local backward = dgn.grid_see_grid(px, py, you_x, you_y)this_p = dgn.point(you_x, you_y)other_p = dgn.point(px, py)if not forward thenyou.moveto(px, py)local temp = this_pthis_p = other_pother_p = tempendif forward ~= backward thendgn.grid(other_p.x, other_p.y, "floor_special")dgn.dbg_dump_map(FAILMAP)assert(false,"grid_see_grid asymmetry detected (iter #" .. checks .. "): ".. this_p .. " sees " .. other_p .. ", but not vice versa.".. " Map saved to " .. FAILMAP)endendendendendlocal function run_los_tests(depth, nlevels, tests_per_level)local place = "D:" .. depthcrawl.mpr("Running LOS tests on " .. place)dgn.dbg_goto_place(place)for lev_i = 1, nlevels dodgn.dbg_flush_map_memory()dgn.dbg_generate_level()for t_i = 1, tests_per_level dotest_gridseegrid_symmetry()endendendfor depth = 1, 27 dorun_los_tests(depth, 1, 10)end
# Sample maps illustrating what LOS should be like.# test/los_maps.lua tests losight() against these.## For the moment, these need to be 10x10 with an @# in the nw corner and show one quadrant, with# visibility encoded as follows.## x: visible wall# c: non-visible wall# .: visible floor# w: non-visible floor (water actually; could just place c's there)# @: centerNAME: los_emptyORIENT: floatTAGS: debug_losMAP@........c.........c........cc........cc........cc.......ccc......cccc.....ccccc..ccccccccccccccccccENDMAPNAME: los_edgeORIENT: floatTAGS: debug_losMAP@.......xc.......xxc.......xcc.......xcc......xxcc.....xxccc....xxcccc.xxxxcccccxxccccccccccccccccccENDMAPNAME: los_blocking_wallORIENT: floatTAGS: debug_losMAP@..xwwwwwc..xxwwwwwc.xxwwwwwccxxwwwwwwccwwwwwwwwccwwwwwwwcccwwwwwwccccwwwwwcccccwwccccccccccccccccccENDMAPNAME: los_cornerORIENT: floatTAGS: debug_losMAP@xccccccccx........ccxxxxxxxccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccENDMAPNAME: los_corner_2ORIENT: floatTAGS: debug_losMAP@.xcccccccxx.......ccccxxxxxccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccENDMAPNAME: los_corner_3ORIENT: floatTAGS: debug_losMAP@..xccccccxxx......ccccccxxxccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccENDMAPNAME: los_corner_4ORIENT: floatTAGS: debug_losMAP@...xcccccxxxx.....ccccccccxccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccENDMAPNAME: los_corner_5ORIENT: floatTAGS: debug_losMAP@....xccccxxxxx....cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccENDMAPNAME: los_corner_6ORIENT: floatTAGS: debug_losMAP@.....xcccxxxxxx...cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccENDMAPNAME: los_corner_7ORIENT: floatTAGS: debug_losMAP@......xccxxxxxxx..cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccENDMAPNAME: los_corner_8ORIENT: floatTAGS: debug_losMAP@.......xcxxxxxxxx.cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccENDMAPNAME: los_corner_9ORIENT: floatTAGS: debug_losMAP@........cxxxxxxxxxwccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccENDMAPNAME: los_gapORIENT: floatTAGS: debug_losMAP@.xcccccccx.xcccccccc..cccccccc...ccccccc....cccccc.....ccccc.....ccccc....cccccc.ccccccccccccccccccENDMAPNAME: los_gap2ORIENT: floatTAGS: debug_losMAP@.xcccccccx.xcccccccc.xcccccccc..cccccccc..cccccccc...ccccccc...ccccccc....cccccc.ccccccccccccccccccENDMAP