Advertisement
npexception

Ingot-Recraft (requires Crafty-Turtle)

Mar 16th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.98 KB | None | 0 0
  1. local version = 0.3
  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, "g4Nr4qMA", {...}) 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.   dropside = direction.bottom,
  28.   tempside = direction.top,
  29.   fetchside = direction.front,
  30.   facing = direction.front
  31. }
  32.  
  33. -- READ OUT COMMAND LINE PARAMETERS --
  34. for _,par in pairs(ARGS) do
  35.   if startswith(par, "d:") then
  36.     vars.dropside = direction[string.sub(par, string.len("d:")+1)]
  37.   elseif startswith(par, "t:") then
  38.     vars.tempside = direction[string.sub(par, string.len("t:")+1)]
  39.   elseif startswith(par, "f:") then
  40.     vars.fetchside = direction[string.sub(par, string.len("f:")+1)]
  41.   elseif par == "help" then
  42.     print("The Turtle will suck ingots from the specified side (default: front) and craft them to nuggets which will be temporarily dropped to the \"temp\" side (default: top).")
  43.     print("The nuggets will be crafted back to ingots and get dropped to the \"drop\" side (default: bottom).")
  44.     print("Usage: "..shell.getRunningProgram().." [f:{fetch side}] [t:{temp side}] [d:{frop side}]")
  45.     print("   For example: "..shell.getRunningProgram().." f:top t:back")
  46.     return
  47.   end
  48. end
  49.  
  50. local function turnLeft()
  51.   turtle.turnLeft()
  52.   vars.facing = vars.facing-1
  53.   if (vars.facing < 0) then
  54.     vars.facing = 3
  55.   end
  56. end
  57.  
  58. -- drops to the designated side and returns whatever the turtle.drop returns.
  59. local function drop( side )
  60.   if side == direction.top then
  61.     return turtle.dropUp()
  62.   elseif side == direction.bottom then
  63.     return turtle.dropDown()
  64.   else
  65.     while vars.facing ~= side do
  66.       turnLeft()
  67.     end
  68.     return turtle.drop()
  69.   end
  70. end
  71.  
  72. -- sucks from the designated side and returns whatever the turtle.suck returns.
  73. local function suck( side )
  74.   if side == direction.top then
  75.     return turtle.suckUp()
  76.   elseif side == direction.bottom then
  77.     return turtle.suckDown()
  78.   else
  79.     while vars.facing ~= side do
  80.       turnLeft()
  81.     end
  82.     return turtle.suck()
  83.   end
  84. end
  85.  
  86. local function status(text)
  87.   term.clear()
  88.   term.setCursorPos(1,1)
  89.   print(" Ingot-Recraft ")
  90.   print("---------------")
  91.   print()
  92.   term.write("--> ")
  93.   print(text)
  94.   print()
  95. end
  96.  
  97. local function main()
  98.   status("Dropping Inventory...")
  99.   for i=1,16 do
  100.     if turtle.getItemCount(i) > 0 then
  101.       turtle.select(i)
  102.       drop(vars.dropside)
  103.     end
  104.   end
  105.  
  106.   turtle.select(1)
  107.   suck(vars.fetchside)
  108.  
  109.   while turtle.getItemCount(1) > 0 do
  110.     local ingotCount = turtle.getItemCount(1)
  111.     -- TODO: check if valid ingots
  112.     status("Crafting ingots to nuggets...")
  113.     turtle.select(2)
  114.     while turtle.getItemCount(1) > 0 do
  115.       turtle.craft()
  116.       drop(vars.tempside)
  117.     end
  118.    
  119.     status("Crafting back to ingots...")
  120.     turtle.select(16)
  121.     for i=1,11 do
  122.       if (i%4) > 0 then
  123.         -- always sucking to slot 16 and then evenly distributing to the 9 crafting slots
  124.         turtle.transferTo(i, ingotCount)
  125.         local nuggetsNeeded = ingotCount - turtle.getItemCount(i)
  126.         if (nuggetsNeeded > 0) then
  127.           suck(vars.tempside)
  128.           turtle.transferTo(i,nuggetsNeeded)
  129.         end
  130.       end
  131.     end
  132.     turtle.craft()
  133.    
  134.     status("Dropping Inventory...")
  135.     for i=1,16 do
  136.       if turtle.getItemCount(i) > 0 then
  137.         turtle.select(i)
  138.         drop(vars.dropside)
  139.       end
  140.     end
  141.    
  142.     turtle.select(1)
  143.     suck(vars.fetchside)
  144.   end
  145.  
  146.   status("Finished.")
  147. end
  148.  
  149. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement