Nike622000

Reactor Reader

Nov 13th, 2019
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.66 KB | None | 0 0
  1. os.loadAPI("ocs/apis/sensor")
  2.  
  3. local monSide = "top"
  4. local senSide = "left"
  5. local senCoords = {}
  6.       senCoords["X"]     = 1685
  7.       senCoords["Y"]     =   37
  8.       senCoords["Z"]     =  540
  9. local reactorCoords = {}
  10.       reactorCoords["X"] = 1684
  11.       reactorCoords["Y"] =   39
  12.       reactorCoords["Z"] =  535
  13. local standardTextCol  = colors.white
  14. local damageValTextCol = colors.orange
  15. local backgroundCol    = colors.lightGray
  16. local gridFrameCol     = colors.white
  17. local gridFillCol      = colors.black
  18.  
  19. local directory    = "reactorIcons"
  20. local scale        =   .5
  21. local columns      =  9
  22. local rows         =  6
  23. local columnWidth  = 14
  24. local rowHeight    = 10
  25. local refreshDelay = 10
  26.  
  27. local m = peripheral.wrap(monSide)
  28. m.setTextScale(.5)
  29. local s = sensor.wrap(senSide)
  30.  
  31. sleep(1)
  32. local xMax, yMax = m.getSize()
  33.  
  34. function JoJa()
  35.   m.setBackgroundColor(colors.gray)
  36.   m.setCursorPos(1, 1)
  37.   for xP = 1, xMax do
  38.     m.write(" ")
  39.   end
  40.   m.setCursorPos(1, 1)
  41.   m.setTextColor(colors.cyan)
  42.   m.write("Jo")
  43.   m.setTextColor(colors.orange)
  44.   m.write("Ja ")
  45.   m.setTextColor(colors.lightGray)
  46.   m.write("Productions")
  47. end
  48.  
  49. function bg(col)
  50.   m.setBackgroundColor(col)
  51.   for yP = 1, yMax do
  52.     m.setCursorPos(1, yP)
  53.     for xP = 1, xMax do
  54.       m.write(" ")
  55.     end
  56.   end
  57. end
  58.  
  59. function drawGrid(xStart, yStart)
  60.   m.setBackgroundColor(gridFrameCol)
  61.   for yP = yStart, yStart+rows*(rowHeight+1), rowHeight+1 do
  62.     m.setCursorPos(xStart, yP)
  63.     for xP = 1, columns*(columnWidth+1) do
  64.       m.write(" ")
  65.     end
  66.   end  
  67.   for xP = xStart, xStart+columns*(columnWidth+1), columnWidth+1 do
  68.     for yP = yStart, yStart+rows*(rowHeight+1) do
  69.       m.setCursorPos(xP, yP)
  70.       m.write(" ")
  71.     end
  72.   end
  73. end
  74.  
  75. function drawImage(xStart, yStart, image)
  76.   for yP, info in pairs(image) do
  77.     for xP, col in pairs(info) do
  78.       if xP <= columnWidth and yP <= rowHeight then
  79.         if col == 0 then
  80.           col = gridFillCol
  81.         end
  82.         m.setBackgroundColor(col)
  83.         m.setCursorPos((xStart-1)+xP, (yStart-1)+yP)
  84.         m.write(" ")
  85.       end
  86.     end
  87.   end
  88. end
  89.  
  90. function getInventory()
  91.   local xP = reactorCoords["X"]-senCoords["X"]
  92.   local yP = reactorCoords["Y"]-senCoords["Y"]
  93.   local zP = reactorCoords["Z"]-senCoords["Z"]
  94.  
  95.   local targets = s.getTargets()
  96.   for target, info in pairs(targets) do
  97.     if  info.Position.X == xP and
  98.         info.Position.Y == yP and
  99.         info.Position.Z == zP then
  100.       details = s.getTargetDetails(target)
  101.       return details.Slots
  102.     end
  103.   end
  104. end
  105.  
  106. function showSymbols(xStart, yStart, setup)
  107.   local xP, yP = xStart+1, yStart+1
  108.   local image = nil
  109.   for slot, info in pairs(setup) do
  110.     if info.Name == "empty" then
  111.       image = paintutils.loadImage(directory ..
  112.                                    "/empty")
  113.     else
  114.       image = paintutils.loadImage(directory ..
  115.                                    "/" .. info.RawName)
  116.     end
  117.     if image == nil then
  118.       image = paintutils.loadImage(directory ..
  119.                                    "/error")
  120.     end
  121.     drawImage(xP, yP, image)
  122.     xP = xP+columnWidth+1
  123.     if math.fmod(slot, columns) == 0 then
  124.       xP, yP = xStart+1, yP+rowHeight+1
  125.     end  
  126.   end
  127. end
  128.  
  129. function showInfo(xStart, yStart, setup)
  130.   local xP, yP = xStart+1, yStart+1
  131.   for slot, info in pairs(setup) do
  132.     local image = paintutils.loadImage(directory ..
  133.                                        "/empty")
  134.     drawImage(xP, yP, image)
  135.     m.setTextColor(standardTextCol)
  136.     m.setCursorPos(xP, yP)
  137.     m.write(tostring(slot))
  138.     m.setCursorPos(xP, yP+1)
  139.     m.write(string.sub(info.Name, 0, columnWidth))
  140.     m.setCursorPos(xP, yP+2)
  141.     local i = 1
  142.     while string.sub(info.Name, columnWidth+i, columnWidth+i) == ' ' do
  143.       i = i+1
  144.     end
  145.     m.write(string.sub(info.Name, columnWidth+i))
  146.     if info.DamageValue ~= nil then
  147.       m.setTextColor(damageValTextCol)
  148.       m.setCursorPos(xP, yP+3)
  149.       m.write(tostring(
  150.               math.floor((1-info.DamageValue/10000)*100)) .. "%")
  151.     end
  152.    
  153.     xP = xP+columnWidth+1
  154.     if math.fmod(slot, columns) == 0 then
  155.       xP, yP = xStart+1, yP+rowHeight+1
  156.     end
  157.   end
  158. end
  159.  
  160. bg(backgroundCol)
  161. JoJa()
  162. drawGrid(15, 5)
  163.  
  164. local refresh = true
  165. local stat = false
  166. local timerId = os.startTimer(refreshDelay)
  167. while true do
  168.   if refresh then
  169.     local inventory = getInventory()
  170.     if stat then
  171.         showInfo(15, 5, inventory)
  172.     else
  173.         showSymbols(15, 5, inventory)
  174.     end
  175.   end
  176.  
  177.   local event, par1 = os.pullEvent()
  178.   refresh = false
  179.   if event == "timer" then
  180.     if par1 == timerId then
  181.         timerId = os.startTimer(refreshDelay)
  182.         refresh = true
  183.     end
  184.   elseif event == "monitor_touch" then
  185.     stat = not stat
  186.     refresh = true
  187.   end
  188. end
Advertisement
Add Comment
Please, Sign In to add comment