Advertisement
Guest User

Tunnel v2 By Ninetainedo

a guest
Apr 4th, 2013
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 20.37 KB | None | 0 0
  1. -- Tunnel script V2 --
  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("Fill walls : "..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 (turtle.getItemCount(1) < 5) then
  566.     manageInventory()
  567.   else
  568.     return
  569.   end
  570.  
  571.   for i = 1, blockSlotsToKeep do
  572.     turtle.select(i)
  573.     if (turtle.compareTo(1)) then
  574.       countBlock = countBlock + turtle.getItemCount(i)
  575.     end
  576.   end
  577.  
  578.   if (countBlock < 5 and goBackForCobble) then
  579.     tell("Need block !")
  580.     if (moved == 0) then goToPos(-1 * x, -1 * y, -1 * z, -1 * side, "szyx") end
  581.     moved = 1
  582.     os.sleep(10)
  583.     return checkBlock()
  584.   end
  585.   if (moved == 1) then
  586.     tell("Going back to work !")
  587.     goToPos(x, y, z, side, "zyxs")
  588.     moved = 0
  589.   end
  590. end
  591.  
  592. function left()
  593.   turnLeft()
  594.   forward()
  595.   turnRight()
  596. end
  597.  
  598. function forward()
  599.   while (not turtle.forward()) do
  600.     turtle.attack()
  601.   end
  602.   if (side == 0) then
  603.     x = x + 1
  604.   elseif (side == 1) then
  605.     y = y + 1
  606.   elseif (side == 2) then
  607.     x = x - 1
  608.   elseif (side == 3) then
  609.     y = y - 1
  610.   else
  611.     tell("Unknown side found : "..side)
  612.   end
  613.   p = p + 1
  614. end
  615.  
  616. function back()
  617.   if (turtle.back()) then
  618.     if (side == 0) then
  619.       x = x - 1
  620.     elseif (side == 1) then
  621.       y = y - 1
  622.     elseif (side == 2) then
  623.       x = x + 1
  624.     elseif (side == 3) then
  625.       y = y + 1
  626.     else
  627.       tell("Unknown side found : "..side)
  628.     end
  629.   else
  630.     turtle.turnLeft()
  631.     turtle.turnLeft()
  632.     forward()
  633.     turtle.turnRight()
  634.     turtle.turnRight()
  635.   end
  636. end
  637.  
  638. function up()
  639.   if (turtle.up()) then
  640.     z = z + 1
  641.   end
  642.   p = p + 1
  643. end
  644.  
  645. function down()
  646.   if (turtle.down()) then
  647.     z = z - 1
  648.   end
  649. end
  650.  
  651. function turnLeft()
  652.   turtle.turnLeft()
  653.   side = side - 1
  654.   if (side < 0) then
  655.     side = side + 4
  656.   end
  657. end
  658.  
  659. function turnRight()
  660.   turtle.turnRight()
  661.   side = side + 1
  662.   if (side >= 4) then
  663.     side = side - 4
  664.   end
  665. end
  666.  
  667. function right()
  668.   turnRight()
  669.   forward()
  670.   turnLeft()
  671. end
  672.  
  673. function tell(message)
  674.   screenQuery("update", "message", message)
  675.   screenQuery("draw")
  676.   if (sendOnRednet) then
  677.     rednet.broadcast(name.." "..message)
  678.   end
  679. end
  680.  
  681. function goToPos(xTmp, yTmp, zTmp, sideTmp, order)
  682.   local xMove = function()
  683.     while (xTmp ~= 0) do
  684.       if (xTmp > 0) then
  685.         while (not turtle.forward()) do
  686.           turtle.dig()
  687.           turtle.attack()
  688.           os.sleep(0.4)
  689.         end
  690.         xTmp = xTmp - 1
  691.       else
  692.         if (not turtle.back()) then
  693.           turtle.turnLeft()
  694.           turtle.turnLeft()
  695.           while (not turtle.forward()) do
  696.             turtle.dig()
  697.             turtle.attack()
  698.             os.sleep(0.4)
  699.           end
  700.           turtle.turnRight()
  701.           turtle.turnRight()
  702.         end
  703.         xTmp = xTmp + 1
  704.       end
  705.     end
  706.   end
  707.   local yMove = function()
  708.     if (yTmp > 0) then
  709.       turtle.turnRight()
  710.       while (yTmp > 0) do
  711.         while (not turtle.forward()) do
  712.           turtle.dig()
  713.           turtle.attack()
  714.           os.sleep(0.4)
  715.         end
  716.         yTmp = yTmp - 1
  717.       end
  718.       turtle.turnLeft()
  719.     elseif (yTmp < 0) then
  720.       turtle.turnLeft()
  721.       while (yTmp < 0) do
  722.         while (not turtle.forward()) do
  723.           turtle.dig()
  724.           turtle.attack()
  725.           os.sleep(0.4)
  726.         end
  727.         yTmp = yTmp + 1
  728.       end
  729.       turtle.turnRight()
  730.     end
  731.   end
  732.   local zMove = function()
  733.     while (zTmp ~= 0) do
  734.       if (zTmp < 0) then
  735.         while (not turtle.down()) do
  736.           turtle.attackDown()
  737.           turtle.digDown()
  738.           os.sleep(0.4)
  739.         end
  740.         zTmp = zTmp + 1
  741.       else
  742.         while (not turtle.up()) do
  743.           turtle.attackUp()
  744.           turtle.digUp()
  745.           os.sleep(0.4)
  746.         end
  747.         zTmp = zTmp - 1
  748.       end
  749.     end
  750.   end
  751.   local sMove = function()
  752.     while (sideTmp ~= 0) do
  753.       if (sideTmp > 0) then
  754.         turtle.turnRight()
  755.         sideTmp = sideTmp - 1
  756.       else
  757.         turtle.turnLeft()
  758.         sideTmp = sideTmp + 1
  759.       end
  760.     end
  761.   end
  762.  
  763.   for a = 1, 4 do
  764.     if (order:sub(a, a) == "x") then
  765.       xMove()
  766.     elseif (order:sub(a, a) == "y") then
  767.       yMove()
  768.     elseif (order:sub(a, a) == "z") then
  769.       zMove()
  770.     elseif (order:sub(a, a) == "s") then
  771.       sMove()
  772.     end
  773.   end
  774. end
  775.  
  776. -- Point de départ du programme --
  777. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement