This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
columna1's profile picture

[Lua][Error] I need help finding out the error im getting for this a* demo

Started by columna1, 13 January 2013 - 07:14 PM
columna1 #1
Posted 13 January 2013 - 08:14 PM
Ok first of all i know my code is sloppy (very sloppy) and am apologising now for it but this is just a proof of concept
This program is NO WHERE NEAR finished and will not do anything if it is in good shape as it is
i know there are other methods i can do to make this but that is not what im focusing on so please if you can just help me here

also I know that there are probably many errors in this code some of which are just plane obvious but that is mostly me
being an idiot and forgetting syntax but anyways here is my problem

I am making a demo and I have run into a problem…
I have tested this in another program so i know that is should work no problems but it wont work for me here

The error I am getting is this 85: index expected, got nil
I have scanned the code many times and i am aggravated at the fact it is doing this and here is my code

Spoiler
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


Anyways if you could tell me what im doing wrong I would appreciate it sooooooooo much and be ultimately grateful
thank you
RunasSudo-AWOLindefinitely #2
Posted 13 January 2013 - 08:17 PM
At a glance, it sounds like either, cell, x, y, or cell[x] is nil. Are you sure all of your for loops and everything are correct?
columna1 #3
Posted 13 January 2013 - 08:21 PM
Im pretty sure that the way im defining it in the for loop:


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

would define cell[x] first then it would define
cell[x][y] as a table
so that i could use it almost as an object with a 2d identifier

Also if you want i could post the code without the lines numbered
and i have defined cell to as a table
crazyguymgd #4
Posted 13 January 2013 - 08:53 PM
Im pretty sure that the way im defining it in the for loop:


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

would define cell[x] first then it would define
cell[x][y] as a table
so that i could use it almost as an object with a 2d identifier

Also if you want i could post the code without the lines numbered
and i have defined cell to as a table

cell[y] = {}, when y is 1, puts an empty table at cell[1].
then you go into the x loop.
when x = 1, cell[1][1] = {} works fine because you created that empty table at cell[1].
But the next time through the for loop, you try to access cell[2][1] which doesn't exist…
columna1 #5
Posted 13 January 2013 - 09:23 PM
well derp i feel stupid now XD
hmm
now all i need to do is figure out a way to get around that…
crazyguymgd #6
Posted 13 January 2013 - 09:32 PM
you should be able to just switch the for loops. for x = 1, w …. for y = 1, h…
columna1 #7
Posted 13 January 2013 - 10:47 PM
but then x and y in cells ends up backwards so do i have to reverse everything that calls it or can i reverse that still somehow?
crazyguymgd #8
Posted 13 January 2013 - 10:49 PM
as far as i can tell, reverse everything that calls it.
columna1 #9
Posted 13 January 2013 - 11:02 PM
That makes it confusing as hell is there any way to reverse cell so that it is not backwards?
crazyguymgd #10
Posted 13 January 2013 - 11:07 PM
wait i think I was confused to your last question there. What I was saying originally was to change it to:


[color=#006666]81[/color][color=#666600]:[/color][color=#000000]	 [/color][color=#000088]for[/color][color=#000000] x [/color][color=#666600]=[/color][color=#000000] [/color][color=#006666]1[/color][color=#666600],h[/color][color=#000000] [/color][color=#000088]do[/color][color=#000000] [/color][color=#666600]--[/color][color=#000000] [/color][color=#000088]for[/color][color=#000000] each cell [/color][color=#000088]do[/color]
[color=#006666]82[/color][color=#666600]:[/color][color=#000000]		cell[/color][color=#666600][[/color][color=#000000]x[/color][color=#666600]][/color][color=#000000] [/color][color=#666600]=[/color][color=#000000] [/color][color=#666600]{}[/color]
[color=#006666]83[/color][color=#666600]:[/color][color=#000000]		stringl [/color][color=#666600]=[/color][color=#000000] filel[/color][color=#666600].[/color][color=#000000]readLine[/color][color=#666600]()[/color]
[color=#006666]84[/color][color=#666600]:[/color][color=#000000]		[/color][color=#000088]for[/color][color=#000000] y [/color][color=#666600]=[/color][color=#000000] [/color][color=#006666]1[/color][color=#666600],w[/color][color=#000000] [/color][color=#000088]do[/color]
[color=#006666]85[/color][color=#666600]:[/color][color=#000000]			  cell[/color][color=#666600][[/color][color=#000000]x[/color][color=#666600]][[/color][color=#000000]y[/color][color=#666600]][/color][color=#000000] [/color][color=#666600]=[/color][color=#000000] [/color][color=#666600]{}[/color]
[color=#006666]86[/color][color=#666600]:[/color][color=#000000]			  [/color][color=#666600]--[/color][color=#000000]calculate heuristic[/color]
[color=#006666]87[/color][color=#666600]:[/color][color=#000000]			  dx [/color][color=#666600]=[/color][color=#000000] math[/color][color=#666600].[/color][color=#000000]abs[/color][color=#666600]([/color][color=#000000]ex [/color][color=#666600]-[/color][color=#000000] x[/color][color=#666600])[/color]
[color=#006666]88[/color][color=#666600]:[/color][color=#000000]			  dy [/color][color=#666600]=[/color][color=#000000] math[/color][color=#666600].[/color][color=#000000]abs[/color][color=#666600]([/color][color=#000000]ey [/color][color=#666600]-[/color][color=#000000] y[/color][color=#666600])[/color]
[color=#006666]89[/color][color=#666600]:[/color][color=#000000]			  h [/color][color=#666600]=[/color][color=#000000] dx [/color][color=#666600]+[/color][color=#000000] dy[/color]
[color=#006666]90[/color][color=#666600]:[/color][color=#000000]			  cell[/color][color=#666600][[/color][color=#000000]x[/color][color=#666600]][[/color][color=#000000]y[/color][color=#666600]].[/color][color=#000000]h [/color][color=#666600]=[/color][color=#000000] h[/color][color=#666600]-[/color][color=#006666]1[/color]
[color=#006666]91[/color][color=#666600]:[/color][color=#000000]			  ce [/color][color=#666600]=[/color][color=#000000] stringl[/color][color=#666600]:[/color][color=#000088]sub[/color][color=#666600]([/color][color=#000000]y[/color][color=#666600],[/color][color=#000000]y[/color][color=#666600])[/color]
[color=#006666]92[/color][color=#666600]:[/color][color=#000000]			  cell[/color][color=#666600][[/color][color=#000000]x[/color][color=#666600]][[/color][color=#000000]y[/color][color=#666600]].[/color][color=#000000]type [/color][color=#666600]=[/color][color=#000000] gettype[/color][color=#666600]([/color][color=#000000]ce[/color][color=#666600])[/color]
[color=#006666]93[/color][color=#666600]:[/color][color=#000000]		[/color][color=#000088]end[/color]
[color=#006666]94[/color][color=#666600]:[/color][color=#000000]	  [/color][color=#000088]end[/color]
[color=#000088][/code[/color][color=#000088]][/color]
[color=#000088]edit - can't figure out formating with this copied and pasted. sorry all you formatting gods :P/>[/color]
columna1 #11
Posted 13 January 2013 - 11:24 PM
thats what i did
that still makes it umm
sideways i think
anyways its still backward and it needs to be reversed
im reading from a file and adding a cell for each "pixel" if you will
x has to be the width and y has to be the height otherwise it wont work
in this case you are making x the height and y the width
which is a problem because you can only read lines with the file api
not colums like this

sssssssss
wwwwwww
sssssssss
wwwwwww
sssssssss
if you read it that way then it will end up:
cell[1][2] = S
instead of W like i want it to be
as in first char on the second line
you get the second char on the first line
crazyguymgd #12
Posted 13 January 2013 - 11:36 PM
oh, your original loops had y associated with the width and x with the height… i thought that was strange but just kept that for you.
basically if it doesn't do what you want it to do, you'll probably have to re-write it to properly fit the structure you want…