Advertisement
macsoares

goto (computercraft turtle program)

May 23rd, 2014
1,964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.66 KB | None | 0 0
  1. rednet.open("right")
  2.  
  3. tArgs = { ... }
  4.  
  5. local goTo =  {}
  6. local facing
  7. local blocksv
  8. local blocks
  9.  
  10. goTo.x = tonumber(tArgs[1])
  11. goTo.y = tonumber(tArgs[2]) or 64
  12. goTo.z = tonumber(tArgs[3])
  13.  
  14. local function turn(_side,_n)  --- function to turn
  15.     if _side == "left" then
  16.         for i=1, _n do
  17.             turtle.turnLeft()
  18.         end
  19.     elseif _side == "right" then
  20.         for i=1, _n do
  21.             turtle.turnRight()
  22.         end
  23.     end
  24. end
  25.  
  26. local function ensureWalking()  --- make sure that the turtle walked
  27.     if turtle.forward() then  --- ensure that the turtle walked
  28.         blocks = blocks - 1
  29.         return true
  30.     else
  31.         turtle.attack()
  32.         ensureWalking()
  33.     end
  34. end
  35.  
  36. local function ensureVertical(_a) --- call like ensureVertical{direction = "up"}
  37.     if _a == "up" then 
  38.         if turtle.up() then  --- ensure that the turtle walked
  39.             blocksv = blocksv - 1
  40.             return true
  41.         else
  42.             turtle.attackUp()
  43.             ensureVertical(_a)
  44.         end
  45.     elseif _a == "down" then
  46.         if turtle.down() then  --- ensure that the turtle walked
  47.             blocksv = blocksv - 1
  48.             return true
  49.         else
  50.             turtle.attackDown()
  51.             ensureVertical(_a)
  52.         end
  53.     end
  54. end
  55.  
  56. local function digAndWalk(_a) --- function to dig, walk and replace blocks, call like digAndWalk{times = 1}
  57.     if _a == nil then _a = 1 end
  58.     blocks = _a or 1
  59.     turtle.select(4)
  60.     turtle.dropUp()
  61.     while blocks > 0 do
  62.         while turtle.detect() do
  63.             turtle.dig()
  64.             if turtle.getItemCount(4) < 1 then  --  making sure it is not lava
  65.                 break
  66.             end
  67.         end
  68.         ensureWalking()
  69.     end
  70. end
  71.  
  72. local function getFacing()
  73.     local cx, cy, cz = gps.locate() -- get the current position
  74.     digAndWalk()
  75.     local nx, ny, nz = gps.locate() -- get the new position
  76.     if nx > cx then
  77.         facing = "east"
  78.     elseif nx < cx then
  79.         facing = "west"
  80.     elseif nz > cz then
  81.         facing = "south"
  82.     elseif nz < cz then
  83.         facing = "north"
  84.     end
  85.     print(facing)
  86. end
  87.  
  88. local function facingNorth()
  89.     if facing == "north" then
  90.         return true
  91.     elseif facing == "east" then
  92.         facing = "north"
  93.         turn("left",1)
  94.         return true
  95.     elseif facing == "south" then
  96.         facing = "north"
  97.         turn("right",2)
  98.         return true
  99.     elseif facing == "west" then
  100.         facing = "north"
  101.         turn("right",1)
  102.         return true
  103.     else
  104.         return false
  105.     end
  106. end
  107.  
  108. local function pickItem(_from,_to,_number)  -- function to use enderchest of illing cabinet as storage for fuel, and also pick it
  109.     turtle.select(7)
  110.     turtle.digDown()
  111.     turtle.select(_from)
  112.     turtle.placeDown()
  113.     turtle.select(_to)
  114.     turtle.suckDown(_number)
  115.     turtle.select(_from)
  116.     turtle.digDown()
  117.     turtle.select(7)
  118.     turtle.placeDown() 
  119. end
  120.  
  121. local function checkFuel()
  122.     local cx, cy, cz = gps.locate() --- yes again!
  123.     local fuelNeeded = math.ceil(math.abs(cx - goTo.x) + math.abs(cz - goTo.z) + math.abs(cy - goTo.y))
  124.     local coalNeeded = math.ceil(fuelNeeded/80 - turtle.getFuelLevel())
  125.     if coalNeeded > 64 then
  126.         print("Put "..tostring(coalNeeded).." coal in the first slot")
  127.         return false
  128.     elseif coalNeeded <= 0 then
  129.         return true
  130.     elseif coalNeeded > 0 and turtle.getItemCount(1) == coalNeeded then
  131.         turtle.select(1)
  132.         turtle.refuel(coalNeeded)
  133.         return true
  134.     elseif coalNeeded > 0 and turtle.getItemCount(16) == 1 then
  135.         pickItem(16,1,coalNeeded)
  136.         turtle.select(1)
  137.         turtle.refuel(coalNeeded)
  138.         return true
  139.     else
  140.         print("Put "..tostring(coalNeeded).." coal in the first slot")
  141.         return false
  142.     end
  143. end
  144.  
  145. local function digVertical()
  146.     local arg = {}
  147.     local cx, cy, cz = gps.locate()
  148.     turtle.select(4)
  149.     turtle.dropUp()
  150.     if cy > goTo.y then
  151.         blocksv = cy - goTo.y
  152.         while blocksv > 0 do
  153.             while turtle.detectDown() do
  154.                 turtle.digDown()
  155.                 if turtle.getItemCount(4) < 1 then  --  making sure it is not lava
  156.                     break
  157.                 end
  158.             end
  159.             ensureVertical("down")
  160.         end
  161.     elseif cy < goTo.y then
  162.         blocksv = goTo.y - cy
  163.         while blocksv > 0 do
  164.             while turtle.detectUp() do
  165.                 turtle.digUp()
  166.                 if turtle.getItemCount(4) < 1 then  --  making sure it is not lava
  167.                     break
  168.                 end
  169.             end
  170.             ensureVertical("up")
  171.         end
  172.     end
  173. end
  174.  
  175. local function walk()
  176.     local cx, cy, cz = gps.locate()
  177.     if facingNorth() then
  178.         digVertical()
  179.         if goTo.z > cz then
  180.             turn("right", 2)
  181.             digAndWalk(math.abs(goTo.z - cz))
  182.             if goTo.x > cx then
  183.                 turn("left",1)
  184.                 digAndWalk(math.abs(goTo.x - cx))
  185.             elseif goTo.x < cx then
  186.                 turn("right", 1)
  187.                 digAndWalk(math.abs(goTo.x - cx))
  188.             end
  189.         elseif goTo.z < cz then
  190.             digAndWalk(math.abs(goTo.z - cz))
  191.             if goTo.x > cx then
  192.                 turn("right",1)
  193.                 digAndWalk(math.abs(goTo.x - cx))
  194.             elseif goTo.x < cx then
  195.                 turn("left",1)
  196.                 digAndWalk(math.abs(goTo.x - cx))
  197.             end
  198.         elseif goTo.z == cz then  --- the z coordinates are the same!
  199.             if goTo.x > cx then
  200.                 turn("right",1)
  201.                 digAndWalk(math.abs(goTo.x - cx))
  202.             elseif goTo.x < cx then
  203.                 turn("left",1)
  204.                 digAndWalk(math.abs(goTo.x - cx))
  205.             end
  206.         end
  207.     end
  208. end
  209.  
  210. local function writeStr(arg) -- writeStr{color = colors.white, bgColor = colors.black, str = "Hi"}
  211.     local color = arg.color or colors.white
  212.     local str = arg.str
  213.     local bgColor = arg.bgColor or colors.black
  214.     term.setTextColor(color)
  215.     term.setBackgroundColor(bgColor)
  216.     print(str)
  217.     term.setTextColor(colors.white)
  218.     term.setBackgroundColor(colors.black)
  219. end
  220.  
  221. local function checkArgs()
  222.     if #tArgs < 3 or #tArgs > 3 then
  223.         writeStr{color = colors.green, str = "Goto usages:"}
  224.         writeStr{color = colors.green, str = "goto <x> <y> <z>"}
  225.         return false
  226.     elseif tonumber(tArgs[1]) and tonumber(tArgs[2]) and tonumber(tArgs[3]) then
  227.         return true
  228.     else
  229.         writeStr{color = colors.green, str = "Goto usages:"}
  230.         writeStr{color = colors.green, str = "goto <x> <y> <z>"}
  231.         return false
  232.     end
  233. end
  234.  
  235. --- MAIN ACTION ---
  236.  
  237. if checkArgs() and checkFuel() then
  238.     getFacing()
  239.     facingNorth()
  240.     walk()
  241. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement