Advertisement
jille_Jr

CC: Tinkers smeltery control

Feb 23rd, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.47 KB | None | 0 0
  1.  
  2. local basinpath = "right"
  3. local tablepath = "bottom"
  4. local drainpath = "tconstruct_smelterydrain_2"
  5. local monitorpath = "monitor_3"
  6.  
  7. local basintime = 10
  8. local basindelay = 5
  9. local tabletime = 2
  10. local tabledelay = 3
  11.  
  12. local drain = peripheral.wrap(drainpath) or error("Unable to wrap drain!",0)
  13. os.loadAPI("display")
  14. local mon = display.load(monitorpath)
  15.  
  16. local savefile = "activestate.sav"
  17. local active = false
  18. local updaterate = .1
  19. local drawrate = .2
  20.  
  21. local mbtoingot = 144
  22. -- millibucket to ingot ratio 144/1
  23.  
  24. local function getingots()
  25.     local tank = drain.getTankInfo("back")
  26.     if not tank[1].amount then return 0 end
  27.     return tank[1].amount/mbtoingot
  28. end
  29.  
  30. local function activatebasin()
  31.     rs.setOutput(basinpath,true)
  32.     sleep(10)
  33.     rs.setOutput(basinpath,false)
  34.     sleep(4)
  35. end
  36.  
  37. local function activatetable()
  38.     rs.setOutput(tablepath,true)
  39.     sleep(2)
  40.     rs.setOutput(tablepath,false)
  41.     sleep(3)
  42. end
  43.  
  44. local function draw()
  45.     local w,h = mon.obj.getSize()
  46.     local ingots = getingots()
  47.    
  48.     mon:setbgcolor(colors.black)
  49.     mon:clear()
  50.    
  51.     -- ON/OFF
  52.     local txt = ""
  53.     if active then
  54.         txt = "ON"
  55.         mon:setbgcolor(colors.lime)
  56.         mon:setfgcolor(colors.green)
  57.     else
  58.         txt = "OFF"
  59.         mon:setbgcolor(colors.red)
  60.         mon:setfgcolor(colors.brown)
  61.     end
  62.     mon:write(txt,3,2)
  63.     mon:rect("fill",1,2,w,6,true)
  64.     if active then
  65.         mon:setbgcolor(colors.green)
  66.         mon:line(2,4,4,6)
  67.         mon:line(4,6,6,2)
  68.     else
  69.         mon:setbgcolor(colors.brown)
  70.         mon:line(1,2,7,6)
  71.         mon:line(7,2,1,6)
  72.     end
  73.    
  74.     -- ingots
  75.     mon:setfgcolor(colors.blue)
  76.     mon:setbgcolor(colors.lightBlue)
  77.     local txt = "storage"
  78.     mon:write(txt,w/2-#txt/2+1,8)
  79.     local txt = tostring(ingots)
  80.     mon:write(txt,w/2-#txt/2+1,9)
  81.     mon:rect("fill",1,7,w,10,true)
  82.    
  83.     mon:update()
  84.     mon:draw()
  85. end
  86.  
  87. local function save()
  88.     local file = fs.open(savefile,"w")
  89.     file.write(tostring(active))
  90.     file.close()
  91. end
  92.  
  93. local function load()
  94.     if fs.exists(savefile) then
  95.         local file = fs.open(savefile,"r")
  96.         local state = file.readLine()
  97.         if state == "true" then active = true end
  98.         if state == "false" then active = false end
  99.         file.close()
  100.     end
  101. end
  102.  
  103. local updatetimer = os.startTimer(updaterate)
  104. local drawtimer = os.startTimer(drawrate)
  105. local basintimer, tabletimer, basindelaytimer, tabledelaytimer
  106. local drainready = true
  107. load()
  108. while true do
  109.     local ev,p1,p2,p3 = os.pullEvent()
  110.    
  111.     if ev == "timer" then
  112.         if p1 == updatetimer then
  113.             updatetimer = os.startTimer(updaterate)
  114.             if active then
  115.                 local ingots = getingots()
  116.                 if ingots < 9 and ingots > 0 and drainready then
  117.                     -- smelt seperate ingot with table
  118.                     rs.setOutput(tablepath,true)
  119.                     tabletimer = os.startTimer(tabletime)
  120.                     drainready = false
  121.                 end
  122.                 local ingots = getingots()
  123.                 if ingots >= 9 and drainready then
  124.                     -- smelt block with basin
  125.                     rs.setOutput(basinpath,true)
  126.                     basintimer = os.startTimer(basintime)
  127.                     drainready = false
  128.                 end
  129.             end
  130.         elseif p1 == drawtimer then
  131.             drawtimer = os.startTimer(drawrate)
  132.             draw()
  133.         elseif p1 == tabletimer then
  134.             rs.setOutput(tablepath,false)
  135.             tabledelaytimer = os.startTimer(tabledelay)
  136.         elseif p1 == basintimer then
  137.             rs.setOutput(basinpath,false)
  138.             basindelaytimer = os.startTimer(basindelay)
  139.         elseif p1 == tabledelaytimer then
  140.             drainready = true
  141.         elseif p1 == basindelaytimer then
  142.             drainready = true
  143.         end
  144.     elseif ev == "monitor_touch" then
  145.         if p2 >= 1 and p2 <= 7
  146.         and p3 >=2 and p3 <= 6 then
  147.             active = not active
  148.             save()
  149.         end
  150.     end
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement