Advertisement
Masserio1

EngineOS

Sep 27th, 2014
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.74 KB | None | 0 0
  1. --[[
  2.  EngineOS v1.0 by Masserio
  3.   ______             _             ____   _____
  4.  |  ____|           (_)           / __ \ / ____|
  5.  | |__   _ __   __ _ _ _ __   ___| |  | | (___  
  6.  |  __| | '_ \ / _` | | '_ \ / _ \ |  | |\___ \
  7.  | |____| | | | (_| | | | | |  __/ |__| |____) |
  8.  |______|_| |_|\__, |_|_| |_|\___|\____/|_____/
  9.                 __/ |                          
  10.                |___/                            
  11.  
  12.  This is one part in a series of programs, and this will not work as
  13.  intended on it's own. If you're planning on using this program please
  14.  check out the following programs as well, as they all communicate:
  15.  -BatteryOS v1.1 Pastebin: 3xja4QFK (Stable)
  16.    Monitors Redstone Energy Cells and sends status messages
  17.  -BatteryHost v1.1 Pastebin: m2HkbMiJ (Stable)
  18.    The program collecting statuses from BatteryOS and processes it.
  19.  -MonitorOS v1.0 Pastebin: HpFunt8W (Stable)
  20.    Updating statuses on a big screen
  21.  
  22.  Feel free to copy parts or this whole code, and change it as you like!
  23.  
  24. --]]
  25.  
  26. -- Set up some variables to be used throughout the programme --
  27. modem = nil -- Side of the modem
  28. rednetCable = nil -- Side where rednet cable is attached
  29. batteryHostID = nil -- ID of computer running batteryHost
  30. monitorID = nil -- ID of the computer running MonitorOS
  31. updateTimer = 0 -- To not flood MonitorOS or BatteryHost
  32. enginesRunning = 0 -- All engines are off by default
  33. batteryOutput = 0 -- Maximum power output from batteries
  34. oldBatteryOutput = 0 -- Last update value for calculations
  35. batteryStored = 0 -- Estimate of how much power is stored in batteries
  36. oldBatteryStored = 0 -- Last update value for calculations
  37.  
  38. -- Calculate how many engines need to run
  39. function calcEngines()
  40.   engines = 0
  41.   emptyCells = 16 - (batteryOutput / 100)
  42.   print("Empty: "..emptyCells..". Stored: "..batteryStored)
  43.   if emptyCells == 0 then
  44.     if batteryStored <= 26 then
  45.       engines = 8
  46.     elseif batteryStored < 28 then
  47.       engines = 4
  48.     end
  49.   elseif emptyCells == 1 then
  50.     if batteryStored <= 24 then
  51.       engines = 16
  52.     else
  53.       engines = 8
  54.     end
  55.   elseif emptyCells == 2 then
  56.     if batteryStored <= 21 then
  57.       engines = 32
  58.     else
  59.       engines = 12
  60.     end
  61.   elseif emptyCells >= 3 or batteryStored >= 15 then
  62.     engines = 48
  63.   end
  64.   return engines
  65. end
  66.  
  67. -- Start and stop engines running --
  68. function engineControl(amount)
  69.   if amount > enginesRunning then
  70.     if amount == 4 then
  71.       rs.setBundledOutput(rednetCable, colors.yellow)
  72.     elseif amount == 8 then
  73.       rs.setBundledOutput(rednetCable, colors.blue)
  74.     elseif amount == 12 then
  75.       rs.setBundledOutput(rednetCable, colors.pink)
  76.     elseif amount == 16 then
  77.       if enginesRunning == 4 or enginesRunning == 12 then
  78.         rs.setBundledOutput(rednetCable, colors.combine(colors.yellow, colors.pink))
  79.       elseif enginesRunning == 8 then
  80.         rs.setBundledOutput(rednetCable, colors.combine(colors.blue, colors.red))
  81.       else
  82.         rs.setBundledOutput(rednetCable, colors.white)
  83.       end
  84.     elseif amount == 32 then
  85.       if enginesRunning == 16 then
  86.         currentColors = rs.getBundledOutput(rednetCable)
  87.         if currentColors == colors.white then
  88.           rs.setBundledOutput(rednetCable, colors.combine(
  89.           colors.white, colors.blue, colors.red))
  90.         else
  91.           rs.setBundledOutput(rednetCable, colors.combine(currentColors, colors.white))
  92.         end
  93.       elseif enginesRunning == 4 or enginesRunning == 12 then
  94.         rs.setBundledOutput(rednetCable,
  95.           colors.combine(colors.white, colors.yellow, colors.pink))
  96.       else
  97.         rs.setBundledOutput(rednetCable,
  98.           colors.combine(colors.white, colors.blue, colors.red))
  99.       end
  100.     elseif amount == 48 then
  101.       -- Just turn on all colors instead of combining one and one
  102.       rs.setBundledOutput(rednetCable, 65535)
  103.     end
  104.     enginesRunning = amount
  105.   elseif amount == 0 then
  106.     rs.setBundledOutput(rednetCable, 0)
  107.     enginesRunning = amount
  108.     print("Stopped all engines")
  109.   else
  110.     print("Keep engines running")
  111.   end
  112. end
  113.  
  114. -- Check if amount of running engines should be updated --
  115. function updateEngines()
  116.   engines = 0
  117.   if batteryOutput == 1600 and batteryStored == 28.8 then
  118.     oldBatteryStored = batteryStored
  119.     oldBatteryOutput = batteryOutput
  120.     print("All full, stop engines")
  121.   else
  122.     engines = calcEngines()
  123.     oldBatteryStored = batteryStored
  124.     oldBatteryOutput = batteryOutput
  125.     print("New calc: "..engines..". Running: "..enginesRunning)
  126.   end
  127.   engineControl(engines)
  128. end
  129.  
  130. -- Send amount of engines running --
  131. function sendEnginesRunning(times)
  132.   rednet.send(monitorID, enginesRunning)
  133.   ID, msg = rednet.receive(1)
  134.   if ID == monitorID and msg == "Thanks" then
  135.     print("Engines running sent and confirmed")
  136.   elseif ID == nil then
  137.     print("No answer, abort")
  138.   elseif ID ~= compID then
  139.     print("Wrong pc answered, try again")
  140.     sendEnginesRunning(times + 1)
  141.   end
  142. end
  143.  
  144. -- Get the maximum power output --
  145. function getOutput(times)
  146.   print("Getting maximum power output")
  147.   done = false
  148.   rednet.send(batteryHostID, "Output")
  149.   ID, msg = rednet.receive(1)
  150.   if ID == batteryHostID and msg ~= "Calling" then
  151.     batteryOutput = tonumber(msg)
  152.     done = true
  153.     rednet.send(batteryHostID, "Thanks")
  154.     print("Got the output: "..msg)
  155.   elseif times == 4 then
  156.     print("Answer timeout, abort")
  157.   else
  158.     print("Wrong or no answer, try again")
  159.     sleep(1)
  160.     getOutput(times + 1)
  161.   end
  162.   return done
  163. end
  164.  
  165. -- Get the estimate of how much power is stored --
  166. function getStored(times)
  167.   print("Getting stored energy estimate")
  168.   done = false
  169.   rednet.send(batteryHostID, "Power")
  170.   ID, msg = rednet.receive(1)
  171.   if ID == batteryHostID and msg ~= "Calling" then
  172.     batteryStored = tonumber(msg)
  173.     done = true
  174.     rednet.send(batteryHostID, "Thanks")
  175.     print("Got the estimate: "..msg)
  176.   elseif times == 4 then
  177.     print("Answer timeout, abort")
  178.   else
  179.     print("Wrong or no answer, try again")
  180.     sleep(1)
  181.     getStored(times + 1)
  182.   end
  183.   return done
  184. end
  185.  
  186. -- Find the computer running BatteryHost --
  187. function findBatteryHost(times)
  188.   print("Find the computer running BatteryHost")
  189.   done = false
  190.   rednet.broadcast("Host")
  191.   ID, msg = rednet.receive(1)
  192.   if ID ~= nil and msg == "Here" then
  193.     rednet.send(ID, "Engine")
  194.     ID, msg = rednet.receive(1)
  195.     if ID ~= nil and msg == "Thanks" then
  196.       batteryHostID = ID
  197.       done = true
  198.       print("Host computer found and connected with ID: "..ID)
  199.     else
  200.       print("No confirmation, try again")
  201.       findBatteryHost(times + 1)
  202.     end
  203.   elseif times == 10 then
  204.     print("Tried too many times with no luck, is a host set up?")
  205.   else
  206.     print("Host busy, try again in a sec")
  207.     sleep(2)
  208.     findBatteryHost(times + 1)
  209.   end
  210.   return done
  211. end
  212.  
  213. -- Function to detect peripherals --
  214. function detectSides()
  215.   print("Detecting Modem and Rednet cable")
  216.   for i, side in pairs(rs.getSides()) do
  217.     if peripheral.isPresent(side) then
  218.       if peripheral.getType(side) == "modem" then
  219.         print("Modem set to "..side.." side")
  220.         modem = side
  221.       elseif peripheral.getType(side) == "rednet_cable" then
  222.         print("Rednet cable set to "..side.." side")
  223.         rednetCable = side
  224.       end
  225.     end
  226.   end
  227. end
  228.  
  229. -- If it couldn't find BatteryHost or EngineOS pc, give option to input ID manually --
  230. function getInput(missing)
  231.   print("Could not find "..missing..". Input manually or write 'terminate' to exit program")
  232.   input = io.read()
  233.   if input == "terminate" then
  234.     print("Please configure "..missing.." correctly and try again")
  235.     error()
  236.   elseif tonumber(input) ~= nil then
  237.     if missing == "BatteryHost" then
  238.       batteryHostID = math.floor(input)
  239.       print("BatteryHost computer set to ID: "..batteryHostID)
  240.     else
  241.       print("Something went wrong! Number input, but not correct missing statement")
  242.       error()
  243.     end
  244.   else
  245.     print("Input not a number, try again")
  246.     getInput(missing)
  247.   end
  248. end
  249.  
  250. -- Set up the engine controlling computer --
  251. function setup()
  252.   term.clear()
  253.   term.setCursorPos(1, 1)
  254.   print("Running EngineOS v0.1")
  255.   detectSides()
  256.   if modem == nil then
  257.     print("No modem detected, please attach a modem and try again")
  258.     error()
  259.   end
  260.   if rednetCable == nil then
  261.     print("No rednet cable detected, please attach a rednet cable and try again")
  262.     error()
  263.   end
  264.   rednet.open(modem)
  265.   print("Modem open and ready")
  266.   rs.setBundledOutput(rednetCable, 0) -- Set engines to off by default
  267.  
  268.   -- Wait 35 seconds for batteryHost to set up then ask for status
  269.   print("Getting statuses, please wait")
  270.   sleep(35)
  271.   if not findBatteryHost(1) then
  272.     getInput("BatteryHost")
  273.   end
  274.   sleep(1)
  275.   updateTimer = 10 -- Make the main loop update everything
  276.   print("Setup complete")
  277. end
  278.  
  279. -- Connect to monitor computer --
  280. function connect(compID)
  281.   print("Connection request received")
  282.   rednet.send(compID, "Here")
  283.   ID, msg = rednet.receive(1)
  284.   if ID == compID and msg == "Monitor" then
  285.     monitorID = ID
  286.     rednet.send(ID, "Thanks")
  287.     print("Monitor computer found with ID: "..ID)
  288.   else
  289.     print("Wrong computer answered, or no answer at all, abort")
  290.   end
  291. end
  292.  
  293. -- Run setup before the main loop --
  294. setup()
  295.  
  296. while true do
  297.   if updateTimer == 10 then
  298.     sleep(1)
  299.     getStored(1)
  300.     sleep(1)
  301.     getOutput(1)
  302.     updateEngines()
  303.     updateTimer = 0
  304.   else
  305.     ID, msg = rednet.receive(10)
  306.     if msg == "Engine" then
  307.       connect(ID)
  308.     elseif ID == monitorID and msg == "Running" then
  309.       sendEnginesRunning(1)
  310.       updateTimer = 0
  311.     elseif msg == "Reboot" then
  312.       rednet.close(modem)
  313.       os.reboot()
  314.     end
  315.     updateTimer = updateTimer + 1
  316.     print("Idle "..updateTimer)
  317.   end
  318. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement