Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- origine
- -- http://www.computercraft.info/forums2/index.php?/topic/1252-turtle-smart-movement-and-coordinate-tracking/
- dataFile = "/posData"
- -- reboot
- function reboot()
- term.clear()
- term.setCursorPos(1,1)
- local data = { }
- -- check file data
- if fs.exists(dataFile) then
- local handle = assert(fs.open(dataFile, "r"), "Couldn't load "..dataFile) -- this file is opened in read mode
- local input = handle.readAll() -- input is now the serialized table
- handle.close()
- -- -- load data
- data = textutils.unserialize(input) -- this will decode our table
- x = tonumber( data.x )
- y = tonumber( data.y )
- z = tonumber( data.z )
- facingnum = tonumber( data.facingnum )
- -- confirm last pos
- status()
- action = ""
- while action ~= "y" or action ~= "n" do
- write("Correct ? (Y/N) ")
- action = string.lower( read() )
- if action == "n" then
- calibrate()
- storeData()
- break
- elseif action == "y" then
- break
- else
- status()
- end
- end
- -- -- create data file
- else
- print("Create "..dataFile.." file")
- calibrate()
- storeData()
- end
- end
- function storeData()
- local data = { }
- data.x = x
- data.y = y
- data.z = z
- data.facingnum = facingnum
- local output = textutils.serialize(data)
- local handle = assert(fs.open(dataFile, "w"), "Couldn't save "..dataFile) -- this will give you an error if something's gone wrong
- handle.write(output) -- this is where the magic happens, we're storing the entire "target" table
- handle.close()
- end
- -- Orientation
- function correctfacing()
- if (facingnum == 5) then
- facingnum = 1
- elseif (facingnum == 0) then
- facingnum = 4
- end
- end
- function facedir(dir)
- if (dir == "n") then
- dirnum = 1
- elseif (dir == "e") then
- dirnum = 2
- elseif (dir == "s") then
- dirnum = 3
- elseif (dir == "w") then
- dirnum = 4
- end
- facingprocess = 1
- while (facingprocess == 1) do
- if (facingnum < dirnum) then
- turtle.turnRight()
- facingnum = (facingnum + 1)
- elseif (facingnum > dirnum) then
- turtle.turnLeft()
- facingnum = (facingnum - 1)
- elseif (facingnum == dirnum) then
- facingprocess = 0
- end
- correctfacing()
- end
- end
- -- Mouvement
- function tforward()
- tdigUp()
- if turtle.forward() then
- if (facingnum == 1) then
- z = (z - 1)
- elseif (facingnum == 2) then
- x = (x + 1)
- elseif (facingnum == 3) then
- z = (z + 1)
- elseif (facingnum == 4) then
- x = (x - 1)
- end
- else
- tdig()
- end
- end
- function tdig()
- while turtle.detect() do
- if turtle.dig() or turtle.attack() then
- sleep(0.5)
- else break end
- end
- end
- function tdigUp()
- while turtle.detectUp() do
- if turtle.digUp() or turtle.attackUp() then
- sleep(0.5)
- else break end
- end
- end
- function tup()
- if turtle.up() then
- y = (y+1)
- else
- tdigUp()
- end
- end
- function tdown()
- if turtle.down() then
- y = (y-1)
- else
- while turtle.detectDown() or turtle.attackDown() do
- if turtle.digDown() then
- sleep(0.5)
- else break end
- end
- end
- end
- function tright()
- turtle.turnRight()
- facingnum = (facingnum + 1)
- correctfacing()
- end
- function tleft()
- turtle.turnLeft()
- facingnum = (facingnum - 1)
- correctfacing()
- end
- -- Positionnement
- function flyto(tarx,tary,tarz)
- -- process
- tx = tonumber(tarx)
- ty = tonumber(tary)
- tz = tonumber(tarz)
- fly = 1
- flyingloop()
- end
- function flyingloop()
- while (fly == 1) do
- if (tonumber(y) < tonumber(ty)) then
- tup()
- elseif (tonumber(z) > tonumber(tz)) then
- facedir("n")
- tforward()
- elseif (tonumber(z) < tonumber(tz)) then
- facedir("s")
- tforward()
- elseif (tonumber(x) < tonumber(tx)) then
- facedir("e")
- tforward()
- elseif (tonumber(x) > tonumber(tx)) then
- facedir("w")
- tforward()
- elseif (tonumber(y) > tonumber(ty)) then
- tdown()
- else
- fly = 0
- end
- end
- end
- -- Coordonnées
- function setcoords()
- term.clear()
- term.setCursorPos(1,1)
- print("Please input target coordinates")
- write("X: ")
- targetx = read()
- write("Y: ")
- targety = read()
- write("Z: ")
- targetz = read()
- print("Coordinates set.")
- sleep(2)
- end
- function status()
- term.clear()
- term.setCursorPos(1,1)
- if (facingnum == 1) then
- facing = "n"
- elseif (facingnum == 2) then
- facing = "e"
- elseif (facingnum == 3) then
- facing = "s"
- elseif (facingnum == 4) then
- facing = "w"
- end
- print("X: "..x.." Y: "..y.." Z: "..z.." Facing: "..facing)
- local fuelLevel = tostring( turtle.getFuelLevel() )
- print("Fuel level: "..fuelLevel)
- sleep(0.5)
- end
- function calibrate()
- print("Please input turtles current X, Y and Z coordinates.")
- write("X: ")
- tempx = read()
- x = tonumber(tempx)
- write("Y: ")
- tempy = read()
- y = tonumber(tempy)
- write("Z: ")
- tempz = read()
- z = tonumber(tempz)
- print("")
- print("Coordinates set.")
- print("Please input the facing of the turtle, n / e / s / w")
- write("Facing: ")
- facingloop = 1
- while (facingloop == 1) do
- facing = read()
- if (facing == "n") then
- facingnum = 1
- facingloop = 0
- elseif (facing == "e") then
- facingloop = 0
- facingnum = 2
- elseif (facing == "s") then
- facingloop = 0
- facingnum = 3
- elseif (facing == "w") then
- facingloop = 0
- facingnum = 4
- else
- print("Invalid input")
- end
- end
- print("Facing set.")
- sleep(1)
- end
- -- Chunk landmark
- function cornerMark()
- turtle.digDown()
- turtle.select(2)
- turtle.placeDown()
- tdig()
- tleft()
- tdig()
- tleft()
- tdig()
- tleft()
- tdig()
- tdigUp()
- tup()
- turtle.select(1)
- turtle.placeDown()
- end
- function ChunkLandMark()
- -- If items needed
- if turtle.getItemCount(1) < 3 or turtle.getItemCount(2) < 4 then
- term.clear()
- term.setCursorPos(1,1)
- print("- /!\\ --------------")
- print("Place at least 3 Land Mark in slot 1 and 4 blocks in slot 2.")
- write("Press any key to resume")
- os.pullEvent("key")
- return false
- end
- countdown()
- -- goto (c 0,0)
- _cx = x - (x % 16)
- _cy = y
- _cz = z - (z % 16)
- flyto(_cx, y, _cz)
- cornerMark()
- flyto(_cx + 1, _cy + 1, _cz)
- flyto(_cx + 1, _cy, _cz)
- flyto(_cx + 16, _cy, _cz)
- cornerMark()
- flyto(_cx + 16, _cy + 1, _cz + 1)
- flyto(_cx + 16, _cy, _cz + 1)
- flyto(_cx + 16, _cy, _cz + 16)
- cornerMark()
- flyto(_cx + 15, _cy + 1, _cz + 16)
- flyto(_cx + 15, _cy, _cz + 16)
- flyto(_cx, _cy, _cz + 16)
- cornerMark()
- flyto(_cx, _cy + 1, _cz + 15)
- flyto(_cx, _cy, _cz + 15)
- flyto(_cx, _cy, _cz + 1)
- turtle.select(2)
- turtle.placeDown()
- return true
- end
- -- Interface
- function countdown()
- -- countdown
- write("Wait 3...")
- sleep(0.9)
- write("2...")
- sleep(0.9)
- write("1...")
- sleep(0.9)
- print("Start")
- end
- action = nil
- local function menu()
- print("--------------------")
- print("Wait instructions")
- action = string.lower( read() )
- print("--------------------")
- if action == "exit" then
- print("Goodbye")
- return false
- elseif action == "clear" then
- term.clear()
- term.setCursorPos(1,1)
- elseif action == "refuel" then
- shell.run('refuel all')
- elseif action == "setpos" then
- calibrate()
- storeData()
- elseif action == "getpos" then
- status()
- elseif action == "flyto" or action == "goto" then
- write("X: ") temp = read()
- if temp == "" then toX = x
- else toX = tonumber( temp )
- end
- write("Y: ") temp = read()
- if temp == "" then toY = y
- else toY = tonumber( temp )
- end
- write("Z: ") temp = read()
- if temp == "" then toZ = z
- else toZ = tonumber( temp )
- end
- countdown()
- flyto(toX, toY, toZ)
- status()
- storeData()
- elseif action == "chunklandmark" then
- if ChunkLandMark() then
- print( "Chunk delimited." )
- end
- storeData()
- elseif action == "update" then
- term.clear()
- term.setCursorPos(1,1)
- shell.run('rm /pos')
- shell.run('pastebin get HQdMc7Uf /pos')
- action = ""
- while action ~= "y" or action ~= "n" do
- write("Restart now ? (Y/N) ")
- action = string.lower( read() )
- if action == "n" then
- break
- elseif action == "y" then
- return false
- end
- end
- else
- if action ~= "help" then print("Unknow: "..action) end
- print("-MENU---------------")
- print("help clear setPos getPos flyto refuel chunkLandMark update exit")
- end
- return true
- end
- reboot()
- while true do
- if not menu() then break end
- end
- return
Advertisement
Add Comment
Please, Sign In to add comment