Advertisement
Pinkishu

Untitled

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