Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local computer = require("computer")
- local eventC = require("event")
- local term = require("term")
- local fs = require("filesystem")
- local ser = require("serialization")
- local sides = require("sides")
- local dipole = {}
- local recipe = {}
- source = component.ntm_pa_source
- local torch = component.radio_torch
- local redstone = component.redstone
- local transposer = component.transposer
- local gpu = component.gpu
- local event = {}
- local momentum = 0
- local now_momentum = 0
- local max_moment = 0
- local state = ""
- local radio = "PA_valve-"
- local cfgData = {}
- local cfgPath = "home/cfg.txt"
- local cfgFile
- local rcpPath = "home/pa_recipes.txt"
- local rcpFile
- local mgtPath = "home/pa_dipoles.txt"
- local mgtFile
- local fileArray = {}
- local change_pass = false
- local change_pos = 1
- local selection = 1
- local commit = false
- local execution = false
- local command = ""
- local coolant = {}
- local coolant_pass = false
- local n_digits = 5
- local items = {}
- local cd_max = 5
- local cd_now = cd_max
- local temp = 0.0
- local auto = false
- local inputs = {}
- local transposer_input = sides.west
- local transposer_output1 = sides.north
- local transposer_output2 = sides.south
- local rs_valves = sides.bottom
- local rs_conveyors = sides.top
- function dipole:new (address, max_momentum)
- local this = {}
- this.proxy = component.proxy(address)
- this.momentum = max_momentum
- return this
- end
- function recipe:new (input1, input2, output, momentum)
- local this = {}
- this.input1 = input1
- this.input2 = input2
- this.output = output
- this.momentum = momentum
- return this
- end
- function recipe:check (input1, input2, rp)
- return ((input1 == rp.input1 and input2 == rp.input2) or (input1 == rp.input2 and input2 == rp.input1))
- end
- local function save()
- cfgData = {momentum, auto, transposer_input, transposer_output1, transposer_output2, rs_valves, rs_conveyors}
- cfgFile = fs.open(cfgPath, "w")
- if (cfgFile == nil) then
- return false
- end
- cfgFile:write(ser.serialize(cfgData))
- cfgFile:close()
- return true
- end
- local function clamp(value, max, min)
- return value > max and max or (value < min and min or value)
- end
- local function reset(all)
- commit = false
- execution = false
- auto = auto and (not all)
- source.cancelOperation()
- return
- end
- local function transearch(peripheral, side, rp)
- local index1 = -1
- local index2 = -1
- local size1 = nil
- local size2 = nil
- local inventory = peripheral.getAllStacks(side).getAll()
- local name = ""
- for i=0,#inventory,1 do
- name = (inventory[i].name ~= nil) and inventory[i].name:gsub("hbm:", "") or ""
- if (name == rp.input1) then
- index1 = i
- size1 = inventory[index1].size
- break
- end
- end
- for i=0,#inventory,1 do
- name = (inventory[i].name ~= nil) and inventory[i].name:gsub("hbm:", "") or ""
- if (name == rp.input2 and i ~= index1) then
- index2 = i
- size2 = inventory[index2].size
- break
- end
- end
- return (index1 >= 0 and index2 >= 0 and size1 ~= nil and size2 ~= nil) and {index1+1, index2+1, (size1 < size2) and size1 or size2} or nil
- end
- cfgData = {momentum, auto, transposer_input, transposer_output1, transposer_output2, rs_valves, rs_conveyors}
- if (fs.exists(cfgPath)) then
- cfgFile = fs.open(cfgPath, "r")
- cfgData = ser.unserialize(cfgFile:read(fs.size(cfgPath)*8))
- else
- cfgFile = fs.open(cfgPath, "w")
- cfgFile:write(ser.serialize(cfgData))
- end
- cfgFile:close()
- momentum = cfgData[1]
- auto = cfgData[2]
- transposer_input = cfgData[3]
- transposer_output1 = cfgData[4]
- transposer_output2 = cfgData[5]
- rs_valves = cfgData[6]
- rs_conveyors = cfgData[7]
- local dipoles = {}
- if (fs.exists(mgtPath)) then
- mgtFile = fs.open(mgtPath, "r")
- fileArray = ser.unserialize(mgtFile:read(fs.size(mgtPath)*8))
- for i=1,#fileArray,1 do
- dipoles[i] = dipole:new(fileArray[i][1], fileArray[i][2])
- end
- else
- mgtFile = fs.open(mgtPath, "w")
- fileArray = {}
- mgtFile:write(ser.serialize(fileArray))
- end
- mgtFile:close()
- local recipes = {}
- if (fs.exists(rcpPath)) then
- rcpFile = fs.open(rcpPath, "r")
- fileArray = ser.unserialize(rcpFile:read(fs.size(rcpPath)*8))
- for i=1,#fileArray,1 do
- recipes[i] = recipe:new(fileArray[i][1], fileArray[i][2], fileArray[i][3], fileArray[i][4])
- end
- else
- rcpFile = fs.open(rcpPath, "w")
- fileArray = {}
- rcpFile:write(ser.serialize(fileArray))
- end
- rcpFile:close()
- for i=1,#dipoles,1 do
- if (dipoles[i].momentum > max_moment) then
- max_moment = dipoles[i].momentum
- end
- end
- for i=0,#sides-1,1 do
- redstone.setOutput(sides[sides[i]], 0)
- end
- term.clear()
- gpu.setBackground(0x0024c0)
- gpu.setForeground(0xffffff)
- term.setCursor(2, 2)
- print("Coil Loop")
- term.setCursor(12, 2)
- print("Command ")
- term.setCursor(22, 2)
- print("Countdown")
- term.setCursor(32, 2)
- print("Auto mode")
- term.setCursor(2, 7)
- print("Target ")
- term.setCursor(12, 7)
- print("Momentum ")
- term.setCursor(22, 7)
- print("Status ")
- term.setCursor(2, 12)
- gpu.setBackground(0x00ee00)
- print("> Save <")
- term.setCursor(12, 12)
- gpu.setBackground(0x99aa00)
- print("> Exec <")
- term.setCursor(22, 12)
- gpu.setBackground(0xff0000)
- print("> Canc <")
- term.setCursor(2, 8)
- gpu.setBackground(0xffffff)
- gpu.setForeground(0xff0000)
- --print("↑↑↑↑↑")
- for i=1,n_digits,1 do
- term.write("↑")
- end
- term.setCursor(2, 10)
- gpu.setForeground(0x00bfff)
- --print("↓↓↓↓↓")
- for i=1,n_digits,1 do
- term.write("↓")
- end
- gpu.setForeground(0xffffff)
- gpu.setBackground(0x000000)
- while (true) do
- event = {eventC.pull(1, "touch")}
- now_momentum = source.getMomentum()
- state = source.getState()
- items = {source.getCrafting()}
- if (event[1] == "touch") then
- if (event[4] == 12) then
- if (event[3] >= 2 and event[3] <= 9) then
- save()
- end
- if (event[3] >= 12 and event[3] <= 19) then
- commit = true
- end
- if (event[3] >= 22 and event[3] <= 29) then
- reset(true)
- end
- end
- if (event[4] == 4) then
- if (event[3] >= 32 and event[3] <= 40) then
- auto = not auto
- end
- end
- if (not auto) then
- if (event[4] == 8) then
- change_pos = 1
- change_pass = true
- elseif (event[4] == 10) then
- change_pos = -1
- change_pass = true
- end
- if (change_pass and event[3] >= 2 and event[3] <= 6) then
- change_pass = false
- momentum = momentum + 10^(2 + n_digits - event[3] - 1) * change_pos
- end
- momentum = momentum > max_moment and max_moment or (momentum < 0 and 0 or momentum)
- end
- end
- if (auto and not (commit or execution)) then
- for i=1,#recipes,1 do
- inputs = transearch(transposer, transposer_input, recipes[i])
- if (inputs ~= nil) then
- transposer.transferItem(transposer_input, transposer_output1, inputs[3], inputs[1], 12)
- transposer.transferItem(transposer_input, transposer_output2, inputs[3], inputs[2], 13)
- momentum = recipes[i].momentum
- commit = true
- break
- end
- end
- end
- selection = 1
- for i=1,#dipoles,1 do
- if (momentum > dipoles[i].momentum) then
- selection = i+1
- end
- end
- if (commit) then
- command = "CHK & SET"
- coolant_pass = true
- for i=1,selection,1 do
- coolant = {dipoles[i].proxy.getCoolant()}
- if (coolant[1] < coolant[3] or coolant[1] < coolant[2]/2) then
- coolant_pass = false
- end
- end
- if (coolant_pass) then
- commit = false
- execution = true
- end
- for i=1,#dipoles,1 do
- dipoles[i].proxy.setThreshold((dipoles[i] == dipoles[selection]) and momentum or dipoles[i].momentum)
- end
- elseif (execution) then
- command = "EXECUTING"
- else
- command = "IDLE"
- end
- if (state:find("CRASH") ~= nil) then
- command = "CRASH DET"
- reset(true)
- end
- if (execution and (items[2] <= 0 or items[4] <= 0) and state ~= "RUNNING") then
- if (math.abs(computer.uptime() - temp) > 1.0) then
- temp = computer.uptime()
- cd_now = cd_now - 1
- end
- else
- cd_now = cd_max
- end
- if (cd_now < 0) then
- cd_now = cd_max
- reset(false)
- end
- redstone.setOutput(rs_valves, (commit or execution) and 15 or 0) --valves
- redstone.setOutput(rs_conveyors, execution and 15 or 0) --conveyors
- torch.setChannel(radio .. selection)
- term.setCursor(2, 4)
- print(("%1d"):format(selection))
- term.setCursor(12, 4)
- print(("%-9s"):format(command))
- term.setCursor(2, 9)
- print(("%5d"):format(momentum))
- term.setCursor(12, 9)
- print(("%5d"):format(now_momentum))
- term.setCursor(12, 10)
- print(("%5.1f%1s"):format(clamp(100.0*now_momentum/(momentum == 0 and 1 or momentum), 100.0, 0.0), "%"))
- term.setCursor(22, 9)
- print(("%-14s"):format(state))
- term.setCursor(22, 4)
- print(("%02d | %02d"):format(cd_now, cd_max))
- term.setCursor(32, 4)
- gpu.setForeground(0xffffff)
- gpu.setBackground(auto and 0x00ee00 or 0xee0000)
- print((" %-3s "):format(auto and "ON" or "OFF"))
- gpu.setBackground(0x000000)
- gpu.setForeground(0xffffff)
- end
Advertisement
Add Comment
Please, Sign In to add comment