Advertisement
Guest User

startup.lua

a guest
Nov 7th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.80 KB | None | 0 0
  1. local homeX, homeY, homeZ -- Coordinates of turtle home station
  2. homeX = 420
  3. homeY = 65
  4. homeZ = -104
  5. local direction
  6. local timeout = 5 -- The timeout of the gps
  7. local safeHeight = 35 --The height that is safe to dig at
  8.  
  9. function inAllowedBlock(block)
  10.     local allowed = {"minecraft:dirt", "minecraft:cobblestone", "minecraft:stone", "minecraft:sand", "minecraft:sandstone", "minecraft:clay", "minecraft:gravel", "minecraft:yellow_flower", "minecraft:red_flower", "minecraft:granite", "minecraft:wood", "minecraft:double_plant", "minecraft:leaves", "minecraft:moss_stone", "minecraft:grass"}
  11.     for i = 1, #allowed do
  12.         if(allowed[i-1]==block) then return true end
  13.     end
  14.     return false
  15. end
  16.  
  17. function go(dist, dig)
  18.     dig = (dig == true)
  19.     dist = dist or 1
  20.     if(dist <= 0) then return end
  21.     for i = 1, dist do
  22.         while(not turtle.forward()) do
  23.             turtle.attack()
  24.             if(turtle.detect()) then
  25.                 local succ, block = turtle.inspect()
  26.                 if(isAllowedBlock(block.name) or dig) then
  27.                     turtle.dig()
  28.                 else
  29.                     local x,y,z = gps.locate(timeout, true)
  30.                     if(safeHeight<y) then
  31.                         goDown(y-safeHeight)
  32.                         turtle.dig()
  33.                     else
  34.                         turtle.dig()
  35.                     end
  36.                 end
  37.             end
  38.         end
  39.     end        
  40. end
  41.  
  42. function goDown(dist, dig)
  43.     dig = true -- Until I find a way to make sure it does go home if it can't go down
  44.     dist = dist or 1
  45.     if(dist<=0) then return end
  46.     for i = 1, dist do
  47.         while(not turtle.down()) do
  48.             turtle.attackDown()
  49.             if(dig) then
  50.                 turtle.digDown()
  51.             else
  52.                 return false
  53.             end
  54.         end
  55.     end
  56. end
  57.  
  58. function goUp(dist)
  59.     dist = dist or 1
  60.     if(dist<=0)then return end
  61.     for i = 1, dist do
  62.         while(not turtle.up()) do
  63.             turtle.up()
  64.             turtle.digUp()
  65.         end
  66.     end
  67. end
  68.  
  69. function getDirection(dig, times)
  70.     dig = dig ~= false
  71.     times = times or 1
  72.     local loc1 = vector.new(gps.locate(timeout, false))
  73.     if(not turtle.forward()) then
  74.         if(turtle.detect()) then
  75.             local succ, block = turtle.inspect()
  76.             if(isAllowedBlock(block.name) or dig) then
  77.                 turtle.dig()
  78.                 turtle.forward()
  79.             else
  80.                 if(times<4) then
  81.                     turnLeft(false)
  82.                     local tmp = getDirection(dig, times+1)
  83.                     turnRight(false)
  84.                     return (tmp+1)%4
  85.                 end
  86.                 return false
  87.             end
  88.         end
  89.     end
  90.     local loc2 = vector.new(gps.locate(timeout, false))
  91.     heading = loc2-loc1
  92.     if(not turtle.back()) then
  93.         turtle.turnLeft()
  94.         turtle.turnLeft()
  95.         while(not turtle.forward()) do
  96.             turtle.dig()
  97.             turtle.attack()
  98.         end
  99.         turtle.turnRight()
  100.         turtle.turnRight()
  101.     end
  102.     return ((heading.x + math.abs(heading.x) * 2) + (heading.z + math.abs(heading.z) * 3))-1
  103. end
  104.  
  105. function turnLeft(followDir)
  106.     followDir = followDir ~= false
  107.     turtle.turnLeft()
  108.     if(followDir) then
  109.         direction = (direction-1)%4
  110.     end
  111. end
  112.  
  113. function turnRight(followDir)
  114.     followDir = followDir ~= false
  115.     turtle.turnRight()
  116.     if(followDir) then
  117.         direction = (direction+1)%4
  118.     end
  119. end
  120.  
  121. function turnTo(dir)
  122.     if(dir >= 0 and dir <= 3) then
  123.         local turns = (dir-direction)%4
  124.         if    (turns == 1 or turns == -3) then
  125.             turnRight()
  126.         elseif(turns == 2) then
  127.             turnRight()
  128.             turnRight()
  129.         elseif(turns == -1 or turns == 3) then
  130.             turnLeft()
  131.         elseif(turns == -2) then
  132.             turnLeft()
  133.             turnLeft()
  134.         end
  135.     end
  136.     if(dir ~= direction) then
  137.         print((dir-direction)%4)
  138.         print("Problem in turnTo()")
  139.     end
  140. end
  141.  
  142. function goHome()
  143.     local x,y,z = gps.locate(5, false)
  144.    
  145. --    if(y > safeHeight) then
  146.   --      goDown(y-safeHeight)
  147.     --end
  148.     direction = getDirection(y<=safeHeight)
  149.     print("Got direction: "..direction)
  150.     if(x>homeX) then
  151.         turnTo(0)
  152.     elseif(x<homeX) then
  153.         turnTo(2)
  154.     end
  155.     go(math.abs(x-homeX))
  156.     if(z>homeZ) then
  157.         turnTo(1)
  158.     elseif(z<homeZ) then
  159.         turnTo(3)
  160.     end
  161.     go(math.abs(z-homeZ))
  162.     if(y>homeY) then
  163.         goDown(y-homeY)
  164.     elseif(y<homeY) then
  165.         goUp(homeY-y)
  166.     end
  167.     if(vector.new(x,y,z)-vector.new(homeX,homeY,homeZ) == 0) then
  168.         print("Error in going home equation")
  169.         print("Current position: ("..x..","..y..","..z..")")
  170.     end
  171. end
  172.  
  173. os.sleep(3)
  174. goHome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement