nSun

turtle_pos_smart

Mar 26th, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.34 KB | None | 0 0
  1. -- origine
  2. -- http://www.computercraft.info/forums2/index.php?/topic/1252-turtle-smart-movement-and-coordinate-tracking/
  3.  
  4. dataFile = "/posData"
  5. -- reboot
  6. function reboot()
  7.     term.clear()
  8.     term.setCursorPos(1,1)
  9.     local data = { }
  10.     -- check file data
  11.     if fs.exists(dataFile) then
  12.         local handle = assert(fs.open(dataFile, "r"), "Couldn't load "..dataFile) -- this file is opened in read mode
  13.         local input = handle.readAll() -- input is now the serialized table
  14.         handle.close()
  15.     -- -- load data
  16.         data = textutils.unserialize(input) -- this will decode our table
  17.         x = tonumber( data.x )
  18.         y = tonumber( data.y )
  19.         z = tonumber( data.z )
  20.         facingnum = tonumber( data.facingnum )
  21.     -- confirm last pos
  22.         status()
  23.         action = ""
  24.         while action ~= "y" or action ~= "n" do
  25.             write("Correct ? (Y/N) ")
  26.             action = string.lower( read() )
  27.             if action == "n" then
  28.                 calibrate()
  29.                 storeData()
  30.                 break
  31.             elseif action == "y" then
  32.                 break
  33.             else
  34.                 status()
  35.             end
  36.         end
  37.     -- -- create data file
  38.     else
  39.         print("Create "..dataFile.." file")
  40.         calibrate()
  41.         storeData()
  42.     end
  43. end
  44.  
  45. function storeData()
  46.     local data = { }
  47.     data.x = x
  48.     data.y = y
  49.     data.z = z
  50.     data.facingnum = facingnum
  51.     local output = textutils.serialize(data)
  52.     local handle = assert(fs.open(dataFile, "w"), "Couldn't save "..dataFile) -- this will give you an error if something's gone wrong
  53.     handle.write(output) -- this is where the magic happens, we're storing the entire "target" table
  54.     handle.close()
  55. end
  56.  
  57. -- Orientation
  58. function correctfacing()
  59.     if (facingnum == 5) then
  60.         facingnum = 1
  61.         elseif (facingnum == 0) then
  62.         facingnum = 4
  63.     end
  64. end
  65.  
  66. function facedir(dir)
  67.     if (dir == "n") then
  68.         dirnum = 1
  69.     elseif (dir == "e") then
  70.         dirnum = 2
  71.     elseif (dir == "s") then
  72.         dirnum = 3
  73.     elseif (dir == "w") then
  74.         dirnum = 4
  75.     end
  76.     facingprocess = 1
  77.     while (facingprocess == 1) do
  78.         if (facingnum < dirnum) then
  79.                 turtle.turnRight()
  80.                 facingnum = (facingnum + 1)
  81.             elseif (facingnum > dirnum) then
  82.                 turtle.turnLeft()
  83.                 facingnum = (facingnum - 1)
  84.             elseif (facingnum == dirnum) then
  85.                 facingprocess = 0
  86.         end
  87.         correctfacing()
  88.     end
  89. end
  90.  
  91. -- Mouvement
  92. function tforward()
  93.     tdigUp()
  94.     if turtle.forward() then
  95.         if (facingnum == 1) then
  96.             z = (z - 1)
  97.         elseif (facingnum == 2) then
  98.             x = (x + 1)
  99.         elseif (facingnum == 3) then
  100.             z = (z + 1)
  101.         elseif (facingnum == 4) then
  102.             x = (x - 1)
  103.         end
  104.     else
  105.         tdig()
  106.     end
  107. end
  108.  
  109.  
  110. function tdig()
  111.     while turtle.detect() do
  112.         if turtle.dig() or turtle.attack() then
  113.             sleep(0.5)
  114.         else break end
  115.     end
  116. end
  117.  
  118. function tdigUp()
  119.     while turtle.detectUp() do
  120.         if turtle.digUp() or turtle.attackUp() then
  121.             sleep(0.5)
  122.         else break end
  123.     end
  124. end
  125.  
  126. function tup()
  127.     if turtle.up() then
  128.         y = (y+1)
  129.     else
  130.         tdigUp()
  131.     end
  132. end
  133.  
  134. function tdown()
  135.     if turtle.down() then
  136.         y = (y-1)
  137.     else
  138.         while turtle.detectDown() or turtle.attackDown() do
  139.             if turtle.digDown() then
  140.                 sleep(0.5)
  141.             else break end
  142.         end
  143.     end
  144. end
  145.  
  146. function tright()
  147.     turtle.turnRight()
  148.     facingnum = (facingnum + 1)
  149.     correctfacing()
  150. end
  151.  
  152. function tleft()
  153.     turtle.turnLeft()
  154.     facingnum = (facingnum - 1)
  155.     correctfacing()
  156. end
  157.  
  158.  
  159. -- Positionnement
  160. function flyto(tarx,tary,tarz)
  161.     -- process
  162.     tx = tonumber(tarx)
  163.     ty = tonumber(tary)
  164.     tz = tonumber(tarz)
  165.     fly = 1
  166.     flyingloop()
  167. end
  168.  
  169. function flyingloop()
  170.     while (fly == 1) do
  171.         if (tonumber(y) < tonumber(ty)) then
  172.             tup()
  173.         elseif (tonumber(z) > tonumber(tz)) then
  174.             facedir("n")
  175.             tforward()
  176.         elseif (tonumber(z) < tonumber(tz)) then
  177.             facedir("s")
  178.             tforward()
  179.         elseif (tonumber(x) < tonumber(tx)) then
  180.             facedir("e")
  181.             tforward()
  182.         elseif (tonumber(x) > tonumber(tx)) then
  183.             facedir("w")
  184.             tforward()
  185.         elseif (tonumber(y) > tonumber(ty)) then
  186.             tdown()
  187.         else
  188.             fly = 0
  189.         end
  190.     end
  191. end
  192.  
  193.  
  194. -- Coordonnées
  195. function setcoords()
  196.     term.clear()
  197.     term.setCursorPos(1,1)
  198.     print("Please input target coordinates")
  199.     write("X: ")
  200.     targetx = read()
  201.     write("Y: ")
  202.     targety = read()
  203.     write("Z: ")
  204.     targetz = read()
  205.     print("Coordinates set.")
  206.     sleep(2)
  207. end
  208.  
  209. function status()
  210.     term.clear()
  211.     term.setCursorPos(1,1)
  212.     if (facingnum == 1) then
  213.         facing = "n"
  214.     elseif (facingnum == 2) then
  215.         facing = "e"
  216.     elseif (facingnum == 3) then
  217.         facing = "s"
  218.     elseif (facingnum == 4) then
  219.         facing = "w"
  220.     end
  221.     print("X: "..x.." Y: "..y.." Z: "..z.." Facing: "..facing)
  222.     local fuelLevel = tostring( turtle.getFuelLevel() )
  223.     print("Fuel level: "..fuelLevel)
  224.     sleep(0.5)
  225. end
  226.  
  227. function calibrate()
  228.     print("Please input turtles current X, Y and Z coordinates.")
  229.     write("X: ")
  230.     tempx = read()
  231.     x = tonumber(tempx)
  232.     write("Y: ")
  233.     tempy = read()
  234.     y = tonumber(tempy)
  235.     write("Z: ")
  236.     tempz = read()
  237.     z = tonumber(tempz)
  238.     print("")
  239.     print("Coordinates set.")
  240.     print("Please input the facing of the turtle, n / e / s / w")
  241.     write("Facing: ")
  242.     facingloop = 1
  243.     while (facingloop == 1) do
  244.         facing = read()
  245.         if (facing == "n") then
  246.             facingnum = 1
  247.             facingloop = 0
  248.         elseif (facing == "e") then
  249.             facingloop = 0
  250.             facingnum = 2
  251.         elseif (facing == "s") then
  252.             facingloop = 0
  253.             facingnum = 3
  254.         elseif (facing == "w") then
  255.             facingloop = 0
  256.             facingnum = 4
  257.         else
  258.             print("Invalid input")
  259.         end
  260.     end
  261.     print("Facing set.")
  262.     sleep(1)
  263. end
  264.  
  265. -- Chunk landmark
  266. function cornerMark()
  267.     turtle.digDown()
  268.     turtle.select(2)
  269.     turtle.placeDown()
  270.     tdig()
  271.     tleft()
  272.     tdig()
  273.     tleft()
  274.     tdig()
  275.     tleft()
  276.     tdig()
  277.     tdigUp()
  278.     tup()
  279.     turtle.select(1)
  280.     turtle.placeDown()
  281. end
  282.  
  283. function ChunkLandMark()
  284.     -- If items needed
  285.     if turtle.getItemCount(1) < 3 or turtle.getItemCount(2) < 4 then
  286.         term.clear()
  287.         term.setCursorPos(1,1)
  288.         print("- /!\\ --------------")
  289.         print("Place at least 3 Land Mark in slot 1 and 4 blocks in slot 2.")
  290.         write("Press any key to resume")
  291.         os.pullEvent("key")
  292.         return false
  293.     end
  294.     countdown()
  295.    
  296.     -- goto (c 0,0)
  297.     _cx = x - (x % 16)
  298.     _cy = y
  299.     _cz = z - (z % 16)
  300.    
  301.     flyto(_cx, y, _cz)
  302.     cornerMark()
  303.     flyto(_cx + 1,  _cy + 1,    _cz)
  304.     flyto(_cx + 1,  _cy,        _cz)
  305.     flyto(_cx + 16, _cy,        _cz)
  306.     cornerMark()
  307.     flyto(_cx + 16, _cy + 1,    _cz + 1)
  308.     flyto(_cx + 16, _cy,        _cz + 1)
  309.     flyto(_cx + 16, _cy,        _cz + 16)
  310.     cornerMark()
  311.     flyto(_cx + 15, _cy + 1,    _cz + 16)
  312.     flyto(_cx + 15, _cy,        _cz + 16)
  313.     flyto(_cx,      _cy,        _cz + 16)
  314.     cornerMark()
  315.     flyto(_cx,      _cy + 1,    _cz + 15)
  316.     flyto(_cx,      _cy,        _cz + 15)
  317.     flyto(_cx,      _cy,        _cz + 1)
  318.  
  319.     turtle.select(2)
  320.     turtle.placeDown()
  321.     return true
  322. end
  323.  
  324. -- Interface
  325. function countdown()
  326.     -- countdown
  327.     write("Wait 3...")
  328.     sleep(0.9)
  329.     write("2...")
  330.     sleep(0.9)
  331.     write("1...")
  332.     sleep(0.9)
  333.     print("Start")
  334. end
  335.  
  336. action = nil
  337. local function menu()
  338.         print("--------------------")
  339.         print("Wait instructions")
  340.         action = string.lower( read() )
  341.         print("--------------------")
  342.         if action == "exit" then
  343.             print("Goodbye")
  344.             return false
  345.         elseif action == "clear" then
  346.             term.clear()
  347.             term.setCursorPos(1,1)
  348.         elseif action == "refuel" then
  349.             shell.run('refuel all')
  350.         elseif action == "setpos" then
  351.             calibrate()
  352.             storeData()
  353.         elseif action == "getpos" then
  354.             status()
  355.         elseif action == "flyto" or action == "goto" then
  356.             write("X: ") temp = read()
  357.             if temp == "" then toX = x
  358.             else toX = tonumber( temp )
  359.             end
  360.             write("Y: ") temp = read()
  361.             if temp == "" then toY = y
  362.             else toY = tonumber( temp )
  363.             end
  364.             write("Z: ") temp = read()
  365.             if temp == "" then toZ = z
  366.             else toZ = tonumber( temp )
  367.             end
  368.             countdown()
  369.             flyto(toX, toY, toZ)
  370.             status()
  371.             storeData()
  372.         elseif action == "chunklandmark" then
  373.             if ChunkLandMark() then
  374.                 print( "Chunk delimited." )
  375.             end
  376.             storeData()
  377.         elseif action == "update" then
  378.             term.clear()
  379.             term.setCursorPos(1,1)
  380.             shell.run('rm /pos')
  381.             shell.run('pastebin get HQdMc7Uf /pos')
  382.             action = ""
  383.             while action ~= "y" or action ~= "n" do
  384.                 write("Restart now ? (Y/N) ")
  385.                 action = string.lower( read() )
  386.                 if action == "n" then
  387.                     break
  388.                 elseif action == "y" then
  389.                     return false
  390.                 end
  391.             end
  392.         else
  393.             if action ~= "help" then print("Unknow: "..action) end
  394.             print("-MENU---------------")
  395.             print("help  clear  setPos  getPos  flyto  refuel  chunkLandMark  update  exit")
  396.         end
  397.         return true
  398. end
  399.  
  400. reboot()
  401. while true do
  402.     if not menu() then break end
  403. end
  404. return
Advertisement
Add Comment
Please, Sign In to add comment