Advertisement
Pinkishu

Untitled

Mar 18th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.48 KB | None | 0 0
  1. dofile("timer.lua")
  2. rednet.open("bottom")
  3.  
  4. function logbase(val,base)
  5.     return math.log(val)/math.log(base)
  6. end
  7.  
  8. local chargeSide = "back"
  9. local running = false
  10.  
  11. local clutch = peripheral.wrap("computer_0")
  12. local extractor = peripheral.wrap("Extractor_0")
  13. local coils = {}
  14. coils[1] = {side="left",p=peripheral.wrap("AdvancedGears_2")}
  15. coils[2] = {side="right",p=peripheral.wrap("AdvancedGears_3")}
  16.  
  17. timer.add(timer.new("switch",0,nil,false))
  18. timer.add(timer.new("charge",0,nil,false))
  19. timer.add(timer.new("stage",0,nil,false))
  20.  
  21. local activeStage = nil
  22. local activeCoil = nil
  23.  
  24. local stages = {}
  25. stages[1] = {base=900,scale=60,speed=2048,slots={0},clutch=1}
  26. stages[2] = {base=400,scale=20,speed=32768,slots={1,4},clutch=3}
  27. stages[3] = {base=600,scale=30,speed=32768,slots={2,5},clutch=3}
  28. stages[4] = {base=1200,scale=80,speed=4096,slots={3,6},clutch=2}
  29.  
  30. --CALC TIMES
  31. function calcStageTime()
  32.     local itemTime = stages[activeStage].base-stages[activeStage].scale*logbase(stages[activeStage].speed,2)+0.05
  33.     local totalTime = (stageItemCount(activeStage) * itemTime)/20 + 2
  34.  
  35.     timer.get("stage"):setCap(totalTime,true)
  36.     timer.get("stage"):start()
  37. end
  38.  
  39. function calcChargeTime()
  40.     timer.get("charge"):stop()
  41.  
  42.     local e = getInactiveCoil().p.getEnergy()
  43.     if(e>=600000000) then return 0 end
  44.     local de = 600000000-e
  45.  
  46.     local t = math.ceil(de / (1024*1024))
  47.     timer.get("charge"):setCap(t,true)
  48.     if(t>0) then
  49.         timer.get("charge"):start()
  50.     end
  51. end
  52.  
  53. function calcSwitchTime()
  54.     local e = getActiveCoil().p.getEnergy()
  55.     if(e>=50000000) then
  56.         e = e-50000000
  57.     end
  58.  
  59.     timer.get("switch"):setCap(math.ceil(e/(1024*1024)),true)
  60.     timer.get("switch"):start()
  61. end
  62.  
  63. --CHARGING
  64. function setCharging(setting)
  65.     rs.setOutput("top",(activeCoil==1))
  66.     rs.setOutput(chargeSide,setting)
  67. end
  68.  
  69. function setChargingSmart()
  70.     setCharging(timer.get("charge"):isRunning())
  71. end
  72.  
  73. --COILS
  74. function chooseCoil()
  75.     if coils[1].p.getEnergy() > coils[2].p.getEnergy() then
  76.         activeCoil = 1
  77.     else
  78.         activeCoil = 2
  79.     end
  80. end
  81.  
  82. function getActiveCoil()
  83.     return coils[activeCoil]
  84. end
  85.  
  86. function getInactiveCoil()
  87.     return coils[(activeCoil==1 and 2 or 1)]
  88. end
  89.  
  90. function setCoilRedstone(setting)
  91.     rs.setOutput(coils[1].side,false)
  92.     rs.setOutput(coils[2].side,false)
  93.     if(setting) then
  94.         rs.setOutput(getActiveCoil().side,true)
  95.     end
  96. end
  97.  
  98. function switchCoil()
  99.     setCoilRedstone(false)
  100.     chooseCoil()
  101.     setClutch()
  102.     calcSwitchTime()
  103.     calcChargeTime()
  104.     setCharging(timer.get("charge"):isRunning())
  105. end
  106.  
  107. --INVENTORIES
  108. function slotItemCount(slot)
  109.     local _,_,am = extractor.getSlot(slot)
  110.     return am or 0
  111. end
  112.  
  113. function stageItemCount(stage)
  114.     local total = 0
  115.     for i,v in ipairs(stages[stage].slots) do
  116.         total = total + slotItemCount(v)
  117.     end
  118.     return total
  119. end
  120.  
  121. function slotHasItems(slot)
  122.     return extractor.getSlot(slot) ~= nil
  123. end
  124.  
  125. function stageHasItems(stage)
  126.     for i,v in ipairs(stages[stage].slots) do
  127.         if(slotHasItems(v)) then return true end
  128.     end
  129.     return false
  130. end
  131.  
  132. --STAGING
  133. function setStage()
  134.     activeStage = nil
  135.     for i=4,1,-1 do
  136.         if(stageHasItems(i)) then
  137.             activeStage = i
  138.             break
  139.         end
  140.     end
  141. end
  142.  
  143. function setClutch()
  144.     clutch.turnOn()
  145.     sleep(0.1)
  146.     rednet.send(36,stages[activeStage].clutch)
  147. end
  148.  
  149. function startProcessing()
  150.     setStage()
  151.     if(activeStage == nil) then return false end
  152.     running = true
  153.     chooseCoil()
  154.     setClutch()
  155.     calcSwitchTime()
  156.     calcChargeTime()
  157.     calcStageTime()
  158.     setChargingSmart()
  159.     setCoilRedstone(true)
  160.     return true
  161. end
  162.  
  163. function stopProcessing()
  164.     running = false
  165.     print("Stopping Processing")
  166.     setCoilRedstone(false)
  167.     setCharging(false)
  168. end
  169.  
  170. function updateTimers(delta)
  171.     local timers = timer.update(delta)
  172.     term.clear()
  173.     term.setCursorPos(1,1)
  174.     print("Charge Time: "..timer.get("charge"):getLeft())
  175.     print("Switch Time: "..timer.get("switch"):getLeft())
  176.     print("Stage Time: "..timer.get("stage"):getLeft())
  177.     return timers,sleepTime
  178. end
  179.  
  180. while true do
  181.     while not startProcessing() do
  182.         if(running) then stopProcessing() end
  183.         sleep(5)
  184.     end
  185.     setChargingSmart()
  186.     startTime = os.clock()
  187.     local pre = os.time()
  188.     while true do
  189.  
  190.         local delta = (os.time()-pre)*50
  191.         if(os.time()<pre) then delta = (24-pre+os.time())*50 end
  192.         local timers,sleepTime = updateTimers(delta)
  193.         if(timers.stage) then
  194.             print("Switching Stage")
  195.             break
  196.         end
  197.         if(timers.switch) then
  198.             print("Switching Coil")
  199.             switchCoil()
  200.         elseif(timers.charge) then
  201.             print("Stopping Charging")
  202.             setCharging(false)
  203.         end
  204.         pre = os.time()
  205.         sleep(2)
  206.     end
  207. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement