Advertisement
npexception

RubbishSorter

Mar 11th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.42 KB | None | 0 0
  1. local version = 0.5
  2. --  +------------------------+  --
  3. --  |-->  INITIALIZATION  <--|  --
  4. --  +------------------------+  --
  5. if not turtle then
  6.   print("This program can only be")
  7.   print("  executed by a turtle!")
  8.   return
  9. end
  10.  
  11. local ARGS = {...}
  12.  
  13. -- INITIALIZING NECESSARY FUNCTIONS
  14. local startswith = function(self, piece)
  15.   return string.sub(self, 1, string.len(piece)) == piece
  16. end
  17.  
  18. -- UPDATE HANDLING --
  19. if _UD and _UD.su(version, "tqGfwDmH", {...}) then return end
  20.  
  21. -- directions in relation to initial placement (which is 0, or front)
  22. local direction = {front=0, right=1, back=2, left=3, top=4, bottom=5 }
  23.  
  24. -- INITIAL VARIABLE SETUP --
  25. local vars = {
  26.   -- width and height of the quarry site
  27.   goodside = direction.top,
  28.   badside = direction.bottom,
  29.   fetchside = direction.front,
  30.   facing = direction.front,
  31.   compareslots = {},
  32.   freeslots = {}
  33. }
  34.  
  35. -- READ OUT COMMAND LINE PARAMETERS --
  36. for _,par in pairs(ARGS) do
  37.   if startswith(par, "g:") then
  38.     vars.goodside = direction[string.sub(par, string.len("g:")+1)]
  39.   elseif startswith(par, "b:") then
  40.     vars.badside = direction[string.sub(par, string.len("b:")+1)]
  41.   elseif startswith(par, "f:") then
  42.     vars.fetchside = direction[string.sub(par, string.len("f:")+1)]
  43.   elseif par == "help" then
  44.     print("The Turtle will suck items from the specified side (default: front) and drop everything that was not present in its inventory at start to the \"good\" side (default: top).")
  45.     print("Everything else will be dropped to the \"bad\" side (default: bottom).")
  46.     print("Usage: "..shell.getRunningProgram().." [f:{fetch side}] [g:{goodstuff side}] [b:{badstuff side}]")
  47.     print("   For example: "..shell.getRunningProgram().." g:top b:back")
  48.     return
  49.   end
  50. end
  51.  
  52. local function turnLeft()
  53.   turtle.turnLeft()
  54.   vars.facing = vars.facing-1
  55.   if (vars.facing < 0) then
  56.     vars.facing = 3
  57.   end
  58. end
  59.  
  60. -- drops to the designated side and returns whatever the turtle.drop returns.
  61. local function drop( side )
  62.   if side == direction.top then
  63.     return turtle.dropUp()
  64.   elseif side == direction.bottom then
  65.     return turtle.dropDown()
  66.   else
  67.     while vars.facing ~= side do
  68.       turnLeft()
  69.     end
  70.     return turtle.drop()
  71.   end
  72. end
  73.  
  74. -- sucks from the designated side and returns whatever the turtle.suck returns.
  75. local function suck( side )
  76.   if side == direction.top then
  77.     return turtle.suckUp()
  78.   elseif side == direction.bottom then
  79.     return turtle.suckDown()
  80.   else
  81.     while vars.facing ~= side do
  82.       turnLeft()
  83.     end
  84.     return turtle.suck()
  85.   end
  86. end
  87.  
  88. local function main()
  89.   -- determine which slots will be used for comparison and which slot will be dropped from and sucked into.
  90.   for i=1,16 do
  91.     if turtle.getItemCount(i) > 0 then
  92.       vars.compareslots[#vars.compareslots+1] = i
  93.     else
  94.       vars.freeslots[#vars.freeslots+1] = i
  95.     end
  96.   end
  97.  
  98.   -- main loop
  99.   while true do
  100.     -- fetch items
  101.     turtle.select(vars.freeslots[1])
  102.     local sucked = suck(vars.fetchside)
  103.     if not sucked then
  104.       print("Waiting for items...")
  105.     end
  106.     while not sucked do
  107.       sleep(3)
  108.       sucked = suck(vars.fetchside)
  109.       if sucked then
  110.         print("Got some! Continuing work...")
  111.       end
  112.     end
  113.     -- suck into all other free slots
  114.     if #vars.freeslots > 1 then
  115.       for i=2, #vars.freeslots do
  116.         turtle.select(vars.freeslots[i])
  117.         suck(vars.fetchside)
  118.       end
  119.     end
  120.    
  121.     -- compare and drop for each
  122.    
  123.     for fs=1, #vars.freeslots do
  124.       local slot = vars.freeslots[fs]
  125.       if turtle.getItemCount(slot) > 0 then
  126.         turtle.select(slot)
  127.         -- compare
  128.         local isGood = true
  129.         for cs=1,#vars.compareslots do
  130.           if turtle.compareTo(vars.compareslots[cs]) then
  131.             isGood = false
  132.             break
  133.           end
  134.         end
  135.        
  136.         -- drop
  137.         local side = vars.goodside
  138.         if not isGood then
  139.           side = vars.badside
  140.         end
  141.        
  142.         drop(side)
  143.         local dropped = (turtle.getItemCount(slot) == 0)
  144.         if not dropped then
  145.           print("Drop chest is full...")
  146.         end
  147.         while not dropped do
  148.           sleep(3)
  149.           drop(side)
  150.           dropped = (turtle.getItemCount(slot) == 0)
  151.           if dropped then
  152.             print("Dropped items! Continuing...")
  153.           end
  154.         end
  155.       end
  156.     end
  157.   end
  158.  
  159. end
  160.  
  161. print("Starting program")
  162. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement