Advertisement
alaestor

init

Nov 22nd, 2019
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. local args = {...}
  2. local verbose = args[1] and args[1] or false
  3.  
  4. local typestr_modem  = "modem"
  5. local typestr_reactor = "BigReactors-Reactor"
  6. local typestr_turbine = "BigReactors-Turbine"
  7.  
  8. _sides = {"front","back","right","left","top","bottom"}
  9.  
  10. function err(...)
  11.   printError(...)
  12.   do return end
  13. end
  14.  
  15. function combineTables(a,b)
  16.   local old_num_a = #a
  17.   for n,v in pairs(b) do
  18.     a[#a+1] = v
  19.   end
  20.   assert(#a == old_num_a + #b)
  21.   return a
  22. end
  23.  
  24. function findLocalPeripherals(type, discriminator)
  25.   local r = {}
  26.   for n,side in ipairs(_sides) do
  27.     if (peripheral.isPresent(side) and peripheral.getType(side) == type) then
  28.       if (not discriminator) then
  29.         r[#r+1] = peripheral.wrap(side)
  30.       elseif (discriminator(side)) then
  31.         r[#r+1] = peripheral.wrap(side)
  32.       end
  33.     end
  34.   end
  35.   return r
  36. end
  37.  
  38. function findRemotePeripherals(modem, type, discriminator)
  39.   local r = {}
  40.   for n,name in ipairs(modem.getNamesRemote()) do
  41.     if (modem.getTypeRemote(name) == type) then
  42.       local o = peripheral.wrap(name)
  43.       if (not discriminator) then
  44.         r[#r+1] = o
  45.       elseif (discriminator(o)) then
  46.         r[#r+1] = o
  47.       end
  48.     end
  49.   end
  50.   return r
  51. end
  52.  
  53. function findPeripherals(type, modems, discriminator)
  54.   local r = {}
  55.   r = findLocalPeripherals(type, discriminator)
  56.   for n,modem in ipairs(modems) do
  57.     r = combineTables(r, (findRemotePeripherals(modem, type, discriminator)))
  58.   end
  59.   return r
  60. end
  61.  
  62. local function isConnected(o)
  63.   local r = o.getConnected() == true
  64.   if (verbose and r == false) then
  65.     printError(name, " is not connected! Either the multiblock structure isn't complete, or the modem is glitched.")
  66.   end
  67.   return r
  68. end
  69.  
  70. _modems = findLocalPeripherals(typestr_modem, function(side) return (peripheral.call(side, "isWireless") == false) end)
  71. _reactors = findPeripherals(typestr_reactor, _modems, isConnected)
  72. _turbines = findPeripherals(typestr_turbine, _modems, isConnected)
  73.  
  74. if (verbose) then
  75.   print(#_modems, " modems, ", #_reactors, " reactors, and ", #_turbines, " turbines found.")
  76. end
  77.  
  78. if (#_reactors < 1) then
  79.   if (#_modems < 1) then
  80.     printError("No modems or reactors found!")
  81.   else
  82.     printError("No reactors found!")
  83.   end
  84.   do return end
  85. end
  86.  
  87. _init_has_run = true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement