Ryusho

Adv Tunnel Fix Unnoficial

Jul 5th, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Tunnel script Unnoficial Fixed 2.1 --
  2. -- By Ninetainedo -- NoFake Dev Team--
  3. -- English Comments, additional comments, and Wall gravel fix by Ryusho, but mostly his friend. --
  4. -- Dig a tunnel and fills walls --
  5.  
  6. args = {...}
  7. local width, height, length = 3, 3, 10
  8. local config = false
  9.  
  10. if (#args == 1 and args[1] == "-c") then
  11.   config = true
  12. elseif #args ~= 1 then
  13.   shell.run("clear")
  14.   print("Usage : tunnel option\n")
  15.   print("Option can be :")
  16.   print("  -c to run in config mode.")
  17.   print("  -d to run in default mode.\n")
  18.   print("In default mode, it makes a 3x3x10 tunnel and fills walls, floor and ceiling.\nIt also empties itself and comes back when it needs blocks.")
  19.   return
  20. end
  21.  
  22. local name = "Tunnel"
  23. local side, x, y, z = 0, 0, 0, 0
  24. local moved = 0
  25. local a, b, c
  26. local blockSlotsToKeep = 8
  27. local slots = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
  28. local goBackForCobble, goBackForEmpty, digOnly, sendOnRednet = true, true, false, true
  29. local fillCeiling, fillFloor, fillLeftWall, fillRightWall, fillBackWall = true, true, true, true, true
  30. local pos = "left"
  31. local percent, oldPercent = 0, -1
  32. local wTerm, hTerm = term.getSize()
  33. local screen = {}
  34. local messages = {}
  35. local percent = 50
  36. local msgLen = math.ceil(hTerm / 2) - 2
  37. local currentMsgIndex = 1
  38. local infos = { blocksDug = 0, blocksPlaced = 0}
  39. local p = 0
  40.  
  41. function main()
  42.   shell.run("clear")
  43.   if (config == true) then
  44.     configMode()
  45.   end
  46.  
  47.   if (sendOnRednet) then
  48.     rednet.open("right")
  49.   end
  50.  
  51.   screenQuery("init")
  52.   tell("Launching !")
  53.   checkBlock()
  54.   a = 0
  55.  
  56.   -- Loop on the Length --
  57.   while (a < length) do
  58.     b = 0
  59.     if (side == 3) then
  60.       turnRight()
  61.     elseif (side == 1) then
  62.       turnLeft()
  63.     end
  64.     if (turtle.detect()) then dig() end
  65.     forward()
  66.    
  67.     -- Loop Vertical Scanning --
  68.     while (b < height) do
  69.       c = 0
  70.       if (side == 0 and pos == "left") then
  71.         turnLeft()
  72.       elseif (side == 0 and pos == "right") then
  73.         turnRight()
  74.       end
  75.       putBlock()
  76.       if (pos == "right") then
  77.         turnLeft()
  78.       elseif (pos == "left") then
  79.         turnRight()
  80.       end
  81.       if (side == 0 and pos == "right") then
  82.         turnLeft()
  83.       elseif (side == 0 and pos == "left") then
  84.         turnRight()
  85.       end
  86.      
  87.       -- Loop Horizontal Scanning --
  88.       while (c < width) do
  89.         if (c == width - 1) then
  90.           putBlock()
  91.         end
  92.         if (z == 0) then
  93.           putBlock("down")
  94.         end
  95.         if (z == height - 1) then
  96.           putBlock("up")
  97.         end
  98.        
  99.         if (not digOnly and fillBackWall and a == length - 1) then
  100.           if (pos == "left") then
  101.             turnLeft()
  102.           else
  103.             turnRight()
  104.           end
  105.           putBlock()
  106.           if (pos == "right") then
  107.             turnLeft()
  108.           else
  109.             turnRight()
  110.           end
  111.         end
  112.        
  113.         if (c < width - 1) then
  114.           if (turtle.detect()) then dig() end
  115.         end
  116.         if (z < height - 1) then
  117.           if (turtle.detectUp()) then dig("up") end
  118.         end
  119.         if (c < width - 1) then
  120.           forward()
  121.         end
  122.         c = c + 1
  123.         checkBlock()
  124.         checkInventory()
  125.         updatePercent()
  126.       end -- While y
  127.       if (z < height - 1) then
  128.         up()
  129.       end
  130.       if (pos == "left") then
  131.         pos = "right"
  132.       else
  133.         pos = "left"
  134.       end
  135.       b = b + 1
  136.     end -- While z
  137.     while (z ~= 0) do down() end
  138.   a = a + 1
  139.   end -- While x
  140.   goToPos(-1 * x, -1 * y, -1 * z, -1 * side, "szyx")
  141.   tell("Done !!")
  142.  
  143.   if (sendOnRednet) then
  144.     rednet.close("right")
  145.   end
  146. end
  147.  
  148. function updatePercent()
  149.   percent = math.floor(reMap(p, 0, length * width * height, 0, 100))
  150.   screenQuery("update", "progress", percent)
  151.   screenQuery("draw")
  152. end
  153.  
  154. function reMap(val, min1, max1, min2, max2)
  155.   local div = max1 - min1
  156.  
  157.   if (div == 0) then div = 1 end
  158.   return ((val - min1) * (max2 - min2) / div + min2)
  159. end
  160.  
  161. function screenQuery(query, ...)
  162.   local tArgs = {...}
  163.   local writeAtPos = function(message, x, y)
  164.     term.setCursorPos(x, y)
  165.     write(message)
  166.   end
  167.   local getCenteredX = function(message)
  168.     return math.ceil((wTerm / 2) - (#message / 2))
  169.   end
  170.   local writeInTable = function(message, x, y)
  171.     if (x + #message > wTerm) then
  172.       return false
  173.     end
  174.     for i = 1, #message do
  175.       screen[y][x + i - 1] = message:sub(i, i)
  176.     end
  177.     return true
  178.   end
  179.   local printProgressBar = function(percent)
  180.     local y = math.ceil(hTerm / 2 * 3 / 2)
  181.     local barPercent = reMap(percent, 0, 100, 4, wTerm - 2)
  182.     local a = 4
  183.  
  184.     screen[y][3] = '['
  185.     screen[y][wTerm - 2] = ']'
  186.     while (a < barPercent) do
  187.       screen[y][a] = '='
  188.       a = a + 1
  189.     end
  190.     writeInTable(tostring(percent).."%", getCenteredX(tostring(percent).."%") + 1, y + 1)
  191.   end
  192.  
  193.   if (query == "init") then
  194.     local mid = math.ceil(hTerm / 2)
  195.     for j = 1, hTerm do
  196.       screen[j] = {}
  197.       for i = 1, wTerm do
  198.         if ((i == 1 or i == wTerm) and (j == 1 or j == hTerm)) then
  199.           screen[j][i] = '+'
  200.         elseif (i == 1 or i == wTerm) then
  201.           screen[j][i] = '|'
  202.         elseif (j == 1 or j == hTerm) then
  203.           screen[j][i] = '-'
  204.         elseif (j == mid) then
  205.           screen[j][i] = '-'
  206.         else
  207.           screen[j][i] = ' '
  208.         end
  209.       end
  210.     end
  211.     printProgressBar(0)
  212.     for j = 1, msgLen do
  213.       messages[j] = ""
  214.     end
  215.     elseif (query == "draw") then
  216.       for j = 1, hTerm do
  217.         for i = 1, wTerm do
  218.           writeAtPos(screen[j][i], i, j)
  219.         end
  220.       end
  221.     elseif (query == "update" and tArgs[1] == "progress") then
  222.       printProgressBar(tArgs[2])
  223.     elseif (query == "update" and tArgs[1] == "message") then
  224.       messages[currentMsgIndex] = tArgs[2]
  225.       for j = 1, msgLen do
  226.         writeInTable(messages[j], 2, j + 1)
  227.       end
  228.       if (currentMsgIndex + 1 > msgLen)then
  229.         table.remove(messages, 1)
  230.         currentMsgIndex = msgLen
  231.       else
  232.         currentMsgIndex = currentMsgIndex + 1
  233.       end
  234.     end
  235. end
  236.  
  237. function checkInventory()
  238.   local count = 1
  239.  
  240.   if (goBackForEmpty and isInventoryFull()) then
  241.     tell("Need empty")
  242.     goToPos(-1 * x, -1 * y, -1 * z, -1 * side, "szyx")
  243.     turtle.turnLeft()
  244.     turtle.turnLeft()
  245.     for i = 2,16 do
  246.       turtle.select(i)
  247.       if (count >= blockSlotsToKeep or not turtle.compareTo(1)) then
  248.         turtle.drop()
  249.       else
  250.         count = count + 1
  251.       end
  252.     end
  253.     turtle.select(1)
  254.     tell("Empty done !")
  255.     turtle.turnRight()
  256.     turtle.turnRight()
  257.     goToPos(x, y, z, side, "zyxs")
  258.   end
  259. end
  260.  
  261. function manageInventory()
  262.   local blockSlots = {}
  263.   local emptySlot = -1
  264.  
  265.   -- Superposition of a maximum of items --
  266.   for i, slot in ipairs(slots) do
  267.     turtle.select(slot)
  268.     if (turtle.getItemCount(slot) > 0) then
  269.       for j, slot2 in ipairs(slots) do
  270.         if (slot2 > slot) then
  271.           if (turtle.getItemSpace(slot) == 0) then
  272.             break
  273.           end
  274.           if (turtle.compareTo(slot2)) then
  275.             turtle.select(slot2)
  276.             turtle.transferTo(slot)
  277.             turtle.select(slot)
  278.           end
  279.         end
  280.       end
  281.     end
  282.   end
  283.  
  284.   -- We put all items uppermost left as possible --
  285.   for i, slot in ipairs(slots) do
  286.     if (turtle.getItemCount(slot) == 0) then
  287.       for j, slot2 in ipairs(slots) do
  288.         if (slot2 > slot and turtle.getItemCount(slot2) > 0) then
  289.           turtle.select(slot2)
  290.           turtle.transferTo(slot)
  291.           turtle.select(slot)
  292.           break
  293.         end
  294.       end
  295.     end
  296.   end
  297.  
  298.   -- Loading information for inventory management filler blocks --
  299.   for i, slot in ipairs(slots) do
  300.     turtle.select(slot)
  301.     if (turtle.compareTo(1)) then
  302.       table.insert(blockSlots, slot)
  303.     end
  304.   end
  305.  
  306.   -- We're looking for an empty slot --
  307.   for i, slot in ipairs(slots) do
  308.     if (slot > blockSlotsToKeep and turtle.getItemCount(slot) == 0) then
  309.       emptySlot = slot
  310.       break
  311.     end
  312.   end
  313.  
  314.   if (emptySlot == -1) then
  315.     return false
  316.   end
  317.   -- It removes the reference table slot --
  318.   table.remove(blockSlots, 1)
  319.   local blockSlotsLen = #blockSlots
  320.  
  321.   -- for each block location it needs to reserve for filler/wall blocks
  322.   -- check if the block is in a different slot ==
  323.   -- --
  324.   -- Ex : If Slot 2 is Empty when blocks to keep is 4
  325.   --it will search for the block in slots 3 to 16
  326.   -- and move the block to slot 2 --
  327.   for i = 2, blockSlotsToKeep do
  328.     turtle.select(i)
  329.     if (not turtle.compareTo(1) and turtle.getItemCount(i) > 0) then
  330.       if (blockSlotsLen > 0) then
  331.         slotTransfer(blockSlots[1], i, emptySlot)
  332.         table.remove(blockSlots, 1)
  333.         blockSlotsLen = blockSlotsLen - 1
  334.       else
  335.         break
  336.       end
  337.     end
  338.   end
  339. end
  340.  
  341. function slotTransfer(slotSrc, slotDest, slotEmpty)
  342.   local hasEmpty = false
  343.  
  344.   if (turtle.getItemCount(slotEmpty) > 0) then
  345.     turtle.select(slotEmpty)
  346.     turtle.drop()
  347.     hasEmpty = true
  348.   end
  349.   if (turtle.getItemCount(slotDest) == 0) then
  350.     turtle.select(slotSrc)
  351.     turtle.transferTo(slotDest)
  352.   else
  353.     turtle.select(slotDest)
  354.     turtle.transferTo(slotEmpty)
  355.     turtle.select(slotSrc)
  356.     turtle.transferTo(slotDest)
  357.     turtle.select(slotEmpty)
  358.     turtle.transferTo(slotSrc)
  359.   end
  360.   if (hasEmpty) then
  361.     turtle.suck()
  362.   end
  363. end
  364.  
  365. function isInventoryFull(var)
  366.   for i, slot in ipairs(slots) do
  367.     if (turtle.getItemCount(slot) == 0) then
  368.       return false
  369.     end
  370.   end
  371.   if (not var) then
  372.     manageInventory()
  373.     return isInventoryFull(true)
  374.   end
  375.   return true
  376. end
  377.  
  378. function putBlock(face)
  379.   turtle.select(1)
  380.  
  381.   if (digOnly) then
  382.     return
  383.   end
  384.  
  385.   if (face == nil) then
  386.     if ((side == 1 and fillRightWall) or (side == 3 and fillLeftWall) or (side == 0 and fillBackWall)) then
  387.         -- While the space here is not the block we want, do what has to in order to clear the space --
  388.         while ( not turtle.compare() ) do
  389.             -- Try and place our block, if that fails, clear the space. This should account for water and lava tiles
  390.             if ( not turtle.place() ) then
  391.                 -- If there is no block in the space, must be an enemy
  392.                 if (not turtle.detect() ) then
  393.                     turtle.attack()
  394.                 -- If we did detect a block, then dig that block --
  395.                 else
  396.                     turtle.dig()
  397.                 end
  398.             end
  399.         end
  400.         -- Increase the blocks placed by 1 for the one we just put down --
  401.         infos.blocksPlaced = infos.blocksPlaced + 1
  402.     end
  403.   elseif (face == "up") then
  404.     if (fillCeiling) then
  405.         -- While the space here is not the block we want, do what has to in order to clear the space --
  406.         while ( not turtle.compareUp() ) do
  407.             -- Try and place our block, if that fails, clear the space. This should account for water and lava tiles
  408.             if ( not turtle.placeUp() ) then
  409.                 -- If there is no block in the space, must be an enemy
  410.                 if (not turtle.detectUp() ) then
  411.                     turtle.attackUp()
  412.                 -- If we did detect a block, then dig that block --
  413.                 else
  414.                     turtle.digUp()
  415.                 end
  416.             end
  417.         end
  418.         -- Increase the blocks placed by 1 for the one we just put down --
  419.         infos.blocksPlaced = infos.blocksPlaced + 1
  420.     end
  421.   elseif (face == "down") then
  422.     if (fillFloor) then
  423.         -- While the space here is not the block we want, do what has to in order to clear the space --
  424.         while ( not turtle.compareDown() ) do
  425.             -- Try and place our block, if that fails, clear the space. This should account for water and lava tiles
  426.             if ( not turtle.placeDown() ) then
  427.                 -- If there is no block in the space, must be an enemy
  428.                 if (not turtle.detectDown() ) then
  429.                     turtle.attackDown()
  430.                 -- If we did detect a block, then dig that block --
  431.                 else
  432.                     turtle.digDown()
  433.                 end
  434.             end
  435.         end
  436.         -- Increase the blocks placed by 1 for the one we just put down --
  437.         infos.blocksPlaced = infos.blocksPlaced + 1
  438.     end
  439.   end
  440. end
  441.  
  442. function readPrompt(prompt, mode, onError, onSuccess, needClear, stricly)
  443.   local value = nil
  444.  
  445.   while (value == nil) do
  446.     if (needClear) then
  447.       shell.run("clear")
  448.     end
  449.     write(prompt)
  450.     value = read()
  451.     if (mode == 1) then
  452.       value = tonumber(value)
  453.       if (value == nil or (stricly and value <= 0)) then
  454.         onError()
  455.         value = nil
  456.       end
  457.     elseif (mode == 2) then
  458.       if (value ~= "y" and value ~= "n") then
  459.         onError()
  460.         value = nil
  461.       end
  462.     end
  463.   end
  464.   onSuccess()
  465.   return value
  466. end
  467.  
  468. function configMode()
  469.   local isOk = false
  470.   local widTmp, heiTmp, lenTmp, tmpBlockSlotsToKeep = nil, nil, nil, nil
  471.   local goBackForCobbleTmp, goBackToEmptyTmp, sendOnRednetTmp = nil, nil, nil
  472.   local fillLeftWallTmp, fillRightWallTmp, fillFloorTmp, fillCeilingTmp, fillBackWallTmp = nil, nil, nil, nil, nil
  473.   local digOnlyTmp = nil
  474.   local timeToWait = 0
  475.   local success = function()
  476.     print("Hum... Ok !")
  477.     os.sleep(timeToWait)
  478.   end
  479.   local numberError = function()
  480.     print("Invalid entry : need a strictly positive number.")
  481.     os.sleep(timeToWait)
  482.   end
  483.   local ynError = function()
  484.     print("I told you to ask y or n.")
  485.     os.sleep(timeToWait)
  486.   end
  487.  
  488.   while (not isOk) do
  489.     widTmp = readPrompt("Tunnel width : ", 1, numberError, success, true, true)
  490.     heiTmp = readPrompt("Tunnel height : ", 1, numberError, success, true, true)
  491.     lenTmp = readPrompt("Tunnel length : ", 1, numberError, success, true, true)
  492.     digOnlyTmp = readPrompt("Run in dig only ? (y/n) ", 2, ynError, success, true)
  493.     if (digOnlyTmp == "n") then
  494.       fillLeftWallTmp = readPrompt("Should I fill the left wall ? (y/n) ", 2, ynError, success, true)
  495.       fillRightWallTmp = readPrompt("Should I fill the right wall ? (y/n) ", 2, ynError, success, true)
  496.       fillBackWallTmp = readPrompt("Should I fill the back wall ? (y/n) ", 2, ynError, success, true)
  497.       fillFloorTmp = readPrompt("Should I fill the floor ? (y/n) ", 2, ynError, success, true)
  498.       fillCeilingTmp = readPrompt("Should I fill the ceiling ? (y/n) ", 2, ynError, success, true)
  499.       if (fillLeftWallTmp == "n" and fillRightWallTmp == "n" and fillBackWallTmp == "n" and fillFloorTmp == "n" and fillCeilingTmp == "n") then
  500.         digOnlyTmp = "y"
  501.       end
  502.     end
  503.     if (digOnlyTmp == "n") then
  504.       tmpBlockSlotsToKeep = readPrompt("How many slots do you want me to keep for filling blocks ? ", 1, numberError, success, true, true)
  505.       goBackForCobbleTmp = readPrompt("Should I go back to start when I need filling blocks ? (y/n) ", 2, ynError, success, true)
  506.     end
  507.     goBackToEmptyTmp = readPrompt("Should I empty when I'm full ? (y/n) ", 2, ynError, success, true)
  508.     sendOnRednetTmp = readPrompt("Should I send infos on rednet ? (y/n) ", 2, ynError, success, true)
  509.     shell.run("clear")
  510.     print("Tunnel w = "..widTmp.." h = "..heiTmp.." l = "..lenTmp)
  511.     print("Dig Only : "..digOnlyTmp)
  512.     if (digOnlyTmp == "n") then
  513.       print("Fill left wall : "..fillLeftWallTmp)
  514.       print("Fill right wall : "..fillRightWallTmp)
  515.       print("Fill back wall : "..fillBackWallTmp)
  516.       print("Fill floor wall : "..fillFloorTmp)
  517.       print("Fill ceiling wall : "..fillCeilingTmp)
  518.       print("Blocks to keep : "..tmpBlockSlotsToKeep)
  519.       print("Back when need blocks : "..goBackForCobbleTmp)
  520.     end
  521.     print("Empty when full : "..goBackToEmptyTmp)
  522.     print("Infos on rednet : "..sendOnRednetTmp)
  523.     print("")
  524.     isOk = readPrompt("Is this ok for you ? (y/n) ", 2, ynError, success, false)
  525.     if (isOk == "y") then isOk = true else isOk = false end
  526.   end
  527.   width = widTmp
  528.   height = heiTmp
  529.   length = lenTmp
  530.   blockSlotsToKeep = tmpBlockSlotsToKeep
  531.   if (digOnlyTmp == "y") then digOnly = true else digOnly = false end
  532.  
  533.   if (fillLeftWallTmp == "y") then fillLeftWall = true else fillLeftWall = false end
  534.   if (fillRightWallTmp == "y") then fillRightWall = true else fillRightWall = false end
  535.   if (fillBackWallTmp == "y") then fillBackWall = true else fillBackWall = false end
  536.   if (fillCeilingTmp == "y") then fillCeiling = true else fillCeiling = false end
  537.   if (fillFloorTmp == "y") then fillFloor = true else fillFloor = false end
  538.  
  539.   if (goBackForCobbleTmp == "y") then goBackForCobble = true else goBackForCobble = false end
  540.   if (goBackToEmptyTmp == "y") then goBackForEmpty = true else goBackForEmpty = false end
  541.   if (sendOnRednetTmp == "y") then sendOnRednet = true else sendOnRednet = false end
  542. end
  543.  
  544. function dig(face)
  545.     if (face == nil) then
  546.         -- Detect if there is a block this direction and keep looking till the space is fully clear --
  547.         while (turtle.detect()) do
  548.             turtle.dig()
  549.             -- Give it some time incase a block falls into place --
  550.             os.sleep(1)
  551.             infos.blocksDug = infos.blocksDug + 1
  552.         end
  553.     elseif (face == "up") then
  554.         -- Detect if there is a block this direction and keep looking till the space is fully clear --
  555.         while (turtle.detectUp()) do
  556.             turtle.digUp()
  557.             -- Give it some time incase a block falls into place --
  558.             os.sleep(0.4)
  559.             infos.blocksDug = infos.blocksDug + 1
  560.         end
  561.     elseif (face == "down") then
  562.         -- Since we are digging down, just dig down whether there is a block or not and carry on --
  563.         turtle.digDown()
  564.         infos.blocksDug = infos.blocksDug + 1
  565.     end
  566. end
  567.  
  568. function checkBlock()
  569.   local countBlock = 0
  570.  
  571.   if (digOnly) then
  572.     return
  573.   end
  574.  
  575.   if (turtle.getItemCount(1) < 5) then
  576.     manageInventory()
  577.   else
  578.     return
  579.   end
  580.  
  581.   for i = 1, blockSlotsToKeep do
  582.     turtle.select(i)
  583.     if (turtle.compareTo(1)) then
  584.       countBlock = countBlock + turtle.getItemCount(i)
  585.     end
  586.   end
  587.  
  588.   if (countBlock < 5 and goBackForCobble) then
  589.     tell("Need block !")
  590.     if (moved == 0) then goToPos(-1 * x, -1 * y, -1 * z, -1 * side, "szyx") end
  591.     moved = 1
  592.     os.sleep(10)
  593.     return checkBlock()
  594.   end
  595.   if (moved == 1) then
  596.     tell("Going back to work !")
  597.     goToPos(x, y, z, side, "zyxs")
  598.     moved = 0
  599.   end
  600. end
  601.  
  602. function left()
  603.   turnLeft()
  604.   forward()
  605.   turnRight()
  606. end
  607.  
  608. function forward()
  609.   while (not turtle.forward()) do
  610.     turtle.attack()
  611.   end
  612.   if (side == 0) then
  613.     x = x + 1
  614.   elseif (side == 1) then
  615.     y = y + 1
  616.   elseif (side == 2) then
  617.     x = x - 1
  618.   elseif (side == 3) then
  619.     y = y - 1
  620.   else
  621.     tell("Unknown side found : "..side)
  622.   end
  623.   p = p + 1
  624. end
  625.  
  626. function back()
  627.   if (turtle.back()) then
  628.     if (side == 0) then
  629.       x = x - 1
  630.     elseif (side == 1) then
  631.       y = y - 1
  632.     elseif (side == 2) then
  633.       x = x + 1
  634.     elseif (side == 3) then
  635.       y = y + 1
  636.     else
  637.       tell("Unknown side found : "..side)
  638.     end
  639.   else
  640.     turtle.turnLeft()
  641.     turtle.turnLeft()
  642.     forward()
  643.     turtle.turnRight()
  644.     turtle.turnRight()
  645.   end
  646. end
  647.  
  648. function up()
  649.   if (turtle.up()) then
  650.     z = z + 1
  651.   end
  652.   p = p + 1
  653. end
  654.  
  655. function down()
  656.   if (turtle.down()) then
  657.     z = z - 1
  658.   end
  659. end
  660.  
  661. function turnLeft()
  662.   turtle.turnLeft()
  663.   side = side - 1
  664.   if (side < 0) then
  665.     side = side + 4
  666.   end
  667. end
  668.  
  669. function turnRight()
  670.   turtle.turnRight()
  671.   side = side + 1
  672.   if (side >= 4) then
  673.     side = side - 4
  674.   end
  675. end
  676.  
  677. function right()
  678.   turnRight()
  679.   forward()
  680.   turnLeft()
  681. end
  682.  
  683. function tell(message)
  684.   screenQuery("update", "message", message)
  685.   screenQuery("draw")
  686.   if (sendOnRednet) then
  687.     rednet.broadcast(name.." "..message)
  688.   end
  689. end
  690.  
  691. function goToPos(xTmp, yTmp, zTmp, sideTmp, order)
  692.   local xMove = function()
  693.     while (xTmp ~= 0) do
  694.       if (xTmp > 0) then
  695.         while (not turtle.forward()) do
  696.           turtle.dig()
  697.           turtle.attack()
  698.           os.sleep(0.4)
  699.         end
  700.         xTmp = xTmp - 1
  701.       else
  702.         if (not turtle.back()) then
  703.           turtle.turnLeft()
  704.           turtle.turnLeft()
  705.           while (not turtle.forward()) do
  706.             turtle.dig()
  707.             turtle.attack()
  708.             os.sleep(0.4)
  709.           end
  710.           turtle.turnRight()
  711.           turtle.turnRight()
  712.         end
  713.         xTmp = xTmp + 1
  714.       end
  715.     end
  716.   end
  717.   local yMove = function()
  718.     if (yTmp > 0) then
  719.       turtle.turnRight()
  720.       while (yTmp > 0) do
  721.         while (not turtle.forward()) do
  722.           turtle.dig()
  723.           turtle.attack()
  724.           os.sleep(0.4)
  725.         end
  726.         yTmp = yTmp - 1
  727.       end
  728.       turtle.turnLeft()
  729.     elseif (yTmp < 0) then
  730.       turtle.turnLeft()
  731.       while (yTmp < 0) do
  732.         while (not turtle.forward()) do
  733.           turtle.dig()
  734.           turtle.attack()
  735.           os.sleep(0.4)
  736.         end
  737.         yTmp = yTmp + 1
  738.       end
  739.       turtle.turnRight()
  740.     end
  741.   end
  742.   local zMove = function()
  743.     while (zTmp ~= 0) do
  744.       if (zTmp < 0) then
  745.         while (not turtle.down()) do
  746.           turtle.attackDown()
  747.           turtle.digDown()
  748.           os.sleep(0.4)
  749.         end
  750.         zTmp = zTmp + 1
  751.       else
  752.         while (not turtle.up()) do
  753.           turtle.attackUp()
  754.           turtle.digUp()
  755.           os.sleep(0.4)
  756.         end
  757.         zTmp = zTmp - 1
  758.       end
  759.     end
  760.   end
  761.   local sMove = function()
  762.     while (sideTmp ~= 0) do
  763.       if (sideTmp > 0) then
  764.         turtle.turnRight()
  765.         sideTmp = sideTmp - 1
  766.       else
  767.         turtle.turnLeft()
  768.         sideTmp = sideTmp + 1
  769.       end
  770.     end
  771.   end
  772.  
  773.   for a = 1, 4 do
  774.     if (order:sub(a, a) == "x") then
  775.       xMove()
  776.     elseif (order:sub(a, a) == "y") then
  777.       yMove()
  778.     elseif (order:sub(a, a) == "z") then
  779.       zMove()
  780.     elseif (order:sub(a, a) == "s") then
  781.       sMove()
  782.     end
  783.   end
  784. end
  785.  
  786. -- move to program start? --
  787. main()
Advertisement
Add Comment
Please, Sign In to add comment