Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local c = require("component")
- local side = require("sides")
- local co = require("computer")
- --Edit these for your i/o locations
- local coolantBox = side.east
- local fuelBox = side.west
- local outputBox = side.north
- local reactorSide = side.south
- --Name of your coolant and fuel
- local coolantType = "gregtech:gt.360k_NaK_Coolantcell"
- local fuelType = "gregtech:gt.reactorMOXQuad"
- local depletedFuelType = "IC2:reactorMOXQuaddepleted"
- --Coolant heat replacement threshold in %
- local heatLevel = 15
- --Don't edit anything past here
- local rs = c.redstone
- local ts = c.transposer
- local gpu = c.gpu
- local coolant = {}
- local fuel = {}
- local nukeDesign = {
- 0001, 2048, 2048, 2048, 0001, 2048, 2048, 0001, 2048,
- 2048, 2048, 0001, 2048, 2048, 2048, 2048, 0001, 2048,
- 0001, 2048, 2048, 2048, 2048, 0001, 2048, 2048, 2048,
- 2048, 2048, 2048, 0001, 2048, 2048, 2048, 2048, 0001,
- 2048, 0001, 2048, 2048, 2048, 2048, 0001, 2048, 2048,
- 2048, 0001, 2048, 2048, 0001, 2048, 2048, 2048, 0001
- }
- local processedDesign = {}
- for i,j in ipairs(nukeDesign) do table.insert(processedDesign, {j, 0}) end
- function tprint (tbl, indent)
- if not indent then indent = 0 end
- local toprint = string.rep(" ", indent) .. "{\r\n"
- indent = indent + 2
- for k, v in pairs(tbl) do
- toprint = toprint .. string.rep(" ", indent)
- if (type(k) == "number") then
- toprint = toprint .. "[" .. k .. "] = "
- elseif (type(k) == "string") then
- toprint = toprint .. k .. "= "
- end
- if (type(v) == "number") then
- toprint = toprint .. v .. ",\r\n"
- elseif (type(v) == "string") then
- toprint = toprint .. "\"" .. v .. "\",\r\n"
- elseif (type(v) == "table") then
- toprint = toprint .. tprint(v, indent + 2) .. ",\r\n"
- else
- toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
- end
- end
- toprint = toprint .. string.rep(" ", indent-2) .. "}"
- return toprint
- end
- function watchdog()
- rs.setOutput(side.down, 15)
- os.sleep(.05)
- rs.setOutput(side.down, 0)
- rs.setOutput(side.west, 0)
- end
- function resetWatchdog()
- rs.setOutput(side.east, 0)
- rs.setOutput(side.west, 15)
- end
- function checkDesign()
- local design = {}
- for i in ts.getAllStacks(side.south) do
- if i.name == coolantType then
- table.insert(design, {1, 100 * i.damage / i.maxDamage})
- elseif i.name == fuelType then
- table.insert(design, {2048, 100 * i.damage / i.maxDamage})
- elseif i.name == depletedFuelType then
- table.insert(design, {4096, 0})
- elseif i.name == nil then
- table.insert(design, {0, 0})
- else
- table.insert(design, {-1, 0})
- end
- end
- return design
- end
- function printDesign(design)
- if #design ~= 54 then
- print("Invalid design!")
- return
- end
- local w, h = gpu.getResolution()
- gpu.set(1, 1, "Durability of components:")
- for j, i in ipairs(design) do
- local text = " "
- i[2] = math.ceil(i[2])
- if i[1] == -1 then --Unknown item
- gpu.setBackground(0xFF0000)
- gpu.setForeground(0x000000)
- text = "NUL"
- elseif i[1] == 0 then --Blank space
- gpu.setBackground(0x1E1E1E)
- gpu.setForeground(0x3C3C3C)
- text = "BLN"
- elseif i[1] == 1 then --Coolant cell
- gpu.setBackground(0x0049FF)
- gpu.setForeground(0xFFFFFF)
- text = getText(i[2])
- elseif i[1] == 2048 then --Fuel
- gpu.setBackground(0x00FF00)
- gpu.setForeground(0x000000)
- text = getText(i[2])
- elseif i[1] == 4096 then --Depleted rod
- gpu.setBackground(0x006D00)
- gpu.setForeground(0xFFFFFF)
- text = "DEP"
- else --Some invalid state
- gpu.setBackground(0xFF00FF)
- end
- gpu.set((((j - 1) % 9) * 3) + 1, (math.ceil(j / 9) + 1), text)
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- end
- end
- function getText(value)
- local text = ""
- local value = 100 - value
- if value == 100 then
- text = "100"
- elseif value < 10 then
- gpu.setForeground(0xFF0000)
- text = "0" .. value .. "%"
- else
- text = value .. "%"
- end
- return text
- end
- function initReactor()
- for i,j in ipairs(processedDesign) do
- if j[1] == 1 then
- table.insert(coolant, i)
- elseif j[1] == 2048 then
- table.insert(fuel, i)
- end
- end
- local w, h = gpu.getResolution()
- gpu.fill(1, 1, w, h, " ")
- end
- function checkCoolant()
- local design = checkDesign()
- local validator = true
- --hotCells = {}
- gpu.set(1, 9, "Coolant Status:")
- for i,j in ipairs(coolant) do
- local text = "BAD" --init to BAD state
- gpu.setBackground(0xFF0000)
- gpu.setForeground(0xFFFFFF)
- k = design[j][1]
- l = 100 - design[j][2]
- if k == 1 and l > heatLevel then --if we have coolant we're happy
- gpu.setBackground(0x00FF00)
- gpu.setForeground(0x000000)
- text = "OK!"
- elseif k == 1 and l <= heatLevel then
- while rs.getInput(side.north) > 0 do
- rs.setOutput(side.east, 0)
- os.sleep(1.1)
- end
- gpu.setBackground(0xFFB600)
- gpu.setForeground(0x000000)
- text = "HOT"
- --table.insert(hotCells, j)
- validator = false --if the cell is too hot don't run reactor
- else
- while rs.getInput(side.north) > 0 do
- rs.setOutput(side.east, 0)
- os.sleep(1.1)
- end
- validator = false --if there's not coolant don't run reactor
- end
- gpu.set((((j - 1) % 9) * 3) + 1, (math.ceil(j / 9) + 9), text)
- end
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- return validator
- end
- function replaceFuel()
- local design = checkDesign()
- local firstValidInputStack = 0
- local firstValidOutputStack = 0
- local fuelAmount = 0
- local counter = 1
- for i in ts.getAllStacks(fuelBox) do
- if i.name == fuelType then
- firstValidInputStack = counter
- fuelAmount = i.size
- break
- counter = counter + 1
- end
- end
- local counter = 1
- for i in ts.getAllStacks(outputBox) do
- if i.name == nil then
- firstValidOutputStack = counter
- break
- counter = counter + 1
- end
- end
- if firstValidOutputStack == 0 then
- gpu.set(1, 17, "Output is full! Cannot remove depleted fuel.")
- else
- gpu.set(1, 17, " ")
- end
- if firstValidInputStack == 0 then
- gpu.set(1, 18, "Fuel input is empty! Cannot insert fresh fuel.")
- else
- gpu.set(1, 18, " ")
- end
- for i, j in ipairs(fuel) do
- k = design[j][1]
- if k == 4096 and firstValidOutputStack ~= 0 then
- ts.transferItem(reactorSide, outputBox, 1, j, firstValidOutputStack)
- if firstValidInputStack ~= 0 and fuelAmount > 0 then
- ts.transferItem(fuelBox, reactorSide, 1, firstValidInputStack, j)
- fuelAmount = fuelAmount - 1
- if fuelAmount == 0 then break end
- end
- elseif k == 0 and firstValidInputStack ~= 0 and fuelAmount > 0 then
- ts.transferItem(fuelBox, reactorSide, 1, firstValidInputStack, j)
- fuelAmount = fuelAmount - 1
- if fuelAmount == 0 then break end
- end
- end
- return design
- end
- function replaceCoolant()
- local safe = true
- local design = checkDesign()
- local firstValidOutputStack = 0
- local coolantSlots = {}
- local outputSlots = {}
- local counter = 1
- for i in ts.getAllStacks(outputBox) do
- if i.name == nil then
- table.insert(outputSlots, counter)
- end
- counter = counter + 1
- end
- local counter = 1
- for i in ts.getAllStacks(coolantBox) do
- if i.name == coolantType then
- table.insert(coolantSlots, counter)
- end
- counter = counter + 1
- end
- if #outputSlots == 0 then
- gpu.set(1, 19, "No coolant output available! Reactor may stop.")
- elseif #outputSlots > 0 then
- gpu.set(1, 19, " ")
- end
- if #coolantSlots == 0 then
- gpu.set(1, 20, "No fresh coolant available! Reactor may stop.")
- elseif #coolantSlots > 0 then
- gpu.set(1, 20, " ")
- end
- for i, j in ipairs(coolant) do
- k = design[j][1]
- l = 100 - design[j][2]
- if k == 0 and #coolantSlots > 0 then
- rs.setOutput(side.east, 0)
- if(rs.getInput(side.north) == 0) then
- ts.transferItem(coolantBox, reactorSide, 1, coolantSlots[1], j)
- table.remove(coolantSlots, 1)
- end
- safe = false
- elseif k == 1 and l <= heatLevel and #outputSlots > 0 then
- while rs.getInput(side.north) > 0 do
- rs.setOutput(side.east, 0)
- os.sleep(.2)
- end
- ts.transferItem(reactorSide, outputBox, 1, j, outputSlots[1])
- table.remove(outputSlots, 1)
- safe = false
- end
- end
- return safe
- end
- function freeMem()
- local max = 0
- for i=1,10 do
- max = math.max(max, co.freeMemory())
- os.sleep(0)
- end
- gpu.set(32, 6, "Memory:")
- gpu.set(32, 7, tostring(max))
- end
- gpu.setResolution(50, 20)
- initReactor()
- replaceCoolant()
- checkCoolant()
- replaceFuel()
- printDesign(checkDesign())
- resetWatchdog()
- while true do
- local shouldReactorRun = false
- a = checkCoolant()
- b = replaceCoolant()
- shouldReactorRun = a and b
- local design = replaceFuel()
- printDesign(design)
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- gpu.set(32, 1, "Status:")
- if rs.getInput(side.top) == 0 then
- shouldReactorRun = false
- gpu.setBackground(0XFFFF00)
- gpu.set(32, 3, " ")
- gpu.setBackground(0xFF0000)
- gpu.setForeground(0x000000)
- gpu.set(34, 3, "ESTOP")
- end
- if shouldReactorRun then
- rs.setOutput(side.east, 15)
- gpu.setBackground(0x00FF00)
- gpu.setForeground(0x000000)
- gpu.set(32, 2, " WORKING ")
- gpu.setBackground(0x000000)
- gpu.set(32, 3, " ")
- else
- rs.setOutput(side.east, 0)
- gpu.setBackground(0xFF0000)
- gpu.setForeground(0x000000)
- gpu.set(32, 2, " STOPPED ")
- end
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- watchdog()
- if (rs.getInput(side.west)) == 0 then
- resetWatchdog()
- else
- os.sleep(.05)
- end
- freeMem()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement