Tests for the existence of unobstructed rays to points within LOS.
4NRM3KWVHQ46WAIF2OF33JUDVTR67ADIDXCOV2AB2R3QVUMWAG5AC -- Check basic find_ray functionality.local FAILMAP = 'losfail.map'local checks = 0local function test_losight_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()-- Forcibly redo LOS.you.losight()checks = checks + 1local you_x, you_y = you.pos()local you_p = dgn.point(you_x, you_y)local visible_spots = { }for y = -8, 8 dofor x = -8, 8 doif x ~= 0 or y ~= 0 thenlocal px, py = x + you_x, y + you_yif you.see_grid(px, py) thentable.insert(visible_spots, { px, py })endendendend-- For each position in LOS, shoot a ray there-- and make sure it doesn't go through an opaque cell.for _, spot in ipairs(visible_spots) dolocal x, y = unpack(spot)local p = dgn.point(x, y)local ray = los.findray(you_x, you_y, x, y)if not ray thendgn.grid(x, y, "floor_special")dgn.dbg_dump_map(FAILMAP)assert(false, "Can't find ray to " .. p .." although it's in view.")endlocal rx, ry = ray:pos()local ray_p = dgn.point(rx, ry)assert(ray_p == you_p,"Ray doesn't start at player position " .. you_p .." but " .. ray_p .. ".")ray:advance()rx, ry = ray:pos()ray_p = dgn.point(rx, ry)while(ray_p ~= p) doif dgn.is_opaque(rx, ry) thendgn.grid(x, y, "floor_special")dgn.dbg_dump_map(FAILMAP)assert(false,"Ray from " .. you_p .. " to " .. p .." passes through opaque cell " .. ray_p.. ".")endray:advance()rx, ry = ray:pos()ray_p = dgn.point(rx, ry)endendendlocal 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_losight_symmetry()endendendfor depth = 1, 27 dorun_los_tests(depth, 1, 10)end