IMarvinTPA

Turtle Block Library

Feb 18th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.18 KB | None | 0 0
  1. if not turtle then
  2.     error("Requires turtle api")
  3. end
  4.  
  5. function fixSide(aSide)
  6.     if aSide == nil then
  7.         aSide = ""
  8.     end
  9.     aSide = string.lower(aSide)
  10.     if aSide == "top" or aSide == "t" or aSide == "up" or aSide == "u"then
  11.         aSide = "t"
  12.     elseif aSide == "front" or aSide == "f" then
  13.         aSide = "f"
  14.     elseif aSide == "bottom" or aSide == "b" or aSide == "down" or aSide == "d" then
  15.         aSide = "b"
  16.     elseif aSide == "none" or aSide == "n" then
  17.         aSide = "n"
  18.     elseif aSide == "rtop" or aSide == "rt" or aSide == "rup" or aSide == "ru"then
  19.         aSide = "rt"
  20.     elseif aSide == "rfront" or aSide == "rf" then
  21.         aSide = "rf"
  22.     elseif aSide == "rbottom" or aSide == "rb" or aSide == "rdown" or aSide == "rd" then
  23.         aSide = "rd"
  24.     elseif aSide == "rright" or aSide == "rr" then
  25.         aSide = "rr"
  26.     elseif aSide == "rleft" or aSide == "rl" then
  27.         aSide = "rl"
  28.     elseif aSide == "rback" or aSide == "rbk" then
  29.         aSide = "rb"
  30.     --attack options
  31.     elseif aSide == "atop" or aSide == "at" or aSide == "aup" or aSide == "au"then
  32.         aSide = "at"
  33.     elseif aSide == "afront" or aSide == "af" then
  34.         aSide = "af"
  35.     elseif aSide == "abottom" or aSide == "ab" or aSide == "adown" or aSide == "ad" then
  36.         aSide = "ad"
  37.     --dig options
  38.     elseif aSide == "dtop" or aSide == "dt" or aSide == "dup" or aSide == "du"then
  39.         aSide = "dt"
  40.     elseif aSide == "dfront" or aSide == "df" then
  41.         aSide = "df"
  42.     elseif aSide == "dbottom" or aSide == "db" or aSide == "ddown" or aSide == "dd" then
  43.         aSide = "dd"
  44.     --place options
  45.     elseif aSide == "ptop" or aSide == "pt" or aSide == "pup" or aSide == "pu"then
  46.         aSide = "pt"
  47.     elseif aSide == "pfront" or aSide == "pf" then
  48.         aSide = "pf"
  49.     elseif aSide == "pbottom" or aSide == "pb" or aSide == "pdown" or aSide == "pd" then
  50.         aSide = "pd"
  51.     --craft options
  52.     elseif aSide == "ctop" or aSide == "ct" or aSide == "cup" or aSide == "cu"then
  53.         aSide = "ct"
  54.     elseif aSide == "cfront" or aSide == "cf" then
  55.         aSide = "cf"
  56.     elseif aSide == "cbottom" or aSide == "cb" or aSide == "cdown" or aSide == "cd" then
  57.         aSide = "cd"
  58.     else
  59.         aSide = nil
  60.     end
  61.     return aSide
  62. end
  63.  
  64. function displaySide(aSide)
  65.     if aSide == nil then
  66.         aSide = ""
  67.     end
  68.     aSide = string.lower(aSide)
  69.     if aSide == "t" then
  70.         aSide = "top"
  71.     elseif aSide == "f" then
  72.         aSide = "front"
  73.     elseif aSide == "b" then
  74.         aSide = "bottom"
  75.     elseif aSide == "n" then
  76.         aSide = "none"
  77.     elseif aSide == "rt" then
  78.         aSide = "redstone pulse top"
  79.     elseif aSide == "rf" then
  80.         aSide = "redstone pulse front"
  81.     elseif aSide == "rd" then
  82.         aSide = "redstone pulse down"
  83.     elseif aSide == "rr" then
  84.         aSide = "redstone pulse right"
  85.     elseif aSide == "rl" then
  86.         aSide = "redstone pulse left"
  87.     elseif aSide == "rb" then
  88.         aSide = "redstone pulse back"
  89.  
  90.     elseif aSide == "pt" then
  91.         aSide = "place top"
  92.     elseif aSide == "pf" then
  93.         aSide = "place front"
  94.     elseif aSide == "pd" then
  95.         aSide = "place down"
  96.  
  97.     elseif aSide == "dt" then
  98.         aSide = "dig top"
  99.     elseif aSide == "df" then
  100.         aSide = "dig front"
  101.     elseif aSide == "dd" then
  102.         aSide = "dig down"
  103.  
  104.     elseif aSide == "ct" then
  105.         aSide = "craft top"
  106.     elseif aSide == "cf" then
  107.         aSide = "craft front"
  108.     elseif aSide == "cd" then
  109.         aSide = "craft down"
  110.  
  111.     else
  112.         aSide = "unknown (" .. aSide .. ")"
  113.     end
  114.     return aSide
  115. end
  116.  
  117. function isEmpty()
  118.     local bFull = true
  119.     local nTotalItems = 0
  120.     for n=1,16 do
  121.         local nCount = turtle.getItemCount(n)
  122.         if nCount == 0 then
  123.                 bFull = false
  124.         end
  125.         nTotalItems = nTotalItems + nCount
  126.     end
  127.          
  128.     if nCount == 0 then
  129.         --print( "No more Items to drop." )
  130.         return true
  131.     end
  132.     return false
  133. end
  134.  
  135. function flashSide(aSide)
  136.     --print("Flash " .. aSide .. " On ")
  137.     redstone.setOutput(aSide, true)
  138.     sleep(0.2)
  139.     --print("Flash " .. aSide .. " Off ")
  140.     redstone.setOutput(aSide, false)
  141.     sleep(0.2)
  142. end
  143.  
  144. function loadSlot(slotNo, sDir)
  145.  
  146.     if sDir == "n" then
  147.         return true
  148.     end
  149.  
  150.     if sDir == "t" then
  151.         res = turtle.suckUp()
  152.     elseif sDir == "f" then
  153.         res = turtle.suck()
  154.     elseif sDir == "b" then
  155.         res = turtle.suckDown()
  156.     elseif sDir == "rt" then
  157.         turtle.select(slotNo)
  158.         flashSide("top")
  159.         res = (turtle.getItemCount(slotNo) ~= 0)
  160.     elseif sDir == "rd" then
  161.         turtle.select(slotNo)
  162.         flashSide("bottom")
  163.         res = (turtle.getItemCount(slotNo) ~= 0)
  164.     elseif sDir == "rf" then
  165.         turtle.select(slotNo)
  166.         flashSide("front")
  167.         res = (turtle.getItemCount(slotNo) ~= 0)
  168.     elseif sDir == "rb" then
  169.         turtle.select(slotNo)
  170.         flashSide("back")
  171.         res = (turtle.getItemCount(slotNo) ~= 0)
  172.     elseif sDir == "rl" then
  173.         turtle.select(slotNo)
  174.         flashSide("left")
  175.         res = (turtle.getItemCount(slotNo) ~= 0)
  176.     elseif sDir == "rf" then
  177.         turtle.select(slotNo)
  178.         flashSide("right")
  179.         res = (turtle.getItemCount(slotNo) ~= 0)
  180.     --Attack Options
  181.     elseif sDir == "at" then
  182.         turtle.select(slotNo)
  183.         res = turtle.attackUp()
  184.     elseif sDir == "ad" then
  185.         turtle.select(slotNo)
  186.         res = turtle.attackDown()
  187.     elseif sDir == "af" then
  188.         turtle.select(slotNo)
  189.         res = turtle.attack()
  190.     --Dig Options
  191.     elseif sDir == "dt" then
  192.         turtle.select(slotNo)
  193.         res = turtle.digUp()
  194.     elseif sDir == "dd" then
  195.         turtle.select(slotNo)
  196.         res = turtle.digDown()
  197.     elseif sDir == "df" then
  198.         turtle.select(slotNo)
  199.         res = turtle.dig()
  200.     elseif sDir == "cf" or sDir == "ct" or sDir == "cd" then
  201.         turtle.select(slotNo)
  202.         local nCount = turtle.getItemCount(slotNo)
  203.         --print( "Slot " .. slotNo .. " has " .. nCount .. " items." )
  204.         if nCount == 1 and slotNo ~= 4 then
  205.             if sDir == "ct" then
  206.                 res = turtle.suckUp(1)
  207.             elseif sDir == "cf" then
  208.                 res = turtle.suck(1)
  209.             elseif sDir == "cd" then
  210.                 res = turtle.suckDown(1)
  211.             end
  212.             --print( "Sucked: " .. res)
  213.         end
  214.     end
  215.    
  216.     return res
  217. end
  218.  
  219. function unloadSlot(slotNo, sDir)
  220.     local res
  221.     if sDir == "n" then
  222.         return true
  223.     end
  224.     --print( "Unloading items..." )
  225.    
  226.     local res = false
  227.  
  228.     if slotNo == 4 and (sDir == "cf" or sDir == "ct" or sDir == "cd" ) then
  229.         local good = true
  230.         --print( "Crafting Slot: " .. turtle.getItemCount(slotNo))
  231.         if (turtle.getItemCount(slotNo) == 0) then
  232.             for rowI = 0, 2 do
  233.                 for colI = 1,3 do
  234.                     if (turtle.getItemCount(rowI * 4 + colI) == 1) then
  235.                         good = false
  236.                     end
  237.                 end
  238.             end
  239.             --print( "Ready to craft: " .. good)
  240.             if good then
  241.                 turtle.select(slotNo)
  242.                 turtle.craft(1)
  243.             end
  244.         end
  245.         if sDir == "ct" then
  246.             res = turtle.dropUp()
  247.         elseif sDir == "cf" then
  248.             res = turtle.drop()
  249.         elseif sDir == "cd" then
  250.             res = turtle.dropDown()
  251.         end    
  252.         return res
  253.     elseif (sDir == "cf" or sDir == "ct" or sDir == "cd" ) then
  254.         return true
  255.     end
  256.    
  257.     if (turtle.getItemCount(slotNo) > 0) then
  258.         turtle.select(slotNo)  
  259.         if sDir == "t" then
  260.             res = turtle.dropUp()
  261.         elseif sDir == "f" then
  262.             res = turtle.drop()
  263.         elseif sDir == "b" then
  264.             res = turtle.dropDown()
  265.         elseif sDir == "rt" then
  266.             flashSide("top")
  267.             res = (turtle.getItemCount(slotNo) == 0)
  268.         elseif sDir == "rd" then
  269.             flashSide("bottom")
  270.             res = (turtle.getItemCount(slotNo) == 0)
  271.         elseif sDir == "rf" then
  272.             flashSide("front")
  273.             res = (turtle.getItemCount(slotNo) == 0)
  274.         elseif sDir == "rb" then
  275.             flashSide("back")
  276.             res = (turtle.getItemCount(slotNo) == 0)
  277.         elseif sDir == "rl" then
  278.             flashSide("left")
  279.             res = (turtle.getItemCount(slotNo) == 0)
  280.         elseif sDir == "rf" then
  281.             flashSide("right")
  282.             res = (turtle.getItemCount(slotNo) == 0)
  283.         --Place Options
  284.         elseif sDir == "pt"then        
  285.             res = turtle.placeUp()
  286.         elseif sDir == "pd" then
  287.             res = turtle.placeDown()
  288.         elseif sDir == "pf" then
  289.             res = turtle.place()
  290.         end
  291.         return res
  292.     else
  293.         return true
  294.     end
  295. end
  296.  
  297. function readSide(sDir)
  298.    
  299.     if sDir == "n" then
  300.         return false
  301.     end
  302.  
  303.     if sDir == "rt" or sDir == "t" or sDir == "ct" then
  304.         res = redstone.getInput("top")
  305.     elseif sDir == "rd" or sDir == "b" or sDir == "cd" then
  306.         res = redstone.getInput("bottom")
  307.     elseif sDir == "rf" or sDir == "f" or sDir == "cf" then
  308.         res = redstone.getInput("front")
  309.     elseif sDir == "rb" then
  310.         res = redstone.getInput("back")
  311.     elseif sDir == "rl" then
  312.         res = redstone.getInput("left")
  313.     elseif sDir == "rf" then
  314.         res = redstone.getInput("right")
  315.     --Attack Options
  316.     elseif sDir == "at" then
  317.         res = turtle.detectUp() ~= true
  318.     elseif sDir == "ad" then
  319.         res = turtle.detectDown() ~= true
  320.     elseif sDir == "af" then
  321.         res = turtle.detect() ~= true
  322.     --Dig Options
  323.     elseif sDir == "dt" then
  324.         res = turtle.detectUp()
  325.     elseif sDir == "dd" then
  326.         res = turtle.detectDown()
  327.     elseif sDir == "df" then
  328.         res = turtle.detect()
  329.     end
  330.     return res
  331. end
  332.  
  333. function emptySlot()
  334.     local n2
  335.     for n2 = 1, 16 do
  336.         if (turtle.getItemCount(n2) == 0) then
  337.             return n2
  338.         end
  339.     end
  340.     return 0
  341. end
  342.  
  343.  
  344. function unload(sOutput, sOverflow)
  345.     if sOutput == "n" then
  346.         return
  347.     end
  348.     if sOverflow == nil then
  349.         sOverflow = "n"
  350.     end
  351.     --print( "Unloading items..." )
  352.    
  353.     local res = false
  354.  
  355.     for n = 1, 16 do
  356.         --unloaded = unloaded + turtle.getItemCount(n)
  357.         if (turtle.getItemCount(n) > 0) or sOutput == "cf" or sOutput == "ct" or sOutput == "cd" then
  358.             turtle.select(n)   
  359.            
  360.             if unloadSlot(n, sOutput) == false and sOverflow ~= "n" then
  361.                 --the router is full, we have to pull the item out and ditch it.
  362.                 print("Destination Stuffed")
  363.                 local cacheSlot = emptySlot()
  364.                 print("Empty Slot at: " .. cacheSlot)
  365.                 if cacheSlot > 0 then
  366.                     turtle.select(cacheSlot)
  367.                     res = loadSlot(cacheSlot, sOutput)
  368.                    
  369.                     print("Pulling result: " .. YesNoOther(res))
  370.                     res = unloadSlot(cacheSlot, sOverflow)
  371.                     print("Overflow Output result: " .. YesNoOther(res))
  372.                     res = unloadSlot(n, sOutput)
  373.                     print("Retry result: " .. YesNoOther(res))
  374.                 end            
  375.             end
  376.             sleep(0.2)
  377.         end
  378.     end    
  379. end
  380.  
  381. function load(sInput, minSlot, maxSlot)
  382.     if sInput == "n" then
  383.         return
  384.     end
  385.     if minSlot == nil then
  386.         minSlot = 1
  387.     end
  388.     if maxSlot == nil then
  389.         maxSlot = 15
  390.     end
  391.  
  392.     --print( "Loading items..." )
  393.  
  394.     turtle.select(1)
  395.     local n, res
  396.     for n = minSlot, maxSlot do
  397.         --unloaded = unloaded + turtle.getItemCount(n)
  398.         if (turtle.getItemCount(n) == 0) or sInput == "cf" or sInput == "ct" or sInput == "cd" then    
  399.  
  400.             res = loadSlot(n, sInput)
  401.            
  402.             if res == false then
  403.                 --print( "No more Items to get." )
  404.                 return false
  405.             end
  406.             --sleep(60)
  407.         end
  408.     end
  409.     return true
  410. end
Advertisement
Add Comment
Please, Sign In to add comment