1: arg = {...}
2: if arg[1] then
3: level = arg[1]
4: run = true
5: else
6: print("Usage: a levelfile")
7: run = false
8: end
9:
10: if fs.exists(level) == false then
11: run = false
12: print("not a file")
13: end
14:
15: -- b = block s = start e = end w = wall
16: --read from file and print to screen
17:
18: --read to find start and end pos
19: if run then
20: movecost = 10
21: watermovecost = 20
22: Li = 0
23: file = fs.open(level,"r")
24: endboolean = false
25: startboolean = false
26: go = false
27: openlist = {}
28: closedlist = {}
29: w,h = term.getSize()
30: while go == false do
31: str = file.readLine()
32: --print(endboolean)
33: --print(startboolean)
34: --print(str)
35: s,e = str:find("E")
36: Li = Li + 1
37: if s ~= null then
38: endboolean = true
39: endpoint = {s,Li}
40: --print(s)
41: --print(Li)
42: --print("end = true")
43: end
44: s2,e2 = str:find("S")
45: if s2 ~= nul then
46: startboolean = true
47: startpoint = {s2,Li}
48: --print(s2)
49: --print(Li)
50: --print("start = true")
51: end
52: if startboolean and endboolean then
53: go = true
54: file.close()
55: end
56: end
57: --print(startpoint[1])
58: --print(startpoint[2])
59: --print(endpoint[1])
60: --print(endpoint[2])
61: function gettype(str)
62: if str == "O" then
63: return "water"
64: elseif str == " " then
65: return "air"
66: elseif str == "W" then
67: return "wall"
68: end
69: end
70: if go then
71: -- finding out the data for each cell
72: -- and then getting the data
73: sx = tonumber(startpoint[1])
74: sy = tonumber(startpoint[2])
75: ex = tonumber(endpoint[1])
76: ey = tonumber(endpoint[2])
77: filel = fs.open(level,"r")
78: x = 1
79: y = 1
80: cell = {}
81: for y = 1,w do -- for each cell do
82: cell[y] = {}
83: stringl = filel.readLine()
84: for x = 1,h do
85: cell[x][y] = {}
86: --calculate heuristic
87: dx = math.abs(ex - x)
88: dy = math.abs(ey - y)
89: h = dx + dy
90: cell[x][y].h = h-1
91: ce = stringl:sub(y,y)
92: cell[x][y].type = gettype(ce)
93: end
94: end
95: filel.close()
96: --[[ starting the search ]]--
97: --adding the start to the open list
98: -- open list will store cells like:
99: -- referencing will be like openlist[1] = {1,2)
100: -- x = openlist[1][1]
101: -- y = openlist[1][2]
102: function lowest(listing)
103: for finding,lists in pairs(listing) do
104: --lists will be like {x,y}
105: local cx = lists[1]
106: local cy = lists[2]
107: if checklist(cx,cy,closed) == false then
108: if cell[cx][cy].f < best then
109: best = cell[cx][cy].f
110: bestcell = {cx,cy}
111: end
112: end
113: end
114: end
115: function find(xx,yy)
116: if cell[xx-1][yy].type ~= "wall" then
117: openlist[table.maxn(openlist)+1] = {xx-1,yy-1}
118: cell[xx-1][yy].parent = {xx,yy}
119: cell[xx-1][yy].g = cell[xx][yy].g + movecost
120: cell[xx-1][yy].f = cell.cell[xx-1][yy].h + cell[xx-1][yy].g
121: end
122: if cell[xx+1][yy].type ~= "wall" then
123: openlist[table.maxn(openlist)+1] = {xx-1,yy-1}
124: cell[xx+1][yy].parent = {xx,yy}
125: cell[xx+1][yy].g = cell[xx][yy].g + movecost
126: cell[xx+1][yy].f = cell.cell[xx+1][yy].h + cell[xx+1][yy].g
127: end
128: if cell[xx][yy+1].type ~= "wall" then
129: openlist[table.maxn(openlist)+1] = {xx-1,yy-1}
130: cell[xx][yy-1].parent = {xx,yy}
131: cell[xx][yy-1].g = cell[xx][yy].g + movecost
132: cell[xx][yy-1].f = cell.cell[xx][yy-1].h + cell[xx][yy-1].g
133: end
134: if cell[xx][yy-1].type ~= "wall" then
135: openlist[table.maxn(openlist)+1] = {xx-1,yy-1}
136: cell[xx][yy+1].parent = {xx,yy}
137: cell[xx][yy+1].g = cell[xx][yy].g + movecost
138: cell[xx][yy+1].f = cell.cell[xx][yy+1].h + cell[xx][yy+1].g
139: end
140: end
141: function toclosed(xx,yy)
142: local n = 0
143: while found == false do
144: n = n + 1
145: if openlist[n].x == xx then
146: if openlist[n].y == yy then
147: table.remove(openlist,n)
148: closedlist[table.maxn(closedlist)+1] = {xx,yy}
149: end
150: end
151: end
152: end
153: function checklist(x,y,list)
154: for j,k in pairs(list) do
155: if k[1] == x then
156: if k[2] == y then
157: return true
158: else
159: return false
160: end
161: else
162: return false
163: end
164: end
165: end
166: -- finding starts here
167: openlist[1] = {sx,sy}
168: cell[sx][sy].g = 0
169: find(sx,sy)
170: toclosed(sx,sy)
171: -- find lowest f value on the open list to choose for checking
172: best = 999999999999999999999999999999999999999
173: lowest(openlist)
174: -- drop the bestcell from the openlist and add it to the closed list
175: toclosed(bestcell[1],bestcell[2])
176: --Check all of the adjacent squares. Ignoring those that are on the closed list or unwalkable (terrain with walls, water, or other illegal
177: --terrain), add squares to the open list if they are not on the open list already. Make the selected square the “parent” of the new squares.
178: find(bestcell[1],bestcell[2])
179: --If an adjacent square is already on the open list, check to see if this path to that square is a better one. In other words, check to see
180: --if the G score for that square is lower if we use the current square to get there. If not, don’t do anything.
181: --On the other hand, if the G cost of the new path is lower, change the parent of the adjacent square to the selected square (in the diagram
182: --above, change the direction of the pointer to point at the selected square). Finally, recalculate both the F and G scores of that square.
183: --If this seems confusing, you will see it illustrated below.
184: end
185: end