Bmorr

status.lua

May 1st, 2023 (edited)
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.08 KB | None | 0 0
  1. --[[
  2.     pastebin get iCdTXzGZ status.lua
  3. ]]
  4. _example_status = {
  5.     id=000,  -- Integer
  6.     name="Squirtle 013",  -- Name String
  7.     job="miner",  -- Job String
  8.     state="waiting", -- State String
  9.     fuel=1000,  -- Int
  10.     max_fuel=100000, -- Int
  11.     position=vector.new(1, 2, 3),  -- Vector
  12.     orientation=0,  -- 0 <= ori <= 3
  13.     inventory_data = {
  14.         {count=64, name="minecraft:cobblestone"},
  15.         {count=13, name="minecraft:cobblestone"},
  16.         {count=48, name="minecraft:dirt"},
  17.         {count=16, name="minecraft:torch"}
  18.     },
  19. }
  20.  
  21. _status_sizes = {
  22.     [1]={w=16, h=10},
  23.     [2]={w=30, h=15},
  24.     [3]={w=-1, h=-1},
  25. }
  26.  
  27. function moveCursorToNextLine(line_x, lines)
  28.     lines = lines or 1
  29.     local cx, cy = term.getCursorPos()
  30.     term.setCursorPos(line_x, cy + lines)
  31. end
  32.  
  33. function moveCursorRel(rel_x, rel_y)
  34.     local cx, cy = term.getCursorPos()
  35.     term.setCursorPos(cx + rel_x, cy + rel_y)
  36. end
  37.  
  38. function line_printer(line_width, lines)
  39.  
  40.     local first_x, first_y = term.getCursorPos()
  41.  
  42.     local line_format = "%."..string.format("%ds", line_width)
  43.     for i, line in ipairs(lines) do
  44.         term.write(string.format(line_format, line))
  45.         moveCursorToNextLine(first_x)
  46.     end
  47. end
  48.  
  49. function draw_status(status, size, has_border)
  50.     has_border = has_border or true
  51.     --[[
  52.         Status: table
  53.         Size: 0, 3
  54.         Cursor_pos: table(x, y)
  55.     ]]
  56.  
  57.     local start_x, start_y = term.getCursorPos()
  58.  
  59.     local term_width, term_height = term.getSize()
  60.     local x, y = term.getCursorPos()
  61.     local param_size = size
  62.     local size = _status_sizes[size]
  63.    
  64.     term.setBackgroundColor(colors.gray)
  65.     term.setCursorPos(x, y)
  66.     if has_border then
  67.         for draw_y = y, y + size.h - 1 do
  68.             term.setCursorPos(x, draw_y)
  69.             term.write(" ")
  70.             term.setCursorPos(x + size.w - 1, draw_y)
  71.             term.write(" ")
  72.         end
  73.         for draw_x = x, x + size.w - 1 do
  74.             term.setCursorPos(draw_x, y)
  75.             term.write(" ")
  76.             term.setCursorPos(draw_x, y + size.h - 1)
  77.             term.write(" ")
  78.         end
  79.     else
  80.         for draw_y = y, y + size.h do
  81.             for draw_x = x, x + size.w do
  82.                 term.write(" ")
  83.             end
  84.             term.setCursorPos(x, draw_y + 1)
  85.         end
  86.     end
  87.    
  88.     if param_size == 1 then
  89.         term.setCursorPos(start_x + 1, start_y + 1)
  90.         line_printer(size.w - 2, {
  91.             status.name,
  92.             "",
  93.             string.format("x: %3d", status.position.x).."   Inv:",
  94.             string.format("y: %3d     %2d", status.position.y, 16 - #status.inventory_data),
  95.             string.format("z: %3d", status.position.z),
  96.             "",
  97.             string.format("d: %d", status.orientation)
  98.  
  99.         })
  100.  
  101.     end
  102. end
  103.  
  104. function status_test()
  105.     term.clear()
  106.     term.setCursorPos(3, 3)
  107.     draw_status(_example_status, 1)
  108.     term.setCursorPos(1, 8)
  109. end
  110.  
  111. status_test()
  112.  
  113. return {
  114.     _example_status=_example_status,
  115.     _status_sizes=_status_sizes,
  116.     draw_status=draw_status
  117. }
Advertisement
Add Comment
Please, Sign In to add comment