Advertisement
Guest User

Tunnel v2.1 By Ninetainedo

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