Advertisement
joebodo

apps.smeltry.lua

Oct 5th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.68 KB | None | 0 0
  1. multishell.setTitle(multishell.getCurrent(), 'Smeltry')
  2. turtle.status = 'Smelting'
  3.  
  4. require = requireInjector(getfenv(1))
  5. local Event = require('event')
  6. local UI = require('ui')
  7.  
  8. local inventory = { }
  9. local selectedTank = nil
  10.  
  11. rs.setOutput('bottom', false)
  12. rs.setOutput('top', false)
  13. rs.setOutput('right', false)
  14.  
  15. turtle.dig()
  16.  
  17. for _, slot in pairs(turtle.getFilledSlots()) do
  18.   turtle.select(slot.index)
  19.   if turtle.place() then
  20.     os.pullEvent('device_attach')
  21.     local contents = device.thermalexpansion_tank.getTankInfo()[1].contents
  22.     if not contents then
  23.       contents = {
  24.         name = 'empty',
  25.         rawName = 'empty',
  26.         amount = 0,
  27.       }
  28.     end
  29.     contents.index = slot.index
  30.     table.insert(inventory, contents)
  31.  
  32.     turtle.dig()
  33.   end
  34. end
  35.  
  36. local function selectFluid(fluidName)
  37.   local tank = Util.find(inventory, 'name', fluidName)  
  38.   if not tank then
  39.     return false
  40.   end
  41.   turtle.select(tank.index)
  42.   selectedTank = tank
  43.   return true
  44. end
  45.  
  46. local function waitTilTankEmpty()
  47.   rs.setOutput('top', true)
  48.   repeat
  49.     local tank = device.thermalexpansion_tank.getTankInfo()[1].contents
  50.     os.sleep(1)
  51.   until not tank
  52.   rs.setOutput('top', false)
  53. end
  54.  
  55. local function emptySmeltry()
  56.   rs.setOutput('bottom', true)
  57.   os.sleep(1)
  58.   repeat
  59.     local f = device.blockconduitbundletileentity.getTankInfo()[1].contents
  60.     if f then
  61.       if not selectFluid(f.name) then
  62.         if not selectFluid('empty') then
  63.           error('Need more tanks')
  64.         end
  65.       end
  66.  
  67.       turtle.place()
  68.       repeat
  69.         os.sleep(1)
  70.         local c = device.blockconduitbundletileentity.getTankInfo()[1].contents
  71.       until not c or c.name ~= f.name
  72.       os.sleep(1)
  73.       local tank = device.thermalexpansion_tank.getTankInfo()[1].contents
  74.       selectedTank.name = tank.name
  75.       selectedTank.rawName = tank.rawName
  76.       selectedTank.amount = tank.amount
  77.       turtle.dig()
  78.     end
  79.   until not f
  80.   rs.setOutput('bottom', false)
  81. end
  82.  
  83. local function insertFluid(tank)
  84.   if not selectFluid(tank.name) then
  85.     error('select failed')
  86.   end
  87.   turtle.place()
  88.   os.pullEvent('device_attach')
  89.   waitTilTankEmpty()
  90.   turtle.dig()
  91.   tank.name = 'empty'
  92.   tank.rawName = 'empty'
  93.   tank.amount = 0
  94. end
  95.  
  96. local page = UI.Page({
  97.   menuBar = UI.MenuBar({
  98.     buttons = {
  99.       { text = 'Empty',  event = 'empty'  },
  100.       { text = 'Ingots', event = 'ingots' },
  101.     },
  102.   }),
  103.   grid = UI.ScrollingGrid({
  104.     y = 2,
  105.     height = UI.term.height-2,
  106.     values = inventory,
  107.     columns = {
  108.       { heading = 'Name',   key = 'rawName' },
  109.       { heading = 'Ingots', key = 'ingots'  },
  110.     },
  111.     sortColumn = 'rawName',
  112.     autospace = true,
  113.   }),
  114.   statusBar = UI.StatusBar(),
  115.   accelerators = {
  116.     q = 'quit',
  117.   },
  118. })
  119.  
  120. function page.grid:draw()
  121.   for _,tank in pairs(inventory) do
  122.     tank.ingots = math.floor(tank.amount / 144 * 100) / 100
  123.   end
  124.   return UI.Grid.draw(self)
  125. end
  126.  
  127. function page:eventHandler(event)
  128.   if event.type == 'empty' then
  129.     emptySmeltry()
  130.     self.grid:draw()
  131.   elseif event.type == 'ingots' then
  132.     rs.setOutput('right', true)
  133.     while true do
  134.       rs.setOutput('right', true)
  135.       os.sleep(.25)
  136.       rs.setOutput('right', false)
  137.       os.sleep(2)
  138.       local sc = device.tconstruct_smelterydrain.getTankInfo()[1].contents
  139.       if not sc or sc.amount < 144 then
  140.         break
  141.       end
  142.     end
  143.   elseif event.type == 'grid_select' then
  144.     insertFluid(event.selected)
  145.     self.grid:draw()
  146.   elseif event.type == 'quit' then
  147.     Event.exitPullEvents()
  148.   end
  149.   UI.Page.eventHandler(self, event)
  150. end
  151.  
  152. UI:setPage(page)
  153. Event.pullEvents()
  154. UI.term:reset()
  155. turtle.status = 'idle'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement