IMarvinTPA

RouterHelper

Mar 11th, 2013
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.60 KB | None | 0 0
  1. --pastebin get rXRCh3Bg RH
  2.  
  3. --shell.run("RH t f b")
  4.  
  5. local tArgs = { ... }
  6. local sInput, sOutput, sOverflow, nDelay = tArgs[1], tArgs[2], tArgs[3], tArgs[4]
  7. local filename = shell.getRunningProgram()
  8.  
  9. local function fixSide(aSide)
  10.     if aSide == nil then
  11.         aSide = ""
  12.     end
  13.     aSide = string.lower(aSide)
  14.     if aSide == "top" or aSide == "t" or aSide == "up" or aSide == "u"then
  15.         aSide = "t"
  16.     elseif aSide == "front" or aSide == "f" then
  17.         aSide = "f"
  18.     elseif aSide == "bottom" or aSide == "b" or aSide == "down" or aSide == "d" then
  19.         aSide = "b"
  20.     elseif aSide == "none" or aSide == "n" then
  21.         aSide = "n"
  22.     elseif aSide == "rtop" or aSide == "rt" or aSide == "rup" or aSide == "ru"then
  23.         aSide = "rt"
  24.     elseif aSide == "rfront" or aSide == "rf" then
  25.         aSide = "rf"
  26.     elseif aSide == "rbottom" or aSide == "rb" or aSide == "rdown" or aSide == "rd" then
  27.         aSide = "rd"
  28.     elseif aSide == "rright" or aSide == "rr" then
  29.         aSide = "rr"
  30.     elseif aSide == "rleft" or aSide == "rl" then
  31.         aSide = "rl"
  32.     elseif aSide == "rback" or aSide == "rbk" then
  33.         aSide = "rb"
  34.     else
  35.         aSide = nil
  36.     end
  37.     return aSide
  38. end
  39. sInput = fixSide(sInput)
  40. sOutput = fixSide(sOutput)
  41. sOverflow = fixSide(sOverflow)
  42.  
  43. if sInput == nil or sOutput == nil or sOverflow == nil then
  44.     print( "Usage: " .. filename .. " <input side> <output side> <overflow side> [Delay Time] [slots]" )
  45.     print( "Side values are Top, Bottom, Front, and None" )
  46.     print( "The turtle will pull from the input side and push into the output side." )
  47.     print( "None can be specified if the turtle is expected to deal with items moving on their own." )
  48.     return
  49. end
  50.  
  51. if nDelay ~= nil then
  52.     nDelay = tonumber(nDelay)
  53. end
  54.  
  55. if nDelay == nil then
  56.     nDelay = 0.5
  57. end
  58.  
  59. local function displaySide(aSide)
  60.     if aSide == nil then
  61.         aSide = ""
  62.     end
  63.     aSide = string.lower(aSide)
  64.     if aSide == "t" then
  65.         aSide = "top"
  66.     elseif aSide == "f" then
  67.         aSide = "front"
  68.     elseif aSide == "b" then
  69.         aSide = "bottom"
  70.     elseif aSide == "n" then
  71.         aSide = "none"
  72.     elseif aSide == "rt" then
  73.         aSide = "redstone pulse top"
  74.     elseif aSide == "rf" then
  75.         aSide = "redstone pulse front"
  76.     elseif aSide == "rd" then
  77.         aSide = "redstone pulse down"
  78.     elseif aSide == "rr" then
  79.         aSide = "redstone pulse right"
  80.     elseif aSide == "rl" then
  81.         aSide = "redstone pulse left"
  82.     elseif aSide == "rb" then
  83.         aSide = "redstone pulse back"
  84.     else
  85.         aSide = "unknown (" .. aSide .. ")"
  86.     end
  87.     return aSide
  88. end
  89.  
  90. local function isEmpty()
  91.     local bFull = true
  92.     local nTotalItems = 0
  93.     for n=1,16 do
  94.         local nCount = turtle.getItemCount(n)
  95.         if nCount == 0 then
  96.                 bFull = false
  97.         end
  98.         nTotalItems = nTotalItems + nCount
  99.     end
  100.          
  101.     if nCount == 0 then
  102.         --print( "No more Items to drop." )
  103.         return true
  104.     end
  105.     return false
  106. end
  107.  
  108. local function flashSide(aSide)
  109.     --print("Flash " .. aSide .. " On ")
  110.     redstone.setOutput(aSide, true)
  111.     sleep(0.2)
  112.     --print("Flash " .. aSide .. " Off ")
  113.     redstone.setOutput(aSide, false)
  114.     sleep(0.2)
  115.  
  116. end
  117.  
  118. local function loadSlot(slotNo, sDir)
  119.  
  120.     if sDir == "n" then
  121.         return true
  122.     end
  123.  
  124.     if sDir == "t" then
  125.         res = turtle.suckUp()
  126.     elseif sDir == "f" then
  127.         res = turtle.suck()
  128.     elseif sDir == "b" then
  129.         res = turtle.suckDown()
  130.     elseif sDir == "rt" then
  131.         turtle.select(n)
  132.         flashSide("top")
  133.         res = (turtle.getItemCount(slotNo) ~= 0)
  134.     elseif sDir == "rd" then
  135.         turtle.select(n)
  136.         flashSide("bottom")
  137.         res = (turtle.getItemCount(slotNo) ~= 0)
  138.     elseif sDir == "rf" then
  139.         turtle.select(n)
  140.         flashSide("front")
  141.         res = (turtle.getItemCount(slotNo) ~= 0)
  142.     elseif sDir == "rb" then
  143.         turtle.select(n)
  144.         flashSide("back")
  145.         res = (turtle.getItemCount(slotNo) ~= 0)
  146.     elseif sDir == "rl" then
  147.         turtle.select(n)
  148.         flashSide("left")
  149.         res = (turtle.getItemCount(slotNo) ~= 0)
  150.     elseif sDir == "rf" then
  151.         turtle.select(n)
  152.         flashSide("right")
  153.         res = (turtle.getItemCount(slotNo) ~= 0)
  154.     end
  155.    
  156.     return res
  157. end
  158.  
  159. local function unloadSlot(slotNo, sDir)
  160.  
  161.     if sDir == "n" then
  162.         return true
  163.     end
  164.     --print( "Unloading items..." )
  165.    
  166.     local res = false
  167.  
  168.     if (turtle.getItemCount(slotNo) > 0) then
  169.         turtle.select(slotNo)  
  170.         if sDir == "t" then
  171.             res = turtle.dropUp()
  172.         elseif sDir == "f" then
  173.             res = turtle.drop()
  174.         elseif sDir == "b" then
  175.             res = turtle.dropDown()
  176.         elseif sDir == "rt" then
  177.             flashSide("top")
  178.             res = (turtle.getItemCount(slotNo) == 0)
  179.         elseif sDir == "rd" then
  180.             flashSide("bottom")
  181.             res = (turtle.getItemCount(slotNo) == 0)
  182.         elseif sDir == "rf" then
  183.             flashSide("front")
  184.             res = (turtle.getItemCount(slotNo) == 0)
  185.         elseif sDir == "rb" then
  186.             flashSide("back")
  187.             res = (turtle.getItemCount(slotNo) == 0)
  188.         elseif sDir == "rl" then
  189.             flashSide("left")
  190.             res = (turtle.getItemCount(slotNo) == 0)
  191.         elseif sDir == "rf" then
  192.             flashSide("right")
  193.             res = (turtle.getItemCount(slotNo) == 0)
  194.         end
  195.         return res
  196.     else
  197.         return true
  198.     end
  199. end
  200.  
  201. local function emptySlot()
  202.     for n2 = 1, 16 do
  203.         if (turtle.getItemCount(n2) == 0) then
  204.             return n2
  205.         end
  206.     end
  207.     return 0
  208. end
  209.  
  210. local function YesNoOther(aBool)
  211.     if aBool == true then
  212.         return "Yes"
  213.     else
  214.         return "No"
  215.     end
  216. end
  217.  
  218. local function unload()
  219.     if sOutput == "n" then
  220.         return
  221.     end
  222.     --print( "Unloading items..." )
  223.    
  224.     local res = false
  225.  
  226.     for n = 1, 16 do
  227.         --unloaded = unloaded + turtle.getItemCount(n)
  228.         if (turtle.getItemCount(n) > 0) then
  229.             turtle.select(n)   
  230.            
  231.             if unloadSlot(n, sOutput) == false then
  232.                 --the router is full, we have to pull the item out and ditch it.
  233.                 print("Router Stuffed")
  234.                 local cacheSlot = emptySlot()
  235.                 print("Empty Slot at: " .. cacheSlot)
  236.                 if cacheSlot > 0 then
  237.                     turtle.select(cacheSlot)
  238.                     res = loadSlot(cacheSlot, sOutput)
  239.                    
  240.                     print("Pulling result: " .. YesNoOther(res))
  241.                     res = unloadSlot(cacheSlot, sOverflow)
  242.                     print("Overflow Output result: " .. YesNoOther(res))
  243.                     res = unloadSlot(n, sOutput)
  244.                     print("Retry result: " .. YesNoOther(res))
  245.                 end            
  246.             end
  247.             sleep(0.2)
  248.         end
  249.     end    
  250. end
  251.  
  252. local function load()
  253.     if sInput == "n" then
  254.         return
  255.     end
  256.  
  257.     --print( "Loading items..." )
  258.  
  259.     turtle.select(1)
  260.     for n = 2, 15 do
  261.         --unloaded = unloaded + turtle.getItemCount(n)
  262.         if (turtle.getItemCount(n) == 0) then          
  263.  
  264.             res = loadSlot(n, sInput)
  265.            
  266.             if res == false then
  267.                 --print( "No more Items to get." )
  268.                 return false
  269.             end
  270.             --sleep(60)
  271.         end
  272.     end
  273.     return true
  274. end
  275.  
  276. print( "Transposing from " .. displaySide(sInput) .. " to " .. displaySide(sOutput) .. " with a delay of " .. nDelay .. " seconds and sending overflow items to " .. displaySide(sOverflow) .. "." )
  277.  
  278.  
  279. while true do
  280.     load()
  281.     unload()
  282.     sleep(nDelay)
  283. end
Advertisement
Add Comment
Please, Sign In to add comment