Advertisement
skypop

CC LaserDisplay

Aug 21st, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.95 KB | None | 0 0
  1. -- Laser sentry based mob farm
  2. -- by SukaiPoppuGo
  3. --
  4. -- Display API
  5. --
  6.  
  7.  
  8. --------------------------------------------------
  9. -- const
  10. local screenW, screenH = term.getSize()
  11.  
  12. --------------------------------------------------
  13. -- utils
  14. local function lineFill(text, filler)
  15.     filler = filler or " "
  16.     return string.sub(text..string.rep(filler, screenW), 1, screenW)
  17. end
  18. local function blitFill(text, filler, textColor, backgroundColor)
  19.     return lineFill(text, filler), lineFill(textColor, textColor), lineFill(backgroundColor, backgroundColor)
  20. end
  21. ---------------------------------------
  22. -- timeStr
  23. -- readable duration
  24. local function timeStr(sec)
  25.     local d = math.floor(sec/86400) --24*60*60 sec
  26.     local h = math.floor(sec/3600)%24 -- 60x60 sec
  27.     local m = math.floor(sec/60)%60 -- 60sec
  28.     local s = math.floor(sec%60) --Reste
  29.     local str = ""
  30.     if d>0 then str = d>1 and d.." days " or "1 day " end
  31.     if h>0 then str = str..h.."h" end
  32.     if m>0 then str = str..(m>9 and m or "0"..m) end
  33.     if h==0 then str = m>0 and str..":"..(s>9 and s or "0"..s) or s.."s" end
  34.     return str
  35. end
  36.  
  37. --------------------------------------------------
  38. -- Screen parts
  39. local div = {}
  40.  
  41. -- Program header
  42. div.header = window.create(
  43.     term.current(),-- parent terminal object
  44.     1,  1,         -- x / y pos
  45.     screenW, 1,    -- width / height
  46.     false           -- isVisible
  47. )
  48. -- display running time
  49. div.time  = window.create(
  50.     term.current(),
  51.     1, 2,
  52.     screenW, 1,
  53.     false
  54. )
  55. -- display range params
  56. div.range  = window.create(
  57.     term.current(),
  58.     1, 3,
  59.     screenW, 1,
  60.     false
  61. )
  62. -- Laser sentry logs
  63. div.output = window.create(
  64.     term.current(),
  65.     1, 4,
  66.     screenW, screenH-4,
  67.     false
  68. )
  69. -- State of running program
  70. div.status  = window.create(
  71.     term.current(),
  72.     1, screenH,
  73.     screenW, 1,
  74.     false
  75. )
  76.  
  77. --------------------------------------------------
  78. -- Redirects functions
  79. local wrap = {}
  80. function wrap.print(target, ...)
  81.     local oldTerm = term.redirect(target)
  82.     print(...)
  83.     term.redirect(oldTerm)
  84.     target.redraw()
  85. end
  86. function wrap.printError(target, ...)
  87.     local oldTerm = term.redirect(target)
  88.     printError(...)
  89.     term.redirect(oldTerm)
  90.     target.redraw()
  91. end
  92. function wrap.write(target, ...)
  93.     local oldTerm = term.redirect(target)
  94.     write(...)
  95.     term.redirect(oldTerm)
  96.     target.redraw()
  97. end
  98. --wrap functions
  99. output = {}
  100. function output.print     (...) wrap.print     (div.output, ...) end
  101. function output.printError(...) wrap.printError(div.output, ...) end
  102. function output.write     (...) wrap.write     (div.output, ...) end
  103.  
  104. --------------------------------------------------
  105. -- update display
  106. function update()
  107.     --time update
  108.     div.time.setTextColor(colors.black)
  109.     div.time.setBackgroundColor(colors.lightGray)
  110.     div.time.setCursorPos(1,1)
  111.     div.time.clearLine()
  112.     div.time.write(string.format("running: %s", timeStr(os.clock())))
  113.     --redraw all
  114.     for _,win in pairs(div) do
  115.         win.redraw()
  116.     end
  117. end
  118. -- Add sub-header
  119. function header(extra)
  120.     extra = (extra and tostring(extra)) and " \7 "..tostring(extra) or ""
  121.     div.header.setCursorPos(1,1)
  122.     div.header.blit(blitFill("Laser sentry"..extra, " ", "f", "0"))
  123.     div.header.redraw()
  124. end
  125. -- Change status
  126. function status(str)
  127.     str = (str and tostring(str)) and tostring(str) or type(str)
  128.     div.status.setCursorPos(1,1)
  129.     div.status.blit(blitFill(string.format("status: %s", str), " ", "0", "8"))
  130.     div.status.redraw()
  131. end
  132. --
  133. function range(maxX, minX, maxY, minY, maxZ, minZ)
  134.     div.range.setTextColor(colors.black)
  135.     div.range.setBackgroundColor(colors.lightGray)
  136.     div.range.setCursorPos(1,1)
  137.     div.range.clearLine()
  138.     div.range.write(string.format("Range X:%s~%s, Y:%s~%s, Z:%s~%s",
  139.         math.ceil(maxX), math.floor(minX),
  140.         math.ceil(maxY), math.floor(minY),
  141.         math.ceil(maxZ), math.floor(minZ)
  142.     ))
  143.     --div.range.redraw()
  144. end
  145.  
  146. --------------------------------------------------
  147. -- init display
  148. function init()
  149.     header()
  150.     status("init")
  151.     for _,win in pairs(div) do
  152.         win.setVisible(true)
  153.     end
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement