NMDanny

Energy Cell Management - Master Computer no API

Apr 15th, 2014
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | None | 0 0
  1. local chan=39701
  2. local monitor=peripheral.wrap("monitor_6")
  3. local modem=peripheral.wrap("left")
  4. modem.open(chan)
  5. os.loadAPI("Button")
  6.  
  7.      -- Quit button
  8.         local quit=Button(12,7,"Quit")
  9.         quit.startRow=3
  10.         quit.startColumn=38
  11.         quit.backgroundColorNormal=colors.red
  12.         quit.backgroundColorPressed=colors.white
  13.         quit.hasBorder=true
  14.         quit.borderColorNormal=colors.white
  15.         quit.borderColorPressed=colors.red
  16.         quit.textColorNormal=colors.white
  17.         quit.textColorPressed=colors.red
  18.         quit.draw(monitor)
  19.        
  20.     -- Setting up variables
  21.        local totalData={0,0,0}
  22.        local writeFormat={[1]={"Maximum Energy Stored: "," RF"},[2]={"Energy Stored: "," RF"},[3]={"Energy Percent: ","%"}} -- each record represents a statistic, each statistic includes a label and a unit
  23.        local reporters={}
  24.        local width,height=monitor.getSize()
  25.        
  26.    
  27.     -- Preparing the monitor for first use 
  28.         monitor.setBackgroundColor(colors.black)
  29.         monitor.setTextColor(colors.white)
  30.         monitor.setTextScale(.5)
  31.         monitor.clear()
  32.    
  33.     -- Typing the monitor's title and colors
  34.        
  35.         monitor.setBackgroundColor(colors.purple)
  36.         monitor.setTextColor(colors.yellow)
  37.         monitor.setCursorPos(1,1)
  38.         monitor.write("Energy Cell Management"..string.rep(" ",width-#"Energy Cell Management"))
  39.        
  40.      -- Resetting the colors
  41.         monitor.setBackgroundColor(colors.black)
  42.         monitor.setTextColor(colors.white)
  43.        
  44.        
  45.        
  46.  
  47. while true do
  48.  
  49.         local totalData={0,0,0}
  50.         local event={os.pullEvent()}
  51.         if event[1]=="monitor_touch" then
  52.                 if quit.clicked(event[3],event[4])==true then
  53.                         monitor.clear()
  54.                         break
  55.                 end
  56.         elseif event[1]=="key" and event[2]==keys.q then
  57.                 monitor.clear()
  58.                 break
  59.         elseif event[1]=="modem_message" and event[3]==chan then
  60.                 local data={}
  61.                 print(event[5]) -- debug purposes
  62.                 for word in string.gmatch(event[5],"[^%s]+") do -- splitting the string into usable data (3 slots)
  63.                         table.insert(data,word)
  64.                 end
  65.                 reporters[data[1]]={[1]=data[2],[2]=data[3],[3]=data[4]} -- data[1] is the reporter's label(and key amongst the reporetrs table), data[2] is energy stored, data[3] is max energy, data[4] is energy percent
  66.         end
  67.        
  68.         local maxLength=#writeFormat[1][1]
  69.         local currentRow=3
  70.         for key,value in pairs(reporters) do -- going over all reporters (at a random order)
  71.                 monitor.setCursorPos(1,currentRow)
  72.                 monitor.write(key..": ")
  73.                 currentRow=currentRow+1
  74.                 for key2,value2 in ipairs(reporters[key]) do -- printing data for each reporter(at a specific order)
  75.                         monitor.setCursorPos(3,currentRow)
  76.                         monitor.write(string.rep(" ",maxLength-#writeFormat[key2][1])..writeFormat[key2][1]..value2..writeFormat[key2][2])
  77.                         totalData[key2]=totalData[key2]+value2
  78.                         currentRow=currentRow+1
  79.                 end
  80.                 currentRow=currentRow+1
  81.         end
  82.        
  83.         -- Calculating and printing total statistics
  84.         totalData[3]=math.floor(100*(totalData[2]/totalData[1])) -- total energy percent
  85.         monitor.setCursorPos(1,currentRow)
  86.         monitor.write("Total Energy Grid Information: ")
  87.         currentRow=currentRow+1
  88.         for i=1,3 do -- going over each statistic
  89.                 monitor.setCursorPos(3,currentRow)
  90.                 monitor.write(string.rep(" ",maxLength-#writeFormat[i][1])..writeFormat[i][1]..totalData[i]..writeFormat[i][2])
  91.                 currentRow=currentRow+1
  92.         end
  93. end
Advertisement
Add Comment
Please, Sign In to add comment