kd8lvt

KdHUD

Oct 31st, 2021 (edited)
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. os.loadAPI("glass.lua")
  2. os.loadAPI("kdhudconfig.lua")
  3.  
  4. local r = peripheral.find("rsBridge")
  5. local c = peripheral.find("colonyIntegrator")
  6.  
  7. local text = {}
  8.  
  9. function add(str)
  10.     table.insert(text,str)
  11. end
  12.  
  13. function truncate(number)
  14.     if (number == nil or tostring(number) == "nan") then number = "0" end
  15.     number = tostring(number)
  16.     local decimalStart = string.find(number,"[\.]")
  17.     if (decimalStart ~= nil) then
  18.         local tmp = string.sub(number,decimalStart+1,decimalStart+3)
  19.         local tmp2 = string.sub(number,1,decimalStart-1)
  20.         return tonumber(tmp2.."."..tmp)
  21.     end
  22.     return tonumber(number)
  23. end
  24.  
  25. function getRSData()
  26.     local data = {}
  27.     local items = r.listItems()
  28.     local fluids = r.listFluids()
  29.     data["itemMax"] = r.getMaxItemDiskStorage()+r.getMaxItemExternalStorage()
  30.     data["fluidMax"] = r.getMaxFluidDiskStorage()+r.getMaxFluidExternalStorage()
  31.     data["totalItems"] = 0
  32.     data["totalFluids"] = 0
  33.     data["craftJobs"] = 0
  34.     for k,v in ipairs(items) do
  35.         data["totalItems"] = data["totalItems"]+v.amount
  36.         if (r.isItemCraftable(v) and r.isItemCrafting(v.name)) then
  37.             data["craftJobs"] = data["craftJobs"] + 1
  38.         end
  39.     end
  40.     for k,v in ipairs(fluids) do
  41.         data["totalFluids"] = data["totalFluids"]+v.amount
  42.     end
  43.     data["itemFullness"] = truncate((data["totalItems"]/data["itemMax"])*100)
  44.     data["fluidFullness"] = truncate((data["totalFluids"]/data["fluidMax"])*100)
  45.     return data
  46. end
  47.  
  48. function getColonyData()
  49.     local data = {}
  50.     data["underAttack"] = c.isUnderAttack()
  51.     data["population"] = c.amountOfCitizens()
  52.     data["housing"] = c.maxOfCitizens()
  53.     data["visitors"] = c.getVisitors()
  54.     data["happiness"] = c.getHappiness()
  55.     return data
  56. end
  57.  
  58. function main()
  59.     r = peripheral.find("rsBridge")
  60.     c = peripheral.find("colonyIntegrator")
  61.     text = {}
  62.     add("      [ KD  HUD ]")
  63.     if ((c == nil or (not c.isInColony())) and r == nil) then
  64.         add("")
  65.         add("No supported peripherals installed, or they are invalid!")
  66.         add("Add some to get data here!")
  67.     end
  68.     if (c ~= nil and c.isInColony()) then
  69.         local cData = getColonyData()
  70.         add("")
  71.         add(" ----------------")
  72.         add("   [COLONY  DATA]")
  73.         add(" ----------------")
  74.         add(cData.name)
  75.         if (cData.underAttack) then
  76.             add("Under attack!!!")
  77.         end
  78.         add("Population: "..cData.population.."/"..cData.housing)
  79.         add("Average Happiness: "..math.floor(cData.happiness))
  80.         add("Visitor Count: "..tostring(#cData.visitors))
  81.     end
  82.     if (r ~= nil) then
  83.         local rsData = getRSData()
  84.         add("")
  85.         add(" ----------------  ")
  86.         add("  [STORAGE SYSTEM]  ")
  87.         add(" ----------------  ")
  88.         add("")
  89.         add("Fluid Fullness: "..rsData.fluidFullness.."%")
  90.         add("Item Fullness: "..rsData.itemFullness.."%")
  91.         add("Craft Jobs: "..rsData.craftJobs)
  92.     end
  93.    
  94.     local h = glass.text.height(text)
  95.     local w = glass.text.width(text)
  96.    
  97.     glass.shapes.stroke.box(3,3,w+10,h+11,0xBB77EE)
  98.     glass.text.multiline(text,6,6,0xCC88FF)
  99.     glass.render()
  100.     glass.clear()
  101. end
  102.  
  103. while true do
  104.     main()
  105.     sleep(kdhudconfig.updateFrequency)
  106. end
Add Comment
Please, Sign In to add comment