Advertisement
Guest User

startup

a guest
Oct 14th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.73 KB | None | 0 0
  1. sizeX = 7
  2. sizeY = 14
  3. g = peripheral.wrap("left")
  4. local function valueX()
  5.     local h = fs.open("x", "r")
  6.     local X = h.readAll()
  7.     h.close()
  8.     print(X)
  9.     return X
  10. end
  11.  
  12. local function valueY()
  13.     local h = fs.open("y", "r")
  14.     local Y = h.readAll()
  15.     h.close()
  16.     return Y
  17. end
  18.  
  19. local function saveX(x)
  20.     local h = fs.open("x","w")
  21.     h.write(x)
  22.     h.close()
  23. end
  24.  
  25. local function saveY(y)
  26.     local h = fs.open("y","w")
  27.     h.write(y)
  28.     h.close()
  29. end
  30.  
  31. local function saveDir(dir)
  32.     local h = fs.open("dir","w")
  33.     h.write(dir)
  34.     h.close()
  35. end
  36.  
  37. local function check()
  38.     if turtle.detectDown() == true then
  39.         turtle.digDown()
  40.         turtle.select(1)
  41.         turtle.placeDown()
  42.     end
  43. end
  44.  
  45. function setDir(d)
  46.     local h = fs.open("dir","r")
  47.     local curDir = tonumber(h.readAll())
  48.     print(curDir)
  49.     h.close()
  50.     local diff = (curDir - d)
  51.     if diff < 0 then
  52.         for i=1,math.abs(diff) do
  53.             turtle.turnRight()
  54.             curDir = curDir + 1
  55.             if curDir >= 5 then
  56.                 curDir = 1
  57.             end
  58.         end
  59.     elseif diff > 0 then
  60.         for i=1,math.abs(diff) do
  61.             turtle.turnLeft()
  62.             curDir = curDir - 1
  63.             if curDir <= 0 then
  64.                 curDir = 4
  65.             end
  66.         end
  67.     else
  68.         return true
  69.     end
  70.     saveDir(curDir)
  71.     if curDir - d == 0 then
  72.         return true
  73.     else
  74.         return false
  75.     end
  76. end
  77.  
  78. local function Home()
  79.     local thisX = tonumber(valueX())
  80.     local thisY = tonumber(valueY())
  81.     local newY = thisY
  82.     local newX = thisX
  83.     if thisX > 0 then
  84.         setDir(2)
  85.         for i=1,thisX do
  86.             turtle.forward()
  87.             newX = newX-1
  88.         end
  89.         saveX(newX)
  90.     end
  91.    
  92.     if thisY > 0 then
  93.         setDir(3)
  94.         for i=1,thisY do
  95.             turtle.forward()
  96.             newY = newY-1            
  97.         end
  98.         saveY(newY)
  99.     end
  100.    
  101.     if newY == 0 and newX == 0 then
  102.         return true
  103.     else
  104.         return false
  105.     end
  106. end
  107.  
  108. if Home() then
  109.     setDir(1)
  110.    
  111.     while true do
  112.         for x=0,sizeX do
  113.             for y=0,sizeY do
  114.                 turtle.forward()
  115.                 saveY(y)
  116.                 check()
  117.             end
  118.             if (valueX() % 2) == 0 then
  119.                 turtle.turnLeft()
  120.                 turtle.forward()
  121.                 turtle.turnLeft()
  122.                 saveDir(3)
  123.             else
  124.                 turtle.turnRight()
  125.                 turtle.forward()
  126.                 turtle.turnRight()
  127.                 saveDir(1)
  128.             end
  129.             saveX(x)
  130.         end
  131.         Home()
  132.         setDir(1)
  133.         sleep(10)
  134.     end
  135.        
  136. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement