IronClaymore

BR_turbine_test

Jul 4th, 2014
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.47 KB | None | 0 0
  1. --[[
  2. Program name: Test turbines
  3. Version: v1.01
  4. Programmer: IronClaymore
  5. Company: IC-TECH
  6. Last update: 04/07/2014
  7.  
  8. Description: just a test
  9.  
  10. ]]--
  11. local loopTime = 0.5 -- Time between program loops, in seconds. You probably don't need to alter this value.
  12. -- The following are just empty arrays
  13. local monitorList = {}
  14. local monitorNames = {}
  15. local reactorList = {}
  16. local reactorNames = {}
  17. local turbineList = {}
  18. local turbineNames = {}
  19.  
  20. ----------
  21.  
  22. -- Basic side determination for wrapping
  23. function getDeviceSide(deviceType)
  24.     deviceType = deviceType:lower()
  25.    
  26.     for i, side in pairs(rs.getSides()) do
  27.         if (peripheral.isPresent(side)) then
  28.             if (string.lower(peripheral.getType(side)) == deviceType) then
  29.                 return side;
  30.             end
  31.         end
  32.     end
  33.    
  34.     return nil;
  35. end
  36.  
  37. -- Peripheral wrapping function
  38. function wrapThis(thing, f)
  39.     local wrapped = nil
  40.     while wrapped == nil and f <= 100 do
  41.         wrapped = peripheral.wrap(thing.."_"..f)
  42.         f = f + 1
  43.     end
  44.  
  45.     if wrapped == nil then
  46.         side = getDeviceSide(thing)
  47.         if side ~= nil then
  48.             return peripheral.wrap(side)
  49.         else
  50.             return nil
  51.         end
  52.     else
  53.         return wrapped
  54.     end
  55. end
  56.  
  57.  
  58. print("Initializing program...",1,1);
  59.  
  60. -- Creates a list of all connected devices, whether adjacent to the computer or connected with wired modems
  61. -- Wraps them too, which is nice
  62. local function getDevices(deviceType)
  63.     local deviceName = nil
  64.     local deviceIndex = 1
  65.     local deviceList, deviceNames = {}, {}
  66.     local peripheralList = peripheral.getNames()
  67.     deviceType = deviceType:lower() -- because case matters and it's stupid and causes bugs and I hate it
  68.    
  69.     for peripheralIndex = 1, #peripheralList do
  70.         if (string.lower(peripheral.getType(peripheralList[peripheralIndex])) == deviceType) then
  71.             deviceNames[deviceIndex] = peripheralList[peripheralIndex]
  72.             deviceList[deviceIndex] = peripheral.wrap(peripheralList[peripheralIndex])
  73.             deviceIndex = deviceIndex + 1
  74.         end
  75.     end
  76.    
  77.     return deviceList, deviceNames
  78. end
  79.  
  80. -- Check for and initialise monitors
  81. local function findMonitors()
  82.     monitorList = {}
  83.     monitorList, monitorNames = getDevices("monitor")
  84.     if #monitorList == 0 then
  85.         print("No monitors found. Please connect one and restart the program.")
  86.         sleep(5)
  87.         finished = true
  88.         os.reboot()
  89.     else
  90.         for monitorIndex = 1, #monitorList do
  91.             local monitor = nil
  92.             monitor = monitorList[monitorIndex]
  93.         end
  94.     end
  95. end
  96.  
  97.  
  98. -- Check for and initialise turbines
  99. local function findTurbines()
  100.     turbineList = {}
  101.     turbineList, turbineNames = getDevices("BigReactors-Turbine")
  102.     if #turbineList == 0 then
  103.         print("No turbines found. Please install connect one or more and restart the program.")
  104.         sleep(5)
  105.         finished = true
  106.         os.reboot()
  107.     else
  108.         for turbineIndex = 1, #turbineList do
  109.             local turbine = nil
  110.             turbine = turbineList[turbineIndex]
  111.         end
  112.     end
  113. end
  114.  
  115. -- Print stuff to the centre of a monitor
  116. local function printCentre(str, posY, monitorIndex)
  117.     local monitor = nil
  118.     monitor = monitorList[monitorIndex]
  119.     local width, height = monitor.getSize()
  120.     monitor.setCursorPos(math.floor(width/2) - math.ceil(str:len()/2), posY)
  121.     monitor.write(str)
  122. end
  123.  
  124. -- Print stuff to the left side of a monitor
  125. local function printLeft(str, posY, monitorIndex)
  126.     local monitor = nil
  127.     monitor = monitorList[monitorIndex]
  128.     local width, height = monitor.getSize()
  129.     monitor.setCursorPos(2,posY)
  130.     monitor.write(str)
  131. end
  132.  
  133. -- Print stuff to the right side of a monitor
  134. local function printRight(str, posY, monitorIndex)
  135.     local monitor = nil
  136.     monitor = monitorList[monitorIndex]
  137.     local width = monitor.getSize()
  138.     monitor.setCursorPos(width - str:len()+1, posY)
  139.     monitor.write(str)
  140. end
  141.  
  142. -- Draw solid bar (paintutils only outputs to term, not monitor, which is why it needs an annoying redirect in there)
  143. local function drawBar(startX, startY, endX, endY, colour, monitorIndex)
  144.     local monitor = nil
  145.     monitor = monitorList[monitorIndex]
  146.     term.redirect(monitor)
  147.     paintutils.drawLine(startX, startY, endX, endY, colour)
  148.     monitor.setBackgroundColor(colors.black)
  149.     term.native()
  150. end
  151.  
  152. -- Draw stuff on the bottom part of the monitors, which will be the same for all of them
  153. function drawBottomPart()
  154.     local turbine = nil
  155.     local onlineTurbine = 0
  156.     local totalTurbineRF = 0
  157.    
  158.     local monitor, monitorIndex = nil, 1
  159.     monitor = monitorList[monitorIndex]
  160.    
  161.     for turbineIndex = 1, #turbineList do
  162.         turbine = turbineList[turbineIndex]
  163.         if turbine.getConnected() then
  164.             if turbine.getActive() then
  165.                 onlineTurbine = onlineTurbine + 1
  166.             end
  167.            
  168.             totalTurbineRF = totalTurbineRF + turbine.getEnergyProducedLastTick()
  169.         end
  170.     end
  171.    
  172.     printLeft("Number of Turbines: ", 2, monitorIndex)
  173.     printLeft(onlineTurbine, 3, monitorIndex)
  174.     printLeft("Total power output: ", 4, monitorIndex)
  175.     printLeft(math.floor(totalTurbineRF), 5, monitorIndex)
  176. end
  177.  
  178. -- Main program
  179. function main()
  180.     while not finished do
  181.         findMonitors()
  182.         findTurbines()
  183.         drawBottomPart()
  184.         sleep(loopTime)
  185.     end
  186. end
  187.  
  188. -- Event handler for exiting, editting and debugging
  189. function eventHandler()
  190.     while not finished do
  191.         event, arg1, arg2, arg3 = os.pullEvent()
  192.         if event == "char" and not inManualMode then
  193.             local ch = string.lower(arg1)
  194.             if ch == "q" then
  195.                 finished = true
  196.             elseif ch == "r" then
  197.                 finished = true
  198.                 os.reboot()
  199.             end
  200.         end
  201.     end
  202. end
  203.  
  204. while not finished do
  205.     parallel.waitForAny(eventHandler, main)
  206.     sleep(loopTime)
  207. end
  208.  
  209. -- Returns control to the terminal if the program has an error or whatever
  210. term.clear()
  211. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment