Advertisement
se7enek

TPS-Holo-Display

Oct 6th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.54 KB | None | 0 0
  1. --Written by A_Tasty_Peanut
  2. --Config Options########################################################################
  3. --hologram options
  4. local hologramScale = 3 --the size of holo
  5. local rotation = false  --Should the hologram rotate
  6. local rotationRate = 3 --How Fast the sign will rotate
  7.  
  8. --Colors are decimal representation of HEX value
  9. local colorA=16777215 --TPS Words default 16777215 is white
  10. local colorB=65280 --Color of TPS number  
  11. local colorC=65793 --Black Backdrop (allows for double sided sign)
  12. local ColorAdjust = true  --Do you want TPS number to change color based on value?
  13.  
  14. local timeConstant = 2 --how long it waits per measure cycle
  15.  
  16. --######################################################################################
  17.  
  18. local com = require "component"
  19. local fs = require "filesystem"
  20. local keyboard = require "keyboard"
  21. local holo = com.hologram
  22.  
  23. --Intialaization for Hologram projector
  24. local rawHoloTxtBase = ""
  25. local f = io.open("TPSBaseRaw", "r")
  26. rawHoloTxtBase = f:read("*all")
  27. f:close()
  28. holo.clear()
  29. holo.setScale(hologramScale)
  30. holo.setTranslation(0,0.2,0)
  31. holo.setPaletteColor(1, colorA)
  32. holo.setPaletteColor(2, colorB)
  33. holo.setPaletteColor(3, colorC)
  34. holo.setRotation(0,0,0,0)
  35. holo.setRaw(rawHoloTxtBase)
  36. if rotation == true then
  37. holo.setRotationSpeed(rotationRate,0,1,0)
  38. else
  39. holo.setRotationSpeed(0,0,1,0)
  40. end
  41.  
  42. --all* glyphs follow standard minecraft text pixels (*the decimal is a lil diffrent)
  43. --all* glyphs are 7char High by 5char Wide (*the decumal is 7char High by 4char Wide)
  44. local glyphsMath = {
  45. ["0"]=[[
  46.  XXX
  47. X   X
  48. X  XX
  49. X X X
  50. XX  X
  51. X   X
  52.  XXX
  53. ]],
  54. ["1"]=[[
  55.   X  
  56.  XX  
  57.   X  
  58.   X  
  59.   X  
  60.   X  
  61. XXXXX
  62. ]],
  63. ["2"]=[[
  64.  XXX
  65. X   X
  66.     X
  67.   XX
  68.  X  
  69. X   X
  70. XXXXX
  71. ]],
  72. ["3"]=[[
  73.  XXX
  74. X   X
  75.     X
  76.   XX
  77.     X
  78. X   X
  79.  XXX
  80. ]],
  81. ["4"]=[[
  82.    XX
  83.   X X
  84.  X  X
  85. X   X
  86. XXXXX
  87.     X
  88.     X
  89. ]],
  90. ["5"]=[[
  91. XXXXX
  92. X    
  93. XXXX
  94.     X
  95.     X
  96. X   X
  97.  XXX
  98. ]],
  99. ["6"]=[[
  100.   XX
  101.  X  
  102. X    
  103. XXXX
  104. X   X
  105. X   X
  106.  XXX
  107. ]],
  108. ["7"]=[[
  109. XXXXX
  110. X   X
  111.     X
  112.    X
  113.   X  
  114.   X  
  115.   X  
  116. ]],
  117. ["8"]=[[
  118.  XXX
  119. X   X
  120. X   X
  121.  XXX
  122. X   X
  123. X   X
  124.  XXX
  125. ]],
  126. ["9"]=[[
  127.  XXX
  128. X   X
  129. X   X
  130.  XXXX
  131.     X
  132.    X
  133.  XX  
  134. ]],
  135. ["."]=[[
  136.    
  137.    
  138.    
  139.    
  140.    
  141.    
  142.  X  
  143. ]],
  144. }
  145.  
  146. local function time() --returns realworld unix time stamp in miliseconds
  147. local f = io.open("/tmp/timeFile","w")
  148. f:write("test")
  149. f:close()
  150. return(fs.lastModified("/tmp/timeFile"))
  151. end
  152.  
  153. local function cvtToGlyp(txtNum)
  154.     local timeTxt = ""
  155.     for row = 1, 7 do  --text code taken from https://github.com/OpenPrograms/Sangar-Programs/blob/master/holo-text.lua
  156.                        --Simplified a decent bit tho since it doesn't need to do as much
  157.         for col = 1, 5 do
  158.             local singleChar = string.sub(txtNum, col, col)
  159.             local glyph = glyphsMath[singleChar]
  160.             local s = 0
  161.             for _ = 2, row do
  162.                 s = string.find(glyph, "\n", s + 1, true)
  163.                 if not s then
  164.                     break
  165.                 end
  166.             end
  167.             if s then
  168.                 local line = string.sub(glyph, s + 1, (string.find(glyph, "\n", s + 1, true) or 0) - 1)
  169.                 timeTxt = timeTxt .. line .. " "
  170.             end
  171.         end
  172.         timeTxt = timeTxt .. "\n"
  173.     end
  174. return(timeTxt)
  175. end
  176.  
  177. local function getColor(tps) --Uses HSV to decide color
  178.     local H, rP, gP, bP, X = tps*12-120, 0, 0, 0, 0
  179.     --H is hue should range from 0 to 120
  180.     if H<0 then H=0 end --forces greater then 0 but if things get wonky lets Hue go above 120 and turn blue
  181.     X = (1-math.abs((H/60)%2-1))
  182.     if H<60 then
  183.         rP = 1
  184.         gP = X
  185.         bP = 0
  186.     elseif H<120 then
  187.         rP = X
  188.         gP = 1
  189.         bP = 0 
  190.     elseif H<180 then
  191.         rP = 0
  192.         gP = 1
  193.         bP =
  194.     elseif H<240 then
  195.         rP = 0
  196.         gP = X
  197.         bP = 1 
  198.     elseif H<300 then
  199.         rP = X
  200.         gP = 0
  201.         bP = 1
  202.     else
  203.         rP = 1
  204.         gP = 0
  205.         bP = X
  206.     end
  207.     return(math.floor((rP)*255)*65536+math.floor((gP)*255)*256+math.floor((bP)*255))
  208. end
  209.  
  210. local function holoset(x,y,z,value,rawString)
  211.     --nesting for setRaw() is x z y
  212.     --holograms are 48x32x48 X-Y-Z
  213.     --for x=11 z=17 y=31
  214.     --then location in sting is
  215.     --loc = (x-1)*48*32 + (z-1)*32 + y
  216.     return(string.sub(rawString,1,(x-1)*1536+(z-1)*32+y-1)..tostring(value)..string.sub(rawString,(x-1)*1536+(z-1)*32+y+1))
  217. end
  218.  
  219. local function writeToHolo(text, avgTPS)
  220.     local timeGlyph = cvtToGlyp(text)
  221.     local bm = {}
  222.    
  223.     for token in timeGlyph:gmatch("([^\r\n]*)") do
  224.         if token ~= "" then
  225.             table.insert(bm, token)
  226.         end
  227.     end
  228.     local zDepth = 26
  229.     local xStart = 10 --Space from left boundry
  230.     local yStart = 5  --Space from bottom boundry
  231.     local xFront = 0
  232.     local xBack = 0
  233.     local rawString = rawHoloTxtBase --starts fresh with no TPS numbers on rawString
  234.     if ColorAdjust==true then holo.setPaletteColor(2,getColor(avgTPS)) end
  235.     for i=1, 28 do --Each number is 5 wide, and the decimal is 4 wide. Between each character is an additional space.  With 4 numbers, One decimal and 4 padds thats a length of 28
  236.                    --Reads from left to right
  237.         xFront = xStart + i --writes forward (so it decides which x colum it should be writing to)
  238.         xBack = 48-xStart - i --writes backward (so it decides which x colum it should be writing to)
  239.         for j=1, 7 do  --reads from bottom to top
  240.             local y = yStart + j
  241.             if bm[8-j]:sub(i, i) ~= " " then
  242.                 rawString = holoset(xFront, y, zDepth, 2,rawString)
  243.                 rawString = holoset(xBack, y, zDepth-3, 2,rawString)
  244.             end
  245.         end
  246.     end
  247.     holo.setRaw(rawString) --uses setRaw so that you dont get a flickering effect from the hologram as it redraws it pixel by pixel
  248.                            --Also makes drawing on holograms faster since it does less API calls, depends on complexity of scence but can speed up by 200%
  249. end
  250.  
  251. local realTimeOld = 0
  252. local realTimeNew = 0
  253. local realTimeDiff = 0
  254.  
  255. local TPS = {}
  256. local avgTPS = 0
  257. for tSlot=1,10 do
  258.     TPS[tSlot]=0
  259. end
  260.  
  261. print("To exit hold down Ctrl + W\n")
  262. for i=1, math.huge do
  263.     for tSlot = 1, 10 do --main averaging loop that measures individual TPS and puts it into a cycling table location
  264.         realTimeOld = time()
  265.         os.sleep(timeConstant) --waits for an estimated ammount game seconds
  266.         realTimeNew = time()
  267.  
  268.         realTimeDiff = realTimeNew-realTimeOld
  269.        
  270.         TPS[tSlot] = 20000*timeConstant/realTimeDiff
  271.         avgTPS = (TPS[1]+TPS[2]+TPS[3]+TPS[4]+TPS[5]+TPS[6]+TPS[7]+TPS[8]+TPS[9]+TPS[10])/10
  272.         --print("Server is running at\n"..tostring(TPS[tSlot]).." TPS\nAveraged Value is\n"..tostring(avgTPS).." TPS")
  273.         writeToHolo(string.sub(tostring(avgTPS)..".000",1,5),avgTPS) --The .000 is needed because sometimes tps is exactly 20 and writeToHolo() is expecting 5 characters, not 2
  274.        
  275.         if keyboard.isKeyDown(keyboard.keys.w) and keyboard.isControlDown() then
  276.             print("Exiting...")
  277.             holo.clear()
  278.             os.exit()
  279.         end
  280.        
  281.     end
  282. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement