NielsUtrecht

base-main

Sep 7th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.32 KB | None | 0 0
  1. local confModemSide = "right"
  2. local confMonitorSide = "bottom"
  3. local confModemPort = 1000
  4. local confTankModemPort = 1001
  5. local confAeModemPort = 1002
  6. local confCellModemPort = 1003
  7. local confTextColor = colors.white
  8. local confRedstone = {}
  9.  
  10. confRedstone["Essence"] = {0.5,0.99,true}
  11.  
  12. local tankData = nil
  13. local aeData = nil
  14. local cellData = nil
  15. local lastScreenRefresh = 0
  16.  
  17. if(not os.loadAPI("hydraApi")) then
  18.     error("Could not load hydraApi")
  19. end
  20.  
  21. function formatPercent(fraction)
  22.     percent = math.floor(fraction * 1000) / 10
  23.     return string.format("%3.1f", percent) .. "%"
  24. end
  25.  
  26. function formatLiquidAmount(amount)
  27.     local postfix
  28.  
  29.     if(amount >= 1000000000) then
  30.         amount = math.floor(amount / 100000000) / 10
  31.         postFix = "MB" 
  32.     elseif(amount >= 1000000) then
  33.         amount = math.floor(amount / 100000) / 10
  34.         postFix = "kB"
  35.     elseif(amount >= 1000) then
  36.         amount = math.floor(amount / 100) / 10
  37.         postFix = "B"
  38.     else
  39.         postFix = "mB"     
  40.     end
  41.    
  42.     return string.format("%3.1f", amount) .. postFix
  43. end
  44.  
  45. function getFractionColor(fraction)
  46.     if(fraction >= 0.9) then
  47.         return colors.white
  48.     elseif(fraction >= 0.5) then
  49.         return colors.orange
  50.     else
  51.         return colors.red
  52.     end
  53. end
  54.  
  55. function handleMessage(modemSide, senderChannel, replyChannel, message, senderDistance)
  56.     if(replyChannel == confTankModemPort) then
  57.         tankData = message
  58.     elseif(replyChannel == confAeModemPort) then
  59.         aeData = message
  60.     elseif(replyChannel == confCellModemPort) then
  61.         cellData = message
  62.     else
  63.         print("Got message from unknown channel(" .. replyChannel .. "): ".. message)
  64.     end
  65. end
  66.  
  67. local modem = peripheral.wrap(confModemSide)
  68. local monitor = hydraApi.getMonitor()
  69.  
  70. modem.open(confModemPort)
  71. os.startTimer(5)
  72.  
  73. function writeTanks()
  74.     local line = 1
  75.     monitor.setCursorPos(1,line)
  76.     monitor.setTextColor(confTextColor)
  77.     monitor.write("Liquids: ")
  78.     if(tankData == nil) then return end
  79.     for k,tank in pairs(tankData) do
  80.         monitor.setTextColor(confTextColor)
  81.         line=line+1
  82.         monitor.setCursorPos(1,line)
  83.         monitor.write(tostring(tank["name"]))
  84.         monitor.setCursorPos(16,line)
  85.         monitor.write(formatLiquidAmount(tank["amount"]))
  86.         monitor.setCursorPos(24,line)
  87.         monitor.setTextColor(getFractionColor(tank["fraction"]))
  88.         monitor.write(formatPercent(tank["fraction"]))
  89.         monitor.setTextColor(confTextColor)
  90.         monitor.setCursorPos(30,line)
  91.         monitor.write(tank["dir"])
  92.     end
  93. end
  94.  
  95. function writeAe()
  96.     local line = 12
  97.     monitor.setCursorPos(1,line)
  98.     monitor.setTextColor(confTextColor)
  99.     monitor.write("AE status: ")
  100.     if(aeData == nil) then return end
  101.     monitor.setTextColor(confTextColor)
  102.     line=line+1
  103.     monitor.setCursorPos(1,line)
  104.     monitor.write("Items: " .. tostring(aeData["storedItemCount"]))
  105.     line=line+1
  106.     monitor.setCursorPos(1,line)
  107.     local used = tonumber(aeData["usedBytes"])
  108.     local total = tonumber(aeData["totalBytes"])
  109.     local fraction = used / total
  110.     monitor.write("Bytes: " .. hydraApi.formatLargeNumber(used) .. " / " .. hydraApi.formatLargeNumber(total))
  111.     monitor.setCursorPos(24,line)
  112.     monitor.write(formatPercent(fraction))
  113. end
  114.  
  115. function writeCells()
  116.     local line = 16
  117.     monitor.setCursorPos(1,line)
  118.     monitor.setTextColor(confTextColor)
  119.     monitor.write("Energy status: ")
  120.     if(cellData == nil) then return end
  121.     monitor.setTextColor(confTextColor)
  122.     line=line+1
  123.     monitor.setCursorPos(1,line)
  124.     local s = hydraApi.formatEnergy(cellData["totalEnergy"]) .. " / " .. hydraApi.formatEnergy(cellData["totalMaxEnergy"])
  125.     monitor.write(s)
  126.     monitor.setCursorPos(24,line)
  127.     monitor.write(formatPercent(cellData["fraction"])) 
  128. end
  129.  
  130. function writeMisc()
  131.     local line = 19
  132.     local mcTime = textutils.formatTime(os.time(), true)
  133.     monitor.setCursorPos(1,line)
  134.     monitor.write("Day " .. tostring(os.day()) .. " " .. mcTime)   
  135. end
  136.  
  137. function refreshScreen()
  138.     if(os.clock() - lastScreenRefresh < 1) then return end
  139.     lastScreenRefresh = os.clock()
  140.    
  141.     monitor.clear()
  142.     monitor.setTextScale(1.5)
  143.     writeTanks()
  144.     writeAe()
  145.     writeCells()
  146.     writeMisc()
  147. end
  148.  
  149. while true do
  150.     print("Loop")
  151.     local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent()
  152.  
  153.     if(event == "modem_message") then
  154.         handleMessage(modemSide, senderChannel, replyChannel, message, senderDistance)
  155.     elseif(event == "timer") then
  156.         print("Tick!")
  157.         os.startTimer(5)
  158.     else
  159.         print ("Got event type "..event)
  160.     end
  161.     refreshScreen()
  162. end
Advertisement
Add Comment
Please, Sign In to add comment