Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dofile("timer.lua")
- rednet.open("bottom")
- function logbase(val,base)
- return math.log(val)/math.log(base)
- end
- local chargeSide = "back"
- local running = false
- local clutch = peripheral.wrap("computer_0")
- local extractor = peripheral.wrap("Extractor_0")
- local coils = {}
- coils[1] = {side="left",p=peripheral.wrap("AdvancedGears_2")}
- coils[2] = {side="right",p=peripheral.wrap("AdvancedGears_3")}
- timer.add(timer.new("switch",0,nil,false))
- timer.add(timer.new("charge",0,nil,false))
- timer.add(timer.new("stage",0,nil,false))
- local activeStage = nil
- local activeCoil = nil
- local stages = {}
- stages[1] = {base=900,scale=60,speed=2048,slots={0},clutch=1}
- stages[2] = {base=400,scale=20,speed=32768,slots={1,4},clutch=3}
- stages[3] = {base=600,scale=30,speed=32768,slots={2,5},clutch=3}
- stages[4] = {base=1200,scale=80,speed=4096,slots={3,6},clutch=2}
- --CALC TIMES
- function calcStageTime()
- local itemTime = stages[activeStage].base-stages[activeStage].scale*logbase(stages[activeStage].speed,2)+0.05
- local totalTime = (stageItemCount(activeStage) * itemTime)/20 + 2
- timer.get("stage"):setCap(totalTime,true)
- timer.get("stage"):start()
- end
- function calcChargeTime()
- timer.get("charge"):stop()
- local e = getInactiveCoil().p.getEnergy()
- if(e>=600000000) then return 0 end
- local de = 600000000-e
- local t = math.ceil(de / (1024*1024))
- timer.get("charge"):setCap(t,true)
- if(t>0) then
- timer.get("charge"):start()
- end
- end
- function calcSwitchTime()
- local e = getActiveCoil().p.getEnergy()
- if(e>=50000000) then
- e = e-50000000
- end
- timer.get("switch"):setCap(math.ceil(e/(1024*1024)),true)
- timer.get("switch"):start()
- end
- --CHARGING
- function setCharging(setting)
- rs.setOutput("top",(activeCoil==1))
- rs.setOutput(chargeSide,setting)
- end
- function setChargingSmart()
- setCharging(timer.get("charge"):isRunning())
- end
- --COILS
- function chooseCoil()
- if coils[1].p.getEnergy() > coils[2].p.getEnergy() then
- activeCoil = 1
- else
- activeCoil = 2
- end
- end
- function getActiveCoil()
- return coils[activeCoil]
- end
- function getInactiveCoil()
- return coils[(activeCoil==1 and 2 or 1)]
- end
- function setCoilRedstone(setting)
- rs.setOutput(coils[1].side,false)
- rs.setOutput(coils[2].side,false)
- if(setting) then
- rs.setOutput(getActiveCoil().side,true)
- end
- end
- function switchCoil()
- setCoilRedstone(false)
- chooseCoil()
- setClutch()
- calcSwitchTime()
- calcChargeTime()
- setCharging(timer.get("charge"):isRunning())
- end
- --INVENTORIES
- function slotItemCount(slot)
- local _,_,am = extractor.getSlot(slot)
- return am or 0
- end
- function stageItemCount(stage)
- local total = 0
- for i,v in ipairs(stages[stage].slots) do
- total = total + slotItemCount(v)
- end
- return total
- end
- function slotHasItems(slot)
- return extractor.getSlot(slot) ~= nil
- end
- function stageHasItems(stage)
- for i,v in ipairs(stages[stage].slots) do
- if(slotHasItems(v)) then return true end
- end
- return false
- end
- --STAGING
- function setStage()
- activeStage = nil
- for i=4,1,-1 do
- if(stageHasItems(i)) then
- activeStage = i
- break
- end
- end
- end
- function setClutch()
- clutch.turnOn()
- sleep(0.1)
- rednet.send(36,stages[activeStage].clutch)
- end
- function startProcessing()
- setStage()
- if(activeStage == nil) then return false end
- running = true
- chooseCoil()
- setClutch()
- calcSwitchTime()
- calcChargeTime()
- calcStageTime()
- setChargingSmart()
- setCoilRedstone(true)
- return true
- end
- function stopProcessing()
- running = false
- print("Stopping Processing")
- setCoilRedstone(false)
- setCharging(false)
- end
- function updateTimers(delta)
- local timers = timer.update(delta)
- term.clear()
- term.setCursorPos(1,1)
- print("Charge Time: "..timer.get("charge"):getLeft())
- print("Switch Time: "..timer.get("switch"):getLeft())
- print("Stage Time: "..timer.get("stage"):getLeft())
- return timers,sleepTime
- end
- while true do
- while not startProcessing() do
- if(running) then stopProcessing() end
- sleep(5)
- end
- setChargingSmart()
- startTime = os.clock()
- local pre = os.time()
- while true do
- local delta = (os.time()-pre)*50
- if(os.time()<pre) then delta = (24-pre+os.time())*50 end
- local timers,sleepTime = updateTimers(delta)
- if(timers.stage) then
- print("Switching Stage")
- break
- end
- if(timers.switch) then
- print("Switching Coil")
- switchCoil()
- elseif(timers.charge) then
- print("Stopping Charging")
- setCharging(false)
- end
- pre = os.time()
- sleep(2)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement