Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- EngineOS v1.0 by Masserio
- ______ _ ____ _____
- | ____| (_) / __ \ / ____|
- | |__ _ __ __ _ _ _ __ ___| | | | (___
- | __| | '_ \ / _` | | '_ \ / _ \ | | |\___ \
- | |____| | | | (_| | | | | | __/ |__| |____) |
- |______|_| |_|\__, |_|_| |_|\___|\____/|_____/
- __/ |
- |___/
- This is one part in a series of programs, and this will not work as
- intended on it's own. If you're planning on using this program please
- check out the following programs as well, as they all communicate:
- -BatteryOS v1.1 Pastebin: 3xja4QFK (Stable)
- Monitors Redstone Energy Cells and sends status messages
- -BatteryHost v1.1 Pastebin: m2HkbMiJ (Stable)
- The program collecting statuses from BatteryOS and processes it.
- -MonitorOS v1.0 Pastebin: HpFunt8W (Stable)
- Updating statuses on a big screen
- Feel free to copy parts or this whole code, and change it as you like!
- --]]
- -- Set up some variables to be used throughout the programme --
- modem = nil -- Side of the modem
- rednetCable = nil -- Side where rednet cable is attached
- batteryHostID = nil -- ID of computer running batteryHost
- monitorID = nil -- ID of the computer running MonitorOS
- updateTimer = 0 -- To not flood MonitorOS or BatteryHost
- enginesRunning = 0 -- All engines are off by default
- batteryOutput = 0 -- Maximum power output from batteries
- oldBatteryOutput = 0 -- Last update value for calculations
- batteryStored = 0 -- Estimate of how much power is stored in batteries
- oldBatteryStored = 0 -- Last update value for calculations
- -- Calculate how many engines need to run
- function calcEngines()
- engines = 0
- emptyCells = 16 - (batteryOutput / 100)
- print("Empty: "..emptyCells..". Stored: "..batteryStored)
- if emptyCells == 0 then
- if batteryStored <= 26 then
- engines = 8
- elseif batteryStored < 28 then
- engines = 4
- end
- elseif emptyCells == 1 then
- if batteryStored <= 24 then
- engines = 16
- else
- engines = 8
- end
- elseif emptyCells == 2 then
- if batteryStored <= 21 then
- engines = 32
- else
- engines = 12
- end
- elseif emptyCells >= 3 or batteryStored >= 15 then
- engines = 48
- end
- return engines
- end
- -- Start and stop engines running --
- function engineControl(amount)
- if amount > enginesRunning then
- if amount == 4 then
- rs.setBundledOutput(rednetCable, colors.yellow)
- elseif amount == 8 then
- rs.setBundledOutput(rednetCable, colors.blue)
- elseif amount == 12 then
- rs.setBundledOutput(rednetCable, colors.pink)
- elseif amount == 16 then
- if enginesRunning == 4 or enginesRunning == 12 then
- rs.setBundledOutput(rednetCable, colors.combine(colors.yellow, colors.pink))
- elseif enginesRunning == 8 then
- rs.setBundledOutput(rednetCable, colors.combine(colors.blue, colors.red))
- else
- rs.setBundledOutput(rednetCable, colors.white)
- end
- elseif amount == 32 then
- if enginesRunning == 16 then
- currentColors = rs.getBundledOutput(rednetCable)
- if currentColors == colors.white then
- rs.setBundledOutput(rednetCable, colors.combine(
- colors.white, colors.blue, colors.red))
- else
- rs.setBundledOutput(rednetCable, colors.combine(currentColors, colors.white))
- end
- elseif enginesRunning == 4 or enginesRunning == 12 then
- rs.setBundledOutput(rednetCable,
- colors.combine(colors.white, colors.yellow, colors.pink))
- else
- rs.setBundledOutput(rednetCable,
- colors.combine(colors.white, colors.blue, colors.red))
- end
- elseif amount == 48 then
- -- Just turn on all colors instead of combining one and one
- rs.setBundledOutput(rednetCable, 65535)
- end
- enginesRunning = amount
- elseif amount == 0 then
- rs.setBundledOutput(rednetCable, 0)
- enginesRunning = amount
- print("Stopped all engines")
- else
- print("Keep engines running")
- end
- end
- -- Check if amount of running engines should be updated --
- function updateEngines()
- engines = 0
- if batteryOutput == 1600 and batteryStored == 28.8 then
- oldBatteryStored = batteryStored
- oldBatteryOutput = batteryOutput
- print("All full, stop engines")
- else
- engines = calcEngines()
- oldBatteryStored = batteryStored
- oldBatteryOutput = batteryOutput
- print("New calc: "..engines..". Running: "..enginesRunning)
- end
- engineControl(engines)
- end
- -- Send amount of engines running --
- function sendEnginesRunning(times)
- rednet.send(monitorID, enginesRunning)
- ID, msg = rednet.receive(1)
- if ID == monitorID and msg == "Thanks" then
- print("Engines running sent and confirmed")
- elseif ID == nil then
- print("No answer, abort")
- elseif ID ~= compID then
- print("Wrong pc answered, try again")
- sendEnginesRunning(times + 1)
- end
- end
- -- Get the maximum power output --
- function getOutput(times)
- print("Getting maximum power output")
- done = false
- rednet.send(batteryHostID, "Output")
- ID, msg = rednet.receive(1)
- if ID == batteryHostID and msg ~= "Calling" then
- batteryOutput = tonumber(msg)
- done = true
- rednet.send(batteryHostID, "Thanks")
- print("Got the output: "..msg)
- elseif times == 4 then
- print("Answer timeout, abort")
- else
- print("Wrong or no answer, try again")
- sleep(1)
- getOutput(times + 1)
- end
- return done
- end
- -- Get the estimate of how much power is stored --
- function getStored(times)
- print("Getting stored energy estimate")
- done = false
- rednet.send(batteryHostID, "Power")
- ID, msg = rednet.receive(1)
- if ID == batteryHostID and msg ~= "Calling" then
- batteryStored = tonumber(msg)
- done = true
- rednet.send(batteryHostID, "Thanks")
- print("Got the estimate: "..msg)
- elseif times == 4 then
- print("Answer timeout, abort")
- else
- print("Wrong or no answer, try again")
- sleep(1)
- getStored(times + 1)
- end
- return done
- end
- -- Find the computer running BatteryHost --
- function findBatteryHost(times)
- print("Find the computer running BatteryHost")
- done = false
- rednet.broadcast("Host")
- ID, msg = rednet.receive(1)
- if ID ~= nil and msg == "Here" then
- rednet.send(ID, "Engine")
- ID, msg = rednet.receive(1)
- if ID ~= nil and msg == "Thanks" then
- batteryHostID = ID
- done = true
- print("Host computer found and connected with ID: "..ID)
- else
- print("No confirmation, try again")
- findBatteryHost(times + 1)
- end
- elseif times == 10 then
- print("Tried too many times with no luck, is a host set up?")
- else
- print("Host busy, try again in a sec")
- sleep(2)
- findBatteryHost(times + 1)
- end
- return done
- end
- -- Function to detect peripherals --
- function detectSides()
- print("Detecting Modem and Rednet cable")
- for i, side in pairs(rs.getSides()) do
- if peripheral.isPresent(side) then
- if peripheral.getType(side) == "modem" then
- print("Modem set to "..side.." side")
- modem = side
- elseif peripheral.getType(side) == "rednet_cable" then
- print("Rednet cable set to "..side.." side")
- rednetCable = side
- end
- end
- end
- end
- -- If it couldn't find BatteryHost or EngineOS pc, give option to input ID manually --
- function getInput(missing)
- print("Could not find "..missing..". Input manually or write 'terminate' to exit program")
- input = io.read()
- if input == "terminate" then
- print("Please configure "..missing.." correctly and try again")
- error()
- elseif tonumber(input) ~= nil then
- if missing == "BatteryHost" then
- batteryHostID = math.floor(input)
- print("BatteryHost computer set to ID: "..batteryHostID)
- else
- print("Something went wrong! Number input, but not correct missing statement")
- error()
- end
- else
- print("Input not a number, try again")
- getInput(missing)
- end
- end
- -- Set up the engine controlling computer --
- function setup()
- term.clear()
- term.setCursorPos(1, 1)
- print("Running EngineOS v0.1")
- detectSides()
- if modem == nil then
- print("No modem detected, please attach a modem and try again")
- error()
- end
- if rednetCable == nil then
- print("No rednet cable detected, please attach a rednet cable and try again")
- error()
- end
- rednet.open(modem)
- print("Modem open and ready")
- rs.setBundledOutput(rednetCable, 0) -- Set engines to off by default
- -- Wait 35 seconds for batteryHost to set up then ask for status
- print("Getting statuses, please wait")
- sleep(35)
- if not findBatteryHost(1) then
- getInput("BatteryHost")
- end
- sleep(1)
- updateTimer = 10 -- Make the main loop update everything
- print("Setup complete")
- end
- -- Connect to monitor computer --
- function connect(compID)
- print("Connection request received")
- rednet.send(compID, "Here")
- ID, msg = rednet.receive(1)
- if ID == compID and msg == "Monitor" then
- monitorID = ID
- rednet.send(ID, "Thanks")
- print("Monitor computer found with ID: "..ID)
- else
- print("Wrong computer answered, or no answer at all, abort")
- end
- end
- -- Run setup before the main loop --
- setup()
- while true do
- if updateTimer == 10 then
- sleep(1)
- getStored(1)
- sleep(1)
- getOutput(1)
- updateEngines()
- updateTimer = 0
- else
- ID, msg = rednet.receive(10)
- if msg == "Engine" then
- connect(ID)
- elseif ID == monitorID and msg == "Running" then
- sendEnginesRunning(1)
- updateTimer = 0
- elseif msg == "Reboot" then
- rednet.close(modem)
- os.reboot()
- end
- updateTimer = updateTimer + 1
- print("Idle "..updateTimer)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement