-- Advanced Ore Finder by Henness
-- Version 2.0 12/16/2012
-- Config
Version = "v2.0"
-- Functions
function version()
return Version
end
function skip()
local id, key = os.pullEvent("key")
end
function saveTable(table,name)
local file = fs.open(name,"w")
file.write(textutils.serialize(table))
file.close()
end
function loadTable(name)
local file = fs.open(name,"r")
local data = file.readAll()
file.close()
return textutils.unserialize(data)
end
function save()
ofsave = {
["currentPos"] = {
["x"] = x, ["y"] = y, ["z"] = y, ["face"] = face
},
["W"] = width,
["L"] = length,
["w"] = w,
["l"] = l
}
saveTable(ofsave, "ofsave")
end
function turnLeft(a)
if not a then
a = 1
end
for i=1,math.abs(a),1 do
turtle.turnLeft()
if face==0 then
face=3
else
face=face-1
end
save()
end
return true
end
function turnRight(a)
if not a then
a = 1
end
for i=1,math.abs(a),1 do
turtle.turnRight()
if face==3 then
face=0
else
face=face+1
end
save()
end
return true
end
function face(a)
local turn = face-a
local done = false
if face == a then
done = true
elseif turn == -1 or turn == 3 then
turnRight()
done = true
elseif turn == 1 or turn == -3 then
turnLeft()
done = true
elseif math.abs(turn) == 2 then
turnRight(2)
done = true
end
return done
end
function up(n) -- Moves the turtle up "n" distance
local moved
if not n then
n=1
end
if y<255 then
for k=1,n,1 do
moved = turtle.up()
if moved then
y=y+1
save()
else
break
end
end
end
return moved
end
function down(n) -- Moves the turtle down "n" distance
local moved
if not n then
n=1
end
if y>1 then
for k=1,n,1 do
moved = turtle.down()
if moved then
y=y-1
save()
else
break
end
end
end
return moved
end
function forward(n) -- Moves the turtle forward "n" distance
local moved
if not n then
n=1
end
if face==0 then
for k=1,n,1 do
moved=turtle.forward()
if moved then
z=z+1
save()
else
break
end
end
elseif face==1 then
for k=1,n,1 do
moved=turtle.forward()
if moved then
x=x-1
save()
else
break
end
end
elseif face==2 then
for k=1,n,1 do
moved=turtle.forward()
if moved then
z=z-1
save()
else
break
end
end
elseif face==3 then
for k=1,n,1 do
moved=turtle.forward()
if moved then
x=x+1
save()
else
break
end
end
end
return moved
end
function dig()
return turtle.dig()
end
function digDown()
return turtle.digDown()
end
function digUp()
return turtle.digUp()
end
function compareForward(endSlot,startSlot)
if not startSlot then
startSlot=2
end
if not endSlot then
endSlot=ignore
end
Ore=true
for i=startSlot,endSlot,1 do
turtle.select(i)
if turtle.compare() then
Ore = false
break
end
end
if Ore then
turtle.dig()
end
end
function findDown(n) -- Finds all ores bellow the turtle for "n" distance
if not n then
n=y-1
end
for i=1,n,1 do
moved=down()
if not moved and turtle.detectDown() then
digDown()
if not down() then
break
end
elseif not moved and not turtle.detectDown() then
repeat
turtle.attackDown()
until down()
end
for j=1,4,1 do
compareForward()
turnRight()
end
end
end
function moveToPos(A, B, C, face)
a=A-x
b=B-y
c=C-z
if b<0 then
for i=1,math.abs(B)/>/> do
moved=down()
if not moved and turtle.detectDown() then
repeat
digDown()
until down()
elseif not moved and not turtle.detectDown() then
repeat
turtle.attackDown()
until down()
end
end
elseif b>0 then
for i=1,math.abs(B)/>/> do
moved=up()
if not moved and turtle.detectUp() then
repeat
digUp()
until up()
elseif not moved and not turtle.detectUp() then
repeat
turtle.attackUp()
until up()
end
end
end
if a<0 then
face()
elseif a>0 then
face()
end
if a ~= 0 then
for i=1,math.abs(a) do
moved=forward()
if not moved and turtle.detect() then
repeat
dig()
until forward()
elseif not moved and not turtle.detect() then
repeat
turtle.attack()
until forward()
end
end
end
if c<0 then
face()
elseif c>0 then
face()
end
if c ~= 0 then
for i=1,math.abs(c) do
moved=forward()
if not moved and turtle.detect() then
repeat
dig()
until forward()
elseif not moved and not turtle.detect() then
repeat
turtle.attack()
until forward()
end
end
end
face(face)
end
function checkReturn()
if turtle.getFuelLevel() < 2*y+W+L then
moveToPos(ofsave["startPos"]["x"],ofsave["startPos"]["y"],ofsave["startPos"]["z"],faceB)
for i=ignore+2,16,1 do
turtle.select(i)
turtle.drop()
end
turtle.select(1)
repeat
if turtle.getItemCount(1) > 1 then
turtle.refuel(1)
end
sleep(0.1)
until turtle.getFuelLevel() > 2*y+W+L
moveToPos(ofsave["returnPos"]["x"], ofsave["returnPos"]["y"], ofsave["returnPos"]["z"], ofsave["returnPos"]["face"])
elseif turtle.getItemCount(16) > 0 then
moveToPos(ofsave["startPos"]["y"], ofsave["startPos"]["y"],ofsave["startPos"]["z"],faceB)
for i=ignore+2,16,1 do
turtle.select(i)
turtle.drop()
end
moveToPos(ofsave["returnPos"]["x"], ofsave["returnPos"]["y"], ofsave["returnPos"]["z"], ofsave["returnPos"]["face"])
end
end
function oreFinder()
l=ofsave["l"]
L=ofsave["L"]
for l=l,L do
w=ofsave["w"]
W=ofsave["W"]
for w=w,W do
ofsave = {["returnPos"] = {["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face}, ["l"] = l, ["w"] = w, ["L"] = L, ["W"] = W}
checkReturn()
returnPosx=ofsave["returnPos"]["x"]
returnPosz=ofsave["returnPos"]["z"]
if math.fmod((ofsave["returnPos"]["x"]+4)-2*(ofsave["returnPos"]["z"]-1), 5) == 0 then
findDown()
moveToPos(ofsave["returnPos"]["x"], ofsave["returnPos"]["y"], ofsave["returnPos"]["z"], ofsave["returnPos"]["face"])
end
if ofsave["w"] == ofsave["W"] then
if face == faceF then
turnRight()
ofsave = {["right"] = true}
elseif face == faceB then
turnLeft()
ofsave = {["left"] = true}
end
ofsave = {["returnPos"] = {["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face}}
end
if not ofsave["forward"] then
moved=forward()
if not moved and turtle.detect() then
repeat dig() until forward()
elseif not moved and not turtle.detect() then
repeat turtle.attack() until forward()
end
ofsave = {["returnPos"] = {["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face}, ["forward"] = true}
end
if ofsave["right"] then
turnRight()
ofsave = {["right"] = false}
elseif ofsave["left"] then
turnLeft()
ofsave = {["left"] = false}
end
ofsave = {["returnPos"] = {["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face},["forward"] = false}
end
ofsave["w"] = 1
end
fs.delete(ofsave)
end
function runOreFinder(width, length, ignore)
if width and length then
local ofsave = {
["startPos"] = {
["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face
},
["currentPos"] = {
["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face
},
["returnPos"] = {
["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face
},
["ignore"] = ignore,
["forward"] = false,
["left"] = false,
["right"] = false,
["W"] = width,
["L"] = length,
["w"] = 1,
["l"] = 1
}
if fs.exists("ofsave") then
fs.delete("ofsave")
end
saveTable(ofsave, "ofsave")
else
local ofsave = loadTable(ofsave)
moveToPos(ofsave["returnPos"]["x"], ofsave["returnPos"]["y"], ofsave["returnPos"]["z"], ofsave["returnPos"]["face"])
end
local faceF = ofsave["startPos"]["face"] -- Line 408
if faceF<=1 then
local faceB = faceF+2
else
local faceB = faceF-2
end
parallel.waitForAny(orefinder, skip)
end
local tArgs = { ... }
if #tArgs > 0 then
width = tArgs[1]
length = tArgs[2]
ignore = tArgs[3]
end
x = 1
y = 64
z = 1
face = 0
runOreFinder(width, length, ignore)
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[Lua][Error] Attempt to index ? (a nil value)
Started by Henness, 17 December 2012 - 01:03 PMPosted 17 December 2012 - 02:03 PM
I'm working on an orefinder program that stores it's variables and cords in a table then saves the table to a file so if the program is stopped then it can be started again. But I'm having the hardest time understading how you retrieve values from a table. I get, Attempt to index ? (a nil value) on line 408.
Posted 17 December 2012 - 02:28 PM
Anyone??? :(/>
Posted 17 December 2012 - 03:12 PM
Can someone please help me?
Posted 17 December 2012 - 03:15 PM
when I paste that into an editor there is no line 408! o.O
What is line 408 for you?
What is line 408 for you?
Posted 17 December 2012 - 03:47 PM
if fs.exists("ofsave") then
fs.delete("ofsave")
end
saveTable(ofsave, "ofsave")
else
local ofsave = loadTable(ofsave)
moveToPos(ofsave["returnPos"]["x"], ofsave["returnPos"]["y"], ofsave["returnPos"]["z"], ofsave["returnPos"]["face"])
end
local faceF = ofsave["startPos"]["face"] -- Line 408
if faceF<=1 then
local faceB = faceF+2
else
local faceB = faceF-2
end
parallel.waitForAny(orefinder, skip)
end
local tArgs = { ... }
if #tArgs > 0 then
width = tArgs[1]
length = tArgs[2]
ignore = tArgs[3]
elseif not fs.exists("ofsave") then
print("Usage: " .. shell.getRunningProgram() .. " <width> <length> <ignore>")
end
x = 1
y = 64
z = 1
face = 0
runOreFinder(width, length, ignore)
Sorry The code thing deleted all the enters
Posted 17 December 2012 - 03:51 PM
maybe next time use pastebin to paste long code. so that snippet just adds to the end?
Posted 17 December 2012 - 04:01 PM
Posted 17 December 2012 - 04:07 PM
thank you. looking into error now :)/>
Posted 17 December 2012 - 04:10 PM
I am getting an error on line 415
local ofsave = loadTable(ofsave)
should be this from what I can tell.
local ofsave = loadTable("ofsave")
Posted 17 December 2012 - 04:11 PM
Is it because I made ofsave local and it doesn't think that it exists?
edit: ya just what I thought of I'll try that.
edit: ya just what I thought of I'll try that.
Posted 17 December 2012 - 04:15 PM
line 294 — line 298
it thinks with face() you are trying to call a number because you forgot the parameter.
if c<0 then
face()
elseif c>0 then
face()
end
it thinks with face() you are trying to call a number because you forgot the parameter.
Posted 17 December 2012 - 04:18 PM
ya I never put the variables in I was on a plane and didn't know what directions to put in.
Posted 17 December 2012 - 04:22 PM
haha ok. well thats what I've found so far :)/>
Posted 17 December 2012 - 06:17 PM
https://github.com/Henness0666/Advanced-Programs/blob/master/advancedorefinder
First time I run it It locks up and freezes and the second time I run it I get attempt to call number on line 291
First time I run it It locks up and freezes and the second time I run it I get attempt to call number on line 291
Posted 17 December 2012 - 06:36 PM
how large is that file that it reads?
Posted 17 December 2012 - 06:46 PM
This table saved with textutils.serialize
ofsave = {
["startPos"] = {
["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face
},
["currentPos"] = {
["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face
},
["returnPos"] = {
["x"] = x, ["y"] = y, ["z"] = z, ["face"] = face
},
["ignore"] = ignore,
["forward"] = false,
["left"] = false,
["right"] = false,
["W"] = width,
["L"] = length,
["w"] = 1,
["l"] = 1
}
Posted 17 December 2012 - 06:52 PM
hmmm ok then. I was thinking that the locking up may have been caused by file reads.
Posted 17 December 2012 - 07:39 PM
I got it there was a typo in the parallel.waitForAny(oreFinder, skip) line