Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- EyeDeck's RoC extractor controller
- Recommended setup:
- _ M M M
- E C P P
- B G V I
- M = CC Modem/cable
- E = Extractor
- C = Computer
- P = Peripheral Proxy (pointed down)
- B = Bevel gears (^<)
- G = 16:1 Bedrock Gearbox, speed mode
- V = CVT, w/lube and 31 belts
- I = Industrial Coil w/ external Redstone
- Next, attach a 2x2 of advanced monitors,
- either to the computer, or via network cable.
- Finally, to load, enter:
- pastebin get 2varmnk0 wget
- wget http://idek.chir.uno/mc/extractor.lua startup
- startup
- --]]
- coil_speed = 4096 -- constant
- stages = {
- {ratio=-2, coil=4096, t=0.4}, -- 16MW: -2x CVT, 4096Nm coil, 0.4s, 32768 @ 512Nm #1/t
- -- 3/t? only 3rd stage matters here anyway
- {ratio=32, coil=512, t=0.6}, -- 2MW: 32x CVT, 512Nm coil, 0.6s, = 2097152 @ 1Nm; #2/t x1.5 12
- {ratio=1, coil=4096, t=0.7} -- 16MW, 1x CVT, 4096Nm coil 0.9s, = 65536 @ 256 #2/t x1.5 18
- }
- w_per_cycle = 0
- total_time = 0
- for _,stage in pairs(stages) do
- w_per_cycle = w_per_cycle + (coil_speed * stage.coil * stage.t)
- total_time = total_time + stage.t
- end
- avg_w = w_per_cycle / total_time
- pwr_remaining_mult = avg_w * 60 * 60
- f = fs.open("buttonAPI","r")
- if f == nil then
- print("Attempting to fetch ButtonAPI...")
- bapi = http.get("http://idek.chir.uno/mc/buttonAPI.lua")
- if (bapi) then
- f = fs.open("buttonAPI","w")
- f.write(bapi.readAll())
- f.close()
- print("...complete, ButtonAPI has been downloaded.")
- else
- print("...failed, ButtonAPI could not be downloaded. Re-run this program to retry, or supply ButtonAPI manually.")
- error("ButtonAPI download failed")
- end
- else
- f.close()
- end
- os.loadAPI("buttonAPI")
- ex, cvt, coil, mon = nil, nil, nil, nil
- warning = {}
- print("Checking peripherals...")
- while ex == nil or cvt == nil or coil == nil or mon == nil do
- ex = peripheral.find("Extractor")
- adv1,adv2 = peripheral.find("AdvancedGear")
- mon = peripheral.find("monitor")
- if adv1.getName() == "CVT Unit" then
- cvt = adv1
- coil = adv2
- else
- cvt = adv2
- coil = adv1
- end
- if ex == nil then
- if warning.ex == nil then
- print("Extractor missing!")
- warning.ex = true
- end
- sleep(2)
- elseif cvt == nil then
- if warning.cvt == nil then
- print("CVT missing!")
- warning.cvt = true
- end
- sleep(2)
- elseif coil == nil then
- if warning.coil == nil then
- print("Coil missing!")
- warning.coil = true
- end
- sleep(2)
- elseif mon == nil then
- if warning.mon == nil then
- print("Monitor missing!")
- warning.mon = true
- end
- sleep(2)
- else
- break
- end
- end
- print("All peripherals present.")
- peripherals = 0
- --Returns right-most item in extractor, else if empty returns false
- function notEmpty()
- local item = {}
- for i = 4,1,-1 do
- item = getStackInSlot(ex,i)
- if item ~= nil then
- return item
- end
- end
- return false
- end
- function getItemNameList(p)
- local items = {}
- for i = 1,4,1 do
- item = getStackInSlot(p,i)
- if item == nil then
- items[i] = " "
- else
- items[i] = string.format("%02d", item["qty"]) .. " x " .. item["display_name"]
- end
- end
- return items
- end
- function getStackInSlot(p,slot)
- sanityCheck()
- local item = {}
- item.raw_name, item.dmg, item.qty, item.display_name = p.getSlot(slot-1)
- -- make sure that nil is returned instead of a useless object if there is no item or the script will break in 12 different places
- if item.qty ~= nil then
- return item
- else
- return nil
- end
- end
- -- A huge hack to prevent a crash when a peripheral is disconnected
- function sanityCheck()
- if peripherals < 0 then
- print("A peripheral has been detached!\nWaiting for reattachment...")
- while peripherals < 0 do
- sleep(2)
- end
- print("Resuming.")
- end
- end
- function writeState(state,val)
- if val == true then val = "1" else val = "0" end
- local file = fs.open("state_"..state,"w")
- file.write(val)
- file.close()
- end
- function readState(state)
- local file = fs.open("state_"..state,"r")
- local val
- if file == nil then
- val = false
- else
- val = file.readAll()
- if val == "1" then val = true else val = false end
- file.close()
- end
- return val
- end
- function updateDisplay(state, items)
- sanityCheck()
- local old_disp = term.redirect(mon)
- local power_in_hours = math.floor(coil.getEnergy() / pwr_remaining_mult * 100)*.01
- local cx, cy = 2, 10
- -- term.setCursorPos(cx,cy)
- if state == "ex" then
- term.setTextColor(colors.lime)
- writeCentered(term, cy, "-- Extracting --")
- elseif state == "jam" then
- term.setTextColor(colors.red)
- writeCentered(term, cy, "-- Jammed! Check output -- ")
- elseif state == "idle" then
- term.setTextColor(colors.yellow)
- writeCentered(term, cy, "-- Idle --")
- end
- term.setTextColor(colors.white)
- cx = cx + 2
- for k,v in ipairs(items) do
- cy = cy + 1
- term.setCursorPos(cx,cy)
- term.clearLine()
- term.write(k .. ": " .. v)
- end
- cx = 2
- cy = 23
- term.setCursorPos(cx,cy - 2)
- term.write("Current jam ct: ".. jam_count .." ")
- term.setCursorPos(cx,cy)
- term.clearLine()
- term.write("Power remaining: ".. power_in_hours .."h")
- term.redirect(old_disp)
- end
- function writeCentered(p,y,txt)
- p.setCursorPos(math.floor(mon_w / 2) - math.floor(#txt / 2), y)
- p.clearLine()
- p.write(txt)
- end
- function exLoop()
- while true do
- if is_on then
- print("Beginning extraction...")
- jammed = false
- while is_on do
- local current_item = notEmpty()
- items = getItemNameList(ex)
- if current_item ~= false and jammed == false then
- updateDisplay("ex", items)
- for _,stage in pairs(stages) do
- sanityCheck()
- cvt.setRatio(stage.ratio)
- coil.setTorque(stage.coil)
- sleep(stage.t)
- end
- else
- if jammed == true then
- updateDisplay("jam", items)
- else
- updateDisplay("idle", items)
- end
- if auto_on then
- sanityCheck()
- coil.setTorque(0)
- sleep(8)
- else
- is_on = false
- buttonAPI.drawButton(mon,2,2,16,7,colors.black,colors.lime,"Start")
- end
- end
- end
- sanityCheck()
- coil.setTorque(0)
- print("Ceasing extraction.")
- else -- is_on == false
- sleep(2)
- end
- end
- end
- function uiLoop()
- while true do
- sanityCheck()
- local event, _, x, y = os.pullEvent("monitor_touch")
- local button_name = buttonAPI.getButton(x,y)
- --print(button_name .. " pressed at " .. x .. "," .. y)
- if button_name == "startstop" then
- if is_on == false then
- buttonAPI.drawButton(mon,2,2,16,7,colors.black,colors.red,"Stop")
- is_on = true
- else
- buttonAPI.drawButton(mon,2,2,16,7,colors.black,colors.lime,"Start")
- is_on = false
- end
- writeState("is_on",is_on)
- elseif button_name == "manualauto" then
- if auto_on == false then
- buttonAPI.drawButton(mon,20,2,16,7,colors.black,colors.yellow,"Automatic")
- auto_on = true
- else
- buttonAPI.drawButton(mon,20,2,16,7,colors.black,colors.blue,"Manual")
- auto_on = false
- end
- writeState("auto_on",auto_on)
- end
- end
- end
- --[[
- While main function is active, if the item in the output slot remains
- unchanged for ~12 seconds or longer, assume the machine has jammed
- and set a variable to stop the main loop.
- --]]
- function jamLoop()
- -- local jam_count = 0 -- moved to global
- local last_item = {}
- local item = nil
- local sleep_time = (len_torque + len_speed) * 1.333
- while true do
- if is_on then
- item = getStackInSlot(ex,9)
- if item == nil then
- item = notEmpty() -- get right-most item other than extra output
- if item == false then
- item = nil
- end
- end
- if item == nil then
- last_item = {}
- jammed = false
- jam_count = 0
- else
- -- comparing the item object directly doesn't work, test count and name instead
- if last_item.display_name == item.display_name and last_item.qty == item.qty then
- jam_count = jam_count + 1
- else
- jam_count = 0
- end
- last_item = item
- if jam_count >= 6 then
- if jammed == false then
- jammed = true
- print("Extractor jammed!")
- end
- else
- jammed = false
- end
- end
- end
- sleep(sleep_time)
- end
- end
- function peripheralLoop()
- while true do
- local event = os.pullEvent()
- if event == "peripheral" then
- peripherals = peripherals + 1
- print("A peripheral has been detached!")
- end
- if event == "peripheral_detach" then
- peripherals = peripherals - 1
- print("A peripheral has been reattached!")
- end
- end
- end
- mon.setTextScale(0.5)
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.clear()
- mon_w, mon_h = mon.getSize()
- -- write(mon_w)
- coil.setSpeed(4096)
- coil.setTorque(0)
- is_on = readState("is_on")
- auto_on = readState("auto_on")
- buttonAPI.drawButton(mon,2,2,16,7,colors.black,is_on and colors.red or colors.lime,is_on and "Stop" or "Start","startstop")
- buttonAPI.drawButton(mon,20,2,16,7,colors.black,auto_on and colors.yellow or colors.blue,auto_on and "Automatic" or "Manual","manualauto")
- jam_count = 0
- items = getItemNameList(ex)
- if (notEmpty() ~= false) then
- updateDisplay("ex", items)
- else
- updateDisplay("idle", items)
- end
- parallel.waitForAll(uiLoop, exLoop, jamLoop, peripheralLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement