maze - Finding nearest unvisited exit by using a table of coordinates and cardinal exits in Lua -
i trying code lua script mud have pgrogrammed set of scripts. mud has begun sending out room coordinates (, y , height) along available exits (north east south). code script keep track of visited exits, , find closest unvisited exit room in: tried consulting stackoverflow , many other sites on google found no answer far, tried creating table of coordinates keep track of coordinates strings of coordinates put spaces , inside keep record of exits leaving each room, set table called backtrack keep track of movements done character , coordinates went to, find closest exit. did not solve problem, player moved different locations , loop built find closest exits became stuck or didn't come useful results.
does have experience or has done similar? clueless how can handle this. thanks.
"the closest unvisited exit" means "the closest room unvisted exit".
with room coordinates can build 2d map. (x,y) tile can of 1 of 3 types:
type 2: visited , exits visited (represented #)
type 1: visited unvisited exits (represented x)
type 0: unvisited (represented space)
for instance:
12345678 1 2 #x## 3 ###x 4 # 5 ## 6 x### 7 ### 8 x
finding nearest unvisited exit(s) simple breadth-first search problem starting position in map. let's represent cell of map this:
{ x = 2, y = 2, type = 1, exits = { east = cell_east, west = cell_west, } }
you write list of nearest rooms @ least 1 unvisited exit:
local visited = {} local visit = function(to_visit) if #to_visit == 0 return nil end local next,found = {},{} i=1,#to_visit visited[#visited+1] = to_visit[i] end _,cell in ipairs(to_visit) if cell.type == 1 found[#found+1] = cell elseif cell.type == 2 _,exit in pairs(cell.exits) if exit.type > 0 next[#next+1] = exit end end else error("something went wrong") end end if #found > 0 return found else return visit(next) end end my_list = visit({current_cell})
this untested , not elegant or efficient way solve problem should give idea :)
Comments
Post a Comment