Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Laser sentry based mob farm
- -- by SukaiPoppuGo
- --
- -- Display API
- --
- --------------------------------------------------
- -- const
- local screenW, screenH = term.getSize()
- --------------------------------------------------
- -- utils
- local function lineFill(text, filler)
- filler = filler or " "
- return string.sub(text..string.rep(filler, screenW), 1, screenW)
- end
- local function blitFill(text, filler, textColor, backgroundColor)
- return lineFill(text, filler), lineFill(textColor, textColor), lineFill(backgroundColor, backgroundColor)
- end
- ---------------------------------------
- -- timeStr
- -- readable duration
- local function timeStr(sec)
- local d = math.floor(sec/86400) --24*60*60 sec
- local h = math.floor(sec/3600)%24 -- 60x60 sec
- local m = math.floor(sec/60)%60 -- 60sec
- local s = math.floor(sec%60) --Reste
- local str = ""
- if d>0 then str = d>1 and d.." days " or "1 day " end
- if h>0 then str = str..h.."h" end
- if m>0 then str = str..(m>9 and m or "0"..m) end
- if h==0 then str = m>0 and str..":"..(s>9 and s or "0"..s) or s.."s" end
- return str
- end
- --------------------------------------------------
- -- Screen parts
- local div = {}
- -- Program header
- div.header = window.create(
- term.current(),-- parent terminal object
- 1, 1, -- x / y pos
- screenW, 1, -- width / height
- false -- isVisible
- )
- -- display running time
- div.time = window.create(
- term.current(),
- 1, 2,
- screenW, 1,
- false
- )
- -- display range params
- div.range = window.create(
- term.current(),
- 1, 3,
- screenW, 1,
- false
- )
- -- Laser sentry logs
- div.output = window.create(
- term.current(),
- 1, 4,
- screenW, screenH-4,
- false
- )
- -- State of running program
- div.status = window.create(
- term.current(),
- 1, screenH,
- screenW, 1,
- false
- )
- --------------------------------------------------
- -- Redirects functions
- local wrap = {}
- function wrap.print(target, ...)
- local oldTerm = term.redirect(target)
- print(...)
- term.redirect(oldTerm)
- target.redraw()
- end
- function wrap.printError(target, ...)
- local oldTerm = term.redirect(target)
- printError(...)
- term.redirect(oldTerm)
- target.redraw()
- end
- function wrap.write(target, ...)
- local oldTerm = term.redirect(target)
- write(...)
- term.redirect(oldTerm)
- target.redraw()
- end
- --wrap functions
- output = {}
- function output.print (...) wrap.print (div.output, ...) end
- function output.printError(...) wrap.printError(div.output, ...) end
- function output.write (...) wrap.write (div.output, ...) end
- --------------------------------------------------
- -- update display
- function update()
- --time update
- div.time.setTextColor(colors.black)
- div.time.setBackgroundColor(colors.lightGray)
- div.time.setCursorPos(1,1)
- div.time.clearLine()
- div.time.write(string.format("running: %s", timeStr(os.clock())))
- --redraw all
- for _,win in pairs(div) do
- win.redraw()
- end
- end
- -- Add sub-header
- function header(extra)
- extra = (extra and tostring(extra)) and " \7 "..tostring(extra) or ""
- div.header.setCursorPos(1,1)
- div.header.blit(blitFill("Laser sentry"..extra, " ", "f", "0"))
- div.header.redraw()
- end
- -- Change status
- function status(str)
- str = (str and tostring(str)) and tostring(str) or type(str)
- div.status.setCursorPos(1,1)
- div.status.blit(blitFill(string.format("status: %s", str), " ", "0", "8"))
- div.status.redraw()
- end
- --
- function range(maxX, minX, maxY, minY, maxZ, minZ)
- div.range.setTextColor(colors.black)
- div.range.setBackgroundColor(colors.lightGray)
- div.range.setCursorPos(1,1)
- div.range.clearLine()
- div.range.write(string.format("Range X:%s~%s, Y:%s~%s, Z:%s~%s",
- math.ceil(maxX), math.floor(minX),
- math.ceil(maxY), math.floor(minY),
- math.ceil(maxZ), math.floor(minZ)
- ))
- --div.range.redraw()
- end
- --------------------------------------------------
- -- init display
- function init()
- header()
- status("init")
- for _,win in pairs(div) do
- win.setVisible(true)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement