Advertisement
Alakazard12

controller

Jan 8th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | None | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local thread = require("thread")
  4.  
  5. local server_channel = 23
  6. local client_channel = 43
  7.  
  8.  
  9. local function wait_for_component(name)
  10.     if component.isAvailable(name) then
  11.         return component.getPrimary(name)
  12.     end
  13.  
  14.     while true do
  15.         local event, component_name = event.pull(math.huge, "component_available")
  16.         if component_name == name then
  17.             return component.getPrimary(name)
  18.         end
  19.     end
  20. end
  21.  
  22. local reactor = wait_for_component("nc_fusion_reactor")
  23. local modem = wait_for_component("modem")
  24.  
  25.  
  26. modem.open(server_channel)
  27. modem.open(client_channel)
  28.  
  29.  
  30.  
  31. local reactor_commands = {
  32.     ["is_processing"] = function()
  33.         return "0"
  34.     end;
  35.  
  36.     ["disable"] = function()
  37.         reactor.deactivate()
  38.     end;
  39.  
  40.     ["enable"] = function()
  41.         reactor.activate()
  42.     end;
  43. }
  44.  
  45. local commands = {
  46.     ["reactor"] = function(subcommand, ...)
  47.         local command = reactor_commands[subcommand]
  48.         if command then
  49.             return command(...)
  50.         else
  51.             print("No reactor subcommand '" .. subcommand .. "'")
  52.         end
  53.     end;
  54. }
  55.  
  56.  
  57. local last_step
  58.  
  59. local function process_message(event, local_address, remote_address, port, distance, message, step)
  60.     if step == last_step then return end
  61.     last_step = step
  62.     if port ~= server_channel then return end
  63.  
  64.     local args = {}
  65.     for i in message:gmatch("[^:]+") do
  66.         table.insert(args, i)
  67.     end
  68.  
  69.     local command = args[1]
  70.     table.remove(args, 1)
  71.  
  72.     local handler = commands[command]
  73.     if handler then
  74.         local response = handler(table.unpack(args))
  75.         if response then
  76.             modem.broadcast(client_channel, "response:" .. response)
  77.             print("Broadcasted response")
  78.         end
  79.     else
  80.         print("No command '" .. command .. "'")
  81.     end
  82. end
  83.  
  84.  
  85.  
  86.  
  87.  
  88. local hit_threshold = false
  89.  
  90. local function check()
  91.     local efficiency = reactor.getEfficiency()
  92.     modem.broadcast(client_channel, "event:reactor:efficiency:" .. tostring(efficiency))
  93.     modem.broadcast(client_channel, "event:reactor:energy_change:" .. tostring(reactor.getEnergyChange()))
  94.     modem.broadcast(client_channel, "event:reactor:process_power:" .. tostring(reactor.getReactorProcessPower()))
  95.  
  96.     if reactor.isProcessing() then
  97.         if efficiency > 95 then
  98.             print("Deactivating")
  99.             reactor.deactivate()
  100.             hit_threshold = true
  101.         end
  102.     else
  103.         if hit_threshold then
  104.             if efficiency < 90 then
  105.                 print("Activating")
  106.                 reactor.activate()
  107.                 hit_threshold = false
  108.             end
  109.         end
  110.     end
  111. end
  112.  
  113.  
  114. function start()
  115.     stop()
  116.  
  117.     messsage_listener = event.listen("modem_message", process_message)
  118.  
  119.     main_thread = thread.create(function()
  120.         while true do
  121.             if reactor.isHotEnough() then
  122.                 print("Starting")
  123.                 reactor.activate()
  124.                 break
  125.             end
  126.             os.sleep(1)
  127.         end
  128.  
  129.         while true do
  130.             os.sleep(1)
  131.             check()
  132.         end
  133.     end)
  134. end
  135.  
  136.  
  137. function stop()
  138.     if messsage_listener then
  139.         event.cancel(messsage_listener)
  140.         messsage_listener = nil
  141.     end
  142.  
  143.     if main_thread then
  144.         main_thread:kill()
  145.         main_thread = nil
  146.     end
  147. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement