Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function forward(num)
- num = num or 1
- for i = 1, num do
- if turtle.forward() then
- if f == 0 then
- z = z - 1
- update("z",z)
- elseif f == 1 then
- x = x + 1
- update("x",x)
- elseif f == 2 then
- z = z + 1
- update("z",z)
- elseif f == 3 then
- x = x - 1
- update("x",x)
- end
- checkFuel(returning)
- else
- return false
- end
- end
- return true
- end
- function back(num)
- num = num or 1
- for i = 1, num do
- if turtle.back() then
- if f == 0 then
- z = z + 1
- update("z",z)
- elseif f == 1 then
- x = x - 1
- update("x",x)
- elseif f == 2 then
- z = z - 1
- update("z",z)
- elseif f == 3 then
- x = x + 1
- update("x",x)
- end
- checkFuel(returning)
- else
- return false
- end
- end
- return true
- end
- function up(num)
- num = num or 1
- for i = 1, num do
- if turtle.up() then
- y = y + 1
- update("y",y)
- checkFuel(returning)
- else
- return false
- end
- end
- return true
- end
- function down(num)
- num = num or 1
- for i = 1, num do
- if turtle.down() then
- y = y - 1
- update("y",y)
- checkFuel(returning)
- return true
- else
- return false
- end
- end
- return true
- end
- function left(num)
- num = num or 1
- for i = 1, num do
- turtle.turnLeft()
- f = (f+3)%4
- update("f",f)
- end
- end
- function right(num)
- num = num or 1
- for i = 1, num do
- turtle.turnRight()
- f = (f+1)%4
- update("f",f)
- end
- end
- function home()
- returning = true
- update("returning",true)
- while y ~= 0 do
- if y < 0 then
- turtle.digUp()
- up()
- elseif y > 0 then
- turtle.digDown()
- down()
- end
- end
- if z > 0 then
- while f ~= 0 do
- left()
- end
- elseif z < 0 then
- while f ~= 2 do
- left()
- end
- end
- while z ~= 0 do
- forward()
- end
- if x > 0 then
- while f ~= 3 do
- left()
- end
- elseif x < 0 then
- while f ~= 1 do
- left()
- end
- end
- while x ~= 0 do
- forward()
- end
- end
- function checkFuel(flag)
- if not flag then
- local fuel = turtle.getFuelLevel()
- local distance = math.abs(x) + math.abs(y) + math.abs(z)
- if fuel-1 <= distance then
- home()
- error("Alert: Fuel level critical. Returning to home point...")
- end
- end
- end
- function update(var,value)
- if not tostring(var) then
- error("var needs to be a string")
- end
- value = tostring(value)
- local str = fs.open("info","r").readAll()
- if str:find(var) then
- str = str:gsub(var.."%=.-%;",var.."="..value..";")
- else
- str = str .."\n"..var.."="..value..";"
- end
- local h = fs.open("info","w")
- h.write(str)
- h.close()
- end
- function log(data)
- local h = fs.open("log", "a")
- if type(data) == "string" then
- h.writeLine(data)
- elseif type(data) == "table" then
- for i = 1, #data do
- h.writeLine(data[i])
- end
- end
- h.close()
- end
- if not fs.exists("info") then
- local h = fs.open("info","w")
- x,y,z,f = 0,0,0,0
- h.writeLine("x=0;")
- h.writeLine("y=0;")
- h.writeLine("z=0;")
- h.writeLine("f=0;")
- h.writeLine("returning=false;")
- h.close()
- else
- shell.run("info")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement