Advertisement
cube_man

Tinker's Construct - Auto Smeltery Program

Aug 1st, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.14 KB | None | 0 0
  1. -- Auto Smeltery for Tinker's Construct
  2. -- By David Jordan (cube_man)
  3.  
  4.  
  5.  
  6. ------ INITIALIZATION ------
  7.  
  8. -- Variables --
  9. local side      = "back"
  10. local monitor   = peripheral.wrap("top")
  11. local startTime = 0
  12. local done      = false
  13. local running   = true
  14.  
  15. -- Input/Output colors --
  16. local out_pour         = colors.white
  17. local out_pop_cast     = colors.orange
  18. local out_return_cast  = colors.magenta
  19. local out_return_item  = colors.lime
  20. local in_press_button  = colors.lightBlue
  21. local in_item_in_chest = colors.yellow
  22.  
  23.  
  24.  
  25. ------ FUNCTIONS ------
  26.  
  27. -- Queue a random event to make sure the program is occupied --
  28. function nullEvent()
  29.     os.queueEvent("randomEvent")
  30.     os.pullEvent()
  31. end
  32.  
  33. -- Print text to the monitor --
  34. function monitorPrint(str)
  35.     monitor.write(str)
  36. end
  37.  
  38. -- Print a line to the monitor --
  39. function monitorPrintln(str)
  40.     monitor.write(str)
  41.     local cx, cy = monitor.getCursorPos()
  42.     monitor.setCursorPos(1, cy + 1)
  43. end
  44.  
  45. -- Set the color index of the boundled output to the given value --
  46. function setOutput(index, value)
  47.     local bundle = redstone.getBundledOutput(side)
  48.    
  49.     if value then
  50.         redstone.setBundledOutput(side, bit.bor(bundle, index))
  51.     else
  52.         redstone.setBundledOutput(side, bit.band(bundle, bit.bnot(index)))
  53.     end
  54. end
  55.  
  56. -- Get the color index of the bundled output --
  57. function getOutput(index)
  58.     return bit.band(redstone.getBundledOutput(side), index) == index
  59. end
  60.  
  61. -- Get the color index of the bundled input --
  62. function getInput(index)
  63.     return bit.band(redstone.getBundledInput(side), index) == index
  64. end
  65.  
  66.  
  67.  
  68. ------ MAIN ------
  69.  
  70. print("Running Smeltery Program.")
  71. monitor.clear()
  72. monitor.setCursorPos(1, 1)
  73. monitorPrintln("Running Smeltery")
  74.  
  75.  
  76.  
  77. while running do
  78.     setOutput(out_pop_cast,    false)
  79.     setOutput(out_return_cast, true)
  80.     setOutput(out_return_item, false)
  81.     setOutput(out_pour,        false)
  82.  
  83.     -- Wait for user to press the button.
  84.     while not getInput(in_press_button) do
  85.         if rs.getInput("left") then
  86.             monitorPrintln("Powering down...")
  87.             os.sleep(1)
  88.             monitor.clear()
  89.             running = false
  90.             break
  91.         end
  92.        
  93.         nullEvent()
  94.     end
  95.    
  96.     if not running then
  97.         break
  98.     end
  99.    
  100.     monitor.clear()
  101.     monitor.setCursorPos(1, 1)
  102.     monitorPrintln("Button Pressed!")
  103.     monitorPrint("Pouring")
  104.    
  105.     -- Keep pouring until the cast is filled.
  106.     local done = false
  107.     local startTime = 0
  108.    
  109.     while not done do
  110.         monitorPrint(".")
  111.        
  112.         -- Try to pour for 0.5 seconds --
  113.         setOutput(out_pour, true)
  114.         startTime = os.clock()
  115.         while os.clock() - startTime < 0.5 and not done do
  116.             if getInput(in_item_in_chest) then
  117.                 done = true
  118.             end
  119.             nullEvent()
  120.         end
  121.         setOutput(out_pour, false)
  122.        
  123.         if not done then
  124.             -- Wait for 1.5 seconds --
  125.             startTime = os.clock()
  126.             while os.clock() - startTime < 1.5 and not done do
  127.                 if getInput(in_item_in_chest) then
  128.                     done = true
  129.                 end
  130.                 nullEvent()
  131.             end
  132.         end
  133.     end
  134.    
  135.     monitorPrintln("")
  136.     monitorPrintln("Done!")
  137.    
  138.     -- Return created item.
  139.     setOutput(out_return_item, true)
  140.    
  141.     -- Pop cast.
  142.     setOutput(out_pop_cast, true)
  143.     os.sleep(0.4)
  144.     setOutput(out_pop_cast, false)
  145.    
  146.     -- Return cast.
  147.     --setOutput(out_return_cast, true)
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement