fzed51

[CC] Turtle - advTurtle

Aug 25th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. +----------------------------------------------------------------------------+
  3. | advTurtle
  4. | version : 2.6
  5. | Auteur : fzed51
  6. | git : https://github.com/fzed51/CC_Script/blob/master/disk/advTurtle.lua
  7. | pastebin : http://pastebin.com/7mLzefhQ
  8. +----------------------------------------------------------------------------+
  9. | tag : [lua] [MC] [MineCraft] [CC] [ComputerCraft] [Turtle]
  10. | Description :
  11. | bibliotèque de fonction pour la turtle.
  12. +----------------------------------------------------------------------------+
  13. ]]--
  14.  
  15. --[[ Fonction de Debug ]]--
  16. local modeDebug, fileDebug = false, nil
  17. function setDebug(activ, file)
  18.     modeDebug = activ
  19.     fileDebug = file
  20. end
  21. function myDebug( message )
  22.     if modeDebug then
  23.         print ( '|> ', message )
  24.     end
  25.     if fileDebug or false then
  26.         file = io.open(fileDebug ,'a')
  27.         file:write(message.."\r\n")
  28.         file:close()
  29.     end
  30. end
  31. function debugVal( o , niveau)
  32.     niveau = niveau or 0
  33.     local function indent(x)
  34.         local ind = ''
  35.         while x>0 do ind=ind.."\t"; x=x-1 end
  36.         return ind
  37.     end
  38.     local oStr = ''
  39.     if type(o) == "number" then
  40.         oStr = '(n) ' .. tostring(o)
  41.     elseif type(o) == "boolean" then
  42.         oStr = '(b) '
  43.         if o then oStr = oStr .. 'O' else oStr = oStr .. 'N' end
  44.     elseif type(o) == "string" then
  45.         oStr = '(s) ' .. string.format("%q", o)
  46.     elseif type(o) == "table" then
  47.         local first = true
  48.         oStr = "{\n" .. indent(niveau+1)
  49.         for k,v in pairs(o) do
  50.             if first then first = false else  oStr = oStr .. ",\n" .. indent(niveau+1) end
  51.             oStr = oStr .. ' '
  52.             oStr = oStr .. debugVal(k,niveau+1)
  53.             oStr = oStr .. ' = '
  54.             oStr = oStr .. debugVal(v,niveau+1)
  55.         end
  56.         oStr = "\n" .. indent(niveau) .. oStr .. "}"
  57.     else
  58.         oStr = '('..type(o)..')'
  59.     end
  60.     return oStr
  61. end
  62.  
  63. --[[ Paramètes & fonction de l'inventaire ]]--
  64. function itemCount( slot )
  65.     myDebug('')
  66.     local oldSlot, nbItem = activSlot, 0
  67.     select(slot)
  68.     for s = 1,16 do
  69.         if s ~= slot then
  70.             if turtle.compareTo(s) then
  71.                 nbItem = nbItem + turtle.getItemCount(s)
  72.             end
  73.         else
  74.             nbItem = nbItem + turtle.getItemCount(slot)
  75.         end
  76.     end
  77.     select(oldSlot)
  78.     return nbItem
  79. end
  80. item = {
  81.     ['safe'] = {},
  82.     ['add'] = function(nom, slot, minQte)
  83.         if item[slot] ~= nil or item[nom] ~= nil then
  84.             error("Impossible d'enregistrer cet item (" .. tostring(nom)..")!")
  85.         else
  86.             item[slot] = nom
  87.             item[nom] = slot
  88.             item.safe[slot] = minQte
  89.         end
  90.     end,
  91.     ['setup'] = function()
  92.         print('Veuillez compléter l\'inventaire.')
  93.         for s = 1,16 do
  94.             if item[s] ~= nil then
  95.                 print('slot n '..s..' : '..item.safe[s]..' x '..item[s])
  96.             end
  97.         end
  98.         print('Appuyer sur une touche pour continuer.')
  99.         os.pullEvent('key')
  100.     end,
  101.     ['test'] = function()
  102.         local slots, mini, maxi, nbItem = item.safe, true, true, 0
  103.         for slot, qte in ipairs(slots) do
  104.             nbItem = itemCount(slot)
  105.             mini = mini and ( nbItem > 1 )
  106.             maxi = maxi and ( nbItem > qte )
  107.         end
  108.         return mini, maxi
  109.     end
  110. }
  111. local collected = 0
  112. function collect()
  113.     collected = collected + 1
  114.     if math.fmod(collected, 25) == 0 then
  115.         print( collected.." items miné(s)." )
  116.     end
  117. end
  118. local activSlot = 1
  119. function select( slot )
  120.     turtle.select(slot)
  121.     activSlot = slot
  122. end
  123. function trySelect( slot )
  124.     myDebug('trySelect( '..slot..' )')
  125.     local slots = {}
  126.     local nbItem = 0
  127.     select(slot)
  128.     for s = 16, 1, -1 do
  129.         if (s ~= slot) then
  130.             if turtle.compareTo(s) then
  131.                 slots[#slots+1] = s
  132.             end
  133.         end
  134.     end
  135.     slots[#slots+1] = slot 
  136.     if slots[1] ~= slot or turtle.getItemCount(slot) > 1 then
  137.         select(slots[1])
  138.         return true
  139.     else
  140.         print("plus de ".. (item[slot] or slot) )
  141.         return false
  142.     end
  143. end
  144. function inventaireIsFull()
  145.     myDebug('inventaireIsFull()')
  146.     local slotVide = 16
  147.     for slot = 1,16 do
  148.         if turtle.getItemCount(slot) > 0 then
  149.             slotVide = slotVide - 1
  150.         end
  151.     end
  152.     return not (slotVide > 0)
  153. end
  154. function rangeInventaire()
  155.     myDebug('rangeInventaire()')
  156.     local oldSlot, selectSlot, lastSlot = activSlot, 1, 16
  157.     while selectSlot < lastSlot do
  158.         if turtle.getItemCount(selectSlot) > 0 then
  159.             select(selectSlot)         
  160.             local espaceLibre = turtle.getItemSpace(selectSlot)
  161.             local compareSlot = 16
  162.             while espaceLibre > 0 and compareSlot > selectSlot do
  163.                 if turtle.compareTo(compareSlot) then
  164.                     select(compareSlot)
  165.                     turtle.transferTo(selectSlot, math.min(espaceLibre, turtle.getItemCount(compareSlot)))
  166.                     select(selectSlot)
  167.                     espaceLibre = turtle.getItemSpace(selectSlot)
  168.                 end
  169.                 compareSlot = compareSlot - 1
  170.             end
  171.         end
  172.         while turtle.getItemCount(lastSlot) == 0 do
  173.             lastSlot = lastSlot - 1
  174.         end
  175.         selectSlot = selectSlot + 1
  176.     end
  177.     select(oldSlot)
  178. end
  179.  
  180. --[[ fonction d'utilisation de l'inventaire ]]--
  181. function tryPlace( slot )
  182.     myDebug('tryPlace( '..slot..' )')
  183.     if not turtle.detect() and  trySelect(slot) then
  184.         return turtle.place()
  185.     else
  186.         return false
  187.     end
  188. end
  189. function tryPlaceUp( slot )
  190.     myDebug('tryPlaceUp( '..slot..' )')
  191.     if not turtle.detectUp() and trySelect(slot) then
  192.         return turtle.placeUp()
  193.     else
  194.         return false
  195.     end
  196. end
  197. function tryPlaceDown( slot )
  198.     myDebug('tryPlaceDown( '..slot..' )')
  199.     if not turtle.detectDown() and trySelect(slot) then
  200.         return turtle.placeDown()
  201.     else
  202.         return false
  203.     end
  204. end
  205. function drop( slot )
  206.     myDebug('drop()')
  207. end
  208. function tryDropAll()
  209.     myDebug('tryDropAll()')
  210. end
  211.  
  212. --[[ fonctions de minage ]]--
  213. function tryDig()
  214.     myDebug('tryDig()')
  215.     while turtle.detect() do
  216.         if turtle.dig() then
  217.             collect()
  218.             sleep(0.5)
  219.         else
  220.             return false
  221.         end
  222.     end
  223.     return true
  224. end
  225. function tryDigUp()
  226.     myDebug('tryDigUp()')
  227.     while turtle.detectUp() do
  228.         if turtle.digUp() then
  229.             collect()
  230.             sleep(0.5)
  231.         else
  232.             return false
  233.         end
  234.     end
  235.     return true
  236. end
  237. function tryDigDown()
  238.     myDebug('tryDigDown()')
  239.     while turtle.detectDown() do
  240.         if turtle.digDown() then
  241.             turtle.suckDown()
  242.             collect()
  243.             sleep(0.5)
  244.         else
  245.             return false
  246.         end
  247.     end
  248.     return true
  249. end
  250. function DigAround(materials, reverse)
  251.     myDebug('DigAround('..tostring(materials)..', '..tostring(reverse)..')')
  252.     reverse = reverse or false
  253.     local function multiCompare(dir, materials, reverse)
  254.         local compareAction, detectAction = nil, nil
  255.         if dir == 'up' then
  256.             compareAction = turtle.compareUp
  257.             detectAction = turtle.detectUp
  258.         elseif dir == 'down' then
  259.             compareAction = turtle.compareDown
  260.             detectAction = turtle.detectDown
  261.         elseif dir == 'front' then
  262.             compareAction = turtle.compare
  263.             detectAction = turtle.detect
  264.         else
  265.             errot ('direction incorrecte')
  266.         end
  267.         local compareTrue = false
  268.         for material = 1, #materials do
  269.             if material == nil then
  270.                 compareTrue = compareTrue or compareAction()
  271.             else
  272.                 compareTrue = compareTrue or compareAction(material)
  273.             end
  274.         end
  275.         if reverse then compareTrue =  not compareTrue end
  276.         return compareTrue
  277.     end
  278.     local function _Forward()
  279.         if multicompare('front', materials, reverse) then
  280.             tryForward()
  281.             DigAround(materials, reverse)
  282.         else
  283.             tryForward()
  284.         end
  285.     end
  286.     local function _Up()
  287.         if multicompare('up', materials, reverse) then
  288.             tryUp()
  289.             DigAround(materials, reverse)
  290.         else
  291.             tryUp()
  292.         end
  293.     end
  294.     local function _Down()
  295.         if multicompare('down', materials, reverse) then
  296.             tryDown()
  297.             DigAround(materials, reverse)
  298.         else
  299.             tryDown()
  300.         end
  301.     end
  302.     local function _Level()
  303.         for _ = 1,4 do
  304.             _Forward()
  305.             turnRight()
  306.             _Forward()
  307.         end
  308.     end
  309.     _Up()
  310.     _Forward()
  311.     turnRight()
  312.     _Level()
  313.     _Down()
  314.     _Level()
  315.     _Down()
  316.     _Level()
  317.     turnRight()
  318.     _Forward()
  319.     _Up()
  320.     turnBack()
  321. end
  322.  
  323. --[[ fonctions dedéplacement ]]--
  324. local fuel = {}
  325. function setFuelItem(newFuel)
  326.     myDebug('setFuelItem('..newFuel..')')
  327.     fuel[#fuel + 1] = newFuel
  328. end
  329. function refuel()
  330.     myDebug('refuel()')
  331.     local fuelLevel = turtle.getFuelLevel()
  332.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  333.         return
  334.     end
  335.    
  336.     local function tryRefuel()
  337.         local oldSlot = activSlot
  338.         local function rf(slot)
  339.             if trySelect(slot) then
  340.                 if turtle.refuel(1) then
  341.                     trySelect(oldSlot)
  342.                     return true
  343.                 end
  344.             else
  345.                 trySelect(oldSlot)
  346.                 return false
  347.             end
  348.         end
  349.         if #fuel > 0 then
  350.             for s = 1, #fuel do
  351.                 if rf(fuel[s]) then return true end
  352.             end
  353.         else
  354.             for s = 1,16 do
  355.                 if rf(s) then return true end
  356.             end
  357.         end
  358.         return false
  359.     end
  360.    
  361.     if not tryRefuel() then
  362.         print( "Ajout plus de fuel pour continuer." )
  363.         while not tryRefuel() do
  364.             sleep(1)
  365.         end
  366.         print( "Retour au tunnel." )
  367.     end
  368. end
  369. function tryUp(autoDig)
  370.     if autoDig == nil then autoDig = true end
  371.     myDebug('tryUp()')
  372.     refuel()
  373.     local try = 20
  374.     while not turtle.up() do
  375.         if (not autoDig) and try < 0 then return false end
  376.         if autoDig and turtle.detectUp() then
  377.             if not tryDigUp() then
  378.                 return false
  379.             end
  380.         elseif turtle.attackUp() then
  381.             collect()
  382.             try=try-1
  383.         else
  384.             sleep( 0.5 )
  385.             try=try-1
  386.         end
  387.     end
  388.     return true
  389. end
  390. function tryDown(autoDig)
  391.     if autoDig == nil then autoDig = true end
  392.     myDebug('tryDown()')
  393.     refuel()
  394.     local try = 20
  395.     while not turtle.down() do
  396.         if (not autoDig) and try < 0 then return false end
  397.         if autoDig and turtle.detectDown() then
  398.             if not tryDigDown() then
  399.                 return false
  400.             end
  401.         elseif turtle.attackDown() then
  402.             collect()
  403.             try=try-1
  404.         else
  405.             sleep( 0.5 )
  406.             try=try-1
  407.         end
  408.     end
  409.     return true
  410. end
  411. function tryForward(autoDig)
  412.     if autoDig == nil then autoDig = true end
  413.     myDebug('tryForward()')
  414.     refuel()
  415.     local try = 20
  416.     while not turtle.forward() do
  417.         if (not autoDig) and try < 0 then return false end
  418.         if autoDig and turtle.detect() then
  419.             if not tryDig() then
  420.                 return false
  421.             end
  422.         elseif turtle.attack() then
  423.             collect()
  424.             try=try-1
  425.         else
  426.             sleep( 0.5 )
  427.             try=try-1
  428.         end
  429.     end
  430.     return true
  431. end
  432. function turnLeft()
  433.     myDebug('turnLeft()')
  434.     turtle.turnLeft()
  435. end
  436. function turnRight()
  437.     myDebug('turnRight()')
  438.     turtle.turnRight()
  439. end
  440. function turnBack()
  441.     myDebug('turnBack()')
  442.     turtle.turnLeft()
  443.     turtle.turnLeft()
  444. end
Advertisement
Add Comment
Please, Sign In to add comment