Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local max = 90 -- pourcentage maximum de stockage d'energie (stop la production d'energie)
- local min = 10 -- pourcentage minimum de stockage d'energie (lance la production d'energie)
- local conversion = {
- J = 4, -- Mekanism: RF multiplier to Joules
- MJ = 40 -- Mekanism: RF multiplier to Mega Joules
- }
- -- utils functions
- function round(val, decimal)
- local exp = decimal and 10^decimal or 1
- return math.ceil(val * exp - 0.5) / exp
- end
- function convertPower(power)
- if power >= 100000000000000 then
- return round(power/1000000000000, 3).." TRF"
- elseif power >= 100000000000 then
- return round(power/1000000000, 3).." GRF"
- elseif power >= 100000000 then
- return round(power/10000000, 3).." MRF"
- elseif power >= 100000 then
- return round(power/1000, 3).." kRF"
- else
- return round(power, 3).." RF"
- end
- end
- function getUnit(type, maxEnergy)
- if type == "cube" then
- if maxEnergy >= 1000000 then
- return "MJ"
- else
- return "J"
- end
- end
- return "RF"
- end
- function convertPowerType(power, type)
- if type == "J" then
- power = power * conversion.J
- elseif type == "MJ" then
- power = power * conversion.MJ
- end
- return power
- end
- function detectDevice(deviceName)
- deviceSide = "none"
- for k,v in pairs(redstone.getSides()) do
- if peripheral.getType(v) == deviceName then
- deviceSide = v
- break
- end
- end
- return(deviceSide)
- end
- function centerText(text, yVal)
- length = string.len(text)
- minus = math.floor(36-length)
- x = math.floor(minus/2)
- monitor.setCursorPos(x+1,yVal)
- monitor.write(text)
- end
- --Sets the BackGround to Gray (Messy! I know!)
- function clearScreen(color)
- monitor.setBackgroundColour(color)
- monitor.clear()
- monitor.setCursorPos(1,1)
- monitor.write(string.rep(" ",36))
- monitor.setCursorPos(1,2)
- monitor.write(string.rep(" ",36))
- monitor.setCursorPos(1,3)
- monitor.write(string.rep(" ",36))
- monitor.setCursorPos(1,4)
- monitor.write(string.rep(" ",36))
- monitor.setCursorPos(1,5)
- monitor.write(string.rep(" ",36))
- monitor.setCursorPos(1,6)
- monitor.write(string.rep(" ",36))
- monitor.setCursorPos(1,7)
- monitor.write(string.rep(" ",36))
- monitor.setCursorPos(1,8)
- monitor.write(string.rep(" ",36))
- monitor.setCursorPos(1,9)
- monitor.write(string.rep(" ",36))
- monitor.setCursorPos(1,10)
- monitor.write(string.rep(" ",36))
- end
- storage = "none"
- storageType = "none"
- storageUnit = "none"
- monitor = "none"
- local peripheralList = peripheral.getNames()
- monitorSize = "0x0"
- ----------------------
- -- Find peripheral
- ----------------------
- capacitor = detectDevice("Elite Energy Cube")
- -- capacitor bank
- if capacitor ~= "none" then
- storage = peripheral.wrap(capacitor)
- storageType = "capacitor"
- print("Capacitor bank on the "..capacitor.." connected.")
- else
- for index=1, #peripheralList do
- -- energy cell
- if string.find(peripheral.getType(peripheralList[index]), "tile_thermalexpansion_cell") then
- storage = peripheral.wrap(peripheralList[index])
- storageType = "cell"
- storageUnit = getUnit("cell")
- print(peripheralList[index].." connected.")
- -- energy cube
- elseif string.find(peripheral.getType(peripheralList[index]), "Energy Cube") then
- storage = peripheral.wrap(peripheralList[index])
- storageType = "cube"
- storageUnit = getUnit("cube", storage.getMaxEnergy())
- print(peripheralList[index].." connected.")
- -- capacitor bank
- elseif string.find(peripheral.getType(peripheralList[index]), "capacitor_bank") then
- storage = peripheral.wrap(peripheralList[index])
- storageType = "capacitor"
- storageUnit = getUnit("capacitor")
- print(peripheralList[index].." connected.")
- end
- end
- if storage == "none" then
- print("No Energy storage found. Halting script!")
- return
- end
- end
- -------------------
- -- Find monitor
- -------------------
- monitorSide = detectDevice("monitor")
- if monitor ~= "none" then
- monitor = peripheral.wrap(monitorSide)
- local w, h = monitor.getSize()
- monitorSize = w.."x"..h
- print("monitor on the "..monitorSide.." connected.")
- else
- for index=1, #peripheralList do
- if peripheral.getType(peripheralList[index]) == "monitor" then
- monitor = peripheral.wrap(monitorSide)
- local w, h = monitor.getSize()
- monitorSize = w.."x"..h
- print("monitor on the "..monitorSide.." connected.")
- end
- end
- if monitor == "none" then
- print("Warning - No monitor attached, continuing without.")
- end
- end
- ----------------
- -- Main code
- ----------------
- redstone.setOutput("back", false)
- -- if monitor is attached, write data on monitor
- if monitor ~= "none" then
- if monitorSize == "36x10" or monitorSize == "18x5" then
- clearScreen(colors.gray)
- monitor.setTextScale(0.5)
- monitor.setCursorPos(1, 1)
- else
- monitor.clear()
- monitor.setBackgroundColour(colors.gray)
- monitor.setCursorPos(1, 4)
- monitor.write(" ON ")
- monitor.setBackgroundColour(colors.green)
- monitor.setCursorPos(5, 4)
- monitor.write(" OFF ")
- monitor.setBackgroundColour(colors.black)
- end
- end
- -- Main loop
- lastEnergy = 0
- while true do
- -- get Storage value
- if storageType == "cube" then
- local current = convertPowerType(storage.getEnergy(), storageUnit)
- info = {
- energy = current,
- maxEnergy = convertPowerType(storage.getMaxEnergy(), storageUnit),
- usage = convertPowerType(storage.getOutput(), storageUnit),
- averageChange = math.floor(current - lastEnergy)
- }
- elseif storageType == "capacitor" then
- info = {
- energy = storage.getEnergyStored("unknown"),
- maxEnergy = storage.getMaxEnergyStored("unknown"),
- usage = 0, -- TODO: Make that
- averageChange = math.floor(storage.getAverageChangePerTick())
- }
- else
- print("NOT SUPPORTED "..storageType)
- return
- end
- -- Get the percentage
- local rawPerc = info.energy / info.maxEnergy
- local percentFull = round(rawPerc * 100, 1)
- local barPerc = math.floor((rawPerc * 34) + 0.5)
- -- if monitor is attached, write data on monitor
- if monitor ~= "none" then
- -- update monitor size
- local w, h = monitor.getSize()
- monitorSize = w.."x"..h
- if monitorSize == "36x10" or monitorSize == "18x5" then
- clearScreen(colors.gray)
- -- Draw the current energy and max energy
- monitor.setCursorPos(1,2)
- monitor.write(string.rep(" ",36))
- monitor.setTextColour(colors.blue)
- centerText("Current Energy: "..convertPower(info.energy), 1)
- monitor.setCursorPos(1,3)
- monitor.write(string.rep(" ",36))
- monitor.setTextColour(colors.white)
- centerText("Of : "..convertPower(info.maxEnergy), 2)
- -- Draw the average change
- monitor.setCursorPos(1,3)
- monitor.write(string.rep(" ",36))
- local avgText = ""
- if info.averageChange > 0 then
- avgText = "+"..convertPower(info.averageChange)
- monitor.setTextColour(colors.green)
- elseif info.averageChange == 0 then
- avgText = "0 RF"
- monitor.setTextColour(colors.black)
- else
- avgText = "-"..convertPower(info.averageChange * (-1))
- monitor.setTextColour(colors.red)
- end
- centerText(avgText.."/t", 3)
- -- Draw the bar
- monitor.setCursorPos(1, 7)
- monitor.write(string.rep(" ", 36))
- if percentFull <= 20 then
- monitor.setTextColour(colors.red)
- elseif percentFull <= 60 then
- monitor.setTextColour(colors.orange)
- else
- monitor.setTextColour(colors.green)
- end
- centerText(percentFull.."% Full", 7)
- --Loading Bar Code Re-Written Much MUCH simpler than before
- monitor.setCursorPos(2, 8)
- monitor.setBackgroundColour(colors.white)
- monitor.write(string.rep(" ", 34))
- monitor.setCursorPos(2, 8)
- monitor.setBackgroundColour(colors.lime)
- monitor.write(string.rep(" ", barPerc))
- monitor.setCursorPos(2, 9)
- monitor.setBackgroundColour(colors.white)
- monitor.write(string.rep(" ", 34))
- monitor.setCursorPos(2, 9)
- monitor.setBackgroundColour(colors.lime)
- monitor.write(string.rep(" ", barPerc))
- else
- clearScreen(colors.gray)
- -- Draw the current energy and max energy
- monitor.setCursorPos(1,2)
- monitor.write(string.rep(" ",36))
- monitor.setTextColour(colors.blue)
- centerText("Current Energy: "..convertPower(info.energy), 1)
- monitor.setCursorPos(1,3)
- monitor.write(string.rep(" ",36))
- monitor.setTextColour(colors.white)
- centerText("Of : "..convertPower(info.maxEnergy), 2)
- end
- end
- -- Toggle engine
- if percentFull > max then
- -- energy level is over max level, turning redstone signal off
- redstone.setOutput("back", false)
- if monitor ~= "none" then
- if monitorSize ~= "36x10" and monitorSize ~= "18x5" then
- monitor.setBackgroundColour(colors.gray)
- monitor.setCursorPos(1, 4)
- monitor.write(" ON ")
- monitor.setBackgroundColour(colors.green)
- monitor.setCursorPos(5, 4)
- monitor.write(" OFF ")
- monitor.setBackgroundColour(colors.black)
- end
- end
- elseif percentFull < min then
- -- energy level is below max level, turning redstone signal on
- redstone.setOutput("back", true)
- if monitor ~= "none" then
- if monitorSize ~= "36x10" and monitorSize ~= "18x5" then
- monitor.setBackgroundColour(colors.green)
- monitor.setCursorPos(1, 4)
- monitor.write(" ON ")
- monitor.setBackgroundColour(colors.gray)
- monitor.setCursorPos(5, 4)
- monitor.write(" OFF ")
- monitor.setBackgroundColour(colors.black)
- end
- end
- end
- -- Update lastEnergy
- lastEnergy = info.energy
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement