Posted 05 June 2013 - 11:49 PM
So i created a short program to test out a couple of file sharing functions i have created. This program will dig a x amount of blocks in width, z in length, and y in height. While it is digging it will save all of its coords when moving with its starting point being 0, 0 ,0 facing 0 (north). The program begins in the top right corner of the area you wish to clear. My problem is somewhere in the banking command and after about a hour of trying to find the error i have given up completely stumped. What it does is it banks what it has then will return to the location it was before it banked. It then will get stuck going forward after it has returned to its position before banking. I can not locate why it does this. I get no error message. From what i can tell if i let it it would go until it went into a unloaded chunk.
code:
http://pastebin.com/a1ci8vVP
Thank you for your time
code:
Spoiler
local x = 0
local y = 0
local z = 0
local direction = 0
local width = 0
local length = 0
local height = 0
local oldx = 0
local oldz = 0
local oldy = 0
local olddir = 0
function file()
if not fs.exists("coord") then
w = fs.open("coord","w")
w.writeLine("0")
w.writeLine("0")
w.writeLine("0")
w.writeLine("0")
w.writeLine("0")
w.writeLine("0")
w.writeLine("0")
w.writeLine("0")
w.writeLine("-- 1 = x, 2 = z, 3 = y, 4 = direction")
w.writeLine("-- 5 = oldx, 6 = oldz, 7 = oldy, 8 = olddir")
w.close()
end
end
function readsave()
file()
r = fs.open("coord","r")
x = tonumber(r.readLine(1))
z = tonumber(r.readLine(2))
y = tonumber(r.readLine(3))
direction = tonumber(r.readLine(4))
oldx = tonumber(r.readLine(5))
oldz = tonumber(r.readLine(6))
oldy = tonumber(r.readLine(7))
olddir = tonumber(r.readLine(8))
r.close()
end
function save()
s = fs.open("coord","w")
s.writeLine(x)
s.writeLine(z)
s.writeLine(y)
s.writeLine(direction)
s.writeLine(oldx)
s.writeLine(oldz)
s.writeLine(oldy)
s.writeLine(olddir)
s.close()
end
function forward()
if turtle.forward()==true then
if direction==0 then
z = z+1
save()
elseif direction==1 then
x = x-1
save()
elseif direction==2 then
z = z-1
save()
elseif direction==3 then
x = x+1
save()
end
else
print("something is blocking my way")
turtle.dig()
turtle.attack()
sleep(1)
forward()
end
end
function dig()
turtle.dig()
forward()
end
function down()
if turtle.down()==true then
y = y-1
save()
else
while turtle.down()==false do
turtle.attack()
turtle.digDown()
end
y = y-1
save()
end
end
function up()
if turtle.up()==true then
y = y+1
save()
else
while turtle.up()==false do
turtle.attack()
turtle.digUp()
end
y = y+1
save()
end
end
function Left()
if turtle.turnLeft()==true then
direction = direction - 1
direction = direction % 4
save()
end
end
function Right()
if turtle.turnRight()==true then
direction = direction + 1
direction = direction % 4
save()
end
end
file()
readsave()
save()
print("enter desired width:")
wid = io.read()
print("enter desired length:")
len = io.read()
print("enter desired height:")
high = io.read()
function length()
for i = 2, len do
dig()
bank()
end
Left()
Left()
for i = 2, len do
dig()
end
Right()
Right()
end
function width()
for j = 2, wid do
Left()
dig()
Right()
length()
end
end
function height()
for m = 2, high do
length()
width()
Right()
for i = 1, x do
dig()
end
Left()
turtle.digDown()
down()
end
length()
width()
end
function face0()
readsave()
while direction~=0 do
Right()
readsave()
print(direction)
end
print("facing direction 0")
end
function face1()
readsave()
while direction~=1 do
Right()
readsave()
print(direction)
end
print("facing direction 1")
end
function face2()
readsave()
while direction~=2 do
Right()
readsave()
print(direction)
end
print("facing direction 2")
end
function face3()
readsave()
while direction~=3 do
Right()
readsave()
print(direction)
end
print("facing direction 3")
end
function xto0()
readsave()
if x>0 then
face1()
while x~=0 do
forward()
readsave()
print("current cords".."("..x..","..z..")")
end
face0()
print("arrived at 0 x coord")
elseif x<0 then
face3()
while x~=0 do
forward()
readsave()
print("current cords".."("..x..","..z..")")
end
face0()
print("arrived at 0 x coord")
elseif x==0 then
face0()
print("turtle already at x=0")
end
end
function zto0()
readsave()
if z>0 then
face2()
while z~=0 do
forward()
readsave()
print("current cords".."("..x..","..z..")")
end
face0()
print("arrived at z=0 coord")
elseif z<0 then
face0()
while z~=0 do
forward()
readsave()
print("current cords".."("..x..","..z..")")
end
face0()
print("arrived at z=0 coord")
elseif z==0 then
face0()
print("turtle already at z=0")
end
end
function yto0()
readsave()
if y>0 then
while y~=0 do
down()
readsave()
print("y ="..y)
end
print("arrived at y=0")
elseif y<0 then
while y~=0 do
up()
readsave()
print("y ="..y)
end
print("arrived at y = 0")
elseif y==0 then
print("arived at y = 0")
end
end
function bank()
readsave()
if turtle.getItemCount(16)>=1 then
oldx = x
oldz = z
oldy = y
olddir = direction
save()
print(oldx..oldz..oldy)
print(x..z..y)
xto0()
zto0()
yto0()
face2()
for banker = 1, 16 do
turtle.select(banker)
turtle.drop()
end
face0()
end
backoldx()
backoldz()
backoldy()
backolddir()
turtle.select(1)
end
function backoldx()
readsave()
if oldx>0 then
face3()
while x~=oldx do
forward()
readsave()
end
elseif oldx<0 then
face1()
while x~=oldx do
forward()
readsave()
end
end
end
function backoldz()
readsave()
if oldz>0 then
face0()
while z~=oldz do
forward()
readsave()
end
elseif oldz<0 then
face2()
while z~=oldz do
forward()
readsave()
end
end
end
function backoldy()
readsave()
if oldy>0 then
while y~=oldy do
up()
readsave()
end
elseif oldy<0 then
while y~=oldy do
down()
readsave()
end
end
end
function backolddir()
readsave()
while direction~=olddir do
Left()
end
end
height()
xto0()
zto0()
yto0()
http://pastebin.com/a1ci8vVP
Thank you for your time
Edited on 05 June 2013 - 11:47 PM